Advertisement
dlm955

bargraph3.lua

Dec 2nd, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.62 KB | None | 0 0
  1. --[[
  2. BARGRAPH WIDGET
  3. v2.1 by wlourf (07 Jan. 2011)
  4. this widget draws a bargraph with different effects
  5. http://u-scripts.blogspot.com/2010/07/bargraph-widget.html
  6.  
  7. To call the script in a conky, use, before TEXT
  8. lua_load /path/to/the/script/bargraph.lua
  9. lua_draw_hook_pre main_bars
  10. and add one line (blank or not) after TEXT
  11.  
  12. Parameters are :
  13. 3 parameters are mandatory
  14. name - the name of the conky variable to display, for example for {$cpu cpu0}, just write name="cpu"
  15. arg - the argument of the above variable, for example for {$cpu cpu0}, just write arg="cpu0"
  16. arg can be a numerical value if name=""
  17. max - the maximum value the above variable can reach, for example, for {$cpu cpu0}, just write max=100
  18.  
  19. Optional parameters:
  20. x,y - coordinates of the starting point of the bar, default = middle of the conky window
  21. cap - end of cap line, ossibles values are r,b,s (for round, butt, square), default="b"
  22. http://www.cairographics.org/samples/set_line_cap/
  23. angle - angle of rotation of the bar in degress, default = 0 (i.e. a vertical bar)
  24. set to 90 for an horizontal bar
  25. skew_x - skew bar around x axis, default = 0
  26. skew_y - skew bar around y axis, default = 0
  27. blocks - number of blocks to display for a bar (values >0) , default= 10
  28. height - height of a block, default=10 pixels
  29. width - width of a block, default=20 pixels
  30. space - space between 2 blocks, default=2 pixels
  31. angle_bar - this angle is used to draw a bar on a circular way (ok, this is no more a bar !) default=0
  32. radius - for cicular bars, internal radius, default=0
  33. with radius, parameter width has no more effect.
  34.  
  35. Colours below are defined into braces {colour in hexadecimal, alpha}
  36. fg_colour - colour of a block ON, default= {0x00FF00,1}
  37. bg_colour - colour of a block OFF, default = {0x00FF00,0.5}
  38. alarm - threshold, values after this threshold will use alarm_colour colour , default=max
  39. alarm_colour - colour of a block greater than alarm, default=fg_colour
  40. smooth - (true or false), create a gradient from fg_colour to bg_colour, default=false
  41. mid_colour - colours to add to gradient, with this syntax {position into the gradient (0 to1), colour hexa, alpha}
  42. for example, this table {{0.25,0xff0000,1},{0.5,0x00ff00,1},{0.75,0x0000ff,1}} will add
  43. 3 colours to gradient created by fg_colour and alarm_colour, default=no mid_colour
  44. led_effect - add LED effects to each block, default=no led_effect
  45. if smooth=true, led_effect is not used
  46. possibles values : "r","a","e" for radial, parallel, perdendicular to the bar (just try!)
  47. led_effect has to be used with theses colours :
  48. fg_led - middle colour of a block ON, default = fg_colour
  49. bg_led - middle colour of a block OFF, default = bg_colour
  50. alarm_led - middle colour of a block > ALARM, default = alarm_colour
  51.  
  52. reflection parameters, not available for circular bars
  53. reflection_alpha - add a reflection effect (values from 0 to 1) default = 0 = no reflection
  54. other values = starting opacity
  55. reflection_scale - scale of the reflection (default = 1 = height of text)
  56. reflection_length - length of reflection, define where the opacity will be set to zero
  57. values from 0 to 1, default =1
  58. reflection - position of reflection, relative to a vertical bar, default="b"
  59. possibles values are : "b","t","l","r" for bottom, top, left, right
  60. draw_me - if set to false, text is not drawn (default = true or 1)
  61. it can be used with a conky string, if the string returns 1, the text is drawn :
  62. example : "${if_empty ${wireless_essid wlan0}}${else}1$endif",
  63.  
  64. v1.0 (10 Feb. 2010) original release
  65. v1.1 (13 Feb. 2010) numeric values can be passed instead conky stats with parameters name="", arg = numeric_value
  66. v1.2 (28 Feb. 2010) just renamed the widget to bargraph
  67. v1.3 (03 Mar. 2010) added parameters radius & angle_bar to draw the bar in a circular way
  68. v2.0 (12 Jul. 2010) rewrite script + add reflection effects and parameters are now set into tables
  69. v2.1 (07 Jan. 2011) Add draw_me parameter and correct memory leaks, thanks to "Creamy Goodness"
  70.  
  71. -- This program is free software; you can redistribute it and/or modify
  72. -- it under the terms of the GNU General Public License as published by
  73. -- the Free Software Foundation version 3 (GPLv3)
  74. --
  75. -- This program is distributed in the hope that it will be useful,
  76. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  77. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  78. -- GNU General Public License for more details.
  79. --
  80. -- You should have received a copy of the GNU General Public License
  81. -- along with this program; if not, write to the Free Software
  82. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  83. -- MA 02110-1301, USA.
  84.  
  85. ]]
  86.  
  87. require 'cairo'
  88.  
  89. ----------------START OF PARAMETERS ----------
  90. function conky_main_bars()
  91. local bars_settings={
  92. { --[ Graph for CPU0 temp ]--
  93. name="hwmon",
  94. arg="temp 1",
  95. max=200,
  96. alarm=100,
  97. alarm_colour={0xFF0000,0.99},
  98. bg_colour={0x00FF00,0.25},
  99. fg_colour={0xFF0000,0.95},
  100. mid_colour={{0.3,0xFF0000,0.50}},
  101. x=9,y=405,
  102. blocks=40,
  103. cap ="r",
  104. led_effect ="r",
  105. bg_led ={0xFFFF00,0.9},
  106. fg_led ={0xFFFF00,0.99},
  107. reflection_alpha=0,
  108. reflection="r",
  109. space=2,
  110. height=3,width=12,
  111. angle=90,
  112. smooth=false
  113. },
  114. { --[ Graph for CPU1 temp ]--
  115. name="hwmon",
  116. arg="temp 3",
  117. max=200,
  118. alarm=100,
  119. alarm_colour={0xFF0000,0.99},
  120. bg_colour={0x00FF00,0.25},
  121. fg_colour={0xFF0000,0.95},
  122. mid_colour={{0.3,0xFF0000,0.50}},
  123. x=9,y=436,
  124. blocks=40,
  125. cap ="r",
  126. led_effect ="r",
  127. bg_led ={0xFFFF00,0.9},
  128. fg_led ={0xFFFF00,0.99},
  129. space=2,
  130. height=3,width=12,
  131. angle=90,
  132. smooth=false
  133. },
  134. { --[ Graph for CPU ]--
  135. name="cpu",
  136. arg="cpu0",
  137. max=100,
  138. alarm=50,
  139. alarm_colour={0xFF0000,0.99},
  140. bg_colour={0x00FF00,0.25},
  141. fg_colour={0xFF0000,0.95},
  142. mid_colour={{0.9,0xFFFF00,0.90}},
  143. x=8,y=466,
  144. blocks=40,
  145. cap ="r",
  146. led_effect ="r",
  147. bg_led ={0xFFFF00,0.9},
  148. fg_led ={0xFFFF00,0.99},
  149. space=2,
  150. height=3,width=12,
  151. angle=90,
  152. smooth=false
  153. },
  154. { --[ Graph for MEM ]--
  155. name="memperc",
  156. arg="",
  157. max=100,
  158. alarm=50,
  159. alarm_colour={0.5,0xFF0000,0.99},
  160. bg_colour={0x00FF00,0.25},
  161. fg_colour={0xFF0000,0.95},
  162. mid_colour={{0.45,0xFFFF00,0.70}},
  163. x=8,y=495,
  164. blocks=40,
  165. cap ="r",
  166. led_effect ="r",
  167. bg_led ={0xFFFF00,0.9},
  168. fg_led ={0xFFFF00,0.99},
  169. space=2,
  170. height=3,width=12,
  171. angle=90,
  172. smooth=false
  173. },
  174. { --[ Graph for HD ]--
  175. name="fs_used_perc",
  176. arg="",
  177. max=100,
  178. alarm=50,
  179. alarm_colour={0xFF0000,0.99},
  180. bg_colour={0x00FF00,0.25},
  181. fg_colour={0xFF0000,0.95},
  182. mid_colour={{0.45,0xFFFF00,0.70}},
  183. x=8,y=568,
  184. blocks=40,
  185. cap ="r",
  186. led_effect ="r",
  187. bg_led ={0xFFFF00,0.9},
  188. fg_led ={0xFFFF00,0.99},
  189. space=2,
  190. height=3,width=12,
  191. angle=90,
  192. smooth=false
  193. },
  194. { --[ Graph for HD ]--
  195. name="fs_used_perc",
  196. arg="/home",
  197. max=100,
  198. alarm=50,
  199. alarm_colour={0xFF0000,0.99},
  200. bg_colour={0x00FF00,0.25},
  201. fg_colour={0xFF0000,0.95},
  202. mid_colour={{0.45,0xFFFF00,0.70}},
  203. x=8,y=600,
  204. blocks=40,
  205. cap ="r",
  206. led_effect ="r",
  207. bg_led ={0xFFFF00,0.9},
  208. fg_led ={0xFFFF00,0.99},
  209. space=2,
  210. height=3,width=12,
  211. angle=90,
  212. smooth=false
  213. },
  214. { --[ Graph for ETH) ]--
  215. name="upspeedf",
  216. arg="eth0",
  217. max=300,
  218. alarm=270,
  219. alarm_colour={0xFF0000,0.99},
  220. bg_colour={0x00FF00,0.25},
  221. fg_colour={0xFF0000,0.95},
  222. mid_colour={{0.45,0xFFFF00,0.70}},
  223. led_effect="r",
  224. bg_led={0xFFFF00,1},
  225. fg_led ={0xFFFF00,1},
  226. x=8,y=675,
  227. blocks=40,
  228. cap="r",
  229. space=2,
  230. height=3,width=12,
  231. angle=90,
  232. smooth=false
  233. },
  234.  
  235. { --[ Graph for ETH) ]--
  236. name="downspeedf",
  237. arg="eth0",
  238. max=300,
  239. alarm=270,
  240. alarm_colour={0xFF0000,0.99},
  241. bg_colour={0x00FF00,0.2},
  242. fg_colour={0xFF0000,0.2},
  243. mid_colour={{0.0,0xFFFF00,0.70}},
  244. led_effect="r",
  245. bg_led ={0xFFFF00,1},
  246. fg_led ={0xFFFF00,1},
  247. x=8,y=705,
  248. blocks=40,
  249. cap="r",
  250. space=2,
  251. height=3,width=12,
  252. angle=90,
  253. smooth=false
  254. },
  255. }
  256. -----------END OF PARAMETERS--------------
  257.  
  258.  
  259.  
  260. if conky_window == nil then return end
  261.  
  262. local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  263.  
  264. cr = cairo_create(cs)
  265. --prevent segmentation error when reading cpu state
  266. if tonumber(conky_parse('${updates}'))>3 then
  267. for i in pairs(bars_settings) do
  268.  
  269. draw_multi_bar_graph(bars_settings[i])
  270.  
  271. end
  272. end
  273. cairo_destroy(cr)
  274. cairo_surface_destroy(cs)
  275. cr=nil
  276.  
  277. end
  278.  
  279.  
  280.  
  281. function draw_multi_bar_graph(t)
  282. cairo_save(cr)
  283. --check values
  284. if t.draw_me == true then t.draw_me = nil end
  285. if t.draw_me ~= nil and conky_parse(tostring(t.draw_me)) ~= "1" then return end
  286. if t.name==nil and t.arg==nil then
  287. print ("No input values ... use parameters 'name' with 'arg' or only parameter 'arg' ")
  288. return
  289. end
  290. if t.max==nil then
  291. print ("No maximum value defined, use 'max'")
  292. return
  293. end
  294. if t.name==nil then t.name="" end
  295. if t.arg==nil then t.arg="" end
  296.  
  297. --set default values
  298. if t.x == nil then t.x = conky_window.width/2 end
  299. if t.y == nil then t.y = conky_window.height/2 end
  300. if t.blocks == nil then t.blocks=10 end
  301. if t.height == nil then t.height=10 end
  302. if t.angle == nil then t.angle=0 end
  303. t.angle = t.angle*math.pi/180
  304. --line cap style
  305. if t.cap==nil then t.cap = "b" end
  306. local cap="b"
  307. for i,v in ipairs({"s","r","b"}) do
  308. if v==t.cap then cap=v end
  309. end
  310. local delta=0
  311. if t.cap=="r" or t.cap=="s" then delta = t.height end
  312. if cap=="s" then cap = CAIRO_LINE_CAP_SQUARE
  313. elseif cap=="r" then
  314. cap = CAIRO_LINE_CAP_ROUND
  315. elseif cap=="b" then
  316. cap = CAIRO_LINE_CAP_BUTT
  317. end
  318. --end line cap style
  319. --if t.led_effect == nil then t.led_effect="r" end
  320. if t.width == nil then t.width=20 end
  321. if t.space == nil then t.space=2 end
  322. if t.radius == nil then t.radius=0 end
  323. if t.angle_bar == nil then t.angle_bar=0 end
  324. t.angle_bar = t.angle_bar*math.pi/360 --halt angle
  325.  
  326. --colours
  327. if t.bg_colour == nil then t.bg_colour = {0x00FF00,0.5} end
  328. if #t.bg_colour~=2 then t.bg_colour = {0x00FF00,0.5} end
  329. if t.fg_colour == nil then t.fg_colour = {0x00FF00,1} end
  330. if #t.fg_colour~=2 then t.fg_colour = {0x00FF00,1} end
  331. if t.alarm_colour == nil then t.alarm_colour = t.fg_colour end
  332. if #t.alarm_colour~=2 then t.alarm_colour = t.fg_colour end
  333.  
  334. if t.mid_colour ~= nil then
  335. for i=1, #t.mid_colour do
  336. if #t.mid_colour[i]~=3 then
  337. print ("error in mid_color table")
  338. t.mid_colour[i]={1,0xFFFFFF,1}
  339. end
  340. end
  341. end
  342.  
  343. if t.bg_led ~= nil and #t.bg_led~=2 then t.bg_led = t.bg_colour end
  344. if t.fg_led ~= nil and #t.fg_led~=2 then t.fg_led = t.fg_colour end
  345. if t.alarm_led~= nil and #t.alarm_led~=2 then t.alarm_led = t.fg_led end
  346.  
  347. if t.led_effect~=nil then
  348. if t.bg_led == nil then t.bg_led = t.bg_colour end
  349. if t.fg_led == nil then t.fg_led = t.fg_colour end
  350. if t.alarm_led == nil then t.alarm_led = t.fg_led end
  351. end
  352.  
  353.  
  354. if t.alarm==nil then t.alarm = t.max end --0.8*t.max end
  355. if t.smooth == nil then t.smooth = false end
  356.  
  357. if t.skew_x == nil then
  358. t.skew_x=0
  359. else
  360. t.skew_x = math.pi*t.skew_x/180
  361. end
  362. if t.skew_y == nil then
  363. t.skew_y=0
  364. else
  365. t.skew_y = math.pi*t.skew_y/180
  366. end
  367.  
  368. if t.reflection_alpha==nil then t.reflection_alpha=0 end
  369. if t.reflection_length==nil then t.reflection_length=1 end
  370. if t.reflection_scale==nil then t.reflection_scale=1 end
  371.  
  372. --end of default values
  373.  
  374.  
  375. local function rgb_to_r_g_b(col_a)
  376. return ((col_a[1] / 0x10000) % 0x100) / 255., ((col_a[1] / 0x100) % 0x100) / 255., (col_a[1] % 0x100) / 255., col_a[2]
  377. end
  378.  
  379.  
  380. --functions used to create patterns
  381.  
  382. local function create_smooth_linear_gradient(x0,y0,x1,y1)
  383. local pat = cairo_pattern_create_linear (x0,y0,x1,y1)
  384. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(t.fg_colour))
  385. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(t.alarm_colour))
  386. if t.mid_colour ~=nil then
  387. for i=1, #t.mid_colour do
  388. cairo_pattern_add_color_stop_rgba (pat, t.mid_colour[i][1], rgb_to_r_g_b({t.mid_colour[i][2],t.mid_colour[i][3]}))
  389. end
  390. end
  391. return pat
  392. end
  393.  
  394. local function create_smooth_radial_gradient(x0,y0,r0,x1,y1,r1)
  395. local pat = cairo_pattern_create_radial (x0,y0,r0,x1,y1,r1)
  396. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(t.fg_colour))
  397. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(t.alarm_colour))
  398. if t.mid_colour ~=nil then
  399. for i=1, #t.mid_colour do
  400. cairo_pattern_add_color_stop_rgba (pat, t.mid_colour[i][1], rgb_to_r_g_b({t.mid_colour[i][2],t.mid_colour[i][3]}))
  401. end
  402. end
  403. return pat
  404. end
  405.  
  406. local function create_led_linear_gradient(x0,y0,x1,y1,col_alp,col_led)
  407. local pat = cairo_pattern_create_linear (x0,y0,x1,y1) ---delta, 0,delta+ t.width,0)
  408. cairo_pattern_add_color_stop_rgba (pat, 0.0, rgb_to_r_g_b(col_alp))
  409. cairo_pattern_add_color_stop_rgba (pat, 0.5, rgb_to_r_g_b(col_led))
  410. cairo_pattern_add_color_stop_rgba (pat, 1.0, rgb_to_r_g_b(col_alp))
  411. return pat
  412. end
  413.  
  414. local function create_led_radial_gradient(x0,y0,r0,x1,y1,r1,col_alp,col_led,mode)
  415. local pat = cairo_pattern_create_radial (x0,y0,r0,x1,y1,r1)
  416. if mode==3 then
  417. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(col_alp))
  418. cairo_pattern_add_color_stop_rgba (pat, 0.5, rgb_to_r_g_b(col_led))
  419. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(col_alp))
  420. else
  421. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(col_led))
  422. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(col_alp))
  423. end
  424. return pat
  425. end
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432. local function draw_single_bar()
  433. --this fucntion is used for bars with a single block (blocks=1) but
  434. --the drawing is cut in 3 blocks : value/alarm/background
  435. --not zvzimzblr for circular bar
  436. local function create_pattern(col_alp,col_led,bg)
  437. local pat
  438.  
  439. if not t.smooth then
  440. if t.led_effect=="e" then
  441. pat = create_led_linear_gradient (-delta, 0,delta+ t.width,0,col_alp,col_led)
  442. elseif t.led_effect=="a" then
  443. pat = create_led_linear_gradient (t.width/2, 0,t.width/2,-t.height,col_alp,col_led)
  444. elseif t.led_effect=="r" then
  445. pat = create_led_radial_gradient (t.width/2, -t.height/2, 0, t.width/2,-t.height/2,t.height/1.5,col_alp,col_led,2)
  446. else
  447. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  448. end
  449. else
  450. if bg then
  451. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(t.bg_colour))
  452. else
  453. pat = create_smooth_linear_gradient(t.width/2, 0, t.width/2,-t.height)
  454. end
  455. end
  456. return pat
  457. end
  458.  
  459. local y1=-t.height*pct/100
  460. local y2,y3
  461. if pct>(100*t.alarm/t.max) then
  462. y1 = -t.height*t.alarm/100
  463. y2 = -t.height*pct/100
  464. if t.smooth then y1=y2 end
  465. end
  466.  
  467. if t.angle_bar==0 then
  468.  
  469. --block for fg value
  470. local pat = create_pattern(t.fg_colour,t.fg_led,false)
  471. cairo_set_source(cr,pat)
  472. cairo_rectangle(cr,0,0,t.width,y1)
  473. cairo_fill(cr)
  474. cairo_pattern_destroy(pat)
  475.  
  476. -- block for alarm value
  477. if not t.smooth and y2 ~=nil then
  478. pat = create_pattern(t.alarm_colour,t.alarm_led,false)
  479. cairo_set_source(cr,pat)
  480. cairo_rectangle(cr,0,y1,t.width,y2-y1)
  481. cairo_fill(cr)
  482. y3=y2
  483. cairo_pattern_destroy(pat)
  484. else
  485. y2,y3=y1,y1
  486. end
  487. -- block for bg value
  488. cairo_rectangle(cr,0,y2,t.width,-t.height-y3)
  489. pat = create_pattern(t.bg_colour,t.bg_led,true)
  490. cairo_set_source(cr,pat)
  491. cairo_pattern_destroy(pat)
  492. cairo_fill(cr)
  493. end
  494. end --end single bar
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501. local function draw_multi_bar()
  502. --function used for bars with 2 or more blocks
  503. for pt = 1,t.blocks do
  504. --set block y
  505. local y1 = -(pt-1)*(t.height+t.space)
  506. local light_on=false
  507.  
  508. --set colors
  509. local col_alp = t.bg_colour
  510. local col_led = t.bg_led
  511. if pct>=(100/t.blocks) or pct>0 then --ligth on or not the block
  512. if pct>=(pcb*(pt-1)) then
  513. light_on = true
  514. col_alp = t.fg_colour
  515. col_led = t.fg_led
  516. if pct>=(100*t.alarm/t.max) and (pcb*pt)>(100*t.alarm/t.max) then
  517. col_alp = t.alarm_colour
  518. col_led = t.alarm_led
  519. end
  520. end
  521. end
  522.  
  523. --set colors
  524. --have to try to create gradients outside the loop ?
  525. local pat
  526.  
  527. if not t.smooth then
  528. if t.angle_bar==0 then
  529. if t.led_effect=="e" then
  530. pat = create_led_linear_gradient (-delta, 0,delta+ t.width,0,col_alp,col_led)
  531. elseif t.led_effect=="a" then
  532. pat = create_led_linear_gradient (t.width/2, -t.height/2+y1,t.width/2,0+t.height/2+y1,col_alp,col_led)
  533. elseif t.led_effect=="r" then
  534. pat = create_led_radial_gradient (t.width/2, y1, 0, t.width/2,y1,t.width/1.5,col_alp,col_led,2)
  535. else
  536. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  537. end
  538. else
  539. if t.led_effect=="a" then
  540. pat = create_led_radial_gradient (0, 0, t.radius+(t.height+t.space)*(pt-1),
  541. 0, 0, t.radius+(t.height+t.space)*(pt),
  542. col_alp,col_led,3)
  543. else
  544. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  545. end
  546.  
  547. end
  548. else
  549.  
  550. if light_on then
  551. if t.angle_bar==0 then
  552. pat = create_smooth_linear_gradient(t.width/2, t.height/2, t.width/2,-(t.blocks-0.5)*(t.height+t.space))
  553. else
  554. pat = create_smooth_radial_gradient(0, 0, (t.height+t.space), 0,0,(t.blocks+1)*(t.height+t.space),2)
  555. end
  556. else
  557. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(t.bg_colour))
  558. end
  559. end
  560. cairo_set_source (cr, pat)
  561. cairo_pattern_destroy(pat)
  562.  
  563. --draw a block
  564. if t.angle_bar==0 then
  565. cairo_move_to(cr,0,y1)
  566. cairo_line_to(cr,t.width,y1)
  567. else
  568. cairo_arc( cr,0,0,
  569. t.radius+(t.height+t.space)*(pt)-t.height/2,
  570. -t.angle_bar -math.pi/2 ,
  571. t.angle_bar -math.pi/2)
  572. end
  573. cairo_stroke(cr)
  574. end
  575. end
  576.  
  577.  
  578.  
  579.  
  580. local function setup_bar_graph()
  581. --function used to retrieve the value to display and to set the cairo structure
  582. if t.blocks ~=1 then t.y=t.y-t.height/2 end
  583.  
  584. local value = 0
  585. if t.name ~="" then
  586. value = tonumber(conky_parse(string.format('${%s %s}', t.name, t.arg)))
  587. --$to_bytes doesn't work when value has a decimal point,
  588. --https://garage.maemo.org/plugins/ggit/browse.php/?p=monky;a=commitdiff;h=174c256c81a027a2ea406f5f37dc036fac0a524b;hp=d75e2db5ed3fc788fb8514121f67316ac3e5f29f
  589. --http://sourceforge.net/tracker/index.php?func=detail&aid=3000865&group_id=143975&atid=757310
  590. --conky bug?
  591. --value = (conky_parse(string.format('${%s %s}', t.name, t.arg)))
  592. --if string.match(value,"%w") then
  593. -- value = conky_parse(string.format('${to_bytes %s}',value))
  594. --end
  595. else
  596. value = tonumber(t.arg)
  597. end
  598.  
  599. if value==nil then value =0 end
  600.  
  601. pct = 100*value/t.max
  602. pcb = 100/t.blocks
  603.  
  604. cairo_set_line_width (cr, t.height)
  605. cairo_set_line_cap (cr, cap)
  606. cairo_translate(cr,t.x,t.y)
  607. cairo_rotate(cr,t.angle)
  608.  
  609. local matrix0 = cairo_matrix_t:create()
  610. tolua.takeownership(matrix0)
  611. cairo_matrix_init (matrix0, 1,t.skew_y,t.skew_x,1,0,0)
  612. cairo_transform(cr,matrix0)
  613.  
  614.  
  615.  
  616. --call the drawing function for blocks
  617. if t.blocks==1 and t.angle_bar==0 then
  618. draw_single_bar()
  619. if t.reflection=="t" or t.reflection=="b" then cairo_translate(cr,0,-t.height) end
  620. else
  621. draw_multi_bar()
  622. end
  623.  
  624. --dot for reminder
  625. --[[
  626. if t.blocks ~=1 then
  627. cairo_set_source_rgba(cr,1,0,0,1)
  628. cairo_arc(cr,0,t.height/2,3,0,2*math.pi)
  629. cairo_fill(cr)
  630. else
  631. cairo_set_source_rgba(cr,1,0,0,1)
  632. cairo_arc(cr,0,0,3,0,2*math.pi)
  633. cairo_fill(cr)
  634. end]]
  635.  
  636. --call the drawing function for reflection and prepare the mask used
  637. if t.reflection_alpha>0 and t.angle_bar==0 then
  638. local pat2
  639. local matrix1 = cairo_matrix_t:create()
  640. tolua.takeownership(matrix1)
  641. if t.angle_bar==0 then
  642. pts={-delta/2,(t.height+t.space)/2,t.width+delta,-(t.height+t.space)*(t.blocks)}
  643. if t.reflection=="t" then
  644. cairo_matrix_init (matrix1,1,0,0,-t.reflection_scale,0,-(t.height+t.space)*(t.blocks-0.5)*2*(t.reflection_scale+1)/2)
  645. pat2 = cairo_pattern_create_linear (t.width/2,-(t.height+t.space)*(t.blocks),t.width/2,(t.height+t.space)/2)
  646. elseif t.reflection=="r" then
  647. cairo_matrix_init (matrix1,-t.reflection_scale,0,0,1,delta+2*t.width,0)
  648. pat2 = cairo_pattern_create_linear (delta/2+t.width,0,-delta/2,0)
  649. elseif t.reflection=="l" then
  650. cairo_matrix_init (matrix1,-t.reflection_scale,0,0,1,-delta,0)
  651. pat2 = cairo_pattern_create_linear (-delta/2,0,delta/2+t.width,-0)
  652. else --bottom
  653. cairo_matrix_init (matrix1,1,0,0,-1*t.reflection_scale,0,(t.height+t.space)*(t.reflection_scale+1)/2)
  654. pat2 = cairo_pattern_create_linear (t.width/2,(t.height+t.space)/2,t.width/2,-(t.height+t.space)*(t.blocks))
  655. end
  656. end
  657. cairo_transform(cr,matrix1)
  658.  
  659. if t.blocks==1 and t.angle_bar==0 then
  660. draw_single_bar()
  661. cairo_translate(cr,0,-t.height/2)
  662. else
  663. draw_multi_bar()
  664. end
  665.  
  666.  
  667. cairo_set_line_width(cr,0.01)
  668. cairo_pattern_add_color_stop_rgba (pat2, 0,0,0,0,1-t.reflection_alpha)
  669. cairo_pattern_add_color_stop_rgba (pat2, t.reflection_length,0,0,0,1)
  670. if t.angle_bar==0 then
  671. cairo_rectangle(cr,pts[1],pts[2],pts[3],pts[4])
  672. end
  673. cairo_clip_preserve(cr)
  674. cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR)
  675. cairo_stroke(cr)
  676. cairo_mask(cr,pat2)
  677. cairo_pattern_destroy(pat2)
  678. cairo_set_operator(cr,CAIRO_OPERATOR_OVER)
  679.  
  680. end --reflection
  681. pct,pcb=nil
  682. end --setup_bar_graph()
  683.  
  684. --start here !
  685. setup_bar_graph()
  686. cairo_restore(cr)
  687. end
  688.  
  689.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement