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
|
||||
Reference in new issue
Block a user