diff options
| author | nicolas <44988633+nschlaepfer@users.noreply.github.com> | 2026-11-21 21:38:20 -0700 |
|---|---|---|
| committer | nicolas <44988633+nschlaepfer@users.noreply.github.com> | 2026-11-21 21:46:14 -0700 |
| commit | b72fc5a4fec776ffa4b19d37c7bf8d62836e3df5 (patch) | |
| tree | a9352dc77152799d399cead60e7bd8271ab1d5cf /runtime/shims/gameinput.cpp | |
| parent | 4a36bb19979b611f525d027f92e0fefad1626546 (diff) | |
Keep gamepad readings in order between title polls
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 105 |
1 files changed, 75 insertions, 30 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 4b5d693ce..ad347babc 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -5,7 +5,8 @@ | |||
| 5 | // vtables follow the public v0 API (gameinput_abi.h) and serves gamepads from SDL3: | 5 | // vtables follow the public v0 API (gameinput_abi.h) and serves gamepads from SDL3: |
| 6 | // | 6 | // |
| 7 | // - device enumeration, connect/disconnect and device callbacks (blocking and async enumeration) | 7 | // - device enumeration, connect/disconnect and device callbacks (blocking and async enumeration) |
| 8 | // - GetCurrentReading / GetNextReading, GetGamepadState, GetUiNavigationState, controller axes and buttons | 8 | // - GetCurrentReading, and GetNextReading / GetPreviousReading over each pad's last half second of readings |
| 9 | // - GetGamepadState, GetUiNavigationState, controller axes and buttons | ||
| 9 | // - SetRumbleState: the two body motors through SDL_RumbleGamepad, the impulse-trigger motors through | 10 | // - SetRumbleState: the two body motors through SDL_RumbleGamepad, the impulse-trigger motors through |
| 10 | // SDL_RumbleGamepadTriggers on pads that have them; on a DualSense the trigger signal becomes an adaptive-trigger | 11 | // SDL_RumbleGamepadTriggers on pads that have them; on a DualSense the trigger signal becomes an adaptive-trigger |
| 11 | // 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 |
| @@ -34,6 +35,7 @@ | |||
| 34 | #include <cstdio> | 35 | #include <cstdio> |
| 35 | #include <cstdlib> | 36 | #include <cstdlib> |
| 36 | #include <cstring> | 37 | #include <cstring> |
| 38 | #include <deque> | ||
| 37 | #include <mutex> | 39 | #include <mutex> |
| 38 | #include <string> | 40 | #include <string> |
| 39 | #include <thread> | 41 | #include <thread> |
| @@ -56,6 +58,8 @@ constexpr Uint32 kRumbleDurationMs = 0xFFFF; // SDL's maximum; refreshed well b | |||
| 56 | constexpr uint64_t kRumbleRefreshUs = 20'000'000; | 58 | constexpr uint64_t kRumbleRefreshUs = 20'000'000; |
| 57 | constexpr uint64_t kBatteryRefreshUs = 1'000'000; | 59 | constexpr uint64_t kBatteryRefreshUs = 1'000'000; |
| 58 | constexpr int kDsEffectSize = 47; // the DualSense effects block SDL_SendGamepadEffect forwards (SDL's DS5EffectsState_t) | 60 | constexpr int kDsEffectSize = 47; // the DualSense effects block SDL_SendGamepadEffect forwards (SDL's DS5EffectsState_t) |
| 61 | constexpr uint64_t kHistoryUs = 500'000; // a reading stays in the history until the one after it is this old | ||
| 62 | constexpr size_t kHistoryCap = 512; // readings per pad, whatever the poll rate | ||
| 59 | 63 | ||
| 60 | uint64_t now_us() { | 64 | uint64_t now_us() { |
| 61 | static const LARGE_INTEGER freq = [] { LARGE_INTEGER f; QueryPerformanceFrequency(&f); return f; }(); | 65 | static const LARGE_INTEGER freq = [] { LARGE_INTEGER f; QueryPerformanceFrequency(&f); return f; }(); |
| @@ -133,7 +137,7 @@ struct Snapshot { | |||
| 133 | 137 | ||
| 134 | class Reading final : public IGameInputReading { | 138 | class Reading final : public IGameInputReading { |
| 135 | public: | 139 | public: |
| 136 | Reading(Device* device, const Snapshot& s, uint64_t seq, uint64_t ts); | 140 | Reading(Device* device, const Snapshot& s, uint64_t seq, uint64_t ts, uint64_t order); |
| 137 | ~Reading(); | 141 | ~Reading(); |
| 138 | 142 | ||
| 139 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { | 143 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { |
| @@ -196,13 +200,14 @@ public: | |||
| 196 | } | 200 | } |
| 197 | 201 | ||
| 198 | Device* device() const { return device_; } | 202 | Device* device() const { return device_; } |
| 199 | uint64_t seq() const { return seq_; } | 203 | uint64_t order() const { return order_; } |
| 200 | 204 | ||
| 201 | private: | 205 | private: |
| 202 | std::atomic<ULONG> refs_{1}; | 206 | std::atomic<ULONG> refs_{1}; |
| 203 | Device* device_; | 207 | Device* device_; |
| 204 | Snapshot s_; | 208 | Snapshot s_; |
| 205 | uint64_t seq_, ts_; | 209 | uint64_t seq_, ts_; |
| 210 | uint64_t order_; // place among every pad's readings, the order GetNextReading/GetPreviousReading follow | ||
| 206 | }; | 211 | }; |
| 207 | 212 | ||
| 208 | struct Rumble { | 213 | struct Rumble { |
| @@ -270,7 +275,9 @@ public: | |||
| 270 | GameInputGamepadInfo gamepad_info{}; | 275 | GameInputGamepadInfo gamepad_info{}; |
| 271 | GameInputUiNavigationInfo ui_info{}; | 276 | GameInputUiNavigationInfo ui_info{}; |
| 272 | std::atomic<uint32_t> status{GameInputDeviceNoStatus}; | 277 | std::atomic<uint32_t> status{GameInputDeviceNoStatus}; |
| 273 | Reading* latest = nullptr; // one reference held | 278 | std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading |
| 279 | uint64_t evicted_order = 0; // order of the newest reading dropped from history | ||
| 280 | Reading* latest() const { return history.empty() ? nullptr : history.back(); } | ||
| 274 | uint64_t seq = 0; | 281 | uint64_t seq = 0; |
| 275 | Rumble wanted; | 282 | Rumble wanted; |
| 276 | GameInputBatteryState battery{0, 0, 0, 0, GameInputBatteryUnknown}; | 283 | GameInputBatteryState battery{0, 0, 0, 0, GameInputBatteryUnknown}; |
| @@ -289,7 +296,8 @@ private: | |||
| 289 | std::atomic<ULONG> refs_{1}; | 296 | std::atomic<ULONG> refs_{1}; |
| 290 | }; | 297 | }; |
| 291 | 298 | ||
| 292 | Reading::Reading(Device* device, const Snapshot& s, uint64_t seq, uint64_t ts) : device_(device), s_(s), seq_(seq), ts_(ts) { | 299 | Reading::Reading(Device* device, const Snapshot& s, uint64_t seq, uint64_t ts, uint64_t order) |
| 300 | : device_(device), s_(s), seq_(seq), ts_(ts), order_(order) { | ||
| 293 | device_->AddRef(); | 301 | device_->AddRef(); |
| 294 | } | 302 | } |
| 295 | Reading::~Reading() { device_->Release(); } | 303 | Reading::~Reading() { device_->Release(); } |
| @@ -335,6 +343,7 @@ struct State { | |||
| 335 | std::vector<Callback> callbacks; | 343 | std::vector<Callback> callbacks; |
| 336 | std::vector<Event> queue; | 344 | std::vector<Event> queue; |
| 337 | GameInputCallbackToken next_token = 1; | 345 | GameInputCallbackToken next_token = 1; |
| 346 | uint64_t last_order = 0; // Reading::order of the newest reading | ||
| 338 | int dispatchers = 0; | 347 | int dispatchers = 0; |
| 339 | HANDLE wait_event = nullptr; // auto-reset, signalled when a dispatcher has work | 348 | HANDLE wait_event = nullptr; // auto-reset, signalled when a dispatcher has work |
| 340 | rt::gameinput::Config config; | 349 | rt::gameinput::Config config; |
| @@ -555,6 +564,20 @@ void fill_info(Device* d, SDL_Gamepad* p, const AppLocalDeviceId& id) { | |||
| 555 | i.displayName = &d->display_name; | 564 | i.displayName = &d->display_name; |
| 556 | } | 565 | } |
| 557 | 566 | ||
| 567 | // Makes snap the device's current reading. A reading leaves the history once the one after it is more than kHistoryUs | ||
| 568 | // old, so the last half second stays reachable however long the pad then idles; kHistoryCap bounds a flood. Readings | ||
| 569 | // the title holds outlive their place here (~Reading only releases the device, which takes no lock). Under g().mu. | ||
| 570 | Reading* push_reading(Device* d, const Snapshot& snap, uint64_t ts) { | ||
| 571 | std::deque<Reading*>& h = d->history; | ||
| 572 | h.push_back(new Reading(d, snap, ++d->seq, ts, ++g().last_order)); | ||
| 573 | while (h.size() > kHistoryCap || (h.size() > 1 && h[1]->GetTimestamp() + kHistoryUs < ts)) { | ||
| 574 | d->evicted_order = h.front()->order(); | ||
| 575 | h.front()->Release(); | ||
| 576 | h.pop_front(); | ||
| 577 | } | ||
| 578 | return h.back(); | ||
| 579 | } | ||
| 580 | |||
| 558 | void open_pad(SDL_JoystickID jid, uint64_t ts) { | 581 | void open_pad(SDL_JoystickID jid, uint64_t ts) { |
| 559 | State& s = g(); | 582 | State& s = g(); |
| 560 | SDL_Gamepad* p = SDL_OpenGamepad(jid); | 583 | SDL_Gamepad* p = SDL_OpenGamepad(jid); |
| @@ -609,8 +632,7 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) { | |||
| 609 | 632 | ||
| 610 | const Snapshot snap = read_pad(p); | 633 | const Snapshot snap = read_pad(p); |
| 611 | d->last_snap = snap; | 634 | d->last_snap = snap; |
| 612 | if (d->latest) d->latest->Release(); | 635 | push_reading(d, snap, ts); |
| 613 | d->latest = new Reading(d, snap, ++d->seq, ts); | ||
| 614 | queue_event({CbType::Device, d, nullptr, ts, status, GameInputDeviceNoStatus, false, 0}); | 636 | queue_event({CbType::Device, d, nullptr, ts, status, GameInputDeviceNoStatus, false, 0}); |
| 615 | std::fprintf(stderr, "[rt] gameinput: connected %s (%04x:%04x, player %d%s%s)\n", d->name.c_str(), d->info.vendorId, | 637 | std::fprintf(stderr, "[rt] gameinput: connected %s (%04x:%04x, player %d%s%s)\n", d->name.c_str(), d->info.vendorId, |
| 616 | d->info.productId, player + 1, d->is_dualsense ? ", DualSense trigger effects" : "", | 638 | d->info.productId, player + 1, d->is_dualsense ? ", DualSense trigger effects" : "", |
| @@ -624,6 +646,9 @@ void close_pad(Device* d, uint64_t ts) { | |||
| 624 | SDL_CloseGamepad(d->pad); | 646 | SDL_CloseGamepad(d->pad); |
| 625 | d->pad = nullptr; | 647 | d->pad = nullptr; |
| 626 | d->player = -1; | 648 | d->player = -1; |
| 649 | if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session | ||
| 650 | for (Reading* r : d->history) r->Release(); | ||
| 651 | d->history.clear(); | ||
| 627 | s.connected.erase(std::remove(s.connected.begin(), s.connected.end(), d), s.connected.end()); | 652 | s.connected.erase(std::remove(s.connected.begin(), s.connected.end(), d), s.connected.end()); |
| 628 | queue_event({CbType::Device, d, nullptr, ts, GameInputDeviceNoStatus, old, false, 0}); | 653 | queue_event({CbType::Device, d, nullptr, ts, GameInputDeviceNoStatus, old, false, 0}); |
| 629 | std::fprintf(stderr, "[rt] gameinput: disconnected %s\n", d->name.c_str()); | 654 | std::fprintf(stderr, "[rt] gameinput: disconnected %s\n", d->name.c_str()); |
| @@ -781,9 +806,7 @@ void poll_once() { | |||
| 781 | d->last_snap = snap; | 806 | d->last_snap = snap; |
| 782 | if (changed) { | 807 | if (changed) { |
| 783 | std::lock_guard lock(s.mu); | 808 | std::lock_guard lock(s.mu); |
| 784 | if (d->latest) d->latest->Release(); | 809 | queue_event({CbType::Reading, d, push_reading(d, snap, now), now, 0, 0, false, 0}); |
| 785 | d->latest = new Reading(d, snap, ++d->seq, now); | ||
| 786 | queue_event({CbType::Reading, d, d->latest, now, 0, 0, false, 0}); | ||
| 787 | } | 810 | } |
| 788 | if (guide_changed) { | 811 | if (guide_changed) { |
| 789 | std::lock_guard lock(s.mu); | 812 | std::lock_guard lock(s.mu); |
| @@ -896,6 +919,42 @@ private: | |||
| 896 | 919 | ||
| 897 | Device* as_device(IGameInputDevice* d) { return static_cast<Device*>(d); } | 920 | Device* as_device(IGameInputDevice* d) { return static_cast<Device*>(d); } |
| 898 | 921 | ||
| 922 | // GetNextReading (forward) and GetPreviousReading: the reading right after or right before the reference in the order | ||
| 923 | // readings were taken, from the filter device's history or, with no filter, every connected pad's. Readings compare by | ||
| 924 | // Reading::order, not timestamp, since pads polled together share one. The horizon is the newest reading dropped from | ||
| 925 | // the histories searched (with no filter the reference's pad counts too, even unplugged): a step that would cross it | ||
| 926 | // is REFERENCE_READING_TOO_OLD rather than a silent skip. | ||
| 927 | HRESULT find_reading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, IGameInputReading** out, | ||
| 928 | bool forward) { | ||
| 929 | if (!out) return E_POINTER; | ||
| 930 | *out = nullptr; | ||
| 931 | if (!ref) return E_INVALIDARG; | ||
| 932 | if (kind != GameInputKindUnknown && !(kind & kSupportedKinds)) return GAMEINPUT_E_READING_NOT_FOUND; | ||
| 933 | const Reading* r = static_cast<Reading*>(ref); | ||
| 934 | State& s = g(); | ||
| 935 | std::lock_guard lock(s.mu); | ||
| 936 | std::vector<Device*> scope = s.connected; | ||
| 937 | uint64_t horizon = 0; | ||
| 938 | if (device) { | ||
| 939 | if (as_device(device)->status == GameInputDeviceNoStatus) return GAMEINPUT_E_DEVICE_DISCONNECTED; | ||
| 940 | scope = {as_device(device)}; | ||
| 941 | } else { | ||
| 942 | horizon = r->device()->evicted_order; | ||
| 943 | } | ||
| 944 | for (Device* d : scope) horizon = std::max(horizon, d->evicted_order); | ||
| 945 | if (forward ? horizon > r->order() : horizon >= r->order()) return GAMEINPUT_E_REFERENCE_READING_TOO_OLD; | ||
| 946 | Reading* best = nullptr; | ||
| 947 | for (Device* d : scope) | ||
| 948 | for (Reading* c : d->history) { | ||
| 949 | const bool beyond = forward ? c->order() > r->order() : c->order() < r->order() && c->order() > horizon; | ||
| 950 | if (beyond && (!best || (c->order() < best->order()) == forward)) best = c; // the nearest one | ||
| 951 | } | ||
| 952 | if (!best) return GAMEINPUT_E_READING_NOT_FOUND; | ||
| 953 | best->AddRef(); | ||
| 954 | *out = best; | ||
| 955 | return S_OK; | ||
| 956 | } | ||
| 957 | |||
| 899 | class GameInput final : public IGameInput { | 958 | class GameInput final : public IGameInput { |
| 900 | public: | 959 | public: |
| 901 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { | 960 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** out) override { |
| @@ -922,10 +981,10 @@ public: | |||
| 922 | if (device) { | 981 | if (device) { |
| 923 | Device* d = as_device(device); | 982 | Device* d = as_device(device); |
| 924 | if (d->status == GameInputDeviceNoStatus) return GAMEINPUT_E_DEVICE_DISCONNECTED; | 983 | if (d->status == GameInputDeviceNoStatus) return GAMEINPUT_E_DEVICE_DISCONNECTED; |
| 925 | best = d->latest; | 984 | best = d->latest(); |
| 926 | } else { | 985 | } else { |
| 927 | for (Device* d : g().connected) // the most recent reading from any gamepad | 986 | for (Device* d : g().connected) // the most recent reading from any gamepad |
| 928 | if (d->latest && (!best || d->latest->GetTimestamp() >= best->GetTimestamp())) best = d->latest; | 987 | if (Reading* r = d->latest(); r && (!best || r->GetTimestamp() >= best->GetTimestamp())) best = r; |
| 929 | } | 988 | } |
| 930 | if (!best) return GAMEINPUT_E_READING_NOT_FOUND; | 989 | if (!best) return GAMEINPUT_E_READING_NOT_FOUND; |
| 931 | best->AddRef(); | 990 | best->AddRef(); |
| @@ -933,27 +992,13 @@ public: | |||
| 933 | return S_OK; | 992 | return S_OK; |
| 934 | } | 993 | } |
| 935 | 994 | ||
| 936 | // No history is kept: the next reading is the current one when it is newer than the reference, and there is no | ||
| 937 | // previous one. A title that walks GetNextReading in a loop sees each poll's state once. | ||
| 938 | HRESULT STDMETHODCALLTYPE GetNextReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, | 995 | HRESULT STDMETHODCALLTYPE GetNextReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, |
| 939 | IGameInputReading** out) override { | 996 | IGameInputReading** out) override { |
| 940 | if (!out) return E_POINTER; | 997 | return find_reading(ref, kind, device, out, true); |
| 941 | *out = nullptr; | ||
| 942 | if (!ref) return E_INVALIDARG; | ||
| 943 | Reading* r = static_cast<Reading*>(ref); | ||
| 944 | if (kind != GameInputKindUnknown && !(kind & kSupportedKinds)) return GAMEINPUT_E_READING_NOT_FOUND; | ||
| 945 | Device* d = device ? as_device(device) : r->device(); | ||
| 946 | std::lock_guard lock(g().mu); | ||
| 947 | if (d->status == GameInputDeviceNoStatus) return GAMEINPUT_E_DEVICE_DISCONNECTED; | ||
| 948 | if (!d->latest || d->latest->seq() <= r->seq() || d->latest->device() != r->device()) return GAMEINPUT_E_READING_NOT_FOUND; | ||
| 949 | d->latest->AddRef(); | ||
| 950 | *out = d->latest; | ||
| 951 | return S_OK; | ||
| 952 | } | 998 | } |
| 953 | HRESULT STDMETHODCALLTYPE GetPreviousReading(IGameInputReading*, uint32_t, IGameInputDevice*, IGameInputReading** out) override { | 999 | HRESULT STDMETHODCALLTYPE GetPreviousReading(IGameInputReading* ref, uint32_t kind, IGameInputDevice* device, |
| 954 | if (!out) return E_POINTER; | 1000 | IGameInputReading** out) override { |
| 955 | *out = nullptr; | 1001 | return find_reading(ref, kind, device, out, false); |
| 956 | return GAMEINPUT_E_READING_NOT_FOUND; | ||
| 957 | } | 1002 | } |
| 958 | HRESULT STDMETHODCALLTYPE GetTemporalReading(uint64_t, IGameInputDevice*, IGameInputReading** out) override { | 1003 | HRESULT STDMETHODCALLTYPE GetTemporalReading(uint64_t, IGameInputDevice*, IGameInputReading** out) override { |
| 959 | if (!out) return E_POINTER; | 1004 | if (!out) return E_POINTER; |