init: share timers when possible

We need time to give this a proper test with various usage scenarios;
multiple screens (and thus widget object instances), widgets
suspending and resuming and so on. Most benefits should come from
running on battery power (and if not suspending all widgets but
Battery it self on battery power), with less wake-ups.

Signed-off-by: Adrian C. (anrxc) <anrxc@sysphere.org>
This commit is contained in:
Dodo 2013-11-07 05:11:49 +01:00 committed by Adrian C. (anrxc)
parent 75cd1039cf
commit 211d4509c1
1 changed files with 25 additions and 10 deletions

View File

@ -116,18 +116,21 @@ local function regregister(reg)
-- Start the timer -- Start the timer
if reg.timer > 0 then if reg.timer > 0 then
timers[reg.update] = { local tm = timers[reg.timer] and timers[reg.timer].timer
timer = capi.timer({ timeout = reg.timer }) tm = tm or capi.timer({ timeout = reg.timer })
}
local tm = timers[reg.update].timer
if tm.connect_signal then if tm.connect_signal then
tm:connect_signal("timeout", reg.update) tm:connect_signal("timeout", reg.update)
else else
tm:add_signal("timeout", reg.update) tm:add_signal("timeout", reg.update)
end end
if not timers[reg.timer] then
timers[reg.timer] = { timer = tm, refs = 1 }
else
timers[reg.timer].refs = timers[reg.timer].refs + 1
end
if not tm.started then
tm:start() tm:start()
end
-- Initial update -- Initial update
tm:emit_signal("timeout") tm:emit_signal("timeout")
end end
@ -195,11 +198,23 @@ function vicious.unregister(widget, keep, reg)
end end
end end
-- Stop the timer if not reg.running then
if timers[reg.update].timer.started then return reg
timers[reg.update].timer:stop() end
-- Disconnect from timer
local tm = timers[reg.timer]
if tm.timer.disconnect_signal then
tm.timer:disconnect_signal("timeout", reg.update)
else
tm.timer:remove_signal("timeout", reg.update)
end end
reg.running = false reg.running = false
-- Stop the timer
tm.refs = tm.refs - 1
if tm.refs == 0 and tm.timer.started then
tm.timer:stop()
end
return reg return reg
end end