From 333c94be52004c96b7de76521178b27374a18721 Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 23 Oct 2025 10:55:46 +0200 Subject: [PATCH 1/8] clean up controller dpad input handling just treat them as "normal" buttons instead of manually polling for them alot cleaner and gets rid of yet another polling loop also clean up some other stuff needs to be tested with multiple controllers in splitscreen but cba to do that atm --- src/g_input.h | 2 +- src/i_system.h | 4 -- src/s_sound.c | 2 +- src/sdl/i_system.cpp | 74 +++++------------------ src/sdl/i_video.c | 136 ++++++++++++++++++++++++++---------------- src/sdl/mixer_sound.c | 2 +- 6 files changed, 102 insertions(+), 118 deletions(-) diff --git a/src/g_input.h b/src/g_input.h index ec4a9d795..c430fe7bc 100644 --- a/src/g_input.h +++ b/src/g_input.h @@ -37,7 +37,7 @@ typedef enum KEY_JOY1 = KEY_MOUSE1 + MOUSEBUTTONS, KEY_HAT1 = KEY_JOY1 + JOYBUTTONS, - KEY_DBLMOUSE1 =KEY_HAT1 + JOYHATS*4, // double clicks + KEY_DBLMOUSE1 = KEY_HAT1 + JOYHATS*4, // double clicks KEY_DBLJOY1 = KEY_DBLMOUSE1 + MOUSEBUTTONS, KEY_DBLHAT1 = KEY_DBLJOY1 + JOYBUTTONS, diff --git a/src/i_system.h b/src/i_system.h index f5187e037..b3dedd396 100644 --- a/src/i_system.h +++ b/src/i_system.h @@ -221,10 +221,6 @@ INT32 I_mkdir(const char *dirname, INT32 unixright); */ const char *I_LocateWad(void); -/** \brief Joystick events -*/ -void I_GetJoystickEvents(UINT8 index); - char *I_GetEnv(const char *name); INT32 I_PutEnv(char *variable); diff --git a/src/s_sound.c b/src/s_sound.c index 1bac40a46..5338b4e7e 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1813,7 +1813,7 @@ void S_ChangeMusicEx(const char *mmusic, UINT16 mflags, boolean looping, UINT32 } } -void S_ChangeMusicSpecial (const char *mmusic) +void S_ChangeMusicSpecial(const char *mmusic) { if (cv_resetspecialmusic.value) S_ChangeMusic(mmusic, MUSIC_FORCERESET, true); diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 4e9e54d72..8f720cfa1 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -719,16 +719,12 @@ static void JoyReset(SDLJoyInfo_t *JoySet) { SDL_GameControllerClose(JoySet->dev); } + JoySet->dev = NULL; JoySet->oldjoy = -1; JoySet->axises = JoySet->buttons = JoySet->hats = JoySet->balls = 0; - //JoySet->scale } -/** \brief joystick up and running -*/ -static INT32 joystick_started[MAXSPLITSCREENPLAYERS] = {0, 0, 0, 0}; - /** \brief SDL info about joystick */ SDLJoyInfo_t JoyInfo[MAXSPLITSCREENPLAYERS]; @@ -788,10 +784,12 @@ void I_UpdateJoystickDeviceIndex(UINT8 player) else { UINT8 joystickID, compareJoystick; + for (joystickID = 0; joystickID < MAXSPLITSCREENPLAYERS; joystickID++) { // is this cv_usejoystick used? const INT32 value = atoi(cv_usejoystick[joystickID].string); + for (compareJoystick = 0; compareJoystick < MAXSPLITSCREENPLAYERS; compareJoystick++) { if (compareJoystick == player) @@ -799,6 +797,7 @@ void I_UpdateJoystickDeviceIndex(UINT8 player) if (value == JoyInfo[compareJoystick].oldjoy || value == cv_usejoystick[compareJoystick].value) break; } + if (compareJoystick == MAXSPLITSCREENPLAYERS) { // We DID make it through the whole loop, so we can use this one! @@ -806,6 +805,7 @@ void I_UpdateJoystickDeviceIndex(UINT8 player) break; } } + if (joystickID == MAXSPLITSCREENPLAYERS) { // We DID NOT make it through the whole loop, so we can't assign this joystick to anything. @@ -828,14 +828,6 @@ void I_UpdateJoystickDeviceIndices(UINT8 excludePlayer) } } -/** \brief Joystick buttons states -*/ -static UINT64 lastjoybuttons[MAXSPLITSCREENPLAYERS] = {0,0,0,0}; - -/** \brief Joystick hats state -*/ -static UINT64 lastjoyhats[MAXSPLITSCREENPLAYERS] = {0,0,0,0}; - /** \brief Shuts down joystick \return void */ @@ -847,74 +839,34 @@ void I_ShutdownJoystick(UINT8 index) event.data2 = 0; event.data3 = 0; - lastjoybuttons[index] = lastjoyhats[index] = 0; - // emulate the up of all joystick buttons for (i = 0; i < JOYBUTTONS; i++) { - event.data1=KEY_JOY1 + i; + event.data1 = KEY_JOY1 + i; D_PostEvent(&event); } // emulate the up of all joystick hats for (i = 0; i < JOYHATS*4; i++) { - event.data1=KEY_HAT1+i; + event.data1 = KEY_HAT1+i; D_PostEvent(&event); } // reset joystick position event.type = ev_joystick; + for (i = 0; i < JOYAXISSET; i++) { event.data1 = i; D_PostEvent(&event); } - joystick_started[index] = 0; JoyReset(&JoyInfo[index]); // don't shut down the subsystem here, because hotplugging } -void I_GetJoystickEvents(UINT8 index) -{ - static event_t event = {}; - INT32 i = 0; - UINT64 joyhats = 0; - - if (!joystick_started[index]) - return; - - if (!JoyInfo[index].dev) //I_ShutdownJoystick(); - return; - - joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_UP); - joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_DOWN) << 1; - joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_LEFT) << 2; - joyhats |= SDL_GameControllerGetButton(JoyInfo[index].dev, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) << 3; - - if (joyhats != lastjoyhats[index]) - { - INT64 j = 1; // keep only bits that changed since last time - INT64 newhats = joyhats ^ lastjoyhats[index]; - lastjoyhats[index] = joyhats; - - for (i = 0; i < JOYHATS*4; i++, j <<= 1) - { - if (newhats & j) // hat changed state? - { - if (joyhats & j) - event.type = ev_keydown; - else - event.type = ev_keyup; - event.data1 = KEY_HAT1 + i; - D_PostEvent(&event); - } - } - } -} - /** \brief Open joystick handle \param fname name of joystick @@ -931,6 +883,7 @@ static int joy_open(int playerIndex, int joyIndex) CONS_Printf(M_GetText("Joystick subsystem not started\n")); return -1; } + if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) == 0) { CONS_Printf(M_GetText("Game Controller subsystem not started\n")); @@ -965,6 +918,7 @@ static int joy_open(int playerIndex, int joyIndex) if (JoyInfo[playerIndex].dev == newdev // same device, nothing to do || (newdev == NULL && SDL_GameControllerGetAttached(JoyInfo[playerIndex].dev))) // we failed, but already have a working device return SDL_CONTROLLER_AXIS_MAX; + // Else, we're changing devices, so send neutral joy events CONS_Debug(DBG_GAMELOGIC, "Joystick1 device is changing; resetting events...\n"); I_ShutdownJoystick(playerIndex); @@ -1058,14 +1012,13 @@ void I_InitJoystick(UINT8 index) // 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[index].oldjoy = I_GetJoystickDeviceIndex(JoyInfo[index].dev) + 1; - joystick_started[index] = 1; } else { if (JoyInfo[index].oldjoy) I_ShutdownJoystick(index); + cv_usejoystick[index].value = 0; - joystick_started[index] = 0; } for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) @@ -1130,8 +1083,10 @@ static void I_ShutdownInput(void) INT32 I_NumJoys(void) { INT32 numjoy = 0; + if (SDL_WasInit(SDL_INIT_JOYSTICK) == SDL_INIT_JOYSTICK) numjoy = SDL_NumJoysticks(); + return numjoy; } @@ -1142,15 +1097,18 @@ 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) == SDL_INIT_JOYSTICK) { tempname = SDL_JoystickNameForIndex(joyindex); + if (tempname) { memcpy(joyname, tempname, 255); joyname[255] = '\0'; } } + return joyname; } diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 9ce860cb4..6a8145b0a 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -699,16 +699,19 @@ static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) event.data1 = KEY_MOUSEWHEELUP; event.type = ev_keydown; } + if (evt.y < 0) { event.data1 = KEY_MOUSEWHEELDOWN; event.type = ev_keydown; } + if (evt.y == 0) { event.data1 = 0; event.type = ev_keyup; } + if (event.type == ev_keyup || event.type == ev_keydown) { D_PostEvent(&event); @@ -719,37 +722,24 @@ static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt) { event_t event; - SDL_JoystickID joyid[4]; INT32 value; UINT8 i; // Determine the Joystick IDs for each current open joystick for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - joyid[i] = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev)); + if (evt.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev))) + { + event.type = ev_joystick + i; + break; + } } + if (i == MAXSPLITSCREENPLAYERS) + return; + event.data1 = event.data2 = event.data3 = INT32_MAX; - if (evt.which == joyid[0]) - { - event.type = ev_joystick; - } - else if (evt.which == joyid[1]) - { - event.type = ev_joystick2; - } - else if (evt.which == joyid[2]) - { - event.type = ev_joystick3; - } - else if (evt.which == joyid[3]) - { - event.type = ev_joystick4; - } - else - return; - //axis if (evt.axis > JOYAXISSET*2) return; @@ -785,47 +775,89 @@ static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt) default: return; } + D_PostEvent(&event); } +static void Impl_HandleControllerHatEvent(SDL_ControllerButtonEvent evt, Uint32 type) +{ + event_t event; + UINT8 i; + static const int hat_buttons_base[] = {KEY_HAT1, KEY_2HAT1, KEY_3HAT1, KEY_4HAT1}; + + // Determine the Joystick IDs for each current open joystick + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (evt.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev))) + { + event.data1 = hat_buttons_base[i]; + break; + } + } + + if (i == MAXSPLITSCREENPLAYERS) + return; + + switch (type) + { + case SDL_CONTROLLERBUTTONUP: + event.type = ev_keyup; + break; + case SDL_CONTROLLERBUTTONDOWN: + event.type = ev_keydown; + break; + default: + return; + } + + switch (evt.button) + { + case SDL_CONTROLLER_BUTTON_DPAD_UP: + break; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + event.data1 += 1; + break; + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + event.data1 += 2; + break; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + event.data1 += 3; + break; + default: + return; + } + + if (event.type != ev_console) + { + D_PostEvent(&event); + } +} + static void Impl_HandleControllerButtonEvent(SDL_ControllerButtonEvent evt, Uint32 type) { event_t event; - SDL_JoystickID joyid[4]; UINT8 i; + static const int buttons_base[] = {KEY_JOY1, KEY_2JOY1, KEY_3JOY1, KEY_4JOY1}; + + // dpad special case handling + if (evt.button >= SDL_CONTROLLER_BUTTON_DPAD_UP && + evt.button <= SDL_CONTROLLER_BUTTON_DPAD_RIGHT) + { + Impl_HandleControllerHatEvent(evt, type); + return; + } // Determine the Joystick IDs for each current open joystick for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - joyid[i] = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev)); + if (evt.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev))) + { + event.data1 = buttons_base[i]; + break; + } } - if ( evt.button == SDL_CONTROLLER_BUTTON_DPAD_UP - || evt.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN - || evt.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT - || evt.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) - { - // dpad buttons are mapped as the hat instead - return; - } - - if (evt.which == joyid[0]) - { - event.data1 = KEY_JOY1; - } - else if (evt.which == joyid[1]) - { - event.data1 = KEY_2JOY1; - } - else if (evt.which == joyid[2]) - { - event.data1 = KEY_3JOY1; - } - else if (evt.which == joyid[3]) - { - event.data1 = KEY_4JOY1; - } - else + if (i == MAXSPLITSCREENPLAYERS) return; switch (type) @@ -848,7 +880,9 @@ static void Impl_HandleControllerButtonEvent(SDL_ControllerButtonEvent evt, Uint return; if (event.type != ev_console) + { D_PostEvent(&event); + } } static void Impl_HandleControllerAddedEvent(SDL_Event evt) @@ -1091,7 +1125,6 @@ void I_StartupMouse(void) void I_OsPolling(void) { SDL_Keymod mod; - INT32 i; if (consolevent) I_GetConsoleEvents(); @@ -1099,9 +1132,6 @@ void I_OsPolling(void) if (SDL_WasInit(SDL_INIT_JOYSTICK|SDL_INIT_GAMECONTROLLER) == (SDL_INIT_JOYSTICK|SDL_INIT_GAMECONTROLLER)) { SDL_GameControllerUpdate(); - - for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) - I_GetJoystickEvents(i); } I_GetEvent(); diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index 646d44135..59cffed4a 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -73,7 +73,7 @@ write netcode into the sound code, OKAY? //static UINT16 BUFFERSIZE = 2048; -static UINT16 SAMPLERATE = 44100; +static const UINT16 SAMPLERATE = 44100; #ifdef HAVE_OPENMPT #include "libopenmpt/libopenmpt.h" From a4cc8b05853abee1ce329f191ff0f289fa14b242 Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 23 Oct 2025 11:12:13 +0200 Subject: [PATCH 2/8] keep track of joystick instance index means we dont constantly need to refetch it needs testing on windoze --- src/sdl/i_system.cpp | 11 ++++++++--- src/sdl/i_video.c | 6 +++--- src/sdl/sdlmain.h | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 8f720cfa1..dd197cd1b 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -721,6 +721,7 @@ static void JoyReset(SDLJoyInfo_t *JoySet) } JoySet->dev = NULL; + JoySet->id = -1; JoySet->oldjoy = -1; JoySet->axises = JoySet->buttons = JoySet->hats = JoySet->balls = 0; } @@ -779,7 +780,7 @@ void I_UpdateJoystickDeviceIndex(UINT8 player) /////////////////////////////////////////////// if (JoyInfo[player].dev) { - cv_usejoystick[player].value = I_GetJoystickDeviceIndex(JoyInfo[player].dev) + 1; + cv_usejoystick[player].value = JoyInfo[player].id + 1; } else { @@ -794,6 +795,7 @@ void I_UpdateJoystickDeviceIndex(UINT8 player) { if (compareJoystick == player) continue; + if (value == JoyInfo[compareJoystick].oldjoy || value == cv_usejoystick[compareJoystick].value) break; } @@ -925,6 +927,7 @@ static int joy_open(int playerIndex, int joyIndex) } JoyInfo[playerIndex].dev = newdev; + JoyInfo[playerIndex].id = I_GetJoystickDeviceIndex(JoyInfo[playerIndex].dev); if (JoyInfo[playerIndex].dev == NULL) { @@ -1003,15 +1006,17 @@ void I_InitJoystick(UINT8 index) break; } + JoyInfo[index].id = I_GetJoystickDeviceIndex(JoyInfo[index].dev); + if (newcontroller && i < MAXSPLITSCREENPLAYERS) // don't override an active device { - cv_usejoystick[index].value = I_GetJoystickDeviceIndex(JoyInfo[index].dev) + 1; + cv_usejoystick[index].value = JoyInfo[index].id + 1; } else if (newcontroller && joy_open(index, cv_usejoystick[index].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[index].oldjoy = I_GetJoystickDeviceIndex(JoyInfo[index].dev) + 1; + JoyInfo[index].oldjoy = JoyInfo[index].id + 1; } else { diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 6a8145b0a..6b2cbec7b 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -728,7 +728,7 @@ static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt) // Determine the Joystick IDs for each current open joystick for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - if (evt.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev))) + if (evt.which == JoyInfo[i].id) { event.type = ev_joystick + i; break; @@ -788,7 +788,7 @@ static void Impl_HandleControllerHatEvent(SDL_ControllerButtonEvent evt, Uint32 // Determine the Joystick IDs for each current open joystick for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - if (evt.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev))) + if (evt.which == JoyInfo[i].id) { event.data1 = hat_buttons_base[i]; break; @@ -850,7 +850,7 @@ static void Impl_HandleControllerButtonEvent(SDL_ControllerButtonEvent evt, Uint // Determine the Joystick IDs for each current open joystick for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - if (evt.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(JoyInfo[i].dev))) + if (evt.which == JoyInfo[i].id) { event.data1 = buttons_base[i]; break; diff --git a/src/sdl/sdlmain.h b/src/sdl/sdlmain.h index 2caafc729..0fe93953a 100644 --- a/src/sdl/sdlmain.h +++ b/src/sdl/sdlmain.h @@ -42,6 +42,8 @@ typedef struct SDLJoyInfo_s { /// Controller handle SDL_GameController *dev; + /// Controller index + INT32 id; /// number of old joystick int oldjoy; /// number of axies From 736a626ccf7b36f6216954ec92b793200cc66b73 Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 23 Oct 2025 11:16:07 +0200 Subject: [PATCH 3/8] init joyinfo in I_InitJoystick --- src/sdl/i_system.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index dd197cd1b..4716fe97c 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -721,8 +721,8 @@ static void JoyReset(SDLJoyInfo_t *JoySet) } JoySet->dev = NULL; - JoySet->id = -1; JoySet->oldjoy = -1; + JoySet->id = -1; JoySet->axises = JoySet->buttons = JoySet->hats = JoySet->balls = 0; } @@ -994,6 +994,11 @@ void I_InitJoystick(UINT8 index) } } + JoyInfo[index].dev = NULL; + JoyInfo[index].oldjoy = -1; + JoyInfo[index].id = -1; + JoyInfo[index].axises = JoyInfo[index].buttons = JoyInfo[index].hats = JoyInfo[index].balls = 0; + if (cv_usejoystick[index].value) newcontroller = SDL_GameControllerOpen(cv_usejoystick[index].value-1); From 1d3dae01e90327da79aad1973d8ded510a30149e Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 23 Oct 2025 11:22:37 +0200 Subject: [PATCH 4/8] set SDL_HINT_AUTO_UPDATE_SENSORS hint to 0 we dont need any of that so disable it --- src/sdl/i_system.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 4716fe97c..2721e2e96 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -964,8 +964,9 @@ void I_InitJoystick(UINT8 index) UINT8 i; SDL_GameController *newcontroller = NULL; - //I_ShutdownJoystick(); - //SDL_SetHintWithPriority("SDL_XINPUT_ENABLED", "0", SDL_HINT_OVERRIDE); + // not sure if this is the best place to put this + SDL_SetHint(SDL_HINT_AUTO_UPDATE_SENSORS, "0"); + if (M_CheckParm("-nojoy")) return; From fcae07262510f9c04f8ba70a9ff8d3da75a18ab2 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 24 Oct 2025 18:17:17 +0200 Subject: [PATCH 5/8] refactor controller dpad menu repeat handling dont hack around manually checking for state checks just use sdl provided stuff and poll if a dpad button currently is being held --- src/d_main.c | 56 ----------------------------------------------- src/g_input.c | 3 --- src/i_video.h | 2 ++ src/m_menu.c | 27 +++-------------------- src/m_menu.h | 10 --------- src/sdl/i_video.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 93 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 1bc346deb..5b86be281 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -164,57 +164,6 @@ UINT8 ctrldown = 0; // 0x1 left, 0x2 right UINT8 altdown = 0; // 0x1 left, 0x2 right boolean capslock = 0; // gee i wonder what this does. -static void D_PadMenuScrollInput(UINT8 input) -{ - event_t dpadev; - memset(&dpadev, 0, sizeof(event_t)); - dpadev.type = ev_keydown; - - switch (input) - { - case DPAD_UP: - dpadev.data1 = KEY_UPARROW; - break; - case DPAD_DOWN: - dpadev.data1 = KEY_DOWNARROW; - break; - case DPAD_LEFT: - dpadev.data1 = KEY_LEFTARROW; - break; - case DPAD_RIGHT: - dpadev.data1 = KEY_RIGHTARROW; - break; - } - - D_PostEvent(&dpadev); // put into eventlist -} - -#define SCROLLDELAY 19 - -// Check if any dpad button is held -// and pass it to the eventlist -static void D_GamePadMenuScrollTicker(void) -{ - UINT8 i; - static UINT8 menuInputDelayTimer = 0; - - for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) - { - if (dpadscrollstate[i]) - { - if (menuInputDelayTimer < SCROLLDELAY) - menuInputDelayTimer++; - else if (menuInputDelayTimer == SCROLLDELAY) - D_PadMenuScrollInput(i); - - return; - } - } - - menuInputDelayTimer = 0; -} -#undef SCROLLDELAY - static UINT16 curcolor[MAXSPLITSCREENPLAYERS] = {0}; static void D_DeviceLEDTick(void) @@ -839,11 +788,6 @@ void D_SRB2Loop(void) if (!dedicated) { - if (menuactive) - { - D_GamePadMenuScrollTicker(); - } - D_DeviceLEDTick(); } } diff --git a/src/g_input.c b/src/g_input.c index e8bc8223e..89d887375 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -262,9 +262,6 @@ void G_ResetControls(void) memset(joyxmove, 0, sizeof(joyxmove)); memset(joyymove, 0, sizeof(joyymove)); - - // reset those just in case the game missed the keyup event - memset(dpadscrollstate, false, sizeof(dpadscrollstate)); } // diff --git a/src/i_video.h b/src/i_video.h index 852645c30..ad29983fa 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -143,6 +143,8 @@ boolean I_CheckNativeRes(void); void I_SetBorderlessWindow(void); +void I_HandleControllerHatRepeat(void); + #ifdef USE_FBO_OGL void RefreshOGLSDLSurface(void); diff --git a/src/m_menu.c b/src/m_menu.c index a7be33c42..3c95030d8 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -759,8 +759,6 @@ static void Command_Manual_f(void) itemOn = 0; } -boolean dpadscrollstate[4] = {false, false, false, false}; - // // M_Responder // @@ -807,19 +805,15 @@ boolean M_Responder(event_t *ev) break; case KEY_HAT1: ch = KEY_UPARROW; - dpadscrollstate[DPAD_UP] = true; break; case KEY_HAT1 + 1: ch = KEY_DOWNARROW; - dpadscrollstate[DPAD_DOWN] = true; break; case KEY_HAT1 + 2: ch = KEY_LEFTARROW; - dpadscrollstate[DPAD_LEFT] = true; break; case KEY_HAT1 + 3: ch = KEY_RIGHTARROW; - dpadscrollstate[DPAD_RIGHT] = true; break; } @@ -835,24 +829,6 @@ boolean M_Responder(event_t *ev) } } } - else if (ev->type == ev_keyup) - { - switch (ev->data1) // if you let go of those set those to false - { - case KEY_HAT1: - dpadscrollstate[DPAD_UP] = false; - break; - case KEY_HAT1 + 1: - dpadscrollstate[DPAD_DOWN] = false; - break; - case KEY_HAT1 + 2: - dpadscrollstate[DPAD_LEFT] = false; - break; - case KEY_HAT1 + 3: - dpadscrollstate[DPAD_RIGHT] = false; - break; - } - } else if (menuactive) { tic_t thistime = I_GetTime(); @@ -1731,6 +1707,9 @@ void M_Ticker(void) if (dedicated) return; + if (menuactive) + I_HandleControllerHatRepeat(); + if (--skullAnimCounter <= 0) skullAnimCounter = 8; diff --git a/src/m_menu.h b/src/m_menu.h index de4f3b983..22b768385 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -38,16 +38,6 @@ extern boolean forceshowhud; // Does all the real work of the menu interaction. boolean M_Responder(event_t *ev); -// for scrolling through menus with controllers -extern boolean dpadscrollstate[4]; -enum -{ - DPAD_UP, - DPAD_DOWN, - DPAD_LEFT, - DPAD_RIGHT -}; - // Called by main loop, runs for demo playback. If this returns true, nullify any further user input. boolean M_DemoResponder(event_t *ev); diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 6b2cbec7b..9152fd8b8 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -581,7 +581,9 @@ static void Impl_HandleKeyboardEvent(SDL_KeyboardEvent evt, Uint32 type) } if (event.data1) + { D_PostEvent(&event); + } } static void Impl_HandleMouseMotionEvent(SDL_MouseMotionEvent evt) @@ -719,6 +721,57 @@ static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) } } +static Uint32 hatrepeattimer[MAXSPLITSCREENPLAYERS]; +#define HATREPEATDELAY 19 + +void I_HandleControllerHatRepeat(void) +{ + static event_t event = {ev_keydown, 0, 0, 0}; + + // why bother if theres no controllers? + if (numcontrollers == 0) + return; + + static const SDL_GameControllerButton hatbutt[4] = + { + SDL_CONTROLLER_BUTTON_DPAD_UP, + SDL_CONTROLLER_BUTTON_DPAD_DOWN, + SDL_CONTROLLER_BUTTON_DPAD_LEFT, + SDL_CONTROLLER_BUTTON_DPAD_RIGHT + }; + + for (int i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (!cv_usejoystick[i].value) + continue; + + SDL_GameController *controller = JoyInfo[i].dev; + + if (!controller) + continue; + + for (UINT8 h = 0; h < JOYHATS; h++) + { + if (SDL_GameControllerGetButton(controller, hatbutt[h])) + { + if (hatrepeattimer[i] < HATREPEATDELAY) + { + hatrepeattimer[i]++; + } + else if (hatrepeattimer[i] == HATREPEATDELAY) + { + event.data1 = KEY_HAT1 + (i * JOYHATS) + h; + D_PostEvent(&event); + } + + return; + } + } + + hatrepeattimer[i] = 0; + } +} + static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt) { event_t event; From cdb7b6fedef3a4b99872bffec68167818c7efedb Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 24 Oct 2025 18:17:59 +0200 Subject: [PATCH 6/8] initialize input events to something --- src/sdl/i_video.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 9152fd8b8..abf82b4f3 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -553,7 +553,7 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) static void Impl_HandleKeyboardEvent(SDL_KeyboardEvent evt, Uint32 type) { - event_t event; + event_t event = {0}; switch (type) { @@ -635,7 +635,7 @@ static void Impl_HandleMouseMotionEvent(SDL_MouseMotionEvent evt) static void Impl_HandleMouseButtonEvent(SDL_MouseButtonEvent evt, Uint32 type) { - event_t event; + event_t event = {0}; // Ignore the event if the mouse is not actually focused on the window. // This can happen if you used the mouse to restore keyboard focus; @@ -690,7 +690,7 @@ static void Impl_HandleMouseButtonEvent(SDL_MouseButtonEvent evt, Uint32 type) static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) { - event_t event; + event_t event = {0}; if (USE_MOUSEINPUT) { @@ -726,12 +726,12 @@ static Uint32 hatrepeattimer[MAXSPLITSCREENPLAYERS]; void I_HandleControllerHatRepeat(void) { - static event_t event = {ev_keydown, 0, 0, 0}; - // why bother if theres no controllers? if (numcontrollers == 0) return; + event_t event = {ev_keydown, 0, 0, 0}; + static const SDL_GameControllerButton hatbutt[4] = { SDL_CONTROLLER_BUTTON_DPAD_UP, @@ -834,7 +834,7 @@ static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt) static void Impl_HandleControllerHatEvent(SDL_ControllerButtonEvent evt, Uint32 type) { - event_t event; + event_t event = {0}; UINT8 i; static const int hat_buttons_base[] = {KEY_HAT1, KEY_2HAT1, KEY_3HAT1, KEY_4HAT1}; @@ -888,7 +888,7 @@ static void Impl_HandleControllerHatEvent(SDL_ControllerButtonEvent evt, Uint32 static void Impl_HandleControllerButtonEvent(SDL_ControllerButtonEvent evt, Uint32 type) { - event_t event; + event_t event = {0}; UINT8 i; static const int buttons_base[] = {KEY_JOY1, KEY_2JOY1, KEY_3JOY1, KEY_4JOY1}; From 8ccf4eb9ba6ee0edf0e515e34311e94eb18e9aca Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 24 Oct 2025 18:27:30 +0200 Subject: [PATCH 7/8] this can be alot smoler --- src/sdl/i_video.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index abf82b4f3..9993a2134 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -721,7 +721,7 @@ static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) } } -static Uint32 hatrepeattimer[MAXSPLITSCREENPLAYERS]; +static UINT8 hatrepeattimer[MAXSPLITSCREENPLAYERS]; #define HATREPEATDELAY 19 void I_HandleControllerHatRepeat(void) From d4a130f1dc73f3dc99711262ab3afdafaf2fec95 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 24 Oct 2025 18:28:57 +0200 Subject: [PATCH 8/8] remove those memsets not needed anymore now --- src/sdl/i_video.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 9993a2134..d2604277c 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -648,8 +648,6 @@ static void Impl_HandleMouseButtonEvent(SDL_MouseButtonEvent evt, Uint32 type) /// \todo inputEvent.button.which if (USE_MOUSEINPUT) { - SDL_memset(&event, 0, sizeof(event_t)); - switch (type) { case SDL_MOUSEBUTTONUP: @@ -694,8 +692,6 @@ static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) if (USE_MOUSEINPUT) { - SDL_memset(&event, 0, sizeof(event_t)); - if (evt.y > 0) { event.data1 = KEY_MOUSEWHEELUP;