feat(wayland): add Window::lastInputTime, the compositor's time of a pointer or touch event

The pointer and touch handlers received wl_pointer's and wl_touch's
`time` and dropped it, so a consumer could date an event only by its own
clock at dispatch. Events queued while the consumer's loop is busy are
dispatched together when it comes back, so by that clock a swipe's
backlog is a burst, and a press and its release that both waited out
a stall happen at one time.

Window::lastInputTime is the compositor's time of the pointer, touch or
scroll event a listener is being invoked for — milliseconds from an
undefined base, wrapping at 2^32 — set before the event fires: the
button events, a motion's onMouseMove, onMouseScroll, onTouchDown,
onTouchMove and onTouchUp, and the pointer events the first finger
stands in for. An event the compositor sends without a time keeps the
last one's: an enter's onMouseEnter and the move that follows it,
onMouseLeave, and a touch_cancel's release and onTouchCancel.

A listener that dispatches Wayland events itself (it makes a Window, or
calls Clipboard::GetText) runs the handlers of what is queued behind
its event, and they set the time too: for that listener from there on,
and for the listeners of its event that come after it. The click, move
and release the first finger stands in for fire after the touch
event's listeners, so the touch handlers set the time again before
them.

The member is the Wayland backend's alone: the Win32 and DOM handlers
set no such time, and design rule 1 keeps the library from "inventing a
shared signature that is a no-op on one side". Those builds have no
such member, and the consumer notes say so.

Tests: InputTime drives the wl_pointer and wl_touch handlers directly,
as MouseScroll and PointerEnterLeave do, and reads lastInputTime in
every listener: 0 before the first event; each event's own time for a
motion, the four button events, a scroll and a touch's down, motion and
up; the touch's time for the first finger's click, move and release,
also when a listener of the touch event has run another handler in
between, as a dispatch from inside it would; the last event's time for
an enter and its move, a leave, and a cancel's release and
onTouchCancel; a time past 2^31 and the one after the wrap as they came.
This commit is contained in:
Jorijn van der Graaf 2026-11-02 16:01:32 +01:00
commit 364d2101c9
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 268 additions and 6 deletions

View file

@ -482,6 +482,7 @@ void Device::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_
if (focusedWindow == nullptr) {
return;
}
focusedWindow->lastInputTime = time;
if (button == BTN_LEFT) {
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
Device::focusedWindow->mouseLeftHeld = true;
@ -506,6 +507,7 @@ void Device::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, std
return;
}
Vector<float, 2> pos(wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y));
Device::focusedWindow->lastInputTime = time;
Device::focusedWindow->PointerMove(pos * Device::focusedWindow->scale);
}
@ -539,11 +541,12 @@ void Device::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t,
}
}
void Device::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t axis, wl_fixed_t value) {
void Device::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t time, std::uint32_t axis, wl_fixed_t value) {
// Vertical wheel only — Window has no horizontal-scroll event (yet).
if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL || focusedWindow == nullptr) {
return;
}
focusedWindow->lastInputTime = time;
// libinput convention (followed by wlroots, Mutter and KWin): one wheel
// detent = 15 axis units, positive = wheel down / toward the user — the
// same sign as DOM deltaY. Window::onMouseScroll speaks whole detents
@ -708,7 +711,7 @@ void Device::ForgetTouches() {
// clipboard has a serial and Device::focusedWindow a window), presses the
// left button, moves it and releases it. Every finger is also reported as
// such, in physical pixels.
void Device::touch_down(void*, wl_touch*, std::uint32_t serial, std::uint32_t, wl_surface* surface, std::int32_t id, wl_fixed_t x, wl_fixed_t y) {
void Device::touch_down(void*, wl_touch*, std::uint32_t serial, std::uint32_t time, wl_surface* surface, std::int32_t id, wl_fixed_t x, wl_fixed_t y) {
Window* window = nullptr;
for (Window* candidate : windows) {
if (candidate->surface == surface) window = candidate;
@ -716,6 +719,7 @@ void Device::touch_down(void*, wl_touch*, std::uint32_t serial, std::uint32_t, w
if (window == nullptr) return;
touchWindow = window;
window->lastPointerSerial_ = serial;
window->lastInputTime = time;
const Vector<float, 2> pos = Vector<float, 2>(wl_fixed_to_double(x), wl_fixed_to_double(y)) * window->scale;
if (TouchSlot* slot = SlotFor(id)) {
slot->x = pos.x;
@ -728,11 +732,16 @@ void Device::touch_down(void*, wl_touch*, std::uint32_t serial, std::uint32_t, w
focusedWindow = window;
window->currentMousePos = pos;
window->mouseLeftHeld = true;
// The time once more: onTouchDown's listeners ran in between, and
// one that dispatches Wayland events itself (it makes a window, or
// reads the clipboard) runs the handlers of what is queued behind
// this event, which set it. The click is this touch's.
window->lastInputTime = time;
window->onMouseLeftClick.Invoke();
}
}
void Device::touch_up(void*, wl_touch*, std::uint32_t, std::uint32_t, std::int32_t id) {
void Device::touch_up(void*, wl_touch*, std::uint32_t, std::uint32_t time, std::int32_t id) {
if (touchWindow == nullptr) return;
Window* window = touchWindow;
Vector<float, 2> pos = window->currentMousePos;
@ -741,20 +750,24 @@ void Device::touch_up(void*, wl_touch*, std::uint32_t, std::uint32_t, std::int32
pos = Vector<float, 2>(slot->x, slot->y);
*slot = TouchSlot{};
}
window->lastInputTime = time;
window->onTouchUp.Invoke({ id, pos });
if (id == window->primaryTouchId) {
window->primaryTouchId = -1;
window->mouseLeftHeld = false;
// Once more, as in touch_down: onTouchUp's listeners ran in between.
window->lastInputTime = time;
window->onMouseLeftRelease.Invoke();
}
if (!AnyTouch()) touchWindow = nullptr;
}
void Device::touch_motion(void*, wl_touch*, std::uint32_t, std::int32_t id, wl_fixed_t x, wl_fixed_t y) {
void Device::touch_motion(void*, wl_touch*, std::uint32_t time, std::int32_t id, wl_fixed_t x, wl_fixed_t y) {
if (touchWindow == nullptr) return;
Window* window = touchWindow;
TouchSlot* slot = SlotFor(id);
if (slot != nullptr && slot->window != nullptr) window = slot->window;
window->lastInputTime = time;
const Vector<float, 2> pos = Vector<float, 2>(wl_fixed_to_double(x), wl_fixed_to_double(y)) * window->scale;
if (slot != nullptr) {
slot->x = pos.x;
@ -763,6 +776,8 @@ void Device::touch_motion(void*, wl_touch*, std::uint32_t, std::int32_t id, wl_f
window->onTouchMove.Invoke({ id, pos });
if (id == window->primaryTouchId) {
window->currentMousePos = pos;
// Once more, as in touch_down: onTouchMove's listeners ran in between.
window->lastInputTime = time;
window->onMouseMove.Invoke();
}
}