From 743d5603d1e8ccda69f3205c9e4500b6d296877f Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sat, 15 Feb 2020 17:08:31 -0800 Subject: [PATCH] SDL: Refresh stale pointers after adding a joystick (fixes #1622) --- CHANGES | 1 + src/platform/sdl/sdl-events.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index a3bba7a0c..c22505937 100644 --- a/CHANGES +++ b/CHANGES @@ -42,6 +42,7 @@ Other fixes: - Qt: Fix non-GB build (fixes mgba.io/i/1664) - Qt: Fix pausing Qt Multimedia audio (fixes mgba.io/i/1643) - Qt: Fix invalid names for modifier keys (fixes mgba.io/i/525) + - SDL: Refresh stale pointers after adding a joystick (fixes mgba.io/i/1622) - Util: Fix crash reading invalid ELFs - VFS: Fix handle leak when double-mapping (fixes mgba.io/i/1659) Misc: diff --git a/src/platform/sdl/sdl-events.c b/src/platform/sdl/sdl-events.c index d29bfb378..a372119c6 100644 --- a/src/platform/sdl/sdl-events.c +++ b/src/platform/sdl/sdl-events.c @@ -351,6 +351,13 @@ void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration* if (!sdlJoystick) { continue; } + ssize_t joysticks[MAX_PLAYERS]; + size_t i; + // Pointers can get invalidated, so we'll need to refresh them + for (i = 0; i < events->playersAttached && i < MAX_PLAYERS; ++i) { + joysticks[i] = events->players[i]->joystick ? SDL_JoystickListIndex(&events->joysticks, events->players[i]->joystick) : SIZE_MAX; + events->players[i]->joystick = NULL; + } struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks); joystick->joystick = sdlJoystick; joystick->id = SDL_JoystickInstanceID(joystick->joystick); @@ -358,8 +365,12 @@ void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration* #if SDL_VERSION_ATLEAST(2, 0, 0) joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick); #endif + for (i = 0; i < events->playersAttached && i < MAX_PLAYERS; ++i) { + if (joysticks[i] != SIZE_MAX) { + events->players[i]->joystick = SDL_JoystickListGetPointer(&events->joysticks, joysticks[i]); + } + } - size_t i; #if SDL_VERSION_ATLEAST(2, 0, 0) char joystickName[34] = {0}; SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick->joystick), joystickName, sizeof(joystickName));