From 7e271fc53ebb9710ecc31343aef5bcaffb5cd8d8 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 11 Dec 2021 11:52:42 -0400 Subject: [PATCH] Use SDL's rumble APIs for Joystick:setVibration, when available. Fixes #1756 --- src/modules/joystick/sdl/Joystick.cpp | 31 +++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/modules/joystick/sdl/Joystick.cpp b/src/modules/joystick/sdl/Joystick.cpp index 2a8a85519..3c8b69067 100644 --- a/src/modules/joystick/sdl/Joystick.cpp +++ b/src/modules/joystick/sdl/Joystick.cpp @@ -392,6 +392,11 @@ bool Joystick::checkCreateHaptic() bool Joystick::isVibrationSupported() { +#if SDL_VERSION_ATLEAST(2, 0, 18) + if (isConnected() && SDL_JoystickHasRumble(joyhandle) == SDL_TRUE) + return true; +#endif + if (!checkCreateHaptic()) return false; @@ -442,8 +447,12 @@ bool Joystick::setVibration(float left, float right, float duration) if (left == 0.0f && right == 0.0f) return setVibration(); - if (!checkCreateHaptic()) + if (!isConnected()) + { + vibration.left = vibration.right = 0.0f; + vibration.endtime = SDL_HAPTIC_INFINITY; return false; + } Uint32 length = SDL_HAPTIC_INFINITY; if (duration >= 0.0f) @@ -453,10 +462,19 @@ bool Joystick::setVibration(float left, float right, float duration) } bool success = false; + +#if SDL_VERSION_ATLEAST(2, 0, 9) + if (SDL_JoystickRumble(joyhandle, (Uint16)(left * LOVE_UINT16_MAX), (Uint16)(right * LOVE_UINT16_MAX), length) == 0) + success = true; +#endif + + if (!success && !checkCreateHaptic()) + return false; + unsigned int features = SDL_HapticQuery(haptic); int axes = SDL_HapticNumAxes(haptic); - if ((features & SDL_HAPTIC_LEFTRIGHT) != 0) + if (!success && (features & SDL_HAPTIC_LEFTRIGHT) != 0) { memset(&vibration.effect, 0, sizeof(SDL_HapticEffect)); vibration.effect.type = SDL_HAPTIC_LEFTRIGHT; @@ -528,9 +546,14 @@ bool Joystick::setVibration(float left, float right, float duration) bool Joystick::setVibration() { - bool success = true; + bool success = false; - if (SDL_WasInit(SDL_INIT_HAPTIC) && haptic && SDL_HapticIndex(haptic) != -1) +#if SDL_VERSION_ATLEAST(2, 0, 9) + if (!success) + success = isConnected() && SDL_JoystickRumble(joyhandle, 0, 0, 0) == 0; +#endif + + if (!success && SDL_WasInit(SDL_INIT_HAPTIC) && haptic && SDL_HapticIndex(haptic) != -1) success = (SDL_HapticStopEffect(haptic, vibration.id) == 0); if (success)