SDL: Refresh stale pointers after adding a joystick (fixes #1622)
Vicki Pfau vi@endrift.com
Sat, 15 Feb 2020 17:08:31 -0800
2 files changed,
13 insertions(+),
1 deletions(-)
M
CHANGES
→
CHANGES
@@ -42,6 +42,7 @@ - Qt: Fix GIF view not allowing manual filename entry
- 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:
M
src/platform/sdl/sdl-events.c
→
src/platform/sdl/sdl-events.c
@@ -351,6 +351,13 @@ SDL_Joystick* sdlJoystick = SDL_JoystickOpen(event.jdevice.which);
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 @@ joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
#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));