Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,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<float, float> rotate_to_output(const std::pair<float, float> &point, const platf::touch_port_t &output) {
const auto [x, y] = point;
const auto width = static_cast<float>(output.logical_width);
const auto height = static_cast<float>(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.
*
Expand Down Expand Up @@ -920,7 +945,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);
}

/**
Expand Down Expand Up @@ -2575,6 +2601,18 @@ namespace input {
::input::passthrough(input, &packet);
}

void send_absolute_mouse_packet(std::shared_ptr<input_t> &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<std::uint32_t>(sizeof(packet) - sizeof(packet.header.size));
packet.header.magic = util::endian::little<std::uint32_t>(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;
Expand Down
11 changes: 11 additions & 0 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ namespace input {
*/
void send_relative_mouse_packet(std::shared_ptr<input_t> &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_t> &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.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/platform/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ namespace platf {
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.
std::string output_name {}; ///< Native connector name used to assign compositor-managed touch and pen devices.
int transform {}; ///< Wayland output transform (rotation) of the display.
};

// These values must match Limelight-internal.h's SS_FF_* constants!
Expand Down Expand Up @@ -858,6 +859,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)
Expand Down
38 changes: 34 additions & 4 deletions src/platform/linux/wayland.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -675,11 +692,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) { // NOSONAR(cpp:S107): libwayland's wl_output geometry callback signature.
viewport.transform = transform;
}

/**
Expand Down Expand Up @@ -718,6 +737,17 @@ namespace wl {
zxdg_output_v1_listener xdg_listener; ///< Callback table for xdg-output events.
};

/**
* @brief Copy a monitor's geometry onto the display that captures it.
*
* Capture initialization uses this, so a reinitialization picks up the monitor's current transform for absolute
* input.
*
* @param display Capture display to update.
* @param monitor Monitor selected for capture.
*/
void apply_monitor_geometry(platf::display_t &display, const monitor_t &monitor);

/**
* @brief Wayland registry state for screencopy, DMA-BUF, and output globals.
*/
Expand Down
29 changes: 19 additions & 10 deletions src/platform/linux/wlgrab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ 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;
}

void apply_monitor_geometry(platf::display_t &display, const monitor_t &monitor) {
display.offset_x = monitor.viewport.offset_x;
display.offset_y = monitor.viewport.offset_y;
display.width = monitor.viewport.width;
display.height = monitor.viewport.height;
display.output_name = monitor.name;
display.logical_width = monitor.viewport.logical_width;
display.logical_height = monitor.viewport.logical_height;
display.transform = monitor.viewport.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,
Expand Down Expand Up @@ -416,14 +431,8 @@ namespace wl {
event_driven = use_damage_capture(interface.screencopy_version, stream_fps, monitor->refresh_mhz);
BOOST_LOG(info) << "[wlgrab] Frame capture: "sv << (event_driven ? "event-driven"sv : "paced"sv) << " (output "sv << output_fps << " Hz, stream "sv << stream_fps << " fps)"sv;

offset_x = monitor->viewport.offset_x;
offset_y = monitor->viewport.offset_y;
width = monitor->viewport.width;
height = monitor->viewport.height;

this->output_name = monitor->name;
this->logical_width = monitor->viewport.logical_width;
this->logical_height = monitor->viewport.logical_height;
apply_monitor_geometry(*this, *monitor);
this->monitor = monitor;

platf::desktop_geometry_t desktop;
for (const auto &monitor_entry : interface.monitors) {
Expand Down Expand Up @@ -493,8 +502,7 @@ namespace wl {

if (
dmabuf.status == dmabuf_t::REINIT ||
current_frame->sd.width != width ||
current_frame->sd.height != height
capture_needs_reinit(current_frame->sd.width, current_frame->sd.height, width, height, monitor->viewport.transform, transform)
) {
return platf::capture_e::reinit;
}
Expand Down Expand Up @@ -597,6 +605,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.
};

/**
Expand Down
5 changes: 2 additions & 3 deletions src/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2643,9 +2643,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;
}
Expand All @@ -2667,6 +2665,7 @@ namespace video {
display->env_offset_x,
display->env_offset_y,
display->output_name,
display->transform,
},
display->env_width,
display->env_height,
Expand Down
19 changes: 19 additions & 0 deletions src/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,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.
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/platform/linux/test_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -154,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));
Expand Down
Loading