* 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..
--- Accelerometer on the Forma, c.f., drivers/hwmon/mma8x5x.c
functionInput:handleMiscEvNTX(ev)
localrotation_mode,screen_mode
ifev.code== MSC_RAW then
ifev.code==C.MSC_RAW then
ifev.value==MSC_RAW_GSENSOR_PORTRAIT_UPthen
-- i.e., UR
rotation_mode=framebuffer.ORIENTATION_PORTRAIT
@ -774,90 +875,198 @@ end
--- Main event handling.
functionInput:waitEvent(timeout_us)
-- `now` corresponds to UIManager:getTime() (a TimeVal), and it's just been updated by UIManager.
-- `deadline` (a TimeVal) is the absolute deadline imposed by UIManager:handleInput() (a.k.a., our main event loop ^^):
-- it's either nil (meaning block forever waiting for input), or the earliest UIManager deadline (in most cases, that's the next scheduled task,
-- in much less common cases, that's the earliest of UIManager.INPUT_TIMEOUT (currently, only KOSync ever sets it) or UIManager.ZMQ_TIMEOUT if there are pending ZMQs).
functionInput:waitEvent(now,deadline)
-- On the first iteration of the loop, we don't need to update now, we're following closely (a couple ms at most) behind UIManager.
localok,ev
-- wrapper for input.waitForEvents that will retry for some cases
-- Wrapper around the platform-specific input.waitForEvent (which itself is generally poll-like).
-- Speaking of input.waitForEvent, it can return:
-- * true, ev: When an input event was read. ev is a table mapped after the input_event <linux/input.h> struct.
-- * false, errno, timerfd: When no input event was read, possibly for benign reasons.
-- One such common case is after a polling timeout, in which case errno is C.ETIME.
-- If the timerfd backend is in use, and the early return was caused by a timerfd expiring,
-- it returns false, C.ETIME, timerfd; where timerfd is a C pointer (i.e., light userdata)
-- to the timerfd node that expired (so as to be able to free it later, c.f., input/timerfd-callbacks.h).
-- Otherwise, errno is the actual error code from the backend (e.g., select's errno for the C backend).
-- * nil: When something terrible happened (e.g., fatal poll/read failure). We abort in such cases.
whiletruedo
if#self.timer_callbacks>0then
localwait_deadline=TimeVal:now()+TimeVal:new{
usec=timeout_us
}
-- we don't block if there is any timer, set wait to 10us
-- If we have timers set, we need to honor them once we're done draining the input events.
while#self.timer_callbacks>0do
ok,ev=pcall(input.waitForEvent,100)
-- Choose the earliest deadline between the next timer deadline, and our full timeout deadline.
localdeadline_is_timer=false
localpoll_deadline
-- If the timer's deadline is handled via timerfd, that's easy
ifself.timer_callbacks[1].timerfdthen
-- We use the ultimate deadline, as the kernel will just signal us when the timer expires during polling.
poll_deadline=deadline
else
ifnotdeadlinethen
-- If we don't actually have a full timeout deadline, just honor the timer's.
poll_deadline=self.timer_callbacks[1].deadline
deadline_is_timer=true
else
ifself.timer_callbacks[1].deadline<deadlinethen
poll_deadline=self.timer_callbacks[1].deadline
deadline_is_timer=true
else
poll_deadline=deadline
end
end
end
localpoll_timeout
-- With the timerfd backend, poll_deadline is set to deadline, which might be nil, in which case,
-- we can happily block forever, like in the no timer_callbacks branch below ;).
ifpoll_deadlinethen
-- If we haven't hit that deadline yet, poll until it expires, otherwise,
-- have select return immediately so that we trip a timeout.
now=noworTimeVal:now()
ifpoll_deadline>nowthen
-- Deadline hasn't been blown yet, honor it.
poll_timeout=poll_deadline-now
else
-- We've already blown the deadline: make select return immediately (most likely straight to timeout)