script.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. -- This is a lua script for use in Conky.
  2. require 'cairo'
  3. require 'rsvg'
  4. function draw_svg_file(path,x,y,w,h)
  5. local rh = rsvg_handle_new_from_file(path)
  6. rect = RsvgRectangle:create()
  7. rect:set(x,y,w,h)
  8. rsvg_handle_render_document(rh,cr,rect)
  9. rsvg_destroy_handle(rh)
  10. end
  11. function draw_text(text,x,y,font,size,color,alpha,weight,align,maxwidth)
  12. slant = CAIRO_FONT_SLANT_NORMAL
  13. cairo_select_font_face (cr,font,slant,weight);
  14. cairo_set_font_size (cr, size)
  15. red, green, blue = hex2rgb(color)
  16. if alpha == nil then
  17. cairo_set_source_rgb (cr, red, green, blue)
  18. else
  19. cairo_set_source_rgba (cr,red, green, blue, alpha)
  20. end
  21. local extents = cairo_text_extents_t:create()
  22. tolua.takeownership(extents)
  23. cairo_text_extents(cr,text,extents)
  24. -- cut off text until it fits the maximum width
  25. if maxwidth then
  26. did=0
  27. maxwidth=tonumber(maxwidth)
  28. while extents.width > maxwidth do
  29. text=string.gsub(text, "[^\128-\191][\128-\191]*$", "") -- delete last char: stackoverflow.com/a/15980690
  30. cairo_text_extents(cr,text .. '…',extents)
  31. did=1
  32. end
  33. if did == 1 then text=text .. '…' end
  34. end
  35. if align == "right" or align == "center" then
  36. --~ print("Alignment: " .. align)
  37. --~ print("Text Extents Width: " .. extents.width)
  38. --~ print("Conky Window Width: " .. conky_window.width)
  39. if align == "right" then
  40. x = x - extents.width
  41. else
  42. x = x/2 - extents.width/2 + extents.x_bearing
  43. end
  44. end
  45. cairo_move_to (cr, x, y+size)
  46. cairo_show_text (cr, text)
  47. cairo_stroke (cr)
  48. return extents.width , extents.height
  49. end
  50. function hex2rgb(c)
  51. c=tonumber("0x"..c)
  52. return ((c / 0x10000) % 0x100) / 255, ((c / 0x100) % 0x100) / 255,
  53. (c % 0x100) / 255
  54. end
  55. function draw_line(x,y,length,height,color,alpha,cap)
  56. cairo_set_line_width(cr,height)
  57. cairo_set_line_cap(cr,cap)
  58. red, green, blue = hex2rgb(color)
  59. if alpha == nil then
  60. cairo_set_source_rgb (cr, red, green, blue)
  61. else
  62. cairo_set_source_rgba (cr,red, green, blue, alpha)
  63. end
  64. cairo_move_to(cr,x,y)
  65. cairo_rel_line_to (cr,length,0)
  66. cairo_stroke (cr)
  67. end
  68. function draw_box(x,y,w,h,color,alpha)
  69. --~ print("Drawing a box with color " .. color)
  70. red,green,blue=hex2rgb(color)
  71. if alpha == nil then
  72. cairo_set_source_rgb (cr, red, green, blue)
  73. else
  74. cairo_set_source_rgba (cr,red, green, blue, alpha)
  75. end
  76. cairo_rectangle (cr,x,y,w,h);
  77. cairo_fill (cr);
  78. end
  79. function conky_main(...)
  80. if conky_window == nil then
  81. return
  82. end
  83. update=tonumber(conky_parse ('${updates}'))
  84. -- This is necessary to avoid a conky/Xorg quirk that would run the script 4 times in a row before respecting its update interval.
  85. if update == 0 or update > 1 then
  86. -- this is the whole magic that "sources" the output of the above command, which was passed to lua through the conkyrc
  87. -- {...} is a table that contains a variable number of arguments passed to main(...)
  88. cmd = table.concat({...},' ')
  89. print("Update " .. update .. ", executing command: " .. cmd)
  90. cmd = io.popen(cmd)
  91. output = cmd:read('*a')
  92. io.close(cmd)
  93. --~ print("To be parsed:\n" .. output)
  94. parse = load(output) -- creates a function "parse()" to be executed
  95. cmd=nil
  96. output=nil
  97. end
  98. cs = cairo_xlib_surface_create (conky_window.display,conky_window.drawable,conky_window.visual,conky_window.width,conky_window.height)
  99. cr = cairo_create (cs)
  100. --~ local fo = cairo_font_options_t:create()
  101. --~ tolua.takeownership(fo)
  102. --~ print(cairo_get_antialias(cr)) -- is this desired?
  103. --~ print(cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE)) -- is this desired?
  104. --~ print(cairo_font_options_set_antialias(cr, CAIRO_ANTIALIAS_NONE)) -- is this desired?
  105. --CAIRO_ANTIALIAS_DEFAULT --- Use the default antialiasing for the subsystem and target device, since 1.0
  106. --CAIRO_ANTIALIAS_NONE --- Use a bilevel alpha mask, since 1.0
  107. --CAIRO_ANTIALIAS_GRAY --- Perform single-color antialiasing (using shades of gray for black text on a white background, for example), since 1.0
  108. --CAIRO_ANTIALIAS_SUBPIXEL --- Perform antialiasing by taking advantage of the order of subpixel elements on devices such as LCD panels, since 1.0
  109. --CAIRO_ANTIALIAS_FAST --- Hint that the backend should perform some antialiasing but prefer speed over quality, since 1.12
  110. --CAIRO_ANTIALIAS_GOOD --- The backend should balance quality against performance, since 1.12
  111. --CAIRO_ANTIALIAS_BEST --- Hint that the backend should render at the highest quality, sacrificing speed if necessary, since 1.12
  112. parse()
  113. ----------------------------------------------------------
  114. ----------------------------------------------------------
  115. cairo_destroy (cr)
  116. cairo_surface_destroy (cs)
  117. cr = nil
  118. cs = nil
  119. collectgarbage();
  120. end