From 4a3388351292a4c6dc6d175ba1905ed3c2bce850 Mon Sep 17 00:00:00 2001 From: Hyper <34012267+hyperbx@users.noreply.github.com> Date: Sun, 16 Feb 2025 19:21:58 +0000 Subject: [PATCH] hid: implemented delay input for switching devices This makes controllers have their first input ignored until after detection, much like other PC ports. This probably won't end up being merged in though, as other Sonic PC ports don't seem to work this way, but I'll push it anyway just for the sake of having this somewhere. --- UnleashedRecomp/gpu/video.cpp | 2 +- UnleashedRecomp/hid/driver/sdl_hid.cpp | 40 +++++++++++++------ UnleashedRecomp/hid/hid.cpp | 9 +++-- UnleashedRecomp/hid/hid.h | 6 ++- UnleashedRecomp/patches/input_patches.cpp | 2 +- UnleashedRecomp/patches/object_patches.cpp | 2 +- UnleashedRecomp/patches/resident_patches.cpp | 2 +- UnleashedRecomp/ui/button_guide.cpp | 2 +- .../ui/options_menu_thumbnails.cpp | 2 +- 9 files changed, 43 insertions(+), 24 deletions(-) diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index 3b061dab..6758e205 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -3596,7 +3596,7 @@ static void SetTexture(GuestDevice* device, uint32_t index, GuestTexture* textur auto isPlayStation = Config::ControllerIcons == EControllerIcons::PlayStation; if (Config::ControllerIcons == EControllerIcons::Auto) - isPlayStation = hid::g_inputDeviceController == hid::EInputDevice::PlayStation; + isPlayStation = hid::g_inputDevicePad == hid::EInputDevice::PlayStation; if (isPlayStation && texture != nullptr && texture->patchedTexture != nullptr) texture = texture->patchedTexture.get(); diff --git a/UnleashedRecomp/hid/driver/sdl_hid.cpp b/UnleashedRecomp/hid/driver/sdl_hid.cpp index 85e6793a..2253c56a 100644 --- a/UnleashedRecomp/hid/driver/sdl_hid.cpp +++ b/UnleashedRecomp/hid/driver/sdl_hid.cpp @@ -174,17 +174,16 @@ static void SetControllerInputDevice(Controller* controller) if (App::s_isLoading) return; + // Signal that we've changed input device to block first input. + if (hid::g_inputDevice == hid::EInputDevice::Keyboard) + hid::g_hasChangedInputDevice = true; + hid::g_inputDevice = controller->GetInputDevice(); - hid::g_inputDeviceController = hid::g_inputDevice; + hid::g_inputDevicePad = hid::g_inputDevice; + hid::g_inputDevicePadExplicit = (hid::EInputDeviceExplicit)controller->GetControllerType(); - auto controllerType = (hid::EInputDeviceExplicit)controller->GetControllerType(); - - if (hid::g_inputDeviceExplicit != controllerType) - { - hid::g_inputDeviceExplicit = controllerType; - - LOGFN("Detected controller: {}", hid::GetInputDeviceName()); - } + if (hid::g_hasChangedInputDevice) + LOGFN("Input Device: {}", hid::GetInputDeviceName()); } static void SetControllerTimeOfDayLED(Controller& controller, bool isNight) @@ -244,14 +243,16 @@ int HID_OnSDLEvent(void*, SDL_Event* event) SetControllerInputDevice(controller); } - controller->PollAxis(); + if (!hid::g_hasChangedInputDevice) + controller->PollAxis(); } else { SDL_ShowCursor(SDL_DISABLE); SetControllerInputDevice(controller); - controller->Poll(); + if (!hid::g_hasChangedInputDevice) + controller->Poll(); } break; @@ -259,8 +260,17 @@ int HID_OnSDLEvent(void*, SDL_Event* event) case SDL_KEYDOWN: case SDL_KEYUP: - hid::g_inputDevice = hid::EInputDevice::Keyboard; + { + if (hid::g_inputDevice != hid::EInputDevice::Keyboard) + { + hid::g_inputDevice = hid::EInputDevice::Keyboard; + hid::g_hasChangedInputDevice = true; + + LOGN("Input Device: Keyboard"); + } + break; + } case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: @@ -331,6 +341,12 @@ uint32_t hid::GetState(uint32_t dwUserIndex, XAMINPUT_STATE* pState) if (!g_activeController) return ERROR_DEVICE_NOT_CONNECTED; + if (hid::g_hasChangedInputDevice) + { + hid::g_hasChangedInputDevice = false; + return ERROR_SUCCESS; + } + pState->Gamepad = g_activeController->state; return ERROR_SUCCESS; diff --git a/UnleashedRecomp/hid/hid.cpp b/UnleashedRecomp/hid/hid.cpp index 0a56224a..44254426 100644 --- a/UnleashedRecomp/hid/hid.cpp +++ b/UnleashedRecomp/hid/hid.cpp @@ -2,9 +2,10 @@ #include #include -hid::EInputDevice hid::g_inputDevice; -hid::EInputDevice hid::g_inputDeviceController; -hid::EInputDeviceExplicit hid::g_inputDeviceExplicit; +hid::EInputDevice hid::g_inputDevice = EInputDevice::None; +hid::EInputDevice hid::g_inputDevicePad = EInputDevice::None; +hid::EInputDeviceExplicit hid::g_inputDevicePadExplicit = EInputDeviceExplicit::Unknown; +bool hid::g_hasChangedInputDevice; uint16_t hid::g_prohibitedButtons; bool hid::g_isLeftStickProhibited; @@ -39,7 +40,7 @@ std::string hid::GetInputDeviceName() return "Mouse"; } - switch (g_inputDeviceExplicit) + switch (g_inputDevicePadExplicit) { case EInputDeviceExplicit::Xbox360: return "Xbox 360"; diff --git a/UnleashedRecomp/hid/hid.h b/UnleashedRecomp/hid/hid.h index 730694a9..f1dce0fc 100644 --- a/UnleashedRecomp/hid/hid.h +++ b/UnleashedRecomp/hid/hid.h @@ -4,6 +4,7 @@ namespace hid { enum class EInputDevice { + None, Keyboard, Mouse, Xbox, @@ -29,8 +30,9 @@ namespace hid }; extern EInputDevice g_inputDevice; - extern EInputDevice g_inputDeviceController; - extern EInputDeviceExplicit g_inputDeviceExplicit; + extern EInputDevice g_inputDevicePad; + extern EInputDeviceExplicit g_inputDevicePadExplicit; + extern bool g_hasChangedInputDevice; extern uint16_t g_prohibitedButtons; extern bool g_isLeftStickProhibited; diff --git a/UnleashedRecomp/patches/input_patches.cpp b/UnleashedRecomp/patches/input_patches.cpp index 6c5c9813..b313f9ec 100644 --- a/UnleashedRecomp/patches/input_patches.cpp +++ b/UnleashedRecomp/patches/input_patches.cpp @@ -186,7 +186,7 @@ public: case SDL_CONTROLLERTOUCHPADDOWN: { - g_worldMapCursorParams = hid::g_inputDeviceExplicit == hid::EInputDeviceExplicit::DualSense + g_worldMapCursorParams = hid::g_inputDevicePadExplicit == hid::EInputDeviceExplicit::DualSense ? (WorldMapCursorParams)g_worldMapCursorParamsProspero : (WorldMapCursorParams)g_worldMapCursorParamsOrbis; diff --git a/UnleashedRecomp/patches/object_patches.cpp b/UnleashedRecomp/patches/object_patches.cpp index 45e28223..b97b169f 100644 --- a/UnleashedRecomp/patches/object_patches.cpp +++ b/UnleashedRecomp/patches/object_patches.cpp @@ -80,7 +80,7 @@ PPC_FUNC(sub_82B14CC0) auto isPlayStation = Config::ControllerIcons == EControllerIcons::PlayStation; if (Config::ControllerIcons == EControllerIcons::Auto) - isPlayStation = hid::g_inputDeviceController == hid::EInputDevice::PlayStation; + isPlayStation = hid::g_inputDevicePad == hid::EInputDevice::PlayStation; if (isPlayStation) { diff --git a/UnleashedRecomp/patches/resident_patches.cpp b/UnleashedRecomp/patches/resident_patches.cpp index 791b4153..592abf95 100644 --- a/UnleashedRecomp/patches/resident_patches.cpp +++ b/UnleashedRecomp/patches/resident_patches.cpp @@ -161,7 +161,7 @@ void LoadingScreenControllerMidAsmHook() auto isPlayStation = Config::ControllerIcons == EControllerIcons::PlayStation; if (Config::ControllerIcons == EControllerIcons::Auto) - isPlayStation = hid::g_inputDeviceController == hid::EInputDevice::PlayStation; + isPlayStation = hid::g_inputDevicePad == hid::EInputDevice::PlayStation; const char* prefix = isPlayStation ? "ps3" : "360"; diff --git a/UnleashedRecomp/ui/button_guide.cpp b/UnleashedRecomp/ui/button_guide.cpp index 9a7a8d1b..59ec3db2 100644 --- a/UnleashedRecomp/ui/button_guide.cpp +++ b/UnleashedRecomp/ui/button_guide.cpp @@ -67,7 +67,7 @@ std::tuple, GuestTexture*> GetButtonIcon(EButtonIcon GuestTexture* texture; auto isPlayStation = Config::ControllerIcons == EControllerIcons::Auto - ? hid::g_inputDeviceController == hid::EInputDevice::PlayStation + ? hid::g_inputDevicePad == hid::EInputDevice::PlayStation : Config::ControllerIcons == EControllerIcons::PlayStation; auto yOffsetCmn = isPlayStation ? 42 : 0; diff --git a/UnleashedRecomp/ui/options_menu_thumbnails.cpp b/UnleashedRecomp/ui/options_menu_thumbnails.cpp index 915eeba8..0e515b3d 100644 --- a/UnleashedRecomp/ui/options_menu_thumbnails.cpp +++ b/UnleashedRecomp/ui/options_menu_thumbnails.cpp @@ -185,7 +185,7 @@ GuestTexture* GetThumbnail(const IConfigDef* cfg) bool isPlayStation = Config::ControllerIcons == EControllerIcons::PlayStation; if (Config::ControllerIcons == EControllerIcons::Auto) - isPlayStation = hid::g_inputDeviceController == hid::EInputDevice::PlayStation; + isPlayStation = hid::g_inputDevicePad == hid::EInputDevice::PlayStation; texture = isPlayStation ? g_namedThumbnails["ControlTutorialPS"].get() : g_namedThumbnails["ControlTutorialXB"].get(); }