SDL: Fix handling of invalid gamepads (fixes #1239)

This commit is contained in:
Vicki Pfau 2018-12-09 18:33:52 -08:00
parent ef59199d64
commit 553fbdfd37
2 changed files with 32 additions and 12 deletions

View File

@ -131,6 +131,7 @@ Bugfixes:
- Core: Reroot timing list when (de)scheduling - Core: Reroot timing list when (de)scheduling
- GB Video: Changing LYC while LCDC off doesn't affect STAT (fixes mgba.io/i/1224) - GB Video: Changing LYC while LCDC off doesn't affect STAT (fixes mgba.io/i/1224)
- GBA I/O: SOUNDCNT_HI is readable when sound is off - GBA I/O: SOUNDCNT_HI is readable when sound is off
- SDL: Fix handling of invalid gamepads (fixes mgba.io/i/1239)
Misc: Misc:
- mGUI: Add SGB border configuration option - mGUI: Add SGB border configuration option
- mGUI: Add support for different settings types - mGUI: Add support for different settings types

View File

@ -64,8 +64,12 @@ bool mSDLInitEvents(struct mSDLEvents* context) {
if (!SDL_JoystickListSize(&context->joysticks)) { if (!SDL_JoystickListSize(&context->joysticks)) {
int i; int i;
for (i = 0; i < nJoysticks; ++i) { for (i = 0; i < nJoysticks; ++i) {
SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
if (!sdlJoystick) {
continue;
}
struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&context->joysticks); struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&context->joysticks);
joystick->joystick = SDL_JoystickOpen(i); joystick->joystick = sdlJoystick;
joystick->index = SDL_JoystickListSize(&context->joysticks) - 1; joystick->index = SDL_JoystickListSize(&context->joysticks) - 1;
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
joystick->id = SDL_JoystickInstanceID(joystick->joystick); joystick->id = SDL_JoystickInstanceID(joystick->joystick);
@ -203,6 +207,9 @@ bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
#else #else
joystickName = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick)); joystickName = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick));
#endif #endif
if (!joystickName) {
continue;
}
if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) { if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
index = i; index = i;
break; break;
@ -253,6 +260,9 @@ void mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration
#else #else
const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick)); const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
#endif #endif
if (!name) {
return;
}
mInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name); mInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name);
const char* value; const char* value;
@ -304,6 +314,9 @@ void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration
#else #else
const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick)); const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
#endif #endif
if (!name) {
return;
}
char value[16]; char value[16];
snprintf(value, sizeof(value), "%i", context->rotation.axisX); snprintf(value, sizeof(value), "%i", context->rotation.axisX);
mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name); mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name);
@ -332,8 +345,12 @@ void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration*
SDL_Event event; SDL_Event event;
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) { while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) {
if (event.type == SDL_JOYDEVICEADDED) { if (event.type == SDL_JOYDEVICEADDED) {
SDL_Joystick* sdlJoystick = SDL_JoystickOpen(event.jdevice.which);
if (!sdlJoystick) {
continue;
}
struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks); struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks);
joystick->joystick = SDL_JoystickOpen(event.jdevice.which); joystick->joystick = sdlJoystick;
joystick->id = SDL_JoystickInstanceID(joystick->joystick); joystick->id = SDL_JoystickInstanceID(joystick->joystick);
joystick->index = SDL_JoystickListSize(&events->joysticks) - 1; joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
@ -347,16 +364,18 @@ void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration*
joystickName = SDL_JoystickName(SDL_JoystickIndex(joystick->joystick)); joystickName = SDL_JoystickName(SDL_JoystickIndex(joystick->joystick));
#endif #endif
size_t i; size_t i;
for (i = 0; (int) i < events->playersAttached; ++i) { if (joystickName) {
if (events->players[i]->joystick) { for (i = 0; (int) i < events->playersAttached; ++i) {
continue; if (events->players[i]->joystick) {
} continue;
if (events->preferredJoysticks[i] && strcmp(events->preferredJoysticks[i], joystickName) == 0) { }
events->players[i]->joystick = joystick; if (events->preferredJoysticks[i] && strcmp(events->preferredJoysticks[i], joystickName) == 0) {
if (config) { events->players[i]->joystick = joystick;
mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName); if (config) {
mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName);
}
return;
} }
return;
} }
} }
for (i = 0; (int) i < events->playersAttached; ++i) { for (i = 0; (int) i < events->playersAttached; ++i) {
@ -364,7 +383,7 @@ void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration*
continue; continue;
} }
events->players[i]->joystick = joystick; events->players[i]->joystick = joystick;
if (config) { if (config && joystickName) {
mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName); mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName);
} }
break; break;