mirror of
https://github.com/LizardByte/Sunshine.git
synced 2026-11-29 00:30:45 +00:00
fix(input): target mouse input at the streamed display (#5739)
This commit is contained in:
1 parent
1732b8714c
commit
db1e08d5b7
11 files changed
+422
-43
No files matched your search
@@ -80,6 +80,72 @@ namespace platf::virtualhid {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Forward one libvirtualhid diagnostic to Sunshine's logger.
|
||||
*
|
||||
* @param level libvirtualhid diagnostic severity.
|
||||
* @param message Diagnostic message.
|
||||
*/
|
||||
void log_virtualhid_message(lvh::LogLevel level, const std::string &message) {
|
||||
switch (level) {
|
||||
case lvh::LogLevel::debug:
|
||||
BOOST_LOG(debug) << "[libvirtualhid] "sv << message;
|
||||
break;
|
||||
case lvh::LogLevel::info:
|
||||
BOOST_LOG(info) << "[libvirtualhid] "sv << message;
|
||||
break;
|
||||
case lvh::LogLevel::warning:
|
||||
BOOST_LOG(warning) << "[libvirtualhid] "sv << message;
|
||||
break;
|
||||
case lvh::LogLevel::error:
|
||||
BOOST_LOG(error) << "[libvirtualhid] "sv << message;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare two pointer viewports.
|
||||
*
|
||||
* @param left First viewport.
|
||||
* @param right Second viewport.
|
||||
* @return `true` when every bound matches.
|
||||
*/
|
||||
bool pointer_viewports_equal(const lvh::PointerViewport &left, const lvh::PointerViewport &right) {
|
||||
return left.offset_x == right.offset_x &&
|
||||
left.offset_y == right.offset_y &&
|
||||
left.width == right.width &&
|
||||
left.height == right.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert Sunshine absolute-input geometry to libvirtualhid bounds.
|
||||
*
|
||||
* @param touch_port Sunshine desktop and display geometry.
|
||||
* @return Full virtual-desktop bounds followed by the streamed viewport.
|
||||
*/
|
||||
std::pair<lvh::PointerViewport, lvh::PointerViewport> pointer_viewports(const touch_port_t &touch_port) {
|
||||
const lvh::PointerViewport desktop {
|
||||
.offset_x = touch_port.env_offset_x,
|
||||
.offset_y = touch_port.env_offset_y,
|
||||
.width = touch_port.width,
|
||||
.height = touch_port.height,
|
||||
};
|
||||
|
||||
if (touch_port.logical_width <= 0 || touch_port.logical_height <= 0) {
|
||||
return {desktop, desktop};
|
||||
}
|
||||
|
||||
return {
|
||||
desktop,
|
||||
{
|
||||
.offset_x = touch_port.offset_x,
|
||||
.offset_y = touch_port.offset_y,
|
||||
.width = touch_port.logical_width,
|
||||
.height = touch_port.logical_height,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
float normalize_axis(std::int16_t value) {
|
||||
if (value < 0) {
|
||||
return std::max(-1.0F, static_cast<float>(value) / 32768.0F);
|
||||
@@ -476,7 +542,13 @@ namespace platf::virtualhid {
|
||||
}
|
||||
|
||||
void input_context_t::refresh_mouse() {
|
||||
refresh_mouse({}, {});
|
||||
}
|
||||
|
||||
void input_context_t::refresh_mouse(const lvh::PointerViewport &desktop, const lvh::PointerViewport &viewport) {
|
||||
mouse.reset();
|
||||
mouse_desktop = desktop;
|
||||
mouse_viewport = viewport;
|
||||
if (!runtime || !runtime->capabilities().supports_mouse) {
|
||||
return;
|
||||
}
|
||||
@@ -484,6 +556,8 @@ namespace platf::virtualhid {
|
||||
lvh::CreateMouseOptions options;
|
||||
options.profile = lvh::profiles::mouse();
|
||||
options.stable_id = "sunshine-mouse";
|
||||
options.desktop = desktop;
|
||||
options.viewport = viewport;
|
||||
auto created = runtime->create_mouse(options);
|
||||
if (created) {
|
||||
mouse = std::move(created.mouse);
|
||||
@@ -492,6 +566,15 @@ namespace platf::virtualhid {
|
||||
}
|
||||
}
|
||||
|
||||
void input_context_t::update_mouse_viewport(const touch_port_t &touch_port) {
|
||||
const auto [desktop, viewport] = pointer_viewports(touch_port);
|
||||
if (mouse && pointer_viewports_equal(mouse_desktop, desktop) && pointer_viewports_equal(mouse_viewport, viewport)) {
|
||||
return;
|
||||
}
|
||||
|
||||
refresh_mouse(desktop, viewport);
|
||||
}
|
||||
|
||||
client_context_t::client_context_t(input_context_t &input):
|
||||
global {&input} {
|
||||
if (!global->runtime) {
|
||||
@@ -526,6 +609,7 @@ namespace platf::virtualhid {
|
||||
std::unique_ptr<lvh::Runtime> create_runtime(lvh::BackendKind backend) {
|
||||
lvh::RuntimeOptions options;
|
||||
options.backend = backend;
|
||||
options.log_callback = log_virtualhid_message;
|
||||
return lvh::Runtime::create(options);
|
||||
}
|
||||
|
||||
@@ -801,18 +885,34 @@ namespace platf::virtualhid {
|
||||
}
|
||||
}
|
||||
|
||||
void abs_mouse(input_context_t &context, const touch_port_t &touch_port, float x, float y) {
|
||||
if (context.mouse) {
|
||||
log_failure(
|
||||
"submit libvirtualhid absolute mouse movement"sv,
|
||||
context.mouse->move_absolute(
|
||||
static_cast<std::int32_t>(std::lround(x)),
|
||||
static_cast<std::int32_t>(std::lround(y)),
|
||||
touch_port.width,
|
||||
touch_port.height
|
||||
)
|
||||
);
|
||||
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;
|
||||
}
|
||||
|
||||
context.update_mouse_viewport(touch_port);
|
||||
if (!context.mouse) {
|
||||
return;
|
||||
}
|
||||
|
||||
log_failure(
|
||||
"submit libvirtualhid absolute mouse movement"sv,
|
||||
context.mouse->move_absolute(
|
||||
x - static_cast<float>(context.mouse_viewport.offset_x),
|
||||
y - static_cast<float>(context.mouse_viewport.offset_y),
|
||||
context.mouse_viewport.width,
|
||||
context.mouse_viewport.height
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void button_mouse(input_context_t &context, int button, bool release) {
|
||||
@@ -1050,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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user