hid: implemented controller type auto detection

This commit is contained in:
Hyper 2024-12-11 20:53:00 +00:00
parent cdcacff53b
commit 59551d8875
8 changed files with 36 additions and 24 deletions

View file

@ -11,6 +11,7 @@
#include <decompressor.h> #include <decompressor.h>
#include <kernel/function.h> #include <kernel/function.h>
#include <kernel/heap.h> #include <kernel/heap.h>
#include <hid/hid_detail.h>
#include <kernel/memory.h> #include <kernel/memory.h>
#include <kernel/xdbf.h> #include <kernel/xdbf.h>
#include <res/bc_diff/button_bc_diff.bin.h> #include <res/bc_diff/button_bc_diff.bin.h>
@ -2710,7 +2711,12 @@ static void ProcSetViewport(const RenderCommand& cmd)
static void SetTexture(GuestDevice* device, uint32_t index, GuestTexture* texture) 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(); texture = texture->patchedTexture.get();
RenderCommand cmd; RenderCommand cmd;

View file

@ -126,6 +126,7 @@ public:
}; };
std::array<Controller, 4> g_controllers; std::array<Controller, 4> g_controllers;
Controller* g_activeController;
inline Controller* EnsureController(DWORD dwUserIndex) inline Controller* EnsureController(DWORD dwUserIndex)
{ {
@ -157,6 +158,13 @@ inline Controller* FindController(int which)
return nullptr; 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) int HID_OnSDLEvent(void*, SDL_Event* event)
{ {
if (event->type >= SDL_CONTROLLERAXISMOTION && event->type < SDL_FINGERDOWN) 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 (event->type == SDL_CONTROLLERAXISMOTION)
{ {
if (abs(event->caxis.value) > 8000)
SetControllerInputDevice(controller);
controller->PollAxis(); controller->PollAxis();
} }
else else
{ {
SetControllerInputDevice(controller);
controller->Poll(); 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++; pState->dwPacketNumber = packet++;
SDL_JoystickUpdate(); if (!g_activeController)
if (!EnsureController(dwUserIndex))
return ERROR_DEVICE_NOT_CONNECTED; return ERROR_DEVICE_NOT_CONNECTED;
pState->Gamepad = g_controllers[dwUserIndex].state; pState->Gamepad = g_activeController->state;
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@ -249,14 +258,10 @@ uint32_t hid::detail::SetState(uint32_t dwUserIndex, XAMINPUT_VIBRATION* pVibrat
if (!pVibration) if (!pVibration)
return ERROR_BAD_ARGUMENTS; return ERROR_BAD_ARGUMENTS;
SDL_JoystickUpdate(); if (!g_activeController)
auto* controller = EnsureController(dwUserIndex);
if (!controller)
return ERROR_DEVICE_NOT_CONNECTED; return ERROR_DEVICE_NOT_CONNECTED;
controller->SetVibration(*pVibration); g_activeController->SetVibration(*pVibration);
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@ -266,11 +271,7 @@ uint32_t hid::detail::GetCapabilities(uint32_t dwUserIndex, XAMINPUT_CAPABILITIE
if (!pCaps) if (!pCaps)
return ERROR_BAD_ARGUMENTS; return ERROR_BAD_ARGUMENTS;
SDL_JoystickUpdate(); if (!g_activeController)
auto* controller = EnsureController(dwUserIndex);
if (!controller)
return ERROR_DEVICE_NOT_CONNECTED; return ERROR_DEVICE_NOT_CONNECTED;
memset(pCaps, 0, sizeof(*pCaps)); 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->Type = XAMINPUT_DEVTYPE_GAMEPAD;
pCaps->SubType = XAMINPUT_DEVSUBTYPE_GAMEPAD; // TODO: other types? pCaps->SubType = XAMINPUT_DEVSUBTYPE_GAMEPAD; // TODO: other types?
pCaps->Flags = 0; pCaps->Flags = 0;
pCaps->Gamepad = controller->state; pCaps->Gamepad = g_activeController->state;
pCaps->Vibration = controller->vibration; pCaps->Vibration = g_activeController->vibration;
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }

View file

@ -3,6 +3,7 @@
#include "hid_detail.h" #include "hid_detail.h"
hid::detail::EInputDevice hid::detail::g_inputDevice; hid::detail::EInputDevice hid::detail::g_inputDevice;
hid::detail::EInputDevice hid::detail::g_inputDeviceController;
void hid::Init() void hid::Init()
{ {

View file

@ -11,6 +11,7 @@ namespace hid::detail
}; };
extern EInputDevice g_inputDevice; extern EInputDevice g_inputDevice;
extern EInputDevice g_inputDeviceController;
void Init(); void Init();

View file

@ -141,6 +141,7 @@ CONFIG_DEFINE_ENUM_LOCALE(EControllerIcons)
{ {
ELanguage::English, ELanguage::English,
{ {
{ EControllerIcons::Auto, { "AUTO", "Auto: the game will determine which icons\nto use based on the current input device." } },
{ EControllerIcons::Xbox, { "XBOX", "" } }, { EControllerIcons::Xbox, { "XBOX", "" } },
{ EControllerIcons::PlayStation, { "PLAYSTATION", "" } } { EControllerIcons::PlayStation, { "PLAYSTATION", "" } }
} }

View file

@ -62,9 +62,9 @@ std::tuple<std::tuple<ImVec2, ImVec2>, GuestTexture*> GetButtonIcon(EButtonIcon
std::tuple<ImVec2, ImVec2> btn; std::tuple<ImVec2, ImVec2> btn;
GuestTexture* texture; GuestTexture* texture;
auto isPlayStation = App::s_isInit auto isPlayStation = Config::ControllerIcons == EControllerIcons::Auto
? Config::ControllerIcons == EControllerIcons::PlayStation ? hid::detail::g_inputDeviceController == hid::detail::EInputDevice::PlayStation
: hid::detail::g_inputDevice == hid::detail::EInputDevice::PlayStation; : Config::ControllerIcons == EControllerIcons::PlayStation;
auto yOffsetCmn = isPlayStation ? 42 : 0; auto yOffsetCmn = isPlayStation ? 42 : 0;
auto yOffsetStartBack = isPlayStation ? 46 : 0; auto yOffsetStartBack = isPlayStation ? 46 : 0;

View file

@ -24,7 +24,7 @@ public:
CONFIG_DEFINE_LOCALISED("Input", bool, XButtonHoming, true); CONFIG_DEFINE_LOCALISED("Input", bool, XButtonHoming, true);
CONFIG_DEFINE_LOCALISED("Input", bool, AllowCancellingUnleash, false); CONFIG_DEFINE_LOCALISED("Input", bool, AllowCancellingUnleash, false);
CONFIG_DEFINE_LOCALISED("Input", bool, AllowBackgroundInput, 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, MusicVolume, 1.0f);
CONFIG_DEFINE_LOCALISED("Audio", float, EffectsVolume, 1.0f); CONFIG_DEFINE_LOCALISED("Audio", float, EffectsVolume, 1.0f);

View file

@ -69,12 +69,14 @@ CONFIG_DEFINE_ENUM_TEMPLATE(ETimeOfDayTransition)
enum class EControllerIcons : uint32_t enum class EControllerIcons : uint32_t
{ {
Auto,
Xbox, Xbox,
PlayStation PlayStation
}; };
CONFIG_DEFINE_ENUM_TEMPLATE(EControllerIcons) CONFIG_DEFINE_ENUM_TEMPLATE(EControllerIcons)
{ {
{ "Auto", EControllerIcons::Auto },
{ "Xbox", EControllerIcons::Xbox }, { "Xbox", EControllerIcons::Xbox },
{ "PlayStation", EControllerIcons::PlayStation } { "PlayStation", EControllerIcons::PlayStation }
}; };