From c09edb0dfddf247c4703235cf9a59e58753c2728 Mon Sep 17 00:00:00 2001 From: ahmedkall Date: Fri, 30 Oct 2026 15:35:15 +0100 Subject: [PATCH 1/3] fix(linux/wlr): map absolute mouse input on rotated outputs A rotated output is captured in its native orientation, so absolute mouse input (touch from Moonlight Android) needs the output transform: store it from wl_output, scale the touch port by the rotated logical size, and turn the point into the output's orientation. Capture reinitializes when the transform changes so the touch port follows a rotation mid-stream. Co-Authored-By: Claude Opus 5.5 --- src/input.cpp | 40 ++++++++++++++++- src/input.h | 11 +++++ src/platform/common.h | 2 + src/platform/linux/wayland.h | 10 +++-- src/platform/linux/wlgrab.cpp | 6 ++- src/video.cpp | 5 +-- src/video.h | 19 ++++++++ tests/unit/platform/linux/test_wayland.cpp | 1 + tests/unit/test_input.cpp | 51 ++++++++++++++++++++++ tests/unit/test_video.cpp | 8 ++++ 10 files changed, 144 insertions(+), 9 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 1facd6afd23..f84c9fce690 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -742,6 +742,31 @@ namespace input { } } + /** + * @brief Turn a point on a rotated output into the output's own orientation. + * @details A rotated output is captured in its native orientation, so a point read off the stream is in that + * orientation. Absolute input targets the output's logical (rotated) coordinates. + * + * @param point Point relative to the output's top-left corner, in the output's native orientation. + * @param output Output geometry; `logical_width` and `logical_height` are the rotated logical size. + * @return The point relative to the output's top-left corner, in its logical orientation. + */ + std::pair rotate_to_output(const std::pair &point, const platf::touch_port_t &output) { + const auto [x, y] = point; + const auto width = static_cast(output.logical_width); + const auto height = static_cast(output.logical_height); + switch (output.transform) { + case 1: // 90 + return {width - y, x}; + case 2: // 180 + return {width - x, height - y}; + case 3: // 270 + return {y, height - x}; + default: + return point; + } + } + /** * @brief Convert stream touch geometry to platform pointer bounds. * @@ -908,7 +933,8 @@ namespace input { auto &touch_port = input->touch_port; - platf::abs_mouse(platf_input, platform_touch_port(touch_port), tpcoords->first, tpcoords->second); + const auto [x_out, y_out] = rotate_to_output({tpcoords->first - touch_port.offset_x, tpcoords->second - touch_port.offset_y}, touch_port); + platf::abs_mouse(platf_input, platform_touch_port(touch_port), x_out + touch_port.offset_x, y_out + touch_port.offset_y); } /** @@ -2523,6 +2549,18 @@ namespace input { ::input::passthrough(input, &packet); } + void send_absolute_mouse_packet(std::shared_ptr &input, std::int16_t x, std::int16_t y, std::int16_t width, std::int16_t height) { + NV_ABS_MOUSE_MOVE_PACKET packet {}; + packet.header.size = util::endian::big(sizeof(packet) - sizeof(packet.header.size)); + packet.header.magic = util::endian::little(MOUSE_MOVE_ABS_MAGIC); + packet.x = util::endian::big(x); + packet.y = util::endian::big(y); + packet.width = util::endian::big(width); + packet.height = util::endian::big(height); + + ::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 f181f633611..bbe26fd0c68 100644 --- a/src/input.h +++ b/src/input.h @@ -173,6 +173,17 @@ namespace input { */ void send_relative_mouse_packet(std::shared_ptr &input, std::int16_t delta_x, std::int16_t delta_y); + /** + * @brief Process one client absolute-mouse packet on the calling thread. + * + * @param input Retained input state. + * @param x Horizontal position on the client surface. + * @param y Vertical position on the client surface. + * @param width Width of the client surface. + * @param height Height of the client surface. + */ + void send_absolute_mouse_packet(std::shared_ptr &input, std::int16_t x, std::int16_t y, std::int16_t width, std::int16_t height); + /** * @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 62961d7a310..3a2bdd9ce6a 100644 --- a/src/platform/common.h +++ b/src/platform/common.h @@ -395,6 +395,7 @@ namespace platf { int logical_height; ///< Logical height after display scaling. int env_offset_x {}; ///< Horizontal origin of the full input environment in native desktop coordinates. int env_offset_y {}; ///< Vertical origin of the full input environment in native desktop coordinates. + int transform {}; ///< Wayland output transform (rotation) of the display. }; // These values must match Limelight-internal.h's SS_FF_* constants! @@ -814,6 +815,7 @@ namespace platf { int height {0}; ///< Height of the captured display in physical pixels. int logical_width {0}; ///< Width of the captured display after display scaling. int logical_height {0}; ///< Height of the captured display after display scaling. + int transform {0}; ///< Wayland output transform (rotation) of the captured display. protected: // collect capture timing data (at loglevel debug) diff --git a/src/platform/linux/wayland.h b/src/platform/linux/wayland.h index 616d51ec891..a9d5b35fe95 100644 --- a/src/platform/linux/wayland.h +++ b/src/platform/linux/wayland.h @@ -629,11 +629,13 @@ namespace wl { } /** - * @brief Ignore physical wl-output geometry; monitor placement comes from xdg-output metadata. - * @details Retain the wl-output listener signature while ignoring all event arguments. + * @brief Record the wl-output transform; monitor placement comes from xdg-output metadata. + * @details Physical geometry is ignored. A rotated output is captured in its native orientation, so absolute + * input needs the transform to map points onto it. + * @param transform Wayland output transform value. */ - void wl_geometry(wl_output *, std::int32_t, std::int32_t, std::int32_t, std::int32_t, std::int32_t, const char *, const char *, std::int32_t) { - // Logical placement comes from xdg-output, so physical geometry has nothing to update. + void wl_geometry(wl_output *, std::int32_t, std::int32_t, std::int32_t, std::int32_t, std::int32_t, const char *, const char *, std::int32_t transform) { + viewport.transform = transform; } /** diff --git a/src/platform/linux/wlgrab.cpp b/src/platform/linux/wlgrab.cpp index 5bd32032128..e2e83695524 100644 --- a/src/platform/linux/wlgrab.cpp +++ b/src/platform/linux/wlgrab.cpp @@ -348,6 +348,8 @@ namespace wl { this->logical_width = monitor->viewport.logical_width; this->logical_height = monitor->viewport.logical_height; + this->transform = monitor->viewport.transform; + this->monitor = monitor; int desktop_logical_width = 0; int desktop_logical_height = 0; @@ -423,7 +425,8 @@ namespace wl { if ( dmabuf.status == dmabuf_t::REINIT || current_frame->sd.width != width || - current_frame->sd.height != height + current_frame->sd.height != height || + monitor->viewport.transform != transform // rotated: re-read the geometry for absolute input ) { return platf::capture_e::reinit; } @@ -526,6 +529,7 @@ namespace wl { dmabuf_t dmabuf; ///< DMA-BUF feedback and format state advertised by the compositor. wl_output *output; ///< Wayland output selected for capture. + wl::monitor_t *monitor {}; ///< Monitor selected for capture. }; /** diff --git a/src/video.cpp b/src/video.cpp index 13ece17213d..69887696af7 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -2536,9 +2536,7 @@ namespace video { int display_env_logical_width = 0; int display_env_logical_height = 0; if (display->logical_width > 0 && display->logical_height > 0 && display->env_logical_width > 0 && display->env_logical_height > 0) { - float lwd = display->logical_width; - float lhd = display->logical_height; - scalar_tpcoords = std::fminf(wd / lwd, hd / lhd); + scalar_tpcoords = touch_port_scale(wd, hd, display->logical_width, display->logical_height, display->transform); display_env_logical_width = display->env_logical_width; display_env_logical_height = display->env_logical_height; } @@ -2559,6 +2557,7 @@ namespace video { display->logical_height, display->env_offset_x, display->env_offset_y, + display->transform, }, display->env_width, display->env_height, diff --git a/src/video.h b/src/video.h index 2092fcacf0c..fbbec41495d 100644 --- a/src/video.h +++ b/src/video.h @@ -788,6 +788,25 @@ namespace video { return AVRational {config.framerate, 1}; } + /** + * @brief Scale from logical to physical pixels for the touch port. + * @details A rotated output (odd transform) is captured in its native orientation, so its logical size is compared + * with the physical size swapped. + * + * @param width Physical width of the captured display. + * @param height Physical height of the captured display. + * @param logical_width Logical width of the display, as rotated. + * @param logical_height Logical height of the display, as rotated. + * @param transform Wayland output transform of the display. + * @return Physical pixels per logical pixel. + */ + inline float touch_port_scale(float width, float height, float logical_width, float logical_height, int transform) { + if (transform % 2 == 1) { + std::swap(logical_width, logical_height); + } + return std::fminf(width / logical_width, height / logical_height); + } + /** * @brief Capture frame interval for the requested framerate. * Uses the exact fractional rate when the client provided an X100 value. diff --git a/tests/unit/platform/linux/test_wayland.cpp b/tests/unit/platform/linux/test_wayland.cpp index dc3371a59c1..1da2d873b41 100644 --- a/tests/unit/platform/linux/test_wayland.cpp +++ b/tests/unit/platform/linux/test_wayland.cpp @@ -139,6 +139,7 @@ TEST(WaylandMonitorTest, ListenerIgnoresPhysicalGeometryScaleAndDone) { EXPECT_EQ(monitor.viewport.width, 2560); EXPECT_EQ(monitor.viewport.height, 1440); EXPECT_EQ(monitor.refresh_mhz, 120000); + EXPECT_EQ(monitor.viewport.transform, WL_OUTPUT_TRANSFORM_90); EXPECT_EQ(monitor.name, "DP-1"); EXPECT_EQ(monitor.description, "Logical monitor"); } diff --git a/tests/unit/test_input.cpp b/tests/unit/test_input.cpp index 4913dd7b198..5411e0c2a60 100644 --- a/tests/unit/test_input.cpp +++ b/tests/unit/test_input.cpp @@ -642,3 +642,54 @@ TEST_F(InputGamepadSessionTest, RetargetsRelativeMouseFromLatestStreamGeometry) EXPECT_EQ(mouse_event.y, 9); EXPECT_EQ(context().mouse_viewport.offset_x, 0); } + +TEST_F(InputGamepadSessionTest, MapsAbsoluteMouseOntoRotatedOutputs) { + auto session_mail = std::make_shared(); + auto stream_input = input::alloc(session_mail, "absolute-mouse-client"); + ASSERT_NE(stream_input, nullptr); + + // A 3000x1920 output at scale 2, streamed at its native size. Rotated outputs are captured in their native + // orientation, so the stream stays 3000x1920 while the logical size turns to 960x1500. + struct rotation_case_t { + int transform; // wl_output transform: rotated by 0, 90, 180 or 270 degrees + int logical_width; + int logical_height; + float expected_x; + float expected_y; + }; + + for (const auto &[transform, logical_width, logical_height, expected_x, expected_y] : { + rotation_case_t {0, 1500, 960, 150.0F, 100.0F}, + rotation_case_t {1, 960, 1500, 860.0F, 150.0F}, + rotation_case_t {2, 1500, 960, 1350.0F, 860.0F}, + rotation_case_t {3, 960, 1500, 100.0F, 1350.0F}, + }) { + input::touch_port_t viewport { + { + .offset_x = 0, + .offset_y = 0, + .width = 3000, + .height = 1920, + .logical_width = logical_width, + .logical_height = logical_height, + .transform = transform, + }, + 3000, + 1920, + 0.0F, + 0.0F, + 1.0F, + 2.0F, + logical_width, + logical_height, + }; + session_mail->event(mail::touch_port)->raise(viewport); + + input::testing::send_absolute_mouse_packet(stream_input, 300, 200, 3000, 1920); + + const auto mouse_event = context().mouse->last_submitted_event(); + EXPECT_EQ(mouse_event.kind, lvh::MouseEventKind::absolute_motion) << "transform " << transform; + EXPECT_FLOAT_EQ(mouse_event.absolute_x, expected_x) << "transform " << transform; + EXPECT_FLOAT_EQ(mouse_event.absolute_y, expected_y) << "transform " << transform; + } +} diff --git a/tests/unit/test_video.cpp b/tests/unit/test_video.cpp index 01046dd8857..42e5584776c 100644 --- a/tests/unit/test_video.cpp +++ b/tests/unit/test_video.cpp @@ -30,6 +30,14 @@ using namespace std::literals; /** * @brief Packet replacement rules retain the original non-owning byte views. */ +TEST(VideoTouchPortTest, ScalesRotatedOutputsByTheirRotatedLogicalSize) { + // 3000x1920 native pixels at scale 2: 1500x960 logical, or 960x1500 when rotated by 90 or 270 degrees. + EXPECT_FLOAT_EQ(video::touch_port_scale(3000.0F, 1920.0F, 1500.0F, 960.0F, 0), 2.0F); + EXPECT_FLOAT_EQ(video::touch_port_scale(3000.0F, 1920.0F, 960.0F, 1500.0F, 1), 2.0F); + EXPECT_FLOAT_EQ(video::touch_port_scale(3000.0F, 1920.0F, 1500.0F, 960.0F, 2), 2.0F); + EXPECT_FLOAT_EQ(video::touch_port_scale(3000.0F, 1920.0F, 960.0F, 1500.0F, 3), 2.0F); +} + TEST(VideoPacketReplacementTest, CopiesViewsWithoutCopyingTheirBackingBytes) { std::string original = "__old__"; std::string replacement = "__new__"; From 119b25da0820eb73e2eb0b47c9b35220a823e888 Mon Sep 17 00:00:00 2001 From: ahmedkall Date: Fri, 30 Oct 2026 15:54:07 +0100 Subject: [PATCH 2/3] fix(linux/wlr): mark wl_geometry's parameter count as dictated by libwayland Co-Authored-By: Claude Opus 5.5 --- src/platform/linux/wayland.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/linux/wayland.h b/src/platform/linux/wayland.h index a9d5b35fe95..2394cfb9107 100644 --- a/src/platform/linux/wayland.h +++ b/src/platform/linux/wayland.h @@ -634,7 +634,7 @@ namespace wl { * input needs the transform to map points onto it. * @param transform Wayland output transform value. */ - void wl_geometry(wl_output *, std::int32_t, std::int32_t, std::int32_t, std::int32_t, std::int32_t, const char *, const char *, std::int32_t transform) { + void wl_geometry(wl_output *, std::int32_t, std::int32_t, std::int32_t, std::int32_t, std::int32_t, const char *, const char *, std::int32_t transform) { // NOSONAR(cpp:S107): libwayland's wl_output geometry callback signature. viewport.transform = transform; } From 6403dbf46caf3a285a083d30d087f4743147aa94 Mon Sep 17 00:00:00 2001 From: ahmedkall Date: Mon, 9 Nov 2026 22:48:33 +0100 Subject: [PATCH 3/3] test(linux/wlr): cover rotation-only capture reinit and touch port rebuild Move the reinit decision into wl::capture_needs_reinit() so it can be tested: a 90 to 270 degree turn keeps both sizes, so only the transform check catches it. Declare video::make_port() and test that a display rebuilt at transform 3 gives a port with that transform and scale 2, which maps a tap at (300,200) to (100,1350). Co-Authored-By: Claude Opus 5.5 --- src/platform/linux/wayland.h | 17 ++++++ src/platform/linux/wlgrab.cpp | 8 ++- src/video.h | 9 +++ tests/unit/platform/linux/test_wayland.cpp | 9 +++ tests/unit/test_input.cpp | 70 ++++++++++++++++++++++ 5 files changed, 110 insertions(+), 3 deletions(-) diff --git a/src/platform/linux/wayland.h b/src/platform/linux/wayland.h index 2394cfb9107..8faec986b08 100644 --- a/src/platform/linux/wayland.h +++ b/src/platform/linux/wayland.h @@ -188,6 +188,23 @@ namespace wl { */ bool use_damage_capture(std::uint32_t screencopy_version, double stream_fps, std::int32_t refresh_mhz); + /** + * @brief Decide whether a received frame means capture has to be reinitialized. + * + * The display's size and transform feed the touch port, so a change in either has to rebuild it. + * Turning an output between 90 and 270 degrees changes neither its native nor its logical size, + * so the transform is compared on its own. + * + * @param frame_width Width of the received frame. + * @param frame_height Height of the received frame. + * @param width Width the capture was initialized with. + * @param height Height the capture was initialized with. + * @param output_transform Current transform of the captured output. + * @param transform Transform the capture was initialized with. + * @return `true` when capture has to be reinitialized. + */ + bool capture_needs_reinit(int frame_width, int frame_height, int width, int height, int output_transform, int transform); + /** * @brief Move the earliest time of the next damage-driven request on by one captured frame. * diff --git a/src/platform/linux/wlgrab.cpp b/src/platform/linux/wlgrab.cpp index e2e83695524..a548653bbcc 100644 --- a/src/platform/linux/wlgrab.cpp +++ b/src/platform/linux/wlgrab.cpp @@ -186,6 +186,10 @@ namespace wl { return screencopy_version >= 2 && stream_fps > 0 && refresh_mhz / 1000.0 >= stream_fps * 1.5; } + bool capture_needs_reinit(int frame_width, int frame_height, int width, int height, int output_transform, int transform) { + return frame_width != width || frame_height != height || output_transform != transform; + } + std::chrono::steady_clock::time_point next_damage_request( std::chrono::steady_clock::time_point next_request, std::chrono::steady_clock::time_point frame_time, @@ -424,9 +428,7 @@ namespace wl { if ( dmabuf.status == dmabuf_t::REINIT || - current_frame->sd.width != width || - current_frame->sd.height != height || - monitor->viewport.transform != transform // rotated: re-read the geometry for absolute input + capture_needs_reinit(current_frame->sd.width, current_frame->sd.height, width, height, monitor->viewport.transform, transform) ) { return platf::capture_e::reinit; } diff --git a/src/video.h b/src/video.h index fbbec41495d..2d0edf22c34 100644 --- a/src/video.h +++ b/src/video.h @@ -807,6 +807,15 @@ namespace video { return std::fminf(width / logical_width, height / logical_height); } + /** + * @brief Build the touch port that maps client input onto a captured display. + * + * @param display Captured display, with its physical, logical and environment geometry. + * @param config Stream configuration carrying the client's resolution. + * @return Touch port for absolute input on that display. + */ + input::touch_port_t make_port(platf::display_t *display, const config_t &config); + /** * @brief Capture frame interval for the requested framerate. * Uses the exact fractional rate when the client provided an X100 value. diff --git a/tests/unit/platform/linux/test_wayland.cpp b/tests/unit/platform/linux/test_wayland.cpp index 1da2d873b41..55f73f389a1 100644 --- a/tests/unit/platform/linux/test_wayland.cpp +++ b/tests/unit/platform/linux/test_wayland.cpp @@ -155,6 +155,15 @@ TEST(WaylandCaptureTest, UsesDamageCaptureOnlyOnAFasterOutput) { EXPECT_FALSE(wl::use_damage_capture(3, 120.0, 120000)); } +TEST(WaylandCaptureTest, ReinitializesWhenTheOutputTurnsWithoutChangingSize) { + // Turning from 90 to 270 degrees keeps both the native 3000x1920 and the logical 960x1500 size. + EXPECT_TRUE(wl::capture_needs_reinit(3000, 1920, 3000, 1920, WL_OUTPUT_TRANSFORM_270, WL_OUTPUT_TRANSFORM_90)); + EXPECT_FALSE(wl::capture_needs_reinit(3000, 1920, 3000, 1920, WL_OUTPUT_TRANSFORM_90, WL_OUTPUT_TRANSFORM_90)); + + // A new frame size still reinitializes, whatever the transform. + EXPECT_TRUE(wl::capture_needs_reinit(1920, 1080, 3000, 1920, WL_OUTPUT_TRANSFORM_NORMAL, WL_OUTPUT_TRANSFORM_NORMAL)); +} + TEST(WaylandCaptureTest, KeepsPacedCaptureWithoutProtocolOrRates) { // copy_with_damage arrived in version 2 of the protocol. EXPECT_FALSE(wl::use_damage_capture(1, 60.0, 120000)); diff --git a/tests/unit/test_input.cpp b/tests/unit/test_input.cpp index 5411e0c2a60..12aa08bf9d4 100644 --- a/tests/unit/test_input.cpp +++ b/tests/unit/test_input.cpp @@ -29,6 +29,7 @@ extern "C" { #include "src/input.h" #include "src/platform/virtualhid_input.h" #include "src/utility.h" +#include "src/video.h" namespace { /** @@ -643,6 +644,75 @@ TEST_F(InputGamepadSessionTest, RetargetsRelativeMouseFromLatestStreamGeometry) EXPECT_EQ(context().mouse_viewport.offset_x, 0); } +namespace { + /** + * @brief Display that only carries geometry, for building a touch port. + */ + class geometry_display_t: public platf::display_t { + public: + /** + * @brief Report no frames; only the geometry is used. + * + * @return Always `capture_e::ok`. + */ + platf::capture_e capture(const push_captured_image_cb_t &, const pull_free_image_cb_t &, bool *) override { + return platf::capture_e::ok; + } + + /** + * @brief Allocate no image; only the geometry is used. + * + * @return Always `nullptr`. + */ + std::shared_ptr alloc_img() override { + return nullptr; + } + + /** + * @brief Leave the image untouched; only the geometry is used. + * + * @return Always 0. + */ + int dummy_img(platf::img_t *) override { + return 0; + } + }; +} // namespace + +TEST_F(InputGamepadSessionTest, RebuildsTheTouchPortForARotatedDisplay) { + // The display after turning from 90 to 270 degrees: same 3000x1920 native and 960x1500 logical size. + geometry_display_t display; + display.width = 3000; + display.height = 1920; + display.logical_width = 960; + display.logical_height = 1500; + display.env_width = 3000; + display.env_height = 1920; + display.env_logical_width = 960; + display.env_logical_height = 1500; + display.transform = 3; + + video::config_t config {}; + config.width = 3000; + config.height = 1920; + + const auto port = video::make_port(&display, config); + EXPECT_EQ(port.transform, 3); + EXPECT_FLOAT_EQ(port.scalar_tpcoords, 2.0F); + + auto session_mail = std::make_shared(); + auto stream_input = input::alloc(session_mail, "rotated-display-client"); + ASSERT_NE(stream_input, nullptr); + session_mail->event(mail::touch_port)->raise(port); + + input::testing::send_absolute_mouse_packet(stream_input, 300, 200, 3000, 1920); + + const auto mouse_event = context().mouse->last_submitted_event(); + EXPECT_EQ(mouse_event.kind, lvh::MouseEventKind::absolute_motion); + EXPECT_FLOAT_EQ(mouse_event.absolute_x, 100.0F); + EXPECT_FLOAT_EQ(mouse_event.absolute_y, 1350.0F); +} + TEST_F(InputGamepadSessionTest, MapsAbsoluteMouseOntoRotatedOutputs) { auto session_mail = std::make_shared(); auto stream_input = input::alloc(session_mail, "absolute-mouse-client");