Advertisement
Guest User

~/.conky/bargraph_small.lua

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