Don't override an already-active controller

* Menu improvements to tell the user that they can't set a controller if it's already active
This commit is contained in:
mazmazz 2018-12-14 05:02:41 -05:00
parent 3b6de810e1
commit e548f6f139
3 changed files with 142 additions and 78 deletions

View file

@ -6777,7 +6777,9 @@ void M_SetupJoystickMenu(INT32 choice)
strcpy(joystickInfo[i], "None"); strcpy(joystickInfo[i], "None");
#ifdef JOYSTICK_HOTPLUG // Hotplugging breaks if this block is run
// Because the cvar is set to 0, which disables controllers for that player
#if 0 // #ifdef JOYSTICK_HOTPLUG
if (0 == cv_usejoystick.value) if (0 == cv_usejoystick.value)
CV_SetValue(&cv_usejoystick, 0); CV_SetValue(&cv_usejoystick, 0);
if (0 == cv_usejoystick2.value) if (0 == cv_usejoystick2.value)
@ -6826,10 +6828,60 @@ static void M_Setup2PJoystickMenu(INT32 choice)
static void M_AssignJoystick(INT32 choice) static void M_AssignJoystick(INT32 choice)
{ {
#ifdef JOYSTICK_HOTPLUG
INT32 oldchoice;
if (choice > I_NumJoys())
return;
if (setupcontrols_secondaryplayer)
{
oldchoice = cv_usejoystick2.value;
CV_SetValue(&cv_usejoystick2, choice);
// Just in case last-minute changes were made to cv_usejoystick.value,
// update the string too
CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value);
if (oldchoice != choice)
{
if (choice && oldchoice > I_NumJoys()) // if we did not select "None", we likely selected a used device
CV_SetValue(&cv_usejoystick2, oldchoice);
if (oldchoice == cv_usejoystick2.value)
M_StartMessage("This joystick is used by another\n"
"player. Reset the joystick\n"
"for that player first.\n\n"
"(Press a key)\n", NULL, MM_NOTHING);
}
}
else
{
oldchoice = cv_usejoystick.value;
CV_SetValue(&cv_usejoystick, choice);
// Just in case last-minute changes were made to cv_usejoystick.value,
// update the string too
CV_SetValue(&cv_usejoystick, cv_usejoystick.value);
if (oldchoice != choice)
{
if (choice && oldchoice > I_NumJoys()) // if we did not select "None", we likely selected a used device
CV_SetValue(&cv_usejoystick, oldchoice);
if (oldchoice == cv_usejoystick.value)
M_StartMessage("This joystick is used by another\n"
"player. Reset the joystick\n"
"for that player first.\n\n"
"(Press a key)\n", NULL, MM_NOTHING);
}
}
#else
if (setupcontrols_secondaryplayer) if (setupcontrols_secondaryplayer)
CV_SetValue(&cv_usejoystick2, choice); CV_SetValue(&cv_usejoystick2, choice);
else else
CV_SetValue(&cv_usejoystick, choice); CV_SetValue(&cv_usejoystick, choice);
#endif
} }
// ============= // =============

View file

@ -1392,6 +1392,8 @@ static int joy_open2(int joyindex)
// //
void I_InitJoystick(void) void I_InitJoystick(void)
{ {
SDL_Joystick *newjoy = NULL;
//I_ShutdownJoystick(); //I_ShutdownJoystick();
if (M_CheckParm("-nojoy")) if (M_CheckParm("-nojoy"))
return; return;
@ -1407,20 +1409,17 @@ void I_InitJoystick(void)
} }
} }
if (cv_usejoystick.value && joy_open(cv_usejoystick.value) != -1) if (cv_usejoystick.value)
newjoy = SDL_JoystickOpen(cv_usejoystick.value-1);
if (newjoy && JoyInfo2.dev == newjoy) // don't override an active device
cv_usejoystick.value = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1;
else if (newjoy && joy_open(cv_usejoystick.value) != -1)
{ {
// SDL's device indexes are unstable, so cv_usejoystick may not match // SDL's device indexes are unstable, so cv_usejoystick may not match
// the actual device index. So let's cheat a bit and find the device's current index. // the actual device index. So let's cheat a bit and find the device's current index.
JoyInfo.oldjoy = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; JoyInfo.oldjoy = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1;
joystick_started = 1; joystick_started = 1;
// If another joystick occupied this device, deactivate that joystick
if (JoyInfo2.dev == JoyInfo.dev)
{
CONS_Debug(DBG_GAMELOGIC, "Joystick2 was set to the same device; disabling...\n");
cv_usejoystick2.value = 0;
I_InitJoystick2();
}
} }
else else
{ {
@ -1429,10 +1428,15 @@ void I_InitJoystick(void)
cv_usejoystick.value = 0; cv_usejoystick.value = 0;
joystick_started = 0; joystick_started = 0;
} }
if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy)
SDL_JoystickClose(newjoy);
} }
void I_InitJoystick2(void) void I_InitJoystick2(void)
{ {
SDL_Joystick *newjoy = NULL;
//I_ShutdownJoystick2(); //I_ShutdownJoystick2();
if (M_CheckParm("-nojoy")) if (M_CheckParm("-nojoy"))
return; return;
@ -1448,20 +1452,17 @@ void I_InitJoystick2(void)
} }
} }
if (cv_usejoystick2.value && joy_open2(cv_usejoystick2.value) != -1) if (cv_usejoystick2.value)
newjoy = SDL_JoystickOpen(cv_usejoystick2.value-1);
if (newjoy && JoyInfo.dev == newjoy) // don't override an active device
cv_usejoystick2.value = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1;
else if (newjoy && joy_open2(cv_usejoystick2.value) != -1)
{ {
// SDL's device indexes are unstable, so cv_usejoystick2 may not match // SDL's device indexes are unstable, so cv_usejoystick may not match
// the actual device index. So let's cheat a bit and find the device's current index. // the actual device index. So let's cheat a bit and find the device's current index.
JoyInfo2.oldjoy = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; JoyInfo2.oldjoy = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1;
joystick2_started = 1; joystick2_started = 1;
// If another joystick occupied this device, deactivate that joystick
if (JoyInfo.dev == JoyInfo2.dev)
{
CONS_Debug(DBG_GAMELOGIC, "Joystick1 was set to the same device; disabling...\n");
cv_usejoystick.value = 0;
I_InitJoystick();
}
} }
else else
{ {
@ -1471,6 +1472,8 @@ void I_InitJoystick2(void)
joystick2_started = 0; joystick2_started = 0;
} }
if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy)
SDL_JoystickClose(newjoy);
} }
static void I_ShutdownInput(void) static void I_ShutdownInput(void)

View file

@ -883,6 +883,9 @@ void I_GetEvent(void)
Impl_HandleJoystickButtonEvent(evt.jbutton, evt.type); Impl_HandleJoystickButtonEvent(evt.jbutton, evt.type);
break; break;
case SDL_JOYDEVICEADDED: case SDL_JOYDEVICEADDED:
{
SDL_Joystick *newjoy = SDL_JoystickOpen(evt.jdevice.which);
CONS_Debug(DBG_GAMELOGIC, "Joystick device index %d added\n", evt.jdevice.which + 1); CONS_Debug(DBG_GAMELOGIC, "Joystick device index %d added\n", evt.jdevice.which + 1);
// Because SDL's device index is unstable, we're going to cheat here a bit: // Because SDL's device index is unstable, we're going to cheat here a bit:
@ -891,7 +894,8 @@ void I_GetEvent(void)
// 2. Set OTHERS' cv_usejoystickX.value to THEIR new device index, because it likely changed // 2. 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) // * 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! // * BUT: If that default index is being occupied, use ANOTHER cv_usejoystick's default value!
if (!JoyInfo.dev || !SDL_JoystickGetAttached(JoyInfo.dev)) if (newjoy && (!JoyInfo.dev || !SDL_JoystickGetAttached(JoyInfo.dev))
&& JoyInfo2.dev != newjoy) // don't override a currently active device
{ {
cv_usejoystick.value = evt.jdevice.which + 1; cv_usejoystick.value = evt.jdevice.which + 1;
@ -906,7 +910,8 @@ void I_GetEvent(void)
else // we tried... else // we tried...
cv_usejoystick2.value = 0; cv_usejoystick2.value = 0;
} }
else if (!JoyInfo2.dev || !SDL_JoystickGetAttached(JoyInfo2.dev)) else if (newjoy && (!JoyInfo2.dev || !SDL_JoystickGetAttached(JoyInfo2.dev))
&& JoyInfo.dev != newjoy) // don't override a currently active device
{ {
cv_usejoystick2.value = evt.jdevice.which + 1; cv_usejoystick2.value = evt.jdevice.which + 1;
@ -946,6 +951,10 @@ void I_GetEvent(void)
// update the menu // update the menu
if (currentMenu == &OP_JoystickSetDef) if (currentMenu == &OP_JoystickSetDef)
M_SetupJoystickMenu(0); M_SetupJoystickMenu(0);
if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy)
SDL_JoystickClose(newjoy);
}
break; break;
case SDL_JOYDEVICEREMOVED: case SDL_JOYDEVICEREMOVED:
if (JoyInfo.dev && !SDL_JoystickGetAttached(JoyInfo.dev)) if (JoyInfo.dev && !SDL_JoystickGetAttached(JoyInfo.dev))