From 364d2101c973905e4ace263ac2b2085e75de725e Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Mon, 2 Nov 2026 16:01:32 +0100 Subject: [PATCH] feat(wayland): add Window::lastInputTime, the compositor's time of a pointer or touch event MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- AGENTS.consumer.md | 2 +- AGENTS.template.md | 2 +- implementations/Crafter.Graphics-Device.cpp | 23 +- interfaces/Crafter.Graphics-Window.cppm | 17 ++ project.cpp | 7 + tests/InputTime/main.cpp | 223 ++++++++++++++++++++ 6 files changed, 268 insertions(+), 6 deletions(-) create mode 100644 tests/InputTime/main.cpp diff --git a/AGENTS.consumer.md b/AGENTS.consumer.md index bf4f01d..dcb1fd1 100644 --- a/AGENTS.consumer.md +++ b/AGENTS.consumer.md @@ -128,7 +128,7 @@ Heap-only: check `Device::rayTracingSupported`, or expect `std::runtime_error` f ### Input -- `Window` events, `Event` unless noted: `onMouseMove` (then `currentMousePos`, `lastMousePos`, `mouseDelta`), `onMouseEnter` and `onMouseLeave` with `mouseInside` (native only: the DOM build declares the two events but never fires them, and has no `mouseInside`), `onMouseLeftClick`, `onMouseLeftHold`, `onMouseLeftRelease` and the right-button trio (`mouseLeftHeld`, `mouseRightHeld`), `onMouseScroll` (`Event`: whole detents, reinterpret the payload as `int32`, positive is wheel down, every backend normalised to one per notch), `onRawKeyDown`, `onRawKeyHold`, `onRawKeyUp` (`Event`, raw platform codes; `heldKeys`), `onTextInput` (`Event`, UTF-8; repeats are synthesised on Wayland), `onPreedit` and `onTextInputDelete` (input methods), `onTouchDown`, `onTouchMove`, `onTouchUp` (`Event`; the first finger also drives the pointer events), `onClose`, `onResize`, `onBeforeUpdate`, `onUpdate` (`Event`: `now`, `delta`). +- `Window` events, `Event` unless noted: `onMouseMove` (then `currentMousePos`, `lastMousePos`, `mouseDelta`), `onMouseEnter` and `onMouseLeave` with `mouseInside` (native only: the DOM build declares the two events but never fires them, and has no `mouseInside`), `onMouseLeftClick`, `onMouseLeftHold`, `onMouseLeftRelease` and the right-button trio (`mouseLeftHeld`, `mouseRightHeld`), `onMouseScroll` (`Event`: whole detents, reinterpret the payload as `int32`, positive is wheel down, every backend normalised to one per notch), `onRawKeyDown`, `onRawKeyHold`, `onRawKeyUp` (`Event`, raw platform codes; `heldKeys`), `onTextInput` (`Event`, UTF-8; repeats are synthesised on Wayland), `onPreedit` and `onTextInputDelete` (input methods), `onTouchDown`, `onTouchMove`, `onTouchUp` (`Event`; the first finger also drives the pointer events), `onClose`, `onResize`, `onBeforeUpdate`, `onUpdate` (`Event`: `now`, `delta`). On Wayland `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; an event the compositor sends without a time — an enter and the move that follows it, a leave, a touch cancel and its release — keeps the last one's): a consumer that dates gestures — a fling's velocity, a tap against a long press — reads it instead of its own clock at dispatch, since events queued while the loop was busy keep the times they happened at. A listener that dispatches Wayland events itself (it makes a `Window`, or calls `Clipboard::GetText`) has them set it too, for itself from there on and for the listeners of its event that come after it. The other backends have no such member. - Keys: `Key(CrafterKeys::Space)` is `consteval` and folds to the platform's raw code (a Win32 scancode, a Linux keycode), so bindings stay portable in source and compare against `onRawKeyDown`'s `KeyCode` at runtime. Bindings serialised with `Input::BindingToString` are platform-specific. - `Input::Map`: `auto& jump = map.AddAction("Jump", Input::ActionType::Button);` (`Button`, `Axis1D`, `Vector2`), `jump.bindings = { Input::KeyBind{Key(CrafterKeys::Space)}, Input::GamepadButtonBind{id, Gamepad::Button::South} };` (also `MouseButtonBind`, `MouseScrollBind`, `MouseDeltaBind{scale}`, `GamepadAxisBind`, `GamepadStickBind{id, Gamepad::Stick::Left}`, `WASDBind{up, down, left, right}`), `map.Attach(window);`, and `map.Tick();` once per frame, after `Gamepad::Tick()`, which the loop runs before `onBeforeUpdate`. Every `Tick` recomputes the mouse delta and drains the scroll accumulator, and on Wayland `onBeforeUpdate` fires on every loop wake, so either tick from `onUpdate`, or tick in `onBeforeUpdate` and consume the delta and scroll actions in that same listener, as the Sponza example does. Read `action.pressed`, `value` or `vector2`, or subscribe to `onPerformed`, `onCanceled`, `onValueChanged`, `onVector2Changed`. `StartRebind(action, CaptureMask, callback)` captures the next input. - `Gamepad::connected`, `onConnected`, `onDisconnected`, per-device `buttons[]`, `axes[]` and events, `Rumble(dev, low, high, duration)`, `FindById(id)`; buttons are named by position (`South`, `East`, `West`, `North`). `Clipboard::SetText(text)` and `GetText()` (`std::optional`). diff --git a/AGENTS.template.md b/AGENTS.template.md index 6582d28..550bea3 100644 --- a/AGENTS.template.md +++ b/AGENTS.template.md @@ -72,7 +72,7 @@ Tests are declared in `project.cpp`, not discovered, through the `LibraryTest(na A test is an `int main()`: exit 0 passes, nonzero fails, 77 skips (for a missing capability, never for a failed check). Each runs in its own process, in parallel, with a 60 s default timeout, and the output of a failed, crashed or timed-out test is in `build/test-logs/.log`; a later pass does not delete the log, so check its timestamp. What the tests need, in three groups: -- Pure logic, no GPU: they stage `Device`'s statics or feed synthetic data to the decision functions that were factored out for them (`Device::ResolveFeatures`, `ChooseDevice`, `CheckShaderCapabilities`, `GetMemoryType`, `PreferDirectDeviceWrite`, `PipelineCacheDataCompatible`, `AlignMappedFlushRange`, `BuildMipChainFinalBarriers`, `SwapchainStageUnion`, `BuildTlasWriteBarrier`, `UIRenderer::ClipFlags`, `Window::SwapchainMinImageCount`, `WorkaroundNvidiaAS::Patch`, `DescriptorHeapVulkan::ComputeLayout`, `FontAtlas::Packer`, the font and input-field helpers). `PushConstantRewrite` and `UIFusedShader` compile GLSL with glslang and check the SPIR-V with spirv-val at runtime, and `ExampleShaders` compiles every GLSL shader under `examples/` the way crafter-build would, the descriptor-set build included, so a shader contract change that breaks an example fails here without building the example; `FontAtlasDecode` reads `ui-shared.glsl` and `dom-webgpu.js` and holds their copies of the SDF encoding constants to `FontAtlas`'s; `MouseScroll` and `PointerEnterLeave` drive the Wayland listener callbacks directly, and `TitleWayland` reads the requests a window sends off a `wl_display` connected to a socketpair. +- Pure logic, no GPU: they stage `Device`'s statics or feed synthetic data to the decision functions that were factored out for them (`Device::ResolveFeatures`, `ChooseDevice`, `CheckShaderCapabilities`, `GetMemoryType`, `PreferDirectDeviceWrite`, `PipelineCacheDataCompatible`, `AlignMappedFlushRange`, `BuildMipChainFinalBarriers`, `SwapchainStageUnion`, `BuildTlasWriteBarrier`, `UIRenderer::ClipFlags`, `Window::SwapchainMinImageCount`, `WorkaroundNvidiaAS::Patch`, `DescriptorHeapVulkan::ComputeLayout`, `FontAtlas::Packer`, the font and input-field helpers). `PushConstantRewrite` and `UIFusedShader` compile GLSL with glslang and check the SPIR-V with spirv-val at runtime, and `ExampleShaders` compiles every GLSL shader under `examples/` the way crafter-build would, the descriptor-set build included, so a shader contract change that breaks an example fails here without building the example; `FontAtlasDecode` reads `ui-shared.glsl` and `dom-webgpu.js` and holds their copies of the SDF encoding constants to `FontAtlas`'s; `MouseScroll`, `PointerEnterLeave` and `InputTime` drive the Wayland listener callbacks directly, and `TitleWayland` reads the requests a window sends off a `wl_display` connected to a socketpair. - A headless device: `Device::Initialize()` and no `Window` (`HeapSlotReuse`, `DeferredDeletion`, `MeshLifetime`, `ImageStagingRelease`, `BLASBuildOptions`, the `TLAS*` tests, ...). Headless still means a Wayland display on Linux. Most of the RT ones check `Device::rayTracingSupported` and return 77 without it; `BLASBuildOptions`, `TLASInstanceDirtyTracking` and `MeshDecompressStagingRelease` do not, and crash on a GPU without ray tracing (`Device::RequireRayTracing` throws out of `main`). `DeviceSelection` skips with one GPU. - A window and a live compositor: `FrameLoopSync`, `SetupCmdBufferReuse`, `UIFallbackParity`, `SwapchainTransferDst`, `WindowUpdateClock`, `HeapASRead`, `HeapDynamicIndexHit`, `RTPassPushData`, `WindowOptionalGlobals` (hides xdg-decoration, viewporter and fractional scale from the window in turn), `SwapchainImageCount` (every present mode, and swapchains with more images than frames in flight, in child processes on llvmpipe where the host's GPU never makes one), `TextAntialiasing` (text through `DispatchText` and `DispatchFused` against stb_truetype's own antialiased rasterizer, at 14 and 64 px and stretched, plus a glyph mirrored by its UVs and empty texels shrunk below 4 px), and `FeatureRequests` for its retry case. They render, read the frame back with `Window::CaptureNextFrame` (a PNG, decoded with a private `STB_IMAGE_STATIC` copy of stb_image) or a transfer pass of their own, and check pixels. diff --git a/implementations/Crafter.Graphics-Device.cpp b/implementations/Crafter.Graphics-Device.cpp index 87e738d..c6b354c 100644 --- a/implementations/Crafter.Graphics-Device.cpp +++ b/implementations/Crafter.Graphics-Device.cpp @@ -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 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 pos = Vector(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 pos = window->currentMousePos; @@ -741,20 +750,24 @@ void Device::touch_up(void*, wl_touch*, std::uint32_t, std::uint32_t, std::int32 pos = Vector(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 pos = Vector(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(); } } diff --git a/interfaces/Crafter.Graphics-Window.cppm b/interfaces/Crafter.Graphics-Window.cppm index a1d3097..bf38930 100644 --- a/interfaces/Crafter.Graphics-Window.cppm +++ b/interfaces/Crafter.Graphics-Window.cppm @@ -273,6 +273,23 @@ export namespace Crafter { #endif #ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND + // The compositor's time of the pointer, touch or scroll event a + // listener is being invoked for: wl_pointer's and wl_touch's `time`, + // milliseconds from an undefined base, wrapping at 2^32, set before + // the event fires. A consumer that dates its gestures reads this, + // not its own clock at dispatch: events queued while the loop was + // busy keep the times they happened at, so a backlog is not read as + // a burst. 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. 0 + // before the first event that has one. A listener that dispatches + // Wayland events itself (it makes a Window, or calls + // Clipboard::GetText) has them set it too, for itself from there on + // and for the listeners of its event that come after it; the + // pointer events the first finger stands in for are given the + // touch's time again. + std::uint32_t lastInputTime = 0; + // Wakes the event loop when `fd` becomes readable and calls // `onReadable` from it, which must consume what made the descriptor // readable, or the loop wakes again at once. For a socket, or an diff --git a/project.cpp b/project.cpp index 624c1a7..cc40776 100644 --- a/project.cpp +++ b/project.cpp @@ -451,6 +451,13 @@ extern "C" Configuration CrafterBuildProject(std::span a // Drives the wl_pointer callbacks directly, as MouseScroll does. cfg.tests.push_back(LibraryTest("PointerEnterLeave")); + // Window::lastInputTime: the compositor's time of a pointer, + // touch or scroll event, set before the event fires, and kept + // through an event that has none (an enter, a leave, a touch + // cancel). Drives the wl_pointer and wl_touch callbacks directly, + // as MouseScroll does. No compositor. + cfg.tests.push_back(LibraryTest("InputTime")); + // SetTitle's xdg_toplevel.set_title, read off a wl_display on a // socketpair: the view's bytes only, and an empty view as an // empty title, not a null string. No compositor. diff --git a/tests/InputTime/main.cpp b/tests/InputTime/main.cpp new file mode 100644 index 0000000..9bc5b27 --- /dev/null +++ b/tests/InputTime/main.cpp @@ -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 +#include +#include + +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; + +// 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 inside; + +// What was seen since the last call. +std::vector Take() { + return std::exchange(seen, {}); +} + +void Report(const std::vector& got) { + for (const Seen& one : got) std::println(" {} at {}", one.event, one.time); +} + +void Expect(const std::vector& want, std::string_view what) { + const std::vector 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 enterSub(&window.onMouseEnter, record("enter")); + EventListener leaveSub(&window.onMouseLeave, record("leave")); + EventListener moveSub(&window.onMouseMove, record("move")); + EventListener leftClickSub(&window.onMouseLeftClick, record("left click")); + EventListener leftReleaseSub(&window.onMouseLeftRelease, record("left release")); + EventListener rightClickSub(&window.onMouseRightClick, record("right click")); + EventListener rightReleaseSub(&window.onMouseRightRelease, record("right release")); + EventListener scrollSub(&window.onMouseScroll, [&window](std::uint32_t) { seen.push_back({ "scroll", window.lastInputTime }); }); + EventListener touchDownSub(&window.onTouchDown, recordTouch("touch down")); + EventListener touchMoveSub(&window.onTouchMove, recordTouch("touch move")); + EventListener touchUpSub(&window.onTouchUp, recordTouch("touch up")); + EventListener 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; +}