fix(linux): restore seat suffix on libvirtualhid device names (#6109)

This commit is contained in:
Christoph Brand authored and GitHub committed 2026-11-15 21:49:43 -05:00
1 parent bb6930bd78
commit 7607b1f4a6
4 files changed
+136 -3

No files matched your search

+8 -2
View File
@@ -243,11 +243,17 @@ If needed, you can override it manually in your systemd service file or shell en
When the seat is not `seat0`, Sunshine appends the seat name to its virtual device names, for example:
- Keyboard passthrough (seat1)
- Sunshine (libvirtualhid) PS5 Controller (seat1)
- libvirtualhid Keyboard (seat1)
- libvirtualhid Mouse (seat1)
- libvirtualhid Mouse (seat1) (Absolute)
- Sunshine (libvirtualhid) X-Box 360 Controller (seat1)
Sunshine creates two mouse devices: a relative one and an absolute one.
> [!NOTE]
> PlayStation gamepads always use Sony's native product name (`Wireless Controller`) so that Steam recognizes them,
> so they do not carry a seat suffix.
To assign Sunshine's virtual devices to the correct seat, create this udev rules file
(/etc/udev/rules.d/72-sunshine-virtual-seat.rules):
```udev
+29
View File
@@ -18,6 +18,9 @@
#include <string_view>
#include <utility>
// lib includes
#include <lizardbyte/common/env.h>
// local includes
#include "src/config.h"
#include "src/logging.h"
@@ -103,6 +106,15 @@ namespace platf::virtualhid {
}
}
/**
* @brief Scope a libvirtualhid profile name to the target seat.
*
* @param profile Profile whose advertised device name is updated.
*/
void apply_target_seat(lvh::DeviceProfile &profile) {
profile.name = device_name_for_seat(profile.name, target_seat());
}
/**
* @brief Compare two pointer viewports.
*
@@ -533,6 +545,7 @@ namespace platf::virtualhid {
lvh::CreateKeyboardOptions options;
options.profile = lvh::profiles::keyboard();
apply_target_seat(options.profile);
options.stable_id = "sunshine-keyboard";
auto created = runtime->create_keyboard(options);
if (created) {
@@ -556,6 +569,7 @@ namespace platf::virtualhid {
lvh::CreateMouseOptions options;
options.profile = lvh::profiles::mouse();
apply_target_seat(options.profile);
options.stable_id = "sunshine-mouse";
options.desktop = desktop;
options.viewport = viewport;
@@ -586,6 +600,7 @@ namespace platf::virtualhid {
if (capabilities.supports_touchscreen) {
lvh::CreateTouchscreenOptions options;
options.profile = lvh::profiles::touchscreen();
apply_target_seat(options.profile);
options.stable_id = "sunshine-touchscreen";
auto created = global->runtime->create_touchscreen(options);
if (created) {
@@ -597,6 +612,7 @@ namespace platf::virtualhid {
if (capabilities.supports_pen_tablet) {
lvh::CreatePenTabletOptions options;
options.profile = lvh::profiles::pen_tablet();
apply_target_seat(options.profile);
options.stable_id = "sunshine-pen-tablet";
auto created = global->runtime->create_pen_tablet(options);
if (created) {
@@ -701,6 +717,7 @@ namespace platf::virtualhid {
const auto &selection = profile_for_metadata(metadata);
auto profile = selection.profile();
profile.name = std::format("Sunshine {}", profile.name);
apply_target_seat(profile);
if (config::input.gamepad != "auto"sv) {
BOOST_LOG(info) << "Gamepad "sv << id.globalIndex << " will be "sv << profile.name << " (manual selection)"sv;
} else {
@@ -1095,6 +1112,18 @@ namespace platf::virtualhid {
return support.supports_touchpad || support.supports_motion;
}
std::string target_seat() {
return lizardbyte::common::get_env("XDG_SEAT");
}
std::string device_name_for_seat(std::string_view base_name, std::string_view seat) {
if (seat.empty() || seat == "seat0"sv) {
return std::string {base_name};
}
return std::format("{} ({})", base_name, seat);
}
} // namespace platf::virtualhid
namespace platf {
+16
View File
@@ -356,4 +356,20 @@ namespace platf::virtualhid {
*/
bool configured_gamepad_supports_controller_extensions();
/**
* @brief Determine the logind seat targeted by this Sunshine instance.
*
* @return Value of `XDG_SEAT`, or an empty string when it is unset.
*/
std::string target_seat();
/**
* @brief Append a seat suffix to a virtual device name for multi-seat udev rules.
*
* @param base_name Device name advertised by the libvirtualhid profile.
* @param seat Target logind seat.
* @return `base_name` unchanged for an empty seat or `seat0`, otherwise `"<base_name> (<seat>)"`.
*/
std::string device_name_for_seat(std::string_view base_name, std::string_view seat);
} // namespace platf::virtualhid
+83 -1
View File
@@ -13,13 +13,16 @@
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
// lib includes
#include <lizardbyte/common/env.h>
#if defined(_WIN32) || defined(__APPLE__)
// lib includes
#include <libvirtualhid/license.hpp>
#endif
@@ -216,6 +219,85 @@ namespace {
} // namespace
TEST(VirtualHidSeatTest, LeavesDeviceNameUnchangedForDefaultSeat) {
EXPECT_EQ(platf::virtualhid::device_name_for_seat("libvirtualhid Keyboard"sv, ""sv), "libvirtualhid Keyboard");
EXPECT_EQ(platf::virtualhid::device_name_for_seat("libvirtualhid Keyboard"sv, "seat0"sv), "libvirtualhid Keyboard");
}
TEST(VirtualHidSeatTest, AppendsNonDefaultSeatToDeviceName) {
EXPECT_EQ(platf::virtualhid::device_name_for_seat("libvirtualhid Mouse"sv, "seat1"sv), "libvirtualhid Mouse (seat1)");
EXPECT_EQ(platf::virtualhid::device_name_for_seat("Sunshine DualSense"sv, "seat_vr"sv), "Sunshine DualSense (seat_vr)");
}
namespace {
/**
* @brief Fixture that creates fake virtual devices while `XDG_SEAT` targets a secondary seat.
*/
class VirtualHidSeatDeviceTest: public ::testing::Test {
protected:
/**
* @brief Point `XDG_SEAT` at `seat1` before creating fake devices.
*/
void SetUp() override {
if (std::string value; lizardbyte::common::get_env("XDG_SEAT", value)) {
original_seat_ = std::move(value);
}
original_input_ = config::input;
ASSERT_EQ(lizardbyte::common::set_env("XDG_SEAT", "seat1"), 0);
context_ = std::make_unique<platf::virtualhid::input_context_t>(lvh::BackendKind::fake);
client_ = std::make_unique<platf::virtualhid::client_context_t>(*context_);
feedback_queue_ = mail::man->queue<platf::gamepad_feedback_msg_t>("virtualhid-input-test-seat");
}
/**
* @brief Release fake devices and restore the original seat and input configuration.
*/
void TearDown() override {
client_.reset();
context_.reset();
feedback_queue_.reset();
config::input = std::move(original_input_);
if (original_seat_) {
lizardbyte::common::set_env("XDG_SEAT", *original_seat_);
} else {
lizardbyte::common::unset_env("XDG_SEAT");
}
}
std::unique_ptr<platf::virtualhid::input_context_t> context_; ///< Shared fake virtual devices.
std::unique_ptr<platf::virtualhid::client_context_t> client_; ///< Per-client fake touch and pen devices.
platf::feedback_queue_t feedback_queue_; ///< Captured gamepad feedback messages.
private:
std::optional<std::string> original_seat_; ///< `XDG_SEAT` value restored after each test.
config::input_t original_input_; ///< Input configuration restored after each test.
};
} // namespace
TEST_F(VirtualHidSeatDeviceTest, ScopesEveryDeviceNameToTargetSeat) {
ASSERT_NE(context_->keyboard, nullptr);
ASSERT_NE(context_->mouse, nullptr);
ASSERT_NE(client_->touch, nullptr);
ASSERT_NE(client_->pen, nullptr);
EXPECT_EQ(context_->keyboard->profile().name, lvh::profiles::keyboard().name + " (seat1)");
EXPECT_EQ(context_->mouse->profile().name, lvh::profiles::mouse().name + " (seat1)");
EXPECT_EQ(client_->touch->profile().name, lvh::profiles::touchscreen().name + " (seat1)");
EXPECT_EQ(client_->pen->profile().name, lvh::profiles::pen_tablet().name + " (seat1)");
config::input.gamepad = "x360";
const platf::gamepad_id_t id {0, 0};
const platf::gamepad_arrival_t metadata {LI_CTYPE_XBOX, 0, 0};
ASSERT_EQ(platf::virtualhid::alloc_gamepad(*context_, id, metadata, feedback_queue_), 0);
auto *adapter = platf::virtualhid::gamepad_adapter_for_testing(*context_, 0);
ASSERT_NE(adapter, nullptr);
ASSERT_NE(adapter->gamepad(), nullptr);
EXPECT_TRUE(adapter->gamepad()->profile().name.starts_with("Sunshine "));
EXPECT_TRUE(adapter->gamepad()->profile().name.ends_with(" (seat1)"));
platf::virtualhid::free_gamepad(*context_, 0);
}
TEST_F(VirtualHidDeviceTest, CreatesEveryDeviceWithFakeRuntime) {
EXPECT_EQ(context()->runtime->backend_kind(), lvh::BackendKind::fake);
EXPECT_EQ(context()->runtime->capabilities().backend_name, "fake");