| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
|
| 8 |
#include <SDL3/SDL_gamepad.h> |
| 9 |
#include <SDL3/SDL_haptic.h> |
| 10 |
|
| 11 |
#include "Common/MathUtil.h" |
| 12 |
|
| 13 |
#include "InputCommon/ControllerInterface/CoreDevice.h" |
| 14 |
|
| 15 |
namespace |
| 16 |
{ |
| 17 |
std::string GetLegacyButtonName(int index) |
| 18 |
{ |
| 19 |
return "Button " + std::to_string(index); |
| 20 |
} |
| 21 |
|
| 22 |
std::string GetLegacyAxisName(int index, int range) |
| 23 |
{ |
| 24 |
return "Axis " + std::to_string(index) + (range < 0 ? '-' : '+'); |
| 25 |
} |
| 26 |
|
| 27 |
std::string GetLegacyHatName(int index, int direction) |
| 28 |
{ |
| 29 |
return "Hat " + std::to_string(index) + ' ' + "NESW"[direction]; |
| 30 |
} |
| 31 |
|
| 32 |
constexpr int GetDirectionFromHatMask(int mask) |
| 33 |
{ |
| 34 |
return MathUtil::IntLog2(mask); |
| 35 |
} |
| 36 |
|
| 37 |
static_assert(GetDirectionFromHatMask(SDL_HAT_UP) == 0); |
| 38 |
static_assert(GetDirectionFromHatMask(SDL_HAT_LEFT) == 3); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
namespace ciface::SDL |
| 43 |
{ |
| 44 |
|
| 45 |
class Gamepad : public Core::Device |
| 46 |
{ |
| 47 |
private: |
| 48 |
|
| 49 |
class Button : public Core::Device::Input |
| 50 |
{ |
| 51 |
public: |
| 52 |
std::string GetName() const override; |
| 53 |
Button(SDL_Gamepad* gc, const SDL_GamepadBinding& binding) : m_gc(gc), m_binding(binding) {} |
| 54 |
ControlState GetState() const override; |
| 55 |
bool IsMatchingName(std::string_view name) const override; |
| 56 |
|
| 57 |
private: |
| 58 |
SDL_Gamepad* const m_gc; |
| 59 |
const SDL_GamepadBinding m_binding; |
| 60 |
}; |
| 61 |
|
| 62 |
class Axis : public Core::Device::Input |
| 63 |
{ |
| 64 |
public: |
| 65 |
std::string GetName() const override; |
| 66 |
Axis(SDL_Gamepad* gc, Sint16 range, SDL_GamepadAxis axis) |
| 67 |
: m_gc(gc), m_range(range), m_axis(axis) |
| 68 |
{ |
| 69 |
} |
| 70 |
ControlState GetState() const override; |
| 71 |
|
| 72 |
private: |
| 73 |
SDL_Gamepad* const m_gc; |
| 74 |
const Sint16 m_range; |
| 75 |
const SDL_GamepadAxis m_axis; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
class LegacyButton : public Core::Device::Input |
| 80 |
{ |
| 81 |
public: |
| 82 |
std::string GetName() const override { return GetLegacyButtonName(m_index); } |
| 83 |
LegacyButton(SDL_Joystick* js, int index) : m_js(js), m_index(index) {} |
| 84 |
ControlState GetState() const override; |
| 85 |
|
| 86 |
private: |
| 87 |
SDL_Joystick* const m_js; |
| 88 |
const int m_index; |
| 89 |
}; |
| 90 |
|
| 91 |
class LegacyAxis : public Core::Device::Input |
| 92 |
{ |
| 93 |
public: |
| 94 |
std::string GetName() const override { return GetLegacyAxisName(m_index, m_range); } |
| 95 |
LegacyAxis(SDL_Joystick* js, int index, s16 range, bool is_handled_elsewhere) |
| 96 |
: m_js(js), m_index(index), m_range(range), m_is_handled_elsewhere(is_handled_elsewhere) |
| 97 |
{ |
| 98 |
} |
| 99 |
ControlState GetState() const override; |
| 100 |
bool IsHidden() const override { return m_is_handled_elsewhere; } |
| 101 |
bool IsDetectable() const override { return !IsHidden(); } |
| 102 |
|
| 103 |
private: |
| 104 |
SDL_Joystick* const m_js; |
| 105 |
const int m_index; |
| 106 |
const s16 m_range; |
| 107 |
const bool m_is_handled_elsewhere; |
| 108 |
}; |
| 109 |
|
| 110 |
class LegacyHat : public Input |
| 111 |
{ |
| 112 |
public: |
| 113 |
std::string GetName() const override { return GetLegacyHatName(m_index, m_direction); } |
| 114 |
LegacyHat(SDL_Joystick* js, int index, u8 direction) |
| 115 |
: m_js(js), m_index(index), m_direction(direction) |
| 116 |
{ |
| 117 |
} |
| 118 |
ControlState GetState() const override; |
| 119 |
|
| 120 |
private: |
| 121 |
SDL_Joystick* const m_js; |
| 122 |
const int m_index; |
| 123 |
const u8 m_direction; |
| 124 |
}; |
| 125 |
|
| 126 |
class BatteryInput final : public Input |
| 127 |
{ |
| 128 |
public: |
| 129 |
explicit BatteryInput(const ControlState* battery_value) : m_battery_value(*battery_value) {} |
| 130 |
std::string GetName() const override { return "Battery"; } |
| 131 |
ControlState GetState() const override { return m_battery_value; } |
| 132 |
bool IsDetectable() const override { return false; } |
| 133 |
|
| 134 |
private: |
| 135 |
const ControlState& m_battery_value; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
class Rumble : public Output |
| 140 |
{ |
| 141 |
public: |
| 142 |
using UpdateCallback = void (Gamepad::*)(void); |
| 143 |
|
| 144 |
Rumble(const char* name, Gamepad& gc, Uint16* state, UpdateCallback update_callback) |
| 145 |
: m_name{name}, m_gc{gc}, m_state{*state}, m_update_callback{update_callback} |
| 146 |
{ |
| 147 |
} |
| 148 |
std::string GetName() const override { return m_name; } |
| 149 |
void SetState(ControlState state) override |
| 150 |
{ |
| 151 |
const auto new_state = std::lround(state * std::numeric_limits<Uint16>::max()); |
| 152 |
if (m_state == new_state) |
| 153 |
return; |
| 154 |
|
| 155 |
m_state = new_state; |
| 156 |
(m_gc.*m_update_callback)(); |
| 157 |
} |
| 158 |
|
| 159 |
private: |
| 160 |
const char* const m_name; |
| 161 |
Gamepad& m_gc; |
| 162 |
Uint16& m_state; |
| 163 |
UpdateCallback const m_update_callback; |
| 164 |
}; |
| 165 |
|
| 166 |
class CombinedMotor : public Output |
| 167 |
{ |
| 168 |
public: |
| 169 |
CombinedMotor(Gamepad& gc, Uint16* low_state, Uint16* high_state) |
| 170 |
: m_gc{gc}, m_low_state{*low_state}, m_high_state{*high_state} |
| 171 |
{ |
| 172 |
} |
| 173 |
std::string GetName() const override { return "Motor"; } |
| 174 |
void SetState(ControlState state) override |
| 175 |
{ |
| 176 |
const auto new_state = std::lround(state * std::numeric_limits<Uint16>::max()); |
| 177 |
if (m_low_state == new_state && m_high_state == new_state) |
| 178 |
return; |
| 179 |
|
| 180 |
m_low_state = new_state; |
| 181 |
m_high_state = new_state; |
| 182 |
m_gc.UpdateRumble(); |
| 183 |
} |
| 184 |
|
| 185 |
private: |
| 186 |
Gamepad& m_gc; |
| 187 |
Uint16& m_low_state; |
| 188 |
Uint16& m_high_state; |
| 189 |
}; |
| 190 |
|
| 191 |
class HapticEffect : public Output |
| 192 |
{ |
| 193 |
public: |
| 194 |
HapticEffect(SDL_Haptic* haptic); |
| 195 |
~HapticEffect(); |
| 196 |
|
| 197 |
protected: |
| 198 |
virtual bool UpdateParameters(s16 value) = 0; |
| 199 |
static void SetDirection(SDL_HapticDirection* dir); |
| 200 |
|
| 201 |
SDL_HapticEffect m_effect = {}; |
| 202 |
|
| 203 |
static constexpr u16 DISABLED_EFFECT_TYPE = 0; |
| 204 |
|
| 205 |
private: |
| 206 |
virtual void SetState(ControlState state) override final; |
| 207 |
void UpdateEffect(); |
| 208 |
SDL_Haptic* const m_haptic; |
| 209 |
int m_id = -1; |
| 210 |
}; |
| 211 |
|
| 212 |
class ConstantEffect : public HapticEffect |
| 213 |
{ |
| 214 |
public: |
| 215 |
ConstantEffect(SDL_Haptic* haptic); |
| 216 |
std::string GetName() const override; |
| 217 |
|
| 218 |
private: |
| 219 |
bool UpdateParameters(s16 value) override; |
| 220 |
}; |
| 221 |
|
| 222 |
class RampEffect : public HapticEffect |
| 223 |
{ |
| 224 |
public: |
| 225 |
RampEffect(SDL_Haptic* haptic); |
| 226 |
std::string GetName() const override; |
| 227 |
|
| 228 |
private: |
| 229 |
bool UpdateParameters(s16 value) override; |
| 230 |
}; |
| 231 |
|
| 232 |
class PeriodicEffect : public HapticEffect |
| 233 |
{ |
| 234 |
public: |
| 235 |
PeriodicEffect(SDL_Haptic* haptic, u16 waveform); |
| 236 |
std::string GetName() const override; |
| 237 |
|
| 238 |
private: |
| 239 |
bool UpdateParameters(s16 value) override; |
| 240 |
|
| 241 |
const u16 m_waveform; |
| 242 |
}; |
| 243 |
|
| 244 |
class LeftRightEffect : public HapticEffect |
| 245 |
{ |
| 246 |
public: |
| 247 |
enum class Motor : u8 |
| 248 |
{ |
| 249 |
Weak, |
| 250 |
Strong, |
| 251 |
}; |
| 252 |
|
| 253 |
LeftRightEffect(SDL_Haptic* haptic, Motor motor); |
| 254 |
std::string GetName() const override; |
| 255 |
|
| 256 |
private: |
| 257 |
bool UpdateParameters(s16 value) override; |
| 258 |
|
| 259 |
const Motor m_motor; |
| 260 |
}; |
| 261 |
|
| 262 |
class NormalizedInput : public Input |
| 263 |
{ |
| 264 |
public: |
| 265 |
NormalizedInput(const char* name, const float* state) : m_name{std::move(name)}, m_state{*state} |
| 266 |
{ |
| 267 |
} |
| 268 |
|
| 269 |
std::string GetName() const override { return std::string{m_name}; } |
| 270 |
ControlState GetState() const override { return m_state; } |
| 271 |
|
| 272 |
private: |
| 273 |
const char* const m_name; |
| 274 |
const float& m_state; |
| 275 |
}; |
| 276 |
|
| 277 |
template <int Scale> |
| 278 |
class NonDetectableDirectionalInput : public Input |
| 279 |
{ |
| 280 |
public: |
| 281 |
NonDetectableDirectionalInput(const char* name, const float* state) |
| 282 |
: m_name{std::move(name)}, m_state{*state} |
| 283 |
{ |
| 284 |
} |
| 285 |
|
| 286 |
std::string GetName() const override { return std::string{m_name} + (Scale > 0 ? '+' : '-'); } |
| 287 |
bool IsDetectable() const override { return false; } |
| 288 |
ControlState GetState() const override { return m_state * Scale; } |
| 289 |
|
| 290 |
private: |
| 291 |
const char* const m_name; |
| 292 |
const float& m_state; |
| 293 |
}; |
| 294 |
|
| 295 |
class MotionInput : public Input |
| 296 |
{ |
| 297 |
public: |
| 298 |
MotionInput(std::string name, SDL_Gamepad* gc, SDL_SensorType type, int index, |
| 299 |
ControlState scale) |
| 300 |
: m_name(std::move(name)), m_gc(gc), m_type(type), m_index(index), m_scale(scale) |
| 301 |
{ |
| 302 |
} |
| 303 |
|
| 304 |
std::string GetName() const override { return m_name; } |
| 305 |
bool IsDetectable() const override { return false; } |
| 306 |
ControlState GetState() const override; |
| 307 |
|
| 308 |
private: |
| 309 |
std::string m_name; |
| 310 |
|
| 311 |
SDL_Gamepad* const m_gc; |
| 312 |
SDL_SensorType const m_type; |
| 313 |
int const m_index; |
| 314 |
|
| 315 |
ControlState const m_scale; |
| 316 |
}; |
| 317 |
|
| 318 |
public: |
| 319 |
Gamepad(SDL_Gamepad* gamepad, SDL_Joystick* joystick); |
| 320 |
~Gamepad() override; |
| 321 |
|
| 322 |
std::string GetName() const override; |
| 323 |
std::string GetSource() const override; |
| 324 |
SDL_JoystickID GetSDLInstanceID() const; |
| 325 |
Core::DeviceRemoval UpdateInput() override |
| 326 |
{ |
| 327 |
UpdateBatteryLevel(); |
| 328 |
|
| 329 |
|
| 330 |
const int touchpad_index = 0; |
| 331 |
const int finger_index = 0; |
| 332 |
|
| 333 |
if (SDL_GetNumGamepadTouchpads(m_gamepad) > touchpad_index && |
| 334 |
SDL_GetNumGamepadTouchpadFingers(m_gamepad, touchpad_index) > finger_index) |
| 335 |
{ |
| 336 |
SDL_GetGamepadTouchpadFinger(m_gamepad, touchpad_index, finger_index, nullptr, &m_touchpad_x, |
| 337 |
&m_touchpad_y, &m_touchpad_pressure); |
| 338 |
m_touchpad_x = m_touchpad_x * 2 - 1; |
| 339 |
m_touchpad_y = m_touchpad_y * 2 - 1; |
| 340 |
} |
| 341 |
|
| 342 |
return Core::DeviceRemoval::Keep; |
| 343 |
} |
| 344 |
|
| 345 |
private: |
| 346 |
void UpdateRumble() |
| 347 |
{ |
| 348 |
SDL_RumbleGamepad(m_gamepad, m_low_freq_rumble, m_high_freq_rumble, RUMBLE_LENGTH_MS); |
| 349 |
} |
| 350 |
|
| 351 |
void UpdateRumbleTriggers() |
| 352 |
{ |
| 353 |
SDL_RumbleGamepadTriggers(m_gamepad, m_trigger_l_rumble, m_trigger_r_rumble, RUMBLE_LENGTH_MS); |
| 354 |
} |
| 355 |
|
| 356 |
bool UpdateBatteryLevel(); |
| 357 |
|
| 358 |
Uint16 m_low_freq_rumble = 0; |
| 359 |
Uint16 m_high_freq_rumble = 0; |
| 360 |
|
| 361 |
Uint16 m_trigger_l_rumble = 0; |
| 362 |
Uint16 m_trigger_r_rumble = 0; |
| 363 |
|
| 364 |
SDL_Gamepad* const m_gamepad; |
| 365 |
std::string m_name; |
| 366 |
SDL_Joystick* const m_joystick; |
| 367 |
SDL_Haptic* m_haptic = nullptr; |
| 368 |
ControlState m_battery_value; |
| 369 |
float m_touchpad_x = 0.f; |
| 370 |
float m_touchpad_y = 0.f; |
| 371 |
float m_touchpad_pressure = 0.f; |
| 372 |
}; |
| 373 |
|
| 374 |
struct SDLMotionAxis |
| 375 |
{ |
| 376 |
std::string_view name; |
| 377 |
int index; |
| 378 |
ControlState scale; |
| 379 |
}; |
| 380 |
using SDLMotionAxisList = std::array<SDLMotionAxis, 6>; |
| 381 |
|
| 382 |
static constexpr std::array<const char*, 21> s_sdl_button_names = { |
| 383 |
"Button S", |
| 384 |
"Button E", |
| 385 |
"Button W", |
| 386 |
"Button N", |
| 387 |
"Back", |
| 388 |
"Guide", |
| 389 |
"Start", |
| 390 |
"Thumb L", |
| 391 |
"Thumb R", |
| 392 |
"Shoulder L", |
| 393 |
"Shoulder R", |
| 394 |
"Pad N", |
| 395 |
"Pad S", |
| 396 |
"Pad W", |
| 397 |
"Pad E", |
| 398 |
"Misc 1", |
| 399 |
"Paddle 1", |
| 400 |
"Paddle 2", |
| 401 |
"Paddle 3", |
| 402 |
"Paddle 4", |
| 403 |
"Touchpad", |
| 404 |
}; |
| 405 |
static constexpr std::array<const char*, 6> s_sdl_axis_names = { |
| 406 |
"Left X", |
| 407 |
"Left Y", |
| 408 |
"Right X", |
| 409 |
"Right Y", |
| 410 |
"Trigger L", |
| 411 |
"Trigger R", |
| 412 |
}; |
| 413 |
static constexpr SDLMotionAxisList SDL_AXES_ACCELEROMETER = {{ |
| 414 |
{"Up", 1, 1}, |
| 415 |
{"Down", 1, -1}, |
| 416 |
{"Left", 0, -1}, |
| 417 |
{"Right", 0, 1}, |
| 418 |
{"Forward", 2, -1}, |
| 419 |
{"Backward", 2, 1}, |
| 420 |
}}; |
| 421 |
static constexpr SDLMotionAxisList SDL_AXES_GYRO = {{ |
| 422 |
{"Pitch Up", 0, 1}, |
| 423 |
{"Pitch Down", 0, -1}, |
| 424 |
{"Roll Left", 2, 1}, |
| 425 |
{"Roll Right", 2, -1}, |
| 426 |
{"Yaw Left", 1, 1}, |
| 427 |
{"Yaw Right", 1, -1}, |
| 428 |
}}; |
| 429 |
} |