From 9ff65e8c5e6e7b75a6b9556d7ff5d30f151b1aa0 Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 23 Oct 2025 10:53:41 +0200 Subject: [PATCH] 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 4848b53c9..9bdaa3d46 100644 --- a/src/i_system.h +++ b/src/i_system.h @@ -225,10 +225,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 a6de3e8a5..38aaafef7 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -1817,7 +1817,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 5faf1b291..20ca205bb 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -725,17 +725,13 @@ static void JoyReset(SDLJoyInfo_t *JoySet) { SDL_GameControllerClose(JoySet->dev); } + JoySet->dev = NULL; JoySet->oldjoy = -1; JoySet->id = -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]; @@ -795,10 +791,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) @@ -806,6 +804,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! @@ -813,6 +812,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. @@ -835,14 +835,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 */ @@ -854,74 +846,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 @@ -938,6 +890,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")); @@ -972,6 +925,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); @@ -1073,14 +1027,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 = JoyInfo[index].id + 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++) @@ -1145,8 +1098,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; } @@ -1157,15 +1112,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 6099b60a1..880f53f75 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -707,16 +707,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); @@ -727,37 +730,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] = JoyInfo[i].id; + if (evt.which == JoyInfo[i].id) + { + 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; @@ -793,47 +783,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 == JoyInfo[i].id) + { + 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] = JoyInfo[i].id; + if (evt.which == JoyInfo[i].id) + { + 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) @@ -856,7 +888,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) @@ -1099,7 +1133,6 @@ void I_StartupMouse(void) void I_OsPolling(void) { SDL_Keymod mod; - INT32 i; if (consolevent) I_GetConsoleEvents(); @@ -1107,9 +1140,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"