From 07cb7c10a15b95387431bcb3a1ae77cfd432707b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 6 Nov 2023 17:10:03 -0800 Subject: [PATCH] Fixed connecting and disconnecting real-joysticks closing virtual joysticks in Emscripten (thanks David!) Emscripten was using its own, private integer in order to allocate new SDL_JoystickIDs. SDL keeps a similar integer for allocating joystick-ids, one which is shared across multiple joystick backends. SDL 2.0.13 introduces a new joystick-backend, a Virtual joystick backend, which allows for software-driven joysticks, and which is designed to sit alongside joystick-backends that provide access to physical joysticks. The Emscripten and the Virtual backends were, at times, getting allocated the same SDL_JoystickIDs, if and when both backends were used simultaneously. This could happen if, for example, an application was using a virtual joystick in order to drive a touch-screen based joystick, while also supporting physical joysticks through the Emscripten backend. When two joysticks end up with the same SDL_JoystickID, conflicts can occur. For example, disconnecting a physical joystick with the same SDL_JoystickID as a virtual one, can lead to the virtual joystick being closed, inadvertently. This fix makes the Emscripten backend use SDL's cross-joystick-backend integer counter, which is shared among joystick backends, for allocating new SDL_JoystickIDs, rather than a private, Emscripten-specific counter. Fixes https://github.com/libsdl-org/SDL/issues/3647 --- src/joystick/emscripten/SDL_sysjoystick.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/joystick/emscripten/SDL_sysjoystick.c b/src/joystick/emscripten/SDL_sysjoystick.c index 902c91d00..4f9dffaaa 100644 --- a/src/joystick/emscripten/SDL_sysjoystick.c +++ b/src/joystick/emscripten/SDL_sysjoystick.c @@ -33,7 +33,6 @@ static SDL_joylist_item *JoystickByIndex(int index); static SDL_joylist_item *SDL_joylist = NULL; static SDL_joylist_item *SDL_joylist_tail = NULL; static int numjoysticks = 0; -static int instance_counter = 0; static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData) { @@ -68,7 +67,7 @@ static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamep item->naxes = gamepadEvent->numAxes; item->nbuttons = gamepadEvent->numButtons; - item->device_instance = instance_counter++; + item->device_instance = SDL_GetNextJoystickInstanceID(); item->timestamp = gamepadEvent->timestamp; @@ -164,7 +163,6 @@ static void EMSCRIPTEN_JoystickQuit(void) SDL_joylist = SDL_joylist_tail = NULL; numjoysticks = 0; - instance_counter = 0; emscripten_set_gamepadconnected_callback(NULL, 0, NULL); emscripten_set_gamepaddisconnected_callback(NULL, 0, NULL);