mirror of
https://github.com/LizardByte/Sunshine.git
synced 2026-12-03 02:24:22 +00:00
fix(input): retain and rebind gamepads across resumes
Add session-scoped retained input state so virtual gamepads survive paused transport connections and are rebound on resume instead of being recreated. Introduce explicit gamepad termination APIs for global and per-session cleanup, and call them when streams, RTSP sessions, or launched processes are terminated. Add a platform-level `rebind_gamepad` path for both virtualhid and Windows ViGEm backends (with feedback routing synchronization), refactor shared allocation logic, and extend unit coverage for rebind behavior and retained gamepad lifecycle.
This commit is contained in:
13 files changed
+602
-65
No files matched your search
+300
-53
@@ -13,8 +13,12 @@ extern "C" {
|
||||
#include <bitset>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -188,9 +192,13 @@ namespace input {
|
||||
|
||||
~gamepad_t() {
|
||||
if (id >= 0) {
|
||||
task_pool.push([id = this->id]() {
|
||||
if (task_pool.running()) {
|
||||
task_pool.push([id = this->id]() {
|
||||
::input::free_gamepad(platf_input, id);
|
||||
});
|
||||
} else {
|
||||
::input::free_gamepad(platf_input, id);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,6 +274,117 @@ namespace input {
|
||||
int32_t accumulated_hscroll_delta; ///< Accumulated hscroll delta.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Hash string-like session identifiers without allocating temporary strings.
|
||||
*/
|
||||
struct transparent_string_hash_t {
|
||||
using is_transparent = void; ///< Enable heterogeneous unordered-map lookup.
|
||||
|
||||
/**
|
||||
* @brief Hash a string view.
|
||||
*
|
||||
* @param value Session identifier to hash.
|
||||
* @return Hash value for the supplied identifier.
|
||||
*/
|
||||
std::size_t operator()(const std::string_view value) const noexcept {
|
||||
return std::hash<std::string_view> {}(value);
|
||||
}
|
||||
};
|
||||
|
||||
using retained_input_map_t = std::unordered_map<
|
||||
std::string,
|
||||
std::shared_ptr<input_t>,
|
||||
transparent_string_hash_t,
|
||||
std::equal_to<>>; ///< Retained inputs keyed by paired-client identity.
|
||||
|
||||
/**
|
||||
* @brief Synchronized storage for retained input sessions.
|
||||
*/
|
||||
struct retained_input_state_t {
|
||||
std::mutex mutex; ///< Synchronizes retained session access across transport threads.
|
||||
retained_input_map_t inputs; ///< Paused input sessions keyed by paired-client identity.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Access process-wide retained input session storage.
|
||||
*
|
||||
* @return Mutable retained input session state.
|
||||
*/
|
||||
retained_input_state_t &retained_input_state() {
|
||||
static retained_input_state_t state;
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Execute lifecycle work on the input task thread when it is running.
|
||||
*
|
||||
* @tparam Function Callable type.
|
||||
* @param function Lifecycle operation to execute.
|
||||
*/
|
||||
template<typename Function>
|
||||
void dispatch_input_task(Function &&function) {
|
||||
if (task_pool.running()) {
|
||||
task_pool.push(std::forward<Function>(function));
|
||||
} else {
|
||||
std::forward<Function>(function)();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Destroy the virtual gamepads owned by retained input sessions.
|
||||
*
|
||||
* @param input Retained input session whose gamepads should be destroyed.
|
||||
*/
|
||||
void destroy_gamepads(const std::shared_ptr<input_t> &input) {
|
||||
for (auto &gamepad : input->gamepads) {
|
||||
if (gamepad.back_timeout_id) {
|
||||
task_pool.cancel(gamepad.back_timeout_id);
|
||||
gamepad.back_timeout_id = nullptr;
|
||||
}
|
||||
if (gamepad.id >= 0) {
|
||||
::input::free_gamepad(platf_input, gamepad.id);
|
||||
gamepad.id = -1;
|
||||
}
|
||||
gamepad.gamepad_state = {};
|
||||
gamepad.back_button_state = button_state_e::NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Destroy the virtual gamepads owned by retained input sessions.
|
||||
*
|
||||
* @param inputs Retained input sessions whose gamepads should be destroyed.
|
||||
*/
|
||||
void destroy_gamepads(const retained_input_map_t &inputs) {
|
||||
for (const auto &[session_id, input] : inputs) {
|
||||
static_cast<void>(session_id);
|
||||
destroy_gamepads(input);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Rebind retained input state to a resumed stream mailbox.
|
||||
*
|
||||
* @param input Retained input state.
|
||||
* @param mail Mailbox for the resumed stream connection.
|
||||
*/
|
||||
void rebind_input(const std::shared_ptr<input_t> &input, const safe::mail_t &mail) {
|
||||
input->touch_port_event = mail->event<input::touch_port_t>(mail::touch_port);
|
||||
input->feedback_queue = mail->queue<platf::gamepad_feedback_msg_t>(mail::gamepad_feedback);
|
||||
|
||||
for (int client_index = 0; client_index < input->gamepads.size(); ++client_index) {
|
||||
auto &gamepad = input->gamepads[client_index];
|
||||
if (gamepad.id < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (platf::rebind_gamepad(platf_input, {gamepad.id, static_cast<std::uint8_t>(client_index)}, input->feedback_queue) != 0) {
|
||||
free_id(gamepadMask, gamepad.id);
|
||||
gamepad.id = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Apply shortcut based on VKEY
|
||||
* @param keyCode The VKEY code
|
||||
@@ -1048,6 +1167,40 @@ namespace input {
|
||||
platf::unicode(platf_input, packet->text, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Allocate a virtual gamepad for a client-relative controller slot.
|
||||
*
|
||||
* @param input Stream input state.
|
||||
* @param client_index Client-relative controller index.
|
||||
* @param arrival Client-reported controller metadata.
|
||||
* @return Assigned global gamepad slot, or -1 when allocation fails.
|
||||
*/
|
||||
int alloc_gamepad(std::shared_ptr<input_t> &input, int client_index, const platf::gamepad_arrival_t &arrival) {
|
||||
if (client_index < 0 || client_index >= input->gamepads.size()) {
|
||||
BOOST_LOG(warning) << "ControllerNumber out of range ["sv << client_index << ']';
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto &gamepad = input->gamepads[client_index];
|
||||
if (gamepad.id >= 0) {
|
||||
BOOST_LOG(warning) << "ControllerNumber already allocated ["sv << client_index << ']';
|
||||
return gamepad.id;
|
||||
}
|
||||
|
||||
const auto id = alloc_id(gamepadMask);
|
||||
if (id < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (platf::alloc_gamepad(platf_input, {id, static_cast<std::uint8_t>(client_index)}, arrival, input->feedback_queue)) {
|
||||
free_id(gamepadMask, id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
gamepad.id = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Called to pass a controller arrival message to the platform backend.
|
||||
* @param input The input context pointer.
|
||||
@@ -1058,34 +1211,12 @@ namespace input {
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet->controllerNumber < 0 || packet->controllerNumber >= input->gamepads.size()) {
|
||||
BOOST_LOG(warning) << "ControllerNumber out of range ["sv << packet->controllerNumber << ']';
|
||||
return;
|
||||
}
|
||||
|
||||
if (input->gamepads[packet->controllerNumber].id >= 0) {
|
||||
BOOST_LOG(warning) << "ControllerNumber already allocated ["sv << packet->controllerNumber << ']';
|
||||
return;
|
||||
}
|
||||
|
||||
platf::gamepad_arrival_t arrival {
|
||||
packet->type,
|
||||
util::endian::little(packet->capabilities),
|
||||
util::endian::little(packet->supportedButtonFlags),
|
||||
};
|
||||
|
||||
auto id = alloc_id(gamepadMask);
|
||||
if (id < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Allocate a new gamepad
|
||||
if (platf::alloc_gamepad(platf_input, {id, packet->controllerNumber}, arrival, input->feedback_queue)) {
|
||||
free_id(gamepadMask, id);
|
||||
return;
|
||||
}
|
||||
|
||||
input->gamepads[packet->controllerNumber].id = id;
|
||||
static_cast<void>(alloc_gamepad(input, packet->controllerNumber, arrival));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1335,17 +1466,9 @@ namespace input {
|
||||
// If this is an event for a new gamepad, create the gamepad now. Ideally, the client would
|
||||
// send a controller arrival instead of this but it's still supported for legacy clients.
|
||||
if ((packet->activeGamepadMask & (1 << packet->controllerNumber)) && gamepad.id < 0) {
|
||||
auto id = alloc_id(gamepadMask);
|
||||
if (id < 0) {
|
||||
if (alloc_gamepad(input, packet->controllerNumber, {}) < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (platf::alloc_gamepad(platf_input, {id, (uint8_t) packet->controllerNumber}, {}, input->feedback_queue)) {
|
||||
free_id(gamepadMask, id);
|
||||
return;
|
||||
}
|
||||
|
||||
gamepad.id = id;
|
||||
} else if (!(packet->activeGamepadMask & (1 << packet->controllerNumber)) && gamepad.id >= 0) {
|
||||
// If this is the final event for a gamepad being removed, free the gamepad and return.
|
||||
::input::free_gamepad(platf_input, gamepad.id);
|
||||
@@ -1824,6 +1947,65 @@ namespace input {
|
||||
task_pool.push(passthrough_next_message, input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Release every pressed mouse button tracked by Sunshine.
|
||||
*/
|
||||
void reset_mouse_buttons() {
|
||||
for (int button = 0; button < mouse_press.size(); ++button) {
|
||||
if (mouse_press[button]) {
|
||||
platf::button_mouse(platf_input, button, true);
|
||||
mouse_press[button] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Release every pressed keyboard key tracked by Sunshine.
|
||||
*/
|
||||
void reset_keyboard_keys() {
|
||||
for (auto &[key, pressed] : key_press) {
|
||||
if (pressed) {
|
||||
platf::keyboard_update(platf_input, vk_from_kpid(key) & 0x00FF, true, flags_from_kpid(key));
|
||||
pressed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Neutralize retained gamepads without destroying their virtual devices.
|
||||
*
|
||||
* @param input Retained stream input state to neutralize.
|
||||
*/
|
||||
void reset_gamepads(const std::shared_ptr<input_t> &input) {
|
||||
for (int client_index = 0; client_index < input->gamepads.size(); ++client_index) {
|
||||
auto &gamepad = input->gamepads[client_index];
|
||||
if (gamepad.back_timeout_id) {
|
||||
task_pool.cancel(gamepad.back_timeout_id);
|
||||
gamepad.back_timeout_id = nullptr;
|
||||
}
|
||||
if (gamepad.id >= 0) {
|
||||
platf::gamepad_update(platf_input, gamepad.id, {});
|
||||
platf::gamepad_touch(
|
||||
platf_input,
|
||||
{{gamepad.id, static_cast<std::uint8_t>(client_index)}, LI_TOUCH_EVENT_CANCEL_ALL, 0, 0.0F, 0.0F, 0.0F}
|
||||
);
|
||||
}
|
||||
gamepad.gamepad_state = {};
|
||||
gamepad.back_button_state = button_state_e::NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reset all pressed input state for a disconnected stream.
|
||||
*
|
||||
* @param input Retained stream input state to reset.
|
||||
*/
|
||||
void reset_input_state(const std::shared_ptr<input_t> &input) {
|
||||
reset_mouse_buttons();
|
||||
reset_keyboard_keys();
|
||||
reset_gamepads(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reset the object to its initial empty state.
|
||||
*/
|
||||
@@ -1832,22 +2014,40 @@ namespace input {
|
||||
task_pool.cancel(input->mouse_left_button_timeout);
|
||||
|
||||
// Ensure input is synchronous, by using the task_pool
|
||||
task_pool.push([]() {
|
||||
for (int x = 0; x < mouse_press.size(); ++x) {
|
||||
if (mouse_press[x]) {
|
||||
platf::button_mouse(platf_input, x, true);
|
||||
mouse_press[x] = false;
|
||||
}
|
||||
task_pool.push(reset_input_state, input);
|
||||
}
|
||||
|
||||
void terminate_gamepads() {
|
||||
retained_input_map_t inputs;
|
||||
{
|
||||
auto &state = retained_input_state();
|
||||
std::lock_guard lock {state.mutex};
|
||||
state.inputs.swap(inputs);
|
||||
}
|
||||
|
||||
if (!inputs.empty()) {
|
||||
dispatch_input_task([inputs = std::move(inputs)]() {
|
||||
destroy_gamepads(inputs);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void terminate_gamepads(const std::string_view session_id) {
|
||||
std::shared_ptr<input_t> input;
|
||||
{
|
||||
auto &state = retained_input_state();
|
||||
std::lock_guard lock {state.mutex};
|
||||
const auto iter = state.inputs.find(session_id);
|
||||
if (iter == state.inputs.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &kp : key_press) {
|
||||
if (!kp.second) {
|
||||
// already released
|
||||
continue;
|
||||
}
|
||||
platf::keyboard_update(platf_input, vk_from_kpid(kp.first) & 0x00FF, true, flags_from_kpid(kp.first));
|
||||
key_press[kp.first] = false;
|
||||
}
|
||||
input = std::move(iter->second);
|
||||
state.inputs.erase(iter);
|
||||
}
|
||||
|
||||
dispatch_input_task([input = std::move(input)]() {
|
||||
destroy_gamepads(input);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1860,6 +2060,13 @@ namespace input {
|
||||
* @brief Destroy the input subsystem deinitializer.
|
||||
*/
|
||||
~deinit_t() override {
|
||||
retained_input_map_t inputs;
|
||||
{
|
||||
auto &state = retained_input_state();
|
||||
std::lock_guard lock {state.mutex};
|
||||
state.inputs.swap(inputs);
|
||||
}
|
||||
destroy_gamepads(inputs);
|
||||
platf_input.reset();
|
||||
}
|
||||
};
|
||||
@@ -1886,11 +2093,30 @@ namespace input {
|
||||
/**
|
||||
* @brief Allocate and initialize platform input state for a stream.
|
||||
*/
|
||||
std::shared_ptr<input_t> alloc(safe::mail_t mail) {
|
||||
auto input = std::make_shared<input_t>(
|
||||
mail->event<input::touch_port_t>(mail::touch_port),
|
||||
mail->queue<platf::gamepad_feedback_msg_t>(mail::gamepad_feedback)
|
||||
);
|
||||
std::shared_ptr<input_t> alloc(safe::mail_t mail, std::string session_id) {
|
||||
std::shared_ptr<input_t> input;
|
||||
bool resumed = false;
|
||||
{
|
||||
auto &state = retained_input_state();
|
||||
std::lock_guard lock {state.mutex};
|
||||
const auto iter = state.inputs.find(session_id);
|
||||
if (iter != state.inputs.end()) {
|
||||
input = iter->second;
|
||||
resumed = true;
|
||||
} else {
|
||||
input = std::make_shared<input_t>(
|
||||
mail->event<input::touch_port_t>(mail::touch_port),
|
||||
mail->queue<platf::gamepad_feedback_msg_t>(mail::gamepad_feedback)
|
||||
);
|
||||
state.inputs.try_emplace(std::move(session_id), input);
|
||||
}
|
||||
}
|
||||
|
||||
if (resumed) {
|
||||
dispatch_input_task([input, mail = std::move(mail)]() {
|
||||
rebind_input(input, mail);
|
||||
});
|
||||
}
|
||||
|
||||
// Workaround to ensure new frames will be captured when a client connects
|
||||
task_pool.pushDelayed([]() {
|
||||
@@ -1901,4 +2127,25 @@ namespace input {
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
#ifdef SUNSHINE_TESTS
|
||||
namespace testing {
|
||||
void set_platform_input(platf::input_t input) {
|
||||
terminate_gamepads();
|
||||
platf_input = std::move(input);
|
||||
gamepadMask.reset();
|
||||
}
|
||||
|
||||
int alloc_gamepad(std::shared_ptr<input_t> &input, std::uint8_t client_index, const platf::gamepad_arrival_t &metadata) {
|
||||
return ::input::alloc_gamepad(input, client_index, metadata);
|
||||
}
|
||||
|
||||
int gamepad_id(const std::shared_ptr<input_t> &input, std::uint8_t client_index) {
|
||||
if (!input || client_index >= input->gamepads.size()) {
|
||||
return -1;
|
||||
}
|
||||
return input->gamepads[client_index].id;
|
||||
}
|
||||
} // namespace testing
|
||||
#endif
|
||||
} // namespace input
|
||||
+48
-1
@@ -6,6 +6,7 @@
|
||||
|
||||
// standard includes
|
||||
#include <functional>
|
||||
#include <string_view>
|
||||
|
||||
// local includes
|
||||
#include "platform/common.h"
|
||||
@@ -27,6 +28,21 @@ namespace input {
|
||||
*/
|
||||
void reset(std::shared_ptr<input_t> &input);
|
||||
|
||||
/**
|
||||
* @brief Destroy every retained virtual gamepad session.
|
||||
*
|
||||
* Retained gamepads survive a paused transport connection so they can be reused on resume. Call this when the
|
||||
* streamed application or all streaming sessions are explicitly terminated.
|
||||
*/
|
||||
void terminate_gamepads();
|
||||
|
||||
/**
|
||||
* @brief Destroy virtual gamepads retained for one paired client.
|
||||
*
|
||||
* @param session_id Stable paired-client identity used by alloc().
|
||||
*/
|
||||
void terminate_gamepads(std::string_view session_id);
|
||||
|
||||
/**
|
||||
* @brief Queue a raw input message for platform passthrough.
|
||||
*/
|
||||
@@ -50,9 +66,40 @@ namespace input {
|
||||
* @brief Allocate and initialize platform input state for a stream.
|
||||
*
|
||||
* @param mail Mailbox used to exchange messages with worker threads.
|
||||
* @param session_id Stable paired-client identity shared by launch and resume connections.
|
||||
* @return Shared input state bound to the stream mailbox.
|
||||
*/
|
||||
std::shared_ptr<input_t> alloc(safe::mail_t mail);
|
||||
std::shared_ptr<input_t> alloc(safe::mail_t mail, std::string session_id);
|
||||
|
||||
#ifdef SUNSHINE_TESTS
|
||||
namespace testing {
|
||||
/**
|
||||
* @brief Replace the global platform input backend for a unit test.
|
||||
*
|
||||
* @param input Test-owned platform input backend.
|
||||
*/
|
||||
void set_platform_input(platf::input_t input);
|
||||
|
||||
/**
|
||||
* @brief Allocate a gamepad directly in retained input state for a unit test.
|
||||
*
|
||||
* @param input Retained input state.
|
||||
* @param client_index Client-relative controller index.
|
||||
* @param metadata Client-reported controller metadata.
|
||||
* @return Assigned global gamepad slot, or -1 on failure.
|
||||
*/
|
||||
int alloc_gamepad(std::shared_ptr<input_t> &input, std::uint8_t client_index, const platf::gamepad_arrival_t &metadata);
|
||||
|
||||
/**
|
||||
* @brief Return the global gamepad slot stored for a test controller.
|
||||
*
|
||||
* @param input Retained input state.
|
||||
* @param client_index Client-relative controller index.
|
||||
* @return Assigned global gamepad slot, or -1 when unallocated.
|
||||
*/
|
||||
int gamepad_id(const std::shared_ptr<input_t> &input, std::uint8_t client_index);
|
||||
} // namespace testing
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Touchscreen coordinate bounds used to scale absolute input.
|
||||
|
||||
@@ -1239,6 +1239,15 @@ namespace platf {
|
||||
* @return 0 on success.
|
||||
*/
|
||||
int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
|
||||
/**
|
||||
* @brief Rebind an existing virtual gamepad to a resumed client session.
|
||||
*
|
||||
* @param input Platform input backend that owns the virtual gamepad.
|
||||
* @param id Global and client-relative identifiers for the resumed controller.
|
||||
* @param feedback_queue Queue used to return gamepad feedback to the resumed client.
|
||||
* @return 0 when the gamepad exists and was rebound; otherwise -1.
|
||||
*/
|
||||
int rebind_gamepad(input_t &input, const gamepad_id_t &id, feedback_queue_t feedback_queue);
|
||||
/**
|
||||
* @brief Release gamepad resources.
|
||||
*
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <numbers>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
@@ -30,6 +31,7 @@ namespace platf::virtualhid {
|
||||
*/
|
||||
struct gamepad_context_t {
|
||||
std::unique_ptr<lvh::GamepadStateAdapter> adapter; ///< State adapter for the virtual gamepad.
|
||||
std::mutex feedback_mutex; ///< Synchronizes feedback routing with session rebinding.
|
||||
feedback_queue_t feedback_queue; ///< Feedback queue for client output events.
|
||||
std::array<std::optional<std::uint32_t>, 2> touch_ids; ///< Client touch IDs assigned to libvirtualhid slots.
|
||||
std::uint8_t client_relative_index = 0; ///< Client-relative controller index.
|
||||
@@ -328,13 +330,37 @@ namespace platf::virtualhid {
|
||||
}
|
||||
}
|
||||
|
||||
void raise_feedback(const std::shared_ptr<gamepad_context_t> &gamepad, const gamepad_feedback_msg_t &message) {
|
||||
/**
|
||||
* @brief Raise gamepad feedback while the caller owns the feedback lock.
|
||||
*
|
||||
* @param gamepad Gamepad receiving output from the virtual device.
|
||||
* @param message Feedback message to deliver to the client.
|
||||
*/
|
||||
void raise_feedback_unlocked(const std::shared_ptr<gamepad_context_t> &gamepad, const gamepad_feedback_msg_t &message) {
|
||||
if (gamepad->feedback_queue) {
|
||||
gamepad->feedback_queue->raise(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Raise gamepad feedback using the current client routing.
|
||||
*
|
||||
* @param gamepad Gamepad receiving output from the virtual device.
|
||||
* @param message Feedback message to deliver to the client.
|
||||
*/
|
||||
void raise_feedback(const std::shared_ptr<gamepad_context_t> &gamepad, const gamepad_feedback_msg_t &message) {
|
||||
std::lock_guard lock {gamepad->feedback_mutex};
|
||||
raise_feedback_unlocked(gamepad, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Translate a libvirtualhid output event into Moonlight feedback.
|
||||
*
|
||||
* @param gamepad Gamepad that emitted the output event.
|
||||
* @param output Output event reported by libvirtualhid.
|
||||
*/
|
||||
void handle_output(const std::shared_ptr<gamepad_context_t> &gamepad, const lvh::GamepadOutput &output) {
|
||||
std::lock_guard lock {gamepad->feedback_mutex};
|
||||
switch (output.kind) {
|
||||
case lvh::GamepadOutputKind::rumble:
|
||||
if (gamepad->has_last_rumble && gamepad->last_low_frequency_rumble == output.low_frequency_rumble && gamepad->last_high_frequency_rumble == output.high_frequency_rumble) {
|
||||
@@ -343,7 +369,7 @@ namespace platf::virtualhid {
|
||||
gamepad->has_last_rumble = true;
|
||||
gamepad->last_low_frequency_rumble = output.low_frequency_rumble;
|
||||
gamepad->last_high_frequency_rumble = output.high_frequency_rumble;
|
||||
raise_feedback(gamepad, gamepad_feedback_msg_t::make_rumble(gamepad->client_relative_index, output.low_frequency_rumble, output.high_frequency_rumble));
|
||||
raise_feedback_unlocked(gamepad, gamepad_feedback_msg_t::make_rumble(gamepad->client_relative_index, output.low_frequency_rumble, output.high_frequency_rumble));
|
||||
break;
|
||||
case lvh::GamepadOutputKind::trigger_rumble:
|
||||
if (gamepad->has_last_trigger_rumble && gamepad->last_left_trigger_rumble == output.left_trigger_rumble && gamepad->last_right_trigger_rumble == output.right_trigger_rumble) {
|
||||
@@ -352,7 +378,7 @@ namespace platf::virtualhid {
|
||||
gamepad->has_last_trigger_rumble = true;
|
||||
gamepad->last_left_trigger_rumble = output.left_trigger_rumble;
|
||||
gamepad->last_right_trigger_rumble = output.right_trigger_rumble;
|
||||
raise_feedback(gamepad, gamepad_feedback_msg_t::make_rumble_triggers(gamepad->client_relative_index, output.left_trigger_rumble, output.right_trigger_rumble));
|
||||
raise_feedback_unlocked(gamepad, gamepad_feedback_msg_t::make_rumble_triggers(gamepad->client_relative_index, output.left_trigger_rumble, output.right_trigger_rumble));
|
||||
break;
|
||||
case lvh::GamepadOutputKind::rgb_led:
|
||||
if (gamepad->has_last_rgb && gamepad->last_red == output.red && gamepad->last_green == output.green && gamepad->last_blue == output.blue) {
|
||||
@@ -362,10 +388,10 @@ namespace platf::virtualhid {
|
||||
gamepad->last_red = output.red;
|
||||
gamepad->last_green = output.green;
|
||||
gamepad->last_blue = output.blue;
|
||||
raise_feedback(gamepad, gamepad_feedback_msg_t::make_rgb_led(gamepad->client_relative_index, output.red, output.green, output.blue));
|
||||
raise_feedback_unlocked(gamepad, gamepad_feedback_msg_t::make_rgb_led(gamepad->client_relative_index, output.red, output.green, output.blue));
|
||||
break;
|
||||
case lvh::GamepadOutputKind::adaptive_triggers:
|
||||
raise_feedback(gamepad, gamepad_feedback_msg_t::make_adaptive_triggers(gamepad->client_relative_index, output.adaptive_trigger_flags, output.left_trigger_effect_type, output.right_trigger_effect_type, output.left_trigger_effect, output.right_trigger_effect));
|
||||
raise_feedback_unlocked(gamepad, gamepad_feedback_msg_t::make_adaptive_triggers(gamepad->client_relative_index, output.adaptive_trigger_flags, output.left_trigger_effect_type, output.right_trigger_effect_type, output.left_trigger_effect, output.right_trigger_effect));
|
||||
break;
|
||||
case lvh::GamepadOutputKind::raw_report:
|
||||
break;
|
||||
@@ -526,8 +552,10 @@ namespace platf::virtualhid {
|
||||
gamepad->adapter = std::move(created.adapter);
|
||||
gamepad->feedback_queue = std::move(feedback_queue);
|
||||
gamepad->client_relative_index = id.clientRelativeIndex;
|
||||
gamepad->adapter->set_output_callback([gamepad](const lvh::GamepadOutput &output) {
|
||||
handle_output(gamepad, output);
|
||||
gamepad->adapter->set_output_callback([weak_gamepad = std::weak_ptr {gamepad}](const lvh::GamepadOutput &output) {
|
||||
if (const auto gamepad = weak_gamepad.lock()) {
|
||||
handle_output(gamepad, output);
|
||||
}
|
||||
});
|
||||
|
||||
const auto &support = gamepad->adapter->support();
|
||||
@@ -542,6 +570,27 @@ namespace platf::virtualhid {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rebind_gamepad(input_context_t &context, const gamepad_id_t &id, feedback_queue_t feedback_queue) {
|
||||
if (!has_gamepad(context, id.globalIndex)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const auto &gamepad = context.gamepads[id.globalIndex];
|
||||
std::lock_guard lock {gamepad->feedback_mutex};
|
||||
gamepad->feedback_queue = std::move(feedback_queue);
|
||||
gamepad->client_relative_index = id.clientRelativeIndex;
|
||||
gamepad->has_last_rumble = false;
|
||||
gamepad->has_last_trigger_rumble = false;
|
||||
gamepad->has_last_rgb = false;
|
||||
|
||||
if (gamepad->adapter->support().supports_motion) {
|
||||
raise_feedback_unlocked(gamepad, gamepad_feedback_msg_t::make_motion_event_state(id.clientRelativeIndex, LI_MOTION_TYPE_ACCEL, 100));
|
||||
raise_feedback_unlocked(gamepad, gamepad_feedback_msg_t::make_motion_event_state(id.clientRelativeIndex, LI_MOTION_TYPE_GYRO, 100));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool has_gamepad(const input_context_t &context, int nr) {
|
||||
return nr >= 0 && nr < context.gamepads.size() && context.gamepads[nr] && context.gamepads[nr]->adapter;
|
||||
}
|
||||
@@ -938,6 +987,10 @@ namespace platf {
|
||||
return virtualhid::alloc_gamepad(virtualhid::get_input_context(input), id, metadata, std::move(feedback_queue));
|
||||
}
|
||||
|
||||
int rebind_gamepad(input_t &input, const gamepad_id_t &id, feedback_queue_t feedback_queue) {
|
||||
return virtualhid::rebind_gamepad(virtualhid::get_input_context(input), id, std::move(feedback_queue));
|
||||
}
|
||||
|
||||
void free_gamepad(input_t &input, int nr) {
|
||||
virtualhid::free_gamepad(virtualhid::get_input_context(input), nr);
|
||||
}
|
||||
|
||||
@@ -110,6 +110,16 @@ namespace platf::virtualhid {
|
||||
*/
|
||||
int alloc_gamepad(input_context_t &context, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
|
||||
|
||||
/**
|
||||
* @brief Rebind an existing libvirtualhid gamepad to a resumed client session.
|
||||
*
|
||||
* @param context Input context.
|
||||
* @param id Sunshine gamepad identifiers for the resumed client.
|
||||
* @param feedback_queue Queue used to return gamepad feedback to the resumed client.
|
||||
* @return 0 when the gamepad exists and was rebound; otherwise -1.
|
||||
*/
|
||||
int rebind_gamepad(input_context_t &context, const gamepad_id_t &id, feedback_queue_t feedback_queue);
|
||||
|
||||
/**
|
||||
* @brief Check whether a libvirtualhid gamepad exists in a slot.
|
||||
*
|
||||
|
||||
@@ -338,6 +338,36 @@ namespace platf {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Route an existing ViGEm gamepad's feedback to a resumed client.
|
||||
*
|
||||
* @param id Global and client-relative identifiers for the resumed controller.
|
||||
* @param feedback_queue Queue used to return gamepad feedback to the resumed client.
|
||||
* @return 0 when the target exists and was rebound; otherwise -1.
|
||||
*/
|
||||
int rebind_gamepad(const gamepad_id_t &id, feedback_queue_t feedback_queue) {
|
||||
if (id.globalIndex < 0 || id.globalIndex >= static_cast<int>(gamepads.size())) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto &gamepad = gamepads[id.globalIndex];
|
||||
if (!gamepad.gp || !vigem_target_is_attached(gamepad.gp.get())) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
gamepad.client_relative_index = id.clientRelativeIndex;
|
||||
gamepad.feedback_queue = std::move(feedback_queue);
|
||||
gamepad.last_rumble = {};
|
||||
gamepad.last_rgb_led = {};
|
||||
|
||||
if (gamepad.feedback_queue && vigem_target_get_type(gamepad.gp.get()) == DualShock4Wired) {
|
||||
gamepad.feedback_queue->raise(gamepad_feedback_msg_t::make_motion_event_state(id.clientRelativeIndex, LI_MOTION_TYPE_ACCEL, 100));
|
||||
gamepad.feedback_queue->raise(gamepad_feedback_msg_t::make_motion_event_state(id.clientRelativeIndex, LI_MOTION_TYPE_GYRO, 100));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Detaches the specified gamepad
|
||||
* @param nr The gamepad.
|
||||
@@ -645,6 +675,20 @@ namespace platf {
|
||||
return raw->vigem->alloc_gamepad_internal(id, feedback_queue, selectedGamepadType);
|
||||
}
|
||||
|
||||
int rebind_gamepad(input_t &input, const gamepad_id_t &id, feedback_queue_t feedback_queue) {
|
||||
auto raw = (input_raw_t *) input.get();
|
||||
|
||||
if (virtualhid::has_gamepad(raw->virtualhid, id.globalIndex)) {
|
||||
return virtualhid::rebind_gamepad(raw->virtualhid, id, std::move(feedback_queue));
|
||||
}
|
||||
|
||||
if (!raw->vigem) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return raw->vigem->rebind_gamepad(id, std::move(feedback_queue));
|
||||
}
|
||||
|
||||
void free_gamepad(input_t &input, int nr) {
|
||||
auto raw = (input_raw_t *) input.get();
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "config.h"
|
||||
#include "crypto.h"
|
||||
#include "display_device.h"
|
||||
#include "input.h"
|
||||
#include "logging.h"
|
||||
#include "platform/common.h"
|
||||
#include "process.h"
|
||||
@@ -318,6 +319,7 @@ namespace proc {
|
||||
}
|
||||
|
||||
void proc_t::terminate() {
|
||||
input::terminate_gamepads();
|
||||
std::error_code ec;
|
||||
placebo = false;
|
||||
terminate_process_group(_process, _process_group, _app.exit_timeout);
|
||||
|
||||
@@ -758,6 +758,7 @@ namespace rtsp_stream {
|
||||
|
||||
void terminate_sessions() {
|
||||
server.clear(true);
|
||||
input::terminate_gamepads();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -765,6 +766,7 @@ namespace rtsp_stream {
|
||||
*/
|
||||
void terminate_sessions_by_cert(std::string_view cert) {
|
||||
server.clear_by_cert(cert);
|
||||
input::terminate_gamepads(cert);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+6
-3
@@ -536,6 +536,7 @@ namespace stream {
|
||||
|
||||
std::uint32_t launch_session_id; ///< RTSP launch-session ID associated with this stream.
|
||||
std::string client_cert; ///< PEM certificate for the paired client owning the stream.
|
||||
std::string input_session_id; ///< Stable client identity used to retain input devices across resume.
|
||||
|
||||
safe::mail_raw_t::event_t<bool> shutdown_event; ///< Event raised when the stream should shut down.
|
||||
safe::signal_t controlEnd; ///< Signal raised when the control channel exits.
|
||||
@@ -1805,8 +1806,8 @@ namespace stream {
|
||||
// but we can simply replace it with the matrix generated by OpenFEC which
|
||||
// works correctly. This is possible because the data and FEC shard count is
|
||||
// constant and known in advance.
|
||||
const unsigned char parity[] = {0x77, 0x40, 0x38, 0x0e, 0xc7, 0xa7, 0x0d, 0x6c};
|
||||
memcpy(rs.get()->p, parity, sizeof(parity));
|
||||
constexpr std::array<unsigned char, 8> parity {0x77, 0x40, 0x38, 0x0e, 0xc7, 0xa7, 0x0d, 0x6c};
|
||||
memcpy(rs.get()->p, parity.data(), parity.size());
|
||||
|
||||
audio_packet.rtp.header = 0x80;
|
||||
audio_packet.rtp.packetType = 97;
|
||||
@@ -2191,6 +2192,7 @@ namespace stream {
|
||||
} else {
|
||||
// We have no app running and also no clients anymore.
|
||||
revert_display_config = true;
|
||||
input::terminate_gamepads();
|
||||
}
|
||||
|
||||
if (revert_display_config) {
|
||||
@@ -2207,7 +2209,7 @@ namespace stream {
|
||||
* @brief Start the audio, video, and control workers for a streaming session.
|
||||
*/
|
||||
int start(session_t &session, const std::string &addr_string) {
|
||||
session.input = input::alloc(session.mail);
|
||||
session.input = input::alloc(session.mail, session.input_session_id);
|
||||
|
||||
session.broadcast_ref = broadcast.ref();
|
||||
if (!session.broadcast_ref) {
|
||||
@@ -2259,6 +2261,7 @@ namespace stream {
|
||||
session->shutdown_event = mail->event<bool>(mail::shutdown);
|
||||
session->launch_session_id = launch_session.id;
|
||||
session->client_cert = launch_session.client_cert;
|
||||
session->input_session_id = launch_session.client_cert.empty() ? launch_session.unique_id : launch_session.client_cert;
|
||||
|
||||
session->config = config;
|
||||
|
||||
|
||||
@@ -136,6 +136,16 @@ namespace thread_pool_util {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check whether worker threads are available to execute queued tasks.
|
||||
*
|
||||
* @return True while the thread pool is running.
|
||||
*/
|
||||
bool running() {
|
||||
std::lock_guard lg(_lock);
|
||||
return _continue;
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Run the main application or worker loop.
|
||||
|
||||
@@ -247,15 +247,18 @@ TEST_F(VirtualHidDeviceTest, RejectsUnavailableAndInvalidGamepadSlots) {
|
||||
EXPECT_FALSE(platf::virtualhid::has_gamepad(*context(), -1));
|
||||
EXPECT_FALSE(platf::virtualhid::has_gamepad(*context(), static_cast<int>(context()->gamepads.size())));
|
||||
EXPECT_EQ(platf::virtualhid::gamepad_adapter_for_testing(*context(), 0), nullptr);
|
||||
EXPECT_EQ(platf::virtualhid::rebind_gamepad(*context(), valid_id, feedback_queue()), -1);
|
||||
}
|
||||
|
||||
TEST_F(VirtualHidDeviceTest, AllocatesManualProfileAndTranslatesFullState) {
|
||||
const auto active_devices_before_gamepad = context()->runtime->active_device_count();
|
||||
config::input.ds4_back_as_touchpad_click = true;
|
||||
config::input.virtualhid_randomize_mac = false;
|
||||
const auto capabilities = static_cast<std::uint16_t>(LI_CCAP_ACCEL | LI_CCAP_GYRO | LI_CCAP_TOUCHPAD | LI_CCAP_RGB_LED | LI_CCAP_BATTERY_STATE);
|
||||
auto *adapter = allocate_gamepad("ds5"sv, LI_CTYPE_PS, capabilities, 1, 4);
|
||||
ASSERT_NE(adapter, nullptr);
|
||||
ASSERT_NE(adapter->gamepad(), nullptr);
|
||||
EXPECT_EQ(context()->runtime->active_device_count(), active_devices_before_gamepad + 1);
|
||||
|
||||
EXPECT_EQ(adapter->gamepad()->profile().gamepad_kind, lvh::GamepadProfileKind::dualsense);
|
||||
EXPECT_TRUE(adapter->gamepad()->profile().name.starts_with("Sunshine "));
|
||||
@@ -301,6 +304,7 @@ TEST_F(VirtualHidDeviceTest, AllocatesManualProfileAndTranslatesFullState) {
|
||||
|
||||
platf::virtualhid::free_gamepad(*context(), 1);
|
||||
EXPECT_FALSE(platf::virtualhid::has_gamepad(*context(), 1));
|
||||
EXPECT_EQ(context()->runtime->active_device_count(), active_devices_before_gamepad);
|
||||
platf::virtualhid::free_gamepad(*context(), 1);
|
||||
platf::virtualhid::gamepad_update(*context(), 1, input_state);
|
||||
}
|
||||
@@ -414,6 +418,18 @@ TEST_F(VirtualHidDeviceTest, RoutesAndDeduplicatesGamepadFeedback) {
|
||||
EXPECT_EQ(feedback->data.adaptive_triggers.left, output.left_trigger_effect);
|
||||
EXPECT_EQ(feedback->data.adaptive_triggers.right, output.right_trigger_effect);
|
||||
|
||||
auto resumed_feedback = mail::man->queue<platf::gamepad_feedback_msg_t>("virtualhid-input-test-resumed-feedback");
|
||||
ASSERT_EQ(platf::virtualhid::rebind_gamepad(*context(), {0, 7}, resumed_feedback), 0);
|
||||
output.kind = lvh::GamepadOutputKind::rumble;
|
||||
output.low_frequency_rumble = 100;
|
||||
output.high_frequency_rumble = 201;
|
||||
ASSERT_TRUE(adapter->dispatch_output(output).ok());
|
||||
feedback = resumed_feedback->pop(10ms);
|
||||
ASSERT_TRUE(feedback);
|
||||
EXPECT_EQ(feedback->type, platf::gamepad_feedback_e::rumble);
|
||||
EXPECT_EQ(feedback->id, 7);
|
||||
EXPECT_FALSE(feedback_queue()->pop(0ms));
|
||||
|
||||
output.kind = lvh::GamepadOutputKind::raw_report;
|
||||
ASSERT_TRUE(adapter->dispatch_output(output).ok());
|
||||
EXPECT_FALSE(feedback_queue()->pop(0ms));
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @file tests/unit/test_input.cpp
|
||||
* @brief Tests for retained stream input and virtual gamepad lifecycle behavior.
|
||||
*/
|
||||
|
||||
// standard includes
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
// local includes
|
||||
#include "../tests_common.h"
|
||||
#include "src/config.h"
|
||||
#include "src/input.h"
|
||||
#include "src/platform/virtualhid_input.h"
|
||||
|
||||
namespace {
|
||||
/**
|
||||
* @brief Fixture that installs a fake global virtual input backend.
|
||||
*/
|
||||
class InputGamepadSessionTest: public ::testing::Test {
|
||||
protected:
|
||||
/**
|
||||
* @brief Preserve configuration and install observable fake devices.
|
||||
*/
|
||||
void SetUp() override {
|
||||
original_input_ = config::input;
|
||||
config::input.controller = true;
|
||||
config::input.gamepad = "xseries";
|
||||
|
||||
auto platform_input = platf::input();
|
||||
ASSERT_TRUE(platform_input);
|
||||
auto &context = platf::virtualhid::get_input_context(platform_input);
|
||||
context = platf::virtualhid::input_context_t {lvh::BackendKind::fake};
|
||||
runtime_ = context.runtime.get();
|
||||
ASSERT_NE(runtime_, nullptr);
|
||||
input::testing::set_platform_input(std::move(platform_input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Destroy retained test sessions and restore configuration.
|
||||
*/
|
||||
void TearDown() override {
|
||||
input::terminate_gamepads();
|
||||
input::testing::set_platform_input({});
|
||||
runtime_ = nullptr;
|
||||
config::input = std::move(original_input_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Access the fake runtime installed for the current test.
|
||||
*
|
||||
* @return Fake libvirtualhid runtime.
|
||||
*/
|
||||
lvh::Runtime &runtime() const {
|
||||
return *runtime_;
|
||||
}
|
||||
|
||||
private:
|
||||
lvh::Runtime *runtime_ = nullptr; ///< Fake runtime installed in the global input backend.
|
||||
config::input_t original_input_; ///< Input configuration restored after each test.
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST_F(InputGamepadSessionTest, ReusesGamepadsAcrossPauseAndDestroysThemOnTermination) {
|
||||
const std::string session_id = "paired-client-certificate";
|
||||
auto first_mail = std::make_shared<safe::mail_raw_t>();
|
||||
auto first = input::alloc(first_mail, session_id);
|
||||
ASSERT_NE(first, nullptr);
|
||||
const auto active_devices_before_gamepad = runtime().active_device_count();
|
||||
|
||||
const platf::gamepad_arrival_t metadata {LI_CTYPE_XBOX, 0, 0};
|
||||
const auto original_id = input::testing::alloc_gamepad(first, 0, metadata);
|
||||
ASSERT_GE(original_id, 0);
|
||||
EXPECT_EQ(runtime().active_device_count(), active_devices_before_gamepad + 1);
|
||||
|
||||
const std::weak_ptr<input::input_t> paused = first;
|
||||
first.reset();
|
||||
EXPECT_FALSE(paused.expired());
|
||||
EXPECT_EQ(runtime().active_device_count(), active_devices_before_gamepad + 1);
|
||||
|
||||
auto resumed_mail = std::make_shared<safe::mail_raw_t>();
|
||||
auto resumed = input::alloc(resumed_mail, session_id);
|
||||
ASSERT_NE(resumed, nullptr);
|
||||
EXPECT_EQ(paused.lock(), resumed);
|
||||
EXPECT_EQ(input::testing::gamepad_id(resumed, 0), original_id);
|
||||
EXPECT_EQ(runtime().active_device_count(), active_devices_before_gamepad + 1);
|
||||
|
||||
input::terminate_gamepads(session_id);
|
||||
EXPECT_EQ(input::testing::gamepad_id(resumed, 0), -1);
|
||||
EXPECT_EQ(runtime().active_device_count(), active_devices_before_gamepad);
|
||||
|
||||
auto replacement = input::alloc(std::make_shared<safe::mail_raw_t>(), session_id);
|
||||
EXPECT_NE(replacement, resumed);
|
||||
}
|
||||
Vendored
+1
-1
Submodule third-party/libvirtualhid updated: 0c9624bd4b...abfd0ee404.
Reference in new issue
Block a user