From 59551d887599c140189d1bf7173117609a823321 Mon Sep 17 00:00:00 2001 From: Hyper <34012267+hyperbx@users.noreply.github.com> Date: Wed, 11 Dec 2024 20:53:00 +0000 Subject: [PATCH] hid: implemented controller type auto detection --- UnleashedRecomp/gpu/video.cpp | 8 +++++- UnleashedRecomp/hid/driver/sdl_hid.cpp | 39 +++++++++++++------------- UnleashedRecomp/hid/hid.cpp | 1 + UnleashedRecomp/hid/hid_detail.h | 1 + UnleashedRecomp/locale/config_locale.h | 1 + UnleashedRecomp/ui/button_guide.cpp | 6 ++-- UnleashedRecomp/user/config.h | 2 +- UnleashedRecomp/user/config_detail.h | 2 ++ 8 files changed, 36 insertions(+), 24 deletions(-) diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index af10285..921ec41 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -2710,7 +2711,12 @@ static void ProcSetViewport(const RenderCommand& cmd) static void SetTexture(GuestDevice* device, uint32_t index, GuestTexture* texture) { - if (Config::ControllerIcons == EControllerIcons::PlayStation && texture != nullptr && texture->patchedTexture != nullptr) + auto isPlayStation = Config::ControllerIcons == EControllerIcons::PlayStation; + + if (Config::ControllerIcons == EControllerIcons::Auto) + isPlayStation = hid::detail::g_inputDeviceController == hid::detail::EInputDevice::PlayStation; + + if (isPlayStation && texture != nullptr && texture->patchedTexture != nullptr) texture = texture->patchedTexture.get(); RenderCommand cmd; diff --git a/UnleashedRecomp/hid/driver/sdl_hid.cpp b/UnleashedRecomp/hid/driver/sdl_hid.cpp index 2aab5f5..7fa0703 100644 --- a/UnleashedRecomp/hid/driver/sdl_hid.cpp +++ b/UnleashedRecomp/hid/driver/sdl_hid.cpp @@ -126,6 +126,7 @@ public: }; std::array g_controllers; +Controller* g_activeController; inline Controller* EnsureController(DWORD dwUserIndex) { @@ -157,6 +158,13 @@ inline Controller* FindController(int which) return nullptr; } +static void SetControllerInputDevice(Controller* controller) +{ + g_activeController = controller; + hid::detail::g_inputDevice = controller->GetInputDevice(); + hid::detail::g_inputDeviceController = hid::detail::g_inputDevice; +} + int HID_OnSDLEvent(void*, SDL_Event* event) { if (event->type >= SDL_CONTROLLERAXISMOTION && event->type < SDL_FINGERDOWN) @@ -183,14 +191,17 @@ int HID_OnSDLEvent(void*, SDL_Event* event) { if (event->type == SDL_CONTROLLERAXISMOTION) { + if (abs(event->caxis.value) > 8000) + SetControllerInputDevice(controller); + controller->PollAxis(); } else { + SetControllerInputDevice(controller); + controller->Poll(); } - - hid::detail::g_inputDevice = controller->GetInputDevice(); } } } @@ -234,12 +245,10 @@ uint32_t hid::detail::GetState(uint32_t dwUserIndex, XAMINPUT_STATE* pState) pState->dwPacketNumber = packet++; - SDL_JoystickUpdate(); - - if (!EnsureController(dwUserIndex)) + if (!g_activeController) return ERROR_DEVICE_NOT_CONNECTED; - pState->Gamepad = g_controllers[dwUserIndex].state; + pState->Gamepad = g_activeController->state; return ERROR_SUCCESS; } @@ -249,14 +258,10 @@ uint32_t hid::detail::SetState(uint32_t dwUserIndex, XAMINPUT_VIBRATION* pVibrat if (!pVibration) return ERROR_BAD_ARGUMENTS; - SDL_JoystickUpdate(); - - auto* controller = EnsureController(dwUserIndex); - - if (!controller) + if (!g_activeController) return ERROR_DEVICE_NOT_CONNECTED; - controller->SetVibration(*pVibration); + g_activeController->SetVibration(*pVibration); return ERROR_SUCCESS; } @@ -266,11 +271,7 @@ uint32_t hid::detail::GetCapabilities(uint32_t dwUserIndex, XAMINPUT_CAPABILITIE if (!pCaps) return ERROR_BAD_ARGUMENTS; - SDL_JoystickUpdate(); - - auto* controller = EnsureController(dwUserIndex); - - if (!controller) + if (!g_activeController) return ERROR_DEVICE_NOT_CONNECTED; memset(pCaps, 0, sizeof(*pCaps)); @@ -278,8 +279,8 @@ uint32_t hid::detail::GetCapabilities(uint32_t dwUserIndex, XAMINPUT_CAPABILITIE pCaps->Type = XAMINPUT_DEVTYPE_GAMEPAD; pCaps->SubType = XAMINPUT_DEVSUBTYPE_GAMEPAD; // TODO: other types? pCaps->Flags = 0; - pCaps->Gamepad = controller->state; - pCaps->Vibration = controller->vibration; + pCaps->Gamepad = g_activeController->state; + pCaps->Vibration = g_activeController->vibration; return ERROR_SUCCESS; } diff --git a/UnleashedRecomp/hid/hid.cpp b/UnleashedRecomp/hid/hid.cpp index 2c0d860..7ac324a 100644 --- a/UnleashedRecomp/hid/hid.cpp +++ b/UnleashedRecomp/hid/hid.cpp @@ -3,6 +3,7 @@ #include "hid_detail.h" hid::detail::EInputDevice hid::detail::g_inputDevice; +hid::detail::EInputDevice hid::detail::g_inputDeviceController; void hid::Init() { diff --git a/UnleashedRecomp/hid/hid_detail.h b/UnleashedRecomp/hid/hid_detail.h index ab6eb70..9f2b22c 100644 --- a/UnleashedRecomp/hid/hid_detail.h +++ b/UnleashedRecomp/hid/hid_detail.h @@ -11,6 +11,7 @@ namespace hid::detail }; extern EInputDevice g_inputDevice; + extern EInputDevice g_inputDeviceController; void Init(); diff --git a/UnleashedRecomp/locale/config_locale.h b/UnleashedRecomp/locale/config_locale.h index e913cdb..f219077 100644 --- a/UnleashedRecomp/locale/config_locale.h +++ b/UnleashedRecomp/locale/config_locale.h @@ -141,6 +141,7 @@ CONFIG_DEFINE_ENUM_LOCALE(EControllerIcons) { ELanguage::English, { + { EControllerIcons::Auto, { "AUTO", "Auto: the game will determine which icons\nto use based on the current input device." } }, { EControllerIcons::Xbox, { "XBOX", "" } }, { EControllerIcons::PlayStation, { "PLAYSTATION", "" } } } diff --git a/UnleashedRecomp/ui/button_guide.cpp b/UnleashedRecomp/ui/button_guide.cpp index 1bec66a..b983a74 100644 --- a/UnleashedRecomp/ui/button_guide.cpp +++ b/UnleashedRecomp/ui/button_guide.cpp @@ -62,9 +62,9 @@ std::tuple, GuestTexture*> GetButtonIcon(EButtonIcon std::tuple btn; GuestTexture* texture; - auto isPlayStation = App::s_isInit - ? Config::ControllerIcons == EControllerIcons::PlayStation - : hid::detail::g_inputDevice == hid::detail::EInputDevice::PlayStation; + auto isPlayStation = Config::ControllerIcons == EControllerIcons::Auto + ? hid::detail::g_inputDeviceController == hid::detail::EInputDevice::PlayStation + : Config::ControllerIcons == EControllerIcons::PlayStation; auto yOffsetCmn = isPlayStation ? 42 : 0; auto yOffsetStartBack = isPlayStation ? 46 : 0; diff --git a/UnleashedRecomp/user/config.h b/UnleashedRecomp/user/config.h index 9b42c01..5cdb15f 100644 --- a/UnleashedRecomp/user/config.h +++ b/UnleashedRecomp/user/config.h @@ -24,7 +24,7 @@ public: CONFIG_DEFINE_LOCALISED("Input", bool, XButtonHoming, true); CONFIG_DEFINE_LOCALISED("Input", bool, AllowCancellingUnleash, false); CONFIG_DEFINE_LOCALISED("Input", bool, AllowBackgroundInput, false); - CONFIG_DEFINE_ENUM_LOCALISED("Input", EControllerIcons, ControllerIcons, EControllerIcons::Xbox); + CONFIG_DEFINE_ENUM_LOCALISED("Input", EControllerIcons, ControllerIcons, EControllerIcons::Auto); CONFIG_DEFINE_LOCALISED("Audio", float, MusicVolume, 1.0f); CONFIG_DEFINE_LOCALISED("Audio", float, EffectsVolume, 1.0f); diff --git a/UnleashedRecomp/user/config_detail.h b/UnleashedRecomp/user/config_detail.h index 455e52a..86b1514 100644 --- a/UnleashedRecomp/user/config_detail.h +++ b/UnleashedRecomp/user/config_detail.h @@ -69,12 +69,14 @@ CONFIG_DEFINE_ENUM_TEMPLATE(ETimeOfDayTransition) enum class EControllerIcons : uint32_t { + Auto, Xbox, PlayStation }; CONFIG_DEFINE_ENUM_TEMPLATE(EControllerIcons) { + { "Auto", EControllerIcons::Auto }, { "Xbox", EControllerIcons::Xbox }, { "PlayStation", EControllerIcons::PlayStation } };