read some more battery data using new bme.c file but not stable, crashes after 23...
[monky] / data / 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=87,
99                         x=5,
100                         autoscale=false,
101                         width=215,
102                         height=29,
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_up wlan0}1${else}0$endif",
132                         bg_orientation="ww",
133             },
134             {
135                         name="downspeedf",
136                         arg="wlan0",
137                         max=9999,
138                         y=233,
139                         x=575,
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_up wlan0}1${else}0$endif",
150                         bg_orientation="ee",
151             },
152             {
153                         name="upspeedf",
154                         arg="gprs0",
155                         max=9999,
156                         y=233,
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,0.8},{1,0xFFFF00,0.6}},
165                         fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
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=233,
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,0.8},{1,0xFFFF00,0.6}},
184                         fg_colour = { {0,0xFF0000,0.65},{0.5,0xFFA209,0.5},{1,0xFFFF00,0.3}},
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=367,
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=690,
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                         name="battery_rate",
225                         arg="",
226                         max=350,
227                         y=187,
228                         x=500,
229                         autoscale=true,
230                         width=160,
231                         height=45,
232                         nb_values=130,
233                         fg_bd_size=0,
234                         bg_colour = {{0,0x667AF6,0},{1,0x000000,0}},
235                         fg_bd_colour =  { {0,0xdd0000,1},{0.5,0xdddd00,1},{1,0xFFFF00,1}},
236                         fg_colour = { {1,0x111111,0.3},{0.5,0x33cc00,0.6},{0,0x00FF00,0.8}},
237                         foreground=true,
238                         bg_orientation="ne",
239                         fg_orientation="ee",
240             },
241     }
242 end
243
244 function check_settings(t)
245     --tables are check only when conky start
246         if t.name==nil and t.arg==nil then 
247                 print ("No input values ... use parameters 'name'" .. 
248                         " with 'arg' or only parameter 'arg' ") 
249                 return 1
250         end
251         if t.max==nil then
252                 print ("No maximum value defined, use 'max'")
253                 print ("for name=" .. t.name .. " with arg=" .. t.arg)
254                 return 1
255         end
256         if t.name==nil then t.name="" end
257         if t.arg==nil then t.arg="" end
258         return 0
259 end
260
261 function conky_main_graph()
262     if conky_window == nil then return end
263     local w=conky_window.width
264     local h=conky_window.height
265     local cs=cairo_xlib_surface_create(conky_window.display, 
266                                         conky_window.drawable, conky_window.visual, w, h)
267     cr=cairo_create(cs)
268     updates=tonumber(conky_parse('${updates}'))
269     --start drawing after "updates_gap" updates
270     --prevent segmentation error for cpu
271     updates_gap=5
272     if updates==1 then    
273         set_settings()
274             flagOK=0
275                         for i in pairs(graph_settings) do
276                                 if graph_settings[i].width==nil then graph_settings[i].width=100 end
277         if graph_settings[i].nb_values==nil then  
278                 graph_settings[i].nb_values= graph_settings[i].width
279         end
280                         --create an empty table to store values
281                         graph_settings[i]["values"]={}
282                         --beginning point
283                         graph_settings[i].beg = graph_settings[i].nb_values
284                         --graph_settings[i].beg = 0
285                         for j =1, graph_settings[i].nb_values do
286                             graph_settings[i].values[j]=0
287                         end
288                   graph_settings[i].flag_init=true
289                   flagOK=flagOK + check_settings(graph_settings[i])
290                   end
291     end
292     if flagOK>0 then 
293         --abort script if error in one of the tables
294         print ("ERROR : Check the graph_setting table")
295         return
296     end
297     --drawing process
298     if updates > updates_gap then
299                 for i in pairs(graph_settings) do
300                         if (graph_settings[i].DrawMe==nil or conky_parse(graph_settings[i].DrawMe) == "1") then 
301                                 --print(graph_settings[i].DrawMe.."= '"..conky_parse(graph_settings[i].DrawMe).."'")
302                                 --return
303                          --cancel fetch value from conky if interface down or whatever
304                         local nb_values=graph_settings[i].nb_values
305                         graph_settings[i].automax=0
306                         for j =1, nb_values do
307                                 if graph_settings[i].values[j+1]==nil then 
308                                     graph_settings[i].values[j+1]=0 
309                                 end
310                                 graph_settings[i].values[j]=graph_settings[i].values[j+1]
311                                 if j==nb_values then
312                                         --store value
313                                         if graph_settings[i].name=="" then
314                                             value=graph_settings[i].arg
315                                         else
316                                         value=tonumber(conky_parse('${' .. 
317                                             graph_settings[i].name .. " " ..
318                                             graph_settings[i].arg ..'}'))
319                         end
320                                         graph_settings[i].values[nb_values]=value
321                                 end
322                                 graph_settings[i].automax=math.max(graph_settings[i].automax,
323                                                                    graph_settings[i].values[j])
324                                 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
325                         end
326                         draw_graph(graph_settings[i])
327                         end
328                 end
329     end
330     cairo_destroy(cr)
331     cairo_surface_destroy(cs)
332         updates=nil
333         updates_gap=nil
334 end
335
336 function draw_graph(t)
337     --drawing function
338     local function rgb_to_r_g_b(colour)
339         return ((colour[2] / 0x10000) % 0x100) / 255., ((colour[2] / 0x100) % 0x100) / 255., (colour[2] % 0x100) / 255., colour[3]
340     end
341  
342         local function linear_orientation(o,w,h)
343             --set gradient for bg and bg border
344                 local p
345                 if o=="nn" then
346                         p={w/2,h,w/2,0}
347                 elseif o=="ne" then
348                         p={w,h,0,0}
349                 elseif o=="ww" then
350                         p={0,h/2,w,h/2}
351                 elseif o=="se" then
352                         p={w,0,0,h}
353                 elseif o=="ss" then
354                         p={w/2,0,w/2,h}
355                 elseif o=="ee" then
356                         p={w,h/2,0,h/2}         
357                 elseif o=="sw" then
358                         p={0,0,w,h}
359                 elseif o=="nw" then
360                         p={0,h,w,0}
361                 end
362                 return p
363         end
364
365         local function linear_orientation_inv(o,w,h)
366             --set gradient for fg and fg border
367                 local p
368                 if o=="ss" then
369                         p={w/2,h,w/2,0}
370                 elseif o=="sw" then
371                         p={w,h,0,0}
372                 elseif o=="ee" then
373                         p={0,h/2,w,h/2}
374                 elseif o=="nw" then
375                         p={w,0,0,h}
376                 elseif o=="nn" then
377                         p={w/2,0,w/2,h}
378                 elseif o=="ww" then
379                         p={w,h/2,0,h/2}         
380                 elseif o=="ne" then
381                         p={0,0,w,h}
382                 elseif o=="se" then
383                         p={0,h,w,0}
384                 end
385                 return p
386         end
387         if t.DrawMe~=nil and conky_parse(t.DrawMe) ~= "1" then 
388                 --print(t.DrawMe.."= '"..conky_parse(t.DrawMe).."'")
389                 return
390         --else
391         --      print(t.name)
392         end --cancel drawing for same reason as earlier 
393         --set default values
394         if t.height==nil        then t.height=20 end
395         --checked in previous part : width and nb_values
396         if t.background==nil    then t.background=true end
397         if t.bg_bd_size==nil    then t.bg_bd_size=0 end
398         if t.x==nil                 then t.x=t.bg_bd_size end
399         if t.y==nil                 then t.y=conky_window.height -t.bg_bd_size end
400         if t.bg_colour==nil     then t.bg_colour={{0,0x000000,.5},{1,0xFFFFFF,.5}} end
401         if t.bg_bd_colour==nil  then t.bg_bd_colour={{1,0xFFFFFF,1}} end
402         if t.foreground==nil    then t.foreground=true end
403         if t.fg_colour==nil     then t.fg_colour={{0,0x00FFFF,1},{1,0x0000FF,1}} end
404         if t.fg_bd_size==nil    then t.fg_bd_size=0 end 
405         if t.fg_bd_colour==nil  then t.fg_bd_colour={{1,0xFFFF00,1}} end
406         if t.autoscale==nil     then t.autoscale=false end
407         if t.inverse==nil       then t.inverse=false end
408         if t.angle==nil         then t.angle=0 end
409         if t.bg_bd_orientation==nil then t.bg_bd_orientation="nn" end
410         if t.bg_orientation==nil then t.bg_orientation="nn" end
411         if t.fg_bd_orientation==nil then t.fg_bd_orientation="nn" end
412         if t.fg_orientation==nil then t.fg_orientation="nn" end
413     --check colours tables
414         for i=1, #t.fg_colour do    
415         if #t.fg_colour[i]~=3 then 
416                 print ("error in fg_colour table")
417                 t.fg_colour[i]={1,0x0000FF,1} 
418         end
419     end
420         for i=1, #t.fg_bd_colour do    
421         if #t.fg_bd_colour[i]~=3 then 
422                 print ("error in fg_bd_colour table")
423                 t.fg_bd_colour[i]={1,0x00FF00,1} 
424         end
425     end
426         for i=1, #t.bg_colour do    
427         if #t.bg_colour[i]~=3 then 
428                 print ("error in background color table")
429                 t.bg_colour[i]={1,0xFFFFFF,0.5} 
430         end
431     end    
432         for i=1, #t.bg_bd_colour do    
433         if #t.bg_bd_colour[i]~=3 then 
434                 print ("error in background border color table")
435                 t.bg_bd_colour[i]={1,0xFFFFFF,1} 
436         end
437     end    
438     --calculate skew parameters if needed
439     if t.flag_init then
440             if t.skew_x == nil then 
441                     t.skew_x=0 
442             else
443                     t.skew_x = math.pi*t.skew_x/180     
444             end
445             if t.skew_y == nil then 
446                     t.skew_y=0
447             else
448                     t.skew_y = math.pi*t.skew_y/180     
449             end
450             t.flag_init=false
451         end
452     cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
453     cairo_set_line_join(cr,CAIRO_LINE_JOIN_ROUND)
454     local matrix0 = cairo_matrix_t:create()
455         tolua.takeownership(matrix0)
456     cairo_save(cr)
457     cairo_matrix_init (matrix0, 1,t.skew_y,t.skew_x,1,0,0)
458     cairo_transform(cr,matrix0)
459         local ratio=t.width/t.nb_values
460         cairo_translate(cr,t.x,t.y)
461         cairo_rotate(cr,t.angle*math.pi/180)
462         cairo_scale(cr,1,-1)
463         --background
464         if t.background then
465             local pts=linear_orientation(t.bg_orientation,t.width,t.height)
466                 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
467                 for i=1, #t.bg_colour do
468                         --print ("i",i,t.colour[i][1], rgb_to_r_g_b(t.colour[i]))
469                     cairo_pattern_add_color_stop_rgba (pat, t.bg_colour[i][1], rgb_to_r_g_b(t.bg_colour[i]))
470                 end
471                 cairo_set_source (cr, pat)
472                 cairo_rectangle(cr,0,0,t.width,t.height)        
473                 cairo_fill(cr)
474                 cairo_pattern_destroy(pat)
475         end
476     --autoscale
477     cairo_save(cr)
478         if t.autoscale then
479                 t.max= t.automax*1.1
480         end
481     local scale_x = t.width/(t.nb_values-1)
482         local scale_y = t.height/t.max
483     --define first point of the graph
484         if updates-updates_gap <t.nb_values then 
485                 t.beg = t.beg - 1
486         --next line prevent segmentation error when conky window is redraw 
487                 --quicly when another window "fly" over it
488                 if t.beg<0 then t.beg=0 end
489         else
490                 t.beg=0
491         end
492     if t.inverse then cairo_scale(cr,-1,1)
493     cairo_translate(cr,-t.width,0) end
494         --graph foreground
495         if t.foreground then
496             local pts_fg=linear_orientation_inv(t.fg_orientation,t.width,t.height)
497             local pat = cairo_pattern_create_linear (pts_fg[1],pts_fg[2],pts_fg[3],pts_fg[4])
498                 for i=1,#t.fg_colour,1 do
499                         cairo_pattern_add_color_stop_rgba (pat, 1-t.fg_colour[i][1], rgb_to_r_g_b(t.fg_colour[i]))
500                 end
501                 cairo_set_source (cr, pat)
502                 cairo_move_to(cr,t.beg*scale_x,0)
503                 cairo_line_to(cr,t.beg*scale_x,t.values[t.beg+1]*scale_y)
504                 for i=t.beg, t.nb_values-1 do
505                         cairo_line_to(cr,i*scale_x,t.values[i+1]*scale_y)               
506                 end
507                 cairo_line_to(cr,(t.nb_values-1)*scale_x,0)
508                 cairo_close_path(cr)
509                 cairo_fill(cr)
510                 cairo_pattern_destroy(pat)
511         end
512         --graph_border
513         if t.fg_bd_size>0 then
514         local pts=linear_orientation_inv(t.fg_bd_orientation,t.width,t.height)
515                 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
516                 for i=1,#t.fg_bd_colour,1 do
517                         cairo_pattern_add_color_stop_rgba (pat, 1-t.fg_bd_colour[i][1], rgb_to_r_g_b(t.fg_bd_colour[i]))
518                 end
519                 cairo_set_source (cr, pat)
520                 cairo_move_to(cr,t.beg*scale_x,t.values[t.beg+1]*scale_y)
521                 for i=t.beg, t.nb_values-1 do
522                         cairo_line_to(cr,i*scale_x,t.values[i+1]*scale_y)               
523                 end
524                 cairo_set_line_width(cr,t.fg_bd_size)
525                 cairo_stroke(cr)
526                 cairo_pattern_destroy(pat)
527         end
528         cairo_restore(cr)
529         --background border
530         if t.bg_bd_size>0 then
531         local pts=linear_orientation(t.bg_bd_orientation,t.width,t.height)
532                 local pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
533                 for i=1, #t.bg_bd_colour do
534                         --print ("i",i,t.colour[i][1], rgb_to_r_g_b(t.colour[i]))
535                     cairo_pattern_add_color_stop_rgba (pat, t.bg_bd_colour[i][1], rgb_to_r_g_b(t.bg_bd_colour[i]))
536                 end
537                 cairo_set_source (cr, pat)
538                 cairo_rectangle(cr,0,0,t.width,t.height)        
539                 cairo_set_line_width(cr,t.bg_bd_size)
540                 cairo_stroke(cr)
541                 cairo_pattern_destroy(pat)
542         end     
543
544         cairo_restore(cr)
545
546 end
547