From 6694b7d679dfa33a307a935788d289980c0eb615 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Thu, 6 Dec 2018 10:54:58 -0500 Subject: [PATCH 01/19] Controller hotplugging by always keeping joy subsystem on (thanks WOLFS) (with log messages) --- src/m_menu.c | 2 +- src/m_menu.h | 5 +- src/sdl/i_system.c | 213 ++++++++++++++++++++++----------------------- src/sdl/i_video.c | 35 ++++++++ src/sdl/sdlmain.h | 4 + 5 files changed, 148 insertions(+), 111 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 1e1b1e696..a4c62c0f3 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6761,7 +6761,7 @@ static void M_DrawJoystick(void) } } -static void M_SetupJoystickMenu(INT32 choice) +void M_SetupJoystickMenu(INT32 choice) { INT32 i = 0; const char *joyNA = "Unavailable"; diff --git a/src/m_menu.h b/src/m_menu.h index de76a2710..3066a261c 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -69,7 +69,6 @@ void M_QuitResponse(INT32 ch); // Determines whether to show a level in the list boolean M_CanShowLevelInList(INT32 mapnum, INT32 gt); - // flags for items in the menu // menu handle (what we do when key is pressed #define IT_TYPE 14 // (2+4+8) @@ -171,6 +170,10 @@ extern menu_t *currentMenu; extern menu_t MainDef; extern menu_t SP_LoadDef; +// Call upon joystick hotplug +void M_SetupJoystickMenu(INT32 choice); +extern menu_t OP_JoystickSetDef; + // Stuff for customizing the player select screen typedef struct { diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 50c3018aa..b18ea9571 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -843,7 +843,7 @@ static UINT64 lastjoyhats = 0; */ -static void I_ShutdownJoystick(void) +void I_ShutdownJoystick(void) { INT32 i; event_t event; @@ -877,14 +877,8 @@ static void I_ShutdownJoystick(void) joystick_started = 0; JoyReset(&JoyInfo); - if (!joystick_started && !joystick2_started && SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) - { - SDL_QuitSubSystem(SDL_INIT_JOYSTICK); - if (cv_usejoystick.value == 0) - { - I_OutputMsg("I_Joystick: SDL's Joystick system has been shutdown\n"); - } - } + + // don't shut down the subsystem here, because hotplugging } void I_GetJoystickEvents(void) @@ -1031,37 +1025,20 @@ static int joy_open(const char *fname) int num_joy = 0; int i; - if (joystick_started == 0 && joystick2_started == 0) + if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { - if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) - { - CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); - return -1; - } - else - { - num_joy = SDL_NumJoysticks(); - } + CONS_Printf(M_GetText("Joystick subsystem not started\n")); + return -1; + } - if (num_joy < joyindex) - { - CONS_Printf(M_GetText("Cannot use joystick #%d/(%s), it doesn't exist\n"),joyindex,fname); - for (i = 0; i < num_joy; i++) - CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); - I_ShutdownJoystick(); - return -1; - } - } - else - { - JoyReset(&JoyInfo); - //I_ShutdownJoystick(); - //joy_open(fname); - } + JoyReset(&JoyInfo); + + if (joyindex <= 0) + return 0; num_joy = SDL_NumJoysticks(); - if (joyindex <= 0 || num_joy == 0 || JoyInfo.oldjoy == joyindex) + if (num_joy == 0 || JoyInfo.oldjoy == joyindex) { // I_OutputMsg("Unable to use that joystick #(%s), non-number\n",fname); if (num_joy != 0) @@ -1069,10 +1046,20 @@ static int joy_open(const char *fname) CONS_Printf(M_GetText("Found %d joysticks on this system\n"), num_joy); for (i = 0; i < num_joy; i++) CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); + + if (num_joy < joyindex) + { + CONS_Printf(M_GetText("Cannot use joystick #%d/(%s), it doesn't exist\n"),joyindex,fname); + for (i = 0; i < num_joy; i++) + CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); + return 0; + } } else + { CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); - if (joyindex <= 0 || num_joy == 0) return 0; + return 0; + } } JoyInfo.dev = SDL_JoystickOpen(joyindex-1); @@ -1080,7 +1067,6 @@ static int joy_open(const char *fname) if (JoyInfo.dev == NULL) { CONS_Printf(M_GetText("Couldn't open joystick: %s\n"), SDL_GetError()); - I_ShutdownJoystick(); return -1; } else @@ -1092,7 +1078,6 @@ static int joy_open(const char *fname) /* if (joyaxes<2) { I_OutputMsg("Not enought axes?\n"); - I_ShutdownJoystick(); return 0; }*/ @@ -1127,7 +1112,7 @@ static UINT64 lastjoy2hats = 0; \return void */ -static void I_ShutdownJoystick2(void) +void I_ShutdownJoystick2(void) { INT32 i; event_t event; @@ -1161,14 +1146,8 @@ static void I_ShutdownJoystick2(void) joystick2_started = 0; JoyReset(&JoyInfo2); - if (!joystick_started && !joystick2_started && SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) - { - SDL_QuitSubSystem(SDL_INIT_JOYSTICK); - if (cv_usejoystick2.value == 0) - { - DEBFILE("I_Joystick2: SDL's Joystick system has been shutdown\n"); - } - } + + // don't shut down the subsystem here, because hotplugging } void I_GetJoystick2Events(void) @@ -1317,35 +1296,20 @@ static int joy_open2(const char *fname) int num_joy = 0; int i; - if (joystick_started == 0 && joystick2_started == 0) + if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { - if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) - { - CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); - return -1; - } - else - num_joy = SDL_NumJoysticks(); + CONS_Printf(M_GetText("Joystick subsystem not started\n")); + return -1; + } - if (num_joy < joyindex) - { - CONS_Printf(M_GetText("Cannot use joystick #%d/(%s), it doesn't exist\n"),joyindex,fname); - for (i = 0; i < num_joy; i++) - CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); - I_ShutdownJoystick2(); - return -1; - } - } - else - { - JoyReset(&JoyInfo2); - //I_ShutdownJoystick(); - //joy_open(fname); - } + JoyReset(&JoyInfo2); + + if (joyindex <= 0) + return 0; num_joy = SDL_NumJoysticks(); - if (joyindex <= 0 || num_joy == 0 || JoyInfo2.oldjoy == joyindex) + if (num_joy == 0 || JoyInfo2.oldjoy == joyindex) { // I_OutputMsg("Unable to use that joystick #(%s), non-number\n",fname); if (num_joy != 0) @@ -1353,18 +1317,27 @@ static int joy_open2(const char *fname) CONS_Printf(M_GetText("Found %d joysticks on this system\n"), num_joy); for (i = 0; i < num_joy; i++) CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); + + if (num_joy < joyindex) + { + CONS_Printf(M_GetText("Cannot use joystick #%d/(%s), it doesn't exist\n"),joyindex,fname); + for (i = 0; i < num_joy; i++) + CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); + return 0; + } } else + { CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); - if (joyindex <= 0 || num_joy == 0) return 0; + return 0; + } } JoyInfo2.dev = SDL_JoystickOpen(joyindex-1); - if (!JoyInfo2.dev) + if (JoyInfo2.dev == NULL) { CONS_Printf(M_GetText("Couldn't open joystick2: %s\n"), SDL_GetError()); - I_ShutdownJoystick2(); return -1; } else @@ -1373,10 +1346,9 @@ static int joy_open2(const char *fname) JoyInfo2.axises = SDL_JoystickNumAxes(JoyInfo2.dev); if (JoyInfo2.axises > JOYAXISSET*2) JoyInfo2.axises = JOYAXISSET*2; -/* if (joyaxes < 2) +/* if (joyaxes<2) { I_OutputMsg("Not enought axes?\n"); - I_ShutdownJoystick2(); return 0; }*/ @@ -1401,57 +1373,89 @@ static int joy_open2(const char *fname) // void I_InitJoystick(void) { - I_ShutdownJoystick(); - SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); - if (!strcmp(cv_usejoystick.string, "0") || M_CheckParm("-nojoy")) + //I_ShutdownJoystick(); + if (M_CheckParm("-nojoy")) return; - if (joy_open(cv_usejoystick.string) != -1) + + if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) + { + CONS_Printf("Initing joy system\n"); + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) + { + CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); + return; + } + else + SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); + } + + if (strcmp(cv_usejoystick.string, "0") && joy_open(cv_usejoystick.string) != -1) + { JoyInfo.oldjoy = atoi(cv_usejoystick.string); + joystick_started = 1; + } else { + if (JoyInfo.oldjoy) + I_ShutdownJoystick(); cv_usejoystick.value = 0; - return; + joystick_started = 0; } - joystick_started = 1; } void I_InitJoystick2(void) { - I_ShutdownJoystick2(); - SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); - if (!strcmp(cv_usejoystick2.string, "0") || M_CheckParm("-nojoy")) + //I_ShutdownJoystick2(); + if (M_CheckParm("-nojoy")) return; - if (joy_open2(cv_usejoystick2.string) != -1) + + if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) + { + CONS_Printf("Initing joy system\n"); + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) + { + CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); + return; + } + else + SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); + } + + if (strcmp(cv_usejoystick2.string, "0") && joy_open2(cv_usejoystick2.string) != -1) + { JoyInfo2.oldjoy = atoi(cv_usejoystick2.string); + joystick2_started = 1; + } else { + if (JoyInfo2.oldjoy) + I_ShutdownJoystick2(); cv_usejoystick2.value = 0; - return; + joystick2_started = 0; } - joystick2_started = 1; + } static void I_ShutdownInput(void) { + // Yes, the name is misleading: these send neutral events to + // clean up the unplugged joystick's input + // Note these methods are internal to this file, not called elsewhere. + I_ShutdownJoystick(); + I_ShutdownJoystick2(); + if (SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) { - JoyReset(&JoyInfo); - JoyReset(&JoyInfo2); + CONS_Printf("Shutting down joy system\n"); SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + I_OutputMsg("I_Joystick: SDL's Joystick system has been shutdown\n"); } - } INT32 I_NumJoys(void) { INT32 numjoy = 0; - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) != -1) - numjoy = SDL_NumJoysticks(); - SDL_QuitSubSystem(SDL_INIT_JOYSTICK); - } - else + if (SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) numjoy = SDL_NumJoysticks(); return numjoy; } @@ -1461,18 +1465,9 @@ static char joyname[255]; // MAX_PATH; joystick name is straight from the driver const char *I_GetJoyName(INT32 joyindex) { const char *tempname = NULL; + joyname[0] = 0; joyindex--; //SDL's Joystick System starts at 0, not 1 - if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) - { - if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) != -1) - { - tempname = SDL_JoystickNameForIndex(joyindex); - if (tempname) - strncpy(joyname, tempname, 255); - } - SDL_QuitSubSystem(SDL_INIT_JOYSTICK); - } - else + if (SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) { tempname = SDL_JoystickNameForIndex(joyindex); if (tempname) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 2c199c2d0..835ba1660 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -116,6 +116,9 @@ static INT32 firstEntry = 0; // Total mouse motion X/Y offsets static INT32 mousemovex = 0, mousemovey = 0; +// Keep track of joy unplugged count +static INT32 joyunplugcount = 0; + // SDL vars static SDL_Surface *vidSurface = NULL; static SDL_Surface *bufSurface = NULL; @@ -882,6 +885,38 @@ void I_GetEvent(void) case SDL_JOYBUTTONDOWN: Impl_HandleJoystickButtonEvent(evt.jbutton, evt.type); break; + case SDL_JOYDEVICEADDED: + CONS_Printf("Joy device %d added\n", evt.jdevice.which); + + // recounts hotplugged joysticks + I_InitJoystick(); + I_InitJoystick2(); + + // update the menu + if (currentMenu == &OP_JoystickSetDef) + M_SetupJoystickMenu(0); + break; + case SDL_JOYDEVICEREMOVED: + { + // every time a device is unplugged, the "which" index increments by 1? + INT32 deviceIdx = evt.jdevice.which - joyunplugcount++; + + CONS_Printf("Joy device %d removed%s\n", deviceIdx, + (JoyInfo.oldjoy-1 == deviceIdx) ? " was first joystick" : + (JoyInfo2.oldjoy-1 == deviceIdx) ? " was second joystick" : ""); + + // I_ShutdownJoystick doesn't shut down the subsystem + // It just fires neutral joy events to clean up the unplugged joy + if (JoyInfo.oldjoy-1 == deviceIdx) + I_ShutdownJoystick(); + if (JoyInfo2.oldjoy-1 == deviceIdx) + I_ShutdownJoystick2(); + + // update the menu + if (currentMenu == &OP_JoystickSetDef) + M_SetupJoystickMenu(0); + } + break; case SDL_QUIT: I_Quit(); M_QuitResponse('y'); diff --git a/src/sdl/sdlmain.h b/src/sdl/sdlmain.h index d12daaa8a..4acbce209 100644 --- a/src/sdl/sdlmain.h +++ b/src/sdl/sdlmain.h @@ -67,6 +67,10 @@ extern SDLJoyInfo_t JoyInfo; */ extern SDLJoyInfo_t JoyInfo2; +// So we can call this from i_video event loop +void I_ShutdownJoystick(void); +void I_ShutdownJoystick2(void); + void I_GetConsoleEvents(void); void SDLforceUngrabMouse(void); From 60afce5771bc9b50ce039c9bca6c4ca284768c8c Mon Sep 17 00:00:00 2001 From: mazmazz Date: Thu, 6 Dec 2018 14:03:46 -0500 Subject: [PATCH 02/19] Disable XINPUT before initing the joy subsystem --- src/sdl/i_system.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index b18ea9571..ae462c527 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1380,13 +1380,12 @@ void I_InitJoystick(void) if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { CONS_Printf("Initing joy system\n"); + SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) { CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); return; } - else - SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); } if (strcmp(cv_usejoystick.string, "0") && joy_open(cv_usejoystick.string) != -1) @@ -1412,13 +1411,12 @@ void I_InitJoystick2(void) if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { CONS_Printf("Initing joy system\n"); + SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) { CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); return; } - else - SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); } if (strcmp(cv_usejoystick2.string, "0") && joy_open2(cv_usejoystick2.string) != -1) From bcd747c1cd24bc50b95857f5d995c4612917f467 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Thu, 13 Dec 2018 22:26:13 -0500 Subject: [PATCH 03/19] Adjust SDL_JOYDEVICEREMOVED handler by checking the player's joy device explicitly --- src/sdl/i_video.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 835ba1660..82f26d23d 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -116,9 +116,6 @@ static INT32 firstEntry = 0; // Total mouse motion X/Y offsets static INT32 mousemovex = 0, mousemovey = 0; -// Keep track of joy unplugged count -static INT32 joyunplugcount = 0; - // SDL vars static SDL_Surface *vidSurface = NULL; static SDL_Surface *bufSurface = NULL; @@ -897,25 +894,21 @@ void I_GetEvent(void) M_SetupJoystickMenu(0); break; case SDL_JOYDEVICEREMOVED: + if (JoyInfo.dev && !SDL_JoystickGetAttached(JoyInfo.dev)) { - // every time a device is unplugged, the "which" index increments by 1? - INT32 deviceIdx = evt.jdevice.which - joyunplugcount++; - - CONS_Printf("Joy device %d removed%s\n", deviceIdx, - (JoyInfo.oldjoy-1 == deviceIdx) ? " was first joystick" : - (JoyInfo2.oldjoy-1 == deviceIdx) ? " was second joystick" : ""); - - // I_ShutdownJoystick doesn't shut down the subsystem - // It just fires neutral joy events to clean up the unplugged joy - if (JoyInfo.oldjoy-1 == deviceIdx) - I_ShutdownJoystick(); - if (JoyInfo2.oldjoy-1 == deviceIdx) - I_ShutdownJoystick2(); - - // update the menu - if (currentMenu == &OP_JoystickSetDef) - M_SetupJoystickMenu(0); + CONS_Printf("Joy device %d removed, was first joystick\n", JoyInfo.oldjoy); + I_ShutdownJoystick(); } + + if (JoyInfo2.dev && !SDL_JoystickGetAttached(JoyInfo2.dev)) + { + CONS_Printf("Joy device %d removed, was second joystick\n", JoyInfo2.oldjoy); + I_ShutdownJoystick2(); + } + + // update the menu + if (currentMenu == &OP_JoystickSetDef) + M_SetupJoystickMenu(0); break; case SDL_QUIT: I_Quit(); From e81f05c496c9a7bb24c8bbdbf054b9116eecfadf Mon Sep 17 00:00:00 2001 From: mazmazz Date: Thu, 13 Dec 2018 23:09:05 -0500 Subject: [PATCH 04/19] Handle unstable device index when hotplugging controller --- src/sdl/i_system.c | 72 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index ae462c527..e48807830 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1021,6 +1021,7 @@ void I_GetJoystickEvents(void) */ static int joy_open(const char *fname) { + SDL_Joystick *newdev = NULL; int joyindex = atoi(fname); int num_joy = 0; int i; @@ -1062,7 +1063,28 @@ static int joy_open(const char *fname) } } - JoyInfo.dev = SDL_JoystickOpen(joyindex-1); + newdev = SDL_JoystickOpen(joyindex-1); + + // Handle the edge case where the device <-> joystick index assignment can change due to hotplugging + // This indexing is SDL's responsibility and there's not much we can do about it. + // + // Example: + // 1. Plug Controller A -> Index 0 opened + // 2. Plug Controller B -> Index 1 opened + // 3. Unplug Controller A -> Index 0 closed, Index 1 active + // 4. Unplug Controller B -> Index 0 inactive, Index 1 closed + // 5. Plug Controller B -> Index 0 opened + // 6. Plug Controller A -> Index 0 REPLACED, opened as Controller A; Index 1 is now Controller B + if (JoyInfo.dev) + { + if (JoyInfo.dev == newdev // same device, nothing to do + || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo.dev))) // we failed, but already have a working device + return JoyInfo.axises; + // Else, we're changing devices, so send neutral joy events + I_ShutdownJoystick(); + } + + JoyInfo.dev = newdev; if (JoyInfo.dev == NULL) { @@ -1075,7 +1097,7 @@ static int joy_open(const char *fname) JoyInfo.axises = SDL_JoystickNumAxes(JoyInfo.dev); if (JoyInfo.axises > JOYAXISSET*2) JoyInfo.axises = JOYAXISSET*2; -/* if (joyaxes<2) + /* if (joyaxes<2) { I_OutputMsg("Not enought axes?\n"); return 0; @@ -1292,6 +1314,7 @@ void I_GetJoystick2Events(void) */ static int joy_open2(const char *fname) { + SDL_Joystick *newdev = NULL; int joyindex = atoi(fname); int num_joy = 0; int i; @@ -1333,7 +1356,28 @@ static int joy_open2(const char *fname) } } - JoyInfo2.dev = SDL_JoystickOpen(joyindex-1); + newdev = SDL_JoystickOpen(joyindex-1); + + // Handle the edge case where the device <-> joystick index assignment can change due to hotplugging + // This indexing is SDL's responsibility and there's not much we can do about it. + // + // Example: + // 1. Plug Controller A -> Index 0 opened + // 2. Plug Controller B -> Index 1 opened + // 3. Unplug Controller A -> Index 0 closed, Index 1 active + // 4. Unplug Controller B -> Index 0 inactive, Index 1 closed + // 5. Plug Controller B -> Index 0 opened + // 6. Plug Controller A -> Index 0 REPLACED, opened as Controller A; Index 1 is now Controller B + if (JoyInfo2.dev) + { + if (JoyInfo2.dev == newdev // same device, nothing to do + || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo2.dev))) // we failed, but already have a working device + return JoyInfo.axises; + // Else, we're changing devices, so send neutral joy events + I_ShutdownJoystick2(); + } + + JoyInfo2.dev = newdev; if (JoyInfo2.dev == NULL) { @@ -1390,7 +1434,16 @@ void I_InitJoystick(void) if (strcmp(cv_usejoystick.string, "0") && joy_open(cv_usejoystick.string) != -1) { - JoyInfo.oldjoy = atoi(cv_usejoystick.string); + // JoyInfo.oldjoy may already be filled because we attempted to hotplug + // a device and the device index has changed + // So in this case, get the new device index + // + // For now, it does not actually matter if the JoyInfo.oldjoy value is inaccurate. We don't use its + // exact value; we just use it like a boolean. + if (JoyInfo.oldjoy <= 0) + JoyInfo.oldjoy = atoi(cv_usejoystick.string); + else + JoyInfo.oldjoy = SDL_JoystickInstanceID(JoyInfo.dev) + 1; joystick_started = 1; } else @@ -1421,7 +1474,16 @@ void I_InitJoystick2(void) if (strcmp(cv_usejoystick2.string, "0") && joy_open2(cv_usejoystick2.string) != -1) { - JoyInfo2.oldjoy = atoi(cv_usejoystick2.string); + // JoyInfo.oldjoy may already be filled because we attempted to hotplug + // a device and the device index has changed + // So in this case, get the new device index + // + // For now, it does not actually matter if the JoyInfo2.oldjoy value is inaccurate. We don't use its + // exact value; we just use it like a boolean. + if (JoyInfo2.oldjoy <= 0) + JoyInfo2.oldjoy = atoi(cv_usejoystick2.string); + else + JoyInfo2.oldjoy = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; joystick2_started = 1; } else From 14cde2d227520bff464773442d11935e4bbfdedc Mon Sep 17 00:00:00 2001 From: mazmazz Date: Thu, 13 Dec 2018 23:26:07 -0500 Subject: [PATCH 05/19] Change joystick log messages to DBG_GAMELOGIC --- src/sdl/i_system.c | 16 ++++++++++++---- src/sdl/i_video.c | 14 ++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index e48807830..7a9439791 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1088,12 +1088,12 @@ static int joy_open(const char *fname) if (JoyInfo.dev == NULL) { - CONS_Printf(M_GetText("Couldn't open joystick: %s\n"), SDL_GetError()); + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick1: Couldn't open device - %s\n"), SDL_GetError()); return -1; } else { - CONS_Printf(M_GetText("Joystick: %s\n"), SDL_JoystickName(JoyInfo.dev)); + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick1: %s\n"), SDL_JoystickName(JoyInfo.dev)); JoyInfo.axises = SDL_JoystickNumAxes(JoyInfo.dev); if (JoyInfo.axises > JOYAXISSET*2) JoyInfo.axises = JOYAXISSET*2; @@ -1381,12 +1381,12 @@ static int joy_open2(const char *fname) if (JoyInfo2.dev == NULL) { - CONS_Printf(M_GetText("Couldn't open joystick2: %s\n"), SDL_GetError()); + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick2: couldn't open device - %s\n"), SDL_GetError()); return -1; } else { - CONS_Printf(M_GetText("Joystick2: %s\n"), SDL_JoystickName(JoyInfo2.dev)); + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick2: %s\n"), SDL_JoystickName(JoyInfo2.dev)); JoyInfo2.axises = SDL_JoystickNumAxes(JoyInfo2.dev); if (JoyInfo2.axises > JOYAXISSET*2) JoyInfo2.axises = JOYAXISSET*2; @@ -1443,7 +1443,11 @@ void I_InitJoystick(void) if (JoyInfo.oldjoy <= 0) JoyInfo.oldjoy = atoi(cv_usejoystick.string); else + { + CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index has changed: was %d, now %d\n", + JoyInfo.oldjoy, SDL_JoystickInstanceID(JoyInfo.dev) + 1); JoyInfo.oldjoy = SDL_JoystickInstanceID(JoyInfo.dev) + 1; + } joystick_started = 1; } else @@ -1483,7 +1487,11 @@ void I_InitJoystick2(void) if (JoyInfo2.oldjoy <= 0) JoyInfo2.oldjoy = atoi(cv_usejoystick2.string); else + { + CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index has changed: was %d, now %d\n", + JoyInfo2.oldjoy, SDL_JoystickInstanceID(JoyInfo2.dev) + 1); JoyInfo2.oldjoy = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; + } joystick2_started = 1; } else diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 82f26d23d..51da55cb2 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -883,12 +883,15 @@ void I_GetEvent(void) Impl_HandleJoystickButtonEvent(evt.jbutton, evt.type); break; case SDL_JOYDEVICEADDED: - CONS_Printf("Joy device %d added\n", evt.jdevice.which); + CONS_Debug(DBG_GAMELOGIC, "Joystick device index %d added\n", evt.jdevice.which + 1); - // recounts hotplugged joysticks + // recount hotplugged joysticks I_InitJoystick(); I_InitJoystick2(); + CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); + CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); + // update the menu if (currentMenu == &OP_JoystickSetDef) M_SetupJoystickMenu(0); @@ -896,16 +899,19 @@ void I_GetEvent(void) case SDL_JOYDEVICEREMOVED: if (JoyInfo.dev && !SDL_JoystickGetAttached(JoyInfo.dev)) { - CONS_Printf("Joy device %d removed, was first joystick\n", JoyInfo.oldjoy); + CONS_Debug(DBG_GAMELOGIC, "Joystick1 removed, device index: %d\n", JoyInfo.oldjoy); I_ShutdownJoystick(); } if (JoyInfo2.dev && !SDL_JoystickGetAttached(JoyInfo2.dev)) { - CONS_Printf("Joy device %d removed, was second joystick\n", JoyInfo2.oldjoy); + CONS_Debug(DBG_GAMELOGIC, "Joystick2 removed, device index: %d\n", JoyInfo2.oldjoy); I_ShutdownJoystick2(); } + CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); + CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); + // update the menu if (currentMenu == &OP_JoystickSetDef) M_SetupJoystickMenu(0); From 216e710b8705dc3b26d5d09aa026064c68e7c8e2 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Thu, 13 Dec 2018 23:50:36 -0500 Subject: [PATCH 06/19] Improve hotplug edge case with changing device indexes; return proper joy_open output on error --- src/sdl/i_system.c | 90 +++++++--------------------------------------- 1 file changed, 12 insertions(+), 78 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 7a9439791..d1ce041a5 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1024,7 +1024,6 @@ static int joy_open(const char *fname) SDL_Joystick *newdev = NULL; int joyindex = atoi(fname); int num_joy = 0; - int i; if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { @@ -1032,35 +1031,15 @@ static int joy_open(const char *fname) return -1; } - JoyReset(&JoyInfo); - if (joyindex <= 0) - return 0; + return -1; num_joy = SDL_NumJoysticks(); - if (num_joy == 0 || JoyInfo.oldjoy == joyindex) + if (num_joy == 0) { -// I_OutputMsg("Unable to use that joystick #(%s), non-number\n",fname); - if (num_joy != 0) - { - CONS_Printf(M_GetText("Found %d joysticks on this system\n"), num_joy); - for (i = 0; i < num_joy; i++) - CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); - - if (num_joy < joyindex) - { - CONS_Printf(M_GetText("Cannot use joystick #%d/(%s), it doesn't exist\n"),joyindex,fname); - for (i = 0; i < num_joy; i++) - CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); - return 0; - } - } - else - { - CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); - return 0; - } + CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); + return -1; } newdev = SDL_JoystickOpen(joyindex-1); @@ -1081,6 +1060,7 @@ static int joy_open(const char *fname) || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo.dev))) // we failed, but already have a working device return JoyInfo.axises; // Else, we're changing devices, so send neutral joy events + CONS_Debug(DBG_GAMELOGIC, "Joystick1 device is changing; resetting events...\n"); I_ShutdownJoystick(); } @@ -1317,7 +1297,6 @@ static int joy_open2(const char *fname) SDL_Joystick *newdev = NULL; int joyindex = atoi(fname); int num_joy = 0; - int i; if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { @@ -1325,35 +1304,15 @@ static int joy_open2(const char *fname) return -1; } - JoyReset(&JoyInfo2); - if (joyindex <= 0) - return 0; + return -1; num_joy = SDL_NumJoysticks(); - if (num_joy == 0 || JoyInfo2.oldjoy == joyindex) + if (num_joy == 0) { -// I_OutputMsg("Unable to use that joystick #(%s), non-number\n",fname); - if (num_joy != 0) - { - CONS_Printf(M_GetText("Found %d joysticks on this system\n"), num_joy); - for (i = 0; i < num_joy; i++) - CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); - - if (num_joy < joyindex) - { - CONS_Printf(M_GetText("Cannot use joystick #%d/(%s), it doesn't exist\n"),joyindex,fname); - for (i = 0; i < num_joy; i++) - CONS_Printf("#%d/(%s)\n", i+1, SDL_JoystickNameForIndex(i)); - return 0; - } - } - else - { - CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); - return 0; - } + CONS_Printf("%s", M_GetText("Found no joysticks on this system\n")); + return -1; } newdev = SDL_JoystickOpen(joyindex-1); @@ -1374,6 +1333,7 @@ static int joy_open2(const char *fname) || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo2.dev))) // we failed, but already have a working device return JoyInfo.axises; // Else, we're changing devices, so send neutral joy events + CONS_Debug(DBG_GAMELOGIC, "Joystick2 device is changing; resetting events...\n"); I_ShutdownJoystick2(); } @@ -1434,20 +1394,7 @@ void I_InitJoystick(void) if (strcmp(cv_usejoystick.string, "0") && joy_open(cv_usejoystick.string) != -1) { - // JoyInfo.oldjoy may already be filled because we attempted to hotplug - // a device and the device index has changed - // So in this case, get the new device index - // - // For now, it does not actually matter if the JoyInfo.oldjoy value is inaccurate. We don't use its - // exact value; we just use it like a boolean. - if (JoyInfo.oldjoy <= 0) - JoyInfo.oldjoy = atoi(cv_usejoystick.string); - else - { - CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index has changed: was %d, now %d\n", - JoyInfo.oldjoy, SDL_JoystickInstanceID(JoyInfo.dev) + 1); - JoyInfo.oldjoy = SDL_JoystickInstanceID(JoyInfo.dev) + 1; - } + JoyInfo.oldjoy = atoi(cv_usejoystick.string); joystick_started = 1; } else @@ -1478,20 +1425,7 @@ void I_InitJoystick2(void) if (strcmp(cv_usejoystick2.string, "0") && joy_open2(cv_usejoystick2.string) != -1) { - // JoyInfo.oldjoy may already be filled because we attempted to hotplug - // a device and the device index has changed - // So in this case, get the new device index - // - // For now, it does not actually matter if the JoyInfo2.oldjoy value is inaccurate. We don't use its - // exact value; we just use it like a boolean. - if (JoyInfo2.oldjoy <= 0) - JoyInfo2.oldjoy = atoi(cv_usejoystick2.string); - else - { - CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index has changed: was %d, now %d\n", - JoyInfo2.oldjoy, SDL_JoystickInstanceID(JoyInfo2.dev) + 1); - JoyInfo2.oldjoy = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; - } + JoyInfo2.oldjoy = atoi(cv_usejoystick2.string); joystick2_started = 1; } else From 44d6a1d2368157f03bbc0620504ed25a6cf68a92 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 00:22:25 -0500 Subject: [PATCH 07/19] Attempt to handle unstable device IDs --- src/sdl/i_system.c | 22 ++++++++++++---------- src/sdl/i_video.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index d1ce041a5..6ac41de16 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1019,10 +1019,9 @@ void I_GetJoystickEvents(void) */ -static int joy_open(const char *fname) +static int joy_open(int joyindex) { SDL_Joystick *newdev = NULL; - int joyindex = atoi(fname); int num_joy = 0; if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) @@ -1292,10 +1291,9 @@ void I_GetJoystick2Events(void) */ -static int joy_open2(const char *fname) +static int joy_open2(int joyindex) { SDL_Joystick *newdev = NULL; - int joyindex = atoi(fname); int num_joy = 0; if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) @@ -1392,16 +1390,18 @@ void I_InitJoystick(void) } } - if (strcmp(cv_usejoystick.string, "0") && joy_open(cv_usejoystick.string) != -1) + if (cv_usejoystick.value && joy_open(cv_usejoystick.value) != -1) { - JoyInfo.oldjoy = atoi(cv_usejoystick.string); + // 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; joystick_started = 1; } else { if (JoyInfo.oldjoy) I_ShutdownJoystick(); - cv_usejoystick.value = 0; joystick_started = 0; } } @@ -1423,16 +1423,18 @@ void I_InitJoystick2(void) } } - if (strcmp(cv_usejoystick2.string, "0") && joy_open2(cv_usejoystick2.string) != -1) + if (cv_usejoystick2.value && joy_open2(cv_usejoystick2.value) != -1) { - JoyInfo2.oldjoy = atoi(cv_usejoystick2.string); + // 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; joystick2_started = 1; } else { if (JoyInfo2.oldjoy) I_ShutdownJoystick2(); - cv_usejoystick2.value = 0; joystick2_started = 0; } diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 51da55cb2..6fc8f5779 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -885,7 +885,27 @@ void I_GetEvent(void) case SDL_JOYDEVICEADDED: CONS_Debug(DBG_GAMELOGIC, "Joystick device index %d added\n", evt.jdevice.which + 1); - // recount hotplugged joysticks + // Because SDL's device index is unstable, we're going to cheat here a bit: + // 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 (!JoyInfo.dev || !SDL_JoystickGetAttached(JoyInfo.dev)) + { + cv_usejoystick.value = evt.jdevice.which + 1; + + if (JoyInfo2.dev) + cv_usejoystick2.value = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; + } + 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; + } + + // If an active joystick's index has changed, these will just + // change the corresponding JoyInfo.oldjoy I_InitJoystick(); I_InitJoystick2(); @@ -909,6 +929,13 @@ void I_GetEvent(void) I_ShutdownJoystick2(); } + // Update the device indexes, because they likely changed + if (JoyInfo.dev) + JoyInfo.oldjoy = SDL_JoystickInstanceID(JoyInfo.dev) + 1; + + if (JoyInfo2.dev) + JoyInfo2.oldjoy = SDL_JoystickInstanceID(JoyInfo2.dev) + 1; + CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); From 6b9fe87b60de3da7ef2f2b4e3553fb34b1b557ad Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 01:45:18 -0500 Subject: [PATCH 08/19] 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); From 3b6de810e11ea90177ab65e21184766172111fea Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 03:58:07 -0500 Subject: [PATCH 09/19] Lots of changes to better support hotplugging and unstable device indexes * Don't save cv_usejoystick/2 because hotplugging changes can be made invalid by next run * Properly set cv_usejoystick/2 for menu toggling * Force-disable a player's joystick if setting another player's joystick to the same device * Properly set cv_usejoystick/2 value of BOTH target player AND other players when hotplugging and unplugging --- src/d_netcmd.c | 24 +++++++++++++++++---- src/m_menu.c | 29 ++++++++++++++++++++++++++ src/sdl/i_system.c | 18 ++++++++++++++++ src/sdl/i_video.c | 52 +++++++++++++++++++++++++++++++++++----------- src/sdl/sdlmain.h | 3 +++ 5 files changed, 110 insertions(+), 16 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 8abfb8709..16c08309f 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -46,6 +46,13 @@ #include "m_cond.h" #include "m_anigif.h" +#if defined(HAVE_SDL) +#include "SDL.h" +#if SDL_VERSION_ATLEAST(2,0,0) +#include "sdl/sdlmain.h" // JOYSTICK_HOTPLUG +#endif +#endif + #ifdef NETGAME_DEVMODE #define CV_RESTRICT CV_NETVAR #else @@ -243,10 +250,19 @@ consvar_t cv_usemouse = {"use_mouse", "On", CV_SAVE|CV_CALL,usemouse_cons_t, I_S consvar_t cv_usemouse2 = {"use_mouse2", "Off", CV_SAVE|CV_CALL,usemouse_cons_t, I_StartupMouse2, 0, NULL, NULL, 0, 0, NULL}; #if defined (DC) || defined (_XBOX) || defined (WMINPUT) || defined (_WII) || defined(HAVE_SDL) || defined(_WINDOWS) //joystick 1 and 2 -consvar_t cv_usejoystick = {"use_joystick", "1", CV_SAVE|CV_CALL, usejoystick_cons_t, - I_InitJoystick, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_usejoystick2 = {"use_joystick2", "2", CV_SAVE|CV_CALL, usejoystick_cons_t, - I_InitJoystick2, 0, NULL, NULL, 0, 0, NULL}; +// JOYSTICK_HOTPLUG is set by sdlmain.h (SDL2) +// because SDL joystick indexes are unstable, and hotplugging can change a device's index. +// So let's not save any index changes to the config +consvar_t cv_usejoystick = {"use_joystick", "1", CV_CALL +#ifndef JOYSTICK_HOTPLUG + |CV_SAVE +#endif + , usejoystick_cons_t, I_InitJoystick, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_usejoystick2 = {"use_joystick2", "2", CV_CALL +#ifndef JOYSTICK_HOTPLUG + |CV_SAVE +#endif + , usejoystick_cons_t, I_InitJoystick2, 0, NULL, NULL, 0, 0, NULL}; #elif defined (PSP) || defined (GP2X) || defined (_NDS) //only one joystick consvar_t cv_usejoystick = {"use_joystick", "1", CV_SAVE|CV_CALL, usejoystick_cons_t, I_InitJoystick, 0, NULL, NULL, 0, 0, NULL}; diff --git a/src/m_menu.c b/src/m_menu.c index a4c62c0f3..7bac5fa24 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -62,6 +62,13 @@ // And just some randomness for the exits. #include "m_random.h" +#if defined(HAVE_SDL) +#include "SDL.h" +#if SDL_VERSION_ATLEAST(2,0,0) +#include "sdl/sdlmain.h" // JOYSTICK_HOTPLUG +#endif +#endif + #ifdef PC_DOS #include // for snprintf int snprintf(char *str, size_t n, const char *fmt, ...); @@ -6770,12 +6777,34 @@ void M_SetupJoystickMenu(INT32 choice) strcpy(joystickInfo[i], "None"); +#ifdef JOYSTICK_HOTPLUG + if (0 == cv_usejoystick.value) + CV_SetValue(&cv_usejoystick, 0); + if (0 == cv_usejoystick2.value) + CV_SetValue(&cv_usejoystick2, 0); +#endif + for (i = 1; i < 8; i++) { if (i <= n && (I_GetJoyName(i)) != NULL) strncpy(joystickInfo[i], I_GetJoyName(i), 28); else strcpy(joystickInfo[i], joyNA); + +#ifdef JOYSTICK_HOTPLUG + // We use cv_usejoystick.string as the USER-SET var + // and cv_usejoystick.value as the INTERNAL var + // + // In practice, if cv_usejoystick.string == 0, this overrides + // cv_usejoystick.value and always disables + // + // Update cv_usejoystick.string here so that the user can + // properly change this value. + if (i == cv_usejoystick.value) + CV_SetValue(&cv_usejoystick, i); + if (i == cv_usejoystick2.value) + CV_SetValue(&cv_usejoystick2, i); +#endif } M_SetupNextMenu(&OP_JoystickSetDef); diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 7cbaf6d35..e6c1b0b5e 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1413,11 +1413,20 @@ void I_InitJoystick(void) // 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; + + // 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 { if (JoyInfo.oldjoy) I_ShutdownJoystick(); + cv_usejoystick.value = 0; joystick_started = 0; } } @@ -1445,11 +1454,20 @@ void I_InitJoystick2(void) // 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; + + // 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 { if (JoyInfo2.oldjoy) I_ShutdownJoystick2(); + cv_usejoystick2.value = 0; joystick2_started = 0; } diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index f6f193038..24228aa8e 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -887,19 +887,21 @@ void I_GetEvent(void) // Because SDL's device index is unstable, we're going to cheat here a bit: // 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! + // 1. Set cv_usejoystickX.value to the new device index (this does not change what is written to config.cfg) + // 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) + // * 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 = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy) + else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy + && atoi(cv_usejoystick2.string) != cv_usejoystick.value) cv_usejoystick2.value = atoi(cv_usejoystick2.string); - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy) + else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy + && atoi(cv_usejoystick.string) != cv_usejoystick.value) cv_usejoystick2.value = atoi(cv_usejoystick.string); else // we tried... cv_usejoystick2.value = 0; @@ -910,16 +912,31 @@ void I_GetEvent(void) if (JoyInfo.dev) cv_usejoystick.value = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; - else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy) + else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy + && atoi(cv_usejoystick.string) != cv_usejoystick2.value) cv_usejoystick.value = atoi(cv_usejoystick.string); - else if (atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy) + else if (atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy + && atoi(cv_usejoystick2.string) != cv_usejoystick2.value) 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 - // change the corresponding JoyInfo.oldjoy + // Was cv_usejoystick disabled in settings? + if (!strcmp(cv_usejoystick.string, "0") || !cv_usejoystick.value) + cv_usejoystick.value = 0; + else if (cv_usejoystick.value) // update the cvar ONLY if a device exists + CV_SetValue(&cv_usejoystick, cv_usejoystick.value); + + if (!strcmp(cv_usejoystick2.string, "0") || !cv_usejoystick2.value) + cv_usejoystick2.value = 0; + else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists + CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); + + // Update all joysticks' init states + // This is a little wasteful since cv_usejoystick already calls this, but + // we need to do this in case CV_SetValue did nothing because the string was already same. + // if the device is already active, this should do nothing, effectively. I_InitJoystick(); I_InitJoystick2(); @@ -944,8 +961,8 @@ 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 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) cv_usejoystick.value = JoyInfo.oldjoy = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy) @@ -964,6 +981,17 @@ void I_GetEvent(void) else // we tried... cv_usejoystick2.value = 0; + // Was cv_usejoystick disabled in settings? + if (!strcmp(cv_usejoystick.string, "0")) + cv_usejoystick.value = 0; + else if (cv_usejoystick.value) // update the cvar ONLY if a device exists + CV_SetValue(&cv_usejoystick, cv_usejoystick.value); + + if (!strcmp(cv_usejoystick2.string, "0")) + cv_usejoystick2.value = 0; + else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists + CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); + 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 b858e1f6f..810c7dce1 100644 --- a/src/sdl/sdlmain.h +++ b/src/sdl/sdlmain.h @@ -31,6 +31,9 @@ extern SDL_bool framebuffer; #define SDL2STUB() CONS_Printf("SDL2: stubbed: %s:%d\n", __func__, __LINE__) #endif +// So m_menu knows whether to store cv_usejoystick value or string +#define JOYSTICK_HOTPLUG + /** \brief The JoyInfo_s struct info about joystick From e548f6f139a69cfd65bb19ac0d7b8db17983bcb6 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 05:02:41 -0500 Subject: [PATCH 10/19] 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 --- src/m_menu.c | 54 +++++++++++++++++++- src/sdl/i_system.c | 41 ++++++++------- src/sdl/i_video.c | 125 ++++++++++++++++++++++++--------------------- 3 files changed, 142 insertions(+), 78 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 7bac5fa24..21d7d25f2 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6777,7 +6777,9 @@ void M_SetupJoystickMenu(INT32 choice) 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) CV_SetValue(&cv_usejoystick, 0); if (0 == cv_usejoystick2.value) @@ -6826,10 +6828,60 @@ static void M_Setup2PJoystickMenu(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) CV_SetValue(&cv_usejoystick2, choice); else CV_SetValue(&cv_usejoystick, choice); +#endif } // ============= diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index e6c1b0b5e..bf8fdbcf3 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1392,6 +1392,8 @@ static int joy_open2(int joyindex) // void I_InitJoystick(void) { + SDL_Joystick *newjoy = NULL; + //I_ShutdownJoystick(); if (M_CheckParm("-nojoy")) 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 // 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; - - // 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 { @@ -1429,10 +1428,15 @@ void I_InitJoystick(void) cv_usejoystick.value = 0; joystick_started = 0; } + + if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy) + SDL_JoystickClose(newjoy); } void I_InitJoystick2(void) { + SDL_Joystick *newjoy = NULL; + //I_ShutdownJoystick2(); if (M_CheckParm("-nojoy")) 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. JoyInfo2.oldjoy = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 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 { @@ -1471,6 +1472,8 @@ void I_InitJoystick2(void) joystick2_started = 0; } + if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy) + SDL_JoystickClose(newjoy); } static void I_ShutdownInput(void) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 24228aa8e..318777f1c 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -883,69 +883,78 @@ void I_GetEvent(void) Impl_HandleJoystickButtonEvent(evt.jbutton, evt.type); break; case SDL_JOYDEVICEADDED: - 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: - // For the first joystick setting that is NOT active: - // 1. Set cv_usejoystickX.value to the new device index (this does not change what is written to config.cfg) - // 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) - // * 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; + SDL_Joystick *newjoy = SDL_JoystickOpen(evt.jdevice.which); - if (JoyInfo2.dev) - cv_usejoystick2.value = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; - else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick.value) - cv_usejoystick2.value = atoi(cv_usejoystick2.string); - else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick.value) - 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; + CONS_Debug(DBG_GAMELOGIC, "Joystick device index %d added\n", evt.jdevice.which + 1); - if (JoyInfo.dev) - cv_usejoystick.value = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; - else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick.string) != cv_usejoystick2.value) - cv_usejoystick.value = atoi(cv_usejoystick.string); - else if (atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy - && atoi(cv_usejoystick2.string) != cv_usejoystick2.value) - cv_usejoystick.value = atoi(cv_usejoystick2.string); - else // we tried... + // Because SDL's device index is unstable, we're going to cheat here a bit: + // For the first joystick setting that is NOT active: + // 1. Set cv_usejoystickX.value to the new device index (this does not change what is written to config.cfg) + // 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) + // * BUT: If that default index is being occupied, use ANOTHER cv_usejoystick's default value! + 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; + + if (JoyInfo2.dev) + cv_usejoystick2.value = I_GetJoystickDeviceIndex(JoyInfo2.dev) + 1; + else if (atoi(cv_usejoystick2.string) != JoyInfo.oldjoy + && atoi(cv_usejoystick2.string) != cv_usejoystick.value) + cv_usejoystick2.value = atoi(cv_usejoystick2.string); + else if (atoi(cv_usejoystick.string) != JoyInfo.oldjoy + && atoi(cv_usejoystick.string) != cv_usejoystick.value) + cv_usejoystick2.value = atoi(cv_usejoystick.string); + else // we tried... + cv_usejoystick2.value = 0; + } + 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; + + if (JoyInfo.dev) + cv_usejoystick.value = I_GetJoystickDeviceIndex(JoyInfo.dev) + 1; + else if (atoi(cv_usejoystick.string) != JoyInfo2.oldjoy + && atoi(cv_usejoystick.string) != cv_usejoystick2.value) + cv_usejoystick.value = atoi(cv_usejoystick.string); + else if (atoi(cv_usejoystick2.string) != JoyInfo2.oldjoy + && atoi(cv_usejoystick2.string) != cv_usejoystick2.value) + cv_usejoystick.value = atoi(cv_usejoystick2.string); + else // we tried... + cv_usejoystick.value = 0; + } + + // Was cv_usejoystick disabled in settings? + if (!strcmp(cv_usejoystick.string, "0") || !cv_usejoystick.value) cv_usejoystick.value = 0; + else if (cv_usejoystick.value) // update the cvar ONLY if a device exists + CV_SetValue(&cv_usejoystick, cv_usejoystick.value); + + if (!strcmp(cv_usejoystick2.string, "0") || !cv_usejoystick2.value) + cv_usejoystick2.value = 0; + else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists + CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); + + // Update all joysticks' init states + // This is a little wasteful since cv_usejoystick already calls this, but + // we need to do this in case CV_SetValue did nothing because the string was already same. + // if the device is already active, this should do nothing, effectively. + I_InitJoystick(); + I_InitJoystick2(); + + CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); + CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); + + // update the menu + if (currentMenu == &OP_JoystickSetDef) + M_SetupJoystickMenu(0); + + if (JoyInfo.dev != newjoy && JoyInfo2.dev != newjoy) + SDL_JoystickClose(newjoy); } - - // Was cv_usejoystick disabled in settings? - if (!strcmp(cv_usejoystick.string, "0") || !cv_usejoystick.value) - cv_usejoystick.value = 0; - else if (cv_usejoystick.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick, cv_usejoystick.value); - - if (!strcmp(cv_usejoystick2.string, "0") || !cv_usejoystick2.value) - cv_usejoystick2.value = 0; - else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists - CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); - - // Update all joysticks' init states - // This is a little wasteful since cv_usejoystick already calls this, but - // we need to do this in case CV_SetValue did nothing because the string was already same. - // if the device is already active, this should do nothing, effectively. - I_InitJoystick(); - I_InitJoystick2(); - - CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); - CONS_Debug(DBG_GAMELOGIC, "Joystick2 device index: %d\n", JoyInfo2.oldjoy); - - // update the menu - if (currentMenu == &OP_JoystickSetDef) - M_SetupJoystickMenu(0); break; case SDL_JOYDEVICEREMOVED: if (JoyInfo.dev && !SDL_JoystickGetAttached(JoyInfo.dev)) From b2c02838c4a3ab2641ab1ca69b6d987c4330dd13 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 05:31:47 -0500 Subject: [PATCH 11/19] Display "joystick used" prompt correctly if setting use_joystick from an old value > I_NumJoys() --- src/m_menu.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 21d7d25f2..7b440d46f 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6751,7 +6751,7 @@ static void M_ScreenshotOptions(INT32 choice) static void M_DrawJoystick(void) { - INT32 i; + INT32 i, compareval2, compareval; M_DrawGenericMenu(); @@ -6760,8 +6760,23 @@ static void M_DrawJoystick(void) M_DrawTextBox(OP_JoystickSetDef.x-8, OP_JoystickSetDef.y+LINEHEIGHT*i-12, 28, 1); //M_DrawSaveLoadBorder(OP_JoystickSetDef.x, OP_JoystickSetDef.y+LINEHEIGHT*i); - if ((setupcontrols_secondaryplayer && (i == cv_usejoystick2.value)) - || (!setupcontrols_secondaryplayer && (i == cv_usejoystick.value))) +#ifdef JOYSTICK_HOTPLUG + if (atoi(cv_usejoystick2.string) > I_NumJoys()) + compareval2 = atoi(cv_usejoystick2.string); + else + compareval2 = cv_usejoystick2.value; + + if (atoi(cv_usejoystick.string) > I_NumJoys()) + compareval = atoi(cv_usejoystick.string); + else + compareval = cv_usejoystick.value; +#else + compareval2 = cv_usejoystick2.value; + compareval = cv_usejoystick.value +#endif + + if ((setupcontrols_secondaryplayer && (i == compareval2)) + || (!setupcontrols_secondaryplayer && (i == compareval))) V_DrawString(OP_JoystickSetDef.x, OP_JoystickSetDef.y+LINEHEIGHT*i-4,V_GREENMAP,joystickInfo[i]); else V_DrawString(OP_JoystickSetDef.x, OP_JoystickSetDef.y+LINEHEIGHT*i-4,0,joystickInfo[i]); @@ -6836,7 +6851,7 @@ static void M_AssignJoystick(INT32 choice) if (setupcontrols_secondaryplayer) { - oldchoice = cv_usejoystick2.value; + oldchoice = atoi(cv_usejoystick2.string) > I_NumJoys() ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value; CV_SetValue(&cv_usejoystick2, choice); // Just in case last-minute changes were made to cv_usejoystick.value, @@ -6848,7 +6863,8 @@ static void M_AssignJoystick(INT32 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) + if (oldchoice == + (atoi(cv_usejoystick2.string) > I_NumJoys() ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value)) M_StartMessage("This joystick is used by another\n" "player. Reset the joystick\n" "for that player first.\n\n" @@ -6857,7 +6873,7 @@ static void M_AssignJoystick(INT32 choice) } else { - oldchoice = cv_usejoystick.value; + oldchoice = atoi(cv_usejoystick.string) > I_NumJoys() ? atoi(cv_usejoystick.string) : cv_usejoystick.value; CV_SetValue(&cv_usejoystick, choice); // Just in case last-minute changes were made to cv_usejoystick.value, @@ -6869,7 +6885,8 @@ static void M_AssignJoystick(INT32 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) + if (oldchoice == + (atoi(cv_usejoystick.string) > I_NumJoys() ? atoi(cv_usejoystick.string) : cv_usejoystick.value)) M_StartMessage("This joystick is used by another\n" "player. Reset the joystick\n" "for that player first.\n\n" From 70d6845d6cb8d542709d5818432f2cc833224e6f Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 05:57:10 -0500 Subject: [PATCH 12/19] Fix menu where cv_usejoystick.value > I_JoyNum and selecting an unused controller and the "used!" prompt pops up erroneously --- src/m_menu.c | 77 +++++++++++++++++++++++++---------------------- src/sdl/i_video.c | 12 +++++--- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 7b440d46f..a07123812 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6792,15 +6792,6 @@ void M_SetupJoystickMenu(INT32 choice) strcpy(joystickInfo[i], "None"); - // 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) - CV_SetValue(&cv_usejoystick, 0); - if (0 == cv_usejoystick2.value) - CV_SetValue(&cv_usejoystick2, 0); -#endif - for (i = 1; i < 8; i++) { if (i <= n && (I_GetJoyName(i)) != NULL) @@ -6845,52 +6836,66 @@ static void M_AssignJoystick(INT32 choice) { #ifdef JOYSTICK_HOTPLUG INT32 oldchoice; - - if (choice > I_NumJoys()) - return; + INT32 numjoys = I_NumJoys(); if (setupcontrols_secondaryplayer) { - oldchoice = atoi(cv_usejoystick2.string) > I_NumJoys() ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value; + oldchoice = atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : 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) + // But don't do this if we're intentionally setting higher than numjoys + if (choice <= numjoys) { - if (choice && oldchoice > I_NumJoys()) // if we did not select "None", we likely selected a used device - CV_SetValue(&cv_usejoystick2, oldchoice); + CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); - if (oldchoice == - (atoi(cv_usejoystick2.string) > I_NumJoys() ? atoi(cv_usejoystick2.string) : 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); + // reset this so the comparison is valid + if (oldchoice > numjoys) + oldchoice = cv_usejoystick2.value; + + if (oldchoice != choice) + { + if (choice && oldchoice > numjoys) // if we did not select "None", we likely selected a used device + CV_SetValue(&cv_usejoystick2, oldchoice); + + if (oldchoice == + (atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : 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 = atoi(cv_usejoystick.string) > I_NumJoys() ? atoi(cv_usejoystick.string) : cv_usejoystick.value; + oldchoice = atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : 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) + // But don't do this if we're intentionally setting higher than numjoys + if (choice <= numjoys) { - if (choice && oldchoice > I_NumJoys()) // if we did not select "None", we likely selected a used device - CV_SetValue(&cv_usejoystick, oldchoice); + CV_SetValue(&cv_usejoystick, cv_usejoystick.value); - if (oldchoice == - (atoi(cv_usejoystick.string) > I_NumJoys() ? atoi(cv_usejoystick.string) : 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); + // reset this so the comparison is valid + if (oldchoice > numjoys) + oldchoice = cv_usejoystick.value; + + if (oldchoice != choice) + { + if (choice && oldchoice > numjoys) // if we did not select "None", we likely selected a used device + CV_SetValue(&cv_usejoystick, oldchoice); + + if (oldchoice == + (atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : 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 diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 318777f1c..4419284e9 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -930,12 +930,14 @@ void I_GetEvent(void) // Was cv_usejoystick disabled in settings? if (!strcmp(cv_usejoystick.string, "0") || !cv_usejoystick.value) cv_usejoystick.value = 0; - else if (cv_usejoystick.value) // update the cvar ONLY if a device exists + else if (atoi(cv_usejoystick.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys + && cv_usejoystick.value) // update the cvar ONLY if a device exists CV_SetValue(&cv_usejoystick, cv_usejoystick.value); if (!strcmp(cv_usejoystick2.string, "0") || !cv_usejoystick2.value) cv_usejoystick2.value = 0; - else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists + else if (atoi(cv_usejoystick2.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys + && cv_usejoystick2.value) // update the cvar ONLY if a device exists CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); // Update all joysticks' init states @@ -993,12 +995,14 @@ void I_GetEvent(void) // Was cv_usejoystick disabled in settings? if (!strcmp(cv_usejoystick.string, "0")) cv_usejoystick.value = 0; - else if (cv_usejoystick.value) // update the cvar ONLY if a device exists + else if (atoi(cv_usejoystick.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys + && cv_usejoystick.value) // update the cvar ONLY if a device exists CV_SetValue(&cv_usejoystick, cv_usejoystick.value); if (!strcmp(cv_usejoystick2.string, "0")) cv_usejoystick2.value = 0; - else if (cv_usejoystick2.value) // update the cvar ONLY if a device exists + else if (atoi(cv_usejoystick2.string) <= I_NumJoys() // don't mess if we intentionally set higher than NumJoys + && cv_usejoystick2.value) // update the cvar ONLY if a device exists CV_SetValue(&cv_usejoystick2, cv_usejoystick2.value); CONS_Debug(DBG_GAMELOGIC, "Joystick1 device index: %d\n", JoyInfo.oldjoy); From fa63ddda86fac929a4e6dea92eb0c627e8d6f090 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 06:05:31 -0500 Subject: [PATCH 13/19] Fix cv_usejoystick being reset to None when cv_usejoystick was > NumJoys and changing to already-used joystick --- src/m_menu.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index a07123812..9b5e10987 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6835,12 +6835,12 @@ static void M_Setup2PJoystickMenu(INT32 choice) static void M_AssignJoystick(INT32 choice) { #ifdef JOYSTICK_HOTPLUG - INT32 oldchoice; + INT32 oldchoice, oldstringchoice; INT32 numjoys = I_NumJoys(); if (setupcontrols_secondaryplayer) { - oldchoice = atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value; + oldchoice = oldstringchoice = atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value; CV_SetValue(&cv_usejoystick2, choice); // Just in case last-minute changes were made to cv_usejoystick.value, @@ -6856,10 +6856,10 @@ static void M_AssignJoystick(INT32 choice) if (oldchoice != choice) { - if (choice && oldchoice > numjoys) // if we did not select "None", we likely selected a used device - CV_SetValue(&cv_usejoystick2, oldchoice); + if (choice && oldstringchoice > numjoys) // if we did not select "None", we likely selected a used device + CV_SetValue(&cv_usejoystick2, (oldstringchoice > numjoys ? oldstringchoice : oldchoice)); - if (oldchoice == + if (oldstringchoice == (atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value)) M_StartMessage("This joystick is used by another\n" "player. Reset the joystick\n" @@ -6870,7 +6870,7 @@ static void M_AssignJoystick(INT32 choice) } else { - oldchoice = atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : cv_usejoystick.value; + oldchoice = oldstringchoice = atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : cv_usejoystick.value; CV_SetValue(&cv_usejoystick, choice); // Just in case last-minute changes were made to cv_usejoystick.value, @@ -6886,10 +6886,10 @@ static void M_AssignJoystick(INT32 choice) if (oldchoice != choice) { - if (choice && oldchoice > numjoys) // if we did not select "None", we likely selected a used device - CV_SetValue(&cv_usejoystick, oldchoice); + if (choice && oldstringchoice > numjoys) // if we did not select "None", we likely selected a used device + CV_SetValue(&cv_usejoystick, (oldstringchoice > numjoys ? oldstringchoice : oldchoice)); - if (oldchoice == + if (oldstringchoice == (atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : cv_usejoystick.value)) M_StartMessage("This joystick is used by another\n" "player. Reset the joystick\n" From d094a70ec3872276b1bd2fb8cfddf15b59bcc7e9 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 06:06:37 -0500 Subject: [PATCH 14/19] Allow use_joystick/2 to be saved once again --- src/d_netcmd.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 16c08309f..8abfb8709 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -46,13 +46,6 @@ #include "m_cond.h" #include "m_anigif.h" -#if defined(HAVE_SDL) -#include "SDL.h" -#if SDL_VERSION_ATLEAST(2,0,0) -#include "sdl/sdlmain.h" // JOYSTICK_HOTPLUG -#endif -#endif - #ifdef NETGAME_DEVMODE #define CV_RESTRICT CV_NETVAR #else @@ -250,19 +243,10 @@ consvar_t cv_usemouse = {"use_mouse", "On", CV_SAVE|CV_CALL,usemouse_cons_t, I_S consvar_t cv_usemouse2 = {"use_mouse2", "Off", CV_SAVE|CV_CALL,usemouse_cons_t, I_StartupMouse2, 0, NULL, NULL, 0, 0, NULL}; #if defined (DC) || defined (_XBOX) || defined (WMINPUT) || defined (_WII) || defined(HAVE_SDL) || defined(_WINDOWS) //joystick 1 and 2 -// JOYSTICK_HOTPLUG is set by sdlmain.h (SDL2) -// because SDL joystick indexes are unstable, and hotplugging can change a device's index. -// So let's not save any index changes to the config -consvar_t cv_usejoystick = {"use_joystick", "1", CV_CALL -#ifndef JOYSTICK_HOTPLUG - |CV_SAVE -#endif - , usejoystick_cons_t, I_InitJoystick, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_usejoystick2 = {"use_joystick2", "2", CV_CALL -#ifndef JOYSTICK_HOTPLUG - |CV_SAVE -#endif - , usejoystick_cons_t, I_InitJoystick2, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_usejoystick = {"use_joystick", "1", CV_SAVE|CV_CALL, usejoystick_cons_t, + I_InitJoystick, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_usejoystick2 = {"use_joystick2", "2", CV_SAVE|CV_CALL, usejoystick_cons_t, + I_InitJoystick2, 0, NULL, NULL, 0, 0, NULL}; #elif defined (PSP) || defined (GP2X) || defined (_NDS) //only one joystick consvar_t cv_usejoystick = {"use_joystick", "1", CV_SAVE|CV_CALL, usejoystick_cons_t, I_InitJoystick, 0, NULL, NULL, 0, 0, NULL}; From c49a5ed8d2947ed4d14dc1626c161f6f55333ccf Mon Sep 17 00:00:00 2001 From: mazmazz Date: Fri, 14 Dec 2018 06:14:21 -0500 Subject: [PATCH 15/19] Better I_InitJoystick log message --- src/sdl/i_system.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index bf8fdbcf3..c152346c5 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1400,7 +1400,7 @@ void I_InitJoystick(void) if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { - CONS_Printf("Initing joy system\n"); + CONS_Printf("I_InitJoystick()...\n"); SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) { @@ -1443,7 +1443,7 @@ void I_InitJoystick2(void) if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { - CONS_Printf("Initing joy system\n"); + CONS_Printf("I_InitJoystick2()...\n"); SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) { From 555b4e176686f971e407df16a566c1341a71851a Mon Sep 17 00:00:00 2001 From: Steel Titanium Date: Sun, 8 Sep 2019 19:39:23 -0400 Subject: [PATCH 16/19] Fix function being redefined --- src/sdl/i_system.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 57edb1c35..78363c85e 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1035,7 +1035,7 @@ void I_GetJoystickEvents(void) */ -static int joy_open2(int joyindex) +static int joy_open(int joyindex) { SDL_Joystick *newdev = NULL; int num_joy = 0; @@ -1069,48 +1069,48 @@ static int joy_open2(int joyindex) // 4. Unplug Controller B -> Index 0 inactive, Index 1 closed // 5. Plug Controller B -> Index 0 opened // 6. Plug Controller A -> Index 0 REPLACED, opened as Controller A; Index 1 is now Controller B - if (JoyInfo2.dev) + if (JoyInfo.dev) { - if (JoyInfo2.dev == newdev // same device, nothing to do - || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo2.dev))) // we failed, but already have a working device + if (JoyInfo.dev == newdev // same device, nothing to do + || (newdev == NULL && SDL_JoystickGetAttached(JoyInfo.dev))) // we failed, but already have a working device return JoyInfo.axises; // Else, we're changing devices, so send neutral joy events - CONS_Debug(DBG_GAMELOGIC, "Joystick2 device is changing; resetting events...\n"); - I_ShutdownJoystick2(); + CONS_Debug(DBG_GAMELOGIC, "Joystick1 device is changing; resetting events...\n"); + I_ShutdownJoystick(); } - JoyInfo2.dev = newdev; + JoyInfo.dev = newdev; - if (JoyInfo2.dev == NULL) + if (JoyInfo.dev == NULL) { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick2: couldn't open device - %s\n"), SDL_GetError()); + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick1: Couldn't open device - %s\n"), SDL_GetError()); return -1; } else { - CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick2: %s\n"), SDL_JoystickName(JoyInfo2.dev)); - JoyInfo2.axises = SDL_JoystickNumAxes(JoyInfo2.dev); - if (JoyInfo2.axises > JOYAXISSET*2) - JoyInfo2.axises = JOYAXISSET*2; -/* if (joyaxes<2) + CONS_Debug(DBG_GAMELOGIC, M_GetText("Joystick1: %s\n"), SDL_JoystickName(JoyInfo.dev)); + JoyInfo.axises = SDL_JoystickNumAxes(JoyInfo.dev); + if (JoyInfo.axises > JOYAXISSET*2) + JoyInfo.axises = JOYAXISSET*2; + /* if (joyaxes<2) { I_OutputMsg("Not enought axes?\n"); return 0; }*/ - JoyInfo2.buttons = SDL_JoystickNumButtons(JoyInfo2.dev); - if (JoyInfo2.buttons > JOYBUTTONS) - JoyInfo2.buttons = JOYBUTTONS; + JoyInfo.buttons = SDL_JoystickNumButtons(JoyInfo.dev); + if (JoyInfo.buttons > JOYBUTTONS) + JoyInfo.buttons = JOYBUTTONS; - JoyInfo2.hats = SDL_JoystickNumHats(JoyInfo2.dev); - if (JoyInfo2.hats > JOYHATS) - JoyInfo2.hats = JOYHATS; + JoyInfo.hats = SDL_JoystickNumHats(JoyInfo.dev); + if (JoyInfo.hats > JOYHATS) + JoyInfo.hats = JOYHATS; - JoyInfo2.balls = SDL_JoystickNumBalls(JoyInfo2.dev); + JoyInfo.balls = SDL_JoystickNumBalls(JoyInfo.dev); - //Joystick.bGamepadStyle = !stricmp(SDL_JoystickName(JoyInfo2.dev), "pad"); + //Joystick.bGamepadStyle = !stricmp(SDL_JoystickName(JoyInfo.dev), "pad"); - return JoyInfo2.axises; + return JoyInfo.axises; } } From 6a870b4467cd24b84db4d0edb8e306eb90fc56b6 Mon Sep 17 00:00:00 2001 From: Steel Titanium Date: Mon, 23 Sep 2019 22:46:44 -0400 Subject: [PATCH 17/19] Various of changes Changed any instance of "joystick" in strings to "gamepad" Renamed some cvars Added a define for MAX_JOYSTICK Added back the missing command line params. --- src/d_netcmd.c | 16 ++++++++-------- src/doomdef.h | 3 +++ src/m_menu.c | 14 +++++++------- src/sdl/i_system.c | 18 +++++++++++++++--- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 7d5963ecc..bd57c2481 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -246,20 +246,20 @@ INT32 cv_debug; consvar_t cv_usemouse = {"use_mouse", "On", CV_SAVE|CV_CALL,usemouse_cons_t, I_StartupMouse, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_usemouse2 = {"use_mouse2", "Off", CV_SAVE|CV_CALL,usemouse_cons_t, I_StartupMouse2, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_usejoystick = {"use_joystick", "1", CV_SAVE|CV_CALL, usejoystick_cons_t, +consvar_t cv_usejoystick = {"use_gamepad", "1", CV_SAVE|CV_CALL, usejoystick_cons_t, I_InitJoystick, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_usejoystick2 = {"use_joystick2", "2", CV_SAVE|CV_CALL, usejoystick_cons_t, +consvar_t cv_usejoystick2 = {"use_gamepad2", "2", CV_SAVE|CV_CALL, usejoystick_cons_t, I_InitJoystick2, 0, NULL, NULL, 0, 0, NULL}; #if (defined (LJOYSTICK) || defined (HAVE_SDL)) #ifdef LJOYSTICK -consvar_t cv_joyport = {"joyport", "/dev/js0", CV_SAVE, joyport_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_joyport2 = {"joyport2", "/dev/js0", CV_SAVE, joyport_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; //Alam: for later +consvar_t cv_joyport = {"padport", "/dev/js0", CV_SAVE, joyport_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_joyport2 = {"padport2", "/dev/js0", CV_SAVE, joyport_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; //Alam: for later #endif -consvar_t cv_joyscale = {"joyscale", "1", CV_SAVE|CV_CALL, NULL, I_JoyScale, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_joyscale2 = {"joyscale2", "1", CV_SAVE|CV_CALL, NULL, I_JoyScale2, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_joyscale = {"padscale", "1", CV_SAVE|CV_CALL, NULL, I_JoyScale, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_joyscale2 = {"padscale2", "1", CV_SAVE|CV_CALL, NULL, I_JoyScale2, 0, NULL, NULL, 0, 0, NULL}; #else -consvar_t cv_joyscale = {"joyscale", "1", CV_SAVE|CV_HIDEN, NULL, NULL, 0, NULL, NULL, 0, 0, NULL}; //Alam: Dummy for save -consvar_t cv_joyscale2 = {"joyscale2", "1", CV_SAVE|CV_HIDEN, NULL, NULL, 0, NULL, NULL, 0, 0, NULL}; //Alam: Dummy for save +consvar_t cv_joyscale = {"padscale", "1", CV_SAVE|CV_HIDEN, NULL, NULL, 0, NULL, NULL, 0, 0, NULL}; //Alam: Dummy for save +consvar_t cv_joyscale2 = {"padscale2", "1", CV_SAVE|CV_HIDEN, NULL, NULL, 0, NULL, NULL, 0, 0, NULL}; //Alam: Dummy for save #endif #if (defined (__unix__) && !defined (MSDOS)) || defined(__APPLE__) || defined (UNIXCOMMON) consvar_t cv_mouse2port = {"mouse2port", "/dev/gpmdata", CV_SAVE, mouse2port_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; diff --git a/src/doomdef.h b/src/doomdef.h index 4a0174369..8d44d0896 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -506,6 +506,9 @@ INT32 I_GetKey(void); #define max(x, y) (((x) > (y)) ? (x) : (y)) #endif +// Max gamepad/joysticks that can be detected/used. +#define MAX_JOYSTICKS 4 + // Floating point comparison epsilons from float.h #ifndef FLT_EPSILON #define FLT_EPSILON 1.1920928955078125e-7f diff --git a/src/m_menu.c b/src/m_menu.c index e1b2b5c90..1b52e400e 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -143,7 +143,7 @@ typedef enum levellist_mode_t levellistmode = LLM_CREATESERVER; UINT8 maplistoption = 0; -static char joystickInfo[8][29]; +static char joystickInfo[MAX_JOYSTICKS][29]; #ifndef NONET static UINT32 serverlistpage; #endif @@ -9993,7 +9993,7 @@ static void M_DrawJoystick(void) // draw title (or big pic) M_DrawMenuTitle(); - for (i = 0; i <= 4; i++) // See MAX_JOYSTICKS + for (i = 0; i <= MAX_JOYSTICKS; i++) // See MAX_JOYSTICKS { M_DrawTextBox(OP_JoystickSetDef.x-8, OP_JoystickSetDef.y+LINEHEIGHT*i-12, 28, 1); //M_DrawSaveLoadBorder(OP_JoystickSetDef.x+4, OP_JoystickSetDef.y+1+LINEHEIGHT*i); @@ -10036,7 +10036,7 @@ void M_SetupJoystickMenu(INT32 choice) strcpy(joystickInfo[i], "None"); - for (i = 1; i < 8; i++) + for (i = 1; i < MAX_JOYSTICKS+1; i++) { if (i <= n && (I_GetJoyName(i)) != NULL) strncpy(joystickInfo[i], I_GetJoyName(i), 28); @@ -10113,8 +10113,8 @@ static void M_AssignJoystick(INT32 choice) if (oldstringchoice == (atoi(cv_usejoystick2.string) > numjoys ? atoi(cv_usejoystick2.string) : cv_usejoystick2.value)) - M_StartMessage("This joystick is used by another\n" - "player. Reset the joystick\n" + M_StartMessage("This gamepad is used by another\n" + "player. Reset the gamepad\n" "for that player first.\n\n" "(Press a key)\n", NULL, MM_NOTHING); } @@ -10143,8 +10143,8 @@ static void M_AssignJoystick(INT32 choice) if (oldstringchoice == (atoi(cv_usejoystick.string) > numjoys ? atoi(cv_usejoystick.string) : cv_usejoystick.value)) - M_StartMessage("This joystick is used by another\n" - "player. Reset the joystick\n" + M_StartMessage("This gamepad is used by another\n" + "player. Reset the gamepad\n" "for that player first.\n\n" "(Press a key)\n", NULL, MM_NOTHING); } diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 97690cf7e..d7926e5b2 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -1397,10 +1397,16 @@ void I_InitJoystick(void) if (M_CheckParm("-nojoy")) return; + if (M_CheckParm("-noxinput")) + SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); + + if (M_CheckParm("-nohidapi")) + SDL_SetHintWithPriority("SDL_JOYSTICK_HIDAPI", "0", SDL_HINT_OVERRIDE); + if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { CONS_Printf("I_InitJoystick()...\n"); - SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) { CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); @@ -1440,10 +1446,16 @@ void I_InitJoystick2(void) if (M_CheckParm("-nojoy")) return; + if (M_CheckParm("-noxinput")) + SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); + + if (M_CheckParm("-nohidapi")) + SDL_SetHintWithPriority("SDL_JOYSTICK_HIDAPI", "0", SDL_HINT_OVERRIDE); + if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) { CONS_Printf("I_InitJoystick2()...\n"); - SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) { CONS_Printf(M_GetText("Couldn't initialize joystick: %s\n"), SDL_GetError()); @@ -1499,7 +1511,7 @@ INT32 I_NumJoys(void) return numjoy; } -static char joyname[255]; // MAX_PATH; joystick name is straight from the driver +static char joyname[255]; // joystick name is straight from the driver const char *I_GetJoyName(INT32 joyindex) { From 5cdbde25c1313cac23f069f3a03bfd2352e82d6c Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Sep 2019 20:23:40 -0700 Subject: [PATCH 18/19] All for a cvar... --- src/m_menu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 1b52e400e..fbc21db9e 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -143,7 +143,7 @@ typedef enum levellist_mode_t levellistmode = LLM_CREATESERVER; UINT8 maplistoption = 0; -static char joystickInfo[MAX_JOYSTICKS][29]; +static char joystickInfo[MAX_JOYSTICKS+1][29]; #ifndef NONET static UINT32 serverlistpage; #endif @@ -10036,7 +10036,7 @@ void M_SetupJoystickMenu(INT32 choice) strcpy(joystickInfo[i], "None"); - for (i = 1; i < MAX_JOYSTICKS+1; i++) + for (i = 1; i <= MAX_JOYSTICKS; i++) { if (i <= n && (I_GetJoyName(i)) != NULL) strncpy(joystickInfo[i], I_GetJoyName(i), 28); From e69dd9bd75ae4fcfa949bda409c704c447374b43 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Sep 2019 20:54:53 -0700 Subject: [PATCH 19/19] Automate the silly joystick menu items definition --- src/m_menu.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index fbc21db9e..ca7593e54 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -1118,14 +1118,7 @@ static menuitem_t OP_Joystick2Menu[] = {IT_STRING | IT_CVAR, NULL, "Third-Person Vert-Look", &cv_chasefreelook2, 130}, }; -static menuitem_t OP_JoystickSetMenu[] = -{ - {IT_CALL | IT_NOTHING, "None", NULL, M_AssignJoystick, '0'}, - {IT_CALL | IT_NOTHING, "", NULL, M_AssignJoystick, '1'}, - {IT_CALL | IT_NOTHING, "", NULL, M_AssignJoystick, '2'}, - {IT_CALL | IT_NOTHING, "", NULL, M_AssignJoystick, '3'}, - {IT_CALL | IT_NOTHING, "", NULL, M_AssignJoystick, '4'}, -}; +static menuitem_t OP_JoystickSetMenu[1+MAX_JOYSTICKS]; static menuitem_t OP_MouseOptionsMenu[] = { @@ -3550,6 +3543,8 @@ void M_Ticker(void) // void M_Init(void) { + int i; + CV_RegisterVar(&cv_nextmap); CV_RegisterVar(&cv_newgametype); CV_RegisterVar(&cv_chooseskin); @@ -3599,6 +3594,17 @@ void M_Init(void) OP_ScreenshotOptionsMenu[op_screenshot_colorprofile].status = IT_GRAYEDOUT; #endif + /* + Well the menu sucks for forcing us to have an item set + at all if every item just calls the same function, and + nothing more. Now just automate the definition. + */ + for (i = 0; i <= MAX_JOYSTICKS; ++i) + { + OP_JoystickSetMenu[i].status = ( IT_NOTHING|IT_CALL ); + OP_JoystickSetMenu[i].itemaction = M_AssignJoystick; + } + #ifndef NONET CV_RegisterVar(&cv_serversort); #endif