diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 873e34399..945ba343a 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -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 diff --git a/src/platform/virtualhid_input.cpp b/src/platform/virtualhid_input.cpp index 5eeb4647f..854c7961b 100644 --- a/src/platform/virtualhid_input.cpp +++ b/src/platform/virtualhid_input.cpp @@ -18,6 +18,9 @@ #include #include +// lib includes +#include + // 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 { diff --git a/src/platform/virtualhid_input.h b/src/platform/virtualhid_input.h index 47431963b..b0e0658da 100644 --- a/src/platform/virtualhid_input.h +++ b/src/platform/virtualhid_input.h @@ -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 `" ()"`. + */ + std::string device_name_for_seat(std::string_view base_name, std::string_view seat); + } // namespace platf::virtualhid diff --git a/tests/unit/platform/test_virtualhid_input.cpp b/tests/unit/platform/test_virtualhid_input.cpp index b1ec94356..7b0656cb6 100644 --- a/tests/unit/platform/test_virtualhid_input.cpp +++ b/tests/unit/platform/test_virtualhid_input.cpp @@ -13,13 +13,16 @@ #include #include #include +#include #include #include #include #include +// lib includes +#include + #if defined(_WIN32) || defined(__APPLE__) - // lib includes #include #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(lvh::BackendKind::fake); + client_ = std::make_unique(*context_); + feedback_queue_ = mail::man->queue("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 context_; ///< Shared fake virtual devices. + std::unique_ptr client_; ///< Per-client fake touch and pen devices. + platf::feedback_queue_t feedback_queue_; ///< Captured gamepad feedback messages. + + private: + std::optional 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");