OSDN Git Service

initial commit
[pacbang-linux/pacbang-settings.git] / skel / .config / conky / clock / pacbang-clock02_rings.lua
1 --[[
2 # Clock Rings by Linux Mint (2012) reEdited by Altin.
3 #
4 # https://github.com/altinukshini/conky_blue
5 #
6 # This script draws percentage meters as rings, and also draws clock hands if you want! 
7 # It is fully customisable; all options are described in the script.
8 # This script is based off a combination of my clock.lua script and my rings.lua script.
9 #
10 # IMPORTANT: If you are using the 'cpu' function, it will cause a segmentation fault if it tries to draw a ring straight away.
11 #                     The if statement on line 324 uses a delay to make sure that this doesn't happen.
12 #                     It calculates the length of the delay by the number of updates since Conky started.
13 #                     Generally, a value of 5s is long enough, so if you update Conky every 1s,
14 #                     use update_num>5 in that if statement (the default).
15 #                     If you only update Conky every 2s, you should change it to update_num>3;
16 #                     conversely if you update Conky every 0.5s, you should use update_num>10.
17 #                     ALSO, if you change your Conky, is it best to use "killall conky; conky" to update it,
18 #                     otherwise the update_num will not be reset and you will get an error.
19 #
20 # To call this script in Conky, use the following in your conkyrc:
21 #
22 #       lua_load ~/.conky/clock01_rings.lua
23 #       lua_draw_hook_pre clock_rings
24 #
25 # Changelog:
26 #   * v1.0 -->  Original release (30.09.2009)
27 #   * v1.1p -->   Jpope edit londonali1010 (05.10.2009)
28 #   * vX 2011mint --> reEdit despot77 (18.02.2011)
29 #   * vX 2012 --> Altin reEdit (22.07.2012)
30 #       * Added weather function (Accu Weather)
31 #       * Added battery monitoring
32 #       * Syslog monitoring
33 #       * Running processes monitoring
34 #       * Rearanged rings
35 #       * Exctra network functions/monitoring
36 #       * Changed Fonts
37 ]]
38
39 settings_table = {
40     {
41         -- Edit this table to customise your rings.
42         -- You can create more rings simply by adding more elements to settings_table.
43         -- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
44         name='time',
45         -- "arg" is the argument to the stat type, e.g. if in Conky you would write ${cpu cpu0}, 'cpu0' would be the argument. If you would not use an argument in the Conky variable, use ''.
46         arg='%I.%M',
47         -- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
48         max=12,
49         -- "bg_colour" is the colour of the base ring.
50         bg_colour=0xffffff,
51         -- "bg_alpha" is the alpha value of the base ring.
52         bg_alpha=0.15,
53         -- "fg_colour" is the colour of the indicator part of the ring.
54         fg_colour=0xB90004,
55         -- "fg_alpha" is the alpha value of the indicator part of the ring.
56         fg_alpha=0.3,
57         -- "x" and "y" are the x and y coordinates of the centre of the ring, relative to the top left corner of the Conky window.
58         x=100, y=175,
59         -- "radius" is the radius of the ring.
60         radius=50,
61         -- "thickness" is the thickness of the ring, centred around the radius.
62         thickness=5,
63         -- "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
64         start_angle=0,
65         -- "end_angle" is the ending angle of the ring, in degrees, clockwise from top. Value can be either positive or negative, but must be larger than start_angle.
66         end_angle=360
67     },
68
69     {
70         name='battery_percent',
71         arg='',
72         max=100,
73         bg_colour=0xffffff,
74         bg_alpha=0.2,
75         fg_colour=0xB90004,
76         fg_alpha=0.8,
77         x=222, y=110,
78         radius=27,
79         thickness=5,
80         start_angle=-90,
81         end_angle=270
82     },
83
84     {
85         name='time',
86         arg='%M.%S',
87         max=60,
88         bg_colour=0xffffff,
89         bg_alpha=0.1,
90         fg_colour=0xB90004,
91         fg_alpha=0.4,
92         x=100, y=175,
93         radius=66,
94         thickness=5,
95         start_angle=0,
96         end_angle=360
97     },
98     {
99         name='time',
100         arg='%S',
101         max=60,
102         bg_colour=0xffffff,
103         bg_alpha=0.1,
104         fg_colour=0xB90004,
105         fg_alpha=0.6,
106         x=100, y=175,
107         radius=72,
108         thickness=5,
109         start_angle=0,
110         end_angle=360
111     },
112     {
113         name='time',
114         arg='%d',
115         max=31,
116         bg_colour=0xffffff,
117         bg_alpha=0.1,
118         fg_colour=0xB90004,
119         fg_alpha=0.8,
120         x=100, y=175,
121         radius=80,
122         thickness=5,
123         start_angle=-90,
124         end_angle=90
125     },
126     {
127         name='time',
128         arg='%m',
129         max=12,
130         bg_colour=0xffffff,
131         bg_alpha=0.1,
132         fg_colour=0xB90004,
133         fg_alpha=1,
134         x=100, y=175,
135         radius=86,
136         thickness=5,
137         start_angle=-90,
138         end_angle=90
139     },
140     {
141         name='cpu',
142         arg='cpu0',
143         max=100,
144         bg_colour=0xffffff,
145         bg_alpha=0.3,
146         fg_colour=0xB90004,
147         fg_alpha=0.8,
148         x=145, y=343,
149         radius=25,
150         thickness=5,
151         start_angle=-90,
152         end_angle=180
153     },
154     {
155         name='cpu',
156         arg='cpu1',
157         max=100,
158         bg_colour=0xffffff,
159         bg_alpha=0.3,
160         fg_colour=0xB90004,
161         fg_alpha=0.8,
162         x=220, y=343,
163         radius=25,
164         thickness=5,
165         start_angle=-90,
166         end_angle=180
167     },
168     {
169         name='fs_used_perc',
170         arg='/',
171         max=100,
172         bg_colour=0xffffff,
173         bg_alpha=0.2,
174         fg_colour=0xB90004,
175         fg_alpha=0.8,
176         x=35, y=597,
177         radius=25,
178         thickness=5,
179         start_angle=-90,
180         end_angle=180
181     },
182     {
183         name='memperc',
184         arg='',
185         max=100,
186         bg_colour=0xffffff,
187         bg_alpha=0.2,
188         fg_colour=0xB90004,
189         fg_alpha=0.8,
190         x=130, y=597,
191         radius=25,
192         thickness=5,
193         start_angle=-90,
194         end_angle=180
195     },
196     {
197         name='swapperc',
198         arg='',
199         max=100,
200         bg_colour=0xffffff,
201         bg_alpha=0.2,
202         fg_colour=0xB90004,
203         fg_alpha=0.8,
204         x=220, y=597,
205         radius=25,
206         thickness=5,
207         start_angle=-90,
208         end_angle=172
209     },
210 }
211
212 -- Use these settings to define the origin and extent of your clock.
213
214 clock_r=65
215
216 -- "clock_x" and "clock_y" are the coordinates of the centre of the clock, in pixels, from the top left of the Conky window.
217
218 clock_x=100
219 clock_y=175
220
221 show_seconds=false -- Change to true if you want the seconds hand
222
223 require 'cairo'
224
225 function rgb_to_r_g_b(colour,alpha)
226     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
227 end
228
229 function window_background(colour,alpha)
230     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
231 end
232
233 function draw_ring(cr,t,pt)
234     local w,h=conky_window.width,conky_window.height
235
236     local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
237     local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
238
239     local angle_0=sa*(2*math.pi/360)-math.pi/2
240     local angle_f=ea*(2*math.pi/360)-math.pi/2
241     local t_arc=t*(angle_f-angle_0)
242
243     -- Draw background ring
244
245     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
246     cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
247     cairo_set_line_width(cr,ring_w)
248     cairo_stroke(cr)
249
250     -- Draw indicator ring
251
252     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
253     cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
254     cairo_stroke(cr)
255 end
256
257 function draw_clock_hands(cr,xc,yc)
258     local secs,mins,hours,secs_arc,mins_arc,hours_arc
259     local xh,yh,xm,ym,xs,ys
260
261     secs=os.date("%S")
262     mins=os.date("%M")
263     hours=os.date("%I")
264
265     secs_arc=(2*math.pi/60)*secs
266     mins_arc=(2*math.pi/60)*mins+secs_arc/60
267     hours_arc=(2*math.pi/12)*hours+mins_arc/12
268
269     -- Draw hour hand
270
271     xh=xc+0.76*clock_r*math.sin(hours_arc)
272     yh=yc-0.72*clock_r*math.cos(hours_arc)
273     cairo_move_to(cr,xc,yc)
274     cairo_line_to(cr,xh,yh)
275
276     cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
277     cairo_set_line_width(cr,5)
278     cairo_set_source_rgba(cr,1.0,1.0,1.0,1.0)
279     cairo_stroke(cr)
280
281     -- Draw minute hand
282
283     xm=xc+0.98*clock_r*math.sin(mins_arc)
284     ym=yc-1.02*clock_r*math.cos(mins_arc)
285     cairo_move_to(cr,xc,yc)
286     cairo_line_to(cr,xm,ym)
287
288     cairo_set_line_width(cr,3)
289     cairo_stroke(cr)
290
291     -- Draw seconds hand
292
293     if show_seconds then
294         xs=xc+1.1*clock_r*math.sin(secs_arc)
295         ys=yc-clock_r*math.cos(secs_arc)
296         cairo_move_to(cr,xc,yc)
297         cairo_line_to(cr,xs,ys)
298
299         cairo_set_line_width(cr,1)
300         cairo_stroke(cr)
301     end
302 end
303
304 function conky_clock_rings()
305     local function setup_rings(cr,pt)
306         local str=''
307         local value=0
308
309         str=string.format('${%s %s}',pt['name'],pt['arg'])
310         str=conky_parse(str)
311
312         value=tonumber(str)
313         pct=value/pt['max']
314
315         draw_ring(cr,pct,pt)
316     end
317
318     -- Check that Conky has been running for at least 5s
319
320     if conky_window==nil then return end
321     local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
322
323     local cr=cairo_create(cs)
324
325     local updates=conky_parse('${updates}')
326     update_num=tonumber(updates)
327
328     if update_num>5 then
329         for i in pairs(settings_table) do
330             setup_rings(cr,settings_table[i])
331         end
332     end
333
334     draw_clock_hands(cr,clock_x,clock_y)
335 end