From 6b9fe87b60de3da7ef2f2b4e3553fb34b1b557ad Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 01:45:18 -0500 Subject: [PATCH] Properly handle unstable device indexes for hotplug --- src/sdl/i_system.c | 27 +++++++++++++++++++++------ src/sdl/i_video.c | 36 ++++++++++++++++++++++++++++++++---- src/sdl/sdlmain.h | 3 +++ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 6ac41de16..7cbaf6d35 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -828,6 +828,23 @@ void I_JoyScale2(void) JoyInfo2.scale = Joystick2.bGamepadStyle?1:cv_joyscale2.value; } +// Cheat to get the device index for a joystick handle +INT32 I_GetJoystickDeviceIndex(SDL_Joystick *dev) +{ + INT32 i, count = SDL_NumJoysticks(); + + for (i = 0; dev && i < count; i++) + { + SDL_Joystick *test = SDL_JoystickOpen(i); + if (test && test == dev) + return i; + else if (JoyInfo.dev != test && JoyInfo2.dev != test) + SDL_JoystickClose(test); + } + + return -1; +} + /** \brief Joystick 1 buttons states */ static UINT64 lastjoybuttons = 0; @@ -1393,9 +1410,8 @@ void I_InitJoystick(void) if (cv_usejoystick.value && joy_open(cv_usejoystick.value) != -1) { // SDL's device indexes are unstable, so cv_usejoystick may not match - // the actual device index. So let's cheat a bit and use the instance ID. - // oldjoy's exact value doesn't matter, because we use it like a boolean - JoyInfo.oldjoy = SDL_JoystickInstanceID(JoyInfo.dev) + 1; + // the actual device index. So let's cheat a bit and find the device's current index. + JoyInfo.oldjoy = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; joystick_started = 1; } else @@ -1426,9 +1442,8 @@ void I_InitJoystick2(void) if (cv_usejoystick2.value && joy_open2(cv_usejoystick2.value) != -1) { // SDL's device indexes are unstable, so cv_usejoystick2 may not match - // the actual device index. So let's cheat a bit and use the instance ID. - // oldjoy's exact value doesn't matter, because we use it like a boolean - JoyInfo2.oldjoy = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; + // the actual device index. So let's cheat a bit and find the device's current index. + JoyInfo2.oldjoy = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; joystick2_started = 1; } else diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 6fc8f5779..f6f193038 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -889,19 +889,33 @@ void I_GetEvent(void) // For the first joystick setting that is NOT active: // Set cv_usejoystickX.value to the new device index (this does not change what is written to config.cfg) // Set OTHERS' cv_usejoystickX.value to THEIR new device index, because it likely changed + // If device doesn't exist, switch cv_usejoystick back to default value (.string) + // BUT: If that default index is being occupied, use ANOTHER cv_usejoystick's default value! if (!JoyInfo.dev || !SDL_JoystickGetAttached(JoyInfo.dev)) { cv_usejoystick.value = evt.jdevice.which + 1; if (JoyInfo2.dev) - cv_usejoystick2.value = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; + cv_usejoystick2.value = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; + else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy) + cv_usejoystick2.value = atoi(cv_usejoystick2.string); + else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy) + cv_usejoystick2.value = atoi(cv_usejoystick.string); + else // we tried... + cv_usejoystick2.value = 0; } else if (!JoyInfo2.dev || !SDL_JoystickGetAttached(JoyInfo2.dev)) { cv_usejoystick2.value = evt.jdevice.which + 1; if (JoyInfo.dev) - cv_usejoystick.value = SDL_JoystickInstanceID(JoyInfo.dev) + 1; + cv_usejoystick.value = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; + else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy) + cv_usejoystick.value = atoi(cv_usejoystick.string); + else if (atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy) + cv_usejoystick.value = atoi(cv_usejoystick2.string); + else // we tried... + cv_usejoystick.value = 0; } // If an active joystick's index has changed, these will just @@ -930,11 +944,25 @@ void I_GetEvent(void) } // Update the device indexes, because they likely changed + // If device doesn't exist, switch cv_usejoystick back to default value (.string) + // BUT: If that default index is being occupied, use ANOTHER cv_usejoystick's default value! if (JoyInfo.dev) - JoyInfo.oldjoy = SDL_JoystickInstanceID(JoyInfo.dev) + 1; + cv_usejoystick.value = JoyInfo.oldjoy = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; + else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy) + cv_usejoystick.value = atoi(cv_usejoystick.string); + else if (atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy) + cv_usejoystick.value = atoi(cv_usejoystick2.string); + else // we tried... + cv_usejoystick.value = 0; if (JoyInfo2.dev) - JoyInfo2.oldjoy = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; + cv_usejoystick2.value = JoyInfo2.oldjoy = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; + else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy) + cv_usejoystick2.value = atoi(cv_usejoystick2.string); + else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy) + cv_usejoystick2.value = atoi(cv_usejoystick.string); + else // we tried... + cv_usejoystick2.value = 0; CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); diff --git a/src/sdl/sdlmain.h b/src/sdl/sdlmain.h index 4acbce209..b858e1f6f 100644 --- a/src/sdl/sdlmain.h +++ b/src/sdl/sdlmain.h @@ -71,6 +71,9 @@ extern SDLJoyInfo_t JoyInfo2; void I_ShutdownJoystick(void); void I_ShutdownJoystick2(void); +// Cheat to get the device index for a joystick handle +INT32 I_GetJoystickDeviceIndex(SDL_Joystick *dev); + void I_GetConsoleEvents(void); void SDLforceUngrabMouse(void);