diff --git a/src/input/input_handler.cpp b/src/input/input_handler.cpp index 4f6b4c6ba..93db8448b 100644 --- a/src/input/input_handler.cpp +++ b/src/input/input_handler.cpp @@ -472,11 +472,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 = SDL_GetGamepadPlayerIndex(SDL_GetGamepadFromID(e.gbutton.which)); + gamepad = GetGamepadIndexFromJoystickId(e.gbutton.which); return InputEvent({InputType::Controller, (u32)e.gbutton.button, gamepad}, e.gbutton.down, 0); case SDL_EVENT_GAMEPAD_AXIS_MOTION: - gamepad = SDL_GetGamepadPlayerIndex(SDL_GetGamepadFromID(e.gaxis.which)); + gamepad = GetGamepadIndexFromJoystickId(e.gaxis.which); return InputEvent({InputType::Axis, (u32)e.gaxis.axis, gamepad}, true, e.gaxis.value / 256); default: return InputEvent(); @@ -761,4 +761,8 @@ void ActivateOutputsFromInputs() { } } +u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id) { + return SDL_GetGamepadPlayerIndex(SDL_GetGamepadFromID(id)); +} + } // namespace Input diff --git a/src/input/input_handler.h b/src/input/input_handler.h index 173fb341c..3484ab446 100644 --- a/src/input/input_handler.h +++ b/src/input/input_handler.h @@ -471,4 +471,6 @@ bool UpdatePressedKeys(InputEvent event); void ActivateOutputsFromInputs(); +u8 GetGamepadIndexFromJoystickId(SDL_JoystickID id); + } // namespace Input diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index 4ce02b8ef..cd576271b 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -190,32 +190,11 @@ void WindowSDL::WaitEvent() { // todo handle userserviceevents here Input::GameControllers::TryOpenSDLControllers(controllers); break; - case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: - case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: - case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION: - controllers[0]->SetTouchpadState(event.gtouchpad.finger, - event.type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP, - event.gtouchpad.x, event.gtouchpad.y); - break; case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_UP: case SDL_EVENT_GAMEPAD_AXIS_MOTION: OnGamepadEvent(&event); break; - // i really would have appreciated ANY KIND OF DOCUMENTATION ON THIS - // AND IT DOESN'T EVEN USE PROPER ENUMS - case SDL_EVENT_GAMEPAD_SENSOR_UPDATE: - switch ((SDL_SensorType)event.gsensor.sensor) { - case SDL_SENSOR_GYRO: - controllers[0]->Gyro(0, event.gsensor.data); - break; - case SDL_SENSOR_ACCEL: - controllers[0]->Acceleration(0, event.gsensor.data); - break; - default: - break; - } - break; case SDL_EVENT_QUIT: is_open = false; break; @@ -343,10 +322,37 @@ 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[0]->CheckButton(0, OrbisPadButtonDataOffset::TouchPad, input_down); + controllers[Input::GetGamepadIndexFromJoystickId(event->gbutton.which)]->CheckButton( + 0, OrbisPadButtonDataOffset::TouchPad, input_down); return; } + switch (event->type) { + 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); + break; + case SDL_SENSOR_ACCEL: + controllers[Input::GetGamepadIndexFromJoystickId(event->gsensor.which)]->Acceleration( + 0, event->gsensor.data); + break; + default: + break; + } + return; + 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); + return; + default: + break; + } + // add/remove it from the list bool inputs_changed = Input::UpdatePressedKeys(input_event);