Guest User

rings2.lua

a guest
Jul 30th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. require 'cairo'
  2. require 'imlib2'
  3.  
  4. --[[ RING WIDGET ]]
  5. --[[ v1.1 by londonali1010 (2009) ]]
  6. --[[ Options (name, arg, max, bg_colour, bg_alpha, xc, yc, radius, thickness, start_angle, end_angle):
  7. "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
  8. "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 ''.
  9. "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
  10. "bg_colour" is the colour of the base ring.
  11. "bg_alpha" is the alpha value of the base ring.
  12. "fg_colour" is the colour of the indicator part of the ring.
  13. "fg_alpha" is the alpha value of the indicator part of the ring.
  14. "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.
  15. "radius" is the radius of the ring.
  16. "thickness" is the thickness of the ring, centred around the radius.
  17. "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
  18. "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 (e.g. more clockwise) than start_angle. ]]
  19.  
  20.  
  21. function rgb_to_r_g_b(colour, alpha)
  22. return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  23. end
  24.  
  25. function ring(name, arg, max, bgc, bga, fgc, fga, xc, yc, r, t, sa, ea)
  26.  
  27. local function draw_ring(pct)
  28. local angle_0 = sa * (2 * math.pi/360) - math.pi/2
  29. local angle_f = ea * (2 * math.pi/360) - math.pi/2
  30. local pct_arc = pct * (angle_f - angle_0)
  31.  
  32. -- Draw background ring
  33. cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT) -- square line ends
  34. cairo_arc(cr, xc, yc, r, angle_0, angle_f)
  35. cairo_set_source_rgba(cr, rgb_to_r_g_b(bgc, bga))
  36. cairo_set_line_width(cr, t)
  37. cairo_stroke(cr)
  38.  
  39. -- Draw indicator ring
  40.  
  41. cairo_arc(cr, xc, yc, r, angle_0, angle_0 + pct_arc)
  42. cairo_set_source_rgba(cr, rgb_to_r_g_b(fgc, fga))
  43. cairo_stroke(cr)
  44. end;
  45. local function setup_ring()
  46. local str = ''
  47. local value = 0
  48.  
  49. str = string.format('${%s %s}', name, arg)
  50. str = conky_parse(str)
  51. value = tonumber(str)
  52. if value == nil then value = 0 end
  53. pct = value/max
  54. draw_ring(pct)
  55. end
  56. local updates = conky_parse('${updates}')
  57. update_num = tonumber(updates)
  58. if update_num > 5 then setup_ring() end
  59. cairo_stroke (cr);
  60. end
  61.  
  62. function draw_atext(arg, x, y, ff, fs, r, cl, ca, sl, wg)
  63. local rad=(math.pi*r)/180
  64. if sl == 'italic' then
  65. slant = CAIRO_FONT_SLANT_ITALIC
  66. else--if sl == 'normal' then
  67. slant = CAIRO_FONT_SLANT_NORMAL
  68. end
  69.  
  70. if wg == 'bold' then
  71. weight = CAIRO_FONT_WEIGHT_BOLD
  72. else--if wg == 'normal' then
  73. weight = CAIRO_FONT_WEIGHT_NORMAL
  74. end
  75.  
  76. cairo_select_font_face (cr, ff, slant, weight)
  77. cairo_set_font_size (cr, fs)
  78. cairo_set_source_rgba(cr, rgb_to_r_g_b(cl, ca))
  79. cairo_move_to (cr, x, y)
  80. cairo_rotate(cr, rad)
  81. cairo_show_text (cr, conky_parse(arg))
  82. cairo_rotate(cr, -rad)
  83. cairo_stroke (cr)
  84. end
  85.  
  86. function draw_aline(x1, y1, x2, y2, width, cl, ca)
  87. cairo_set_source_rgba(cr, rgb_to_r_g_b(cl, ca))
  88. cairo_set_line_width (cr, width)
  89. cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND)
  90. cairo_move_to (cr, x1, y1)
  91. cairo_rel_line_to (cr, -x1+x2, -y1+y2)
  92. cairo_stroke (cr)
  93. end
  94.  
  95. function draw_arectangle(x1, y1, width, height, radius, rct_fill_cl, rct_fill_al, outl_width, outl_cl, outl_al)
  96. --aspect=1.0
  97. --corner_radius = 30.0
  98. --radius = corner_radius / aspect
  99. degrees = math.pi / 180.0
  100. cairo_new_sub_path (cr)
  101. cairo_arc (cr, x1 + width - radius, y1 + radius, radius, -90 * degrees, 0 * degrees)
  102. cairo_arc (cr, x1 + width - radius, y1 + height - radius, radius, 0 * degrees, 90 * degrees)
  103. cairo_arc (cr, x1 + radius, y1 + height - radius, radius, 90 * degrees, 180 * degrees)
  104. cairo_arc (cr, x1 + radius, y1 + radius, radius, 180 * degrees, 270 * degrees)
  105. cairo_close_path (cr)
  106. cairo_set_source_rgba(cr, rgb_to_r_g_b(rct_fill_cl, rct_fill_al))
  107. cairo_fill_preserve (cr)
  108. cairo_set_source_rgba(cr, rgb_to_r_g_b(outl_cl, outl_al))
  109. --cairo_set_source_rgba (cr, 0.5, 0, 0, 0.5)
  110. cairo_set_line_width (cr, outl_width)
  111. cairo_stroke (cr)
  112. end
  113.  
  114. -- RUNTIME
  115.  
  116. function conky_draw_widgets()
  117. if conky_window == nil then return end
  118. local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  119. cr = cairo_create(cs)
  120.  
  121. draw_arectangle(0, 0, 200, 660, 20, 0x000000, 0.4, 0, 0x92CC00, 0.4)
  122. draw_arectangle(1, 1, 198, 658, 18, 0x000000, 0.0, 2, 0x92CC00, 0.4)
  123.  
  124. ring('time','%I.%M',12,0x92CC00,0.1,0x92CC00,0.4,95, 35,20,4,0,360)
  125. ring('time','%M.%S',60,0xA4E600,0.1,0xA4E600,0.6,95, 35,25,4,0,360)
  126. ring('time','%S',60,0xB7FF00,0.1,0xB7FF00,0.8,95, 35,29,2,0,360)
  127. ring('memperc','',100,0x1E90FF,0.2,0x1E90FF,0.8,127, 150.5,25,12,180,360)
  128. ring('swapperc','',100,0x1E90FF,0.2,0x1E90FF,0.8,127, 220,25,12,180,360)
  129. ring('cpu','cpu1',100,0x1E90FF,0.2,0x1E90FF,0.8,50, 280,25,4,90,180)
  130. ring('cpu','cpu2',100,0xB7FF00,0.2,0xB7FF00,0.8,50, 335,25,4,0,90)
  131. ring('cpu','cpu1',100,0xB7FF00,0.2,0xB7FF00,0.8,105, 280,25,4,180,270)
  132. ring('cpu','cpu2',100,0x1E90FF,0.2,0x1E90FF,0.8,105, 335,25,4,270,360)
  133. ring('fs_free_perc','/',100,0xFF6E00,0.1,0xFF6E00,0.6,127, 400,25,12,180,360)
  134. ring('fs_used_perc','/home',100,0xFF6E00,0.1,0xFF6E00,0.6,127, 470,25,12,180,360)
  135.  
  136. draw_aline(10, 25, 60, 25, 2, 0x92CC00, 0.4)
  137. --draw_aline(60, 210, 130, 210, 5, 0xffffff, 0.1)
  138.  
  139. draw_atext('${time %H:%M}', 16.0, 20.0, 'Ubuntu', 14.0, 0.0, 0xB7FF00, 1.0, 'normal', 'bold')
  140. draw_atext('Memory', 25.0, 226.0, 'Ubuntu', 22.0, -90, 0xffffff, 0.2, 'normal', 'bold')
  141. draw_atext('RAM', 50.0, 128.0, 'Ubuntu', 14.0, 0.0, 0x1E90FF, 1.0, 'normal', 'normal')
  142. draw_atext('Swap', 50.0, 198.0, 'Ubuntu', 14.0, 0.0, 0x1E90FF, 1.0, 'normal', 'normal')
  143. draw_atext('CPU', 25.0, 330.0, 'Ubuntu', 22.0, -90, 0xffffff, 0.2, 'normal', 'bold')
  144. draw_atext('1', 54.0, 295.0, 'Ubuntu', 16.0, 0, 0xffffff, 0.2, 'normal', 'bold')
  145. draw_atext('2', 90.0, 295.0, 'Ubuntu', 16.0, 0, 0xffffff, 0.2, 'normal', 'bold')
  146. draw_atext('3', 54.0, 330.0, 'Ubuntu', 16.0, 0, 0xffffff, 0.2, 'normal', 'bold')
  147. draw_atext('4', 90.0, 330.0, 'Ubuntu', 16.0, 0, 0xffffff, 0.2, 'normal', 'bold')
  148. draw_atext('Disk Usage', 25.0, 495.0, 'Ubuntu', 22.0, -90, 0xffffff, 0.2, 'normal', 'bold')
  149. draw_atext('Root', 50.0, 378.0, 'Ubuntu', 14.0, 0.0, 0xFF6E00, 1.0, 'normal', 'normal')
  150. draw_atext('Home', 50.0, 448.0, 'Ubuntu', 14.0, 0.0, 0xFF6E00, 1.0, 'normal', 'normal')
  151. draw_atext('NET', 25.0, 600, 'Ubuntu', 22.0, -90, 0xffffff, 0.2, 'normal', 'bold')
  152.  
  153.  
  154. cairo_destroy(cr)
  155.  
  156. end
Advertisement
Add Comment
Please, Sign In to add comment