Fix for XInput controllers having a different ID range

This commit is contained in:
kalaposfos13 2025-04-15 17:15:58 +02:00
parent 781a2d0b8d
commit 858dda2bd0
5 changed files with 30 additions and 18 deletions

View file

@ -3,6 +3,7 @@
#include <unordered_set>
#include <SDL3/SDL.h>
#include <common/singleton.h>
#include "common/config.h"
#include "common/logging/log.h"
#include "core/libraries/kernel/time.h"
@ -358,4 +359,15 @@ u32 GameController::Poll() {
return 100;
}
u8 GameControllers::GetGamepadIndexFromJoystickId(SDL_JoystickID id) {
auto controllers = *Common::Singleton<GameControllers>::Instance();
for (int i = 0; i < 4; i++) {
if (controllers[i]->m_sdl_gamepad &&
SDL_GetGamepadID(controllers[i]->m_sdl_gamepad) == id) {
return i + 1;
}
}
return -1; // Not found
}
} // namespace Input

View file

@ -8,6 +8,8 @@
#include "common/types.h"
#include "core/libraries/pad/pad.h"
#include "SDL3/SDL_joystick.h"
struct SDL_Gamepad;
namespace Input {
@ -106,6 +108,8 @@ public:
return controllers[i];
}
static void TryOpenSDLControllers(GameControllers& controllers);
static u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id);
};
} // namespace Input

View file

@ -468,11 +468,11 @@ InputEvent InputBinding::GetInputEventFromSDLEvent(const SDL_Event& e) {
e.type == SDL_EVENT_MOUSE_WHEEL, 0);
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
gamepad = GetGamepadIndexFromJoystickId(e.gbutton.which);
gamepad = Input::GameControllers::GetGamepadIndexFromJoystickId(e.gbutton.which);
return InputEvent({InputType::Controller, (u32)e.gbutton.button, gamepad}, e.gbutton.down,
0);
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
gamepad = GetGamepadIndexFromJoystickId(e.gaxis.which);
gamepad = Input::GameControllers::GetGamepadIndexFromJoystickId(e.gaxis.which);
return InputEvent({InputType::Axis, (u32)e.gaxis.axis, gamepad}, true, e.gaxis.value / 256);
default:
return InputEvent();
@ -753,8 +753,4 @@ void ActivateOutputsFromInputs() {
}
}
u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id) {
return SDL_GetGamepadPlayerIndex(SDL_GetGamepadFromID(id)) + 1;
}
} // namespace Input

View file

@ -471,6 +471,4 @@ bool UpdatePressedKeys(InputEvent event);
void ActivateOutputsFromInputs();
u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id);
} // namespace Input

View file

@ -191,7 +191,8 @@ void WindowSDL::WaitEvent() {
OnGamepadEvent(&event);
break;
case SDL_EVENT_GAMEPAD_SENSOR_UPDATE: {
int controller_id = Input::GetGamepadIndexFromJoystickId(event.gsensor.which) - 1;
int controller_id =
Input::GameControllers::GetGamepadIndexFromJoystickId(event.gsensor.which) - 1;
switch ((SDL_SensorType)event.gsensor.sensor) {
case SDL_SENSOR_GYRO:
controllers[controller_id]->Gyro(0, event.gsensor.data);
@ -354,8 +355,8 @@ void WindowSDL::OnGamepadEvent(const SDL_Event* event) {
// as it would break the entire touchpad handling
// You can still bind other things to it though
if (event->gbutton.button == SDL_GAMEPAD_BUTTON_TOUCHPAD) {
controllers[Input::GetGamepadIndexFromJoystickId(event->gbutton.which)]->CheckButton(
0, OrbisPadButtonDataOffset::TouchPad, input_down);
controllers[Input::GameControllers::GetGamepadIndexFromJoystickId(event->gbutton.which)]
->CheckButton(0, OrbisPadButtonDataOffset::TouchPad, input_down);
return;
}
@ -363,12 +364,12 @@ void WindowSDL::OnGamepadEvent(const SDL_Event* event) {
case SDL_EVENT_GAMEPAD_SENSOR_UPDATE:
switch ((SDL_SensorType)event->gsensor.sensor) {
case SDL_SENSOR_GYRO:
controllers[Input::GetGamepadIndexFromJoystickId(event->gsensor.which)]->Gyro(
0, event->gsensor.data);
controllers[Input::GameControllers::GetGamepadIndexFromJoystickId(event->gsensor.which)]
->Gyro(0, event->gsensor.data);
break;
case SDL_SENSOR_ACCEL:
controllers[Input::GetGamepadIndexFromJoystickId(event->gsensor.which)]->Acceleration(
0, event->gsensor.data);
controllers[Input::GameControllers::GetGamepadIndexFromJoystickId(event->gsensor.which)]
->Acceleration(0, event->gsensor.data);
break;
default:
break;
@ -377,9 +378,10 @@ void WindowSDL::OnGamepadEvent(const SDL_Event* event) {
case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
case SDL_EVENT_GAMEPAD_TOUCHPAD_UP:
case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:
controllers[Input::GetGamepadIndexFromJoystickId(event->gtouchpad.which)]->SetTouchpadState(
event->gtouchpad.finger, event->type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP,
event->gtouchpad.x, event->gtouchpad.y);
controllers[Input::GameControllers::GetGamepadIndexFromJoystickId(event->gtouchpad.which)]
->SetTouchpadState(event->gtouchpad.finger,
event->type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP, event->gtouchpad.x,
event->gtouchpad.y);
return;
default:
break;