//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; }