conky_clock.lua (11184B)
1 -- clock_conky_110.lua 2 -- by damo, August 2020 3 -- 4 -- With inspiration from: 5 -- Air Clock by Alison Pitt (londonali1010) (2009) 6 -- seamod_rings.lua http://custom-linux.deviantart.com/art/Conky-Seamod-v0-1-283461046 7 -- Boris Krinkel <olgmen> 8 9 -- to be used by clock_conky_110.conf 10 ------------------------------------------------------------------------ 11 12 require ('cairo_xlib') 13 require ('cairo') 14 15 -- called by "lua_startup_hook" in conky 16 -- "config" = /path/to/config file (set in conky) 17 function conky_load_config (config) 18 if file_exists(config) then 19 -- get clock settings from external config file 20 clock_variables = loadfile(config)() 21 else 22 return 23 end 24 end 25 26 function file_exists(name) 27 local f=io.open(name,"r") 28 if f~=nil then 29 io.close(f) return true 30 else 31 print("Configuration file not found") 32 return false 33 end 34 end 35 36 -- called by "lua_draw_hook_pre" in conky 37 -- draw clock using settings from "config" 38 function conky_clock (config) 39 40 if conky_window == nil then return end 41 42 local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height) 43 44 -- allow conky window to be established before trying to draw clock 45 local updates=conky_parse('${updates}') 46 update_num=tonumber(updates) 47 48 if update_num>1 then 49 for i, cv in pairs(clock_vars) do 50 check_settings (cv) 51 cr = cairo_create (cs) 52 display_clock (cv) 53 cairo_destroy (cr) 54 end 55 end 56 end 57 58 function rgb_to_r_g_b(colour,alpha) 59 return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha 60 end 61 62 function check_settings (t) -- lua doesn't have a case/switch statement: 63 -- set default values if necessary 64 if t.x == nil then -- if x,y,radius = "nil", set up clock relative to conky window. 65 t.x = conky_window.width/2 66 elseif t.y == nil then 67 t.y = t.x 68 elseif t.radius == nil then 69 t.radius = t.x*0.95 -- so it fits inside conky window 70 elseif t.border == nil then 71 t.border = false 72 elseif t.border_out == nil then 73 t.border = false 74 elseif t.color == nil then 75 t.color = 0xffffff 76 elseif t.alpha == nil then 77 t.alpha = 1 78 elseif t.line_width == nil then 79 t.line_width = 2 80 elseif t.border_width == nil then 81 t.border_width = t.line_width 82 elseif t.hours_num == nil then 83 t.hours_num = 12 84 elseif t.color_hands == nil then 85 t.color_hands = t.color 86 elseif t.alpha_hands == nil then 87 t.alpha_hands = 1 88 elseif t.color_sec == nil then 89 t.color_sec = t.color 90 elseif t.alpha_sec == nil then 91 t.alpha_sec = t.alpha 92 elseif t.color_face == nil then 93 t.color_face = 0xffffff 94 elseif t.alpha_face == nil then 95 t.alpha_face = 0.5 96 elseif t.color_marks == nil then 97 t.color_marks = t.color 98 elseif t.alpha_marks == nil then 99 t.alpha_marks = t.alpha 100 elseif t.color_center == nil then 101 t.color_center = t.color 102 elseif t.alpha_center == nil then 103 t.alpha_center = 1 104 elseif t.numerals == nil then 105 t.numerals = false 106 elseif t.text_radius == nil then 107 t.text_radius = 0.75 108 elseif t.font_name == nil then 109 t.font_name = "Noto Sans" 110 elseif t.font_size == nil then 111 t.font_size = 12 112 elseif t.italic == nil then 113 t.italic = false 114 elseif t.oblique == nil then 115 t.oblique = false 116 elseif t.bold == nil then 117 t.bold = false 118 elseif t.font_color == nil then 119 t.font_color = t.color 120 elseif t.font_alpha == nil then 121 t.font_alpha = t.alpha 122 elseif t.clock_face == nil then 123 t.clock_face = false 124 elseif t.hours_marks == nil then 125 t.hours_marks = true 126 elseif t.minutes_marks == nil then 127 t.minutes_marks = false 128 elseif t.clock_center == nil then 129 t.clock_center = false 130 elseif t.clock_center_radius == nil then 131 t.clock_center_radius = 0.1 132 elseif t.marks_radius_mins == nil then 133 t.marks_radius_mins = 0.95 134 elseif t.marks_radius_hrs == nil then 135 t.marks_radius_hrs = 0.9 136 elseif t.hour_radius == nil then 137 t.hour_radius = 0.65 138 elseif t.minute_radius == nil then 139 t.minute_radius = 0.8 140 elseif t.seconds_radius == nil then 141 t.seconds_radius = 0.9 142 elseif hour_hand_width == nil then 143 hour_hand_width = 6 144 elseif minute_hand_width == nil then 145 minute_hand_width = 4 146 elseif seconds_hand_width == nil then 147 seconds_hand_width = 2 148 elseif t.hand_style == nil then 149 t.hand_style = 1 150 end 151 end 152 153 function display_clock (t) 154 local slant = CAIRO_FONT_SLANT_NORMAL 155 local weight =CAIRO_FONT_WEIGHT_NORMAL 156 157 if t.italic then slant = CAIRO_FONT_SLANT_ITALIC end 158 if t.bold then weight = CAIRO_FONT_WEIGHT_BOLD end 159 160 cairo_select_font_face(cr, t.font_name, slant, weight) 161 cairo_set_font_size(cr, t.font_size) 162 te=cairo_text_extents_t:create() 163 cairo_text_extents (cr,t.text,te) 164 165 -- make sure clock radius has been set before drawing: 166 if t.radius then 167 -- draw border ring 168 if t.border then 169 cairo_set_source_rgba(cr, rgb_to_r_g_b(t.color, t.alpha)) 170 cairo_set_line_width(cr, t.border_width) 171 if t.border_out then -- draw border ring outside clock radius 172 cairo_arc (cr, t.x, t.y,t.radius+t.border_width/2, 0, 2*math.pi) 173 else 174 cairo_arc (cr, t.x, t.y, t.radius-t.border_width/2, 0, 2*math.pi) 175 end 176 cairo_stroke (cr) 177 end 178 179 -- Set clock face 180 if t.clock_face then 181 if t.color_face then 182 cairo_set_source_rgba(cr,rgb_to_r_g_b(t.color_face,t.alpha_face)) 183 cairo_arc(cr,t.x,t.y,t.radius,0,2*math.pi) 184 cairo_fill(cr) 185 end 186 end 187 188 -- draw hour marks 189 if t.hours_marks then 190 if t.color_marks then 191 cairo_set_source_rgba(cr,rgb_to_r_g_b(t.color_marks,t.alpha_marks)) 192 end 193 cairo_set_line_width(cr, t.line_width) 194 195 local i = 0 196 197 -- is clock 12H or 24H? 198 if t.hours_num == 12 then 199 num_hours = 11 200 angle_hours = math.rad(30) 201 else 202 num_hours = 23 203 angle_hours = math.rad(15) 204 end 205 206 local num = num_hours 207 local angle = angle_hours 208 209 for i= 0, num, 1 do 210 cairo_move_to(cr, t.x - math.sin(angle*i)*t.radius, t.y - math.cos(angle*i)*t.radius) 211 cairo_line_to(cr, t.x - math.sin(angle*i)*(t.radius*t.marks_radius_hrs), t.y - math.cos(angle*i)*(t.radius*t.marks_radius_hrs)) 212 cairo_stroke (cr) 213 end 214 end 215 216 -- draw minute marks 217 if t.minutes_marks then 218 if t.hours_num == 24 then 219 num_mins = 119 220 angle_mins = math.rad(3) 221 else 222 num_mins = 59 223 angle_mins = math.rad(6) 224 end 225 local num = num_mins 226 local angle = angle_mins 227 228 cairo_set_line_width(cr, t.line_width*0.5) 229 230 for i=0, num, 1 do 231 cairo_move_to(cr, t.x - math.sin(angle * i) * t.radius, t.y - math.cos(angle * i) * t.radius) 232 cairo_line_to(cr, t.x - math.sin(angle * i) * (t.radius * t.marks_radius_mins), t.y - math.cos(angle * i) * (t.radius*t.marks_radius_mins)) 233 cairo_stroke (cr) 234 end 235 end 236 -- numbers radius,color,alpha 237 if t.numerals then 238 cairo_save (cr) 239 cairo_translate(cr, t.x, t.y) 240 mx, my = 0, 0 241 local i = 0 242 243 -- is clock 12H or 24H? 244 if t.hours_num == 24 then 245 num_hours = 24 246 angle_hours = math.rad(15) 247 else 248 num_hours = 12 249 angle_hours = math.rad(30) 250 end 251 252 local num = num_hours 253 local angle = angle_hours 254 255 for i = 1, num, 1 do 256 mov_x = math.sin(angle*i)*(t.radius*t.text_radius) 257 mov_y = math.cos(angle*i)*(t.radius*t.text_radius) 258 te=cairo_text_extents_t:create() 259 cairo_text_extents (cr,i,te) 260 if t.font_color then 261 cairo_set_source_rgba(cr, rgb_to_r_g_b(t.font_color, t.font_alpha)) 262 end 263 mx = -te.width/2 264 my = -te.height/2-te.y_bearing 265 cairo_move_to(cr, mx + mov_x, my - mov_y) 266 cairo_show_text(cr, i) 267 end 268 cairo_restore (cr) 269 end -- end of numerals test 270 271 272 local hours = os.date("%I") 273 -- hours = hours + 1 274 if hours == 13 then 275 hours = 0 276 end 277 local mins = os.date("%M") 278 local secs = os.date("%S") 279 secs_arc = (2*math.pi/60)*secs 280 mins_arc = (2*math.pi/60)*mins 281 hours_arc = (2*math.pi/12)*hours + mins_arc/12 282 283 -- hour and minute hand color 284 if t.color_hands then 285 cairo_set_source_rgba(cr, rgb_to_r_g_b(t.color_hands, t.alpha_hands)) 286 if t.hand_style == 0 then -- set line end cap shape 287 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND) 288 else 289 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT) 290 end 291 -- draw hour hand 292 xh = t.x + t.hour_radius*t.radius*math.sin(hours_arc) 293 yh = t.y - t.hour_radius*t.radius*math.cos(hours_arc) 294 cairo_set_line_width(cr, t.hour_hand_width) 295 cairo_move_to(cr, t.x, t.y) 296 cairo_line_to(cr, xh, yh) 297 cairo_stroke(cr) 298 299 -- draw minute hand 300 xm = t.x + t.minute_radius*t.radius*math.sin(mins_arc) 301 ym = t.y - t.minute_radius*t.radius*math.cos(mins_arc) 302 cairo_set_line_width(cr, t.minute_hand_width) 303 cairo_move_to(cr, t.x, t.y) 304 cairo_line_to(cr, xm, ym) 305 cairo_stroke(cr) 306 307 -- draw seconds hand 308 -- set color for seconds hand 309 if t.color_sec then 310 cairo_set_source_rgba(cr, rgb_to_r_g_b(t.color_sec, t.alpha_sec)) 311 xs = t.x + t.seconds_radius*t.radius*math.sin(secs_arc) 312 ys = t.y - t.seconds_radius*t.radius*math.cos(secs_arc) 313 cairo_set_line_width(cr, t.seconds_hand_width) 314 cairo_move_to(cr, t.x, t.y) 315 cairo_line_to(cr,xs,ys) 316 cairo_stroke (cr) 317 end 318 end 319 320 -- draw centre on top of hands 321 if t.clock_center then 322 if t.color_center then 323 cairo_set_source_rgba(cr,rgb_to_r_g_b(t.color_center,t.alpha_center)) 324 cairo_arc(cr,t.x,t.y,t.radius*t.clock_center_radius,0,2*math.pi) 325 cairo_fill(cr) 326 end 327 end 328 end -- end of radius test 329 end