This time for sure

This commit is contained in:
kalaposfos13 2025-04-25 20:20:25 +02:00
parent e26ed73533
commit ba29985151
5 changed files with 30 additions and 26 deletions

View File

@ -3,6 +3,7 @@
#include <unordered_set> #include <unordered_set>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <common/singleton.h>
#include "common/config.h" #include "common/config.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/libraries/kernel/time.h" #include "core/libraries/kernel/time.h"
@ -287,10 +288,7 @@ void GameControllers::TryOpenSDLControllers(GameControllers& controllers) {
controllers[i]->m_sdl_gamepad = pad; controllers[i]->m_sdl_gamepad = pad;
controllers[i]->user_id = i + 1; controllers[i]->user_id = i + 1;
slot_taken[i] = true; slot_taken[i] = true;
bool err = SDL_SetGamepadPlayerIndex(pad, i); controllers[i]->player_index = i;
if (!err) {
LOG_ERROR(Input, "Failed to set controller index: {}", SDL_GetError());
}
AddUserServiceEvent( AddUserServiceEvent(
{OrbisUserServiceEventType::Login, SDL_GetGamepadPlayerIndex(pad) + 1}); {OrbisUserServiceEventType::Login, SDL_GetGamepadPlayerIndex(pad) + 1});
@ -347,4 +345,15 @@ u32 GameController::Poll() {
return 100; return 100;
} }
u8 GameControllers::GetGamepadIndexFromJoystickId(SDL_JoystickID id) {
auto& controllers = *Common::Singleton<GameControllers>::Instance();
auto gamepad = SDL_GetGamepadFromID(id);
for (int i = 0; i < 4; i++) {
if (controllers[i]->m_sdl_gamepad == gamepad) {
return controllers[i]->player_index;
}
}
UNREACHABLE_MSG("Gamepad not registered!");
}
} // namespace Input } // namespace Input

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <mutex> #include <mutex>
#include "SDL3/SDL_joystick.h"
#include "common/assert.h" #include "common/assert.h"
#include "common/types.h" #include "common/types.h"
#include "core/libraries/pad/pad.h" #include "core/libraries/pad/pad.h"
@ -89,6 +90,7 @@ private:
std::array<StateInternal, MAX_STATES> m_private; std::array<StateInternal, MAX_STATES> m_private;
SDL_Gamepad* m_sdl_gamepad = nullptr; SDL_Gamepad* m_sdl_gamepad = nullptr;
u8 player_index = -1;
}; };
class GameControllers { class GameControllers {
@ -106,6 +108,7 @@ public:
return controllers[i]; return controllers[i];
} }
static void TryOpenSDLControllers(GameControllers& controllers); static void TryOpenSDLControllers(GameControllers& controllers);
static u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id);
}; };
} // namespace Input } // namespace Input

View File

@ -468,11 +468,11 @@ InputEvent InputBinding::GetInputEventFromSDLEvent(const SDL_Event& e) {
e.type == SDL_EVENT_MOUSE_WHEEL, 0); e.type == SDL_EVENT_MOUSE_WHEEL, 0);
case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP: case SDL_EVENT_GAMEPAD_BUTTON_UP:
gamepad = GetGamepadIndexFromJoystickId(e.gbutton.which); gamepad = GameControllers::GetGamepadIndexFromJoystickId(e.gbutton.which) + 1;
return InputEvent({InputType::Controller, (u32)e.gbutton.button, gamepad}, e.gbutton.down, return InputEvent({InputType::Controller, (u32)e.gbutton.button, gamepad}, e.gbutton.down,
0); 0);
case SDL_EVENT_GAMEPAD_AXIS_MOTION: case SDL_EVENT_GAMEPAD_AXIS_MOTION:
gamepad = GetGamepadIndexFromJoystickId(e.gaxis.which); gamepad = GameControllers::GetGamepadIndexFromJoystickId(e.gaxis.which) + 1;
return InputEvent({InputType::Axis, (u32)e.gaxis.axis, gamepad}, true, e.gaxis.value / 256); return InputEvent({InputType::Axis, (u32)e.gaxis.axis, gamepad}, true, e.gaxis.value / 256);
default: default:
return InputEvent(); return InputEvent();
@ -753,12 +753,4 @@ void ActivateOutputsFromInputs() {
} }
} }
u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id) {
u8 index = SDL_GetGamepadPlayerIndex(SDL_GetGamepadFromID(id));
if (index > 3) [[unlikely]] {
UNREACHABLE_MSG("Index out of bounds: {}", index);
}
return index;
}
} // namespace Input } // namespace Input

View File

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

View File

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