nothing interesting
[monky] / lua_scripts / graph.lua
1 --[[ GRAPH widget v1.0 by wlourf (31.10.2010)
2         this widget draws some graphs with some effects 
3         http://u-scripts.blogspot.com/2010/10/graph-widget.html
4         
5 To call the script in a conky, use, before TEXT
6         lua_load /path/to/the/script/graph.lua
7         lua_draw_hook_pre main_graph
8 and add one line (blank or not) after TEXT
9
10
11 Parameters are :
12 3 parameters are mandatory
13 name            - the name of the conky variable to display,
14                           for example for {$cpu cpu0}, just write name="cpu"
15 arg                     - the argument of the above variable,
16                           for example for {$cpu cpu1}, just write arg="cpu1"
17                           arg can be a numerical value if name=""
18 max                     - the maximum value the above variable can reach,
19                           for example for {$cpu cpu1}, just write max=100 or less or more
20         
21 Optional parameters:
22 x,y             - coordinates of the bottom-left corner of the graph,
23               relative to the top-left corner of the conky window 
24                           default =  bottom-left corner of the conky window
25 width       - width of the graph, default = 100 pixels
26 height      - height of the graph, default = 20 pixels
27 nb_values   - number of values to display in the graph, default=width 
28               i.e. 1 pixel for 1 value
29 autoscale   - if set to true, calculate the max valeu of the y axis and
30               doesn't use the max parameter above, default=false
31 skew_x      - skew graph around x axis, défaut = 0
32 skew_y      - skew graph around y axis, défaut = 0
33 angle       - angle of rotation of the graph in degress, default = 0
34               i.e. a horizontal graph)
35 inverse     - if set to true, graph are draw from right to left, default=false
36 background  - if set to false, background is not drawn, default=true
37 foreground  - if set to false, foreground is not drawn, default=true
38               foreground = plain graph
39 bg_bd_size  - size of the border of the background, default=0=no border
40 fg_bd_size  - size of the border of the foreground, default=0=no border
41
42
43 Colours tables below are defined into braces :
44 {position in the gradient (0 to 1), colour in hexadecimal, alpha (0 to 1)}
45 example for a single colour table : 
46 {{0,0xFFAA00,1}} position parameter doesn't matter
47 example for a two-colours table : 
48 {{0,0xFFAA00,1},{1,0x00AA00,1}} or {{0.5,0xFFAA00,1},{1,0x00AA00,1}}
49 example for a three-colours table : 
50 {{0,0xFFAA00,1},{0.5,0xFF0000,1},{1,0x00AA00,1}}
51
52 bg_colour       - colour table for background,
53                           default = {{0,0x000000,.5},{1,0xFFFFFF,.5}}
54 fg_colour       - colour table for foreground,
55                           default = {{0,0x00FFFF,1},{1,0x0000FF,1}}
56 bg_bd_colour- colour table for background border,
57                           default = {{1,0xFFFFFF,1}}                      
58 fg_bd_colour- colour table for foreground border,
59                           default = {{1,0xFFFF00,1}}                      
60
61 bg_orientation, bg_bd_orientation, fg_orientation, fg_bd_orientation,
62                 - "orientation" defines the starting point of the gradient,
63                   default="nn"
64                           there are 8 available starting points : 
65                           "nw","nn","ne","ee","se","ss","sw","ww"
66                           (n for north, w for west ...)
67                           theses 8 points are the 4 corners + the 4 middles of graph
68                           so a gradient "nn" will go from "nn" to "ss"
69                           a gradient "nw" will go from "nw" to "se"
70
71
72 v1.0 (31 Oct. 2010) original release
73
74 --      This program is free software; you can redistribute it and/or modify
75 --      it under the terms of the GNU General Public License as published by
76 --      the Free Software Foundation version 3 (GPLv3)
77 --     
78 --      This program is distributed in the hope that it will be useful,
79 --      but WITHOUT ANY WARRANTY; without even the implied warranty of
80 --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81 --      GNU General Public License for more details.
82 --     
83 --      You should have received a copy of the GNU General Public License
84 --      along with this program; if not, write to the Free Software
85 --      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
86 --      MA 02110-1301, USA.
87
88 ]]
89
90 require 'cairo'
91
92 function set_settings()
93         graph_settings={
94             {
95             name="cpu",
96             arg="",
97             max=100,
98       y=85,
99       x=5,
100       autoscale=false,
101       width=215,
102       height=30,
103       nb_values=120,
104       fg_bd_size=2,
105       bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
106       --fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
107       fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFF3300,1},{1,0xFFFFFF,1}},
108       --fg_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
109       fg_colour = { {0,0x00CCFF,0.7},{0.5,0x0000FF,0.7},{1,0x000000,0.1}},
110       foreground=true,
111       fg_orientation="ne",
112       bg_bd_orientation="nn",
113       bg_orientation="ne",
114             },
115             {
116             name="upspeedf",
117             arg="wlan0",
118             max=9999,
119       y=233,
120       x=5,
121       autoscale=true,
122       width=275,
123       height=35,
124       nb_values=120,
125       fg_bd_size=1,
126       bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
127       fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,0.8},{1,0xFFFF00,0.6}},
128       fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
129       foreground=true,
130       inverse=true,
131       DrawMe="${if_empty ${wireless_essid wlan0}}${else}1$endif",
132       bg_orientation="ww",
133             },
134             {
135             name="downspeedf",
136             arg="wlan0",
137             max=9999,
138       y=233,
139       x=525,
140       autoscale=true,
141       width=275,
142       height=35,
143       nb_values=120,
144       fg_bd_size=1,
145       bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
146       fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,0.8},{1,0xFFFF00,0.6}},
147       fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
148       foreground=true,
149       DrawMe="${if_empty ${wireless_essid wlan0}}${else}1$endif",
150       bg_orientation="ee",
151             },
152             {
153             name="upspeedf",
154             arg="gprs0",
155             max=9999,
156       y=230,
157       x=5,
158       autoscale=true,
159       width=275,
160       height=35,
161       nb_values=120,
162       fg_bd_size=1,
163       bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
164       fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
165       fg_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
166       foreground=true,
167       inverse=true,
168       DrawMe="${if_up gprs0}1${else}0$endif",
169       bg_orientation="ww",
170             },
171             {
172             name="downspeedf",
173             arg="gprs0",
174             max=9999,
175       y=230,
176       x=525,
177       autoscale=true,
178       width=275,
179       height=35,
180       nb_values=120,
181       fg_bd_size=1,
182       bg_colour = {{0,0x007AF6,0},{1,0x000000,0}},
183       fg_bd_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
184       fg_colour = { {0,0xFF0000,1},{0.5,0xFFA209,1},{1,0xFFFF00,1}},
185       foreground=true,
186       DrawMe="${if_up gprs0}1${else}0$endif",
187       bg_orientation="ee",
188             },
189             {
190             name="loadavg",
191             arg="1",
192             max=20,
193       y=95,
194       x=372,
195       autoscale=true,
196       width=140,
197       height=36,
198       nb_values=100,
199       fg_bd_size=0,
200       bg_colour = {{0,0x667AF6,0},{1,0x000000,0}},
201       fg_bd_colour =  { {0,0xdd0000,0.7},{0.5,0xdddd00,0.7},{1,0xFFFF00,0.7}},
202       fg_colour = { {0,0x000000,0.7},{0.5,0xdd0000,0.7},{1,0xff0000,0.7}},
203       foreground=true,
204             },
205             {
206             name="to_bytes ${diskio",
207             arg="mmcblk0}",
208             max=2500000,
209       y=187,
210       x=640,
211       autoscale=true,
212       width=160,
213       height=89,
214       nb_values=130,
215       fg_bd_size=0,
216       bg_colour = {{0,0x667AF6,0},{1,0x000000,0}},
217       fg_bd_colour =  { {0,0xdd0000,1},{0.5,0xdddd00,1},{1,0xFFFF00,1}},
218       fg_colour = { {1,0x111111,0.3},{0.5,0x33cc00,0.6},{0,0x00FF00,0.8}},
219       foreground=true,
220       bg_orientation="ne",
221       fg_orientation="ee",
222             },
223     }
224 end
225
226 function check_settings(t)
227     --tables are check only when conky start
228         if t.name==nil and t.arg==nil then 
229                 print ("No input values ... use parameters 'name'" .. 
230                         " with 'arg' or only parameter 'arg' ") 
231                 return 1
232         end
233         if t.max==nil then
234                 print ("No maximum value defined, use 'max'")
235                 print ("for name=" .. t.name .. " with arg=" .. t.arg)
236                 return 1
237         end
238         if t.name==nil then t.name="" end
239         if t.arg==nil then t.arg="" end
240         return 0
241 end
242
243 function conky_main_graph()
244     if conky_window == nil then return end
245     local w=conky_window.width
246     local h=conky_window.height
247     local cs=cairo_xlib_surface_create(conky_window.display, 
248                                         conky_window.drawable, conky_window.visual, w, h)
249     cr=cairo_create(cs)
250     updates=tonumber(conky_parse('${updates}'))
251     --start drawing after "updates_gap" updates
252     --prevent segmentation error for cpu
253     updates_gap=5
254     if updates==1 then    
255         set_settings()
256             flagOK=0
257                         for i in pairs(graph_settings) do
258                                 if graph_settings[i].width==nil then graph_settings[i].width=100 end
259         if graph_settings[i].nb_values==nil then  
260                 graph_settings[i].nb_values= graph_settings[i].width
261         end
262                         --create an empty table to store values
263                         graph_settings[i]["values"]={}
264                         --beginning point
265                         graph_settings[i].beg = graph_settings[i].nb_values
266                         --graph_settings[i].beg = 0
267                         for j =1, graph_settings[i].nb_values do
268                             graph_settings[i].values[j]=0
269                         end
270                   graph_settings[i].flag_init=true
271                   flagOK=flagOK + check_settings(graph_settings[i])
272                   end
273     end
274     if flagOK>0 then 
275         --abort script if error in one of the tables
276         print ("ERROR : Check the graph_setting table")
277         return
278     end
279     --drawing process
280     if updates > updates_gap then
281                 for i in pairs(graph_settings) do
282                         if (graph_settings[i].DrawMe==nil or conky_parse(graph_settings[i].DrawMe) == "1") then 
283                                 --print(graph_settings[i].DrawMe.."= '"..conky_parse(graph_settings[i].DrawMe).."'")
284                                 --return
285                          --cancel fetch value from conky if interface down or whatever
286                         local nb_values=graph_settings[i].nb_values
287                         graph_settings[i].automax=0
288                         for j =1, nb_values do
289                                 if graph_settings[i].values[j+1]==nil then 
290                                     graph_settings[i].values[j+1]=0 
291                                 end
292                                 graph_settings[i].values[j]=graph_settings[i].values[j+1]
293                                 if j==nb_values then
294                                         --store value
295                                         if graph_settings[i].name=="" then
296                                             value=graph_settings[i].arg
297                                         else
298                                         value=tonumber(conky_parse('${' .. 
299                                             graph_settings[i].name .. " " ..
300                                             graph_settings[i].arg ..'}'))
301                         end
302                                         graph_settings[i].values[nb_values]=value
303                                 end
304                                 graph_settings[i].automax=math.max(graph_settings[i].automax,
305                                                                    graph_settings[i].values[j])
306                                 if graph_settings[i].automax == 0 then graph_settings[i].automax = 1 end --should stop weird glitches at beginning when no values reported yet for upspeed or diskio
307                         end
308                         draw_graph(graph_settings[i])
309                         end
310                 end
311     end
312     cairo_destroy(cr)
313     cairo_surface_destroy(cs)
314         updates=nil
315         updates_gap=nil
316 end
317
318 function draw_graph(t)
319     --drawing function
320     local function rgb_to_r_g_b(colour)
321         return ((colour[2] / 0x10000) % 0x100) / 255., ((colour[2] / 0x100) % 0x100) / 255., (colour[2] % 0x100) / 255., colour[3]
322     end
323  
324         local function linear_orientation(o,w,h)
325             --set gradient for bg and bg border
326                 local p
327                 if o=="nn" then
328                         p={w/2,h,w/2,0}
329                 elseif o=="ne" then
330                         p={w,h,0,0}
331                 elseif o=="ww" then
332                         p={0,h/2,w,h/2}
333                 elseif o=="se" then
334                         p={w,0,0,h}
335                 elseif o=="ss" then
336                         p={w/2,0,w/2,h}
337                 elseif o=="ee" then
338                         p={w,h/2,0,h/2}         
339                 elseif o=="sw" then
340                         p={0,0,w,h}
341                 elseif o=="nw" then
342                         p={0,h,w,0}
343                 end
344                 return p
345         end
346
347         local function linear_orientation_inv(o,w,h)
348             --set gradient for fg and fg border
349                 local p
350                 if o=="ss" then
351                         p={w/2,h,w/2,0}
352                 elseif o=="sw" then
353                         p={w,h,0,0}
354                 elseif o=="ee" then
355                         p={0,h/2,w,h/2}
356                 elseif o=="nw" then
357                         p={w,0,0,h}
358                 elseif o=="nn" then
359                         p={w/2,0,w/2,h}
360                 elseif o=="ww" then
361                         p={w,h/2,0,h/2}         
362                 elseif o=="ne" then
363                         p={0,0,w,h}
364                 elseif o=="se" then
365                         p={0,h,w,0}
366                 end
367                 return p
368         end
369         if t.DrawMe~=nil and conky_parse(t.DrawMe) ~= "1" then 
370                 --print(t.DrawMe.."= '"..conky_parse(t.DrawMe).."'")
371                 return
372         --else
373         --      print(t.name)
374         end --cancel drawing for same reason as earlier 
375         --set default values
376         if t.height==nil        then t.height=20 end
377         --checked in previous part : width and nb_values
378         if t.background==nil    then t.background=true end
379         if t.bg_bd_size==nil    then t.bg_bd_size=0 end
380         if t.x==nil                 then t.x=t.bg_bd_size end
381         if t.y==nil                 then t.y=conky_window.height -t.bg_bd_size end
382         if t.bg_colour==nil     then t.bg_colour={{0,0x000000,.5},{1,0xFFFFFF,.5}} end
383         if t.bg_bd_colour==nil  then t.bg_bd_colour={{1,0xFFFFFF,1}} end
384         if t.foreground==nil    then t.foreground=true end
385         if t.fg_colour==nil     then t.fg_colour={{0,0x00FFFF,1},{1,0x0000FF,1}} end
386         if t.fg_bd_size==nil    then t.fg_bd_size=0 end 
387         if t.fg_bd_colour==nil  then t.fg_bd_colour={{1,0xFFFF00,1}} end
388         if t.autoscale==nil     then t.autoscale=false end
389         if t.inverse==nil       then t.inverse=false end
390         if t.angle==nil         then t.angle=0 end
391         if t.bg_bd_orientation==nil then t.bg_bd_orientation="nn" end
392         if t.bg_orientation==nil then t.bg_orientation="nn" end
393         if t.fg_bd_orientation==nil then t.fg_bd_orientation="nn" end
394         if t.fg_orientation==nil then t.fg_orientation="nn" end
395     --check colours tables
396         for i=1, #t.fg_colour do    
397         if #t.fg_colour[i]~=3 then 
398                 print ("error in fg_colour table")
399                 t.fg_colour[i]={1,0x0000FF,1} 
400         end
401     end
402         for i=1, #t.fg_bd_colour do    
403         if #t.fg_bd_colour[i]~=3 then 
404                 print ("error in fg_bd_colour table")
405                 t.fg_bd_colour[i]={1,0x00FF00,1} 
406         end
407     end
408         for i=1, #t.bg_colour do    
409         if #t.bg_colour[i]~=3 then 
410                 print ("error in background color table")
411                 t.bg_colour[i]={1,0xFFFFFF,0.5} 
412         end
413     end    
414         for i=1, #t.bg_bd_colour do    
415         if #t.bg_bd_colour[i]~=3 then 
416                 print ("error in background border color table")
417                 t.bg_bd_colour[i]={1,0xFFFFFF,1} 
418         end
419     end    
420     --calculate skew parameters if needed
421     if t.flag_init then
422             if t.skew_x == nil then 
423                     t.skew_x=0 
424             else
425                     t.skew_x = math.pi*t.skew_x/180     
426             end
427             if t.skew_y == nil then 
428                     t.skew_y=0
429             else
430                     t.skew_y = math.pi*t.skew_y/180     
431             end
432             t.flag_init=false
433         end
434     cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
435     cairo_set_line_join(cr,CAIRO_LINE_JOIN_ROUND)
436     local matrix0 = cairo_matrix_t:create()
437         tolua.takeownership(matrix0)
438     cairo_save(cr)
439     cairo_matrix_init (matrix0, 1,t.skew_y,t.skew_x,1,0,0)
440     cairo_transform(cr,matrix0)
441         local ratio=t.width/t.nb_values
442         cairo_translate(cr,t.x,t.y)
443         cairo_rotate(cr,t.angle*math.pi/180)
444         cairo_scale(cr,1,-1)
445         --background
446         if t.background then
447             local pts=linear_orientation(t.bg_orientation,t.width,t.height)
448                 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
449                 for i=1, #t.bg_colour do
450                         --print ("i",i,t.colour[i][1], rgb_to_r_g_b(t.colour[i]))
451                     cairo_pattern_add_color_stop_rgba (pat, t.bg_colour[i][1], rgb_to_r_g_b(t.bg_colour[i]))
452                 end
453                 cairo_set_source (cr, pat)
454                 cairo_rectangle(cr,0,0,t.width,t.height)        
455                 cairo_fill(cr)
456                 cairo_pattern_destroy(pat)
457         end
458     --autoscale
459     cairo_save(cr)
460         if t.autoscale then
461                 t.max= t.automax*1.1
462         end
463     local scale_x = t.width/(t.nb_values-1)
464         local scale_y = t.height/t.max
465     --define first point of the graph
466         if updates-updates_gap <t.nb_values then 
467                 t.beg = t.beg - 1
468         --next line prevent segmentation error when conky window is redraw 
469                 --quicly when another window "fly" over it
470                 if t.beg<0 then t.beg=0 end
471         else
472                 t.beg=0
473         end
474     if t.inverse then cairo_scale(cr,-1,1)
475     cairo_translate(cr,-t.width,0) end
476         --graph foreground
477         if t.foreground then
478             local pts_fg=linear_orientation_inv(t.fg_orientation,t.width,t.height)
479             local pat = cairo_pattern_create_linear (pts_fg[1],pts_fg[2],pts_fg[3],pts_fg[4])
480                 for i=1,#t.fg_colour,1 do
481                         cairo_pattern_add_color_stop_rgba (pat, 1-t.fg_colour[i][1], rgb_to_r_g_b(t.fg_colour[i]))
482                 end
483                 cairo_set_source (cr, pat)
484                 cairo_move_to(cr,t.beg*scale_x,0)
485                 cairo_line_to(cr,t.beg*scale_x,t.values[t.beg+1]*scale_y)
486                 for i=t.beg, t.nb_values-1 do
487                         cairo_line_to(cr,i*scale_x,t.values[i+1]*scale_y)               
488                 end
489                 cairo_line_to(cr,(t.nb_values-1)*scale_x,0)
490                 cairo_close_path(cr)
491                 cairo_fill(cr)
492                 cairo_pattern_destroy(pat)
493         end
494         --graph_border
495         if t.fg_bd_size>0 then
496         local pts=linear_orientation_inv(t.fg_bd_orientation,t.width,t.height)
497                 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
498                 for i=1,#t.fg_bd_colour,1 do
499                         cairo_pattern_add_color_stop_rgba (pat, 1-t.fg_bd_colour[i][1], rgb_to_r_g_b(t.fg_bd_colour[i]))
500                 end
501                 cairo_set_source (cr, pat)
502                 cairo_move_to(cr,t.beg*scale_x,t.values[t.beg+1]*scale_y)
503                 for i=t.beg, t.nb_values-1 do
504                         cairo_line_to(cr,i*scale_x,t.values[i+1]*scale_y)               
505                 end
506                 cairo_set_line_width(cr,t.fg_bd_size)
507                 cairo_stroke(cr)
508                 cairo_pattern_destroy(pat)
509         end
510         cairo_restore(cr)
511         --background border
512         if t.bg_bd_size>0 then
513         local pts=linear_orientation(t.bg_bd_orientation,t.width,t.height)
514                 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
515                 for i=1, #t.bg_bd_colour do
516                         --print ("i",i,t.colour[i][1], rgb_to_r_g_b(t.colour[i]))
517                     cairo_pattern_add_color_stop_rgba (pat, t.bg_bd_colour[i][1], rgb_to_r_g_b(t.bg_bd_colour[i]))
518                 end
519                 cairo_set_source (cr, pat)
520                 cairo_rectangle(cr,0,0,t.width,t.height)        
521                 cairo_set_line_width(cr,t.bg_bd_size)
522                 cairo_stroke(cr)
523                 cairo_pattern_destroy(pat)
524         end     
525
526         cairo_restore(cr)
527
528 end
529