Advertisement
iCalPer

Fixed clock_rings.lua

Jan 14th, 2012
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.69 KB | None | 0 0
  1. --[[
  2. Clock Rings by londonali1010 (2009)
  3.  
  4. This script draws percentage meters as rings, and also draws clock hands if you want! It is fully customisable; all options are described in the script. This script is based off a combination of my clock.lua script and my rings.lua script.
  5.  
  6. IMPORTANT: if you are using the 'cpu' function, it will cause a segmentation fault if it tries to draw a ring straight away. The if statement near the end of the script uses a delay to make sure that this doesn't happen. It calculates the length of the delay by the number of updates since Conky started. Generally, a value of 5s is long enough, so if you update Conky every 1s, use update_num > 5 in that if statement (the default). If you only update Conky every 2s, you should change it to update_num > 3; conversely if you update Conky every 0.5s, you should use update_num > 10. ALSO, if you change your Conky, is it best to use "killall conky; conky" to update it, otherwise the update_num will not be reset and you will get an error.
  7.  
  8. To call this script in Conky, use the following (assuming that you save this script to ~/scripts/rings.lua):
  9.     lua_load ~/scripts/clock_rings-v1.1.1.lua
  10.     lua_draw_hook_pre clock_rings
  11.  
  12. Changelog:
  13. + v1.1.1 -- Fixed minor bug that caused the script to crash if conky_parse() returns a nil value (20.10.2009)
  14. + v1.1 -- Added colour option for clock hands (07.10.2009)
  15. + v1.0 -- Original release (30.09.2009)
  16. ]]
  17.  
  18. settings_table = {
  19.     {
  20.         -- Edit this table to customise your rings.
  21.         -- You can create more rings simply by adding more elements to settings_table.
  22.         -- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
  23.         name='time',
  24.         -- "arg" is the argument to the stat type, e.g. if in Conky you would write ${cpu cpu0}, 'cpu0' would be the argument. If you would not use an argument in the Conky variable, use ''.
  25.         arg='%I.%M',
  26.         -- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
  27.         max=12,
  28.         -- "bg_colour" is the colour of the base ring.
  29.         bg_colour=0xFFFFFF,
  30.         -- "bg_alpha" is the alpha value of the base ring.
  31.         bg_alpha=0.1,
  32.         -- "fg_colour" is the colour of the indicator part of the ring.
  33.         fg_colour=0xFFFFFF,
  34.         -- "fg_alpha" is the alpha value of the indicator part of the ring.
  35.         fg_alpha=0.5,
  36.         -- "x" and "y" are the x and y coordinates of the centre of the ring, relative to the top left corner of the Conky window.
  37.         x=150, y=150,
  38.         -- "radius" is the radius of the ring.
  39.         radius=135,
  40.         -- "thickness" is the thickness of the ring, centred around the radius.
  41.         thickness=4,
  42.         -- "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
  43.         start_angle=0,
  44.         -- "end_angle" is the ending angle of the ring, in degrees, clockwise from top. Value can be either positive or negative, but must be larger than start_angle.
  45.         end_angle=360
  46.     },
  47.     {
  48.         name='time',
  49.         arg='%M.%S',
  50.         max=60,
  51.         bg_colour=0xFFFFFF,
  52.         bg_alpha=0.1,
  53.         fg_colour=0xFFFFFF,
  54.         fg_alpha=0.5,
  55.         x=150, y=150,
  56.         radius=140,
  57.         thickness=4,
  58.         start_angle=0,
  59.         end_angle=360
  60.     },
  61.     {
  62.         name='time',
  63.         arg='%S',
  64.         max=60,
  65.         bg_colour=0xFFFFFF,
  66.         bg_alpha=0.1,
  67.         fg_colour=0xFFFFFF,
  68.         fg_alpha=0.5,
  69.         x=150, y=150,
  70.         radius=145,
  71.         thickness=4,
  72.         start_angle=0,
  73.         end_angle=360
  74.     },
  75.     {
  76.         name='eval',
  77.         arg='1',
  78.         max=1,
  79.         bg_colour=0x000000,
  80.         bg_alpha=0.1,
  81.         fg_colour=0xFFFFFF,
  82.         fg_alpha=0.0,
  83.         x=150, y=150,
  84.         radius=66,
  85.         thickness=130,
  86.         start_angle=0,
  87.         end_angle=360
  88.     },
  89.     {
  90.         name='eval',
  91.         arg='1',
  92.         max=1,
  93.         bg_colour=0xFFFFFF,
  94.         bg_alpha=1.0,
  95.         fg_colour=0xFFFFFF,
  96.         fg_alpha=0.0,
  97.         x=150, y=150,
  98.         radius=1,
  99.         thickness=10,
  100.         start_angle=0,
  101.         end_angle=360
  102.     },
  103.     {
  104.         name='eval',
  105.         arg='1',
  106.         max=1,
  107.         bg_colour=0xFFFFFF,
  108.         bg_alpha=1.0,
  109.         fg_colour=0xFFFFFF,
  110.         fg_alpha=0.0,
  111.         x=85, y=150,
  112.         radius=30,
  113.         thickness=1,
  114.         start_angle=0,
  115.         end_angle=360
  116.     },
  117.     {
  118.         name='eval',
  119.         arg='1',
  120.         max=1,
  121.         bg_colour=0x000000,
  122.         bg_alpha=0.3,
  123.         fg_colour=0xFFFFFF,
  124.         fg_alpha=0.0,
  125.         x=85, y=150,
  126.         radius=15,
  127.         thickness=27,
  128.         start_angle=0,
  129.         end_angle=360
  130.     },
  131.     {
  132.         name='eval',
  133.         arg='1',
  134.         max=1,
  135.         bg_colour=0xFFFFFF,
  136.         bg_alpha=1.0,
  137.         fg_colour=0xFFFFFF,
  138.         fg_alpha=0.0,
  139.         x=85, y=150,
  140.         radius=1,
  141.         thickness=2,
  142.         start_angle=0,
  143.         end_angle=360
  144.     },
  145.     {
  146.         name='eval',
  147.         arg='1',
  148.         max=1,
  149.         bg_colour=0xFFFFFF,
  150.         bg_alpha=1.0,
  151.         fg_colour=0xFFFFFF,
  152.         fg_alpha=0.0,
  153.         x=215, y=150,
  154.         radius=30,
  155.         thickness=1,
  156.         start_angle=0,
  157.         end_angle=360
  158.     },
  159.     {
  160.         name='eval',
  161.         arg='1',
  162.         max=1,
  163.         bg_colour=0x000000,
  164.         bg_alpha=0.3,
  165.         fg_colour=0xFFFFFF,
  166.         fg_alpha=0.0,
  167.         x=215, y=150,
  168.         radius=15,
  169.         thickness=27,
  170.         start_angle=0,
  171.         end_angle=360
  172.     },
  173.     {
  174.         name='eval',
  175.         arg='1',
  176.         max=1,
  177.         bg_colour=0xFFFFFF,
  178.         bg_alpha=1.0,
  179.         fg_colour=0xFFFFFF,
  180.         fg_alpha=0.0,
  181.         x=215, y=150,
  182.         radius=1,
  183.         thickness=2,
  184.         start_angle=0,
  185.         end_angle=360
  186.     },
  187.     {
  188.         name='eval',
  189.         arg='1',
  190.         max=1,
  191.         bg_colour=0xFFFFFF,
  192.         bg_alpha=1.0,
  193.         fg_colour=0xFFFFFF,
  194.         fg_alpha=0.0,
  195.         x=150, y=75,
  196.         radius=25,
  197.         thickness=1,
  198.         start_angle=0,
  199.         end_angle=360
  200.     },
  201.     {
  202.         name='eval',
  203.         arg='1',
  204.         max=1,
  205.         bg_colour=0x000000,
  206.         bg_alpha=0.3,
  207.         fg_colour=0xFFFFFF,
  208.         fg_alpha=0.0,
  209.         x=150, y=75,
  210.         radius=13,
  211.         thickness=22,
  212.         start_angle=0,
  213.         end_angle=360
  214.     },
  215.     {
  216.         name='eval',
  217.         arg='1',
  218.         max=1,
  219.         bg_colour=0xFFFFFF,
  220.         bg_alpha=1.0,
  221.         fg_colour=0xFFFFFF,
  222.         fg_alpha=0.0,
  223.         x=150, y=75,
  224.         radius=1,
  225.         thickness=2,
  226.         start_angle=0,
  227.         end_angle=360
  228.     },
  229.     }
  230.  
  231. -- Use these settings to define the origin and extent of your clock.
  232.  
  233. clock_r=127
  234.  
  235. -- "clock_x" and "clock_y" are the coordinates of the centre of the clock, in pixels, from the top left of the Conky window.
  236.  
  237. clock_x=150
  238. clock_y=150
  239.  
  240. -- Colour & alpha of the clock hands
  241.  
  242. clock_colour=0xFFFFFF
  243. clock_alpha=1
  244.  
  245. -- Do you want to show the seconds hand?
  246.  
  247. show_seconds=true
  248.  
  249. require 'cairo'
  250.  
  251. function rgb_to_r_g_b(colour,alpha)
  252.     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  253. end
  254.  
  255. function draw_ring(cr,t,pt)
  256.     local w,h=conky_window.width,conky_window.height
  257.  
  258.     local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']
  259.     local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']
  260.  
  261.     local angle_0=sa*(2*math.pi/360)-math.pi/2
  262.     local angle_f=ea*(2*math.pi/360)-math.pi/2
  263.     local t_arc=t*(angle_f-angle_0)
  264.  
  265.     -- Draw background ring
  266.  
  267.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)
  268.     cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
  269.     cairo_set_line_width(cr,ring_w)
  270.     cairo_stroke(cr)
  271.  
  272.     -- Draw indicator ring
  273.  
  274.     cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)
  275.     cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
  276.     cairo_stroke(cr)
  277. end
  278.  
  279. function draw_clock_hands(cr,xc,yc)
  280.     local secs,mins,hours,secs_arc,mins_arc,hours_arc
  281.     local xh,yh,xm,ym,xs,ys
  282.  
  283.     secs=os.date("%S")
  284.     mins=os.date("%M")
  285.     hours=os.date("%I")
  286.  
  287.     secs_arc=(2*math.pi/60)*secs
  288.     mins_arc=(2*math.pi/60)*mins+secs_arc/60
  289.     hours_arc=(2*math.pi/12)*hours+mins_arc/12
  290.  
  291.     -- Draw hour hand
  292.  
  293.     xh=xc+0.7*clock_r*math.sin(hours_arc)
  294.     yh=yc-0.7*clock_r*math.cos(hours_arc)
  295.     cairo_move_to(cr,xc,yc)
  296.     cairo_line_to(cr,xh,yh)
  297.  
  298.     cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
  299.     cairo_set_line_width(cr,5)
  300.     cairo_set_source_rgba(cr,rgb_to_r_g_b(clock_colour,clock_alpha))
  301.     cairo_stroke(cr)
  302.  
  303.     -- Draw minute hand
  304.  
  305.     xm=xc+clock_r*math.sin(mins_arc)
  306.     ym=yc-clock_r*math.cos(mins_arc)
  307.     cairo_move_to(cr,xc,yc)
  308.     cairo_line_to(cr,xm,ym)
  309.  
  310.     cairo_set_line_width(cr,3)
  311.     cairo_stroke(cr)
  312.  
  313.     -- Draw seconds hand
  314.  
  315.     if show_seconds then
  316.         xs=xc+clock_r*math.sin(secs_arc)
  317.         ys=yc-clock_r*math.cos(secs_arc)
  318.         cairo_move_to(cr,xc,yc)
  319.         cairo_line_to(cr,xs,ys)
  320.  
  321.         cairo_set_line_width(cr,1)
  322.         cairo_stroke(cr)
  323.     end
  324. end
  325.  
  326. function conky_clock_rings()
  327.     local function setup_rings(cr,pt)
  328.         local str=''
  329.         local value=0
  330.  
  331.         str=string.format('${%s %s}',pt['name'],pt['arg'])
  332.         str=conky_parse(str)
  333.  
  334.         value=tonumber(str)
  335.         if value == nil then value = 0 end
  336.         pct=value/pt['max']
  337.  
  338.         draw_ring(cr,pct,pt)
  339.     end
  340.  
  341.     -- Check that Conky has been running for at least 5s
  342.  
  343.     if conky_window==nil then return end
  344.     local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  345.  
  346.     local cr=cairo_create(cs)  
  347.  
  348.     local updates=conky_parse('${updates}')
  349.     update_num=tonumber(updates)
  350.  
  351.     if update_num>5 then
  352.         for i in pairs(settings_table) do
  353.             setup_rings(cr,settings_table[i])
  354.         end
  355.     end
  356.  
  357.     draw_clock_hands(cr,clock_x,clock_y)
  358. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement