Guest User

.bargraph.lua

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