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

223
tests/InputTime/main.cpp Normal file
View file

@ -0,0 +1,223 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Window::lastInputTime on the Wayland backend: the compositor's time of the
// pointer, touch or scroll event a listener is being invoked for. The
// wl_pointer and wl_touch handlers received that time and dropped it, so a
// consumer could date an event only by its own clock at dispatch, by which
// events queued behind a busy loop all happen when the loop comes back.
//
// Drives the wl_pointer and wl_touch listener callbacks directly, as
// MouseScroll and PointerEnterLeave do, so no compositor is needed, and
// asserts:
//
// - 0 before the first event
// - the listeners of a motion, of the four button events, of a scroll and
// of a touch's down, motion and up each find that event's own time: it
// is set before the event fires
// - the pointer events the first finger stands in for (the left button's
// click and release, the move) find the touch event's time
// - and find it when a listener of the touch event has dispatched Wayland
// events in between, as one that makes a window or reads the clipboard
// does: the handlers of what is queued behind the touch run inside that
// listener and set the time, and the stand-in event comes after it. A
// press and its release dated by later events are a tap read as a long
// press, or the other way round
// - an event the compositor sends without a time keeps the last one:
// wl_pointer.enter's onMouseEnter and the move that follows it,
// wl_pointer.leave's onMouseLeave, and wl_touch.cancel's release and
// onTouchCancel
// - the time is kept as it came, past 2^31 and across the wrap at 2^32
#include <wayland-client.h>
#include <linux/input-event-codes.h>
#include <cstdlib>
import Crafter.Graphics;
import Crafter.Event;
import std;
using namespace Crafter;
namespace {
int failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
// An event as it fired, and the time its listener found on the window.
struct Seen {
std::string_view event;
std::uint32_t time;
bool operator==(const Seen&) const = default;
};
std::vector<Seen> seen;
// What the next touch listener to run does after it has recorded its event,
// once: the handler a dispatch from inside that listener would run.
std::function<void()> inside;
// What was seen since the last call.
std::vector<Seen> Take() {
return std::exchange(seen, {});
}
void Report(const std::vector<Seen>& got) {
for (const Seen& one : got) std::println(" {} at {}", one.event, one.time);
}
void Expect(const std::vector<Seen>& want, std::string_view what) {
const std::vector<Seen> got = Take();
Check(got == want, what);
if (got != want) Report(got);
}
// A default-constructed window has no wl_surface, so a null surface is the
// one the enter and the touch-down handlers find it by.
void Enter() {
Device::PointerListenerHandleEnter(nullptr, nullptr, /*serial=*/1, /*surface=*/nullptr, wl_fixed_from_int(5), wl_fixed_from_int(5));
}
void Motion(std::uint32_t time) {
Device::PointerListenerHandleMotion(nullptr, nullptr, time, wl_fixed_from_int(10), wl_fixed_from_int(10));
}
void Button(std::uint32_t time, std::uint32_t button, std::uint32_t state) {
Device::pointer_handle_button(nullptr, nullptr, /*serial=*/2, time, button, state);
}
// One detent of the vertical wheel: 15 axis units.
void Scroll(std::uint32_t time) {
Device::PointerListenerHandleAxis(nullptr, nullptr, time, WL_POINTER_AXIS_VERTICAL_SCROLL, wl_fixed_from_int(15));
}
void TouchDown(std::uint32_t time, std::int32_t id) {
Device::touch_down(nullptr, nullptr, /*serial=*/3, time, /*surface=*/nullptr, id, wl_fixed_from_int(20), wl_fixed_from_int(20));
}
void TouchMotion(std::uint32_t time, std::int32_t id) {
Device::touch_motion(nullptr, nullptr, time, id, wl_fixed_from_int(30), wl_fixed_from_int(30));
}
void TouchUp(std::uint32_t time, std::int32_t id) {
Device::touch_up(nullptr, nullptr, /*serial=*/4, time, id);
}
} // namespace
int main() {
Window window; // default ctor creates no surface — safe without a compositor
Device::windows.push_back(&window);
auto record = [&window](std::string_view event) {
return [&window, event] { seen.push_back({ event, window.lastInputTime }); };
};
auto recordTouch = [&window](std::string_view event) {
return [&window, event](TouchPoint) {
seen.push_back({ event, window.lastInputTime });
if (inside) std::exchange(inside, nullptr)();
};
};
EventListener<void> enterSub(&window.onMouseEnter, record("enter"));
EventListener<void> leaveSub(&window.onMouseLeave, record("leave"));
EventListener<void> moveSub(&window.onMouseMove, record("move"));
EventListener<void> leftClickSub(&window.onMouseLeftClick, record("left click"));
EventListener<void> leftReleaseSub(&window.onMouseLeftRelease, record("left release"));
EventListener<void> rightClickSub(&window.onMouseRightClick, record("right click"));
EventListener<void> rightReleaseSub(&window.onMouseRightRelease, record("right release"));
EventListener<std::uint32_t> scrollSub(&window.onMouseScroll, [&window](std::uint32_t) { seen.push_back({ "scroll", window.lastInputTime }); });
EventListener<TouchPoint> touchDownSub(&window.onTouchDown, recordTouch("touch down"));
EventListener<TouchPoint> touchMoveSub(&window.onTouchMove, recordTouch("touch move"));
EventListener<TouchPoint> touchUpSub(&window.onTouchUp, recordTouch("touch up"));
EventListener<void> touchCancelSub(&window.onTouchCancel, record("touch cancel"));
Check(window.lastInputTime == 0, "0 before the first event");
// The pointer. wl_pointer.enter carries no time.
Enter();
Expect({ { "enter", 0 }, { "move", 0 } }, "an enter and its move, which have no time, leave it");
Motion(1000);
Expect({ { "move", 1000 } }, "motion: its time, set before onMouseMove fires");
Button(1010, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
Button(1020, BTN_LEFT, WL_POINTER_BUTTON_STATE_RELEASED);
Button(1030, BTN_RIGHT, WL_POINTER_BUTTON_STATE_PRESSED);
Button(1040, BTN_RIGHT, WL_POINTER_BUTTON_STATE_RELEASED);
Expect({ { "left click", 1010 }, { "left release", 1020 }, { "right click", 1030 }, { "right release", 1040 } },
"the four button events: each its own time");
Scroll(1050);
Expect({ { "scroll", 1050 } }, "a scroll: its time, set before onMouseScroll fires");
Enter();
Expect({ { "move", 1050 } }, "a second enter's move keeps the last event's time");
Device::PointerListenerHandleLeave(nullptr, nullptr, /*serial=*/5, nullptr);
Expect({ { "leave", 1050 } }, "a leave, which has no time, keeps it too");
// The enter after it fires onMouseEnter again, now with a time to keep.
Enter();
Expect({ { "enter", 1050 }, { "move", 1050 } }, "and so does the enter after it, with its move");
// The touchscreen. The first finger also presses, moves and releases the
// pointer; the second is reported as a touch only.
TouchDown(2000, /*id=*/7);
Expect({ { "touch down", 2000 }, { "left click", 2000 } }, "a touch down, and the click the first finger stands in for: the touch's time");
TouchDown(2005, /*id=*/8);
Expect({ { "touch down", 2005 } }, "a second finger's down: its time");
TouchMotion(2010, /*id=*/7);
Expect({ { "touch move", 2010 }, { "move", 2010 } }, "a touch motion, and the first finger's move: the touch's time");
TouchUp(2020, /*id=*/8);
TouchUp(2030, /*id=*/7);
Expect({ { "touch up", 2020 }, { "touch up", 2030 }, { "left release", 2030 } }, "the touch ups, and the first finger's release: each touch's time");
// wl_touch.cancel carries no time either.
TouchDown(2040, /*id=*/7);
Take();
Device::touch_cancel(nullptr, nullptr);
Expect({ { "left release", 2040 }, { "touch cancel", 2040 } }, "a cancel's release and onTouchCancel keep the last event's time");
// A listener that dispatches Wayland events itself: the handlers of what
// is queued behind its event run inside it. Here the touch listener
// calls the handler such a dispatch would, with a later time, and the
// pointer event the first finger stands in for must still find its own
// touch's.
inside = [] { TouchMotion(3010, /*id=*/7); };
TouchDown(3000, /*id=*/7);
Expect({ { "touch down", 3000 }, { "touch move", 3010 }, { "left click", 3000 } },
"a motion dispatched inside onTouchDown: the click that follows is the down's");
inside = [] { TouchDown(3030, /*id=*/8); };
TouchMotion(3020, /*id=*/7);
Expect({ { "touch move", 3020 }, { "touch down", 3030 }, { "move", 3020 } },
"a second finger's down dispatched inside onTouchMove: the move that follows is the motion's");
inside = [] { TouchMotion(3050, /*id=*/8); };
TouchUp(3040, /*id=*/7);
Expect({ { "touch up", 3040 }, { "touch move", 3050 }, { "left release", 3040 } },
"a motion dispatched inside onTouchUp: the release that follows is the up's");
TouchUp(3060, /*id=*/8);
Take();
// Milliseconds on a 32-bit clock: nothing is done to the value.
Device::focusedWindow = &window;
Motion(0xFFFFFFF0u);
Motion(0x00000010u);
Expect({ { "move", 0xFFFFFFF0u }, { "move", 0x00000010u } }, "a time past 2^31, and the one after the wrap, are kept as they came");
std::erase(Device::windows, &window);
Device::focusedWindow = nullptr;
if (failures != 0) {
std::println("{} check(s) failed", failures);
return EXIT_FAILURE;
}
std::println("all checks passed");
return EXIT_SUCCESS;
}