2019-09-01 19:30:19 +00:00
|
|
|
local Event = require("ui/event")
|
|
|
|
local PluginShare = require("pluginshare")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
2022-11-17 04:53:35 +00:00
|
|
|
local datetime = require("datetime")
|
2019-09-01 19:30:19 +00:00
|
|
|
local logger = require("logger")
|
2022-05-05 19:00:22 +00:00
|
|
|
local time = require("ui/time")
|
2019-09-01 19:30:19 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
local T = require("ffi/util").template
|
|
|
|
|
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 AutoTurn = WidgetContainer:extend{
|
2020-12-04 15:04:54 +00:00
|
|
|
name = "autoturn",
|
2019-09-01 19:30:19 +00:00
|
|
|
is_doc_only = true,
|
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
|
|
|
autoturn_sec = 0,
|
|
|
|
autoturn_distance = 1,
|
|
|
|
enabled = false,
|
2022-05-05 19:00:22 +00:00
|
|
|
last_action_time = 0,
|
2021-04-30 17:59:27 +00:00
|
|
|
task = nil,
|
2019-09-01 19:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function AutoTurn:_enabled()
|
|
|
|
return self.enabled and self.autoturn_sec > 0
|
|
|
|
end
|
|
|
|
|
2021-04-30 17:59:27 +00:00
|
|
|
function AutoTurn:_schedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
if not self:_enabled() then
|
|
|
|
logger.dbg("AutoTurn:_schedule is disabled")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-04-05 18:54:47 +00:00
|
|
|
local delay = self.last_action_time + time.s(self.autoturn_sec) - UIManager:getElapsedTimeSinceBoot()
|
2019-09-01 19:30:19 +00:00
|
|
|
|
|
|
|
if delay <= 0 then
|
2023-02-01 23:29:23 +00:00
|
|
|
local top_wg = UIManager:getTopmostVisibleWidget() or {}
|
|
|
|
if top_wg.name == "ReaderUI" then
|
2019-09-01 19:30:19 +00:00
|
|
|
logger.dbg("AutoTurn: go to next page")
|
|
|
|
self.ui:handleEvent(Event:new("GotoViewRel", self.autoturn_distance))
|
2023-04-05 18:54:47 +00:00
|
|
|
self.last_action_time = UIManager:getElapsedTimeSinceBoot()
|
2019-09-01 19:30:19 +00:00
|
|
|
end
|
2020-10-18 18:38:17 +00:00
|
|
|
logger.dbg("AutoTurn: schedule in", self.autoturn_sec)
|
2021-04-30 17:59:27 +00:00
|
|
|
UIManager:scheduleIn(self.autoturn_sec, self.task)
|
2022-03-29 20:59:10 +00:00
|
|
|
self.scheduled = true
|
2019-09-01 19:30:19 +00:00
|
|
|
else
|
2022-05-05 19:00:22 +00:00
|
|
|
local delay_s = time.to_number(delay)
|
|
|
|
logger.dbg("AutoTurn: schedule in", delay_s, "s")
|
|
|
|
UIManager:scheduleIn(delay_s, self.task)
|
2022-03-29 20:59:10 +00:00
|
|
|
self.scheduled = true
|
2019-09-01 19:30:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-30 17:59:27 +00:00
|
|
|
function AutoTurn:_unschedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
PluginShare.pause_auto_suspend = false
|
2022-03-29 20:59:10 +00:00
|
|
|
if self.scheduled then
|
2021-04-30 17:59:27 +00:00
|
|
|
logger.dbg("AutoTurn: unschedule")
|
|
|
|
UIManager:unschedule(self.task)
|
2022-03-29 20:59:10 +00:00
|
|
|
self.scheduled = false
|
2021-04-30 17:59:27 +00:00
|
|
|
end
|
2019-09-01 19:30:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function AutoTurn:_start()
|
|
|
|
if self:_enabled() then
|
2023-04-05 18:54:47 +00:00
|
|
|
local time_since_boot = UIManager:getElapsedTimeSinceBoot()
|
|
|
|
logger.dbg("AutoTurn: start at", time.format_time(time_since_boot))
|
2019-09-01 19:30:19 +00:00
|
|
|
PluginShare.pause_auto_suspend = true
|
2023-04-05 18:54:47 +00:00
|
|
|
self.last_action_time = time_since_boot
|
2021-04-30 17:59:27 +00:00
|
|
|
self:_schedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
|
|
|
|
local text
|
|
|
|
if self.autoturn_distance == 1 then
|
2023-02-12 22:22:11 +00:00
|
|
|
local time_string = datetime.secondsToClockDuration("letters", self.autoturn_sec, false, true, true)
|
2022-05-31 22:04:46 +00:00
|
|
|
text = T(_("Autoturn is now active and will automatically turn the page every %1."), time_string)
|
2019-09-01 19:30:19 +00:00
|
|
|
else
|
|
|
|
text = T(_("Autoturn is now active and will automatically scroll %1 % of the page every %2 seconds."),
|
|
|
|
self.autoturn_distance * 100,
|
|
|
|
self.autoturn_sec)
|
|
|
|
end
|
|
|
|
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = text,
|
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoTurn:init()
|
|
|
|
UIManager.event_hook:registerWidget("InputEvent", self)
|
2022-03-29 20:59:10 +00:00
|
|
|
self.autoturn_sec = G_reader_settings:readSetting("autoturn_timeout_seconds", 0)
|
|
|
|
self.autoturn_distance = G_reader_settings:readSetting("autoturn_distance", 1)
|
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.enabled = G_reader_settings:isTrue("autoturn_enabled")
|
2019-09-01 19:30:19 +00:00
|
|
|
self.ui.menu:registerToMainMenu(self)
|
2021-04-30 17:59:27 +00:00
|
|
|
self.task = function()
|
|
|
|
self:_schedule()
|
|
|
|
end
|
2019-09-01 19:30:19 +00:00
|
|
|
self:_start()
|
|
|
|
end
|
|
|
|
|
2021-04-02 23:48:35 +00:00
|
|
|
function AutoTurn:onCloseWidget()
|
|
|
|
logger.dbg("AutoTurn: onCloseWidget")
|
2021-04-30 17:59:27 +00:00
|
|
|
self:_unschedule()
|
|
|
|
self.task = nil
|
2021-04-02 23:48:35 +00:00
|
|
|
end
|
|
|
|
|
2019-09-01 19:30:19 +00:00
|
|
|
function AutoTurn:onCloseDocument()
|
|
|
|
logger.dbg("AutoTurn: onCloseDocument")
|
2021-04-30 17:59:27 +00:00
|
|
|
self:_unschedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function AutoTurn:onInputEvent()
|
|
|
|
logger.dbg("AutoTurn: onInputEvent")
|
2023-04-05 18:54:47 +00:00
|
|
|
self.last_action_time = UIManager:getElapsedTimeSinceBoot()
|
2022-03-29 20:59:10 +00:00
|
|
|
end
|
|
|
|
|
2019-09-01 19:30:19 +00:00
|
|
|
-- We do not want autoturn to turn pages during the suspend process.
|
|
|
|
-- Unschedule it and restart after resume.
|
|
|
|
function AutoTurn:onSuspend()
|
|
|
|
logger.dbg("AutoTurn: onSuspend")
|
2021-04-30 17:59:27 +00:00
|
|
|
self:_unschedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
end
|
|
|
|
|
2022-03-29 20:59:10 +00:00
|
|
|
function AutoTurn:_onResume()
|
2019-09-01 19:30:19 +00:00
|
|
|
logger.dbg("AutoTurn: onResume")
|
|
|
|
self:_start()
|
|
|
|
end
|
|
|
|
|
|
|
|
function AutoTurn:addToMainMenu(menu_items)
|
|
|
|
menu_items.autoturn = {
|
2020-10-09 05:40:23 +00:00
|
|
|
sorting_hint = "navi",
|
2022-05-31 22:04:46 +00:00
|
|
|
text_func = function()
|
2023-02-12 22:22:11 +00:00
|
|
|
local time_string = datetime.secondsToClockDuration("letters", self.autoturn_sec, false, true, true)
|
2022-05-31 22:04:46 +00:00
|
|
|
return self:_enabled() and T(_("Autoturn: %1"), time_string) or _("Autoturn")
|
|
|
|
end,
|
2019-09-01 19:30:19 +00:00
|
|
|
checked_func = function() return self:_enabled() end,
|
|
|
|
callback = function(menu)
|
2022-05-31 22:04:46 +00:00
|
|
|
local DateTimeWidget = require("ui/widget/datetimewidget")
|
|
|
|
local autoturn_seconds = G_reader_settings:readSetting("autoturn_timeout_seconds", 30)
|
2022-10-10 20:21:27 +00:00
|
|
|
local autoturn_minutes = math.floor(autoturn_seconds * (1/60))
|
2022-05-31 22:04:46 +00:00
|
|
|
autoturn_seconds = autoturn_seconds % 60
|
|
|
|
local autoturn_spin = DateTimeWidget:new {
|
|
|
|
title_text = _("Autoturn time"),
|
|
|
|
info_text = _("Enter time in minutes and seconds."),
|
|
|
|
min = autoturn_minutes,
|
|
|
|
min_max = 60 * 24, -- maximum one day
|
|
|
|
min_default = 0,
|
|
|
|
sec = autoturn_seconds,
|
|
|
|
sec_default = 30,
|
|
|
|
keep_shown_on_apply = true,
|
2019-09-01 19:30:19 +00:00
|
|
|
ok_text = _("Set timeout"),
|
|
|
|
cancel_text = _("Disable"),
|
|
|
|
cancel_callback = function()
|
|
|
|
self.enabled = false
|
2021-03-06 21:44:18 +00:00
|
|
|
G_reader_settings:makeFalse("autoturn_enabled")
|
2021-04-30 17:59:27 +00:00
|
|
|
self:_unschedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
menu:updateItems()
|
2022-03-29 20:59:10 +00:00
|
|
|
self.onResume = nil
|
2019-09-01 19:30:19 +00:00
|
|
|
end,
|
2022-03-29 20:59:10 +00:00
|
|
|
ok_always_enabled = true,
|
2022-05-31 22:04:46 +00:00
|
|
|
callback = function(t)
|
|
|
|
self.autoturn_sec = t.min * 60 + t.sec
|
|
|
|
G_reader_settings:saveSetting("autoturn_timeout_seconds", self.autoturn_sec)
|
2019-09-01 19:30:19 +00:00
|
|
|
self.enabled = true
|
2021-03-06 21:44:18 +00:00
|
|
|
G_reader_settings:makeTrue("autoturn_enabled")
|
2021-04-30 17:59:27 +00:00
|
|
|
self:_unschedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
self:_start()
|
|
|
|
menu:updateItems()
|
2022-03-29 20:59:10 +00:00
|
|
|
self.onResume = self._onResume
|
2019-09-01 19:30:19 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
UIManager:show(autoturn_spin)
|
|
|
|
end,
|
|
|
|
hold_callback = function(menu)
|
|
|
|
local SpinWidget = require("ui/widget/spinwidget")
|
|
|
|
local curr_items = G_reader_settings:readSetting("autoturn_distance") or 1
|
|
|
|
local autoturn_spin = SpinWidget:new {
|
|
|
|
value = curr_items,
|
|
|
|
value_min = -20,
|
|
|
|
value_max = 20,
|
|
|
|
precision = "%.2f",
|
|
|
|
value_step = .1,
|
|
|
|
value_hold_step = .5,
|
|
|
|
ok_text = _("Set distance"),
|
|
|
|
title_text = _("Scrolling distance"),
|
|
|
|
callback = function(autoturn_spin)
|
|
|
|
self.autoturn_distance = autoturn_spin.value
|
|
|
|
G_reader_settings:saveSetting("autoturn_distance", autoturn_spin.value)
|
|
|
|
if self.enabled then
|
2021-04-30 17:59:27 +00:00
|
|
|
self:_unschedule()
|
2019-09-01 19:30:19 +00:00
|
|
|
self:_start()
|
|
|
|
end
|
|
|
|
menu:updateItems()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
UIManager:show(autoturn_spin)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return AutoTurn
|