Conky has now cairo support embedded


22.07.2009 | Categories: Ubuntu


Lua bindings for Cairo enable us to use Cairo within Conky!

A great feature is in the pipeline for the next release of Conky.

Not happy to have included built-in lua support, Brenden and co. have now included bindings for Cairo and Imlib2 as well.

This will allow us to use the whole Cairo API within Conky!

The following code snippet (inspired by cairographics.org) will show the concept. Save the following lua code into your home directory (lets call it cairo.lua):

-- Conky Lua scripting example
--
require 'cairo'
do
	function conky_round_rect(cr, x0, y0, w, h, r)
		 if (w == 0) or (h == 0) then return end
		 local x1 = x0 + w
		 local y1 = y0 + h
		 if w/2 < r then
    		    if h/2 < r then
		        cairo_move_to  (cr, x0, (y0 + y1)/2)
			cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0)
        		cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2)
        		cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1)
        		cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2)
		    else
			cairo_move_to  (cr, x0, y0 + r)
        		cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0)
        		cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + r)
        		cairo_line_to (cr, x1 , y1 - r)
        		cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1)
        		cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- r)
		    end
		 else
		    if h/2 < r then
        		cairo_move_to  (cr, x0, (y0 + y1)/2)
        		cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + r, y0)
        		cairo_line_to (cr, x1 - r, y0)
        		cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2)
        		cairo_curve_to (cr, x1, y1, x1, y1, x1 - r, y1)
        		cairo_line_to (cr, x0 + r, y1)
        		cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2)
		    else
			cairo_move_to  (cr, x0, y0 + r)
        		cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + r, y0)
        		cairo_line_to (cr, x1 - r, y0)
        		cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + r)
        		cairo_line_to (cr, x1 , y1 - r)
        		cairo_curve_to (cr, x1, y1, x1, y1, x1 - r, y1)
        		cairo_line_to (cr, x0 + r, y1)
        		cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- r)
		   end
		 end
		 cairo_close_path (cr)
	end

	function conky_cairo_test()
		if conky_window == nil then return end
		local w = conky_window.width
		local h = conky_window.height
		local cs = cairo_xlib_surface_create(conky_window.display,
		      conky_window.drawable, conky_window.visual, w, h)
		cr = cairo_create(cs)

		conky_round_rect(cr, 0.7825*w, 0.0675*h, 0.1*w, 0.1*h, 0.04*w)
		local pat = cairo_pattern_create_linear (0.0, 0.065*h,
		      0.0, 0.165*h)
		cairo_pattern_add_color_stop_rgba (pat, 0, 0, 0, 0, 1.0)
		cairo_pattern_add_color_stop_rgba (pat, 1, 0.6, 0.6, 0.6, 0.5)
		cairo_set_source (cr, pat)
		cairo_fill_preserve (cr)
		cairo_pattern_destroy (pat)
		cairo_set_source_rgba (cr, 0.5, 0, 0, 0.5)
		cairo_set_line_width (cr, 10.0)
		cairo_stroke (cr)
	end
end

Now save the following code into a .conkyrc.cairo file, again into your home directory:

lua_load ~/cairo.lua
lua_draw_hook_pre cairo_test
TEXT
${color orange}SYSTEM ${hr 2}$color
$nodename $sysname $kernel on $machine


${alignc}${color red}${time %a %d %b}
${alignc}${time %H:%M}

Fire up conky with:

conky -d -f "Radio Space" -c .conkyrc.cairo
and you should see this on your desktop:

Cairo example

So, start cranking up your conky and show the world the best desktop!

Comments