diff options
| author | Leonida local test <local-test@localhost> | 2026-11-22 18:26:28 +0100 |
|---|---|---|
| committer | Leonida local test <local-test@localhost> | 2026-11-22 18:26:28 +0100 |
| commit | a3b1719c61b4b916964368bf9968e5f822cd50ca (patch) | |
| tree | 311db6edfb35ed70d274b4124ecbc55ab5386a82 /runtime/shims/gameinput.cpp | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
gameinput: report and drive force-feedback and haptic motors through SDL3 haptics (RT-160)
IGameInputDevice answered the force-feedback and haptic half of the API with
nothing: no motors in GameInputDeviceInfo, E_INVALIDARG from
CreateForceFeedbackEffect, false from IsForceFeedbackMotorPoweredOn, a no-op
SetForceFeedbackMotorGain and E_INVALIDARG from SetHapticMotorState. A pad SDL
opens as a haptic device now reports one force-feedback motor with the effect
kinds SDL lists, and a pad with trigger motors reports two haptic motors.
Force feedback goes to SDL_CreateHapticEffect/RunHapticEffect on the poll
thread, so title calls still never touch SDL; the eleven GameInput effect kinds
are SDL's eleven force-feedback effects. The trigger haptic motors play their
one waveform as an intensity on the existing trigger output. A pad with neither
keeps the old answers, and LEONIDA_RUMBLE=0 silences both. gameinput_test drives
all of it through a fake haptic backend, because SDL's virtual joysticks report
no haptics.
AI-assisted.
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 603 |
1 files changed, 577 insertions, 26 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 35eac4c4e..1b9a55db2 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -12,6 +12,9 @@ | |||
| 12 | // effect (SDL_SendGamepadEffect), as set by LEONIDA_DS_TRIGGERS. That is an approximation of the Xbox impulse | 12 | // effect (SDL_SendGamepadEffect), as set by LEONIDA_DS_TRIGGERS. That is an approximation of the Xbox impulse |
| 13 | // trigger signal, not the PS5 version's authored effects. | 13 | // trigger signal, not the PS5 version's authored effects. |
| 14 | // - DualSense/DualShock 4 lightbar colour (LEONIDA_DS_LIGHTBAR) and the player LEDs by connection order | 14 | // - DualSense/DualShock 4 lightbar colour (LEONIDA_DS_LIGHTBAR) and the player LEDs by connection order |
| 15 | // - force-feedback motors, reported from the SDL haptic device SDL opens for the pad's joystick, and played through | ||
| 16 | // SDL_CreateHapticEffect/RunHapticEffect (IGameInputForceFeedbackEffect); haptic trigger motors, reported from the | ||
| 17 | // trigger motors the pad already has, played as an intensity through the same trigger output as SetRumbleState | ||
| 15 | // - the keyboard pad (LEONIDA_KEYBOARD_PAD): one SDL virtual gamepad driven by the keyboard and mouse | 18 | // - the keyboard pad (LEONIDA_KEYBOARD_PAD): one SDL virtual gamepad driven by the keyboard and mouse |
| 16 | // | 19 | // |
| 17 | // One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when | 20 | // One thread owns SDL: the background poll thread by default, or the caller of rt::gameinput::poll() when |
| @@ -60,6 +63,9 @@ constexpr Uint32 kRumbleDurationMs = 0xFFFF; // SDL's maximum; refreshed well b | |||
| 60 | constexpr uint64_t kRumbleRefreshUs = 20'000'000; | 63 | constexpr uint64_t kRumbleRefreshUs = 20'000'000; |
| 61 | constexpr uint64_t kBatteryRefreshUs = 1'000'000; | 64 | constexpr uint64_t kBatteryRefreshUs = 1'000'000; |
| 62 | constexpr int kDsEffectSize = 47; // the DualSense effects block SDL_SendGamepadEffect forwards (SDL's DS5EffectsState_t) | 65 | constexpr int kDsEffectSize = 47; // the DualSense effects block SDL_SendGamepadEffect forwards (SDL's DS5EffectsState_t) |
| 66 | constexpr uint32_t kForceFeedbackMotors = 1; // one per SDL haptic device: the wheel or stick it belongs to | ||
| 67 | constexpr uint32_t kHapticMotors = 2; // the left and right trigger, on a pad that backs them | ||
| 68 | constexpr uint32_t kHapticHoldMs = 60'000; // length of an effect GameInput gives no duration; re-run before it ends | ||
| 63 | constexpr uint64_t kHistoryUs = 500'000; // a reading stays in the history until the one after it is this old | 69 | constexpr uint64_t kHistoryUs = 500'000; // a reading stays in the history until the one after it is this old |
| 64 | constexpr size_t kHistoryCap = 512; // readings per pad, whatever the poll rate | 70 | constexpr size_t kHistoryCap = 512; // readings per pad, whatever the poll rate |
| 65 | // Motion. SDL gives accel in m/s^2 (gravity included) and gyro in rad/s; the state the ABI defines carries signed | 71 | // Motion. SDL gives accel in m/s^2 (gravity included) and gyro in rad/s; the state the ABI defines carries signed |
| @@ -87,6 +93,41 @@ ULONG release_floor(std::atomic<ULONG>& refs) { | |||
| 87 | 93 | ||
| 88 | bool same_guid(const GUID& a, const GUID& b) { return std::memcmp(&a, &b, sizeof(GUID)) == 0; } | 94 | bool same_guid(const GUID& a, const GUID& b) { return std::memcmp(&a, &b, sizeof(GUID)) == 0; } |
| 89 | 95 | ||
| 96 | // ---- SDL haptics ------------------------------------------------------------------------------------------------ | ||
| 97 | // | ||
| 98 | // Force feedback and the trigger haptic motors go through SDL's haptic API, opened from the pad's joystick: SDL's | ||
| 99 | // gamepad API has no force feedback at all, and a wheel or stick it models as a joystick exposes one only as a haptic | ||
| 100 | // device. Every call goes through one table so gameinput_test can stand in for it, because SDL's virtual joysticks | ||
| 101 | // report no haptics and a wheel is the only other way to reach this path. | ||
| 102 | |||
| 103 | void* sdl_open_haptic(void* js) { return SDL_OpenHapticFromJoystick(static_cast<SDL_Joystick*>(js)); } | ||
| 104 | void sdl_close_haptic(void* h) { SDL_CloseHaptic(static_cast<SDL_Haptic*>(h)); } | ||
| 105 | uint32_t sdl_haptic_features(void* h) { return SDL_GetHapticFeatures(static_cast<SDL_Haptic*>(h)); } | ||
| 106 | int sdl_haptic_axes(void* h) { return SDL_GetNumHapticAxes(static_cast<SDL_Haptic*>(h)); } | ||
| 107 | int sdl_haptic_max_effects(void* h) { return SDL_GetMaxHapticEffects(static_cast<SDL_Haptic*>(h)); } | ||
| 108 | bool sdl_haptic_supported(void* h, const void* e) { | ||
| 109 | return SDL_HapticEffectSupported(static_cast<SDL_Haptic*>(h), static_cast<const SDL_HapticEffect*>(e)); | ||
| 110 | } | ||
| 111 | int sdl_haptic_create(void* h, const void* e) { | ||
| 112 | return SDL_CreateHapticEffect(static_cast<SDL_Haptic*>(h), static_cast<const SDL_HapticEffect*>(e)); | ||
| 113 | } | ||
| 114 | bool sdl_haptic_update(void* h, int id, const void* e) { | ||
| 115 | return SDL_UpdateHapticEffect(static_cast<SDL_Haptic*>(h), id, static_cast<const SDL_HapticEffect*>(e)); | ||
| 116 | } | ||
| 117 | bool sdl_haptic_run(void* h, int id, uint32_t iterations) { | ||
| 118 | return SDL_RunHapticEffect(static_cast<SDL_Haptic*>(h), id, iterations); | ||
| 119 | } | ||
| 120 | bool sdl_haptic_stop(void* h, int id) { return SDL_StopHapticEffect(static_cast<SDL_Haptic*>(h), id); } | ||
| 121 | void sdl_haptic_destroy(void* h, int id) { SDL_DestroyHapticEffect(static_cast<SDL_Haptic*>(h), id); } | ||
| 122 | bool sdl_haptic_gain(void* h, int gain) { return SDL_SetHapticGain(static_cast<SDL_Haptic*>(h), gain); } | ||
| 123 | |||
| 124 | const rt::gameinput::HapticApi kSdlHaptic{sdl_open_haptic, sdl_close_haptic, sdl_haptic_features, sdl_haptic_axes, | ||
| 125 | sdl_haptic_max_effects, sdl_haptic_supported, sdl_haptic_create, | ||
| 126 | sdl_haptic_update, sdl_haptic_run, sdl_haptic_stop, | ||
| 127 | sdl_haptic_destroy, sdl_haptic_gain}; | ||
| 128 | std::atomic<const rt::gameinput::HapticApi*> g_haptic_api{&kSdlHaptic}; | ||
| 129 | const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); } | ||
| 130 | |||
| 90 | // ---- configuration ---------------------------------------------------------------------------------------------- | 131 | // ---- configuration ---------------------------------------------------------------------------------------------- |
| 91 | 132 | ||
| 92 | std::string env(const char* name) { | 133 | std::string env(const char* name) { |
| @@ -135,6 +176,7 @@ rt::gameinput::Config read_config() { | |||
| 135 | // ---- objects ---------------------------------------------------------------------------------------------------- | 176 | // ---- objects ---------------------------------------------------------------------------------------------------- |
| 136 | 177 | ||
| 137 | class Device; | 178 | class Device; |
| 179 | class ForceFeedbackEffect; | ||
| 138 | 180 | ||
| 139 | struct Snapshot { | 181 | struct Snapshot { |
| 140 | GameInputGamepadState gamepad{}; | 182 | GameInputGamepadState gamepad{}; |
| @@ -238,6 +280,149 @@ struct Rumble { | |||
| 238 | float low = 0, high = 0, left = 0, right = 0; | 280 | float low = 0, high = 0, left = 0, right = 0; |
| 239 | }; | 281 | }; |
| 240 | 282 | ||
| 283 | // GameInput -1..1 magnitudes and 0..1 saturations against SDL's levels and saturations. | ||
| 284 | Sint16 hlevel(float v) { return static_cast<Sint16>(std::lround(std::clamp(v, -1.0f, 1.0f) * 32767.0f)); } | ||
| 285 | Uint16 hsat(float v) { return static_cast<Uint16>(std::lround(std::clamp(v, 0.0f, 1.0f) * 65535.0f)); } | ||
| 286 | uint16_t ms_from_us(uint64_t us) { return static_cast<uint16_t>(std::min<uint64_t>((us + 999) / 1000, 65535)); } | ||
| 287 | |||
| 288 | // The SDL_HAPTIC_* bit an effect kind needs, for the motor flags and the create-time check. | ||
| 289 | uint32_t kind_feature(GameInputForceFeedbackEffectKind kind) { | ||
| 290 | switch (kind) { | ||
| 291 | case GameInputForceFeedbackConstant: return SDL_HAPTIC_CONSTANT; | ||
| 292 | case GameInputForceFeedbackRamp: return SDL_HAPTIC_RAMP; | ||
| 293 | case GameInputForceFeedbackSineWave: return SDL_HAPTIC_SINE; | ||
| 294 | case GameInputForceFeedbackSquareWave: return SDL_HAPTIC_SQUARE; | ||
| 295 | case GameInputForceFeedbackTriangleWave: return SDL_HAPTIC_TRIANGLE; | ||
| 296 | case GameInputForceFeedbackSawtoothUpWave: return SDL_HAPTIC_SAWTOOTHUP; | ||
| 297 | case GameInputForceFeedbackSawtoothDownWave: return SDL_HAPTIC_SAWTOOTHDOWN; | ||
| 298 | case GameInputForceFeedbackSpring: return SDL_HAPTIC_SPRING; | ||
| 299 | case GameInputForceFeedbackFriction: return SDL_HAPTIC_FRICTION; | ||
| 300 | case GameInputForceFeedbackDamper: return SDL_HAPTIC_DAMPER; | ||
| 301 | case GameInputForceFeedbackInertia: return SDL_HAPTIC_INERTIA; | ||
| 302 | } | ||
| 303 | return 0; | ||
| 304 | } | ||
| 305 | |||
| 306 | bool valid_kind(GameInputForceFeedbackEffectKind kind) { | ||
| 307 | return kind >= GameInputForceFeedbackConstant && kind <= GameInputForceFeedbackInertia; | ||
| 308 | } | ||
| 309 | |||
| 310 | // An all-zero envelope is how a title that only sets a magnitude leaves it: that means no envelope, not silence. | ||
| 311 | bool envelope_used(const GameInputForceFeedbackEnvelope& e) { | ||
| 312 | return e.attackDuration || e.sustainDuration || e.releaseDuration || e.attackGain != 0.0f || | ||
| 313 | e.sustainGain != 0.0f || e.releaseGain != 0.0f || e.playCount || e.repeatDelay; | ||
| 314 | } | ||
| 315 | |||
| 316 | uint32_t play_count(const GameInputForceFeedbackParams& p) { | ||
| 317 | return p.kind <= GameInputForceFeedbackSawtoothDownWave ? p.data.constant.envelope.playCount : 0; | ||
| 318 | } | ||
| 319 | |||
| 320 | // One GameInput effect as SDL's template. GameInput's eleven kinds are SDL's eleven force-feedback effects, so the | ||
| 321 | // kind picks the SDL type and the fields are magnitudes; conditions have no envelope in GameInput either. Returns | ||
| 322 | // false for a kind the shim does not map. `endless` says the effect has no duration of its own (GameInput left the | ||
| 323 | // envelope empty), which is what the poll thread re-runs before the SDL length runs out. | ||
| 324 | bool build_effect(const GameInputForceFeedbackParams& p, float gain, bool* endless, uint32_t* length_ms, | ||
| 325 | SDL_HapticEffect* out) { | ||
| 326 | std::memset(out, 0, sizeof *out); | ||
| 327 | if (!valid_kind(p.kind)) return false; | ||
| 328 | const bool envelope_kind = p.kind <= GameInputForceFeedbackSawtoothDownWave; | ||
| 329 | const GameInputForceFeedbackEnvelope env = envelope_kind ? p.data.constant.envelope : GameInputForceFeedbackEnvelope{}; | ||
| 330 | const bool used = envelope_kind && envelope_used(env); | ||
| 331 | const float level = gain * (used ? env.sustainGain : 1.0f); // GameInput's sustainGain is the effect's level | ||
| 332 | const uint64_t total_us = used ? env.attackDuration + env.sustainDuration + env.releaseDuration : 0; | ||
| 333 | *endless = total_us == 0; | ||
| 334 | *length_ms = total_us == 0 ? kHapticHoldMs | ||
| 335 | : static_cast<uint32_t>(std::clamp<uint64_t>((total_us + 999) / 1000, 1, 86'400'000)); | ||
| 336 | // The envelope's attack and release, when the title gave one. SDL only uses the envelope when attack_length or | ||
| 337 | // fade_level is set, so a zero-gain envelope is left alone rather than muting the effect. | ||
| 338 | auto envelope = [&](SDL_HapticConstant& e) { | ||
| 339 | if (!used || (!env.attackDuration && !env.releaseDuration)) return; | ||
| 340 | e.attack_length = ms_from_us(env.attackDuration); | ||
| 341 | e.attack_level = hsat(env.attackGain); | ||
| 342 | e.fade_length = ms_from_us(env.releaseDuration); | ||
| 343 | e.fade_level = hsat(env.releaseGain); | ||
| 344 | }; | ||
| 345 | // Force along +X: the axis a wheel's or a stick's motor acts on, which is the one the motor reports (LinearX). | ||
| 346 | auto direction = [](SDL_HapticDirection& d) { | ||
| 347 | d.type = SDL_HAPTIC_CARTESIAN; | ||
| 348 | d.dir[0] = 1; | ||
| 349 | }; | ||
| 350 | switch (p.kind) { | ||
| 351 | case GameInputForceFeedbackConstant: { | ||
| 352 | SDL_HapticConstant& e = out->constant; | ||
| 353 | e.type = SDL_HAPTIC_CONSTANT; | ||
| 354 | direction(e.direction); | ||
| 355 | e.length = *length_ms; | ||
| 356 | e.level = hlevel(p.data.constant.magnitude.linearX * level); | ||
| 357 | envelope(e); | ||
| 358 | break; | ||
| 359 | } | ||
| 360 | case GameInputForceFeedbackRamp: { | ||
| 361 | SDL_HapticRamp& e = out->ramp; | ||
| 362 | e.type = SDL_HAPTIC_RAMP; | ||
| 363 | direction(e.direction); | ||
| 364 | e.length = *length_ms; | ||
| 365 | e.start = hlevel(p.data.ramp.startMagnitude.linearX * level); | ||
| 366 | e.end = hlevel(p.data.ramp.endMagnitude.linearX * level); | ||
| 367 | if (used) { | ||
| 368 | e.attack_length = ms_from_us(env.attackDuration); | ||
| 369 | e.attack_level = hsat(env.attackGain); | ||
| 370 | e.fade_length = ms_from_us(env.releaseDuration); | ||
| 371 | e.fade_level = hsat(env.releaseGain); | ||
| 372 | } | ||
| 373 | break; | ||
| 374 | } | ||
| 375 | case GameInputForceFeedbackSineWave: | ||
| 376 | case GameInputForceFeedbackSquareWave: | ||
| 377 | case GameInputForceFeedbackTriangleWave: | ||
| 378 | case GameInputForceFeedbackSawtoothUpWave: | ||
| 379 | case GameInputForceFeedbackSawtoothDownWave: { | ||
| 380 | SDL_HapticPeriodic& e = out->periodic; | ||
| 381 | e.type = p.kind == GameInputForceFeedbackSineWave ? SDL_HAPTIC_SINE | ||
| 382 | : p.kind == GameInputForceFeedbackSquareWave ? SDL_HAPTIC_SQUARE | ||
| 383 | : p.kind == GameInputForceFeedbackTriangleWave ? SDL_HAPTIC_TRIANGLE | ||
| 384 | : p.kind == GameInputForceFeedbackSawtoothUpWave ? SDL_HAPTIC_SAWTOOTHUP | ||
| 385 | : SDL_HAPTIC_SAWTOOTHDOWN; | ||
| 386 | direction(e.direction); | ||
| 387 | e.length = *length_ms; | ||
| 388 | const GameInputForceFeedbackPeriodicParams& pp = p.data.sineWave; | ||
| 389 | // GameInput gives the wave's frequency in Hz and its phase in degrees; SDL wants a period in ms and a | ||
| 390 | // phase in hundredths of a degree. | ||
| 391 | e.period = pp.frequency > 0.0f ? static_cast<Uint16>(std::clamp(std::lround(1000.0f / pp.frequency), 1L, 65535L)) : 100; | ||
| 392 | e.magnitude = hlevel(pp.magnitude.linearX * level); | ||
| 393 | e.offset = hlevel(pp.bias * level); | ||
| 394 | e.phase = static_cast<Uint16>(std::clamp(std::lround(pp.phase * 100.0f), 0L, 35999L)); | ||
| 395 | if (used) { | ||
| 396 | e.attack_length = ms_from_us(env.attackDuration); | ||
| 397 | e.attack_level = hsat(env.attackGain); | ||
| 398 | e.fade_length = ms_from_us(env.releaseDuration); | ||
| 399 | e.fade_level = hsat(env.releaseGain); | ||
| 400 | } | ||
| 401 | break; | ||
| 402 | } | ||
| 403 | default: { // spring, friction, damper, inertia | ||
| 404 | SDL_HapticCondition& e = out->condition; | ||
| 405 | e.type = p.kind == GameInputForceFeedbackSpring ? SDL_HAPTIC_SPRING | ||
| 406 | : p.kind == GameInputForceFeedbackFriction ? SDL_HAPTIC_FRICTION | ||
| 407 | : p.kind == GameInputForceFeedbackDamper ? SDL_HAPTIC_DAMPER | ||
| 408 | : SDL_HAPTIC_INERTIA; | ||
| 409 | e.length = *length_ms; | ||
| 410 | const GameInputForceFeedbackConditionParams& cp = p.data.spring; | ||
| 411 | const float center[3] = {cp.magnitude.linearX, cp.magnitude.linearY, cp.magnitude.linearZ}; | ||
| 412 | for (int a = 0; a < 3; ++a) { | ||
| 413 | e.right_sat[a] = hsat(cp.maxPositiveMagnitude); | ||
| 414 | e.left_sat[a] = hsat(cp.maxNegativeMagnitude); | ||
| 415 | e.right_coeff[a] = hlevel(cp.positiveCoefficient * level); | ||
| 416 | e.left_coeff[a] = hlevel(-cp.negativeCoefficient * level); | ||
| 417 | e.deadband[a] = hsat(cp.deadZone); | ||
| 418 | e.center[a] = hlevel(center[a]); | ||
| 419 | } | ||
| 420 | break; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | return true; | ||
| 424 | } | ||
| 425 | |||
| 241 | class Device final : public IGameInputDevice { | 426 | class Device final : public IGameInputDevice { |
| 242 | public: | 427 | public: |
| 243 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { | 428 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { |
| @@ -258,13 +443,10 @@ public: | |||
| 258 | const GameInputDeviceInfo* STDMETHODCALLTYPE GetDeviceInfo() override { return &info; } | 443 | const GameInputDeviceInfo* STDMETHODCALLTYPE GetDeviceInfo() override { return &info; } |
| 259 | uint32_t STDMETHODCALLTYPE GetDeviceStatus() override { return status.load(); } | 444 | uint32_t STDMETHODCALLTYPE GetDeviceStatus() override { return status.load(); } |
| 260 | void STDMETHODCALLTYPE GetBatteryState(GameInputBatteryState* state) override; | 445 | void STDMETHODCALLTYPE GetBatteryState(GameInputBatteryState* state) override; |
| 261 | HRESULT STDMETHODCALLTYPE CreateForceFeedbackEffect(uint32_t, const void*, void** effect) override { | 446 | HRESULT STDMETHODCALLTYPE CreateForceFeedbackEffect(uint32_t motorIndex, const void* params, void** effect) override; |
| 262 | if (effect) *effect = nullptr; | 447 | bool STDMETHODCALLTYPE IsForceFeedbackMotorPoweredOn(uint32_t motorIndex) override; |
| 263 | return E_INVALIDARG; // forceFeedbackMotorCount is 0 | 448 | void STDMETHODCALLTYPE SetForceFeedbackMotorGain(uint32_t motorIndex, float masterGain) override; |
| 264 | } | 449 | HRESULT STDMETHODCALLTYPE SetHapticMotorState(uint32_t motorIndex, const void* params) override; |
| 265 | bool STDMETHODCALLTYPE IsForceFeedbackMotorPoweredOn(uint32_t) override { return false; } | ||
| 266 | void STDMETHODCALLTYPE SetForceFeedbackMotorGain(uint32_t, float) override {} | ||
| 267 | HRESULT STDMETHODCALLTYPE SetHapticMotorState(uint32_t, const void*) override { return E_INVALIDARG; } | ||
| 268 | void STDMETHODCALLTYPE SetRumbleState(const GameInputRumbleParams* params) override; | 450 | void STDMETHODCALLTYPE SetRumbleState(const GameInputRumbleParams* params) override; |
| 269 | void STDMETHODCALLTYPE SetInputSynchronizationState(bool) override {} | 451 | void STDMETHODCALLTYPE SetInputSynchronizationState(bool) override {} |
| 270 | void STDMETHODCALLTYPE SendInputSynchronizationHint() override {} | 452 | void STDMETHODCALLTYPE SendInputSynchronizationHint() override {} |
| @@ -299,12 +481,20 @@ public: | |||
| 299 | GameInputGamepadInfo gamepad_info{}; | 481 | GameInputGamepadInfo gamepad_info{}; |
| 300 | GameInputUiNavigationInfo ui_info{}; | 482 | GameInputUiNavigationInfo ui_info{}; |
| 301 | GameInputMotionInfo motion_info{}; // filled only when the pad has sensors; info.motionInfo points at it | 483 | GameInputMotionInfo motion_info{}; // filled only when the pad has sensors; info.motionInfo points at it |
| 484 | GameInputForceFeedbackMotorInfo ff_motor_info{}; // filled when SDL opened the pad as a haptic device | ||
| 485 | GameInputHapticFeedbackMotorInfo haptic_motor_info[kHapticMotors]{}; // the trigger motors, when the pad has them | ||
| 486 | GameInputHapticWaveformInfo haptic_waveform_info[kHapticMotors]{}; // one waveform each, an intensity | ||
| 302 | std::atomic<uint32_t> status{GameInputDeviceNoStatus}; | 487 | std::atomic<uint32_t> status{GameInputDeviceNoStatus}; |
| 303 | std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading | 488 | std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading |
| 304 | uint64_t evicted_order = 0; // order of the newest reading dropped from history | 489 | uint64_t evicted_order = 0; // order of the newest reading dropped from history |
| 305 | Reading* latest() const { return history.empty() ? nullptr : history.back(); } | 490 | Reading* latest() const { return history.empty() ? nullptr : history.back(); } |
| 306 | uint64_t seq = 0; | 491 | uint64_t seq = 0; |
| 307 | Rumble wanted; | 492 | Rumble wanted; |
| 493 | float motor_gain = 1.0f; // SetForceFeedbackMotorGain, 0..1 | ||
| 494 | std::vector<ForceFeedbackEffect*> effects; // every live effect, so the poll thread can drive them | ||
| 495 | std::vector<int> retired; // SDL effect ids whose object is gone: destroyed on the poll thread | ||
| 496 | float haptic_intensity[kHapticMotors] = {}; // SetHapticMotorState, 0..1 | ||
| 497 | uint64_t haptic_until[kHapticMotors] = {}; // when the request ends, 0 for "until changed" | ||
| 308 | GameInputBatteryState battery{0, 0, 0, 0, GameInputBatteryUnknown}; | 498 | GameInputBatteryState battery{0, 0, 0, 0, GameInputBatteryUnknown}; |
| 309 | AppLocalDeviceId base_id{}; // id_bytes of the identity alone: twins share it, info.deviceId tells them apart | 499 | AppLocalDeviceId base_id{}; // id_bytes of the identity alone: twins share it, info.deviceId tells them apart |
| 310 | 500 | ||
| @@ -313,6 +503,11 @@ public: | |||
| 313 | SDL_JoystickID id = 0; | 503 | SDL_JoystickID id = 0; |
| 314 | int player = -1; | 504 | int player = -1; |
| 315 | bool is_dualsense = false, has_rumble = false, has_trigger_rumble = false, has_motion = false; | 505 | bool is_dualsense = false, has_rumble = false, has_trigger_rumble = false, has_motion = false; |
| 506 | bool has_trigger_output = false; // the pad backs trigger motors: rumble, DualSense effects or haptic motors | ||
| 507 | void* haptic = nullptr; // SDL_Haptic behind the joystick, when SDL has one for this pad | ||
| 508 | uint32_t ff_features = 0; // the SDL_HAPTIC_* bits it reports: which effect kinds it can play | ||
| 509 | int sent_gain = -1; // device gain 0..100 last sent, so a steady gain costs one call | ||
| 510 | uint32_t haptic_motors = 0; // trigger haptic motors the pad reports (0 or 2) | ||
| 316 | rt::gameinput::OutputState sent{}; | 511 | rt::gameinput::OutputState sent{}; |
| 317 | Snapshot last_snap{}; // what the latest reading holds | 512 | Snapshot last_snap{}; // what the latest reading holds |
| 318 | uint64_t last_reading_us = 0; // for the motion cadence: readings stream at ~125 Hz on pads with sensors | 513 | uint64_t last_reading_us = 0; // for the motion cadence: readings stream at ~125 Hz on pads with sensors |
| @@ -359,6 +554,50 @@ struct Event { | |||
| 359 | GameInputCallbackToken only; // enumeration: deliver to this callback only | 554 | GameInputCallbackToken only; // enumeration: deliver to this callback only |
| 360 | }; | 555 | }; |
| 361 | 556 | ||
| 557 | // One effect a title created, on the device it belongs to. The object and its parameters belong to the title; SDL's | ||
| 558 | // effect is created, run and destroyed on the poll thread, so a title call only writes its state and bumps `version_`, | ||
| 559 | // which the poll thread compares before it touches SDL. A new effect is Running: CreateForceFeedbackEffect already | ||
| 560 | // carried the parameters, and the documented use (create once, SetParams per frame) never reaches SetState. | ||
| 561 | class ForceFeedbackEffect final : public IGameInputForceFeedbackEffect { | ||
| 562 | public: | ||
| 563 | ForceFeedbackEffect(Device* device, uint32_t motor, const GameInputForceFeedbackParams& params); | ||
| 564 | ~ForceFeedbackEffect(); | ||
| 565 | |||
| 566 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override; | ||
| 567 | ULONG STDMETHODCALLTYPE AddRef() override; | ||
| 568 | ULONG STDMETHODCALLTYPE Release() override; | ||
| 569 | void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; | ||
| 570 | uint32_t STDMETHODCALLTYPE GetMotorIndex() override; | ||
| 571 | float STDMETHODCALLTYPE GetGain() override; | ||
| 572 | void STDMETHODCALLTYPE SetGain(float gain) override; | ||
| 573 | void STDMETHODCALLTYPE GetParams(GameInputForceFeedbackParams* params) override; | ||
| 574 | bool STDMETHODCALLTYPE SetParams(const GameInputForceFeedbackParams* params) override; | ||
| 575 | GameInputFeedbackEffectState STDMETHODCALLTYPE GetState() override; | ||
| 576 | void STDMETHODCALLTYPE SetState(GameInputFeedbackEffectState state) override; | ||
| 577 | |||
| 578 | // Poll thread: make SDL match the effect's parameters, gain and state. `muted` says every output is off | ||
| 579 | // (LEONIDA_RUMBLE=0); the motor's master gain is SDL's device gain, set once by apply_effects. | ||
| 580 | void sync(Device* device, uint64_t now, bool muted); | ||
| 581 | // Poll thread, on unplug: the SDL device went with the pad, so forget its effect id without touching SDL. | ||
| 582 | void orphan(); | ||
| 583 | |||
| 584 | private: | ||
| 585 | std::atomic<ULONG> refs_{1}; | ||
| 586 | Device* device_; | ||
| 587 | uint32_t motor_; | ||
| 588 | // Written by title calls under g().mu and read by the poll thread. | ||
| 589 | GameInputForceFeedbackParams params_; | ||
| 590 | float gain_ = 1.0f; | ||
| 591 | GameInputFeedbackEffectState state_ = GameInputFeedbackRunning; | ||
| 592 | uint64_t version_ = 1; | ||
| 593 | // Poll thread only. | ||
| 594 | uint64_t applied_ = 0; // the version SDL has | ||
| 595 | int sdl_effect_ = -1; | ||
| 596 | bool sdl_running_ = false; | ||
| 597 | bool endless_ = true; // GameInput gave no duration: re-run before the SDL length runs out | ||
| 598 | uint64_t run_at_ = 0, run_for_ = 0; | ||
| 599 | }; | ||
| 600 | |||
| 362 | class Dispatcher; | 601 | class Dispatcher; |
| 363 | 602 | ||
| 364 | struct State { | 603 | struct State { |
| @@ -595,8 +834,7 @@ void fill_info(Device* d, SDL_Gamepad* p, const AppLocalDeviceId& id) { | |||
| 595 | // as the per-device one, so only pads with sensors answer a motion query. | 834 | // as the per-device one, so only pads with sensors answer a motion query. |
| 596 | i.supportedInput = kBaseKinds; | 835 | i.supportedInput = kBaseKinds; |
| 597 | if (d->has_rumble) i.supportedRumbleMotors |= GameInputRumbleLowFrequency | GameInputRumbleHighFrequency; | 836 | if (d->has_rumble) i.supportedRumbleMotors |= GameInputRumbleLowFrequency | GameInputRumbleHighFrequency; |
| 598 | if (d->has_trigger_rumble || (d->is_dualsense && g().config.ds_triggers != rt::gameinput::DsTriggerMode::Off)) | 837 | if (d->has_trigger_output) i.supportedRumbleMotors |= GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger; |
| 599 | i.supportedRumbleMotors |= GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger; | ||
| 600 | if (d->has_motion) { | 838 | if (d->has_motion) { |
| 601 | i.supportedInput |= GameInputKindMotion; | 839 | i.supportedInput |= GameInputKindMotion; |
| 602 | // The maxima GetMotionState's fractions divide by. The magnetometer is not available: SDL's gamepad API has | 840 | // The maxima GetMotionState's fractions divide by. The magnetometer is not available: SDL's gamepad API has |
| @@ -604,6 +842,17 @@ void fill_info(Device* d, SDL_Gamepad* p, const AppLocalDeviceId& id) { | |||
| 604 | d->motion_info = {kMaxAcceleration, kMaxAngularVelocity, 0.0f}; | 842 | d->motion_info = {kMaxAcceleration, kMaxAngularVelocity, 0.0f}; |
| 605 | i.motionInfo = &d->motion_info; | 843 | i.motionInfo = &d->motion_info; |
| 606 | } | 844 | } |
| 845 | // The motors this pad can back. Force feedback is whatever SDL opened a haptic device for with effect kinds of its | ||
| 846 | // own; the haptic motors are the trigger motors the pad already has for rumble, which the shim can drive as an | ||
| 847 | // intensity. A pad with neither keeps the old answers: no motors at all. | ||
| 848 | if (d->haptic) { | ||
| 849 | i.forceFeedbackMotorCount = kForceFeedbackMotors; | ||
| 850 | i.forceFeedbackMotorInfo = &d->ff_motor_info; | ||
| 851 | } | ||
| 852 | if (d->haptic_motors) { | ||
| 853 | i.hapticFeedbackMotorCount = d->haptic_motors; | ||
| 854 | i.hapticFeedbackMotorInfo = d->haptic_motor_info; | ||
| 855 | } | ||
| 607 | i.controllerAxisCount = kAxisCount; | 856 | i.controllerAxisCount = kAxisCount; |
| 608 | i.controllerButtonCount = kButtonCount; | 857 | i.controllerButtonCount = kButtonCount; |
| 609 | for (uint32_t a = 0; a < kAxisCount; ++a) { | 858 | for (uint32_t a = 0; a < kAxisCount; ++a) { |
| @@ -701,9 +950,60 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) { | |||
| 701 | SDL_SetGamepadSensorEnabled(p, SDL_SENSOR_GYRO, kMotionSensorRate); | 950 | SDL_SetGamepadSensorEnabled(p, SDL_SENSOR_GYRO, kMotionSensorRate); |
| 702 | SDL_SetGamepadSensorEnabled(p, SDL_SENSOR_ACCEL, kMotionSensorRate); | 951 | SDL_SetGamepadSensorEnabled(p, SDL_SENSOR_ACCEL, kMotionSensorRate); |
| 703 | } | 952 | } |
| 953 | d->has_trigger_output = d->has_trigger_rumble || | ||
| 954 | (d->is_dualsense && s.config.ds_triggers != rt::gameinput::DsTriggerMode::Off); | ||
| 955 | // Force feedback: SDL opens a wheel or a stick as a joystick with a haptic device behind it and never through the | ||
| 956 | // gamepad API, so it is opened from the joystick and closed with the pad. Only what the device really reports is | ||
| 957 | // passed on: no haptic device, no motor, and an effect kind SDL does not list is not offered to a title. | ||
| 958 | d->haptic = haptics().open_joystick(SDL_GetGamepadJoystick(p)); | ||
| 959 | d->ff_features = 0; | ||
| 960 | d->ff_motor_info = {}; | ||
| 961 | d->sent_gain = -1; | ||
| 962 | if (d->haptic) { | ||
| 963 | const uint32_t features = haptics().features(d->haptic); | ||
| 964 | if (features) { | ||
| 965 | GameInputForceFeedbackMotorInfo& m = d->ff_motor_info; | ||
| 966 | d->ff_features = features; | ||
| 967 | m.supportedAxes = GameInputFeedbackAxisLinearX; // the axis a wheel's or a stick's motor acts on | ||
| 968 | m.location = GameInputLocationChassis; | ||
| 969 | m.locationId = 0; | ||
| 970 | m.maxSimultaneousEffects = static_cast<uint32_t>(std::max(1, haptics().max_effects(d->haptic))); | ||
| 971 | m.isConstantEffectSupported = (features & SDL_HAPTIC_CONSTANT) != 0; | ||
| 972 | m.isRampEffectSupported = (features & SDL_HAPTIC_RAMP) != 0; | ||
| 973 | m.isSineWaveEffectSupported = (features & SDL_HAPTIC_SINE) != 0; | ||
| 974 | m.isSquareWaveEffectSupported = (features & SDL_HAPTIC_SQUARE) != 0; | ||
| 975 | m.isTriangleWaveEffectSupported = (features & SDL_HAPTIC_TRIANGLE) != 0; | ||
| 976 | m.isSawtoothUpWaveEffectSupported = (features & SDL_HAPTIC_SAWTOOTHUP) != 0; | ||
| 977 | m.isSawtoothDownWaveEffectSupported = (features & SDL_HAPTIC_SAWTOOTHDOWN) != 0; | ||
| 978 | m.isSpringEffectSupported = (features & SDL_HAPTIC_SPRING) != 0; | ||
| 979 | m.isFrictionEffectSupported = (features & SDL_HAPTIC_FRICTION) != 0; | ||
| 980 | m.isDamperEffectSupported = (features & SDL_HAPTIC_DAMPER) != 0; | ||
| 981 | m.isInertiaEffectSupported = (features & SDL_HAPTIC_INERTIA) != 0; | ||
| 982 | } else { | ||
| 983 | haptics().close(d->haptic); | ||
| 984 | d->haptic = nullptr; | ||
| 985 | } | ||
| 986 | } | ||
| 987 | // Haptic motors: the trigger motors, one waveform each. The waveform identifier is this shim's choice, an | ||
| 988 | // intensity (HID physical-interface usage 0x0F/0x58), because an intensity is the one thing SDL lets it send. | ||
| 989 | d->haptic_motors = 0; | ||
| 990 | if (d->has_trigger_output) { | ||
| 991 | for (uint32_t m = 0; m < kHapticMotors; ++m) { | ||
| 992 | d->haptic_waveform_info[m] = {{0x0F, 0x58}, true, true, true, false, 0}; | ||
| 993 | d->haptic_motor_info[m] = {}; | ||
| 994 | d->haptic_motor_info[m].mappedRumbleMotors = m ? GameInputRumbleRightTrigger : GameInputRumbleLeftTrigger; | ||
| 995 | d->haptic_motor_info[m].location = GameInputLocationChassis; | ||
| 996 | d->haptic_motor_info[m].locationId = m; | ||
| 997 | d->haptic_motor_info[m].waveformCount = 1; | ||
| 998 | d->haptic_motor_info[m].waveformInfo = &d->haptic_waveform_info[m]; | ||
| 999 | } | ||
| 1000 | d->haptic_motors = kHapticMotors; | ||
| 1001 | } | ||
| 704 | d->sent = {}; | 1002 | d->sent = {}; |
| 705 | d->sent.is_dualsense = d->is_dualsense; | 1003 | d->sent.is_dualsense = d->is_dualsense; |
| 706 | d->sent.trigger_rumble = d->has_trigger_rumble; | 1004 | d->sent.trigger_rumble = d->has_trigger_rumble; |
| 1005 | d->sent.force_feedback = d->haptic != nullptr; | ||
| 1006 | d->sent.haptic_motors = d->haptic_motors; | ||
| 707 | d->ds_sent_once = false; | 1007 | d->ds_sent_once = false; |
| 708 | d->motors_sent_at = d->triggers_sent_at = d->battery_at = 0; | 1008 | d->motors_sent_at = d->triggers_sent_at = d->battery_at = 0; |
| 709 | d->wanted = {}; | 1009 | d->wanted = {}; |
| @@ -730,9 +1030,10 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) { | |||
| 730 | d->last_snap = snap; | 1030 | d->last_snap = snap; |
| 731 | push_reading(d, snap, ts); | 1031 | push_reading(d, snap, ts); |
| 732 | queue_event({CbType::Device, d, nullptr, ts, status, GameInputDeviceNoStatus, false, 0}); | 1032 | queue_event({CbType::Device, d, nullptr, ts, status, GameInputDeviceNoStatus, false, 0}); |
| 733 | std::fprintf(stderr, "[rt] gameinput: connected %s (%04x:%04x, player %d%s%s%s)\n", d->name.c_str(), | 1033 | std::fprintf(stderr, "[rt] gameinput: connected %s (%04x:%04x, player %d%s%s%s%s)\n", d->name.c_str(), |
| 734 | d->info.vendorId, d->info.productId, player + 1, d->is_dualsense ? ", DualSense trigger effects" : "", | 1034 | d->info.vendorId, d->info.productId, player + 1, d->is_dualsense ? ", DualSense trigger effects" : "", |
| 735 | d->has_trigger_rumble ? ", trigger rumble" : "", d->has_motion ? ", gyro" : ""); | 1035 | d->has_trigger_rumble ? ", trigger rumble" : "", d->has_motion ? ", gyro" : "", |
| 1036 | d->haptic ? ", force feedback" : ""); | ||
| 736 | } | 1037 | } |
| 737 | 1038 | ||
| 738 | void close_pad(Device* d, uint64_t ts) { | 1039 | void close_pad(Device* d, uint64_t ts) { |
| @@ -742,6 +1043,19 @@ void close_pad(Device* d, uint64_t ts) { | |||
| 742 | SDL_CloseGamepad(d->pad); | 1043 | SDL_CloseGamepad(d->pad); |
| 743 | d->pad = nullptr; | 1044 | d->pad = nullptr; |
| 744 | d->player = -1; | 1045 | d->player = -1; |
| 1046 | // The haptic device belongs to the joystick, which goes with the gamepad. Effects a title still holds stay alive | ||
| 1047 | // and pick up again when the pad comes back. | ||
| 1048 | if (d->haptic) { | ||
| 1049 | haptics().close(d->haptic); | ||
| 1050 | d->haptic = nullptr; | ||
| 1051 | } | ||
| 1052 | d->retired.clear(); | ||
| 1053 | d->ff_features = 0; | ||
| 1054 | d->ff_motor_info = {}; | ||
| 1055 | d->sent_gain = -1; | ||
| 1056 | d->haptic_motors = 0; | ||
| 1057 | for (ForceFeedbackEffect* e : d->effects) e->orphan(); | ||
| 1058 | for (uint32_t m = 0; m < kHapticMotors; ++m) d->haptic_intensity[m] = 0, d->haptic_until[m] = 0; | ||
| 745 | if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session | 1059 | if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session |
| 746 | for (Reading* r : d->history) r->Release(); | 1060 | for (Reading* r : d->history) r->Release(); |
| 747 | d->history.clear(); | 1061 | d->history.clear(); |
| @@ -768,14 +1082,55 @@ void ds_effect(float strength, uint8_t out[11]) { | |||
| 768 | } | 1082 | } |
| 769 | } | 1083 | } |
| 770 | 1084 | ||
| 1085 | // The force-feedback effects and the device gain, on the poll thread: SDL's effects are created, updated, run and | ||
| 1086 | // destroyed here and nowhere else, so a title call costs a lock and a copy. | ||
| 1087 | void apply_effects(Device* d, uint64_t now) { | ||
| 1088 | State& s = g(); | ||
| 1089 | std::vector<ForceFeedbackEffect*> live; | ||
| 1090 | std::vector<int> retire; | ||
| 1091 | float motor_gain; | ||
| 1092 | bool muted; | ||
| 1093 | { | ||
| 1094 | std::lock_guard lock(s.mu); | ||
| 1095 | live.reserve(d->effects.size()); | ||
| 1096 | for (ForceFeedbackEffect* e : d->effects) { // referenced: the title may release one while this runs | ||
| 1097 | e->AddRef(); | ||
| 1098 | live.push_back(e); | ||
| 1099 | } | ||
| 1100 | retire.swap(d->retired); | ||
| 1101 | motor_gain = d->motor_gain; | ||
| 1102 | muted = !s.config.rumble; | ||
| 1103 | } | ||
| 1104 | if (d->haptic) { | ||
| 1105 | for (int id : retire) haptics().destroy_effect(d->haptic, id); | ||
| 1106 | const int gain = static_cast<int>(std::lround(std::clamp(motor_gain, 0.0f, 1.0f) * 100.0f)); | ||
| 1107 | if (gain != d->sent_gain) { | ||
| 1108 | haptics().set_gain(d->haptic, gain); | ||
| 1109 | d->sent_gain = gain; | ||
| 1110 | } | ||
| 1111 | } | ||
| 1112 | for (ForceFeedbackEffect* e : live) { | ||
| 1113 | e->sync(d, now, muted); | ||
| 1114 | e->Release(); | ||
| 1115 | } | ||
| 1116 | } | ||
| 1117 | |||
| 771 | void apply_output(Device* d, uint64_t now) { | 1118 | void apply_output(Device* d, uint64_t now) { |
| 772 | State& s = g(); | 1119 | State& s = g(); |
| 773 | Rumble want; | 1120 | Rumble want; |
| 1121 | float haptic[kHapticMotors] = {}; | ||
| 774 | { | 1122 | { |
| 775 | std::lock_guard lock(s.mu); | 1123 | std::lock_guard lock(s.mu); |
| 776 | want = d->wanted; | 1124 | want = d->wanted; |
| 777 | } | 1125 | for (uint32_t m = 0; m < d->haptic_motors; ++m) |
| 778 | if (!s.config.rumble) want = {}; | 1126 | if (!d->haptic_until[m] || now < d->haptic_until[m]) haptic[m] = d->haptic_intensity[m]; |
| 1127 | } | ||
| 1128 | const bool muted = !s.config.rumble; | ||
| 1129 | if (muted) want = {}; | ||
| 1130 | // The haptic motors sit on the trigger motors, so their intensity and SetRumbleState's trigger level go out on one | ||
| 1131 | // output: the stronger of the two wins, and a request that has run out is dropped here. | ||
| 1132 | want.left = std::max(want.left, muted ? 0.0f : haptic[0]); | ||
| 1133 | want.right = std::max(want.right, muted ? 0.0f : haptic[1]); | ||
| 779 | rt::gameinput::OutputState& o = d->sent; | 1134 | rt::gameinput::OutputState& o = d->sent; |
| 780 | 1135 | ||
| 781 | if (d->has_rumble) { | 1136 | if (d->has_rumble) { |
| @@ -789,19 +1144,21 @@ void apply_output(Device* d, uint64_t now) { | |||
| 789 | } | 1144 | } |
| 790 | } | 1145 | } |
| 791 | if (d->is_dualsense) { | 1146 | if (d->is_dualsense) { |
| 792 | if (s.config.ds_triggers == rt::gameinput::DsTriggerMode::Off) return; // leave the triggers alone | 1147 | // LEONIDA_DS_TRIGGERS=off leaves the triggers alone. |
| 793 | uint8_t right[11], left[11]; | 1148 | if (s.config.ds_triggers != rt::gameinput::DsTriggerMode::Off) { |
| 794 | ds_effect(want.right, right); | 1149 | uint8_t right[11], left[11]; |
| 795 | ds_effect(want.left, left); | 1150 | ds_effect(want.right, right); |
| 796 | if (!d->ds_sent_once || std::memcmp(right, o.ds_right, 11) || std::memcmp(left, o.ds_left, 11)) { | 1151 | ds_effect(want.left, left); |
| 797 | uint8_t block[kDsEffectSize] = {}; | 1152 | if (!d->ds_sent_once || std::memcmp(right, o.ds_right, 11) || std::memcmp(left, o.ds_left, 11)) { |
| 798 | block[0] = 0x04 | 0x08; // valid flags: right and left trigger effect | 1153 | uint8_t block[kDsEffectSize] = {}; |
| 799 | std::memcpy(block + 10, right, 11); | 1154 | block[0] = 0x04 | 0x08; // valid flags: right and left trigger effect |
| 800 | std::memcpy(block + 21, left, 11); | 1155 | std::memcpy(block + 10, right, 11); |
| 801 | if (SDL_SendGamepadEffect(d->pad, block, sizeof block)) { | 1156 | std::memcpy(block + 21, left, 11); |
| 802 | std::memcpy(o.ds_right, right, 11); | 1157 | if (SDL_SendGamepadEffect(d->pad, block, sizeof block)) { |
| 803 | std::memcpy(o.ds_left, left, 11); | 1158 | std::memcpy(o.ds_right, right, 11); |
| 804 | d->ds_sent_once = true; | 1159 | std::memcpy(o.ds_left, left, 11); |
| 1160 | d->ds_sent_once = true; | ||
| 1161 | } | ||
| 805 | } | 1162 | } |
| 806 | } | 1163 | } |
| 807 | } else if (d->has_trigger_rumble) { | 1164 | } else if (d->has_trigger_rumble) { |
| @@ -814,6 +1171,7 @@ void apply_output(Device* d, uint64_t now) { | |||
| 814 | d->triggers_sent_at = now; | 1171 | d->triggers_sent_at = now; |
| 815 | } | 1172 | } |
| 816 | } | 1173 | } |
| 1174 | apply_effects(d, now); | ||
| 817 | } | 1175 | } |
| 818 | 1176 | ||
| 819 | void refresh_battery(Device* d, uint64_t now) { | 1177 | void refresh_battery(Device* d, uint64_t now) { |
| @@ -1123,6 +1481,197 @@ void Device::SetRumbleState(const GameInputRumbleParams* params) { | |||
| 1123 | wanted = params ? Rumble{params->lowFrequency, params->highFrequency, params->leftTrigger, params->rightTrigger} : Rumble{}; | 1481 | wanted = params ? Rumble{params->lowFrequency, params->highFrequency, params->leftTrigger, params->rightTrigger} : Rumble{}; |
| 1124 | } | 1482 | } |
| 1125 | 1483 | ||
| 1484 | HRESULT Device::CreateForceFeedbackEffect(uint32_t motorIndex, const void* params, void** effect) { | ||
| 1485 | if (!effect) return E_POINTER; | ||
| 1486 | *effect = nullptr; | ||
| 1487 | if (!params || motorIndex >= info.forceFeedbackMotorCount) return E_INVALIDARG; | ||
| 1488 | const auto* p = static_cast<const GameInputForceFeedbackParams*>(params); | ||
| 1489 | // Only what the motor reported. SDL is not asked here: this is a title call, so the cached feature mask decides, | ||
| 1490 | // and the poll thread is what finds out if SDL refuses the effect anyway. | ||
| 1491 | if (!valid_kind(p->kind) || !(ff_features & kind_feature(p->kind))) return E_INVALIDARG; | ||
| 1492 | *effect = new ForceFeedbackEffect(this, motorIndex, *p); | ||
| 1493 | return S_OK; | ||
| 1494 | } | ||
| 1495 | |||
| 1496 | bool Device::IsForceFeedbackMotorPoweredOn(uint32_t motorIndex) { | ||
| 1497 | return motorIndex < info.forceFeedbackMotorCount && haptic != nullptr && status.load() != GameInputDeviceNoStatus; | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | void Device::SetForceFeedbackMotorGain(uint32_t motorIndex, float masterGain) { | ||
| 1501 | std::lock_guard lock(g().mu); | ||
| 1502 | if (motorIndex < info.forceFeedbackMotorCount) motor_gain = std::clamp(masterGain, 0.0f, 1.0f); | ||
| 1503 | } | ||
| 1504 | |||
| 1505 | HRESULT Device::SetHapticMotorState(uint32_t motorIndex, const void* params) { | ||
| 1506 | std::lock_guard lock(g().mu); | ||
| 1507 | if (motorIndex >= haptic_motors) return E_INVALIDARG; | ||
| 1508 | if (!params) { // stop the motor | ||
| 1509 | haptic_intensity[motorIndex] = 0.0f; | ||
| 1510 | haptic_until[motorIndex] = 0; | ||
| 1511 | return S_OK; | ||
| 1512 | } | ||
| 1513 | const auto* p = static_cast<const GameInputHapticFeedbackParams*>(params); | ||
| 1514 | if (p->waveformIndex >= haptic_motor_info[motorIndex].waveformCount) return E_INVALIDARG; | ||
| 1515 | haptic_intensity[motorIndex] = std::clamp(p->intensity, 0.0f, 1.0f); | ||
| 1516 | // duration and repeatDelay are microseconds; playCount 0 plays until the state is set again. The poll thread sends | ||
| 1517 | // the level and drops it when the deadline passes. | ||
| 1518 | uint64_t total = p->playCount == 0 ? 0 : p->duration; | ||
| 1519 | if (p->playCount > 1) total = p->duration * p->playCount + p->repeatDelay * (p->playCount - 1); | ||
| 1520 | haptic_until[motorIndex] = total ? now_us() + total : 0; | ||
| 1521 | return S_OK; | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | // ---- IGameInputForceFeedbackEffect ---------------------------------------------------------------- | ||
| 1525 | |||
| 1526 | ForceFeedbackEffect::ForceFeedbackEffect(Device* device, uint32_t motor, const GameInputForceFeedbackParams& params) | ||
| 1527 | : device_(device), motor_(motor), params_(params) { | ||
| 1528 | device_->AddRef(); | ||
| 1529 | std::lock_guard lock(g().mu); | ||
| 1530 | device_->effects.push_back(this); | ||
| 1531 | } | ||
| 1532 | |||
| 1533 | ForceFeedbackEffect::~ForceFeedbackEffect() { | ||
| 1534 | std::lock_guard lock(g().mu); | ||
| 1535 | std::vector<ForceFeedbackEffect*>& list = device_->effects; | ||
| 1536 | list.erase(std::remove(list.begin(), list.end(), this), list.end()); | ||
| 1537 | if (sdl_effect_ >= 0) { // SDL's effect is destroyed by the poll thread, which is the only thread that may | ||
| 1538 | device_->retired.push_back(sdl_effect_); | ||
| 1539 | sdl_effect_ = -1; | ||
| 1540 | } | ||
| 1541 | device_->Release(); | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | HRESULT STDMETHODCALLTYPE ForceFeedbackEffect::QueryInterface(REFIID riid, void** out) { | ||
| 1545 | if (!out) return E_POINTER; | ||
| 1546 | if (same_guid(riid, IID_IUnknown) || same_guid(riid, IID_IGameInputForceFeedbackEffect)) { | ||
| 1547 | AddRef(); | ||
| 1548 | *out = static_cast<IGameInputForceFeedbackEffect*>(this); | ||
| 1549 | return S_OK; | ||
| 1550 | } | ||
| 1551 | *out = nullptr; | ||
| 1552 | return E_NOINTERFACE; | ||
| 1553 | } | ||
| 1554 | |||
| 1555 | ULONG STDMETHODCALLTYPE ForceFeedbackEffect::AddRef() { return ++refs_; } | ||
| 1556 | |||
| 1557 | ULONG STDMETHODCALLTYPE ForceFeedbackEffect::Release() { | ||
| 1558 | const ULONG r = --refs_; | ||
| 1559 | if (r == 0) delete this; | ||
| 1560 | return r; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | void STDMETHODCALLTYPE ForceFeedbackEffect::GetDevice(IGameInputDevice** device) { | ||
| 1564 | if (!device) return; | ||
| 1565 | device_->AddRef(); | ||
| 1566 | *device = device_; | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | uint32_t STDMETHODCALLTYPE ForceFeedbackEffect::GetMotorIndex() { return motor_; } | ||
| 1570 | |||
| 1571 | float STDMETHODCALLTYPE ForceFeedbackEffect::GetGain() { | ||
| 1572 | std::lock_guard lock(g().mu); | ||
| 1573 | return gain_; | ||
| 1574 | } | ||
| 1575 | |||
| 1576 | void STDMETHODCALLTYPE ForceFeedbackEffect::SetGain(float gain) { | ||
| 1577 | std::lock_guard lock(g().mu); | ||
| 1578 | gain_ = std::clamp(gain, 0.0f, 1.0f); | ||
| 1579 | ++version_; | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | void STDMETHODCALLTYPE ForceFeedbackEffect::GetParams(GameInputForceFeedbackParams* params) { | ||
| 1583 | if (!params) return; | ||
| 1584 | std::lock_guard lock(g().mu); | ||
| 1585 | *params = params_; | ||
| 1586 | } | ||
| 1587 | |||
| 1588 | bool STDMETHODCALLTYPE ForceFeedbackEffect::SetParams(const GameInputForceFeedbackParams* params) { | ||
| 1589 | if (!params || !valid_kind(params->kind)) return false; | ||
| 1590 | std::lock_guard lock(g().mu); | ||
| 1591 | params_ = *params; | ||
| 1592 | ++version_; | ||
| 1593 | return true; | ||
| 1594 | } | ||
| 1595 | |||
| 1596 | GameInputFeedbackEffectState STDMETHODCALLTYPE ForceFeedbackEffect::GetState() { | ||
| 1597 | std::lock_guard lock(g().mu); | ||
| 1598 | return state_; | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | void STDMETHODCALLTYPE ForceFeedbackEffect::SetState(GameInputFeedbackEffectState state) { | ||
| 1602 | std::lock_guard lock(g().mu); | ||
| 1603 | state_ = state; | ||
| 1604 | ++version_; | ||
| 1605 | } | ||
| 1606 | |||
| 1607 | void ForceFeedbackEffect::orphan() { | ||
| 1608 | sdl_effect_ = -1; | ||
| 1609 | sdl_running_ = false; | ||
| 1610 | applied_ = 0; | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | void ForceFeedbackEffect::sync(Device* d, uint64_t now, bool muted) { | ||
| 1614 | GameInputForceFeedbackParams params; | ||
| 1615 | float gain; | ||
| 1616 | GameInputFeedbackEffectState state; | ||
| 1617 | uint64_t version; | ||
| 1618 | { | ||
| 1619 | std::lock_guard lock(g().mu); | ||
| 1620 | params = params_; | ||
| 1621 | gain = gain_; | ||
| 1622 | state = state_; | ||
| 1623 | version = version_; | ||
| 1624 | } | ||
| 1625 | if (!d->haptic) return; | ||
| 1626 | // A stopped or paused effect is stopped in SDL: GameInput pauses one effect where SDL pauses the device, so a | ||
| 1627 | // pause here is a stop that keeps the parameters. LEONIDA_RUMBLE=0 silences this path like every other. | ||
| 1628 | if (state != GameInputFeedbackRunning || muted) { | ||
| 1629 | if (sdl_running_) { | ||
| 1630 | haptics().stop_effect(d->haptic, sdl_effect_); | ||
| 1631 | sdl_running_ = false; | ||
| 1632 | } | ||
| 1633 | return; | ||
| 1634 | } | ||
| 1635 | // An effect that played the length GameInput gave it is over, as the API says of a finished effect. The run is | ||
| 1636 | // forgotten so that a second SetState(Running) starts it again instead of expiring at once. | ||
| 1637 | if (!endless_ && run_for_ && now >= run_at_ + run_for_) { | ||
| 1638 | if (sdl_running_) { | ||
| 1639 | haptics().stop_effect(d->haptic, sdl_effect_); | ||
| 1640 | sdl_running_ = false; | ||
| 1641 | } | ||
| 1642 | endless_ = true; | ||
| 1643 | run_for_ = 0; | ||
| 1644 | std::lock_guard lock(g().mu); | ||
| 1645 | state_ = GameInputFeedbackStopped; | ||
| 1646 | return; | ||
| 1647 | } | ||
| 1648 | const bool stale = sdl_running_ && endless_ && now - run_at_ > kRumbleRefreshUs; | ||
| 1649 | if (sdl_running_ && version == applied_ && !stale) return; | ||
| 1650 | |||
| 1651 | SDL_HapticEffect fx; | ||
| 1652 | bool endless = true; | ||
| 1653 | uint32_t length_ms = kHapticHoldMs; | ||
| 1654 | if (!build_effect(params, gain, &endless, &length_ms, &fx)) { | ||
| 1655 | if (sdl_running_) haptics().stop_effect(d->haptic, sdl_effect_); | ||
| 1656 | sdl_running_ = false; | ||
| 1657 | return; | ||
| 1658 | } | ||
| 1659 | if (sdl_effect_ < 0) { | ||
| 1660 | sdl_effect_ = haptics().create_effect(d->haptic, &fx); // SDL refused: nothing to drive this effect | ||
| 1661 | if (sdl_effect_ < 0) return; | ||
| 1662 | } else if (!haptics().update_effect(d->haptic, sdl_effect_, &fx)) { | ||
| 1663 | return; | ||
| 1664 | } | ||
| 1665 | // GameInput's playCount 0 means "until stopped", which is what SDL's 0 iterations means. | ||
| 1666 | const uint32_t iterations = endless ? 0 : play_count(params); | ||
| 1667 | if (!haptics().run_effect(d->haptic, sdl_effect_, iterations)) return; | ||
| 1668 | sdl_running_ = true; | ||
| 1669 | applied_ = version; | ||
| 1670 | endless_ = endless; | ||
| 1671 | run_at_ = now; | ||
| 1672 | run_for_ = endless || iterations == 0 ? 0 : static_cast<uint64_t>(length_ms) * 1000ull * iterations; | ||
| 1673 | } | ||
| 1674 | |||
| 1126 | // ---- IGameInputDispatcher ---- | 1675 | // ---- IGameInputDispatcher ---- |
| 1127 | 1676 | ||
| 1128 | class Dispatcher final : public IGameInputDispatcher { | 1677 | class Dispatcher final : public IGameInputDispatcher { |
| @@ -1494,6 +2043,8 @@ void set_keyboard_source(KeyDownFn key_down, HasFocusFn has_focus) { | |||
| 1494 | 2043 | ||
| 1495 | void set_cursor_source(CursorPosFn cursor_pos) { g_cursor_pos = cursor_pos ? cursor_pos : system_cursor_pos; } | 2044 | void set_cursor_source(CursorPosFn cursor_pos) { g_cursor_pos = cursor_pos ? cursor_pos : system_cursor_pos; } |
| 1496 | 2045 | ||
| 2046 | void set_haptic_source(const HapticApi* api) { g_haptic_api = api ? api : &kSdlHaptic; } | ||
| 2047 | |||
| 1497 | const KeyBinding* keyboard_bindings(int* count) { | 2048 | const KeyBinding* keyboard_bindings(int* count) { |
| 1498 | if (count) *count = static_cast<int>(std::size(kKeyBindings)); | 2049 | if (count) *count = static_cast<int>(std::size(kKeyBindings)); |
| 1499 | return kKeyBindings; | 2050 | return kKeyBindings; |