Advertisement
arcum

bargraph_small.lua - 8 cores

Jun 14th, 2013
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.37 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_rings
  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 CPU1 ]--
  93. name="cpu",
  94. arg="cpu1",
  95. max=100,
  96. alarm=50,
  97. alarm_colour={0xFF0000,0.72},
  98. bg_colour={0xFFFFFF,0.25},
  99. fg_colour={0x00FF00,0.55},
  100. mid_colour={{0.45,0xFFFF00,0.70}},
  101. x=75,y=235,
  102. blocks=51,
  103. space=1,
  104. height=2,width=5,
  105. angle=90,
  106. smooth=true
  107. },
  108. { --[ Graph for CPU2 ]--
  109. name="cpu",
  110. arg="cpu2",
  111. max=100,
  112. alarm=50,
  113. alarm_colour={0xFF0000,0.72},
  114. bg_colour={0xFFFFFF,0.25},
  115. fg_colour={0x00FF00,0.55},
  116. mid_colour={{0.45,0xFFFF00,0.70}},
  117. x=75,y=248,
  118. blocks=51,
  119. space=1,
  120. height=2,width=5,
  121. angle=90,
  122. smooth=true
  123. },
  124. { --[ Graph for CPU3 ]--
  125. name="cpu",
  126. arg="cpu3",
  127. max=100,
  128. alarm=50,
  129. alarm_colour={0xFF0000,0.72},
  130. bg_colour={0xFFFFFF,0.25},
  131. fg_colour={0x00FF00,0.55},
  132. mid_colour={{0.45,0xFFFF00,0.70}},
  133. x=75,y=261,
  134. blocks=51,
  135. space=1,
  136. height=2,width=5,
  137. angle=90,
  138. smooth=true
  139. },
  140. { --[ Graph for CPU4 ]--
  141. name="cpu",
  142. arg="cpu4",
  143. max=100,
  144. alarm=50,
  145. alarm_colour={0xFF0000,0.72},
  146. bg_colour={0xFFFFFF,0.25},
  147. fg_colour={0x00FF00,0.55},
  148. mid_colour={{0.45,0xFFFF00,0.70}},
  149. x=75,y=274,
  150. blocks=51,
  151. space=1,
  152. height=2,width=5,
  153. angle=90,
  154. smooth=true
  155. },
  156. { --[ Graph for CPU5 ]--
  157. name="cpu",
  158. arg="cpu5",
  159. max=100,
  160. alarm=50,
  161. alarm_colour={0xFF0000,0.72},
  162. bg_colour={0xFFFFFF,0.25},
  163. fg_colour={0x00FF00,0.55},
  164. mid_colour={{0.45,0xFFFF00,0.70}},
  165. x=75,y=287,
  166. blocks=51,
  167. space=1,
  168. height=2,width=5,
  169. angle=90,
  170. smooth=true
  171. },
  172. { --[ Graph for CPU6 ]--
  173. name="cpu",
  174. arg="cpu6",
  175. max=100,
  176. alarm=50,
  177. alarm_colour={0xFF0000,0.72},
  178. bg_colour={0xFFFFFF,0.25},
  179. fg_colour={0x00FF00,0.55},
  180. mid_colour={{0.45,0xFFFF00,0.70}},
  181. x=75,y=300,
  182. blocks=51,
  183. space=1,
  184. height=2,width=5,
  185. angle=90,
  186. smooth=true
  187. },
  188. { --[ Graph for CPU7 ]--
  189. name="cpu",
  190. arg="cpu7",
  191. max=100,
  192. alarm=50,
  193. alarm_colour={0xFF0000,0.72},
  194. bg_colour={0xFFFFFF,0.25},
  195. fg_colour={0x00FF00,0.55},
  196. mid_colour={{0.45,0xFFFF00,0.70}},
  197. x=75,y=313,
  198. blocks=51,
  199. space=1,
  200. height=2,width=5,
  201. angle=90,
  202. smooth=true
  203. },
  204. { --[ Graph for CPU8 ]--
  205. name="cpu",
  206. arg="cpu8",
  207. max=100,
  208. alarm=50,
  209. alarm_colour={0xFF0000,0.72},
  210. bg_colour={0xFFFFFF,0.25},
  211. fg_colour={0x00FF00,0.55},
  212. mid_colour={{0.45,0xFFFF00,0.70}},
  213. x=75,y=326,
  214. blocks=51,
  215. space=1,
  216. height=2,width=5,
  217. angle=90,
  218. smooth=true
  219. },
  220. { --[ Graph for Memory ]--
  221. name="memperc",
  222. arg="",
  223. max=100,
  224. alarm=50,
  225. alarm_colour={0xFF0000,0.72},
  226. bg_colour={0xFFFFFF,0.25},
  227. fg_colour={0x00FF00,0.55},
  228. mid_colour={{0.45,0xFFFF00,0.70}},
  229. x=5,y=375,
  230. blocks=75,
  231. space=1,
  232. height=2,width=5,
  233. angle=90,
  234. smooth=true
  235. },
  236. { --[ Graph for Root ]--
  237. name="fs_used_perc",
  238. arg="/home",
  239. max=100,
  240. alarm=50,
  241. alarm_colour={0xFF0000,0.72},
  242. bg_colour={0xFFFFFF,0.25},
  243. fg_colour={0x00FF00,0.55},
  244. mid_colour={{0.45,0xFFFF00,0.70}},
  245. x=5,y=427,
  246. blocks=75,
  247. space=1,
  248. height=2,width=5,
  249. angle=90,
  250. smooth=true
  251. },
  252. { --[ Graph for Home ]--
  253. name="fs_used_perc",
  254. arg="/media/Windhelm",
  255. max=100,
  256. alarm=50,
  257. alarm_colour={0xFF0000,0.72},
  258. bg_colour={0xFFFFFF,0.25},
  259. fg_colour={0x00FF00,0.55},
  260. mid_colour={{0.45,0xFFFF00,0.70}},
  261. x=5,y=453,
  262. blocks=75,
  263. space=1,
  264. height=2,width=5,
  265. angle=90,
  266. smooth=true
  267. },
  268. { --[ Graph for Swap ]--
  269. name="swapperc",
  270. arg="",
  271. max=100,
  272. alarm=50,
  273. alarm_colour={0xFF0000,0.72},
  274. bg_colour={0xFFFFFF,0.25},
  275. fg_colour={0x00FF00,0.55},
  276. mid_colour={{0.45,0xFFFF00,0.70}},
  277. x=5,y=479,
  278. blocks=75,
  279. space=1,
  280. height=2,width=5,
  281. angle=90,
  282. smooth=true
  283. },
  284. }
  285. -----------END OF PARAMETERS--------------
  286.  
  287.  
  288.  
  289. if conky_window == nil then return end
  290.  
  291. local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  292.  
  293. cr = cairo_create(cs)
  294. --prevent segmentation error when reading cpu state
  295. if tonumber(conky_parse('${updates}'))>3 then
  296. for i in pairs(bars_settings) do
  297.  
  298. draw_multi_bar_graph(bars_settings[i])
  299.  
  300. end
  301. end
  302. cairo_destroy(cr)
  303. cairo_surface_destroy(cs)
  304. cr=nil
  305.  
  306. end
  307.  
  308.  
  309.  
  310. function draw_multi_bar_graph(t)
  311. cairo_save(cr)
  312. --check values
  313. if t.draw_me == true then t.draw_me = nil end
  314. if t.draw_me ~= nil and conky_parse(tostring(t.draw_me)) ~= "1" then return end
  315. if t.name==nil and t.arg==nil then
  316. print ("No input values ... use parameters 'name' with 'arg' or only parameter 'arg' ")
  317. return
  318. end
  319. if t.max==nil then
  320. print ("No maximum value defined, use 'max'")
  321. return
  322. end
  323. if t.name==nil then t.name="" end
  324. if t.arg==nil then t.arg="" end
  325.  
  326. --set default values
  327. if t.x == nil then t.x = conky_window.width/2 end
  328. if t.y == nil then t.y = conky_window.height/2 end
  329. if t.blocks == nil then t.blocks=10 end
  330. if t.height == nil then t.height=10 end
  331. if t.angle == nil then t.angle=0 end
  332. t.angle = t.angle*math.pi/180
  333. --line cap style
  334. if t.cap==nil then t.cap = "b" end
  335. local cap="b"
  336. for i,v in ipairs({"s","r","b"}) do
  337. if v==t.cap then cap=v end
  338. end
  339. local delta=0
  340. if t.cap=="r" or t.cap=="s" then delta = t.height end
  341. if cap=="s" then cap = CAIRO_LINE_CAP_SQUARE
  342. elseif cap=="r" then
  343. cap = CAIRO_LINE_CAP_ROUND
  344. elseif cap=="b" then
  345. cap = CAIRO_LINE_CAP_BUTT
  346. end
  347. --end line cap style
  348. --if t.led_effect == nil then t.led_effect="r" end
  349. if t.width == nil then t.width=20 end
  350. if t.space == nil then t.space=2 end
  351. if t.radius == nil then t.radius=0 end
  352. if t.angle_bar == nil then t.angle_bar=0 end
  353. t.angle_bar = t.angle_bar*math.pi/360 --halt angle
  354.  
  355. --colours
  356. if t.bg_colour == nil then t.bg_colour = {0x00FF00,0.5} end
  357. if #t.bg_colour~=2 then t.bg_colour = {0x00FF00,0.5} end
  358. if t.fg_colour == nil then t.fg_colour = {0x00FF00,1} end
  359. if #t.fg_colour~=2 then t.fg_colour = {0x00FF00,1} end
  360. if t.alarm_colour == nil then t.alarm_colour = t.fg_colour end
  361. if #t.alarm_colour~=2 then t.alarm_colour = t.fg_colour end
  362.  
  363. if t.mid_colour ~= nil then
  364. for i=1, #t.mid_colour do
  365. if #t.mid_colour[i]~=3 then
  366. print ("error in mid_color table")
  367. t.mid_colour[i]={1,0xFFFFFF,1}
  368. end
  369. end
  370. end
  371.  
  372. if t.bg_led ~= nil and #t.bg_led~=2 then t.bg_led = t.bg_colour end
  373. if t.fg_led ~= nil and #t.fg_led~=2 then t.fg_led = t.fg_colour end
  374. if t.alarm_led~= nil and #t.alarm_led~=2 then t.alarm_led = t.fg_led end
  375.  
  376. if t.led_effect~=nil then
  377. if t.bg_led == nil then t.bg_led = t.bg_colour end
  378. if t.fg_led == nil then t.fg_led = t.fg_colour end
  379. if t.alarm_led == nil then t.alarm_led = t.fg_led end
  380. end
  381.  
  382.  
  383. if t.alarm==nil then t.alarm = t.max end --0.8*t.max end
  384. if t.smooth == nil then t.smooth = false end
  385.  
  386. if t.skew_x == nil then
  387. t.skew_x=0
  388. else
  389. t.skew_x = math.pi*t.skew_x/180
  390. end
  391. if t.skew_y == nil then
  392. t.skew_y=0
  393. else
  394. t.skew_y = math.pi*t.skew_y/180
  395. end
  396.  
  397. if t.reflection_alpha==nil then t.reflection_alpha=0 end
  398. if t.reflection_length==nil then t.reflection_length=1 end
  399. if t.reflection_scale==nil then t.reflection_scale=1 end
  400.  
  401. --end of default values
  402.  
  403.  
  404. local function rgb_to_r_g_b(col_a)
  405. return ((col_a[1] / 0x10000) % 0x100) / 255., ((col_a[1] / 0x100) % 0x100) / 255., (col_a[1] % 0x100) / 255., col_a[2]
  406. end
  407.  
  408.  
  409. --functions used to create patterns
  410.  
  411. local function create_smooth_linear_gradient(x0,y0,x1,y1)
  412. local pat = cairo_pattern_create_linear (x0,y0,x1,y1)
  413. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(t.fg_colour))
  414. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(t.alarm_colour))
  415. if t.mid_colour ~=nil then
  416. for i=1, #t.mid_colour do
  417. 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]}))
  418. end
  419. end
  420. return pat
  421. end
  422.  
  423. local function create_smooth_radial_gradient(x0,y0,r0,x1,y1,r1)
  424. local pat = cairo_pattern_create_radial (x0,y0,r0,x1,y1,r1)
  425. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(t.fg_colour))
  426. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(t.alarm_colour))
  427. if t.mid_colour ~=nil then
  428. for i=1, #t.mid_colour do
  429. 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]}))
  430. end
  431. end
  432. return pat
  433. end
  434.  
  435. local function create_led_linear_gradient(x0,y0,x1,y1,col_alp,col_led)
  436. local pat = cairo_pattern_create_linear (x0,y0,x1,y1) ---delta, 0,delta+ t.width,0)
  437. cairo_pattern_add_color_stop_rgba (pat, 0.0, rgb_to_r_g_b(col_alp))
  438. cairo_pattern_add_color_stop_rgba (pat, 0.5, rgb_to_r_g_b(col_led))
  439. cairo_pattern_add_color_stop_rgba (pat, 1.0, rgb_to_r_g_b(col_alp))
  440. return pat
  441. end
  442.  
  443. local function create_led_radial_gradient(x0,y0,r0,x1,y1,r1,col_alp,col_led,mode)
  444. local pat = cairo_pattern_create_radial (x0,y0,r0,x1,y1,r1)
  445. if mode==3 then
  446. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(col_alp))
  447. cairo_pattern_add_color_stop_rgba (pat, 0.5, rgb_to_r_g_b(col_led))
  448. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(col_alp))
  449. else
  450. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(col_led))
  451. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(col_alp))
  452. end
  453. return pat
  454. end
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461. local function draw_single_bar()
  462. --this fucntion is used for bars with a single block (blocks=1) but
  463. --the drawing is cut in 3 blocks : value/alarm/background
  464. --not zvzimzblr for circular bar
  465. local function create_pattern(col_alp,col_led,bg)
  466. local pat
  467.  
  468. if not t.smooth then
  469. if t.led_effect=="e" then
  470. pat = create_led_linear_gradient (-delta, 0,delta+ t.width,0,col_alp,col_led)
  471. elseif t.led_effect=="a" then
  472. pat = create_led_linear_gradient (t.width/2, 0,t.width/2,-t.height,col_alp,col_led)
  473. elseif t.led_effect=="r" then
  474. 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)
  475. else
  476. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  477. end
  478. else
  479. if bg then
  480. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(t.bg_colour))
  481. else
  482. pat = create_smooth_linear_gradient(t.width/2, 0, t.width/2,-t.height)
  483. end
  484. end
  485. return pat
  486. end
  487.  
  488. local y1=-t.height*pct/100
  489. local y2,y3
  490. if pct>(100*t.alarm/t.max) then
  491. y1 = -t.height*t.alarm/100
  492. y2 = -t.height*pct/100
  493. if t.smooth then y1=y2 end
  494. end
  495.  
  496. if t.angle_bar==0 then
  497.  
  498. --block for fg value
  499. local pat = create_pattern(t.fg_colour,t.fg_led,false)
  500. cairo_set_source(cr,pat)
  501. cairo_rectangle(cr,0,0,t.width,y1)
  502. cairo_fill(cr)
  503. cairo_pattern_destroy(pat)
  504.  
  505. -- block for alarm value
  506. if not t.smooth and y2 ~=nil then
  507. pat = create_pattern(t.alarm_colour,t.alarm_led,false)
  508. cairo_set_source(cr,pat)
  509. cairo_rectangle(cr,0,y1,t.width,y2-y1)
  510. cairo_fill(cr)
  511. y3=y2
  512. cairo_pattern_destroy(pat)
  513. else
  514. y2,y3=y1,y1
  515. end
  516. -- block for bg value
  517. cairo_rectangle(cr,0,y2,t.width,-t.height-y3)
  518. pat = create_pattern(t.bg_colour,t.bg_led,true)
  519. cairo_set_source(cr,pat)
  520. cairo_pattern_destroy(pat)
  521. cairo_fill(cr)
  522. end
  523. end --end single bar
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530. local function draw_multi_bar()
  531. --function used for bars with 2 or more blocks
  532. for pt = 1,t.blocks do
  533. --set block y
  534. local y1 = -(pt-1)*(t.height+t.space)
  535. local light_on=false
  536.  
  537. --set colors
  538. local col_alp = t.bg_colour
  539. local col_led = t.bg_led
  540. if pct>=(100/t.blocks) or pct>0 then --ligth on or not the block
  541. if pct>=(pcb*(pt-1)) then
  542. light_on = true
  543. col_alp = t.fg_colour
  544. col_led = t.fg_led
  545. if pct>=(100*t.alarm/t.max) and (pcb*pt)>(100*t.alarm/t.max) then
  546. col_alp = t.alarm_colour
  547. col_led = t.alarm_led
  548. end
  549. end
  550. end
  551.  
  552. --set colors
  553. --have to try to create gradients outside the loop ?
  554. local pat
  555.  
  556. if not t.smooth then
  557. if t.angle_bar==0 then
  558. if t.led_effect=="e" then
  559. pat = create_led_linear_gradient (-delta, 0,delta+ t.width,0,col_alp,col_led)
  560. elseif t.led_effect=="a" then
  561. pat = create_led_linear_gradient (t.width/2, -t.height/2+y1,t.width/2,0+t.height/2+y1,col_alp,col_led)
  562. elseif t.led_effect=="r" then
  563. pat = create_led_radial_gradient (t.width/2, y1, 0, t.width/2,y1,t.width/1.5,col_alp,col_led,2)
  564. else
  565. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  566. end
  567. else
  568. if t.led_effect=="a" then
  569. pat = create_led_radial_gradient (0, 0, t.radius+(t.height+t.space)*(pt-1),
  570. 0, 0, t.radius+(t.height+t.space)*(pt),
  571. col_alp,col_led,3)
  572. else
  573. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  574. end
  575.  
  576. end
  577. else
  578.  
  579. if light_on then
  580. if t.angle_bar==0 then
  581. pat = create_smooth_linear_gradient(t.width/2, t.height/2, t.width/2,-(t.blocks-0.5)*(t.height+t.space))
  582. else
  583. pat = create_smooth_radial_gradient(0, 0, (t.height+t.space), 0,0,(t.blocks+1)*(t.height+t.space),2)
  584. end
  585. else
  586. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(t.bg_colour))
  587. end
  588. end
  589. cairo_set_source (cr, pat)
  590. cairo_pattern_destroy(pat)
  591.  
  592. --draw a block
  593. if t.angle_bar==0 then
  594. cairo_move_to(cr,0,y1)
  595. cairo_line_to(cr,t.width,y1)
  596. else
  597. cairo_arc( cr,0,0,
  598. t.radius+(t.height+t.space)*(pt)-t.height/2,
  599. -t.angle_bar -math.pi/2 ,
  600. t.angle_bar -math.pi/2)
  601. end
  602. cairo_stroke(cr)
  603. end
  604. end
  605.  
  606.  
  607.  
  608.  
  609. local function setup_bar_graph()
  610. --function used to retrieve the value to display and to set the cairo structure
  611. if t.blocks ~=1 then t.y=t.y-t.height/2 end
  612.  
  613. local value = 0
  614. if t.name ~="" then
  615. value = tonumber(conky_parse(string.format('${%s %s}', t.name, t.arg)))
  616. --$to_bytes doesn't work when value has a decimal point,
  617. --https://garage.maemo.org/plugins/ggit/browse.php/?p=monky;a=commitdiff;h=174c256c81a027a2ea406f5f37dc036fac0a524b;hp=d75e2db5ed3fc788fb8514121f67316ac3e5f29f
  618. --http://sourceforge.net/tracker/index.php?func=detail&aid=3000865&group_id=143975&atid=757310
  619. --conky bug?
  620. --value = (conky_parse(string.format('${%s %s}', t.name, t.arg)))
  621. --if string.match(value,"%w") then
  622. -- value = conky_parse(string.format('${to_bytes %s}',value))
  623. --end
  624. else
  625. value = tonumber(t.arg)
  626. end
  627.  
  628. if value==nil then value =0 end
  629.  
  630. pct = 100*value/t.max
  631. pcb = 100/t.blocks
  632.  
  633. cairo_set_line_width (cr, t.height)
  634. cairo_set_line_cap (cr, cap)
  635. cairo_translate(cr,t.x,t.y)
  636. cairo_rotate(cr,t.angle)
  637.  
  638. local matrix0 = cairo_matrix_t:create()
  639. tolua.takeownership(matrix0)
  640. cairo_matrix_init (matrix0, 1,t.skew_y,t.skew_x,1,0,0)
  641. cairo_transform(cr,matrix0)
  642.  
  643.  
  644.  
  645. --call the drawing function for blocks
  646. if t.blocks==1 and t.angle_bar==0 then
  647. draw_single_bar()
  648. if t.reflection=="t" or t.reflection=="b" then cairo_translate(cr,0,-t.height) end
  649. else
  650. draw_multi_bar()
  651. end
  652.  
  653. --dot for reminder
  654. --[[
  655. if t.blocks ~=1 then
  656. cairo_set_source_rgba(cr,1,0,0,1)
  657. cairo_arc(cr,0,t.height/2,3,0,2*math.pi)
  658. cairo_fill(cr)
  659. else
  660. cairo_set_source_rgba(cr,1,0,0,1)
  661. cairo_arc(cr,0,0,3,0,2*math.pi)
  662. cairo_fill(cr)
  663. end]]
  664.  
  665. --call the drawing function for reflection and prepare the mask used
  666. if t.reflection_alpha>0 and t.angle_bar==0 then
  667. local pat2
  668. local matrix1 = cairo_matrix_t:create()
  669. tolua.takeownership(matrix1)
  670. if t.angle_bar==0 then
  671. pts={-delta/2,(t.height+t.space)/2,t.width+delta,-(t.height+t.space)*(t.blocks)}
  672. if t.reflection=="t" then
  673. 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)
  674. pat2 = cairo_pattern_create_linear (t.width/2,-(t.height+t.space)*(t.blocks),t.width/2,(t.height+t.space)/2)
  675. elseif t.reflection=="r" then
  676. cairo_matrix_init (matrix1,-t.reflection_scale,0,0,1,delta+2*t.width,0)
  677. pat2 = cairo_pattern_create_linear (delta/2+t.width,0,-delta/2,0)
  678. elseif t.reflection=="l" then
  679. cairo_matrix_init (matrix1,-t.reflection_scale,0,0,1,-delta,0)
  680. pat2 = cairo_pattern_create_linear (-delta/2,0,delta/2+t.width,-0)
  681. else --bottom
  682. cairo_matrix_init (matrix1,1,0,0,-1*t.reflection_scale,0,(t.height+t.space)*(t.reflection_scale+1)/2)
  683. pat2 = cairo_pattern_create_linear (t.width/2,(t.height+t.space)/2,t.width/2,-(t.height+t.space)*(t.blocks))
  684. end
  685. end
  686. cairo_transform(cr,matrix1)
  687.  
  688. if t.blocks==1 and t.angle_bar==0 then
  689. draw_single_bar()
  690. cairo_translate(cr,0,-t.height/2)
  691. else
  692. draw_multi_bar()
  693. end
  694.  
  695.  
  696. cairo_set_line_width(cr,0.01)
  697. cairo_pattern_add_color_stop_rgba (pat2, 0,0,0,0,1-t.reflection_alpha)
  698. cairo_pattern_add_color_stop_rgba (pat2, t.reflection_length,0,0,0,1)
  699. if t.angle_bar==0 then
  700. cairo_rectangle(cr,pts[1],pts[2],pts[3],pts[4])
  701. end
  702. cairo_clip_preserve(cr)
  703. cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR)
  704. cairo_stroke(cr)
  705. cairo_mask(cr,pat2)
  706. cairo_pattern_destroy(pat2)
  707. cairo_set_operator(cr,CAIRO_OPERATOR_OVER)
  708.  
  709. end --reflection
  710. pct,pcb=nil
  711. end --setup_bar_graph()
  712.  
  713. --start here !
  714. setup_bar_graph()
  715. cairo_restore(cr)
  716. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement