mirror of
https://git.do.srb2.org/STJr/SRB2.git
synced 2024-12-11 05:11:25 +00:00
Properly handle unstable device indexes for hotplug
This commit is contained in:
parent
44d6a1d236
commit
6b9fe87b60
3 changed files with 56 additions and 10 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue