Advertisement
plusDm

Lua for Conky Equalizer

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