From 211d4509c146a3224b859c3e16b609a1f769d568 Mon Sep 17 00:00:00 2001 From: Dodo Date: Thu, 7 Nov 2013 05:11:49 +0100 Subject: [PATCH] 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) --- init.lua | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/init.lua b/init.lua index 275799a..46fdec5 100644 --- a/init.lua +++ b/init.lua @@ -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