From c8b887fe32a893a441a63dceb022281d2b330698 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 7 Dec 2019 16:17:01 -0800 Subject: [PATCH] Use the new SDL_GameControllerRumble() API on SDL 2.0.9 to fix rumble on HIDAPI gamepads --- app/streaming/input.cpp | 19 ++++++++++++++++++- app/streaming/input.h | 5 ++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/streaming/input.cpp b/app/streaming/input.cpp index f29e34e7..dbb5c6be 100644 --- a/app/streaming/input.cpp +++ b/app/streaming/input.cpp @@ -112,12 +112,14 @@ SdlInputHandler::SdlInputHandler(StreamingPreferences& prefs, NvComputer*, int s SDL_GetError()); } +#if !SDL_VERSION_ATLEAST(2, 0, 9) SDL_assert(!SDL_WasInit(SDL_INIT_HAPTIC)); if (SDL_InitSubSystem(SDL_INIT_HAPTIC) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_InitSubSystem(SDL_INIT_HAPTIC) failed: %s", SDL_GetError()); } +#endif // Initialize the gamepad mask with currently attached gamepads to avoid // causing gamepads to unexpectedly disappear and reappear on the host @@ -141,9 +143,11 @@ SdlInputHandler::~SdlInputHandler() Session::get()->notifyMouseEmulationMode(false); SDL_RemoveTimer(m_GamepadState[i].mouseEmulationTimer); } +#if !SDL_VERSION_ATLEAST(2, 0, 9) if (m_GamepadState[i].haptic != nullptr) { SDL_HapticClose(m_GamepadState[i].haptic); } +#endif if (m_GamepadState[i].controller != nullptr) { SDL_GameControllerClose(m_GamepadState[i].controller); } @@ -154,8 +158,10 @@ SdlInputHandler::~SdlInputHandler() SDL_RemoveTimer(m_RightButtonReleaseTimer); SDL_RemoveTimer(m_DragTimer); +#if !SDL_VERSION_ATLEAST(2, 0, 9) SDL_QuitSubSystem(SDL_INIT_HAPTIC); SDL_assert(!SDL_WasInit(SDL_INIT_HAPTIC)); +#endif SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER); SDL_assert(!SDL_WasInit(SDL_INIT_GAMECONTROLLER)); @@ -947,6 +953,8 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve state->controller = controller; state->jsId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(state->controller)); + +#if !SDL_VERSION_ATLEAST(2, 0, 9) state->haptic = SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(state->controller)); state->hapticEffectId = -1; state->hapticMethod = GAMEPAD_HAPTIC_METHOD_NONE; @@ -965,6 +973,7 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve state->hapticMethod = GAMEPAD_HAPTIC_METHOD_LEFTRIGHT; } } +#endif SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(SDL_GameControllerGetJoystick(state->controller)), guidStr, sizeof(guidStr)); @@ -1004,9 +1013,12 @@ void SdlInputHandler::handleControllerDeviceEvent(SDL_ControllerDeviceEvent* eve } SDL_GameControllerClose(state->controller); + +#if !SDL_VERSION_ATLEAST(2, 0, 9) if (state->haptic != nullptr) { SDL_HapticClose(state->haptic); } +#endif // Remove this from the gamepad mask in MC-mode if (m_MultiController) { @@ -1067,6 +1079,11 @@ void SdlInputHandler::rumble(unsigned short controllerNumber, unsigned short low return; } +#if SDL_VERSION_ATLEAST(2, 0, 9) + if (m_GamepadState[controllerNumber].controller != nullptr) { + SDL_GameControllerRumble(m_GamepadState[controllerNumber].controller, lowFreqMotor, highFreqMotor, 30000); + } +#else // Check if the controller supports haptics (and if the controller exists at all) SDL_Haptic* haptic = m_GamepadState[controllerNumber].haptic; if (haptic == nullptr) { @@ -1110,7 +1127,7 @@ void SdlInputHandler::rumble(unsigned short controllerNumber, unsigned short low GAMEPAD_HAPTIC_SIMPLE_LOWFREQ_MOTOR_WEIGHT*lowFreqMotor) / 65535.0), SDL_HAPTIC_INFINITY); } - +#endif } void SdlInputHandler::handleTouchFingerEvent(SDL_TouchFingerEvent* event) diff --git a/app/streaming/input.h b/app/streaming/input.h index 06ed30ef..073c3cc1 100644 --- a/app/streaming/input.h +++ b/app/streaming/input.h @@ -8,10 +8,13 @@ struct GamepadState { SDL_GameController* controller; SDL_JoystickID jsId; + short index; + +#if !SDL_VERSION_ATLEAST(2, 0, 9) SDL_Haptic* haptic; int hapticMethod; int hapticEffectId; - short index; +#endif SDL_TimerID mouseEmulationTimer; uint32_t lastStartDownTime;