2017-06-23 17:04:11 +00:00
|
|
|
local Device = require("device")
|
|
|
|
|
2020-10-17 10:59:24 +00:00
|
|
|
if not Device:isCervantes() and
|
2021-04-29 17:26:52 +00:00
|
|
|
not Device:isKindle() and
|
2020-10-17 10:59:24 +00:00
|
|
|
not Device:isKobo() and
|
|
|
|
not Device:isRemarkable() and
|
|
|
|
not Device:isSDL() and
|
|
|
|
not Device:isSonyPRSTUX() and
|
|
|
|
not Device:isPocketBook() then
|
2018-10-31 22:48:36 +00:00
|
|
|
return { disabled = true, }
|
|
|
|
end
|
2017-06-23 17:04:11 +00:00
|
|
|
|
2017-06-27 04:39:23 +00:00
|
|
|
local PluginShare = require("pluginshare")
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
local TimeVal = require("ui/timeval")
|
2017-06-23 17:04:11 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
|
|
|
local logger = require("logger")
|
|
|
|
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
|
|
|
|
2019-09-12 12:15:08 +00:00
|
|
|
local default_autoshutdown_timeout_seconds = 3*24*60*60
|
2021-03-06 21:44:18 +00:00
|
|
|
local default_auto_suspend_timeout_seconds = 60*60
|
2019-09-12 12:15:08 +00:00
|
|
|
|
|
|
|
local AutoSuspend = WidgetContainer:new{
|
|
|
|
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,
|
|
|
|
last_action_tv = TimeVal.zero,
|
2020-10-17 10:59:24 +00:00
|
|
|
standby_prevented = 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
|
|
|
task = nil,
|
2017-06-23 17:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function AutoSuspend:_enabled()
|
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)
|
|
|
|
if not self:_enabled() and (Device:canPowerOff() and not self:_enabledShutdown()) then
|
2017-06-23 17:04:11 +00:00
|
|
|
logger.dbg("AutoSuspend:_schedule is disabled")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-09-12 12:15:08 +00:00
|
|
|
local delay_suspend, delay_shutdown
|
2017-06-27 04:39:23 +00:00
|
|
|
|
2020-10-17 10:59:24 +00:00
|
|
|
if PluginShare.pause_auto_suspend or Device.standby_prevented or Device.powerd:isCharging() then
|
2021-03-06 21:44:18 +00:00
|
|
|
delay_suspend = self.auto_suspend_timeout_seconds
|
2019-09-14 13:52:53 +00:00
|
|
|
delay_shutdown = self.autoshutdown_timeout_seconds
|
2017-06-27 04:39:23 +00:00
|
|
|
else
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
local now_tv = UIManager:getTime()
|
2021-04-14 23:00:28 +00:00
|
|
|
delay_suspend = self.last_action_tv + TimeVal:new{ sec = self.auto_suspend_timeout_seconds, usec = 0 } - now_tv
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
delay_suspend = delay_suspend:tonumber()
|
2021-04-14 23:00:28 +00:00
|
|
|
delay_shutdown = self.last_action_tv + TimeVal:new{ sec = self.autoshutdown_timeout_seconds, usec = 0 } - now_tv
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
delay_shutdown = delay_shutdown:tonumber()
|
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.
|
|
|
|
if delay_shutdown <= 0 then
|
2019-09-12 12:15:08 +00:00
|
|
|
logger.dbg("AutoSuspend: initiating shutdown")
|
|
|
|
UIManager:poweroff_action()
|
2021-03-10 01:21:02 +00:00
|
|
|
elseif delay_suspend <= 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
|
|
|
|
logger.dbg("AutoSuspend: scheduling next suspend check in", delay_suspend)
|
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:scheduleIn(delay_suspend, self.task)
|
2019-09-12 12:15:08 +00:00
|
|
|
end
|
|
|
|
if self:_enabledShutdown() then
|
2021-03-10 01:21:02 +00:00
|
|
|
logger.dbg("AutoSuspend: scheduling next shutdown check in", delay_shutdown)
|
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:scheduleIn(delay_shutdown, 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
|
|
|
|
logger.dbg("AutoSuspend: unschedule")
|
|
|
|
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
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
local now_tv = UIManager:getTime()
|
|
|
|
logger.dbg("AutoSuspend: start at", now_tv:tonumber())
|
|
|
|
self.last_action_tv = now_tv
|
2019-09-12 12:15:08 +00:00
|
|
|
self:_schedule()
|
2017-06-23 17:04:11 +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
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
local now_tv = UIManager:getTime()
|
|
|
|
logger.dbg("AutoSuspend: restart at", now_tv:tonumber())
|
|
|
|
self.last_action_tv = now_tv
|
2021-03-10 01:21:02 +00:00
|
|
|
self:_schedule(true)
|
|
|
|
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")
|
2020-10-17 10:59:24 +00:00
|
|
|
if Device:isPocketBook() and not Device:canSuspend() then return end
|
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.autoshutdown_timeout_seconds = G_reader_settings:readSetting("autoshutdown_timeout_seconds") or default_autoshutdown_timeout_seconds
|
|
|
|
self.auto_suspend_timeout_seconds = G_reader_settings:readSetting("auto_suspend_timeout_seconds") or default_auto_suspend_timeout_seconds
|
|
|
|
|
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.
|
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
|
2017-06-23 17:04:11 +00:00
|
|
|
self:_start()
|
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
|
|
|
|
|
2021-04-02 23:48:35 +00:00
|
|
|
-- For event_hook automagic deregistration purposes
|
|
|
|
function AutoSuspend:onCloseWidget()
|
|
|
|
logger.dbg("AutoSuspend: onCloseWidget")
|
|
|
|
if Device:isPocketBook() and not Device:canSuspend() then return end
|
|
|
|
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
|
2021-04-02 23:48:35 +00:00
|
|
|
end
|
|
|
|
|
2017-06-23 17:04:11 +00:00
|
|
|
function AutoSuspend:onInputEvent()
|
|
|
|
logger.dbg("AutoSuspend: onInputEvent")
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
self.last_action_tv = UIManager:getTime()
|
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()
|
|
|
|
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
|
2017-06-23 17:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function AutoSuspend:onResume()
|
|
|
|
logger.dbg("AutoSuspend: onResume")
|
2019-10-17 21:31:30 +00:00
|
|
|
if self:_enabledShutdown() and Device.wakeup_mgr then
|
|
|
|
Device.wakeup_mgr:removeTask(nil, nil, UIManager.poweroff_action)
|
|
|
|
end
|
2021-03-10 01:21:02 +00:00
|
|
|
-- Unschedule in case we tripped onUnexpectedWakeupLimit first...
|
|
|
|
self:_unschedule()
|
2017-06-23 17:04:11 +00:00
|
|
|
self:_start()
|
|
|
|
end
|
|
|
|
|
2021-03-10 01:21:02 +00:00
|
|
|
function AutoSuspend:onUnexpectedWakeupLimit()
|
|
|
|
logger.dbg("AutoSuspend: onUnexpectedWakeupLimit")
|
|
|
|
-- Only re-engage the *shutdown* schedule to avoid doing the same dance indefinitely.
|
|
|
|
self:_restart()
|
|
|
|
end
|
|
|
|
|
2020-10-17 10:59:24 +00:00
|
|
|
function AutoSuspend:onAllowStandby()
|
|
|
|
self.standby_prevented = false
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoSuspend:onPreventStandby()
|
|
|
|
self.standby_prevented = true
|
|
|
|
end
|
|
|
|
|
2019-09-12 12:15:08 +00:00
|
|
|
function AutoSuspend:addToMainMenu(menu_items)
|
2019-03-27 15:46:22 +00:00
|
|
|
menu_items.autosuspend = {
|
2020-10-09 05:40:23 +00:00
|
|
|
sorting_hint = "device",
|
2019-03-27 15:46:22 +00:00
|
|
|
text = _("Autosuspend timeout"),
|
|
|
|
callback = function()
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local SpinWidget = require("ui/widget/spinwidget")
|
|
|
|
local autosuspend_spin = SpinWidget:new {
|
2021-03-06 21:44:18 +00:00
|
|
|
value = self.auto_suspend_timeout_seconds / 60,
|
2019-03-27 15:46:22 +00:00
|
|
|
value_min = 5,
|
|
|
|
value_max = 240,
|
|
|
|
value_hold_step = 15,
|
|
|
|
ok_text = _("Set timeout"),
|
|
|
|
title_text = _("Timeout in minutes"),
|
|
|
|
callback = function(autosuspend_spin)
|
2021-03-06 21:44:18 +00:00
|
|
|
self.auto_suspend_timeout_seconds = autosuspend_spin.value * 60
|
|
|
|
G_reader_settings:saveSetting("auto_suspend_timeout_seconds", self.auto_suspend_timeout_seconds)
|
2019-03-27 15:46:22 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
2019-09-12 12:15:08 +00:00
|
|
|
text = T(_("The system will automatically suspend after %1 minutes of inactivity."),
|
2021-03-06 21:44:18 +00:00
|
|
|
string.format("%.2f", self.auto_suspend_timeout_seconds / 60)),
|
2019-09-12 12:15:08 +00:00
|
|
|
timeout = 3,
|
2019-03-27 15:46:22 +00:00
|
|
|
})
|
2019-09-12 12:15:08 +00:00
|
|
|
self:_unschedule()
|
|
|
|
self:_start()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
UIManager:show(autosuspend_spin)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
if not (Device:canPowerOff() or Device:isEmulator()) then return end
|
|
|
|
menu_items.autoshutdown = {
|
2020-10-17 10:59:24 +00:00
|
|
|
sorting_hint = "device",
|
2019-09-12 12:15:08 +00:00
|
|
|
text = _("Autoshutdown timeout"),
|
|
|
|
callback = function()
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local SpinWidget = require("ui/widget/spinwidget")
|
|
|
|
local autosuspend_spin = SpinWidget:new {
|
2021-03-06 21:44:18 +00:00
|
|
|
value = self.autoshutdown_timeout_seconds / 60 / 60,
|
2019-09-12 12:15:08 +00:00
|
|
|
-- About a minute, good for testing and battery life fanatics.
|
|
|
|
-- Just high enough to avoid an instant shutdown death scenario.
|
|
|
|
value_min = 0.017,
|
|
|
|
-- More than three weeks seems a bit excessive if you want to enable authoshutdown,
|
|
|
|
-- even if the battery can last up to three months.
|
|
|
|
value_max = 28*24,
|
|
|
|
value_hold_step = 24,
|
|
|
|
precision = "%.2f",
|
|
|
|
ok_text = _("Set timeout"),
|
|
|
|
title_text = _("Timeout in hours"),
|
|
|
|
callback = function(autosuspend_spin)
|
2021-03-06 21:44:18 +00:00
|
|
|
self.autoshutdown_timeout_seconds = math.floor(autosuspend_spin.value * 60 * 60)
|
|
|
|
G_reader_settings:saveSetting("autoshutdown_timeout_seconds", self.autoshutdown_timeout_seconds)
|
2019-09-12 12:15:08 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("The system will automatically shut down after %1 hours of inactivity."),
|
2021-03-06 21:44:18 +00:00
|
|
|
string.format("%.2f", self.autoshutdown_timeout_seconds / 60 / 60)),
|
2019-09-12 12:15:08 +00:00
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
self:_unschedule()
|
|
|
|
self:_start()
|
2019-03-27 15:46:22 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
UIManager:show(autosuspend_spin)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-12 12:15:08 +00:00
|
|
|
return AutoSuspend
|