summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerInterface
diff options
context:
space:
mode:
authorJMC47 <JMC4789@gmail.com>2024-12-10 12:52:43 -0500
committerGitHub <noreply@github.com>2024-12-10 12:52:43 -0500
commit6ea8edd531e0206cdf996b31df526c5da2a6ca96 (patch)
tree21136999d131806e5af4f46664b42e6c18657f50 /Source/Core/InputCommon/ControllerInterface
parent394db8b7981ab02a92dd6f1177a84396c60a5037 (diff)
parentad1511982ab025561f375500bac89aec088438b9 (diff)
Merge pull request #13209 from jordan-woyak/sdl-touchpad
InputCommon/SDL: Add touchpad inputs.
Diffstat (limited to 'Source/Core/InputCommon/ControllerInterface')
-rw-r--r--Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp59
1 files changed, 59 insertions, 0 deletions
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 <int Scale>
+ 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) {