From 26a297f1dea145b2f03dda1d73992fa70204561b Mon Sep 17 00:00:00 2001 From: str1k3r <115313679+S1l3ntStr1ke87@users.noreply.github.com> Date: Sun, 9 Aug 2026 02:02:37 -0400 Subject: [PATCH] fix: main pad idx not swapping when controller disconnects --- 4J_Input/Windows64/INP_Main.cpp | 69 ++++++++++++++++++++++++++++----- 4J_Input/Windows64/INP_Main.h | 2 + 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/4J_Input/Windows64/INP_Main.cpp b/4J_Input/Windows64/INP_Main.cpp index 9577e98..f6070a6 100644 --- a/4J_Input/Windows64/INP_Main.cpp +++ b/4J_Input/Windows64/INP_Main.cpp @@ -647,23 +647,72 @@ void CInput::HandleGamepadEvents() } break; case SDL_EVENT_GAMEPAD_REMOVED: - for (int i = 0; i < MAX_JOYPADS; i++) - { - if (m_Joypads[i].m_pGamepad && - SDL_GetGamepadID(m_Joypads[i].m_pGamepad) == event.gdevice.which) - { - SDL_CloseGamepad(m_Joypads[i].m_pGamepad); - m_Joypads[i].m_pGamepad = nullptr; - m_Joypads[i].m_bIsConnected = false; + { + int removedIndex = -1; - break; + for (int i = 0; i < MAX_JOYPADS; i++) + { + if (m_Joypads[i].m_pGamepad && + SDL_GetGamepadID(m_Joypads[i].m_pGamepad) == event.gdevice.which) + { + SDL_CloseGamepad(m_Joypads[i].m_pGamepad); + m_Joypads[i].m_pGamepad = nullptr; + m_Joypads[i].m_bIsConnected = false; + removedIndex = i; + break; + } } + + if (removedIndex == 0) + { + for (int i = 1; i < MAX_JOYPADS; i++) + { + if (m_Joypads[i].m_bIsConnected && m_Joypads[i].m_pGamepad) + { + ShiftGamepadData(0, i); + SDL_SetGamepadPlayerIndex(m_Joypads[0].m_pGamepad, 0); + ClearGamepadSlot(i); + break; + } + } + } + break; } - break; } } } +void CInput::ShiftGamepadData(int destIndex, int srcIndex) +{ + m_Joypads[destIndex] = m_Joypads[srcIndex]; + + m_Joypads[destIndex].m_pfLeftThumbXAxisMap = &m_Joypads[destIndex].m_fNormalizedLeftThumbX; + m_Joypads[destIndex].m_pfLeftThumbYAxisMap = &m_Joypads[destIndex].m_fNormalizedLeftThumbY; + m_Joypads[destIndex].m_pfRightThumbXAxisMap = &m_Joypads[destIndex].m_fNormalizedRightThumbX; + m_Joypads[destIndex].m_pfRightThumbYAxisMap = &m_Joypads[destIndex].m_fNormalizedRightThumbY; + m_Joypads[destIndex].m_pucLeftTriggerAxisMap = &m_Joypads[destIndex].m_ucLeftTriggerState; + m_Joypads[destIndex].m_pucRightTriggerAxisMap = &m_Joypads[destIndex].m_ucRightTriggerState; +} + +void CInput::ClearGamepadSlot(int index) +{ + m_Joypads[index].m_pGamepad = nullptr; + m_Joypads[index].m_bIsConnected = false; + m_Joypads[index].m_uiButtons = 0; + m_Joypads[index].m_ucInputStateIndex = -1; + m_Joypads[index].m_ucLeftTriggerState = 0; + m_Joypads[index].m_ucRightTriggerState = 0; + m_Joypads[index].m_iRightThumbX = 0; + m_Joypads[index].m_iRightThumbY = 0; + + m_Joypads[index].m_pfLeftThumbXAxisMap = &m_Joypads[index].m_fNormalizedLeftThumbX; + m_Joypads[index].m_pfLeftThumbYAxisMap = &m_Joypads[index].m_fNormalizedLeftThumbY; + m_Joypads[index].m_pfRightThumbXAxisMap = &m_Joypads[index].m_fNormalizedRightThumbX; + m_Joypads[index].m_pfRightThumbYAxisMap = &m_Joypads[index].m_fNormalizedRightThumbY; + m_Joypads[index].m_pucLeftTriggerAxisMap = &m_Joypads[index].m_ucLeftTriggerState; + m_Joypads[index].m_pucRightTriggerAxisMap = &m_Joypads[index].m_ucRightTriggerState; +} + bool CInput::UpdateJoypads() { assert(m_bJoypadMapArrayIsSetup); diff --git a/4J_Input/Windows64/INP_Main.h b/4J_Input/Windows64/INP_Main.h index 30e09b6..e95a1d0 100644 --- a/4J_Input/Windows64/INP_Main.h +++ b/4J_Input/Windows64/INP_Main.h @@ -157,6 +157,8 @@ public: FLOAT GetIdleSeconds(int iPad); void HandleGamepadEvents(); + void ShiftGamepadData(int destIndex, int srcIndex); + void ClearGamepadSlot(int index); bool UpdateJoypads(void); void ClearJoypadValues(JOYPAD *pThisPad);