script.lua 5.0 KB

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