2017-06-23 17:04:11 +00:00
local Device = require ( " device " )
2022-07-02 18:20:35 +00:00
-- If a device can power off or go into standby, it can also suspend ;).
if not Device : canSuspend ( ) then
2018-10-31 22:48:36 +00:00
return { disabled = true , }
end
2017-06-23 17:04:11 +00:00
2022-11-17 04:53:35 +00:00
local Math = require ( " optmath " )
2022-03-29 20:59:10 +00:00
local NetworkMgr = require ( " ui/network/manager " )
2017-06-27 04:39:23 +00:00
local PluginShare = require ( " pluginshare " )
2022-12-12 02:05:41 +00:00
local PowerD = Device : getPowerDevice ( )
2017-06-23 17:04:11 +00:00
local UIManager = require ( " ui/uimanager " )
local WidgetContainer = require ( " ui/widget/container/widgetcontainer " )
2022-11-17 04:53:35 +00:00
local datetime = require ( " datetime " )
2017-06-23 17:04:11 +00:00
local logger = require ( " logger " )
2022-05-05 19:00:22 +00:00
local time = require ( " ui/time " )
2017-06-23 17:04:11 +00:00
local _ = require ( " gettext " )
2019-09-12 12:15:08 +00:00
local T = require ( " ffi/util " ) . template
2017-06-23 17:04:11 +00:00
2021-11-28 21:18:44 +00:00
local default_autoshutdown_timeout_seconds = 3 * 24 * 60 * 60 -- three days
local default_auto_suspend_timeout_seconds = 15 * 60 -- 15 minutes
2022-03-29 20:59:10 +00:00
local default_auto_standby_timeout_seconds = 4 -- 4 seconds; should be safe on Kobo/Sage
2023-09-01 15:55:24 +00:00
local default_standby_timeout_after_resume_seconds = 4 -- 4 seconds; should be safe on Kobo/Sage, not customizable
2022-12-12 02:05:41 +00:00
local default_kindle_t1_timeout_reset_seconds = 5 * 60 -- 5 minutes (i.e., half of the standard t1 timeout).
2019-09-12 12:15:08 +00:00
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
local AutoSuspend = WidgetContainer : extend {
2019-09-12 12:15:08 +00:00
name = " autosuspend " ,
is_doc_only = false ,
AutoSuspend: Unbreak scheduling & settings across instances
Plugins are loaded *once*, but torn-down/instantiated multiple times,
and sometimes in the reverse order.
As such, if we use a public function member as the scheduled task, we're
always pointing to the same function, and going from FM to RD
effectively *un*schedules it.
Instead, use an instance-specific closure, so that each instance
schedules & unschedules don't affect each other.
In the same vein, settings ought to be read at instantiation, not at
loading, otherwise, changing a setting in the FM, then switching to the
reader will not pick up the changes.
2021-04-30 17:37:42 +00:00
autoshutdown_timeout_seconds = default_autoshutdown_timeout_seconds ,
auto_suspend_timeout_seconds = default_auto_suspend_timeout_seconds ,
2022-03-29 20:59:10 +00:00
auto_standby_timeout_seconds = default_auto_standby_timeout_seconds ,
2022-05-05 19:00:22 +00:00
last_action_time = 0 ,
AutoSuspend: Don't send LeaveStandby events from a zombie plugin instance (#9124)
Long story short: the LeaveStandby event is sent via `tickAfterNext`, so if we tear down the plugin right after calling it (in this case, that means that the very input event that wakes the device up from suspend is one that kills ReaderUI or FileManager), what's in UIManager's task queue isn't the actual function, but the anonymous nextTick wrapper constructed by `tickAfterNext` (c.f.,
https://github.com/koreader/koreader/issues/9112#issuecomment-1133999385).
Tweak `UIManager:tickAfterNext` to return a reference to said wrapper, so that we can store it and unschedule that one, too, in `AutoSuspend:onCloseWidget`.
Fix #9112 (many thanks to [@boredhominid](https://github.com/boredhominid) for his help in finding a repro for this ;)).
Re: #8638, as the extra debugging facilities (i.e., ebb81b98451e2a8f54c46f51e861c19fdfb40499) added during testing might help pinpoint the root issue for that one, too.
Also includes a minor simplification to `UIManager:_checkTasks`, and various other task queue related codepaths (e.g., `WakeupMgr`) ;).
2022-05-25 21:36:41 +00:00
is_standby_scheduled = nil ,
AutoSuspend: Unbreak scheduling & settings across instances
Plugins are loaded *once*, but torn-down/instantiated multiple times,
and sometimes in the reverse order.
As such, if we use a public function member as the scheduled task, we're
always pointing to the same function, and going from FM to RD
effectively *un*schedules it.
Instead, use an instance-specific closure, so that each instance
schedules & unschedules don't affect each other.
In the same vein, settings ought to be read at instantiation, not at
loading, otherwise, changing a setting in the FM, then switching to the
reader will not pick up the changes.
2021-04-30 17:37:42 +00:00
task = nil ,
2022-12-12 02:05:41 +00:00
kindle_task = nil ,
2022-04-10 17:25:37 +00:00
standby_task = nil ,
AutoSuspend: Don't send LeaveStandby events from a zombie plugin instance (#9124)
Long story short: the LeaveStandby event is sent via `tickAfterNext`, so if we tear down the plugin right after calling it (in this case, that means that the very input event that wakes the device up from suspend is one that kills ReaderUI or FileManager), what's in UIManager's task queue isn't the actual function, but the anonymous nextTick wrapper constructed by `tickAfterNext` (c.f.,
https://github.com/koreader/koreader/issues/9112#issuecomment-1133999385).
Tweak `UIManager:tickAfterNext` to return a reference to said wrapper, so that we can store it and unschedule that one, too, in `AutoSuspend:onCloseWidget`.
Fix #9112 (many thanks to [@boredhominid](https://github.com/boredhominid) for his help in finding a repro for this ;)).
Re: #8638, as the extra debugging facilities (i.e., ebb81b98451e2a8f54c46f51e861c19fdfb40499) added during testing might help pinpoint the root issue for that one, too.
Also includes a minor simplification to `UIManager:_checkTasks`, and various other task queue related codepaths (e.g., `WakeupMgr`) ;).
2022-05-25 21:36:41 +00:00
going_to_suspend = nil ,
2017-06-23 17:04:11 +00:00
}
2022-03-29 20:59:10 +00:00
function AutoSuspend : _enabledStandby ( )
return Device : canStandby ( ) and self.auto_standby_timeout_seconds > 0
end
2017-06-23 17:04:11 +00:00
function AutoSuspend : _enabled ( )
2022-07-02 18:20:35 +00:00
-- NOTE: Plugin is only enabled if Device:canSuspend(), so we can elide the check here
2021-03-06 21:44:18 +00:00
return self.auto_suspend_timeout_seconds > 0
2017-06-23 17:04:11 +00:00
end
2019-09-12 12:15:08 +00:00
function AutoSuspend : _enabledShutdown ( )
2019-09-14 13:52:53 +00:00
return Device : canPowerOff ( ) and self.autoshutdown_timeout_seconds > 0
2019-09-12 12:15:08 +00:00
end
2021-03-10 01:21:02 +00:00
function AutoSuspend : _schedule ( shutdown_only )
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
if not self : _enabled ( ) and not self : _enabledShutdown ( ) then
logger.dbg ( " AutoSuspend: suspend/shutdown timer is disabled " )
2017-06-23 17:04:11 +00:00
return
end
2022-05-05 19:00:22 +00:00
local suspend_delay_seconds , shutdown_delay_seconds
2022-04-22 16:59:48 +00:00
local is_charging
-- On devices with an auxiliary battery, we only care about the auxiliary battery being charged...
2022-12-12 02:05:41 +00:00
if Device : hasAuxBattery ( ) and PowerD : isAuxBatteryConnected ( ) then
is_charging = PowerD : isAuxCharging ( ) and not PowerD : isAuxCharged ( )
2022-04-22 16:59:48 +00:00
else
2022-12-12 02:05:41 +00:00
is_charging = PowerD : isCharging ( ) and not PowerD : isCharged ( )
2022-04-22 16:59:48 +00:00
end
2022-07-02 18:20:35 +00:00
-- We *do* want to make sure we attempt to go into suspend/shutdown again while *fully* charged, though.
2022-04-22 16:59:48 +00:00
if PluginShare.pause_auto_suspend or is_charging then
2022-05-05 19:00:22 +00:00
suspend_delay_seconds = self.auto_suspend_timeout_seconds
shutdown_delay_seconds = self.autoshutdown_timeout_seconds
2017-06-27 04:39:23 +00:00
else
2022-05-05 19:00:22 +00:00
local now = UIManager : getElapsedTimeSinceBoot ( )
suspend_delay_seconds = self.auto_suspend_timeout_seconds - time.to_number ( now - self.last_action_time )
shutdown_delay_seconds = self.autoshutdown_timeout_seconds - time.to_number ( now - self.last_action_time )
2017-06-27 04:39:23 +00:00
end
2020-10-17 10:59:24 +00:00
-- Try to shutdown first, as we may have been woken up from suspend just for the sole purpose of doing that.
2022-05-05 19:00:22 +00:00
if self : _enabledShutdown ( ) and shutdown_delay_seconds <= 0 then
2019-09-12 12:15:08 +00:00
logger.dbg ( " AutoSuspend: initiating shutdown " )
UIManager : poweroff_action ( )
2022-05-05 19:00:22 +00:00
elseif self : _enabled ( ) and suspend_delay_seconds <= 0 and not shutdown_only then
2020-10-17 10:59:24 +00:00
logger.dbg ( " AutoSuspend: will suspend the device " )
UIManager : suspend ( )
2017-06-23 17:04:11 +00:00
else
2021-03-10 01:21:02 +00:00
if self : _enabled ( ) and not shutdown_only then
2022-05-05 19:00:22 +00:00
logger.dbg ( " AutoSuspend: scheduling next suspend check in " , suspend_delay_seconds )
UIManager : scheduleIn ( suspend_delay_seconds , self.task )
2019-09-12 12:15:08 +00:00
end
if self : _enabledShutdown ( ) then
2022-05-05 19:00:22 +00:00
logger.dbg ( " AutoSuspend: scheduling next shutdown check in " , shutdown_delay_seconds )
UIManager : scheduleIn ( shutdown_delay_seconds , self.task )
2019-09-12 12:15:08 +00:00
end
2017-06-23 17:04:11 +00:00
end
end
2019-09-12 12:15:08 +00:00
function AutoSuspend : _unschedule ( )
AutoSuspend: Unbreak scheduling & settings across instances
Plugins are loaded *once*, but torn-down/instantiated multiple times,
and sometimes in the reverse order.
As such, if we use a public function member as the scheduled task, we're
always pointing to the same function, and going from FM to RD
effectively *un*schedules it.
Instead, use an instance-specific closure, so that each instance
schedules & unschedules don't affect each other.
In the same vein, settings ought to be read at instantiation, not at
loading, otherwise, changing a setting in the FM, then switching to the
reader will not pick up the changes.
2021-04-30 17:37:42 +00:00
if self.task then
2022-04-22 16:59:48 +00:00
logger.dbg ( " AutoSuspend: unschedule suspend/shutdown timer " )
AutoSuspend: Unbreak scheduling & settings across instances
Plugins are loaded *once*, but torn-down/instantiated multiple times,
and sometimes in the reverse order.
As such, if we use a public function member as the scheduled task, we're
always pointing to the same function, and going from FM to RD
effectively *un*schedules it.
Instead, use an instance-specific closure, so that each instance
schedules & unschedules don't affect each other.
In the same vein, settings ought to be read at instantiation, not at
loading, otherwise, changing a setting in the FM, then switching to the
reader will not pick up the changes.
2021-04-30 17:37:42 +00:00
UIManager : unschedule ( self.task )
end
2017-06-23 17:04:11 +00:00
end
function AutoSuspend : _start ( )
2019-09-12 12:15:08 +00:00
if self : _enabled ( ) or self : _enabledShutdown ( ) then
2022-05-05 19:00:22 +00:00
logger.dbg ( " AutoSuspend: start suspend/shutdown timer at " , time.format_time ( self.last_action_time ) )
2019-09-12 12:15:08 +00:00
self : _schedule ( )
2017-06-23 17:04:11 +00:00
end
end
2023-04-05 18:54:47 +00:00
function AutoSuspend : _start_standby ( sleep_in )
2022-04-12 21:08:48 +00:00
if self : _enabledStandby ( ) then
2022-05-05 19:00:22 +00:00
logger.dbg ( " AutoSuspend: start standby timer at " , time.format_time ( self.last_action_time ) )
2023-04-05 18:54:47 +00:00
self : _schedule_standby ( sleep_in )
2022-04-12 21:08:48 +00:00
end
end
2021-03-10 01:21:02 +00:00
-- Variant that only re-engages the shutdown timer for onUnexpectedWakeupLimit
function AutoSuspend : _restart ( )
if self : _enabledShutdown ( ) then
2022-05-05 19:00:22 +00:00
logger.dbg ( " AutoSuspend: restart shutdown timer at " , time.format_time ( self.last_action_time ) )
2021-03-10 01:21:02 +00:00
self : _schedule ( true )
end
end
2022-12-12 02:05:41 +00:00
if Device : isKindle ( ) then
function AutoSuspend : _schedule_kindle ( )
-- NOTE: Would technically only need to be enabled if autosuspend timeout is larger than t1_timeout (10 minutes)...
if not self : _enabled ( ) then
logger.dbg ( " AutoSuspend: t1 timeout timer is disabled " )
return
end
-- NOTE: Unlike us, powerd doesn't care about charging, so we always use the delta since the last user input.
local now = UIManager : getElapsedTimeSinceBoot ( )
local kindle_t1_reset_seconds = default_kindle_t1_timeout_reset_seconds - time.to_number ( now - self.last_action_time )
if self : _enabled ( ) and kindle_t1_reset_seconds <= 0 then
logger.dbg ( " AutoSuspend: will reset the system's t1 timeout, re-scheduling check " )
PowerD : resetT1Timeout ( )
-- Re-schedule ourselves, as, unlike suspend/shutdown/standby, we don't have a specific Event to handle that for us.
UIManager : scheduleIn ( default_kindle_t1_timeout_reset_seconds , self.kindle_task )
else
if self : _enabled ( ) then
logger.dbg ( " AutoSuspend: scheduling next t1 timeout check in " , kindle_t1_reset_seconds )
UIManager : scheduleIn ( kindle_t1_reset_seconds , self.kindle_task )
end
end
end
function AutoSuspend : _unschedule_kindle ( )
if self.kindle_task then
logger.dbg ( " AutoSuspend: unschedule t1 timeout timer " )
UIManager : unschedule ( self.kindle_task )
end
end
function AutoSuspend : _start_kindle ( )
if self : _enabled ( ) then
logger.dbg ( " AutoSuspend: start t1 timeout timer at " , time.format_time ( self.last_action_time ) )
self : _schedule_kindle ( )
end
end
else
-- NOP these on other platforms to avoid a proliferation of Device:isKindle() checks everywhere
function AutoSuspend : _schedule_kindle ( ) end
function AutoSuspend : _unschedule_kindle ( ) end
function AutoSuspend : _start_kindle ( ) end
end
2017-06-23 17:04:11 +00:00
function AutoSuspend : init ( )
2021-04-02 23:48:35 +00:00
logger.dbg ( " AutoSuspend: init " )
2021-11-28 21:18:44 +00:00
self.autoshutdown_timeout_seconds = G_reader_settings : readSetting ( " autoshutdown_timeout_seconds " ,
default_autoshutdown_timeout_seconds )
self.auto_suspend_timeout_seconds = G_reader_settings : readSetting ( " auto_suspend_timeout_seconds " ,
default_auto_suspend_timeout_seconds )
2022-03-29 20:59:10 +00:00
-- Disabled, until the user opts in.
self.auto_standby_timeout_seconds = G_reader_settings : readSetting ( " auto_standby_timeout_seconds " , - 1 )
AutoSuspend: Don't send LeaveStandby events from a zombie plugin instance (#9124)
Long story short: the LeaveStandby event is sent via `tickAfterNext`, so if we tear down the plugin right after calling it (in this case, that means that the very input event that wakes the device up from suspend is one that kills ReaderUI or FileManager), what's in UIManager's task queue isn't the actual function, but the anonymous nextTick wrapper constructed by `tickAfterNext` (c.f.,
https://github.com/koreader/koreader/issues/9112#issuecomment-1133999385).
Tweak `UIManager:tickAfterNext` to return a reference to said wrapper, so that we can store it and unschedule that one, too, in `AutoSuspend:onCloseWidget`.
Fix #9112 (many thanks to [@boredhominid](https://github.com/boredhominid) for his help in finding a repro for this ;)).
Re: #8638, as the extra debugging facilities (i.e., ebb81b98451e2a8f54c46f51e861c19fdfb40499) added during testing might help pinpoint the root issue for that one, too.
Also includes a minor simplification to `UIManager:_checkTasks`, and various other task queue related codepaths (e.g., `WakeupMgr`) ;).
2022-05-25 21:36:41 +00:00
-- We only want those to exist as *instance* members
self.is_standby_scheduled = false
self.going_to_suspend = false
2017-06-23 17:04:11 +00:00
UIManager.event_hook : registerWidget ( " InputEvent " , self )
ReaderUI: Saner FM/RD lifecycle
* Ensure that going from one to the other tears down the former and
its plugins before instantiating the latter and its plugins.
UIManager: Unify Event sending & broadcasting
* Make the two behave the same way (walk the widget stack from top to
bottom), and properly handle the window stack shrinking shrinking
*and* growing.
Previously, broadcasting happened bottom-to-top and didn't really
handle the list shrinking/growing, while sending only handled the list
shrinking by a single element, and hopefully that element being the one
the event was just sent to.
These two items combined allowed us to optimize suboptimal
refresh behavior with Menu and other Menu classes when
opening/closing a document.
e.g., the "opening document" Notification is now properly regional,
and the "open last doc" option no longer flashes like a crazy person
anymore.
Plugins: Allow optimizing Menu refresh with custom menus, too.
Requires moving Menu's close_callback *after* onMenuSelect, which, eh,
probably makes sense, and is probably harmless in the grand scheme of
things.
2021-05-01 16:53:04 +00:00
-- We need an instance-specific function reference to schedule, because in some rare cases,
-- we may instantiate a new plugin instance *before* tearing down the old one.
2022-04-10 17:25:37 +00:00
-- If we only cared about accessing the right instance members,
-- we could use scheduleIn(t, self.function, self),
-- but we also care about unscheduling the task from *this* instance only:
-- unschedule(self.function) would unschedule that function for *every* instance,
-- as self.function == AutoSuspend.function ;).
AutoSuspend: Unbreak scheduling & settings across instances
Plugins are loaded *once*, but torn-down/instantiated multiple times,
and sometimes in the reverse order.
As such, if we use a public function member as the scheduled task, we're
always pointing to the same function, and going from FM to RD
effectively *un*schedules it.
Instead, use an instance-specific closure, so that each instance
schedules & unschedules don't affect each other.
In the same vein, settings ought to be read at instantiation, not at
loading, otherwise, changing a setting in the FM, then switching to the
reader will not pick up the changes.
2021-04-30 17:37:42 +00:00
self.task = function ( shutdown_only )
self : _schedule ( shutdown_only )
end
2022-12-12 02:05:41 +00:00
if Device : isKindle ( ) then
self.kindle_task = function ( )
self : _schedule_kindle ( )
end
end
2022-04-10 17:25:37 +00:00
self.standby_task = function ( )
2022-04-12 21:08:48 +00:00
self : _schedule_standby ( )
2022-04-10 17:25:37 +00:00
end
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
-- Make sure we only have an AllowStandby handler when we actually want one...
self : toggleStandbyHandler ( self : _enabledStandby ( ) )
2022-05-05 19:00:22 +00:00
self.last_action_time = UIManager : getElapsedTimeSinceBoot ( )
2017-06-23 17:04:11 +00:00
self : _start ( )
2022-12-12 02:05:41 +00:00
self : _start_kindle ( )
2022-04-12 21:08:48 +00:00
self : _start_standby ( )
2022-03-29 20:59:10 +00:00
2019-09-12 12:15:08 +00:00
-- self.ui is nil in the testsuite
if not self.ui or not self.ui . menu then return end
self.ui . menu : registerToMainMenu ( self )
2017-06-23 17:04:11 +00:00
end
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
-- NOTE: event_hook takes care of overloading this to unregister the hook, too.
2021-04-02 23:48:35 +00:00
function AutoSuspend : onCloseWidget ( )
logger.dbg ( " AutoSuspend: onCloseWidget " )
2022-04-12 21:08:48 +00:00
2021-04-02 23:48:35 +00:00
self : _unschedule ( )
AutoSuspend: Unbreak scheduling & settings across instances
Plugins are loaded *once*, but torn-down/instantiated multiple times,
and sometimes in the reverse order.
As such, if we use a public function member as the scheduled task, we're
always pointing to the same function, and going from FM to RD
effectively *un*schedules it.
Instead, use an instance-specific closure, so that each instance
schedules & unschedules don't affect each other.
In the same vein, settings ought to be read at instantiation, not at
loading, otherwise, changing a setting in the FM, then switching to the
reader will not pick up the changes.
2021-04-30 17:37:42 +00:00
self.task = nil
2022-03-29 20:59:10 +00:00
2022-12-12 02:05:41 +00:00
self : _unschedule_kindle ( )
self.kindle_task = nil
2022-03-29 20:59:10 +00:00
self : _unschedule_standby ( )
2022-04-10 17:25:37 +00:00
self.standby_task = nil
2021-04-02 23:48:35 +00:00
end
2017-06-23 17:04:11 +00:00
function AutoSuspend : onInputEvent ( )
logger.dbg ( " AutoSuspend: onInputEvent " )
2022-05-05 19:00:22 +00:00
self.last_action_time = UIManager : getElapsedTimeSinceBoot ( )
2022-03-29 20:59:10 +00:00
end
function AutoSuspend : _unschedule_standby ( )
2022-04-10 17:25:37 +00:00
if self.is_standby_scheduled and self.standby_task then
2022-04-22 16:59:48 +00:00
logger.dbg ( " AutoSuspend: unschedule standby timer " )
2022-04-10 17:25:37 +00:00
UIManager : unschedule ( self.standby_task )
-- Restore the UIManager balance, as we run preventStandby right after scheduling this task.
UIManager : allowStandby ( )
self.is_standby_scheduled = false
end
2022-03-29 20:59:10 +00:00
end
2023-04-05 18:54:47 +00:00
function AutoSuspend : _schedule_standby ( sleep_in )
sleep_in = sleep_in or self.auto_standby_timeout_seconds
2022-04-12 21:08:48 +00:00
-- Start the long list of conditions in which we do *NOT* want to go into standby ;).
2023-04-05 18:54:47 +00:00
if not Device : canStandby ( ) or self.going_to_suspend then
2022-04-12 21:08:48 +00:00
return
end
-- Don't even schedule standby if we haven't set a proper timeout yet.
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
-- NOTE: We've essentially split the _enabledStandby check in two branches,
-- simply to avoid logging noise on devices that can't even standby ;).
if self.auto_standby_timeout_seconds <= 0 then
2022-04-12 21:08:48 +00:00
logger.dbg ( " AutoSuspend: No timeout set, no standby " )
return
end
2022-04-10 17:25:37 +00:00
2022-04-12 21:08:48 +00:00
-- When we're in a state where entering suspend is undesirable, we simply postpone the check by the full delay.
2022-05-05 19:00:22 +00:00
local standby_delay_seconds
Assorted bag'o tweaks & fixes (#9569)
* UIManager: Support more specialized update modes for corner-cases:
* A2, which we'll use for the VirtualKeyboards keys (they'd... inadvertently switched to UI with the highlight refactor).
* NO_MERGE variants of ui & partial (for sunxi). Use `[ui]` in ReaderHighlight's popup, because of a Sage kernel bug that could otherwise make it translucent, sometimes completely so (*sigh*).
* UIManager: Assorted code cleanups & simplifications.
* Logger & dbg: Unify logging style, and code cleanups.
* SDL: Unbreak suspend/resume outside of the emulator (fix #9567).
* NetworkMgr: Cache the network status, and allow it to be queried. (Used by AutoSuspend to avoid repeatedly poking the system when computing the standby schedule delay).
* OneTimeMigration: Don't forget about `NETWORK_PROXY` & `STARDICT_DATA_DIR` when migrating `defaults.persistent.lua` (fix #9573)
* WakeupMgr: Workaround an apparent limitation of the RTC found on i.MX5 Kobo devices, where setting a wakealarm further than UINT16_MAX seconds in the future would apparently overflow and wraparound... (fix #8039, many thanks to @yfede for the extensive deep-dive and for actually accurately pinpointing the issue!).
* Kobo: Handle standby transitions at full CPU clock speeds, in order to limit the latency hit.
* UIManager: Properly quit on reboot & exit. This ensures our exit code is preserved, as we exit on our own terms (instead of being killed by the init system). This is important on platforms where exit codes are semantically meaningful (e.g., Kobo).
* UIManager: Speaking of reboot & exit, make sure the Screensaver shows in all circumstances (e.g., autoshutdown, re: #9542)), and that there aren't any extraneous refreshes triggered. (Additionally, fix a minor regression since #9448 about tracking this very transient state on Kobo & Cervantes).
* Kindle: ID the upcoming Scribe.
* Bump base (https://github.com/koreader/koreader-base/pull/1524)
2022-10-02 01:01:49 +00:00
-- NOTE: As this may fire repeatedly, we don't want to poke the actual Device implementation every few seconds,
-- instead, we rely on NetworkMgr's last known status. (i.e., this *should* match NetworkMgr:isWifiOn).
if NetworkMgr : getWifiState ( ) then
2022-04-12 21:08:48 +00:00
-- Don't enter standby if wifi is on, as this will break in fun and interesting ways (from Wi-Fi issues to kernel deadlocks).
--logger.dbg("AutoSuspend: WiFi is on, delaying standby")
2023-04-30 20:15:34 +00:00
standby_delay_seconds = sleep_in
2022-04-12 21:08:48 +00:00
elseif Device.powerd : isCharging ( ) and not Device : canPowerSaveWhileCharging ( ) then
Assorted bag'o tweaks & fixes (#9569)
* UIManager: Support more specialized update modes for corner-cases:
* A2, which we'll use for the VirtualKeyboards keys (they'd... inadvertently switched to UI with the highlight refactor).
* NO_MERGE variants of ui & partial (for sunxi). Use `[ui]` in ReaderHighlight's popup, because of a Sage kernel bug that could otherwise make it translucent, sometimes completely so (*sigh*).
* UIManager: Assorted code cleanups & simplifications.
* Logger & dbg: Unify logging style, and code cleanups.
* SDL: Unbreak suspend/resume outside of the emulator (fix #9567).
* NetworkMgr: Cache the network status, and allow it to be queried. (Used by AutoSuspend to avoid repeatedly poking the system when computing the standby schedule delay).
* OneTimeMigration: Don't forget about `NETWORK_PROXY` & `STARDICT_DATA_DIR` when migrating `defaults.persistent.lua` (fix #9573)
* WakeupMgr: Workaround an apparent limitation of the RTC found on i.MX5 Kobo devices, where setting a wakealarm further than UINT16_MAX seconds in the future would apparently overflow and wraparound... (fix #8039, many thanks to @yfede for the extensive deep-dive and for actually accurately pinpointing the issue!).
* Kobo: Handle standby transitions at full CPU clock speeds, in order to limit the latency hit.
* UIManager: Properly quit on reboot & exit. This ensures our exit code is preserved, as we exit on our own terms (instead of being killed by the init system). This is important on platforms where exit codes are semantically meaningful (e.g., Kobo).
* UIManager: Speaking of reboot & exit, make sure the Screensaver shows in all circumstances (e.g., autoshutdown, re: #9542)), and that there aren't any extraneous refreshes triggered. (Additionally, fix a minor regression since #9448 about tracking this very transient state on Kobo & Cervantes).
* Kindle: ID the upcoming Scribe.
* Bump base (https://github.com/koreader/koreader-base/pull/1524)
2022-10-02 01:01:49 +00:00
-- Don't enter standby when charging on devices where charging *may* prevent entering low power states.
-- (*May*, because depending on the USB controller, it might depend on what it's plugged to, and how it's setup:
-- e.g., generally, on those devices, USBNet being enabled is guaranteed to prevent PM).
2022-04-22 16:59:48 +00:00
-- NOTE: Minor simplification here, we currently don't do the hasAuxBattery dance like in _schedule,
-- because all the hasAuxBattery devices can currently enter PM states while charging ;).
2022-04-12 21:08:48 +00:00
--logger.dbg("AutoSuspend: charging, delaying standby")
2023-04-30 20:15:34 +00:00
standby_delay_seconds = sleep_in
2022-04-12 21:08:48 +00:00
else
2022-05-05 19:00:22 +00:00
local now = UIManager : getElapsedTimeSinceBoot ( )
2023-04-05 18:54:47 +00:00
standby_delay_seconds = sleep_in - time.to_number ( now - self.last_action_time )
2022-03-29 20:59:10 +00:00
2022-04-22 16:59:48 +00:00
-- If we blow past the deadline on the first call of a scheduling cycle,
2022-04-12 21:08:48 +00:00
-- make sure we don't go straight to allowStandby, as we haven't called preventStandby yet...
2022-05-05 19:00:22 +00:00
if not self.is_standby_scheduled and standby_delay_seconds <= 0 then
2022-04-22 16:59:48 +00:00
-- If this happens, it means we hit LeaveStandby or Resume *before* consuming new input events,
-- e.g., if there weren't any input events at all (woken up by an alarm),
-- or if the only input events we consumed did not trigger an InputEvent event (woken up by gyro events),
2022-05-05 19:00:22 +00:00
-- meaning self.last_action_time is further in the past than it ought to.
2022-04-22 16:59:48 +00:00
-- Delay by the full amount to avoid further bad scheduling interactions.
2023-04-05 18:54:47 +00:00
standby_delay_seconds = sleep_in
2022-04-12 21:08:48 +00:00
end
end
2022-04-10 17:25:37 +00:00
2022-05-05 19:00:22 +00:00
if standby_delay_seconds <= 0 then
2022-04-12 21:08:48 +00:00
-- We blew the deadline, tell UIManager we're ready to enter standby
self : allowStandby ( )
else
-- Reschedule standby for the full or remaining delay
-- NOTE: This is fairly chatty, given the low delays, but really helpful nonetheless... :/
2022-05-05 19:00:22 +00:00
logger.dbg ( " AutoSuspend: scheduling next standby check in " , standby_delay_seconds )
UIManager : scheduleIn ( standby_delay_seconds , self.standby_task )
2022-04-12 21:08:48 +00:00
-- Prevent standby until we actually blow the deadline
if not self.is_standby_scheduled then
self : preventStandby ( )
end
2022-04-10 17:25:37 +00:00
2022-04-12 21:08:48 +00:00
self.is_standby_scheduled = true
end
2022-03-29 20:59:10 +00:00
end
function AutoSuspend : preventStandby ( )
2022-04-22 16:59:48 +00:00
logger.dbg ( " AutoSuspend: preventStandby " )
2022-04-10 17:25:37 +00:00
-- Tell UIManager that we want to prevent standby until our allowStandby scheduled task runs.
UIManager : preventStandby ( )
2022-03-29 20:59:10 +00:00
end
2022-04-12 21:08:48 +00:00
-- NOTE: This is what our scheduled task runs to trip the UIManager state to standby
2022-03-29 20:59:10 +00:00
function AutoSuspend : allowStandby ( )
2022-04-12 21:08:48 +00:00
logger.dbg ( " AutoSuspend: allowStandby " )
2022-04-10 17:25:37 +00:00
-- Tell UIManager that we now allow standby.
UIManager : allowStandby ( )
2022-03-29 20:59:10 +00:00
2022-04-10 17:25:37 +00:00
-- We've just run our course.
self.is_standby_scheduled = false
2017-06-23 17:04:11 +00:00
end
function AutoSuspend : onSuspend ( )
logger.dbg ( " AutoSuspend: onSuspend " )
2019-09-12 12:15:08 +00:00
-- We do not want auto suspend procedure to waste battery during suspend. So let's unschedule it
-- when suspending and restart it after resume.
self : _unschedule ( )
2022-12-12 02:05:41 +00:00
self : _unschedule_kindle ( )
2022-03-29 20:59:10 +00:00
self : _unschedule_standby ( )
2019-09-12 12:15:08 +00:00
if self : _enabledShutdown ( ) and Device.wakeup_mgr then
2019-09-14 13:52:53 +00:00
Device.wakeup_mgr : addTask ( self.autoshutdown_timeout_seconds , UIManager.poweroff_action )
2019-09-12 12:15:08 +00:00
end
2022-04-22 16:59:48 +00:00
-- Make sure we won't attempt to standby during suspend
-- (because _unschedule_standby calls allowStandby,
-- so we may trip UIManager's _standbyTransition and end up in AutoSuspend:onAllowStandby)...
-- NOTE: We only want to do this *once*, because we might get a series of Suspend events before actually getting a Resume!
-- (e.g., Power (button) -> Charging (USB plug) -> SleepCover).
if self : _enabledStandby ( ) and not self.going_to_suspend then
UIManager : preventStandby ( )
end
2023-04-05 18:54:47 +00:00
-- Make sure that we don't re-schedule standby *after* us if we suspended *during* standby,
2022-04-22 16:59:48 +00:00
self.going_to_suspend = true
2017-06-23 17:04:11 +00:00
end
function AutoSuspend : onResume ( )
logger.dbg ( " AutoSuspend: onResume " )
2022-04-22 16:59:48 +00:00
-- Restore standby balance after onSuspend
if self : _enabledStandby ( ) and self.going_to_suspend then
UIManager : allowStandby ( )
end
self.going_to_suspend = false
2019-10-17 21:31:30 +00:00
if self : _enabledShutdown ( ) and Device.wakeup_mgr then
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
Device.wakeup_mgr : removeTasks ( nil , UIManager.poweroff_action )
2019-10-17 21:31:30 +00:00
end
2021-03-10 01:21:02 +00:00
-- Unschedule in case we tripped onUnexpectedWakeupLimit first...
self : _unschedule ( )
2022-05-05 19:00:22 +00:00
-- We should always follow an InputEvent, so last_action_time is already up to date :).
2017-06-23 17:04:11 +00:00
self : _start ( )
2022-12-12 02:05:41 +00:00
self : _start_kindle ( )
2022-04-12 21:08:48 +00:00
self : _unschedule_standby ( )
2023-09-01 15:55:24 +00:00
-- Use a default value for first scheduled standby after a suspend here.
-- This avoids screen glitches after a full suspend
-- and avoids broken refreshes after aborted suspend (when standby_time is 1s).
-- (And we like 1s for power saving reasons!)
self : _start_standby ( default_standby_timeout_after_resume_seconds )
2022-03-29 20:59:10 +00:00
end
2021-03-10 01:21:02 +00:00
function AutoSuspend : onUnexpectedWakeupLimit ( )
logger.dbg ( " AutoSuspend: onUnexpectedWakeupLimit " )
2022-07-02 18:20:35 +00:00
-- Should be unnecessary, because we should *always* follow onSuspend, which already does this...
-- Better safe than sorry, though ;).
self : _unschedule ( )
2021-03-10 01:21:02 +00:00
-- Only re-engage the *shutdown* schedule to avoid doing the same dance indefinitely.
self : _restart ( )
end
2022-07-02 18:20:35 +00:00
function AutoSuspend : onNotCharging ( )
logger.dbg ( " AutoSuspend: onNotCharging " )
-- Make sure both the suspend & shutdown timers are re-engaged on unplug,
-- in case we hit an UnexpectedWakeupLimit during the charge cycle...
self : _unschedule ( )
self : _start ( )
end
2022-03-29 20:59:10 +00:00
-- time_scale:
-- 2 ... display day:hour
-- 1 ... display hour:min
-- else ... display min:sec
2022-04-12 21:08:48 +00:00
function AutoSuspend : pickTimeoutValue ( touchmenu_instance , title , info , setting ,
2022-03-29 20:59:10 +00:00
default_value , range , time_scale )
2022-04-12 21:08:48 +00:00
-- NOTE: if is_day_hour then time.hour stands for days and time.min for hours
2021-11-28 21:18:44 +00:00
local InfoMessage = require ( " ui/widget/infomessage " )
local DateTimeWidget = require ( " ui/widget/datetimewidget " )
local setting_val = self [ setting ] > 0 and self [ setting ] or default_value
2022-04-12 21:08:48 +00:00
-- Standby uses a different scheduled task than suspend/shutdown
local is_standby = setting == " auto_standby_timeout_seconds "
2022-05-23 18:32:59 +00:00
local day , hour , minute , second
local day_max , hour_max , min_max , sec_max
2022-03-29 20:59:10 +00:00
if time_scale == 2 then
2022-10-10 20:21:27 +00:00
day = math.floor ( setting_val * ( 1 / ( 24 * 3600 ) ) )
hour = math.floor ( setting_val * ( 1 / 3600 ) ) % 24
day_max = math.floor ( range [ 2 ] * ( 1 / ( 24 * 3600 ) ) ) - 1
2022-05-23 18:32:59 +00:00
hour_max = 23
2022-03-29 20:59:10 +00:00
elseif time_scale == 1 then
2022-10-10 20:21:27 +00:00
hour = math.floor ( setting_val * ( 1 / 3600 ) )
minute = math.floor ( setting_val * ( 1 / 60 ) ) % 60
hour_max = math.floor ( range [ 2 ] * ( 1 / 3600 ) ) - 1
2022-05-23 18:32:59 +00:00
min_max = 59
2022-03-29 20:59:10 +00:00
else
2022-10-10 20:21:27 +00:00
minute = math.floor ( setting_val * ( 1 / 60 ) )
2022-05-23 18:32:59 +00:00
second = math.floor ( setting_val ) % 60
2022-10-10 20:21:27 +00:00
min_max = math.floor ( range [ 2 ] * ( 1 / 60 ) ) - 1
2022-05-23 18:32:59 +00:00
sec_max = 59
2022-03-29 20:59:10 +00:00
end
2021-11-28 21:18:44 +00:00
local time_spinner
time_spinner = DateTimeWidget : new {
2022-05-23 18:32:59 +00:00
day = day ,
hour = hour ,
min = minute ,
sec = second ,
day_hold_step = 5 ,
2021-11-28 21:18:44 +00:00
hour_hold_step = 5 ,
min_hold_step = 10 ,
2022-05-23 18:32:59 +00:00
sec_hold_step = 10 ,
day_max = day_max ,
hour_max = hour_max ,
min_max = min_max ,
sec_max = sec_max ,
2021-11-28 21:18:44 +00:00
ok_text = _ ( " Set timeout " ) ,
title_text = title ,
info_text = info ,
2022-05-23 18:32:59 +00:00
callback = function ( t )
self [ setting ] = ( ( ( t.day or 0 ) * 24 +
( t.hour or 0 ) ) * 60 +
( t.min or 0 ) ) * 60 +
( t.sec or 0 )
2021-11-28 21:18:44 +00:00
self [ setting ] = Math.clamp ( self [ setting ] , range [ 1 ] , range [ 2 ] )
G_reader_settings : saveSetting ( setting , self [ setting ] )
2022-04-12 21:08:48 +00:00
if is_standby then
self : _unschedule_standby ( )
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
self : toggleStandbyHandler ( self : _enabledStandby ( ) )
2022-04-12 21:08:48 +00:00
self : _start_standby ( )
else
self : _unschedule ( )
self : _start ( )
end
2021-11-28 21:18:44 +00:00
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
2023-02-12 22:22:11 +00:00
local time_string = datetime.secondsToClockDuration ( " letters " , self [ setting ] ,
time_scale == 2 or time_scale == 1 , true )
2021-11-28 21:18:44 +00:00
UIManager : show ( InfoMessage : new {
text = T ( _ ( " %1: %2 " ) , title , time_string ) ,
timeout = 3 ,
} )
end ,
2023-02-12 22:22:11 +00:00
default_value = datetime.secondsToClockDuration ( " letters " , default_value ,
time_scale == 2 or time_scale == 1 , true ) ,
2021-11-28 21:18:44 +00:00
default_callback = function ( )
2022-05-23 18:32:59 +00:00
local day , hour , min , sec -- luacheck: ignore 431
2022-03-29 20:59:10 +00:00
if time_scale == 2 then
2022-10-10 20:21:27 +00:00
day = math.floor ( default_value * ( 1 / ( 24 * 3600 ) ) )
hour = math.floor ( default_value * ( 1 / 3600 ) ) % 24
2022-03-29 20:59:10 +00:00
elseif time_scale == 1 then
2022-10-10 20:21:27 +00:00
hour = math.floor ( default_value * ( 1 / 3600 ) )
min = math.floor ( default_value * ( 1 / 60 ) ) % 60
2022-03-29 20:59:10 +00:00
else
2022-10-10 20:21:27 +00:00
min = math.floor ( default_value * ( 1 / 60 ) )
2022-05-23 18:32:59 +00:00
sec = math.floor ( default_value % 60 )
2022-03-29 20:59:10 +00:00
end
2022-05-23 18:32:59 +00:00
time_spinner : update ( nil , nil , day , hour , min , sec ) -- It is ok to pass nils here.
2021-11-28 21:18:44 +00:00
end ,
extra_text = _ ( " Disable " ) ,
2022-03-29 20:59:10 +00:00
extra_callback = function ( this )
2021-11-28 21:18:44 +00:00
self [ setting ] = - 1 -- disable with a negative time/number
2022-01-19 13:02:09 +00:00
G_reader_settings : saveSetting ( setting , - 1 )
2022-04-12 21:08:48 +00:00
if is_standby then
self : _unschedule_standby ( )
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
self : toggleStandbyHandler ( false )
2022-04-12 21:08:48 +00:00
else
self : _unschedule ( )
end
2021-11-28 21:18:44 +00:00
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
UIManager : show ( InfoMessage : new {
text = T ( _ ( " %1: disabled " ) , title ) ,
timeout = 3 ,
} )
2022-03-29 20:59:10 +00:00
this : onClose ( )
2021-11-28 21:18:44 +00:00
end ,
keep_shown_on_apply = true ,
}
UIManager : show ( time_spinner )
end
2019-09-12 12:15:08 +00:00
function AutoSuspend : addToMainMenu ( menu_items )
2022-07-02 18:20:35 +00:00
-- Device:canSuspend() check elided because it's a plugin requirement
2019-03-27 15:46:22 +00:00
menu_items.autosuspend = {
2020-10-09 05:40:23 +00:00
sorting_hint = " device " ,
2021-11-28 21:18:44 +00:00
checked_func = function ( )
return self : _enabled ( )
end ,
2021-11-23 20:17:07 +00:00
text_func = function ( )
2021-11-28 21:18:44 +00:00
if self.auto_suspend_timeout_seconds and self.auto_suspend_timeout_seconds > 0 then
2023-02-12 22:22:11 +00:00
local time_string = datetime.secondsToClockDuration ( " letters " ,
self.auto_suspend_timeout_seconds , true , true )
2021-11-28 21:18:44 +00:00
return T ( _ ( " Autosuspend timeout: %1 " ) , time_string )
2021-11-23 20:17:07 +00:00
else
return _ ( " Autosuspend timeout " )
end
end ,
keep_menu_open = true ,
callback = function ( touchmenu_instance )
2021-11-28 21:18:44 +00:00
-- 60 sec (1') is the minimum and 24*3600 sec (1day) is the maximum suspend time.
-- A suspend time of one day seems to be excessive.
2022-05-23 18:32:59 +00:00
-- But it might make sense for battery testing.
2022-04-12 21:08:48 +00:00
self : pickTimeoutValue ( touchmenu_instance ,
2021-11-28 21:18:44 +00:00
_ ( " Timeout for autosuspend " ) , _ ( " Enter time in hours and minutes. " ) ,
" auto_suspend_timeout_seconds " , default_auto_suspend_timeout_seconds ,
2022-03-29 20:59:10 +00:00
{ 60 , 24 * 3600 } , 1 )
2019-03-27 15:46:22 +00:00
end ,
}
2022-07-02 18:20:35 +00:00
if Device : canPowerOff ( ) then
2022-03-29 20:59:10 +00:00
menu_items.autoshutdown = {
sorting_hint = " device " ,
checked_func = function ( )
return self : _enabledShutdown ( )
end ,
text_func = function ( )
if self.autoshutdown_timeout_seconds and self.autoshutdown_timeout_seconds > 0 then
2023-02-12 22:22:11 +00:00
local time_string = datetime.secondsToClockDuration ( " letters " , self.autoshutdown_timeout_seconds ,
true , true )
2022-03-29 20:59:10 +00:00
return T ( _ ( " Autoshutdown timeout: %1 " ) , time_string )
else
return _ ( " Autoshutdown timeout " )
end
end ,
keep_menu_open = true ,
callback = function ( touchmenu_instance )
-- 5*60 sec (5') is the minimum and 28*24*3600 (28days) is the maximum shutdown time.
-- Minimum time has to be big enough, to avoid start-stop death scenarious.
-- Maximum more than four weeks seems a bit excessive if you want to enable authoshutdown,
-- even if the battery can last up to three months.
2022-04-12 21:08:48 +00:00
self : pickTimeoutValue ( touchmenu_instance ,
2022-05-23 18:32:59 +00:00
_ ( " Timeout for autoshutdown " ) , _ ( " Enter time in days and hours. " ) ,
2022-03-29 20:59:10 +00:00
" autoshutdown_timeout_seconds " , default_autoshutdown_timeout_seconds ,
{ 5 * 60 , 28 * 24 * 3600 } , 2 )
end ,
}
end
if Device : canStandby ( ) then
local standby_help = _ ( [ [ Standby puts the device into a power - saving state in which the screen is on and user input can be performed .
Standby can not be entered if Wi - Fi is on .
2022-10-27 20:28:20 +00:00
Upon user input , the device needs a certain amount of time to wake up . Generally , the newer the device , the less noticeable this delay will be , but it can be fairly aggravating on slower devices . ] ] )
-- Add a big fat warning on unreliable NTX boards
if Device : isKobo ( ) and not Device : hasReliableMxcWaitFor ( ) then
standby_help = standby_help .. " \n " ..
_ ( [[Your device is known to be extremely unreliable, as such, failure to enter a power-saving state *may* hang the kernel, resulting in a full device hang or a device restart.]] )
end
2022-03-29 20:59:10 +00:00
menu_items.autostandby = {
sorting_hint = " device " ,
checked_func = function ( )
return self : _enabledStandby ( )
end ,
text_func = function ( )
if self.auto_standby_timeout_seconds and self.auto_standby_timeout_seconds > 0 then
2023-02-12 22:22:11 +00:00
local time_string = datetime.secondsToClockDuration ( " letters " , self.auto_standby_timeout_seconds ,
false , true , true )
2022-03-29 20:59:10 +00:00
return T ( _ ( " Autostandby timeout: %1 " ) , time_string )
else
return _ ( " Autostandby timeout " )
end
end ,
help_text = standby_help ,
keep_menu_open = true ,
callback = function ( touchmenu_instance )
2022-04-10 17:25:37 +00:00
-- 4 sec is the minimum and 15*60 sec (15min) is the maximum standby time.
2022-03-29 20:59:10 +00:00
-- We need a minimum time, so that scheduled function have a chance to execute.
-- A standby time of 15 min seem excessive.
-- But or battery testing it might give some sense.
2022-04-12 21:08:48 +00:00
self : pickTimeoutValue ( touchmenu_instance ,
2022-03-29 20:59:10 +00:00
_ ( " Timeout for autostandby " ) , _ ( " Enter time in minutes and seconds. " ) ,
" auto_standby_timeout_seconds " , default_auto_standby_timeout_seconds ,
2023-09-01 15:55:24 +00:00
{ 1 , 15 * 60 } , 0 )
2022-03-29 20:59:10 +00:00
end ,
}
end
end
-- KOReader is merely waiting for user input right now.
-- UI signals us that standby is allowed at this very moment because nothing else goes on in the background.
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
-- NOTE: To make sure this will not even run when autostandby is disabled,
-- this is only aliased as `onAllowStandby` when necessary.
-- (Because the Event is generated regardless of us, as many things can call UIManager:allowStandby).
function AutoSuspend : AllowStandbyHandler ( )
2022-03-29 20:59:10 +00:00
logger.dbg ( " AutoSuspend: onAllowStandby " )
2022-04-10 17:25:37 +00:00
-- This piggy-backs minimally on the UI framework implemented for the PocketBook autostandby plugin,
-- see its own AllowStandby handler for more details.
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
local wake_in
-- Wake up before the next scheduled function executes (e.g. footer update, suspend ...)
local next_task_time = UIManager : getNextTaskTime ( )
if next_task_time then
2022-04-10 17:25:37 +00:00
-- Wake up slightly after the formerly scheduled event,
-- to avoid resheduling the same function after a fraction of a second again (e.g. don't draw footer twice).
2022-05-05 19:00:22 +00:00
wake_in = math.floor ( time.to_number ( next_task_time ) ) + 1
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
else
wake_in = math.huge
2022-04-10 17:25:37 +00:00
end
2022-03-29 20:59:10 +00:00
2023-09-01 15:55:24 +00:00
if wake_in >= 1 then -- Don't go into standby, if scheduled wakeup is in less than 1 second.
2022-04-12 21:08:48 +00:00
logger.dbg ( " AutoSuspend: entering standby with a wakeup alarm in " , wake_in , " s " )
2022-03-29 20:59:10 +00:00
2022-04-10 17:25:37 +00:00
-- This obviously needs a matching implementation in Device, the canonical one being Kobo.
Device : standby ( wake_in )
2022-03-29 20:59:10 +00:00
2022-05-05 19:00:22 +00:00
logger.dbg ( " AutoSuspend: left standby after " , time.format_time ( Device.last_standby_time ) , " s " )
2022-03-29 20:59:10 +00:00
2023-04-05 18:54:47 +00:00
-- NOTE: UIManager consumes scheduled tasks before input events,
2022-06-06 22:22:22 +00:00
-- solely because of where we run inside an UI frame (via UIManager:_standbyTransition):
-- we're neither a scheduled task nor an input event, we run *between* scheduled tasks and input polling.
-- That means we go straight to input polling when returning, *without* a trip through the task queue
-- (c.f., UIManager:_checkTasks in UIManager:handleInput).
2023-04-05 18:54:47 +00:00
UIManager : shiftScheduledTasksBy ( - Device.last_standby_time ) -- correct scheduled times by last_standby_time
2022-06-06 22:22:22 +00:00
-- Since we go straight to input polling, and that our time spent in standby won't have affected the already computed
-- input polling deadline (because MONOTONIC doesn't tick during standby/suspend),
-- tweak said deadline to make sure poll will return immediately, so we get a chance to run through the task queue ASAP.
-- This shouldn't prevent us from actually consuming any pending input events first,
-- because if we were woken up by user input, those events should already be in the evdev queue...
2022-06-11 17:40:10 +00:00
UIManager : consumeInputEarlyAfterPM ( true )
2023-04-05 18:54:47 +00:00
-- When we exit this method, we are sure that the input polling deadline is zero (consumeInputEarly).
-- UIManager will check newly scheduled tasks before going to input polling again (with a new deadline).
self : _start_standby ( ) -- Schedule the next standby check in the future.
2022-06-27 19:15:45 +00:00
else
2023-04-05 18:54:47 +00:00
-- When we exit this method, we are sure that the input polling deadline is approximately `wake_in`.
-- So it is safe to schedule another task a bit later.
self : _start_standby ( wake_in + 0.1 ) -- Schedule the next standby check 0.1 seconds after the next calculated wakeup time.
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
end
end
function AutoSuspend : toggleStandbyHandler ( toggle )
if toggle then
self.onAllowStandby = self.AllowStandbyHandler
else
self.onAllowStandby = nil
2022-03-29 20:59:10 +00:00
end
2019-03-27 15:46:22 +00:00
end
2023-04-30 20:15:34 +00:00
function AutoSuspend : onNetworkConnected ( )
logger.dbg ( " AutoSuspend: onNetworkConnected " )
self : _unschedule_standby ( )
-- Schedule the next check at the end of our timescale, the subsequent checks are ... well never ;)
self : _start_standby ( math.huge )
end
function AutoSuspend : onNetworkConnecting ( )
logger.dbg ( " AutoSuspend: onNetworkConnecting " )
self : _unschedule_standby ( )
NetworkMgr: Handle non-blocking turnOnWifi implementations better (#10863)
* Device: Add a `hasSeamlessWifiToggle` devcap to complement `hasWifiToggle`, to denote platforms where we can toggle WiFi without losing focus, as this has obvious UX impacts, and less obvious technical impacts on some of the NetworkMgr innards...
* Android: Mark as `!hasSeamlessWifiToggle`, as it requires losing focus to the system settings. Moreover, `turnOnWifi` returns *immediately* and we *still* run in the background during that time, for extra spiciness...
* NetworkMgr: Ensure only *one* call to `turnOnWifi` will actually go on when stuff gets re-scheduled by the `beforeWifiAction` framework.
* NetworkMgr: Ensure the `beforeWifiAction` framework will not re-schedule the same thing *ad vitam aeternam* if a previous connection attempt is still ongoing. (i.e., previously, on Android, if you backed out of the system settings, you entered the Benny Hill dimension, as NetworkMgr would keep throwing you back into the system settings ;p). This has a few implications on callbacks requested by subsequent connection attempts, though. Generally, we'll try to honor *explicitly interactive* callbacks, but `beforeWifiAction` stuff will be dropped (only the original cb is preserved). That's what prevents the aforementioned infinite loop, as the `beforeWifiAction` framework was based on the assumption that `turnOnWifi` somewhat guaranteed `isConnected` to be true on return, something which is only actually true on `hasWifiManager` platforms.
* NetworkMgr: In `prompt` mode, the above implies that the prompt will not even be shown for concurrent attempts, as it's otherwise extremely confusing (KOSync on Android being a prime example, as it has a pair of Suspend/Resume handlers, so the initial attempt trips those two because of the focus switch >_<").
* NetworkMgr: Don't attempt to kill wifi when aborting a connection attempt on `!hasSeamlessWifiToggle` (because, again, it'll break UX, and also because it might run at very awkward times (e.g., I managed to go back to KOReader *between* a FM/Reader switch at one point, which promptly caused `UIManager` to exit because there was nothing to show ;p).
* NetworkMgr: Don't drop the connectivity callback when `beforeWifiAction` is set to prompt and the target happens to use a connectivity check in its `turnOnWifi` implementation (e.g., on Kindle).
* Android: Add an `"ignore"` `beforeWifiAction` mode, that'll do nothing but schedule the connectivity check with its callback (with the intent being the system will eventually enable wifi on its own Soon(TM)). If you're already online, the callback will run immediately, obviously. If you followed the early discussions on this PR, this closely matches what happens on `!hasWifiToggle` platforms (as flagging Android that way was one of the possible approaches here).
* NetworkMgr: Bail out early in `goOnlineToRun` if `beforeWifiAction` isn't `"turn_on"`. Prompt cannot work there, and while ignore technically could, it would serve very little purpose given its intended use case.
* KOSync: Neuter the Resume/Suspend handlers early on `CloseDocument`, as this is how focus switches are handled on Android, and if `beforeWifiAction` is `turn_on` and you were offline at the time, we'd trip them because of the swap to system settings to enable wifi.
* KOSync: Allow `auto_sync` to be enabled regardless of the `beforeWifiAction` mode on `!hasSeamlessWifiToggle` platforms. Prompt is still a terrible idea, but given that `goOnlineToRun` now aborts early if the mode is not supported, it's less of a problem.
2023-09-21 16:21:09 +00:00
-- Schedule the next check in 60s, which should account for the full duration of NetworkMgr's connectivity check (it times out at 45s),
-- with some extra breathing room.
-- If the connection attempt fails (in which case we get neither a NetworkConnected nor a NetworkDisconnected event),
-- the subsequent checks are in the usual `self.auto_standby_timeout_seconds`.
2023-04-30 20:15:34 +00:00
self : _start_standby ( time.s ( 60 ) )
end
function AutoSuspend : onNetworkDisconnected ( )
2023-07-12 00:42:16 +00:00
logger.dbg ( " AutoSuspend: onNetworkDisconnected " )
2023-04-30 20:15:34 +00:00
self : _unschedule_standby ( )
-- Schedule the next check as usual.
self : _start_standby ( )
end
2023-09-01 15:55:24 +00:00
--[[-- not necessary right now
2023-04-30 20:15:34 +00:00
function AutoSuspend : onNetworkDisconnecting ( )
logger.dbg ( " AutoSuspend: onNetworkDisconnecting " )
end
--]]
2019-09-12 12:15:08 +00:00
return AutoSuspend