Guest User

.rigns.lua

a guest
Aug 14th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.89 KB | None | 0 0
  1. --[[ RINGS with SECTORS widget
  2.     v1.1 by wlourf (07 Jan. 2011)
  3.     this widget draws a ring with differents effects
  4.     http://u-scripts.blogspot.com/2010/08/rings-sectors-widgets.html
  5.    
  6. To call the script in a conky, use, before TEXT
  7.     lua_load /path/to/the/script/rings.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,
  15.               for example for {$cpu cpu0}, just write name="cpu"
  16. arg         - the argument of the above variable,
  17.               for example for {$cpu cpu0}, just write arg="cpu0"
  18.               arg can be a numerical value if name=""
  19. max         - the maximum value the above variable can reach,
  20.               for example for {$cpu cpu0}, just write max=100
  21.    
  22. Optional parameters:
  23. xc,yc       - coordinates of the center of the ring,
  24.               default = middle of the conky window
  25. radius      - external radius of the ring, in pixels,
  26.               default = quarter of the width of the conky window
  27. thickness   - thickness of the ring, in pixels, default = 10 pixels
  28. start_angle - starting angle of the ring, in degrees, value can be negative,
  29.               default = 0 degree
  30. end_angle   - ending angle of the ring, in degrees,
  31.               value must be greater than start_angle, default = 360 degrees
  32. sectors     - number of sectors in the ring, default = 10
  33. gap_sectors - gap between two sectors, in pixels, default = 1 pixel
  34. cap         - the way to close a sector, available values are
  35.                 "p" for parallel , default value
  36.                 "r" for radial (follow the radius)
  37. inverse_arc - if set to true, arc will be anticlockwise, default=false
  38. border_size - size of the border, in pixels, default = 0 pixel i.e. no border
  39. fill_sector - if set to true, each sector will be completely filled,
  40.               default=false, this parameter is inoperate if sectors=1
  41. background  - if set to false, background will not be drawn, default=true
  42. foreground  - if set to false, foreground will not be drawn, default=true
  43.  
  44. Colours tables below are defined into braces :
  45. {position in the gradient (0 to 1), colour in hexadecimal, alpha (0 to 1)}
  46. example for a single colour table :
  47. {{0,0xFFAA00,1}} position parameter doesn't matter
  48. example for a two-colours table :
  49. {{0,0xFFAA00,1},{1,0x00AA00,1}} or {{0.5,0xFFAA00,1},{1,0x00AA00,1}}
  50. example for a three-colours table :
  51. {{0,0xFFAA00,1},{0.5,0xFF0000,1},{1,0x00AA00,1}}
  52.  
  53. bg_colour1  - colour table for background,
  54.               default = {{0,0x00ffff,0.1},{0.5,0x00FFFF,0.5},{1,0x00FFFF,0.1}}
  55. fg_colour1  - colour table for foreground,
  56.               default = {{0,0x00FF00,0.1},{0.5,0x00FF00,1},{1,0x00FF00,0.1}}
  57. bd_colour1  - colour table for border,
  58.               default = {{0,0xFFFF00,0.5},{0.5,0xFFFF00,1},{1,0xFFFF00,0.5}}             
  59.  
  60. Seconds tables for radials gradients :
  61. bg_colour2  - second colour table for background, default = no second colour
  62. fg_colour2  - second colour table for foreground, default = no second colour
  63. bd_colour2  - second colour table for border, default = no second colour
  64.  
  65. draw_me     - if set to false, text is not drawn (default = true or 1)
  66.               it can be used with a conky string, if the string returns 1, the text is drawn :
  67.               example : "${if_empty ${wireless_essid wlan0}}${else}1$endif",
  68.  
  69. v1.0 (08 Aug. 2010) original release
  70. v1.1 (07 Jan. 2011) Add draw_me parameter and correct memory leaks, thanks to "Creamy Goodness"
  71.                     text is parsed inside the function, not in the array of settings
  72.  
  73. --      This program is free software; you can redistribute it and/or modify
  74. --      it under the terms of the GNU General Public License as published by
  75. --      the Free Software Foundation version 3 (GPLv3)
  76. --    
  77. --      This program is distributed in the hope that it will be useful,
  78. --      but WITHOUT ANY WARRANTY; without even the implied warranty of
  79. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  80. --      GNU General Public License for more details.
  81. --    
  82. --      You should have received a copy of the GNU General Public License
  83. --      along with this program; if not, write to the Free Software
  84. --      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  85. --      MA 02110-1301, USA.    
  86.  
  87. ]]
  88.  
  89.  
  90. require 'cairo'
  91.  
  92. function conky_main_rings()
  93. -- START PARAMETERS HERE
  94. local rings_settings={
  95.     {
  96.         name="fs_used_perc",
  97.         arg="/",
  98.         max=100,
  99.         xc=40,
  100.         yc=540,
  101.         thickness=15,
  102.         radius=35,
  103.         sectors=50,
  104.         start_angle=-180,
  105.         end_angle=90,
  106.         bg_colour1={{0,0x00ff00,0.15},{1,0x00ff00,0.15}},
  107.         fg_colour1={{0,0x00ff00,1},{1,0x00ff00,1}},
  108.         fill_sector=true,
  109.     },
  110.    
  111.     {
  112.         name="fs_used_perc",
  113.         arg="/home",
  114.         max=100,
  115.         xc=190,
  116.         yc=540,
  117.         thickness=15,
  118.         radius=35,
  119.         sectors=50,
  120.         start_angle=-180,
  121.         end_angle=90,
  122.         bg_colour1={{0,0x00ff00,0.15},{1,0x00ff00,0.15}},
  123.         fg_colour1={{0,0x00ff00,1},{1,0x00ff00,1}},
  124.         fill_sector=true,
  125.     },
  126.    
  127.     {
  128.         name="fs_used_perc",
  129.         arg="/mnt/userdata",
  130.         max=100,
  131.         xc=340,
  132.         yc=540,
  133.         thickness=15,
  134.         radius=35,
  135.         sectors=50,
  136.         start_angle=-180,
  137.         end_angle=90,
  138.         bg_colour1={{0,0x00ff00,0.15},{1,0x00ff00,0.15}},
  139.         fg_colour1={{0,0x00ff00,1},{1,0x00ff00,1}},
  140.         fill_sector=true,
  141.     },
  142.    
  143. }
  144. --END OF PARAMETERS HERE
  145.  
  146. --main function
  147.  
  148.     --if conky_window==nil then return end
  149.  
  150.     local cs=cairo_xlib_surface_create(conky_window.display,
  151.         conky_window.drawable,
  152.         conky_window.visual, conky_window.width, conky_window.height)
  153.     local cr=cairo_create(cs)
  154.  
  155.     if tonumber(conky_parse('${updates}'))>3 then
  156.         for i in pairs(rings_settings) do
  157.             draw_ring(cr,rings_settings[i])
  158.         end
  159.     end
  160.  
  161.     cairo_destroy(cr)
  162.  
  163. end
  164.  
  165.  
  166.  
  167.  
  168. function draw_ring(cr, t)
  169.  
  170.     local function rgba_to_r_g_b_a(tcolour)
  171.         local colour,alpha=tcolour[2],tcolour[3]
  172.         return ((colour / 0x10000) % 0x100) / 255.,
  173.             ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  174.     end
  175.            
  176.            
  177.     local function calc_delta(tcol1,tcol2)
  178.         --calculate deltas P R G B A to table_colour 1
  179.  
  180.         for x = 1, #tcol1 do
  181.             tcol1[x].dA = 0
  182.             tcol1[x].dP = 0
  183.             tcol1[x].dR = 0
  184.             tcol1[x].dG = 0
  185.             tcol1[x].dB = 0
  186.             if tcol2~=nil and #tcol1 == #tcol2 then
  187.                 local r1,g1,b1,a1 = rgba_to_r_g_b_a(tcol1[x])
  188.                 local r2,g2,b2,a2 = rgba_to_r_g_b_a(tcol2[x])
  189.                 tcol1[x].dP = (tcol2[x][1]-tcol1[x][1])/t.sectors
  190.                 tcol1[x].dR = (r2-r1)/t.sectors
  191.                 tcol1[x].dG = (g2-g1)/t.sectors
  192.                 tcol1[x].dB = (b2-b1)/t.sectors
  193.                 tcol1[x].dA = (a2-a1)/t.sectors    
  194.                
  195.             end
  196.         end
  197.        
  198.         return tcol1
  199.     end
  200.  
  201.     --check values
  202.     local function setup(t)
  203.         if t.name==nil and t.arg==nil then
  204.             print ("No input values ... use parameters 'name'" +
  205.                 " with 'arg' or only parameter 'arg' ")
  206.             return
  207.         end
  208.  
  209.         if t.max==nil then
  210.             print ("No maximum value defined, use 'max'")
  211.             print ("for name=" .. t.name)
  212.             print ("with arg=" .. t.arg)
  213.             return
  214.         end
  215.         if t.name==nil then t.name="" end
  216.         if t.arg==nil then t.arg="" end
  217.  
  218.         if t.xc==nil then t.xc=conky_window.width/2 end
  219.         if t.yc==nil then t.yc=conky_window.height/2 end
  220.         if t.thickness ==nil then t.thickness = 10 end
  221.         if t.radius ==nil then t.radius =conky_window.width/4 end
  222.         if t.start_angle==nil then t.start_angle =0 end
  223.         if t.end_angle==nil then t.end_angle=360 end
  224.         if t.bg_colour1==nil then
  225.             t.bg_colour1={{0,0x00ffff,0.1},{0.5,0x00FFFF,0.5},{1,0x00FFFF,0.1}}
  226.         end
  227.         if t.fg_colour1==nil then
  228.             t.fg_colour1={{0,0x00FF00,0.1},{0.5,0x00FF00,1},{1,0x00FF00,0.1}}
  229.         end
  230.         if t.bd_colour1==nil then
  231.             t.bd_colour1={{0,0xFFFF00,0.5},{0.5,0xFFFF00,1},{1,0xFFFF00,0.5}}
  232.         end
  233.         if t.sectors==nil then t.sectors=10 end
  234.         if t.gap_sectors==nil then t.gap_sectors=1 end
  235.         if t.fill_sector==nil then t.fill_sector=false end
  236.         if t.sectors==1 then t.fill_sector=false end
  237.         if t.border_size==nil then t.border_size=0 end
  238.         if t.cap==nil then t.cap="p" end
  239.         --some checks
  240.         if t.thickness>t.radius then t.thickness=t.radius*0.1 end
  241.         t.int_radius = t.radius-t.thickness
  242.  
  243.         --check colors tables
  244.         for i=1, #t.bg_colour1 do
  245.             if #t.bg_colour1[i]~=3 then t.bg_colour1[i]={1,0xFFFFFF,0.5} end
  246.         end
  247.         for i=1, #t.fg_colour1 do
  248.             if #t.fg_colour1[i]~=3 then t.fg_colour1[i]={1,0xFF0000,1} end
  249.         end
  250.         for i=1, #t.bd_colour1 do
  251.             if #t.bd_colour1[i]~=3 then t.bd_colour1[i]={1,0xFFFF00,1} end
  252.         end
  253.    
  254.         if t.bg_colour2~=nil then
  255.             for i=1, #t.bg_colour2 do
  256.                 if #t.bg_colour2[i]~=3 then t.bg_colour2[i]={1,0xFFFFFF,0.5} end
  257.             end
  258.         end
  259.         if t.fg_colour2~=nil then
  260.             for i=1, #t.fg_colour2 do
  261.                 if #t.fg_colour2[i]~=3 then t.fg_colour2[i]={1,0xFF0000,1} end
  262.             end
  263.         end
  264.         if t.bd_colour2~=nil then
  265.             for i=1, #t.bd_colour2 do
  266.                 if #t.bd_colour2[i]~=3 then t.bd_colour2[i]={1,0xFFFF00,1} end
  267.             end
  268.         end    
  269.        
  270.         if t.start_angle>=t.end_angle then
  271.          local tmp_angle=t.end_angle
  272.          t.end_angle= t.start_angle
  273.          t.start_angle = tmp_angle
  274.          -- print ("inversed angles")
  275.             if t.end_angle-t.start_angle>360 and t.start_angle>0 then
  276.                 t.end_angle=360+t.start_angle
  277.                 print ("reduce angles")
  278.             end
  279.        
  280.             if t.end_angle+t.start_angle>360 and t.start_angle<=0 then
  281.                 t.end_angle=360+t.start_angle
  282.                 print ("reduce angles")
  283.             end
  284.        
  285.             if t.int_radius<0 then t.int_radius =0 end
  286.             if t.int_radius>t.radius then
  287.                 local tmp_radius=t.radius
  288.                 t.radius=t.int_radius
  289.                 t.int_radius=tmp_radius
  290.                 print ("inversed radius")
  291.             end
  292.             if t.int_radius==t.radius then
  293.                 t.int_radius=0
  294.                 print ("int radius set to 0")
  295.             end
  296.         end
  297.        
  298.         t.fg_colour1 = calc_delta(t.fg_colour1,t.fg_colour2)
  299.         t.bg_colour1 = calc_delta(t.bg_colour1,t.bg_colour2)
  300.         t.bd_colour1 = calc_delta(t.bd_colour1,t.bd_colour2)
  301.     end
  302.    
  303.     if t.draw_me == true then t.draw_me = nil end
  304.     if t.draw_me ~= nil and conky_parse(tostring(t.draw_me)) ~= "1" then return end
  305.     --initialize table
  306.     setup(t)
  307.    
  308.     --initialize cairo context
  309.     cairo_save(cr)
  310.     cairo_translate(cr,t.xc,t.yc)
  311.     cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND)
  312.     cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND)
  313.  
  314.     --get value
  315.     local value = 0
  316.     if t.name ~="" then
  317.         value = tonumber(conky_parse(string.format('${%s %s}', t.name, t.arg)))
  318.     else
  319.         value = tonumber(t.arg)
  320.     end
  321.     if value==nil then value =0 end
  322.  
  323.     --initialize sectors
  324.     --angle of a sector :
  325.     local angleA = ((t.end_angle-t.start_angle)/t.sectors)*math.pi/180
  326.     --value of a sector :
  327.     local valueA = t.max/t.sectors
  328.     --first angle of a sector :
  329.     local lastAngle = t.start_angle*math.pi/180
  330.  
  331.  
  332.     local function draw_sector(type_arc,angle0,angle,valpc, idx)
  333.      
  334.         --this function draws a portion of arc
  335.         --type of arc, angle0 = strating angle, angle= angle of sector,
  336.         --valpc = percentage inside the sector, idx = sctor number #
  337.         local tcolor
  338.          if type_arc=="bg" then         --background
  339.              if valpc==1 then return end
  340.             tcolor=t.bg_colour1
  341.          elseif type_arc=="fg" then --foreground
  342.             if valpc==0 then return end
  343.             tcolor=t.fg_colour1
  344.          elseif type_arc=="bd" then --border
  345.             tcolor=t.bd_colour1
  346.          end
  347.  
  348.         --angles equivalents to gap_sector
  349.         local ext_delta=math.atan(t.gap_sectors/(2*t.radius))
  350.         local int_delta=math.atan(t.gap_sectors/(2*t.int_radius))
  351.  
  352.         --angles of arcs
  353.         local ext_angle=(angle-ext_delta*2)*valpc
  354.         local int_angle=(angle-int_delta*2)*valpc
  355.  
  356.         --define colours to use for this sector
  357.         if #tcolor==1 then
  358.             --plain color
  359.             local vR,vG,vB,vA = rgba_to_r_g_b_a(tcolor[1])
  360.             cairo_set_source_rgba(cr,vR+tcolor[1].dR*idx,
  361.                                     vG+tcolor[1].dG*idx,
  362.                                     vB+tcolor[1].dB*idx,
  363.                                     vA+tcolor[1].dA*idx )
  364.         else
  365.             --radient color
  366.             local pat=cairo_pattern_create_radial(0,0,t.int_radius,0,0,t.radius)
  367.             for i=1, #tcolor do
  368.                 local vP,vR,vG,vB,vA = tcolor[i][1], rgba_to_r_g_b_a(tcolor[i])
  369.                 cairo_pattern_add_color_stop_rgba (pat,
  370.                                     vP+tcolor[i].dP*idx,
  371.                                     vR+tcolor[i].dR*idx,
  372.                                     vG+tcolor[i].dG*idx,
  373.                                     vB+tcolor[i].dB*idx,
  374.                                     vA+tcolor[i].dA*idx )
  375.             end
  376.             cairo_set_source (cr, pat)
  377.             cairo_pattern_destroy(pat)
  378.         end
  379.  
  380.         --start drawing
  381.          cairo_save(cr)
  382.         --x axis is parrallel to start of sector
  383.         cairo_rotate(cr,angle0-math.pi/2)
  384.  
  385.         local ri,re = t.int_radius ,t.radius
  386.  
  387.         --point A
  388.         local angle_a
  389.    
  390.         if t.cap == "p" then
  391.             angle_a = int_delta
  392.             if t.inverse_arc and type_arc ~="bg" then
  393.                 angle_a = angle-int_angle-int_delta
  394.             end
  395.             if not(t.inverse_arc) and type_arc =="bg" then
  396.                 angle_a = int_delta+int_angle
  397.             end
  398.         else --t.cap=="r"
  399.             angle_a = ext_delta
  400.             if t.inverse_arc and type_arc~="bg" then
  401.                 angle_a = angle-ext_angle-ext_delta
  402.             end
  403.             if not(t.inverse_arc) and type_arc=="bg" then
  404.                 angle_a = ext_delta+ext_angle
  405.             end
  406.         end
  407.         local ax,ay = ri*math.cos(angle_a),ri*math.sin(angle_a)
  408.  
  409.  
  410.         --point B
  411.         local angle_b = ext_delta
  412.         if t.cap == "p" then
  413.             if t.inverse_arc and type_arc ~="bg" then
  414.                 angle_b = angle-ext_angle-ext_delta
  415.             end
  416.             if not(t.inverse_arc) and type_arc=="bg" then
  417.                 angle_b = ext_delta+ext_angle
  418.             end
  419.         else
  420.             if t.inverse_arc and type_arc ~="bg" then
  421.                 angle_b = angle-ext_angle-ext_delta
  422.             end
  423.             if not(t.inverse_arc) and type_arc=="bg" then
  424.                 angle_b = ext_delta+ext_angle
  425.             end
  426.         end
  427.         local bx,by = re*math.cos(angle_b),re*math.sin(angle_b)
  428.  
  429.         -- EXTERNAL ARC B --> C
  430.         local b0,b1
  431.         if t.inverse_arc then
  432.             if type_arc=="bg" then
  433.                 b0,b1= ext_delta, angle-ext_delta-ext_angle
  434.             else
  435.                 b0,b1= angle-ext_angle-ext_delta, angle-ext_delta
  436.             end
  437.         else
  438.             if type_arc=="bg" then
  439.                 b0,b1= ext_delta+ext_angle, angle-ext_delta
  440.             else
  441.                 b0,b1= ext_delta, ext_angle+ext_delta
  442.             end
  443.         end
  444.        
  445.         ---POINT D
  446.         local angle_c, angle_d
  447.         if t.cap == "p" then
  448.             angle_d = angle-int_delta
  449.             if t.inverse_arc and type_arc=="bg" then
  450.                 angle_d = angle-int_delta-int_angle
  451.             end
  452.             if not(t.inverse_arc) and type_arc~="bg" then
  453.                 angle_d=int_delta+int_angle
  454.             end
  455.         else
  456.             angle_d = angle-ext_delta
  457.             if t.inverse_arc and type_arc=="bg" then
  458.                 angle_d =angle-ext_delta-ext_angle
  459.             end
  460.             if not(t.inverse_arc) and type_arc~="bg" then
  461.                 angle_d = ext_angle+ext_delta
  462.             end
  463.         end
  464.         local dx,dy = ri*math.cos(angle_d),ri*math.sin(angle_d)
  465.        
  466.         -- INTERNAL ARC D --> A
  467.         local d0,d1
  468.         if t.cap=="p" then 
  469.             if t.inverse_arc then  
  470.                 if type_arc=="bg" then
  471.                     d0,d1= angle-int_delta-int_angle,int_delta
  472.                 else
  473.                     d0,d1= angle-int_delta, angle- int_angle-int_delta
  474.                 end
  475.             else
  476.                 if type_arc=="bg" then
  477.                     d0,d1= angle-int_delta, int_delta+int_angle
  478.                 else
  479.                     d0,d1= int_delta+int_angle, int_delta
  480.                 end
  481.             end
  482.         else
  483.             if t.inverse_arc then  
  484.                 if type_arc=="bg" then 
  485.                     d0,d1= angle-ext_delta-ext_angle,ext_delta
  486.                 else
  487.                     d0,d1= angle-ext_delta, angle- ext_angle-ext_delta
  488.                 end
  489.             else
  490.                 if type_arc=="bg" then 
  491.                     d0,d1= angle-ext_delta,ext_delta+ext_angle
  492.                 else   
  493.                     d0,d1= ext_angle+ext_delta, ext_delta
  494.                 end
  495.             end        
  496.         end
  497.            
  498.         --draw sector
  499.         cairo_move_to(cr,ax,ay)
  500.         cairo_line_to(cr,bx,by)
  501.         cairo_arc(cr,0,0,re,b0,b1)
  502.         cairo_line_to(cr,dx,dy)
  503.         cairo_arc_negative(cr,0,0,ri,d0,d1)
  504.          cairo_close_path (cr);
  505.  
  506.         --stroke or fill sector
  507.          if type_arc=="bd" then
  508.             cairo_set_line_width(cr,t.border_size)
  509.             cairo_stroke(cr)
  510.          else
  511.              cairo_fill(cr)
  512.          end
  513.  
  514.          cairo_restore(cr)
  515.  
  516.      end
  517.     --draw sectors
  518.     local n0,n1,n2 = 1,t.sectors,1
  519.     if t.inverse_arc then n0,n1,n2 = t.sectors,1,-1 end
  520.     local index = 0
  521.     for i = n0,n1,n2 do
  522.         index = index +1
  523.         local valueZ=1
  524.         local cstA, cstB = (i-1),i
  525.         if t.inverse_arc then cstA,cstB = (t.sectors-i), (t.sectors-i+1) end
  526.        
  527.         if value>valueA *cstA and value<valueA*cstB then
  528.             if not t.fill_sector then
  529.                 valueZ = (value-valueA*cstA)/valueA
  530.             end
  531.         else
  532.             if value<valueA*cstB then valueZ=0 end
  533.         end
  534.        
  535.         local start_angle= lastAngle+(i-1)*angleA
  536.         if t.foreground ~= false then
  537.             draw_sector("fg",start_angle,angleA,valueZ, index)
  538.         end
  539.         if t.background ~= false then
  540.             draw_sector("bg",start_angle,angleA,valueZ, i)
  541.         end
  542.         if t.border_size>0 then draw_sector("bd",start_angle,angleA,1, i) end
  543.     end
  544.  
  545.     cairo_restore(cr)
  546. end
  547.  
  548.  
  549. --[[END OF RING-SECTORS WIDGET]]
Advertisement
Add Comment
Please, Sign In to add comment