Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1. --[[ EQUALIZER WIDGET
  2. v1.0 by wlourf (10 Feb. 2010)
  3. This widget draw a simple bar like (old) equalizers on hi-fi systems.
  4. http://u-scripts.blogspot.com/
  5.  
  6. The arguments are :
  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.
  9. If you would not use an argument in the Conky variable, use ''.
  10. - "max" is the maximum value of the bar. If the Conky variable outputs a percentage, use 100.
  11. - "nb_blocks" is the umber of block to draw
  12. - "cap" id the cap of a block, possibles values are CAIRO_LINE_CAP_ROUND , CAIRO_LINE_CAP_SQUARE or CAIRO_LINE_CAP_BUTT
  13. see http://www.cairographics.org/samples/set_line_cap/
  14. - "xb" and "yb" are the coordinates of the bottom left point of the bar
  15. - "w" and "h" are the width and the height of a block (without caps)
  16. - "space" is the space betwwen two blocks, can be null or negative
  17. - "bgc" and "bga" are background colors and alpha when the block is not LIGHT OFF
  18. - "fgc" and "fga" are foreground colors and alpha when the block is not LIGHT ON
  19. - "alc" and "ala" are foreground colors and alpha when the block is not LIGHT ON and ALARM ON
  20. - "alarm" is the value where blocks LIGHT ON are in a different color (values from 0 to 100)
  21. - "led_effect" true or false : to show a block with a led effect
  22. - "led_alpha" alpha of the center of the led (values from 0 to 1)
  23. - "smooth" true or false : colors in the bar has a smooth effect
  24. - "mid_color",mid_alpha" : colors of the center of the bar (mid_color can to be set to nil)
  25. - "rotation" : angle of rotation of the bar (values are 0 to 360 degrees). 0 = vertical bar, 90 = horizontal bar
  26.  
  27. ]]
  28. require 'cairo'
  29.  
  30. function equalizer(cr, xb, yb, name, arg, max, nb_blocks, cap, w, h, space, bgc, bga, fgc, fga,alc,ala,alarm,led_effect,led_alpha,smooth,mid_color,mid_alpha,rotation)
  31.  
  32. -- функция прекодиране на цвета
  33.  
  34. local function rgb_to_r_g_b(colour, alpha)
  35. return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  36. end
  37. -- данни необходими за вида на въвежданите деления на еквилайзера
  38.  
  39. local cap_round = CAIRO_LINE_CAP_ROUND -- във вид на кръг
  40. local cap_square = CAIRO_LINE_CAP_SQUARE -- във вид на правоъгълник
  41. local cap_butt = CAIRO_LINE_CAP_BUTT -- във вид на правоъгълник (друг вид)
  42.  
  43. local function setup_equalizer()
  44.  
  45. -- вземане на данни от ОС
  46.  
  47. local str = conky_parse(string.format('${%s %s}', name, arg))
  48.  
  49. local value = tonumber(str)
  50. if value == nil then value = 0 end
  51.  
  52. local pct = 100*value/max
  53. local pcb = 100/nb_blocks
  54. -- задаване на размери и вид на деленията
  55. cairo_set_line_width (cr, h)
  56. cairo_set_line_cap (cr, cap)
  57. -- развъртане на еквилайзера
  58. local angle= rotation*math.pi/180
  59. for pt = 1,nb_blocks do
  60. local light_on=false
  61. --set colors
  62. local col,alpha = bgc,bga
  63. if pct>=(100/nb_blocks/2) then --start after an half bloc
  64. if pct>=(pcb*(pt-1)) then
  65. light_on=true
  66. col,alpha = fgc,fga
  67. if pct>=alarm and (pcb*pt)>alarm then col,alpha = alc,ala end
  68. end
  69. end
  70.  
  71. --vertical points
  72. local x1=xb
  73. local y1=yb-pt*(h+space)
  74. local radius0 = yb-y1
  75.  
  76. local x2=xb+radius0*math.sin(angle)
  77. local y2=yb-radius0*math.cos(angle)
  78.  
  79. --line on angle
  80. local a1=(y2-yb)/(x2-xb)
  81. local b1=y2-a1*x2
  82.  
  83. --line perpendicular to angle
  84. local a2=-1/a1
  85. local b2=y2-a2*x2
  86.  
  87. --dots on perpendicular
  88. local xx0,xx1,yy0,yy1=0,0,0,0
  89. if rotation == 90 or rotation == 270 then
  90. xx0,xx1=x2,x2
  91. yy0=yb
  92. yy1=yb+w
  93. else
  94. xx0,xx1=x2,x2+w*math.cos(angle)
  95. yy0=xx0*a2+b2
  96. yy1=xx1*a2+b2
  97. end
  98.  
  99. --perpendicular segment
  100. cairo_move_to (cr, xx0 ,yy0)
  101. cairo_line_to (cr, xx1 ,yy1)
  102.  
  103. --colors
  104. local xc,yc=(xx0+xx1)/2,(yy0+yy1)/2
  105. if light_on and led_effect then
  106. local pat = cairo_pattern_create_radial (xc, yc, 0, xc,yc,w/1.5)
  107. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(col,led_alpha))
  108. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(col,alpha))
  109. cairo_set_source (cr, pat)
  110. cairo_pattern_destroy(pat)
  111. else
  112. cairo_set_source_rgba(cr, rgb_to_r_g_b(col,alpha))
  113. end
  114.  
  115. if light_on and smooth then
  116. local radius = (nb_blocks+1)*(h+space)
  117. if pt==1 then
  118. xc0,yc0=xc,yc --remember the center of first block
  119. end
  120. cairo_move_to(cr,xc0,yc0)
  121. local pat = cairo_pattern_create_radial (xc0, yc0, 0, xc0,yc0,radius)
  122. cairo_pattern_add_color_stop_rgba (pat, 0, rgb_to_r_g_b(fgc,fga))
  123. cairo_pattern_add_color_stop_rgba (pat, 1, rgb_to_r_g_b(alc,ala))
  124. if mid_color ~=nil then
  125. cairo_pattern_add_color_stop_rgba (pat, 0.5,rgb_to_r_g_b(mid_color,mid_alpha))
  126. end
  127. cairo_set_source (cr, pat)
  128. cairo_pattern_destroy(pat)
  129. end
  130.  
  131. cairo_stroke (cr);
  132.  
  133. end
  134. end
  135. --prevent segmentatioon error
  136. local updates=tonumber(conky_parse('${updates}'))
  137.  
  138. setup_equalizer()
  139. end
  140. --[[ END WIDGET ]]
  141. -- -------------------------------------------------------------------------------------
  142. --[[ TEXT WIDGET ]]
  143.  
  144. function text_widget(cr, x, y, prefix, name, arg, suffix, fsize, fgc, fga, rotation)
  145.  
  146. -- функция прекодиране на цвета
  147.  
  148. local function rgb_to_r_g_b(colour, alpha)
  149. return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
  150. end
  151.  
  152.  
  153.  
  154. function addzero100(num)
  155.  
  156. if tonumber(num) < 10 then
  157. return "00" .. num
  158. elseif tonumber(num) <100 then
  159. return "0" .. num
  160. else
  161. return num
  162. end
  163. end
  164.  
  165. -- вземане на данни от ОС
  166.  
  167. local str = conky_parse(string.format('${%s %s}', name, arg))
  168. local value = tonumber(str)
  169. if value == nil then value = 0 end
  170.  
  171. -- указване размер на шрифта
  172. cairo_set_font_size (cr, fsize)
  173. -- указване на точка на извеждане
  174. cairo_move_to(cr, x, y)
  175. -- указване цвят на шрифта
  176. cairo_set_source_rgba(cr, rgb_to_r_g_b(fgc, fga))
  177. -- завъртане на текст
  178. cairo_rotate (cr, rotation*math.pi/180)
  179. -- извеждане на текст
  180. cairo_show_text(cr, prefix .. (addzero100(value)) .. suffix)
  181. end
  182. --[[ END TEXT WIDGET ]]
  183.  
  184.  
  185. -- -------------------------------------------------------------------------------------
  186. function conky_widgets()
  187. if conky_window == nil then return end
  188. local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  189. -- -------------------------------------------------------------------------------------
  190. --[[
  191. equalizer(cr, xb, yb, name, arg, max, nb_blocks, cap, w, h, space, bgc, bga, fgc, fga,alc,ala,alarm,led_effect,led_alpha,smooth,mid_color,mid_alpha,rotation)
  192. ]]
  193. cr = cairo_create(cs)
  194. --text_widget(cr, 25, 120, "CPU2 ", 'cpu', 'cpu2', " %", 10, 0xffffff, 1, 50)
  195. -- cpu
  196. equalizer(cr, 35, 140, 'cpu', 'cpu0', 100, 63, CAIRO_LINE_CAP_SQUARE, 12, 2, 1, 0x606070, 1, 0x00ccff, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  197. cairo_destroy(cr)
  198. --cr = cairo_create(cs)
  199. --equalizer(cr, 66, 153, 'cpu', 'cpu1', 100, 60, CAIRO_LINE_CAP_SQUARE, 12, 2, 1, 0x606070, 1, 0x00ccff, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  200. --cairo_destroy(cr)
  201. --cr = cairo_create(cs)
  202. --equalizer(cr, 66, 178, 'cpu', 'cpu2', 100, 60, CAIRO_LINE_CAP_SQUARE, 12, 2, 1, 0x606070, 1, 0x00ccff, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  203. --cairo_destroy(cr)
  204. --cr = cairo_create(cs)
  205. --equalizer(cr, 66, 205, 'cpu', 'cpu3', 100, 60, CAIRO_LINE_CAP_SQUARE, 12, 2, 1, 0x606070, 1, 0x00ccff, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  206. --cairo_destroy(cr)
  207. -- памет
  208. cr = cairo_create(cs)
  209. --equalizer(cr, 7, 260, 'memperc', '', 100, 100, CAIRO_LINE_CAP_SQUARE, 5, 2, 1, 0x606070, 1, 0xffdf00, 1, 0xff8700, 1, 80, true, 1, true, 0xff0000, 1, 90)
  210. equalizer(cr, 5, 223, 'memperc', '', 100, 80, CAIRO_LINE_CAP_SQUARE, 5, 2, 1, 0x606070, 1, 0x00fe00, 1, 0xff0000, 1, 80, true, 1, true, 0xff0000, 1, 90)
  211. cairo_destroy(cr)
  212. --swap
  213. cr = cairo_create(cs)
  214. equalizer(cr, 5, 249, 'swapperc', '', 100, 80, CAIRO_LINE_CAP_SQUARE, 5, 2, 1, 0x606070, 1, 0x00fe00, 1, 0xff0000, 1, 80, true, 1, true, 0xff0000, 1, 90)
  215. cairo_destroy(cr)
  216.  
  217.  
  218. cr = cairo_create(cs)
  219. equalizer(cr, 5, 328, 'nvidia', 'temp', 110, 80, CAIRO_LINE_CAP_SQUARE, 5, 2, 1, 0x606070, 1, 0x00fe00, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  220. cairo_destroy(cr)
  221.  
  222. cr = cairo_create(cs)
  223. equalizer(cr, 5, 171, 'hwmon', 'temp 1', 100, 80, CAIRO_LINE_CAP_SQUARE, 5, 2, 1, 0x606070, 1, 0x00fe00, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  224. cairo_destroy(cr)
  225.  
  226. -- hdd Root
  227. --cr = cairo_create(cs)
  228. --equalizer(cr, 7, 312, 'fs_used_perc', '/', 100, 100, CAIRO_LINE_CAP_SQUARE, 5, 2, 1, 0x606070, 1, 0x00ccff, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  229. --cairo_destroy(cr)
  230. --hdd XP
  231. --cr = cairo_create(cs)
  232. --equalizer(cr, 7, 340, 'fs_used_perc', '/media/10B835D1B835B5D6', 100, 100, CAIRO_LINE_CAP_SQUARE, 5, 2, 1, 0x606070, 1, 0x00ccff, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  233. --cairo_destroy(cr)
  234. -- downspeed
  235. --cr = cairo_create(cs)
  236. --equalizer(cr, 7, 380, 'downspeedf', 'eth0', 1350, 100, CAIRO_LINE_CAP_SQUARE, 10, 2, 1, 0x606070, 1, 0x00ccff, 1, 0xff0000, 1, 80, true, 1, true, 0x66ff00, 1, 90)
  237. --text_widget(cr, 8, 375, "Download ", 'downspeedf', 'eth0', " Kb/s", 11, 0xffffff, 1, 0)
  238. --cairo_destroy(cr)
  239. -- upspeed
  240. --cr = cairo_create(cs)
  241. --equalizer(cr, 7, 410, 'upspeedf', 'eth0', 170, 100, CAIRO_LINE_CAP_SQUARE, 10, 2, 1, 0x606070, 1, 0xffdf00, 1, 0xff8700, 1, 80, true, 1, true, 0xff0000, 1, 90)
  242. --text_widget(cr,8 , 405, "Upload ", 'upspeedf', 'eth0', " Kb/s", 11, 0xffffff, 1, 0)
  243. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement