Lock joysticks when they are connected/disconnected on emscripten

Fixes https://github.com/libsdl-org/SDL/issues/11499
This commit is contained in:
Sam Lantinga
2025-01-01 13:51:47 -08:00
parent c68ed04a06
commit 0281071243

View File

@@ -36,17 +36,18 @@ static int numjoysticks = 0;
static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData) static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
{ {
SDL_joylist_item *item;
int i; int i;
SDL_joylist_item *item; SDL_LockJoysticks();
if (JoystickByIndex(gamepadEvent->index) != NULL) { if (JoystickByIndex(gamepadEvent->index) != NULL) {
return 1; goto done;
} }
item = (SDL_joylist_item *)SDL_malloc(sizeof(SDL_joylist_item)); item = (SDL_joylist_item *)SDL_malloc(sizeof(SDL_joylist_item));
if (!item) { if (!item) {
return 1; goto done;
} }
SDL_zerop(item); SDL_zerop(item);
@@ -55,14 +56,14 @@ static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamep
item->name = SDL_CreateJoystickName(0, 0, NULL, gamepadEvent->id); item->name = SDL_CreateJoystickName(0, 0, NULL, gamepadEvent->id);
if (!item->name) { if (!item->name) {
SDL_free(item); SDL_free(item);
return 1; goto done;
} }
item->mapping = SDL_strdup(gamepadEvent->mapping); item->mapping = SDL_strdup(gamepadEvent->mapping);
if (!item->mapping) { if (!item->mapping) {
SDL_free(item->name); SDL_free(item->name);
SDL_free(item); SDL_free(item);
return 1; goto done;
} }
item->naxes = gamepadEvent->numAxes; item->naxes = gamepadEvent->numAxes;
@@ -98,6 +99,9 @@ static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamep
SDL_Log("Added joystick with index %d", item->index); SDL_Log("Added joystick with index %d", item->index);
#endif #endif
done:
SDL_UnlockJoysticks();
return 1; return 1;
} }
@@ -106,6 +110,8 @@ static EM_BOOL Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGa
SDL_joylist_item *item = SDL_joylist; SDL_joylist_item *item = SDL_joylist;
SDL_joylist_item *prev = NULL; SDL_joylist_item *prev = NULL;
SDL_LockJoysticks();
while (item) { while (item) {
if (item->index == gamepadEvent->index) { if (item->index == gamepadEvent->index) {
break; break;
@@ -115,7 +121,7 @@ static EM_BOOL Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGa
} }
if (!item) { if (!item) {
return 1; goto done;
} }
if (item->joystick) { if (item->joystick) {
@@ -143,6 +149,10 @@ static EM_BOOL Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGa
SDL_free(item->name); SDL_free(item->name);
SDL_free(item->mapping); SDL_free(item->mapping);
SDL_free(item); SDL_free(item);
done:
SDL_UnlockJoysticks();
return 1; return 1;
} }