Advertisement
big_bum

Untitled

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