Code : Tout sélectionner
require "cairo" -- cairo graphic library
-- the funtion which paints the image in circle
function draw_image (ir,xc, yc, radius, path)
local w, h;
cairo_arc (ir, xc, yc, radius, 0, 2*math.pi);
cairo_clip (ir);
cairo_new_path (ir);
local image = cairo_image_surface_create_from_png (path);
w = cairo_image_surface_get_width (image);
h = cairo_image_surface_get_height (image);
cairo_scale (ir, 2*radius/w, 2*radius/h);
w = cairo_image_surface_get_width (image);
h = cairo_image_surface_get_height (image);
cairo_set_source_surface (ir, image, xc*(1/(2*radius/w)) - w/2, yc*(1/(2*radius/h)) - h/2);
cairo_paint (ir);
cairo_surface_destroy (image);
cairo_destroy(ir);
end
-- the funtion which will be called at the beginning of the run, used to setup a few global values
function conky_setup( )
noquote = 1;
quote = "";
author = "";
checkquote = 0;
start = 1;
changeweather = 0;
condition = "";
noweather = 1;
city = "";
region = "";
country = "";
code = "";
temp = "";
end
function conky_main( )
-- if no conky window then exit
if conky_window == nil then return end
-- the number of update
local updates = tonumber(conky_parse("${updates}"));
-- if not third update exit
if updates < 3 then return end
-- prepare cairo drawing surface
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height);
cr = cairo_create(cs);
-- for text extents
local extents = cairo_text_extents_t:create();
local text = "";
-- globals
local width = 1366;
local height = 748;
-- the centered time and date
-- varaibles
local hour = conky_parse('${time %I}');
local minute = conky_parse('${time %M}');
local part = conky_parse('${time %p}');
local day = conky_parse('${time %d}');
local month = conky_parse('${time %B}');
local year = conky_parse('${time %G}');
-- time
text = hour..":"..minute;
cairo_set_source_rgba(cr, 1, 1, 1, 1);
cairo_select_font_face(cr, "Radio Space", 0,0);
cairo_set_font_size(cr,height/6);
cairo_text_extents(cr,text,extents)
cairo_move_to(cr, width/2 - extents.width/2, height/2 - extents.height/4);
local line = height/2 - extents.height/4;
cairo_show_text(cr, text);
-- date
text = day.." "..month..", "..year;
cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
cairo_set_font_size(cr,height/15);
cairo_text_extents(cr,text,extents)
cairo_move_to(cr, width/2 - extents.width/2, line + extents.height*1.2);
local line = height/2 - extents.height/4;
cairo_show_text(cr, text);
-- mail checker
-- setup variables for web based content
local min = tonumber(conky_parse('${time %M}'));
local sec = tonumber(conky_parse('${time %S}'));
local file = io.popen("/sbin/route -n | grep -c '^0\.0\.0\.0'");
internet = tonumber(file:read("*a"));
io.close(file);
-- print(internet);
-- font settings
cairo_set_source_rgba(cr, 1, 1, 1, 1);
cairo_select_font_face(cr, "Sans Serif", 0 , 0);
cairo_set_font_size(cr, 15);
-- weather
local city_id = 612977;
hour = tonumber(hour);
if (hour*60 + min)%59 == 0 then
changeweather = 1;
end
if ((hour*60 + min)%60 == 0 and changeweather == 1) or start == 1 or noweather == 1 then
if internet == 1 then
print ("checkweather");
local file = io.popen("curl -m 120 'http://weather.yahooapis.com/forecastrss?w="..city_id.."&u=c'");
weather = file:read("*a")
io.close(file);
end
changeweather = 0;
if (weather == "") then
noweather = 1;
else
noweather = 0;
city ="";
region = "";
country = "";
condition = "";
code = "";
temp = "";
_,_,city,region,country = string.find(weather,"yweather:location%s*city=[\"](.-)[\"]%s*region=[\"](.-)[\"]%s*country=[\"](.-)[\"]");
_,_,condition,code,temp,_ = string.find(weather,"yweather:condition%s*text=[\"](.-)[\"]%s*code=[\"](.-)[\"]%s*temp=[\"](.-)[\"]%s*date=[\"](.-)[\"]");
end
-- start = 0;
end
if noweather == 1 then
text = "Cannot fetch weather info";
cairo_text_extents(cr, text, extents);
cairo_move_to(cr, width - 30 - extents.width, 30);
cairo_show_text(cr, text);
else
local ir = cairo_create(cs);
draw_image(ir, width - 40, 30, 30, "w/"..code..".png");
text = temp.."°C".." : "..condition;
cairo_text_extents(cr, text, extents);
cairo_move_to(cr, width - 80 - extents.width, 30);
cairo_show_text(cr, text);
text = city.." "..region..", "..country;
cairo_set_font_size(cr, 12);
cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
cairo_text_extents(cr, text, extents);
cairo_move_to(cr, width - 80 - extents.width, 45);
cairo_show_text(cr, text);
end
-- quotes
if hour%11 == 0 then
checkquote = 1;
end
if (hour%12 == 0 and checkquote == 1) or start == 1 or noquote == 1 then
if internet == 1 then
print ("checkquote");
local file = io.popen("curl -m 100 'http://feeds.feedburner.com/brainyquote/QUOTEBR'");
output = file:read("*a");
io.close(file);
end
checkquote = 0;
start = 0;
if (output == "") then
noquote = 1;
else
noquote = 0;
quote = "";
author = "";
local nex = 0;
local a = "";
_,nex,a = string.find(output, "<item>%s*(.-)%s*</item>",nex);
_,_,author = string.find(a, "<title>%s*(.-)%s*</title>");
_,_,quote = string.find(a, "<description>%s*(.-)%s*</description>");
end
end
if(noquote == 0) then
text = author;
cairo_set_font_size(cr, 16);
cairo_set_source_rgba(cr, 1, 1, 1, 1);
cairo_text_extents(cr, text, extents);
cairo_move_to(cr, width/2 - extents.width/2, height - 30);
cairo_show_text(cr, text);
text = quote;
cairo_select_font_face(cr, "Comic Sans MS", 0, 1);
cairo_text_extents(cr,text,extents);
cairo_move_to(cr, width/2 - extents.width/2, height - 50);
cairo_show_text(cr, text);
else
print("Trouble fetching quote")
end
-- destroying the cairo surface
cairo_destroy(cr);
cairo_surface_destroy(cs);
cr=nil;
end