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
if reg.timer > 0 then
timers[reg.update] = {
timer = capi.timer({ timeout = reg.timer })
}
local tm = timers[reg.update].timer
local tm = timers[reg.timer] and timers[reg.timer].timer
tm = tm or capi.timer({ timeout = reg.timer })
if tm.connect_signal then
tm:connect_signal("timeout", reg.update)
else
tm:add_signal("timeout", reg.update)
end
tm:start()
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()
end
-- Initial update
tm:emit_signal("timeout")
end
@ -195,11 +198,23 @@ function vicious.unregister(widget, keep, reg)
end
end
-- Stop the timer
if timers[reg.update].timer.started then
timers[reg.update].timer:stop()
if not reg.running then
return reg
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
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
end