From a5aaa9319f80b8e1f73ebe5ba50e71f044fc2a84 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Thu, 17 Sep 2026 16:05:35 -0400 Subject: [PATCH] fix(input): retarget relative mouse after display switches Consume pending touch-port geometry before forwarding relative mouse motion so libvirtualhid receives the streamed viewport selected by display switching. Reuse the same desktop conversion for absolute and relative input, avoid recreating the mouse when bounds are unchanged, and preserve the no-device path used on unsupported platforms. Add input-pipeline and virtual HID regression coverage. --- src/input.cpp | 82 +++++++++++++------ src/input.h | 9 ++ src/platform/common.h | 9 ++ src/platform/virtualhid_input.cpp | 13 +++ src/platform/virtualhid_input.h | 12 ++- tests/unit/platform/test_virtualhid_input.cpp | 35 ++++++++ tests/unit/test_input.cpp | 52 ++++++++++++ 7 files changed, 184 insertions(+), 28 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 9470c89f4..25e96cfb8 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -737,6 +737,38 @@ namespace input { } } + /** + * @brief Consume the latest streamed-display geometry for an input session. + * + * @param input Input session receiving the geometry. + */ + void refresh_touch_port(std::shared_ptr &input) { + auto &touch_port_event = input->touch_port_event; + if (touch_port_event->peek()) { + input->touch_port = *touch_port_event->pop(); + } + } + + /** + * @brief Convert stream touch geometry to platform pointer bounds. + * + * @param touch_port Stream touch geometry. + * @return Platform pointer bounds for the full desktop and streamed display. + */ + platf::touch_port_t platform_touch_port(const input::touch_port_t &touch_port) { + const bool has_logical_environment = touch_port.env_logical_width != 0 && touch_port.env_logical_height != 0; + return { + touch_port.offset_x, + touch_port.offset_y, + has_logical_environment ? touch_port.env_logical_width : touch_port.env_width, + has_logical_environment ? touch_port.env_logical_height : touch_port.env_height, + touch_port.logical_width, + touch_port.logical_height, + touch_port.env_offset_x, + touch_port.env_offset_y, + }; + } + /** * @brief Forward a client input packet directly to the platform backend. * @@ -749,7 +781,17 @@ namespace input { } input->mouse_left_button_timeout = DISABLE_LEFT_BUTTON_DELAY; - platf::move_mouse(platf_input, util::endian::big(packet->deltaX), util::endian::big(packet->deltaY)); + refresh_touch_port(input); + if (input->touch_port) { + platf::move_mouse( + platf_input, + platform_touch_port(input->touch_port), + util::endian::big(packet->deltaX), + util::endian::big(packet->deltaY) + ); + } else { + platf::move_mouse(platf_input, util::endian::big(packet->deltaX), util::endian::big(packet->deltaY)); + } } /** @@ -760,11 +802,8 @@ namespace input { * @return The host-relative coordinate pair if a touchport is available. */ std::optional> client_to_touchport(std::shared_ptr &input, const std::pair &val, const std::pair &size) { - auto &touch_port_event = input->touch_port_event; + refresh_touch_port(input); auto &touch_port = input->touch_port; - if (touch_port_event->peek()) { - touch_port = *touch_port_event->pop(); - } if (!touch_port) { BOOST_LOG(verbose) << "Ignoring early absolute input without a touch port"sv; return std::nullopt; @@ -876,28 +915,7 @@ namespace input { auto &touch_port = input->touch_port; - int touch_port_dim_x; - int touch_port_dim_y; - if (touch_port.env_logical_width != 0 && touch_port.env_logical_height != 0) { - touch_port_dim_x = touch_port.env_logical_width; - touch_port_dim_y = touch_port.env_logical_height; - } else { - touch_port_dim_x = touch_port.env_width; - touch_port_dim_y = touch_port.env_height; - } - - platf::touch_port_t abs_port { - touch_port.offset_x, - touch_port.offset_y, - touch_port_dim_x, - touch_port_dim_y, - touch_port.logical_width, - touch_port.logical_height, - touch_port.env_offset_x, - touch_port.env_offset_y, - }; - - platf::abs_mouse(platf_input, abs_port, tpcoords->first, tpcoords->second); + platf::abs_mouse(platf_input, platform_touch_port(touch_port), tpcoords->first, tpcoords->second); } /** @@ -2490,6 +2508,16 @@ namespace input { ::input::passthrough(input, &packet); } + void send_relative_mouse_packet(std::shared_ptr &input, std::int16_t delta_x, std::int16_t delta_y) { + NV_REL_MOUSE_MOVE_PACKET packet {}; + packet.header.size = util::endian::big(sizeof(packet) - sizeof(packet.header.size)); + packet.header.magic = util::endian::little(MOUSE_MOVE_REL_MAGIC_GEN5); + packet.deltaX = util::endian::big(delta_x); + packet.deltaY = util::endian::big(delta_y); + + ::input::passthrough(input, &packet); + } + void reset_keyboard_state() { task_pool.cancel(key_press_repeat_id); key_press_repeat_id = nullptr; diff --git a/src/input.h b/src/input.h index fc744b06d..f181f6336 100644 --- a/src/input.h +++ b/src/input.h @@ -164,6 +164,15 @@ namespace input { */ void send_keyboard_packet(std::shared_ptr &input, std::uint16_t key_code, std::uint8_t modifiers, std::uint8_t flags, bool release); + /** + * @brief Process one client relative-mouse packet on the calling thread. + * + * @param input Retained input state. + * @param delta_x Horizontal client mouse delta. + * @param delta_y Vertical client mouse delta. + */ + void send_relative_mouse_packet(std::shared_ptr &input, std::int16_t delta_x, std::int16_t delta_y); + /** * @brief Forget every key Sunshine tracks as pressed and cancel any pending key repeat. */ diff --git a/src/platform/common.h b/src/platform/common.h index 0d341253f..365f9a6fa 100644 --- a/src/platform/common.h +++ b/src/platform/common.h @@ -1158,6 +1158,15 @@ namespace platf { * @param deltaY Delta y. */ void move_mouse(input_t &input, int deltaX, int deltaY); + /** + * @brief Retarget the virtual mouse and move it relatively. + * + * @param input Platform input backend that receives the event. + * @param touch_port Desktop and streamed-display bounds for the pointer. + * @param deltaX Delta x. + * @param deltaY Delta y. + */ + void move_mouse(input_t &input, const touch_port_t &touch_port, int deltaX, int deltaY); /** * @brief Move the pointer to an absolute client-provided touch coordinate. * diff --git a/src/platform/virtualhid_input.cpp b/src/platform/virtualhid_input.cpp index ccc1882df..30ab2c4f9 100644 --- a/src/platform/virtualhid_input.cpp +++ b/src/platform/virtualhid_input.cpp @@ -885,6 +885,15 @@ namespace platf::virtualhid { } } + void move_mouse(input_context_t &context, const touch_port_t &touch_port, int delta_x, int delta_y) { + if (!context.mouse) { + return; + } + + context.update_mouse_viewport(touch_port); + move_mouse(context, delta_x, delta_y); + } + void abs_mouse(input_context_t &context, const touch_port_t &touch_port, float x, float y) { if (!context.mouse) { return; @@ -1141,6 +1150,10 @@ namespace platf { virtualhid::move_mouse(virtualhid::get_input_context(input), deltaX, deltaY); } + void move_mouse(input_t &input, const touch_port_t &touch_port, int deltaX, int deltaY) { + virtualhid::move_mouse(virtualhid::get_input_context(input), touch_port, deltaX, deltaY); + } + void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y) { virtualhid::abs_mouse(virtualhid::get_input_context(input), touch_port, x, y); } diff --git a/src/platform/virtualhid_input.h b/src/platform/virtualhid_input.h index 16c629cb7..47431963b 100644 --- a/src/platform/virtualhid_input.h +++ b/src/platform/virtualhid_input.h @@ -57,7 +57,7 @@ namespace platf::virtualhid { /** * @brief Retarget the shared mouse when streamed display geometry changes. * - * @param touch_port Desktop and viewport geometry for absolute input. + * @param touch_port Desktop and viewport geometry for pointer input. */ void update_mouse_viewport(const touch_port_t &touch_port); @@ -256,6 +256,16 @@ namespace platf::virtualhid { */ void move_mouse(input_context_t &context, int delta_x, int delta_y); + /** + * @brief Retarget the virtual mouse and move it relatively. + * + * @param context Input context. + * @param touch_port Desktop and streamed-display bounds for the pointer. + * @param delta_x Horizontal delta. + * @param delta_y Vertical delta. + */ + void move_mouse(input_context_t &context, const touch_port_t &touch_port, int delta_x, int delta_y); + /** * @brief Move the virtual mouse absolutely inside a target touch port. * diff --git a/tests/unit/platform/test_virtualhid_input.cpp b/tests/unit/platform/test_virtualhid_input.cpp index ce2f91924..d143a7e92 100644 --- a/tests/unit/platform/test_virtualhid_input.cpp +++ b/tests/unit/platform/test_virtualhid_input.cpp @@ -807,6 +807,7 @@ TEST_F(VirtualHidDeviceTest, TranslatesMouseAndKeyboardInput) { context()->mouse.reset(); context()->keyboard.reset(); platf::virtualhid::move_mouse(*context(), 1, 1); + platf::virtualhid::move_mouse(*context(), viewport, 1, 1); platf::virtualhid::abs_mouse(*context(), viewport, 1.0F, 1.0F); platf::virtualhid::button_mouse(*context(), BUTTON_LEFT, false); platf::virtualhid::scroll(*context(), 1); @@ -849,6 +850,40 @@ TEST_F(VirtualHidDeviceTest, RetargetsAbsoluteMouseWhenStreamedViewportChanges) EXPECT_FLOAT_EQ(mouse_event.absolute_y, 540.0F); } +TEST_F(VirtualHidDeviceTest, RetargetsRelativeMouseWhenStreamedViewportChanges) { + const platf::touch_port_t left_viewport { + .offset_x = -1920, + .offset_y = 0, + .width = 3840, + .height = 1080, + .logical_width = 1920, + .logical_height = 1080, + .env_offset_x = -1920, + .env_offset_y = 0, + }; + + const auto initial_mouse_id = context()->mouse->device_id(); + platf::virtualhid::move_mouse(*context(), left_viewport, 12, -7); + const auto left_mouse_id = context()->mouse->device_id(); + EXPECT_NE(left_mouse_id, initial_mouse_id); + auto mouse_event = context()->mouse->last_submitted_event(); + EXPECT_EQ(mouse_event.kind, lvh::MouseEventKind::relative_motion); + EXPECT_EQ(mouse_event.x, 12); + EXPECT_EQ(mouse_event.y, -7); + + platf::virtualhid::move_mouse(*context(), left_viewport, 3, 4); + EXPECT_EQ(context()->mouse->device_id(), left_mouse_id); + + auto right_viewport = left_viewport; + right_viewport.offset_x = 0; + platf::virtualhid::move_mouse(*context(), right_viewport, -5, 9); + EXPECT_NE(context()->mouse->device_id(), left_mouse_id); + mouse_event = context()->mouse->last_submitted_event(); + EXPECT_EQ(mouse_event.kind, lvh::MouseEventKind::relative_motion); + EXPECT_EQ(mouse_event.x, -5); + EXPECT_EQ(mouse_event.y, 9); +} + TEST_F(VirtualHidDeviceTest, TranslatesTouchscreenLifecycleAndGeometry) { const platf::touch_port_t viewport {10, 20, 800, 600, 400, 300}; platf::touch_input_t touch {LI_TOUCH_EVENT_DOWN, 100, 7, -0.1F, 1.1F, 1.2F, 15.0F, 10.0F}; diff --git a/tests/unit/test_input.cpp b/tests/unit/test_input.cpp index 10c5461ac..724d6dce6 100644 --- a/tests/unit/test_input.cpp +++ b/tests/unit/test_input.cpp @@ -70,6 +70,7 @@ namespace { void SetUp() override { original_input_ = config::input; config::input.controller = true; + config::input.mouse = true; config::input.gamepad = "xseries"; auto platform_input = platf::input(); @@ -408,3 +409,54 @@ TEST_F(InputGamepadSessionTest, RefreshesSharedVirtualInputAfterLicenseStateChan EXPECT_NE(context().mouse->device_id(), original_mouse_id); EXPECT_EQ(runtime().active_device_count(), active_devices); } + +TEST_F(InputGamepadSessionTest, RetargetsRelativeMouseFromLatestStreamGeometry) { + auto session_mail = std::make_shared(); + auto stream_input = input::alloc(session_mail, "relative-mouse-client"); + ASSERT_NE(stream_input, nullptr); + + input::touch_port_t left_viewport { + { + .offset_x = -1920, + .offset_y = 0, + .width = 1920, + .height = 1080, + .logical_width = 1920, + .logical_height = 1080, + .env_offset_x = -1920, + .env_offset_y = 0, + }, + 3840, + 1080, + 0.0F, + 0.0F, + 1.0F, + 1.0F, + 3840, + 1080, + }; + + const auto initial_mouse_id = context().mouse->device_id(); + session_mail->event(mail::touch_port)->raise(left_viewport); + input::testing::send_relative_mouse_packet(stream_input, 12, -7); + const auto left_mouse_id = context().mouse->device_id(); + EXPECT_NE(left_mouse_id, initial_mouse_id); + auto mouse_event = context().mouse->last_submitted_event(); + EXPECT_EQ(mouse_event.kind, lvh::MouseEventKind::relative_motion); + EXPECT_EQ(mouse_event.x, 12); + EXPECT_EQ(mouse_event.y, -7); + EXPECT_EQ(context().mouse_desktop.offset_x, -1920); + EXPECT_EQ(context().mouse_desktop.width, 3840); + EXPECT_EQ(context().mouse_viewport.offset_x, -1920); + EXPECT_EQ(context().mouse_viewport.width, 1920); + + left_viewport.offset_x = 0; + session_mail->event(mail::touch_port)->raise(left_viewport); + input::testing::send_relative_mouse_packet(stream_input, -5, 9); + EXPECT_NE(context().mouse->device_id(), left_mouse_id); + mouse_event = context().mouse->last_submitted_event(); + EXPECT_EQ(mouse_event.kind, lvh::MouseEventKind::relative_motion); + EXPECT_EQ(mouse_event.x, -5); + EXPECT_EQ(mouse_event.y, 9); + EXPECT_EQ(context().mouse_viewport.offset_x, 0); +}