Code:
--[[
Conky Widgets by londonali1010 (2009)
This script is meant to be a "shell" to hold a suite of widgets for use in Conky.
To configure:
+ Copy the widget's code block (will be framed by --(( WIDGET NAME )) and --(( END WIDGET NAME )), with "[" instead of "(") somewhere between "require 'cairo'" and "function conky_widgets()", ensuring not to paste into another widget's code block
+ To call the widget, add the following just before the last "end" of the entire script:
cr = cairo_create(cs)
NAME_OF_FUNCTION(cr, OPTIONS)
cairo_destroy(cr)
+ Replace OPTIONS with the options for your widget (should be specified in the widget's code block)
Call this script in Conky using the following before TEXT (assuming you save this script to ~/scripts/conky_widgets.lua):
lua_load ~/scripts/conky_widgets.lua
lua_draw_hook_pre widgets
Changelog:
+ v1.0 -- Original release (17.10.2009)
]]
require 'cairo'
require 'imlib2'
--[[ CLOCK WIDGET ]]
function ring(cr, name, arg, max, bgc, bga, fgc, fga, xc, yc, r, t, sa, ea)
local function rgb_to_r_g_b(colour,alpha)
return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
end
local function draw_ring(pct)
local angle_0=sa*(2*math.pi/360)-math.pi/2
local angle_f=ea*(2*math.pi/360)-math.pi/2
local pct_arc=pct*(angle_f-angle_0)
-- Draw background ring
cairo_arc(cr,xc,yc,r,angle_0,angle_f)
cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))
cairo_set_line_width(cr,t)
cairo_stroke(cr)
-- Draw indicator ring
cairo_arc(cr,xc,yc,r,angle_0,angle_0+pct_arc)
cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))
cairo_stroke(cr)
end
local function setup_ring()
local str = ''
local value = 0
str = string.format('${%s %s}', name, arg)
str = conky_parse(str)
value = tonumber(str)
if value == nil then value = 0 end
pct = value/max
draw_ring(pct)
end
local updates=conky_parse('${updates}')
update_num=tonumber(updates)
if update_num>5 then setup_ring() end
end
--[[ END CLOCK WIDGET ]]
--[[ GRADIENT RING WIDGET ]]
--[[ Options (name, arg, max, colour, alpha, x, y, inner_radius, outer_radius, frac, thickness, start_angle, end_angle):
"name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.
"arg" is the argument to the stat type, e.g. if in Conky you would write ${cpu cpu0}, 'cpu0' would be the argument. If you would not use an argument in the Conky variable, use ''.
"max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.
"fg_colour" is the colour of the ring.
"fg_alpha" is the alpha value of the ring.
"x" and "y" are the x and y coordinates of the centre of the ring, relative to the top left corner of the Conky window.
"inner_radius" is the inner radius of the ring.
"outer_radius" is the outer radius of the ring.
"frac" determines the extent of the gradient around the ring - 2 implies the gradient fades halfway around the ring, 4 equals a quarter of the way, etc...
"thickness" is the thickness of the ring, centred around the radius.
"start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.
"end_angle" is the ending angle of the ring, in degrees, clockwise from top. Value can be either positive or negative, but must be larger (e.g. more clockwise) than start_angle. ]]
function gradient_ring(cr, name, arg, max, fgc, fga, xc, yc, ring_i, ring_o, frac, t, sa, ea)
local function rgb_to_r_g_b(colour,alpha)
return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
end
local function draw_gradient_ring(pct)
local angle_0=sa*(2*math.pi/360)-math.pi/2
local angle_f=ea*(2*math.pi/360)-math.pi/2
local pct_arc=pct*(angle_f-angle_0)
for i = 1,max/frac do
cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga*i/max)) --for flat shading of bars
--local pat=cairo_pattern_create_linear(xc-ring_i*math.sin(angle_0+t_arc+2*math.pi*i/max),yc+ring_i*math.cos(angle_0+t_arc+2*math.pi*i/max),xc-ring_o*math.sin(angle_0+t_arc+2*math.pi*i/max),yc+ring_o*math.cos(angle_0+t_arc+2*math.pi*i/max))
--cairo_pattern_add_color_stop_rgba(pat,0,rgb_to_r_g_b(fgc,0))
--cairo_pattern_add_color_stop_rgba(pat,1,rgb_to_r_g_b(fgc,fga*i/max))
--cairo_set_source(cr,pat) -- for gradient shading of bars
cairo_move_to(cr,xc-ring_i*math.cos(angle_0+pct_arc+2*math.pi*i/max),yc-ring_i*math.sin(angle_0+pct_arc+2*math.pi*i/max))
cairo_line_to(cr,xc-ring_o*math.cos(angle_0+pct_arc+2*math.pi*i/max),yc-ring_o*math.sin(angle_0+pct_arc+2*math.pi*i/max))
cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)
cairo_set_line_width(cr,t)
cairo_stroke(cr)
end
end
local function setup_gradient_ring()
local str = ''
local value = 0
str = string.format('${%s %s}', name, arg)
str = conky_parse(str)
value = tonumber(str)
if value == nil then value = 0 end
pct = value/max
draw_gradient_ring(pct)
end
local updates=conky_parse('${updates}')
update_num=tonumber(updates)
if update_num>5 then setup_gradient_ring() end
end
--[[ END GRADIENT RING WIDGET ]]
--[[ PHOTO ALBUM WIDGET ]]
album_dir = "/home/graham/Pictures/Conky/"
-- "xc" and "yc" are the coordinates of the centre of the photo album, relative to the top left corner of the Conky window, in pixels
xc, yc = 400, 600
-- "w_max" and "h_max" are the maximum dimensions, in pixels, that you want the widget to be. The script will ensure that the photo album fits inside the box bounded by w_max and h_max
w_max, h_max = 200, 200
-- "t" is the thickness of the frame, in pixels
t = 3
-- "update_interval" is the number of Conky updates between refreshes
update_interval = 10
function get_file_to_use()
num_files = tonumber(conky_parse("${exec ls -A " .. album_dir .. " | wc -l}"))
if num_files == nil then num_files = 0 end
if num_files == 0 then return "none" end
updates = tonumber(conky_parse("${updates}"))
whole = math.ceil(updates/update_interval)
if whole <= num_files
then
num_file_to_show = whole
else
whole = whole % num_files
num_file_to_show = whole
end
return conky_parse("${exec ls " .. album_dir .. " | sed -n " .. num_file_to_show .. "p}")
end
function init_drawing_surface()
imlib_set_cache_size(4096 * 1024)
imlib_context_set_dither(1)
end
function draw_frame()
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)
cairo_rectangle(cr, xc - width/2 - t, yc - height/2 - t, width + 2*t, height + 2*t)
cairo_set_source_rgba(cr, 1, 1, 1, 0.8)
cairo_fill(cr)
cairo_destroy(cr)
end
function draw_image()
init_drawing_surface()
image = imlib_load_image(album_dir .. filename)
if image == nil then return end
imlib_context_set_image(image)
w_img, h_img = imlib_image_get_width(), imlib_image_get_height()
if w_img >= h_img
then
width = w_max - 2*t
height = width * (h_img/w_img)
else
height = h_max - 2*t
width = height * (w_img/h_img)
end
draw_frame()
buffer = imlib_create_image(width, height)
imlib_context_set_image(buffer)
imlib_blend_image_onto_image(image, 0, 0, 0, w_img, h_img, 0, 0, width, height)
imlib_context_set_image(image)
imlib_free_image()
imlib_context_set_image(buffer)
imlib_render_image_on_drawable(xc - width/2, yc - height/2)
imlib_free_image()
end
function conky_photo_album()
if conky_window == nil then return end
filename = get_file_to_use()
if filename == "none"
then
print(album_dir .. ": No files found")
else
draw_image()
end
end
--[[ END PHOTO ALBUM WIDGET ]]
--[[ BACKGROUND WIDGET ]]
-- Change these settings to affect your background.
-- "corner_r" is the radius, in pixels, of the rounded corners. If you don't want rounded corners, use 0.
corner_r=15
-- Set the colour and transparency (alpha) of your background.
bg_colour=0xffffff
bg_alpha=0.2
require 'cairo'
function rgb_to_r_g_b(colour,alpha)
return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
end
function conky_draw_bg()
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)
cairo_move_to(cr,corner_r,0)
cairo_line_to(cr,w-corner_r,0)
cairo_curve_to(cr,w,0,w,0,w,corner_r)
cairo_line_to(cr,w,h-corner_r)
cairo_curve_to(cr,w,h,w,h,w-corner_r,h)
cairo_line_to(cr,corner_r,h)
cairo_curve_to(cr,0,h,0,h,0,h-corner_r)
cairo_line_to(cr,0,corner_r)
cairo_curve_to(cr,0,0,0,0,corner_r,0)
cairo_close_path(cr)
cairo_set_source_rgba(cr,rgb_to_r_g_b(bg_colour,bg_alpha))
cairo_fill(cr)
end
--[[ END BACKGROUND WIDGET ]]
function conky_widgets()
if conky_window == nil then return end
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)
cairo_destroy(cr)
-------------------[[ TIME SETTINGS ]]---------------
cr = cairo_create(cs)
ring(cr, 'time', '%I', 12, 0xFFFFFF, 0.1, 0xFFFFFF, 0.2, 120, 70, 45, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'time', '%M', 60, 0xFFFFFF, 0.1, 0xFFFFFF, 0.2, 120, 70, 55, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'time', '%S', 60, 0xFFFFFF, 0.1, 0xFFFFFF, 0.3, 120, 70, 63, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
-------------------[[ END TIME SETTINGS ]]---------------
-------------------[[ GRADIENT RING SETTINGS ]]---------------
cr = cairo_create(cs)
gradient_ring(cr, '', '', 12, 0xFF0000, 10.0, 120, 70, 65, 60, 1, 2, 0, 360)
cairo_destroy(cr)
-------------------[[ END GRADIENT RING SETTINGS ]]---------------
-------------------[[ STORAGE RINGS SETTINGS ]]---------------
cr = cairo_create(cs)
ring(cr, 'fs_used_perc', '/media/ELEMENTS', 100, 0xFFFFFF, 0.1, 0xFF0000, 1, 260, 60, 45, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'fs_used_perc', '/media/Pen_Drive', 100, 0xFFFFFF, 0.4, 0xFF0000, 1, 360, 60, 45, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'fs_used_perc', '/media/Cam_Card', 100, 0xFFFFFF, 0.4, 0xFF0000, 1, 460, 60, 45, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'fs_used_perc', '/media/MOVIES', 100, 0xFFFFFF, 0.1, 0xFF0000, 1, 560, 60, 45, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'fs_used_perc', '/media/MOVIES', 100, 0xFFFFFF, 0.1, 0xFF0000, 1, 660, 60, 45, 7, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
-------------------[[ END STORAGE RINGS SETTINGS ]]---------------
-------------------[[ CPU RINGS SETTINGS ]]---------------
cr = cairo_create(cs)
ring(cr, 'cpu', 'cpu0', 100, 0xFFFFFF, 0, 0xFF8C00, 1.0, 850, 148, 140, 10, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'memperc', '', 100, 0xFFFFFF, 0, 0x00BFFF, 1.0, 850, 148, 130, 10, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'swapperc', '', 100, 0xFFFFFF, 0, 0xFFFF58, 1.0, 850, 148, 120, 10, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'fs_used_perc', '/', 100, 0xFFFFFF, 0, 0xFFDEAD, 1.0, 850, 148, 110, 10, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'fs_used_perc', '/media/Shitsta', 100, 0xFFFFFF, 0, 0xFFFF00, 1.0, 850, 148, 100, 10, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
cr = cairo_create(cs)
ring(cr, 'battery_percent', 'BAT0', 100, 0xFFFFFF, 0, 0x00FF2A, 1.0, 850, 148, 90, 10, 0, 360) -- options: name, arg, max, bg_colour, bg_alpha, fg_colour, fg_alpha, xc, yc, radius, thickness, start_angle, end_angle
cairo_destroy(cr)
-------------------[[ END CPU RINGS SETTINGS ]]---------------
-------------------[[ PHOTO ALBUM SETTINGS ]]---------------
--conky_photo_album('/home/graham/Pictures/Conky/', 400, 600, 200, 200, 10, 10)
-------------------[[ END PHOTO ALBUM SETTINGS ]]---------------
conky_draw_bg(0xFFFFFF, 0.4)
end
Bookmarks