Add runtime input toggles and gated input sending
Some checks are pending
Build / setup (push) Waiting to run
Build / build-appimage (push) Blocked by required conditions
Build / build-steamlink (push) Blocked by required conditions
Build / build-windows-macos (push) Blocked by required conditions

This commit is contained in:
Joey Yakimowich-Payne 2026-02-11 17:18:53 -07:00
commit f78a63ab75
9 changed files with 312 additions and 68 deletions

View file

@ -6,6 +6,18 @@
#include <QtMath>
static bool isGamepadInputAllowed()
{
auto session = Session::get();
return session == nullptr || session->isGamepadInputAllowed();
}
static bool isKeyboardMouseInputAllowed()
{
auto session = Session::get();
return session == nullptr || session->isKeyboardMouseInputAllowed();
}
// How long the Start button must be pressed to toggle mouse emulation
#define MOUSE_EMULATION_LONG_PRESS_TIME 750
@ -101,15 +113,17 @@ void SdlInputHandler::sendGamepadState(GamepadState* state)
}
}
LiSendMultiControllerEvent(state->index,
m_GamepadMask,
buttons,
lt,
rt,
lsX,
lsY,
rsX,
rsY);
if (isGamepadInputAllowed()) {
LiSendMultiControllerEvent(state->index,
m_GamepadMask,
buttons,
lt,
rt,
lsX,
lsY,
rsX,
rsY);
}
}
void SdlInputHandler::sendGamepadBatteryState(GamepadState* state, SDL_JoystickPowerLevel level)
@ -150,7 +164,9 @@ void SdlInputHandler::sendGamepadBatteryState(GamepadState* state, SDL_JoystickP
return;
}
LiSendControllerBatteryEvent(state->index, batteryState, batteryPercentage);
if (isGamepadInputAllowed()) {
LiSendControllerBatteryEvent(state->index, batteryState, batteryPercentage);
}
}
Uint32 SdlInputHandler::mouseEmulationTimerCallback(Uint32 interval, void *param)
@ -181,7 +197,7 @@ Uint32 SdlInputHandler::mouseEmulationTimerCallback(Uint32 interval, void *param
deltaX = qAbs(deltaX) > MOUSE_EMULATION_DEADZONE ? deltaX - MOUSE_EMULATION_DEADZONE : 0;
deltaY = qAbs(deltaY) > MOUSE_EMULATION_DEADZONE ? deltaY - MOUSE_EMULATION_DEADZONE : 0;
if (deltaX != 0 || deltaY != 0) {
if ((deltaX != 0 || deltaY != 0) && isKeyboardMouseInputAllowed()) {
LiSendMouseMoveEvent((short)deltaX, (short)deltaY);
}
@ -291,31 +307,49 @@ void SdlInputHandler::handleControllerButtonEvent(SDL_ControllerButtonEvent* eve
}
else if (state->mouseEmulationTimer != 0) {
if (event->button == SDL_CONTROLLER_BUTTON_A) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_LEFT);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_LEFT);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_B) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_RIGHT);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_RIGHT);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_X) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_MIDDLE);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_MIDDLE);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_X1);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_X1);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_X2);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, BUTTON_X2);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_DPAD_UP) {
LiSendScrollEvent(1);
if (isKeyboardMouseInputAllowed()) {
LiSendScrollEvent(1);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) {
LiSendScrollEvent(-1);
if (isKeyboardMouseInputAllowed()) {
LiSendScrollEvent(-1);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) {
LiSendHScrollEvent(1);
if (isKeyboardMouseInputAllowed()) {
LiSendHScrollEvent(1);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_DPAD_LEFT) {
LiSendHScrollEvent(-1);
if (isKeyboardMouseInputAllowed()) {
LiSendHScrollEvent(-1);
}
}
}
}
@ -346,19 +380,29 @@ void SdlInputHandler::handleControllerButtonEvent(SDL_ControllerButtonEvent* eve
}
else if (state->mouseEmulationTimer != 0) {
if (event->button == SDL_CONTROLLER_BUTTON_A) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_LEFT);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_LEFT);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_B) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_RIGHT);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_RIGHT);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_X) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_MIDDLE);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_MIDDLE);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_X1);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_X1);
}
}
else if (event->button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_X2);
if (isKeyboardMouseInputAllowed()) {
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, BUTTON_X2);
}
}
}
}
@ -375,8 +419,10 @@ void SdlInputHandler::handleControllerButtonEvent(SDL_ControllerButtonEvent* eve
SDL_PushEvent(&event);
// Clear buttons down on this gamepad
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
if (isGamepadInputAllowed()) {
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
}
return;
}
@ -390,8 +436,41 @@ void SdlInputHandler::handleControllerButtonEvent(SDL_ControllerButtonEvent* eve
!Session::get()->getOverlayManager().isOverlayEnabled(Overlay::OverlayDebug));
// Clear buttons down on this gamepad
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
if (isGamepadInputAllowed()) {
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
}
return;
}
if (state->buttons == (BACK_FLAG | LB_FLAG | RB_FLAG | Y_FLAG)) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Detected keyboard/mouse input toggle gamepad combo");
Session::get()->toggleKeyboardMouseInputAllowed();
if (isGamepadInputAllowed()) {
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
}
return;
}
if (state->buttons == (BACK_FLAG | LB_FLAG | RB_FLAG | A_FLAG)) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Detected gamepad input toggle gamepad combo");
bool gamepadWasEnabled = isGamepadInputAllowed();
if (gamepadWasEnabled) {
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
}
Session::get()->toggleGamepadInputAllowed();
if (isGamepadInputAllowed()) {
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
}
return;
}
@ -418,7 +497,9 @@ void SdlInputHandler::handleControllerSensorEvent(SDL_ControllerSensorEvent* eve
memcpy(state->lastAccelEventData, event->data, sizeof(event->data));
state->lastAccelEventTime = event->timestamp;
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_ACCEL, event->data[0], event->data[1], event->data[2]);
if (isGamepadInputAllowed()) {
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_ACCEL, event->data[0], event->data[1], event->data[2]);
}
}
break;
case SDL_SENSOR_GYRO:
@ -429,10 +510,12 @@ void SdlInputHandler::handleControllerSensorEvent(SDL_ControllerSensorEvent* eve
state->lastGyroEventTime = event->timestamp;
// Convert rad/s to deg/s
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_GYRO,
event->data[0] * 57.2957795f,
event->data[1] * 57.2957795f,
event->data[2] * 57.2957795f);
if (isGamepadInputAllowed()) {
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_GYRO,
event->data[0] * 57.2957795f,
event->data[1] * 57.2957795f,
event->data[2] * 57.2957795f);
}
}
break;
}
@ -460,7 +543,9 @@ void SdlInputHandler::handleControllerTouchpadEvent(SDL_ControllerTouchpadEvent*
return;
}
LiSendControllerTouchEvent((uint8_t)state->index, eventType, event->finger, event->x, event->y, event->pressure);
if (isGamepadInputAllowed()) {
LiSendControllerTouchEvent((uint8_t)state->index, eventType, event->finger, event->x, event->y, event->pressure);
}
}
#endif
@ -708,7 +793,9 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve
#endif
type == LI_CTYPE_PS;
LiSendControllerArrivalEvent(state->index, m_GamepadMask, type, supportedButtonFlags, capabilities);
if (isGamepadInputAllowed()) {
LiSendControllerArrivalEvent(state->index, m_GamepadMask, type, supportedButtonFlags, capabilities);
}
#else
// Send an empty event to tell the PC we've arrived
@ -750,8 +837,10 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve
state->index);
// Send a final event to let the PC know this gamepad is gone
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
if (isGamepadInputAllowed()) {
LiSendMultiControllerEvent(state->index, m_GamepadMask,
0, 0, 0, 0, 0, 0, 0);
}
// Clear all remaining state from this slot
SDL_memset(state, 0, sizeof(*state));