xGeek

Untitled

Feb 19th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.32 KB | None | 0 0
  1.  
  2. require 'cairo'
  3.  
  4. ----------------START OF PARAMETERS ----------
  5. function conky_main_bars()
  6. local bars_settings={
  7. { --[ Graph for CPU1 ]--
  8. name="cpu",
  9. arg="cpu1",
  10. max=100,
  11. alarm=50,
  12. alarm_colour={0xFF0000,0.72},
  13. bg_colour={0xFFFFFF,0.25},
  14. fg_colour={0x00FF00,0.55},
  15. mid_colour={{0.45,0xFFFF00,0.70}},
  16. x=78,y=142,
  17. blocks=56,
  18. space=1,
  19. height=2,width=5,
  20. angle=90,
  21. smooth=true
  22. },
  23. { --[ Graph for CPU2 ]--
  24. name="cpu",
  25. arg="cpu2",
  26. max=100,
  27. alarm=50,
  28. alarm_colour={0xFF0000,0.72},
  29. bg_colour={0xFFFFFF,0.25},
  30. fg_colour={0x00FF00,0.55},
  31. mid_colour={{0.45,0xFFFF00,0.70}},
  32. x=78,y=159,
  33. blocks=56,
  34. space=1,
  35. height=2,width=5,
  36. angle=90,
  37. smooth=true
  38. },
  39. { --[ Graph for CPU3 ]--
  40. name="cpu",
  41. arg="cpu3",
  42. max=100,
  43. alarm=50,
  44. alarm_colour={0xFF0000,0.72},
  45. bg_colour={0xFFFFFF,0.25},
  46. fg_colour={0x00FF00,0.55},
  47. mid_colour={{0.45,0xFFFF00,0.70}},
  48. x=78,y=176,
  49. blocks=56,
  50. space=1,
  51. height=2,width=5,
  52. angle=90,
  53. smooth=true
  54. },
  55. { --[ Graph for CPU4 ]--
  56. name="cpu",
  57. arg="cpu4",
  58. max=100,
  59. alarm=50,
  60. alarm_colour={0xFF0000,0.72},
  61. bg_colour={0xFFFFFF,0.25},
  62. fg_colour={0x00FF00,0.55},
  63. mid_colour={{0.45,0xFFFF00,0.70}},
  64. x=78,y=193,
  65. blocks=56,
  66. space=1,
  67. height=2,width=5,
  68. angle=90,
  69. smooth=true
  70. },
  71.  
  72. { --[ Graph for Memory ]--
  73. name="memperc",
  74. arg="",
  75. max=100,
  76. alarm=50,
  77. alarm_colour={0xFF0000,0.72},
  78. bg_colour={0xFFFFFF,0.25},
  79. fg_colour={0x00FF00,0.55},
  80. mid_colour={{0.45,0xFFFF00,0.70}},
  81. x=15,y=255,
  82. blocks=77,
  83. space=1,
  84. height=2,width=5,
  85. angle=90,
  86. smooth=true
  87. },
  88. { --[ Graph for Root ]--
  89. name="fs_used_perc",
  90. arg="/",
  91. max=100,
  92. alarm=50,
  93. alarm_colour={0xFF0000,0.72},
  94. bg_colour={0xFFFFFF,0.25},
  95. fg_colour={0x00FF00,0.55},
  96. mid_colour={{0.45,0xFFFF00,0.70}},
  97. x=15,y=302,
  98. blocks=77,
  99. space=1,
  100. height=2,width=5,
  101. angle=90,
  102. smooth=true
  103. },
  104. { --[ Graph for Home ]--
  105. name="fs_used_perc",
  106. arg="/home",
  107. max=100,
  108. alarm=50,
  109. alarm_colour={0xFF0000,0.72},
  110. bg_colour={0xFFFFFF,0.25},
  111. fg_colour={0x00FF00,0.55},
  112. mid_colour={{0.45,0xFFFF00,0.70}},
  113. x=15,y=332,
  114. blocks=77,
  115. space=1,
  116. height=2,width=5,
  117. angle=90,
  118. smooth=true
  119. },
  120. -- { --[ Graph for Swap ]--
  121. -- name="swapperc",
  122. -- arg="",
  123. -- max=100,
  124. -- alarm=50,
  125. -- alarm_colour={0xFF0000,0.72},
  126. -- bg_colour={0xFFFFFF,0.25},
  127. -- fg_colour={0x00FF00,0.55},
  128. -- mid_colour={{0.45,0xFFFF00,0.70}},
  129. ---- x=15,y=323,
  130. -- x=15,y=293,
  131. -- blocks=77,
  132. -- space=1,
  133. -- height=2,width=5,
  134. -- angle=90,
  135. -- smooth=true
  136. -- },
  137. }
  138. -----------END OF PARAMETERS--------------
  139.  
  140.  
  141.  
  142. if conky_window == nil then return end
  143.  
  144. local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  145.  
  146. cr = cairo_create(cs)
  147. --prevent segmentation error when reading cpu state
  148. if tonumber(conky_parse('${updates}'))>3 then
  149. for i in pairs(bars_settings) do
  150.  
  151. draw_multi_bar_graph(bars_settings[i])
  152.  
  153. end
  154. end
  155. cairo_destroy(cr)
  156. cairo_surface_destroy(cs)
  157. cr=nil
  158.  
  159. end
  160.  
  161.  
  162.  
  163. function draw_multi_bar_graph(t)
  164. cairo_save(cr)
  165. --check values
  166. if t.draw_me == true then t.draw_me = nil end
  167. if t.draw_me ~= nil and conky_parse(tostring(t.draw_me)) ~= "1" then return end
  168. if t.name==nil and t.arg==nil then
  169. print ("No input values ... use parameters 'name' with 'arg' or only parameter 'arg' ")
  170. return
  171. end
  172. if t.max==nil then
  173. print ("No maximum value defined, use 'max'")
  174. return
  175. end
  176. if t.name==nil then t.name="" end
  177. if t.arg==nil then t.arg="" end
  178.  
  179. --set default values
  180. if t.x == nil then t.x = conky_window.width/2 end
  181. if t.y == nil then t.y = conky_window.height/2 end
  182. if t.blocks == nil then t.blocks=10 end
  183. if t.height == nil then t.height=10 end
  184. if t.angle == nil then t.angle=0 end
  185. t.angle = t.angle*math.pi/180
  186. --line cap style
  187. if t.cap==nil then t.cap = "b" end
  188. local cap="b"
  189. for i,v in ipairs({"s","r","b"}) do
  190. if v==t.cap then cap=v end
  191. end
  192. local delta=0
  193. if t.cap=="r" or t.cap=="s" then delta = t.height end
  194. if cap=="s" then cap = CAIRO_LINE_CAP_SQUARE
  195. elseif cap=="r" then
  196. cap = CAIRO_LINE_CAP_ROUND
  197. elseif cap=="b" then
  198. cap = CAIRO_LINE_CAP_BUTT
  199. end
  200. --end line cap style
  201. --if t.led_effect == nil then t.led_effect="r" end
  202. if t.width == nil then t.width=20 end
  203. if t.space == nil then t.space=2 end
  204. if t.radius == nil then t.radius=0 end
  205. if t.angle_bar == nil then t.angle_bar=0 end
  206. t.angle_bar = t.angle_bar*math.pi/360 --halt angle
  207.  
  208. --colours
  209. if t.bg_colour == nil then t.bg_colour = {0x00FF00,0.5} end
  210. if #t.bg_colour~=2 then t.bg_colour = {0x00FF00,0.5} end
  211. if t.fg_colour == nil then t.fg_colour = {0x00FF00,1} end
  212. if #t.fg_colour~=2 then t.fg_colour = {0x00FF00,1} end
  213. if t.alarm_colour == nil then t.alarm_colour = t.fg_colour end
  214. if #t.alarm_colour~=2 then t.alarm_colour = t.fg_colour end
  215.  
  216. if t.mid_colour ~= nil then
  217. for i=1, #t.mid_colour do
  218. if #t.mid_colour[i]~=3 then
  219. print ("error in mid_color table")
  220. t.mid_colour[i]={1,0xFFFFFF,1}
  221. end
  222. end
  223. end
  224.  
  225. if t.bg_led ~= nil and #t.bg_led~=2 then t.bg_led = t.bg_colour end
  226. if t.fg_led ~= nil and #t.fg_led~=2 then t.fg_led = t.fg_colour end
  227. if t.alarm_led~= nil and #t.alarm_led~=2 then t.alarm_led = t.fg_led end
  228.  
  229. if t.led_effect~=nil then
  230. if t.bg_led == nil then t.bg_led = t.bg_colour end
  231. if t.fg_led == nil then t.fg_led = t.fg_colour end
  232. if t.alarm_led == nil then t.alarm_led = t.fg_led end
  233. end
  234.  
  235.  
  236. if t.alarm==nil then t.alarm = t.max end --0.8*t.max end
  237. if t.smooth == nil then t.smooth = false end
  238.  
  239. if t.skew_x == nil then
  240. t.skew_x=0
  241. else
  242. t.skew_x = math.pi*t.skew_x/180
  243. end
  244. if t.skew_y == nil then
  245. t.skew_y=0
  246. else
  247. t.skew_y = math.pi*t.skew_y/180
  248. end
  249.  
  250. if t.reflection_alpha==nil then t.reflection_alpha=0 end
  251. if t.reflection_length==nil then t.reflection_length=1 end
  252. if t.reflection_scale==nil then t.reflection_scale=1 end
  253.  
  254. --end of default values
  255.  
  256.  
  257. local function rgb_to_r_g_b(col_a)
  258. return ((col_a[1] / 0x10000) % 0x100) / 255., ((col_a[1] / 0x100) % 0x100) / 255., (col_a[1] % 0x100) / 255., col_a[2]
  259. end
  260.  
  261.  
  262. --functions used to create patterns
  263.  
  264. local function create_smooth_linear_gradient(x0,y0,x1,y1)
  265. local pat = cairo_pattern_create_linear (x0,y0,x1,y1)
  266. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(t.fg_colour))
  267. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(t.alarm_colour))
  268. if t.mid_colour ~=nil then
  269. for i=1, #t.mid_colour do
  270. 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]}))
  271. end
  272. end
  273. return pat
  274. end
  275.  
  276. local function create_smooth_radial_gradient(x0,y0,r0,x1,y1,r1)
  277. local pat = cairo_pattern_create_radial (x0,y0,r0,x1,y1,r1)
  278. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(t.fg_colour))
  279. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(t.alarm_colour))
  280. if t.mid_colour ~=nil then
  281. for i=1, #t.mid_colour do
  282. 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]}))
  283. end
  284. end
  285. return pat
  286. end
  287.  
  288. local function create_led_linear_gradient(x0,y0,x1,y1,col_alp,col_led)
  289. local pat = cairo_pattern_create_linear (x0,y0,x1,y1) ---delta, 0,delta+ t.width,0)
  290. cairo_pattern_add_color_stop_rgba (pat, 0.0, rgb_to_r_g_b(col_alp))
  291. cairo_pattern_add_color_stop_rgba (pat, 0.5, rgb_to_r_g_b(col_led))
  292. cairo_pattern_add_color_stop_rgba (pat, 1.0, rgb_to_r_g_b(col_alp))
  293. return pat
  294. end
  295.  
  296. local function create_led_radial_gradient(x0,y0,r0,x1,y1,r1,col_alp,col_led,mode)
  297. local pat = cairo_pattern_create_radial (x0,y0,r0,x1,y1,r1)
  298. if mode==3 then
  299. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(col_alp))
  300. cairo_pattern_add_color_stop_rgba (pat, 0.5, rgb_to_r_g_b(col_led))
  301. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(col_alp))
  302. else
  303. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(col_led))
  304. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(col_alp))
  305. end
  306. return pat
  307. end
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314. local function draw_single_bar()
  315. --this fucntion is used for bars with a single block (blocks=1) but
  316. --the drawing is cut in 3 blocks : value/alarm/background
  317. --not zvzimzblr for circular bar
  318. local function create_pattern(col_alp,col_led,bg)
  319. local pat
  320.  
  321. if not t.smooth then
  322. if t.led_effect=="e" then
  323. pat = create_led_linear_gradient (-delta, 0,delta+ t.width,0,col_alp,col_led)
  324. elseif t.led_effect=="a" then
  325. pat = create_led_linear_gradient (t.width/2, 0,t.width/2,-t.height,col_alp,col_led)
  326. elseif t.led_effect=="r" then
  327. 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)
  328. else
  329. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  330. end
  331. else
  332. if bg then
  333. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(t.bg_colour))
  334. else
  335. pat = create_smooth_linear_gradient(t.width/2, 0, t.width/2,-t.height)
  336. end
  337. end
  338. return pat
  339. end
  340.  
  341. local y1=-t.height*pct/100
  342. local y2,y3
  343. if pct>(100*t.alarm/t.max) then
  344. y1 = -t.height*t.alarm/100
  345. y2 = -t.height*pct/100
  346. if t.smooth then y1=y2 end
  347. end
  348.  
  349. if t.angle_bar==0 then
  350.  
  351. --block for fg value
  352. local pat = create_pattern(t.fg_colour,t.fg_led,false)
  353. cairo_set_source(cr,pat)
  354. cairo_rectangle(cr,0,0,t.width,y1)
  355. cairo_fill(cr)
  356. cairo_pattern_destroy(pat)
  357.  
  358. -- block for alarm value
  359. if not t.smooth and y2 ~=nil then
  360. pat = create_pattern(t.alarm_colour,t.alarm_led,false)
  361. cairo_set_source(cr,pat)
  362. cairo_rectangle(cr,0,y1,t.width,y2-y1)
  363. cairo_fill(cr)
  364. y3=y2
  365. cairo_pattern_destroy(pat)
  366. else
  367. y2,y3=y1,y1
  368. end
  369. -- block for bg value
  370. cairo_rectangle(cr,0,y2,t.width,-t.height-y3)
  371. pat = create_pattern(t.bg_colour,t.bg_led,true)
  372. cairo_set_source(cr,pat)
  373. cairo_pattern_destroy(pat)
  374. cairo_fill(cr)
  375. end
  376. end --end single bar
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383. local function draw_multi_bar()
  384. --function used for bars with 2 or more blocks
  385. for pt = 1,t.blocks do
  386. --set block y
  387. local y1 = -(pt-1)*(t.height+t.space)
  388. local light_on=false
  389.  
  390. --set colors
  391. local col_alp = t.bg_colour
  392. local col_led = t.bg_led
  393. if pct>=(100/t.blocks) or pct>0 then --ligth on or not the block
  394. if pct>=(pcb*(pt-1)) then
  395. light_on = true
  396. col_alp = t.fg_colour
  397. col_led = t.fg_led
  398. if pct>=(100*t.alarm/t.max) and (pcb*pt)>(100*t.alarm/t.max) then
  399. col_alp = t.alarm_colour
  400. col_led = t.alarm_led
  401. end
  402. end
  403. end
  404.  
  405. --set colors
  406. --have to try to create gradients outside the loop ?
  407. local pat
  408.  
  409. if not t.smooth then
  410. if t.angle_bar==0 then
  411. if t.led_effect=="e" then
  412. pat = create_led_linear_gradient (-delta, 0,delta+ t.width,0,col_alp,col_led)
  413. elseif t.led_effect=="a" then
  414. pat = create_led_linear_gradient (t.width/2, -t.height/2+y1,t.width/2,0+t.height/2+y1,col_alp,col_led)
  415. elseif t.led_effect=="r" then
  416. pat = create_led_radial_gradient (t.width/2, y1, 0, t.width/2,y1,t.width/1.5,col_alp,col_led,2)
  417. else
  418. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  419. end
  420. else
  421. if t.led_effect=="a" then
  422. pat = create_led_radial_gradient (0, 0, t.radius+(t.height+t.space)*(pt-1),
  423. 0, 0, t.radius+(t.height+t.space)*(pt),
  424. col_alp,col_led,3)
  425. else
  426. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(col_alp))
  427. end
  428.  
  429. end
  430. else
  431.  
  432. if light_on then
  433. if t.angle_bar==0 then
  434. pat = create_smooth_linear_gradient(t.width/2, t.height/2, t.width/2,-(t.blocks-0.5)*(t.height+t.space))
  435. else
  436. pat = create_smooth_radial_gradient(0, 0, (t.height+t.space), 0,0,(t.blocks+1)*(t.height+t.space),2)
  437. end
  438. else
  439. pat = cairo_pattern_create_rgba (rgb_to_r_g_b(t.bg_colour))
  440. end
  441. end
  442. cairo_set_source (cr, pat)
  443. cairo_pattern_destroy(pat)
  444.  
  445. --draw a block
  446. if t.angle_bar==0 then
  447. cairo_move_to(cr,0,y1)
  448. cairo_line_to(cr,t.width,y1)
  449. else
  450. cairo_arc( cr,0,0,
  451. t.radius+(t.height+t.space)*(pt)-t.height/2,
  452. -t.angle_bar -math.pi/2 ,
  453. t.angle_bar -math.pi/2)
  454. end
  455. cairo_stroke(cr)
  456. end
  457. end
  458.  
  459.  
  460.  
  461.  
  462. local function setup_bar_graph()
  463. --function used to retrieve the value to display and to set the cairo structure
  464. if t.blocks ~=1 then t.y=t.y-t.height/2 end
  465.  
  466. local value = 0
  467. if t.name ~="" then
  468. value = tonumber(conky_parse(string.format('${%s %s}', t.name, t.arg)))
  469. --$to_bytes doesn't work when value has a decimal point,
  470. --https://garage.maemo.org/plugins/ggit/browse.php/?p=monky;a=commitdiff;h=174c256c81a027a2ea406f5f37dc036fac0a524b;hp=d75e2db5ed3fc788fb8514121f67316ac3e5f29f
  471. --http://sourceforge.net/tracker/index.php?func=detail&aid=3000865&group_id=143975&atid=757310
  472. --conky bug?
  473. --value = (conky_parse(string.format('${%s %s}', t.name, t.arg)))
  474. --if string.match(value,"%w") then
  475. -- value = conky_parse(string.format('${to_bytes %s}',value))
  476. --end
  477. else
  478. value = tonumber(t.arg)
  479. end
  480.  
  481. if value==nil then value =0 end
  482.  
  483. pct = 100*value/t.max
  484. pcb = 100/t.blocks
  485.  
  486. cairo_set_line_width (cr, t.height)
  487. cairo_set_line_cap (cr, cap)
  488. cairo_translate(cr,t.x,t.y)
  489. cairo_rotate(cr,t.angle)
  490.  
  491. local matrix0 = cairo_matrix_t:create()
  492. tolua.takeownership(matrix0)
  493. cairo_matrix_init (matrix0, 1,t.skew_y,t.skew_x,1,0,0)
  494. cairo_transform(cr,matrix0)
  495.  
  496.  
  497.  
  498. --call the drawing function for blocks
  499. if t.blocks==1 and t.angle_bar==0 then
  500. draw_single_bar()
  501. if t.reflection=="t" or t.reflection=="b" then cairo_translate(cr,0,-t.height) end
  502. else
  503. draw_multi_bar()
  504. end
  505.  
  506. --dot for reminder
  507. --[[
  508. if t.blocks ~=1 then
  509. cairo_set_source_rgba(cr,1,0,0,1)
  510. cairo_arc(cr,0,t.height/2,3,0,2*math.pi)
  511. cairo_fill(cr)
  512. else
  513. cairo_set_source_rgba(cr,1,0,0,1)
  514. cairo_arc(cr,0,0,3,0,2*math.pi)
  515. cairo_fill(cr)
  516. end]]
  517.  
  518. --call the drawing function for reflection and prepare the mask used
  519. if t.reflection_alpha>0 and t.angle_bar==0 then
  520. local pat2
  521. local matrix1 = cairo_matrix_t:create()
  522. tolua.takeownership(matrix1)
  523. if t.angle_bar==0 then
  524. pts={-delta/2,(t.height+t.space)/2,t.width+delta,-(t.height+t.space)*(t.blocks)}
  525. if t.reflection=="t" then
  526. 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)
  527. pat2 = cairo_pattern_create_linear (t.width/2,-(t.height+t.space)*(t.blocks),t.width/2,(t.height+t.space)/2)
  528. elseif t.reflection=="r" then
  529. cairo_matrix_init (matrix1,-t.reflection_scale,0,0,1,delta+2*t.width,0)
  530. pat2 = cairo_pattern_create_linear (delta/2+t.width,0,-delta/2,0)
  531. elseif t.reflection=="l" then
  532. cairo_matrix_init (matrix1,-t.reflection_scale,0,0,1,-delta,0)
  533. pat2 = cairo_pattern_create_linear (-delta/2,0,delta/2+t.width,-0)
  534. else --bottom
  535. cairo_matrix_init (matrix1,1,0,0,-1*t.reflection_scale,0,(t.height+t.space)*(t.reflection_scale+1)/2)
  536. pat2 = cairo_pattern_create_linear (t.width/2,(t.height+t.space)/2,t.width/2,-(t.height+t.space)*(t.blocks))
  537. end
  538. end
  539. cairo_transform(cr,matrix1)
  540.  
  541. if t.blocks==1 and t.angle_bar==0 then
  542. draw_single_bar()
  543. cairo_translate(cr,0,-t.height/2)
  544. else
  545. draw_multi_bar()
  546. end
  547.  
  548.  
  549. cairo_set_line_width(cr,0.01)
  550. cairo_pattern_add_color_stop_rgba (pat2, 0,0,0,0,1-t.reflection_alpha)
  551. cairo_pattern_add_color_stop_rgba (pat2, t.reflection_length,0,0,0,1)
  552. if t.angle_bar==0 then
  553. cairo_rectangle(cr,pts[1],pts[2],pts[3],pts[4])
  554. end
  555. cairo_clip_preserve(cr)
  556. cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR)
  557. cairo_stroke(cr)
  558. cairo_mask(cr,pat2)
  559. cairo_pattern_destroy(pat2)
  560. cairo_set_operator(cr,CAIRO_OPERATOR_OVER)
  561.  
  562. end --reflection
  563. pct,pcb=nil
  564. end --setup_bar_graph()
  565.  
  566. --start here !
  567. setup_bar_graph()
  568. cairo_restore(cr)
  569. end
Advertisement
Add Comment
Please, Sign In to add comment