From ad1511982ab025561f375500bac89aec088438b9 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 8 Nov 2024 22:27:03 -0600 Subject: [PATCH] InputCommon/SDL: Add touchpad inputs. --- .../ControllerInterface/SDL/SDL.cpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index 44abd54cd5..7b68b96712 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -283,6 +283,39 @@ private: const Motor m_motor; }; + class NormalizedInput : public Input + { + public: + NormalizedInput(const char* name, const float* state) : m_name{std::move(name)}, m_state{*state} + { + } + + std::string GetName() const override { return std::string{m_name}; } + ControlState GetState() const override { return m_state; } + + private: + const char* const m_name; + const float& m_state; + }; + + template + class NonDetectableDirectionalInput : public Input + { + public: + NonDetectableDirectionalInput(const char* name, const float* state) + : m_name{std::move(name)}, m_state{*state} + { + } + + std::string GetName() const override { return std::string{m_name} + (Scale > 0 ? '+' : '-'); } + bool IsDetectable() const override { return false; } + ControlState GetState() const override { return m_state * Scale; } + + private: + const char* const m_name; + const float& m_state; + }; + class MotionInput : public Input { public: @@ -316,6 +349,17 @@ public: Core::DeviceRemoval UpdateInput() override { m_battery_value = GetBatteryValueFromSDLPowerLevel(SDL_JoystickCurrentPowerLevel(m_joystick)); + + // We only support one touchpad and one finger. + const int touchpad_index = 0; + const int finger_index = 0; + + Uint8 state = 0; + SDL_GameControllerGetTouchpadFinger(m_gamecontroller, touchpad_index, finger_index, &state, + &m_touchpad_x, &m_touchpad_y, &m_touchpad_pressure); + m_touchpad_x = m_touchpad_x * 2 - 1; + m_touchpad_y = m_touchpad_y * 2 - 1; + return Core::DeviceRemoval::Keep; } @@ -325,6 +369,9 @@ private: SDL_Joystick* const m_joystick; SDL_Haptic* m_haptic = nullptr; ControlState m_battery_value; + float m_touchpad_x = 0.f; + float m_touchpad_y = 0.f; + float m_touchpad_pressure = 0.f; }; class InputBackend final : public ciface::InputBackend @@ -712,6 +759,18 @@ GameController::GameController(SDL_GameController* const gamecontroller, AddOutput(new MotorR(m_gamecontroller)); } + // Touchpad + if (SDL_GameControllerGetNumTouchpads(m_gamecontroller) > 0) + { + const char* const name_x = "Touchpad X"; + AddInput(new NonDetectableDirectionalInput<-1>(name_x, &m_touchpad_x)); + AddInput(new NonDetectableDirectionalInput<+1>(name_x, &m_touchpad_x)); + const char* const name_y = "Touchpad Y"; + AddInput(new NonDetectableDirectionalInput<-1>(name_y, &m_touchpad_y)); + AddInput(new NonDetectableDirectionalInput<+1>(name_y, &m_touchpad_y)); + AddInput(new NormalizedInput("Touchpad Pressure", &m_touchpad_pressure)); + } + // Motion const auto add_sensor = [this](SDL_SensorType type, std::string_view sensor_name, const SDLMotionAxisList& axes) {