3 Licensed under GNU General Public License v2
9 local gears = require("gears")
10 local wibox = require("wibox")
11 local dpi = require("beautiful.xresources").apply_dpi
20 function binclock.dec2bin(num, bits)
21 local bits, t = bits or select(2, math.frexp(num)), {}
22 for b = bits, 1, -1 do
23 t[b] = math.fmod(num, 2)
24 num = (num - t[b]) / 2
29 function binclock.paintdot(cr, val, shift)
31 for _, bit in ipairs(binclock.dec2bin(val, 4)) do
33 cr:set_source(gears.color(binclock.color_active))
35 cr:set_source(gears.color(binclock.color_inactive))
37 cr:rectangle(shift, height, binclock.dotsize, binclock.dotsize)
39 height = height + binclock.dotsize + binclock.step
43 local function factory(args)
44 local args = args or {}
46 binclock.width = args.width or dpi(42)
47 binclock.height = args.height or dpi(18)
48 binclock.show_seconds = args.show_seconds or false
49 binclock.color_active = args.color_active or "#CCCCCC"
50 binclock.color_inactive = args.color_inactive or "#444444"
51 binclock.dotsize = math.floor(binclock.height / 5)
52 binclock.step = math.floor(binclock.dotsize / 3)
54 binclock.widget = wibox.widget {
55 fit = function(self, context, width, height)
56 return binclock.width, binclock.height
58 draw = function(self, context, cr, width, height)
61 local hour = string.format("%02d", t.hour)
62 local min = string.format("%02d", t.min)
63 local sec = string.format("%02d", t.sec)
66 if binclock.show_seconds then
69 local step = math.floor((binclock.width - col_count * binclock.dotsize) / 8)
71 binclock.paintdot(cr, string.sub(hour, 1, 1), step, 2)
72 binclock.paintdot(cr, string.sub(hour, 2, 2), binclock.dotsize + 2 * step)
74 binclock.paintdot(cr, string.sub(min, 1, 1), binclock.dotsize * 2 + 4 * step)
75 binclock.paintdot(cr, string.sub(min, 2, 2), binclock.dotsize * 3 + 5 * step)
77 if binclock.show_seconds then
78 binclock.paintdot(cr, string.sub(sec, 1, 1), binclock.dotsize * 4 + 7 * step)
79 binclock.paintdot(cr, string.sub(sec, 2, 2), binclock.dotsize * 5 + 8 * step)
82 layout = wibox.widget.base.make_widget
85 binclock.timer = gears.timer {
87 timeout = binclock.show_seconds and 1 or 60,
89 binclock.widget:emit_signal("widget::redraw_needed")