From 53458700b6f0b8ae16e00642366773602a2ab93a Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Thu, 8 Oct 2026 12:59:51 -0400 Subject: [PATCH 1/3] fix(macos): create fresh mouse events for each submission Avoid retaining a CoreGraphics mouse event for the backend lifetime. Preserve movement, button, and modifier metadata while reporting transient event allocation failures. Add regression coverage for aged cursor snapshots, dragging, modifier flags, and allocation failure recovery. --- src/platform/macos/macos_backend.cpp | 38 ++++--- .../fixtures/macos_backend_test_hooks.hpp | 45 +++++++- tests/fixtures/macos_backend_test_hooks.cpp | 106 +++++++++++++++-- tests/unit/test_macos_backend.cpp | 107 +++++++++++++++++- 4 files changed, 266 insertions(+), 30 deletions(-) diff --git a/src/platform/macos/macos_backend.cpp b/src/platform/macos/macos_backend.cpp index 16f3f4a..b28a928 100644 --- a/src/platform/macos/macos_backend.cpp +++ b/src/platform/macos/macos_backend.cpp @@ -487,23 +487,19 @@ namespace lvh::detail { * * Local-event suppression on `source` is set to zero so cursor warps do not * discard the following click or mouse-moved events. CoreGraphics copies - * that configuration into each event at creation time, so `mouse_event` is - * refreshed from `source` after the interval is configured. + * that configuration into each event at creation time, so mouse events are + * created after the interval is configured. */ class MacosInputState { public: MacosInputState(): source {CGEventSourceCreate(kCGEventSourceStateHIDSystemState)}, keyboard_source {CGEventSourceCreate(kCGEventSourceStatePrivate)}, - mouse_event {source ? CGEventCreate(source) : nullptr}, scroll_lines_per_detent_value {read_scroll_lines_per_detent(scrollwheel_scaling)} { // Each move warps the cursor. The default 250ms suppression then drops the // following click and mouse-moved events. The cursor image still moves. if (source) { CGEventSourceSetLocalEventsSuppressionInterval(source, 0); - if (mouse_event) { - CGEventSetSource(mouse_event, source); - } } } @@ -513,9 +509,6 @@ namespace lvh::detail { MacosInputState &operator=(MacosInputState &&) noexcept = delete; ~MacosInputState() { - if (mouse_event) { - CFRelease(mouse_event); - } if (keyboard_source) { CFRelease(keyboard_source); } @@ -526,7 +519,6 @@ namespace lvh::detail { CGEventSourceRef source {}; ///< Mouse/scroll source with local-event suppression disabled. CGEventSourceRef keyboard_source {}; ///< CoreGraphics event source for keyboard events. - CGEventRef mouse_event {}; ///< Reusable mouse event refreshed from `source` after suppression is disabled. double scrollwheel_scaling = default_scrollwheel_scaling; ///< Raw macOS scroll-wheel scaling preference. int scroll_lines_per_detent_value = default_scroll_lines_per_detent; ///< Logical lines per wheel detent. CGEventFlags keyboard_flags {}; ///< Active modifier flags applied to mouse events. @@ -734,7 +726,7 @@ namespace lvh::detail { } /** - * @brief Backend mouse backed by CoreGraphics mouse and scroll events. + * @brief Backend mouse that creates a fresh CoreGraphics event for each submission. */ class MacosMouse final: public BackendMouse { public: @@ -753,7 +745,7 @@ namespace lvh::detail { if (!open_) { return OperationStatus::failure(ErrorCode::device_closed, "macOS mouse is closed"); } - if (!state_->source || !state_->mouse_event) { + if (!state_->source) { return OperationStatus::failure(ErrorCode::backend_failure, "macOS mouse event source is unavailable"); } @@ -798,6 +790,16 @@ namespace lvh::detail { return current; } + /** + * @brief Create and post a mouse event with a current timestamp and event metadata. + * + * @param button CoreGraphics mouse button. + * @param type Mouse movement, drag, or button event type. + * @param raw_location Requested cursor location before display clamping. + * @param previous_location Cursor location used to calculate movement deltas. + * @param click_count Number of clicks in the current button sequence. + * @return Submission status, including an event allocation failure when applicable. + */ OperationStatus post_mouse( CGMouseButton button, CGEventType type, @@ -810,10 +812,11 @@ namespace lvh::detail { std::clamp(raw_location.y, viewport_bounds_.origin.y, viewport_bounds_.origin.y + viewport_bounds_.size.height - 1) }; - const auto event = state_->mouse_event; - CGEventSetType(event, type); - CGEventSetLocation(event, location); - CGEventSetIntegerValueField(event, kCGMouseEventButtonNumber, button); + const auto event = CGEventCreateMouseEvent(state_->source, type, location, button); + if (!event) { + return OperationStatus::failure(ErrorCode::backend_failure, "create macOS mouse event"); + } + CGEventSetIntegerValueField(event, kCGMouseEventClickState, click_count); CGEventSetDoubleValueField(event, kCGMouseEventDeltaX, raw_location.x - previous_location.x); CGEventSetDoubleValueField(event, kCGMouseEventDeltaY, raw_location.y - previous_location.y); @@ -824,6 +827,7 @@ namespace lvh::detail { } CGEventPost(kCGHIDEventTap, event); + CFRelease(event); CGWarpMouseCursorPosition(location); return OperationStatus::success(); } @@ -927,7 +931,7 @@ namespace lvh::detail { return {OperationStatus::failure(ErrorCode::unsupported_profile, "device profile is not a mouse"), nullptr}; } auto state = input_state(); - if (!state->source || !state->mouse_event) { + if (!state->source) { return {OperationStatus::failure(ErrorCode::backend_failure, "macOS mouse event source is unavailable"), nullptr}; } diff --git a/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp b/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp index 1fd2a90..4e7802a 100644 --- a/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp +++ b/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp @@ -5,6 +5,7 @@ #pragma once // standard includes +#include #include #include #include @@ -59,6 +60,31 @@ namespace lvh::detail::test { std::uint32_t event_type {}; ///< CoreGraphics mouse event type value. }; + /** + * @brief Metadata captured from a CoreGraphics mouse event at submission time. + */ + struct MacosMouseEventResult { + std::uint32_t tap_location {}; ///< CoreGraphics event tap used for posting. + std::uint32_t event_type {}; ///< CoreGraphics event type. + std::int64_t button {}; ///< CoreGraphics mouse button number. + std::int64_t click_count {}; ///< Number of clicks in the button sequence. + std::uint64_t timestamp {}; ///< Event timestamp in nanoseconds since startup. + std::uint64_t flags {}; ///< Keyboard modifier flags applied to the event. + std::optional suppression_interval; ///< Local-event suppression copied into the event source. + MacosPoint location; ///< Clamped cursor position. + MacosPoint delta; ///< Requested movement delta before clamping. + }; + + /** + * @brief Results of mouse submissions with posting and cursor warping intercepted. + */ + struct MacosMouseEventsResult { + std::vector statuses; ///< Status of each requested submission. + std::vector events; ///< Events captured before posting. + std::size_t creation_attempts {}; ///< Calls to create a new CoreGraphics mouse event. + std::size_t cursor_warps {}; ///< Calls to update the cursor after posting. + }; + /** * @brief Portable view of one CoreGraphics keyboard event the macOS backend built. */ @@ -192,6 +218,23 @@ namespace lvh::detail::test { */ MacosMouseMotionResult macos_backend_mouse_motion(bool left_down, bool right_down, bool middle_down); + /** + * @brief Submit mouse events using aged cursor snapshots without changing the desktop. + * + * Cursor snapshots have timestamp 1 and location (40, 60) on a display with + * bounds (10, 20, 400, 200). Shift and Control modifiers are held throughout. + * + * @param events Mouse events to submit through the production backend. + * @param fail_first_creation Whether the first mouse event allocation should fail. + * @param source_available Whether the mouse event source remains available. + * @return Submission statuses, posted event metadata, and allocation and warp counts. + */ + MacosMouseEventsResult macos_backend_mouse_events( + const std::vector &events, + bool fail_first_creation = false, + bool source_available = true + ); + /** * @brief Exercise macOS backend creation and unsupported-device paths. * @@ -200,7 +243,7 @@ namespace lvh::detail::test { MacosBackendUtilityResult macos_backend_utilities(); /** - * @brief Read the suppression interval stored on the backend's reusable mouse event. + * @brief Read the suppression interval stored on a freshly submitted backend mouse event. * * @return Suppression interval in seconds, or no value if event-source creation fails. */ diff --git a/tests/fixtures/macos_backend_test_hooks.cpp b/tests/fixtures/macos_backend_test_hooks.cpp index 90567eb..c733117 100644 --- a/tests/fixtures/macos_backend_test_hooks.cpp +++ b/tests/fixtures/macos_backend_test_hooks.cpp @@ -61,8 +61,60 @@ namespace { lvh::detail::test::MacosPoint cursor_location; std::optional posted_location; std::optional warped_location; + lvh::detail::test::MacosMouseEventsResult *events = nullptr; ///< Optional capture of native event metadata. + bool fail_first_creation = false; ///< Whether to fail the next mouse event allocation. }; + /** + * @brief Age cursor snapshots while capturing fresh mouse event submissions. + * + * @param source CoreGraphics event source. + * @return Native cursor snapshot, or null when allocation fails. + */ + CGEventRef mouse_create_event(CGEventSourceRef source) { + const auto event = CGEventCreate(source); + if (event && mouse_environment() && mouse_environment()->events) { + CGEventSetTimestamp(event, 1); + } + return event; + } + + /** + * @brief Count native mouse event allocations and optionally fail the first allocation. + * + * @param source CoreGraphics event source. + * @param type Requested event type. + * @param location Cursor location. + * @param button CoreGraphics button number. + * @return New native mouse event, or null for the injected failure. + */ + CGEventRef mouse_create_mouse_event(CGEventSourceRef source, CGEventType type, CGPoint location, CGMouseButton button) { + if (mouse_environment() && mouse_environment()->events) { + ++mouse_environment()->events->creation_attempts; + if (mouse_environment()->fail_first_creation) { + mouse_environment()->fail_first_creation = false; + return nullptr; + } + } + return CGEventCreateMouseEvent(source, type, location, button); + } + + /** + * @brief Read the local-event suppression copied into a native event source. + * + * @param event Mouse event whose source is inspected. + * @return Suppression interval in seconds, or no value when source creation fails. + */ + std::optional mouse_event_suppression_interval(CGEventRef event) { + const auto source = CGEventCreateSourceFromEvent(event); + if (!source) { + return std::nullopt; + } + const auto interval = CGEventSourceGetLocalEventsSuppressionInterval(source); + CFRelease(source); + return interval; + } + CGDirectDisplayID mouse_main_display_id() { return mouse_environment() ? 42 : CGMainDisplayID(); } @@ -89,6 +141,19 @@ namespace { } const auto point = CGEventGetLocation(event); mouse_environment()->posted_location = {.x = point.x, .y = point.y}; + if (mouse_environment()->events) { + mouse_environment()->events->events.push_back({ + .tap_location = static_cast(tap), + .event_type = static_cast(CGEventGetType(event)), + .button = CGEventGetIntegerValueField(event, kCGMouseEventButtonNumber), + .click_count = CGEventGetIntegerValueField(event, kCGMouseEventClickState), + .timestamp = CGEventGetTimestamp(event), + .flags = static_cast(CGEventGetFlags(event)), + .suppression_interval = mouse_event_suppression_interval(event), + .location = {point.x, point.y}, + .delta = {CGEventGetDoubleValueField(event, kCGMouseEventDeltaX), CGEventGetDoubleValueField(event, kCGMouseEventDeltaY)}, + }); + } } CGError mouse_warp_cursor_position(CGPoint point) { @@ -96,6 +161,9 @@ namespace { return CGWarpMouseCursorPosition(point); } mouse_environment()->warped_location = {.x = point.x, .y = point.y}; + if (mouse_environment()->events) { + ++mouse_environment()->events->cursor_warps; + } return kCGErrorSuccess; } @@ -105,6 +173,8 @@ namespace { // separate namespace so instrumented inline symbols remain isolated. #define macos macos_backend_test #define CGMainDisplayID mouse_main_display_id +#define CGEventCreate mouse_create_event +#define CGEventCreateMouseEvent mouse_create_mouse_event #define CGDisplayBounds mouse_display_bounds #define CGEventGetLocation mouse_event_location #define CGEventPost mouse_event_post @@ -116,6 +186,8 @@ namespace { #undef CGEventPost #undef CGEventGetLocation #undef CGDisplayBounds +#undef CGEventCreateMouseEvent +#undef CGEventCreate #undef CGMainDisplayID #undef macos @@ -124,17 +196,8 @@ namespace lvh::detail::test { namespace macos = lvh::detail::macos_backend_test; std::optional macos_backend_mouse_event_suppression_interval() { - macos::MacosInputState state; - if (!state.mouse_event) { - return std::nullopt; - } - const auto source = CGEventCreateSourceFromEvent(state.mouse_event); - if (!source) { - return std::nullopt; - } - const auto interval = CGEventSourceGetLocalEventsSuppressionInterval(source); - CFRelease(source); - return interval; + const auto result = macos_backend_mouse_events({{.kind = MouseEventKind::relative_motion, .x = 1, .y = 1}}); + return result.events.empty() ? std::nullopt : result.events.front().suppression_interval; } std::optional macos_backend_key_code(KeyboardKeyCode key_code) { @@ -263,6 +326,27 @@ namespace lvh::detail::test { }; } + MacosMouseEventsResult macos_backend_mouse_events(const std::vector &events, bool fail_first_creation, bool source_available) { + MacosMouseEventsResult result; + MouseEnvironment environment {{.origin_x = 10, .origin_y = 20, .width = 400, .height = 200}}; + environment.cursor_location = {40, 60}; + environment.events = &result; + environment.fail_first_creation = fail_first_creation; + + auto state = std::make_shared(); + state->keyboard_flags = kCGEventFlagMaskShift | kCGEventFlagMaskControl; + if (!source_available && state->source) { + CFRelease(state->source); + state->source = nullptr; + } + + macos::MacosMouse mouse {std::move(state), {}}; + for (const auto &event : events) { + result.statuses.push_back(mouse.submit(event)); + } + return result; + } + MacosBackendUtilityResult macos_backend_utilities() { auto backend = create_platform_backend_for_macos_backend_test_hooks(); MacosBackendUtilityResult result; diff --git a/tests/unit/test_macos_backend.cpp b/tests/unit/test_macos_backend.cpp index 06e92fa..c21bcbb 100644 --- a/tests/unit/test_macos_backend.cpp +++ b/tests/unit/test_macos_backend.cpp @@ -21,7 +21,7 @@ */ class MacosBackendTest: public MacOSTest {}; -TEST_F(MacosBackendTest, DisablesSuppressionForReusableMouseEvent) { +TEST_F(MacosBackendTest, DisablesSuppressionForFreshMouseEvent) { const auto interval = lvh::detail::test::macos_backend_mouse_event_suppression_interval(); ASSERT_TRUE(interval.has_value()); EXPECT_DOUBLE_EQ(*interval, 0.0); @@ -355,6 +355,111 @@ TEST_F(MacosBackendTest, SelectsMouseMotionMetadataForHeldButtons) { EXPECT_EQ(motion.event_type, kCGEventOtherMouseDragged); } +TEST_F(MacosBackendTest, PostsFreshMouseEventsAfterAgedCursorSnapshots) { + const auto result = lvh::detail::test::macos_backend_mouse_events({ + {.kind = lvh::MouseEventKind::relative_motion, .x = 10, .y = -5}, + {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::left, .pressed = true}, + {.kind = lvh::MouseEventKind::relative_motion, .x = 500, .y = -100}, + {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::left, .pressed = false}, + {.kind = lvh::MouseEventKind::absolute_motion, .x = 100, .y = 100, .width = 200, .height = 100}, + }); + + ASSERT_EQ(result.statuses.size(), 5U); + for (const auto &status : result.statuses) { + ASSERT_TRUE(status.ok()) << status.message(); + } + ASSERT_EQ(result.events.size(), 5U); + EXPECT_EQ(result.creation_attempts, 5U); + EXPECT_EQ(result.cursor_warps, 5U); + for (const auto &event : result.events) { + EXPECT_EQ(event.tap_location, kCGHIDEventTap); + EXPECT_GT(event.timestamp, 1U); + EXPECT_EQ(event.flags, kCGEventFlagMaskShift | kCGEventFlagMaskControl); + ASSERT_TRUE(event.suppression_interval.has_value()); + EXPECT_DOUBLE_EQ(*event.suppression_interval, 0.0); + EXPECT_EQ(event.button, kCGMouseButtonLeft); + } + + EXPECT_EQ(result.events[0].event_type, kCGEventMouseMoved); + EXPECT_DOUBLE_EQ(result.events[0].location.x, 50); + EXPECT_DOUBLE_EQ(result.events[0].location.y, 55); + EXPECT_DOUBLE_EQ(result.events[0].delta.x, 10); + EXPECT_DOUBLE_EQ(result.events[0].delta.y, -5); + EXPECT_EQ(result.events[1].event_type, kCGEventLeftMouseDown); + EXPECT_EQ(result.events[1].click_count, 1); + EXPECT_EQ(result.events[2].event_type, kCGEventLeftMouseDragged); + EXPECT_DOUBLE_EQ(result.events[2].location.x, 409); + EXPECT_DOUBLE_EQ(result.events[2].location.y, 20); + EXPECT_DOUBLE_EQ(result.events[2].delta.x, 500); + EXPECT_DOUBLE_EQ(result.events[2].delta.y, -100); + EXPECT_EQ(result.events[3].event_type, kCGEventLeftMouseUp); + EXPECT_EQ(result.events[3].click_count, 1); + EXPECT_EQ(result.events[4].event_type, kCGEventMouseMoved); + EXPECT_DOUBLE_EQ(result.events[4].location.x, 210); + EXPECT_DOUBLE_EQ(result.events[4].location.y, 219); + EXPECT_DOUBLE_EQ(result.events[4].delta.x, 170); + EXPECT_DOUBLE_EQ(result.events[4].delta.y, 160); +} + +TEST_F(MacosBackendTest, PreservesRightAndMiddleButtonDragMetadataOnFreshEvents) { + const auto result = lvh::detail::test::macos_backend_mouse_events({ + {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::right, .pressed = true}, + {.kind = lvh::MouseEventKind::relative_motion, .x = 1, .y = 2}, + {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::right, .pressed = false}, + {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::middle, .pressed = true}, + {.kind = lvh::MouseEventKind::relative_motion, .x = -1, .y = -2}, + {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::middle, .pressed = false}, + }); + + for (const auto &status : result.statuses) { + ASSERT_TRUE(status.ok()) << status.message(); + } + ASSERT_EQ(result.events.size(), 6U); + EXPECT_EQ(result.events[0].event_type, kCGEventRightMouseDown); + EXPECT_EQ(result.events[1].event_type, kCGEventRightMouseDragged); + EXPECT_EQ(result.events[2].event_type, kCGEventRightMouseUp); + EXPECT_EQ(result.events[3].event_type, kCGEventOtherMouseDown); + EXPECT_EQ(result.events[4].event_type, kCGEventOtherMouseDragged); + EXPECT_EQ(result.events[5].event_type, kCGEventOtherMouseUp); + for (std::size_t index = 0; index < result.events.size(); ++index) { + EXPECT_EQ(result.events[index].button, index < 3 ? kCGMouseButtonRight : kCGMouseButtonCenter); + EXPECT_GT(result.events[index].timestamp, 1U); + } +} + +TEST_F(MacosBackendTest, RecoversFromMouseEventAllocationFailureOnNextSubmission) { + const auto result = lvh::detail::test::macos_backend_mouse_events({ + {.kind = lvh::MouseEventKind::relative_motion, .x = 1, .y = 2}, + {.kind = lvh::MouseEventKind::relative_motion, .x = 3, .y = 4}, + }, + true); + + ASSERT_EQ(result.statuses.size(), 2U); + EXPECT_EQ(result.statuses[0].code(), lvh::ErrorCode::backend_failure); + EXPECT_EQ(result.statuses[0].message(), "create macOS mouse event"); + ASSERT_TRUE(result.statuses[1].ok()) << result.statuses[1].message(); + EXPECT_EQ(result.creation_attempts, 2U); + EXPECT_EQ(result.cursor_warps, 1U); + ASSERT_EQ(result.events.size(), 1U); + EXPECT_GT(result.events[0].timestamp, 1U); + EXPECT_DOUBLE_EQ(result.events[0].delta.x, 3); + EXPECT_DOUBLE_EQ(result.events[0].delta.y, 4); +} + +TEST_F(MacosBackendTest, RejectsMouseSubmissionWithoutAnEventSource) { + const auto result = lvh::detail::test::macos_backend_mouse_events({ + {.kind = lvh::MouseEventKind::relative_motion, .x = 1, .y = 2}, + }, + false, + false); + + ASSERT_EQ(result.statuses.size(), 1U); + EXPECT_EQ(result.statuses[0].code(), lvh::ErrorCode::backend_failure); + EXPECT_EQ(result.creation_attempts, 0U); + EXPECT_EQ(result.cursor_warps, 0U); + EXPECT_TRUE(result.events.empty()); +} + TEST_F(MacosBackendTest, ReportsCapabilitiesAndUnsupportedDevices) { const auto result = lvh::detail::test::macos_backend_utilities(); From 37f0211ff6b2c6553fcf28eda3ed9daf9eb19ee9 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Thu, 8 Oct 2026 13:30:17 -0400 Subject: [PATCH 2/3] test(macos): correct fresh event assertions and capture state --- src/platform/macos/macos_backend.cpp | 2 +- .../fixtures/macos_backend_test_hooks.hpp | 2 +- tests/unit/test_macos_backend.cpp | 43 +++++++++++-------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/platform/macos/macos_backend.cpp b/src/platform/macos/macos_backend.cpp index b28a928..857bf1b 100644 --- a/src/platform/macos/macos_backend.cpp +++ b/src/platform/macos/macos_backend.cpp @@ -791,7 +791,7 @@ namespace lvh::detail { } /** - * @brief Create and post a mouse event with a current timestamp and event metadata. + * @brief Create and post a fresh native mouse event with the requested metadata. * * @param button CoreGraphics mouse button. * @param type Mouse movement, drag, or button event type. diff --git a/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp b/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp index 4e7802a..ad59930 100644 --- a/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp +++ b/tests/fixtures/include/fixtures/macos_backend_test_hooks.hpp @@ -68,7 +68,7 @@ namespace lvh::detail::test { std::uint32_t event_type {}; ///< CoreGraphics event type. std::int64_t button {}; ///< CoreGraphics mouse button number. std::int64_t click_count {}; ///< Number of clicks in the button sequence. - std::uint64_t timestamp {}; ///< Event timestamp in nanoseconds since startup. + std::uint64_t timestamp {}; ///< Native event timestamp, which may be unset before posting. std::uint64_t flags {}; ///< Keyboard modifier flags applied to the event. std::optional suppression_interval; ///< Local-event suppression copied into the event source. MacosPoint location; ///< Clamped cursor position. diff --git a/tests/unit/test_macos_backend.cpp b/tests/unit/test_macos_backend.cpp index c21bcbb..7309e1a 100644 --- a/tests/unit/test_macos_backend.cpp +++ b/tests/unit/test_macos_backend.cpp @@ -356,12 +356,14 @@ TEST_F(MacosBackendTest, SelectsMouseMotionMetadataForHeldButtons) { } TEST_F(MacosBackendTest, PostsFreshMouseEventsAfterAgedCursorSnapshots) { + using enum lvh::MouseEventKind; + const auto result = lvh::detail::test::macos_backend_mouse_events({ - {.kind = lvh::MouseEventKind::relative_motion, .x = 10, .y = -5}, - {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::left, .pressed = true}, - {.kind = lvh::MouseEventKind::relative_motion, .x = 500, .y = -100}, - {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::left, .pressed = false}, - {.kind = lvh::MouseEventKind::absolute_motion, .x = 100, .y = 100, .width = 200, .height = 100}, + {.kind = relative_motion, .x = 10, .y = -5}, + {.kind = button, .button = lvh::MouseButton::left, .pressed = true}, + {.kind = relative_motion, .x = 500, .y = -100}, + {.kind = button, .button = lvh::MouseButton::left, .pressed = false}, + {.kind = absolute_motion, .x = 100, .y = 100, .width = 200, .height = 100}, }); ASSERT_EQ(result.statuses.size(), 5U); @@ -373,7 +375,8 @@ TEST_F(MacosBackendTest, PostsFreshMouseEventsAfterAgedCursorSnapshots) { EXPECT_EQ(result.cursor_warps, 5U); for (const auto &event : result.events) { EXPECT_EQ(event.tap_location, kCGHIDEventTap); - EXPECT_GT(event.timestamp, 1U); + // Fresh synthetic events may have timestamp zero before posting; the aged snapshot has timestamp one. + EXPECT_NE(event.timestamp, 1U); EXPECT_EQ(event.flags, kCGEventFlagMaskShift | kCGEventFlagMaskControl); ASSERT_TRUE(event.suppression_interval.has_value()); EXPECT_DOUBLE_EQ(*event.suppression_interval, 0.0); @@ -402,13 +405,15 @@ TEST_F(MacosBackendTest, PostsFreshMouseEventsAfterAgedCursorSnapshots) { } TEST_F(MacosBackendTest, PreservesRightAndMiddleButtonDragMetadataOnFreshEvents) { + using enum lvh::MouseEventKind; + const auto result = lvh::detail::test::macos_backend_mouse_events({ - {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::right, .pressed = true}, - {.kind = lvh::MouseEventKind::relative_motion, .x = 1, .y = 2}, - {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::right, .pressed = false}, - {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::middle, .pressed = true}, - {.kind = lvh::MouseEventKind::relative_motion, .x = -1, .y = -2}, - {.kind = lvh::MouseEventKind::button, .button = lvh::MouseButton::middle, .pressed = false}, + {.kind = button, .button = lvh::MouseButton::right, .pressed = true}, + {.kind = relative_motion, .x = 1, .y = 2}, + {.kind = button, .button = lvh::MouseButton::right, .pressed = false}, + {.kind = button, .button = lvh::MouseButton::middle, .pressed = true}, + {.kind = relative_motion, .x = -1, .y = -2}, + {.kind = button, .button = lvh::MouseButton::middle, .pressed = false}, }); for (const auto &status : result.statuses) { @@ -423,14 +428,16 @@ TEST_F(MacosBackendTest, PreservesRightAndMiddleButtonDragMetadataOnFreshEvents) EXPECT_EQ(result.events[5].event_type, kCGEventOtherMouseUp); for (std::size_t index = 0; index < result.events.size(); ++index) { EXPECT_EQ(result.events[index].button, index < 3 ? kCGMouseButtonRight : kCGMouseButtonCenter); - EXPECT_GT(result.events[index].timestamp, 1U); + EXPECT_NE(result.events[index].timestamp, 1U); } } TEST_F(MacosBackendTest, RecoversFromMouseEventAllocationFailureOnNextSubmission) { + using enum lvh::MouseEventKind; + const auto result = lvh::detail::test::macos_backend_mouse_events({ - {.kind = lvh::MouseEventKind::relative_motion, .x = 1, .y = 2}, - {.kind = lvh::MouseEventKind::relative_motion, .x = 3, .y = 4}, + {.kind = relative_motion, .x = 1, .y = 2}, + {.kind = relative_motion, .x = 3, .y = 4}, }, true); @@ -441,14 +448,16 @@ TEST_F(MacosBackendTest, RecoversFromMouseEventAllocationFailureOnNextSubmission EXPECT_EQ(result.creation_attempts, 2U); EXPECT_EQ(result.cursor_warps, 1U); ASSERT_EQ(result.events.size(), 1U); - EXPECT_GT(result.events[0].timestamp, 1U); + EXPECT_NE(result.events[0].timestamp, 1U); EXPECT_DOUBLE_EQ(result.events[0].delta.x, 3); EXPECT_DOUBLE_EQ(result.events[0].delta.y, 4); } TEST_F(MacosBackendTest, RejectsMouseSubmissionWithoutAnEventSource) { + using enum lvh::MouseEventKind; + const auto result = lvh::detail::test::macos_backend_mouse_events({ - {.kind = lvh::MouseEventKind::relative_motion, .x = 1, .y = 2}, + {.kind = relative_motion, .x = 1, .y = 2}, }, false, false); From 77548c55b386ff18bd852eec8660df012c68750f Mon Sep 17 00:00:00 2001 From: Dave Lane <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:46:04 -0500 Subject: [PATCH 3/3] fix(macos): preserve button state on event allocation failure Update drag state and click timing only after a fresh mouse event is posted. Cover a failed press, a normal move, and a successful retry. --- src/platform/macos/macos_backend.cpp | 15 +++++++++++--- tests/unit/test_macos_backend.cpp | 29 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/platform/macos/macos_backend.cpp b/src/platform/macos/macos_backend.cpp index 857bf1b..f1eb228 100644 --- a/src/platform/macos/macos_backend.cpp +++ b/src/platform/macos/macos_backend.cpp @@ -845,6 +845,12 @@ namespace lvh::detail { return post_mouse(motion.button, motion.event_type, location, current_location(), 0); } + /** + * @brief Post a button transition and update the drag and click state on success. + * + * @param event Requested mouse button transition. + * @return Submission status. A failed allocation leaves button and click state unchanged. + */ OperationStatus submit_button(const MouseEvent &event) { using enum ErrorCode; @@ -853,14 +859,17 @@ namespace lvh::detail { return OperationStatus::failure(unsupported_profile, "macOS mouse backend supports only left, middle, and right buttons"); } - mouse_down_[button->index] = event.pressed; const auto now = std::chrono::steady_clock::now(); const auto release_index = event.pressed ? 0U : 1U; const auto mouse_position = current_location(); const auto click_count = now < last_mouse_event_[button->index][release_index] + multi_click_delay ? 2 : 1; - last_mouse_event_[button->index][release_index] = now; - return post_mouse(button->button, event.pressed ? button->down_event : button->up_event, mouse_position, mouse_position, click_count); + const auto status = post_mouse(button->button, event.pressed ? button->down_event : button->up_event, mouse_position, mouse_position, click_count); + if (status.ok()) { + mouse_down_[button->index] = event.pressed; + last_mouse_event_[button->index][release_index] = now; + } + return status; } OperationStatus submit_scroll(std::int32_t high_resolution_vertical, std::int32_t high_resolution_horizontal) const { diff --git a/tests/unit/test_macos_backend.cpp b/tests/unit/test_macos_backend.cpp index 7309e1a..1dca9d1 100644 --- a/tests/unit/test_macos_backend.cpp +++ b/tests/unit/test_macos_backend.cpp @@ -5,6 +5,7 @@ // standard includes #include +#include // lib includes #include @@ -453,6 +454,34 @@ TEST_F(MacosBackendTest, RecoversFromMouseEventAllocationFailureOnNextSubmission EXPECT_DOUBLE_EQ(result.events[0].delta.y, 4); } +TEST_F(MacosBackendTest, FailedButtonAllocationPreservesButtonState) { + using enum lvh::MouseEventKind; + + const std::vector events { + {.kind = button, .button = lvh::MouseButton::left, .pressed = true}, + {.kind = relative_motion, .x = 1, .y = 2}, + {.kind = button, .button = lvh::MouseButton::left, .pressed = true}, + {.kind = relative_motion, .x = 3, .y = 4}, + {.kind = button, .button = lvh::MouseButton::left, .pressed = false}, + {.kind = relative_motion, .x = 5, .y = 6}, + }; + const auto result = lvh::detail::test::macos_backend_mouse_events(events, true); + + ASSERT_EQ(result.statuses.size(), events.size()); + EXPECT_EQ(result.statuses.front().code(), lvh::ErrorCode::backend_failure); + for (std::size_t i = 1; i < result.statuses.size(); ++i) { + ASSERT_TRUE(result.statuses[i].ok()) << result.statuses[i].message(); + } + ASSERT_EQ(result.events.size(), 5U); + EXPECT_EQ(result.cursor_warps, result.events.size()); + EXPECT_EQ(result.events[0].event_type, kCGEventMouseMoved); + EXPECT_EQ(result.events[1].event_type, kCGEventLeftMouseDown); + EXPECT_EQ(result.events[1].click_count, 1); + EXPECT_EQ(result.events[2].event_type, kCGEventLeftMouseDragged); + EXPECT_EQ(result.events[3].event_type, kCGEventLeftMouseUp); + EXPECT_EQ(result.events[4].event_type, kCGEventMouseMoved); +} + TEST_F(MacosBackendTest, RejectsMouseSubmissionWithoutAnEventSource) { using enum lvh::MouseEventKind;