From 0a924b185d2f459a2efbe54484394a17b97f1632 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 29 Jul 2024 12:48:40 -0700 Subject: [PATCH] Fixed crash with joystick rumble after disconnection This prevents continuing a rumble after the first one fails, and fixes a long standing crash issue if rumble is started immediately before the controller is disconnected. Thanks to @AntTheAlchemist for the key bug report that showed what was happening here. Fixes https://github.com/libsdl-org/SDL/issues/10422 --- src/joystick/SDL_joystick.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index 8564f94e1f..419ef5a7fd 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -1733,7 +1733,14 @@ int SDL_RumbleJoystick(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint retval = 0; } else { retval = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble); - joystick->rumble_resend = SDL_GetTicks() + SDL_RUMBLE_RESEND_MS; + if (retval == 0) { + joystick->rumble_resend = SDL_GetTicks() + SDL_RUMBLE_RESEND_MS; + if (joystick->rumble_resend == 0) { + joystick->rumble_resend = 1; + } + } else { + joystick->rumble_resend = 0; + } } if (retval == 0) { @@ -2408,12 +2415,14 @@ void SDL_UpdateJoysticks(void) #endif /* SDL_JOYSTICK_HIDAPI */ for (joystick = SDL_joysticks; joystick; joystick = joystick->next) { - if (joystick->attached) { - joystick->driver->Update(joystick); + if (!joystick->attached) { + continue; + } - if (joystick->delayed_guide_button) { - SDL_GamepadHandleDelayedGuideButton(joystick); - } + joystick->driver->Update(joystick); + + if (joystick->delayed_guide_button) { + SDL_GamepadHandleDelayedGuideButton(joystick); } now = SDL_GetTicks();