SDL: Refresh stale pointers after adding a joystick (fixes #1622)

This commit is contained in:
Vicki Pfau 2020-02-15 17:08:31 -08:00
parent 52a4cbcb81
commit 743d5603d1
2 changed files with 13 additions and 1 deletions

View File

@ -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:

View File

@ -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));