From e051d3d7038b456323a42079b937fc5b2c6df60a Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Sat, 10 Sep 2022 01:56:01 +0200 Subject: [PATCH] Kobo: Fix a few bad interactions between suspend & checkUnexpectedWakeup (#9499) * Kobo: Make sure checkUnexpectedWakeup won't run between scheduled suspend calls Because of the suspend_wait_timeout, a previously scheduled check *could* shortcircuit a suspend scheduled a tiny bit later by a real Power event. This led to fun breakage when you plugged/unplugged a "sleeping" device, for instance. Fix #9457 * And clear up the unexpected_wakeup_count semantics Bits of an older design seeped through and were causing weird corner-cases... --- frontend/device/generic/device.lua | 2 +- frontend/device/kobo/device.lua | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/frontend/device/generic/device.lua b/frontend/device/generic/device.lua index 45f04753c..cdb250976 100644 --- a/frontend/device/generic/device.lua +++ b/frontend/device/generic/device.lua @@ -284,7 +284,7 @@ function Device:onPowerEvent(ev) -- Already in screen saver mode, no need to update UI/state before -- suspending the hardware. This usually happens when sleep cover -- is closed after the device was sent to suspend state. - logger.dbg("Already in screen saver mode, suspending...") + logger.dbg("Already in screen saver mode, going back to suspend...") self:rescheduleSuspend() end -- else we were not in screensaver mode diff --git a/frontend/device/kobo/device.lua b/frontend/device/kobo/device.lua index 3ba2a12be..081cee19f 100644 --- a/frontend/device/kobo/device.lua +++ b/frontend/device/kobo/device.lua @@ -847,6 +847,14 @@ local function getProductId() return product_id end +-- NOTE: We overload this to make sure checkUnexpectedWakeup doesn't trip *before* the newly scheduled suspend +function Kobo:rescheduleSuspend() + local UIManager = require("ui/uimanager") + UIManager:unschedule(self.suspend) + UIManager:unschedule(self.checkUnexpectedWakeup) + UIManager:scheduleIn(self.suspend_wait_timeout, self.suspend, self) +end + function Kobo:checkUnexpectedWakeup() local UIManager = require("ui/uimanager") -- Just in case another event like SleepCoverClosed also scheduled a suspend @@ -864,10 +872,8 @@ function Kobo:checkUnexpectedWakeup() else -- We've hit an early resume, assume this is unexpected (as we only run if Kobo:resume hasn't already). logger.dbg("Kobo suspend: checking unexpected wakeup number", self.unexpected_wakeup_count) - if self.unexpected_wakeup_count == 0 or self.unexpected_wakeup_count > 20 then - -- Don't put device back to sleep under the following two cases: - -- 1. a resume event triggered Kobo:resume() function - -- 2. trying to put device back to sleep more than 20 times after unexpected wakeup + if self.unexpected_wakeup_count > 20 then + -- If we've failed to put the device back to sleep over 20 consecutive times, we give up. -- Broadcast a specific event, so that AutoSuspend can pick up the baton... local Event = require("ui/event") UIManager:broadcastEvent(Event:new("UnexpectedWakeupLimit")) @@ -879,8 +885,6 @@ function Kobo:checkUnexpectedWakeup() end end -function Kobo:getUnexpectedWakeup() return self.unexpected_wakeup_count end - --- The function to put the device into standby, with enabled touchscreen. -- max_duration ... maximum time for the next standby, can wake earlier (e.g. Tap, Button ...) function Kobo:standby(max_duration) @@ -1034,10 +1038,8 @@ function Kobo:suspend() -- NOTE: We unflag /sys/power/state-extended in Kobo:resume() to keep -- things tidy and easier to follow - -- Kobo:resume() will reset unexpected_wakeup_count = 0 to signal an - -- expected wakeup, which gets checked in checkUnexpectedWakeup(). + -- Kobo:resume() will reset unexpected_wakeup_count and unschedule the check to signal a sane wakeup. self.unexpected_wakeup_count = self.unexpected_wakeup_count + 1 - -- We're assuming Kobo:resume() will be called in the next 15 seconds in ordrer to cancel that check. logger.dbg("Kobo suspend: scheduling unexpected wakeup guard") UIManager:scheduleIn(15, self.checkUnexpectedWakeup, self) end @@ -1080,8 +1082,11 @@ function Kobo:resume() end function Kobo:usbPlugOut() - -- Reset the unexpected wakeup shenanigans, since we're no longer charging, meaning power savings are now critical again ;). - self.unexpected_wakeup_count = 0 + -- Rewind the unexpected wakeup counter, since we're no longer charging, meaning power savings are now critical again ;). + -- NOTE: We don't reset it to 0 because, semantically, only resume should ever be allowed to do so. + if self.unexpected_wakeup_count > 0 then + self.unexpected_wakeup_count = 1 + end end function Kobo:saveSettings()