upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authornicolas <44988633+nschlaepfer@users.noreply.github.com>2026-11-21 21:38:20 -0700
committernicolas <44988633+nschlaepfer@users.noreply.github.com>2026-11-21 21:46:14 -0700
commitb72fc5a4fec776ffa4b19d37c7bf8d62836e3df5 (patch)
treea9352dc77152799d399cead60e7bd8271ab1d5cf /runtime
parent4a36bb19979b611f525d027f92e0fefad1626546 (diff)
Keep gamepad readings in order between title polls
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/gameinput.cpp105
-rw-r--r--runtime/test/gameinput_test.cpp171
2 files changed, 245 insertions, 31 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
56constexpr uint64_t kRumbleRefreshUs = 20'000'000; 58constexpr uint64_t kRumbleRefreshUs = 20'000'000;
57constexpr uint64_t kBatteryRefreshUs = 1'000'000; 59constexpr uint64_t kBatteryRefreshUs = 1'000'000;
58constexpr int kDsEffectSize = 47; // the DualSense effects block SDL_SendGamepadEffect forwards (SDL's DS5EffectsState_t) 60constexpr int kDsEffectSize = 47; // the DualSense effects block SDL_SendGamepadEffect forwards (SDL's DS5EffectsState_t)
61constexpr uint64_t kHistoryUs = 500'000; // a reading stays in the history until the one after it is this old
62constexpr size_t kHistoryCap = 512; // readings per pad, whatever the poll rate
59 63
60uint64_t now_us() { 64uint64_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
134class Reading final : public IGameInputReading { 138class Reading final : public IGameInputReading {
135public: 139public:
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
201private: 205private:
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
208struct Rumble { 213struct 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
292Reading::Reading(Device* device, const Snapshot& s, uint64_t seq, uint64_t ts) : device_(device), s_(s), seq_(seq), ts_(ts) { 299Reading::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}
295Reading::~Reading() { device_->Release(); } 303Reading::~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.
570Reading* 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
558void open_pad(SDL_JoystickID jid, uint64_t ts) { 581void 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
897Device* as_device(IGameInputDevice* d) { return static_cast<Device*>(d); } 920Device* 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.
927HRESULT 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
899class GameInput final : public IGameInput { 958class GameInput final : public IGameInput {
900public: 959public:
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;
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 1a154a7a9..671dbbbe5 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -5,7 +5,8 @@
5// (Sony vendor/product id, so SDL treats it as one and the shim takes the trigger-effect path) and a plain pad 5// (Sony vendor/product id, so SDL treats it as one and the shim takes the trigger-effect path) and a plain pad
6// without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping, 6// without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping,
7// UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for 7// UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for
8// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, and unplugging and replugging. No game data. 8// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, unplugging and replugging, and the reading history behind
9// GetNextReading/GetPreviousReading. No game data.
9#include <windows.h> 10#include <windows.h>
10 11
11#include <SDL3/SDL.h> 12#include <SDL3/SDL.h>
@@ -132,6 +133,46 @@ void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_
132 133
133void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); } 134void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); }
134 135
136uint32_t buttons_of(IGameInputReading* r) {
137 GameInputGamepadState s{};
138 return r && r->GetGamepadState(&s) ? s.buttons : ~0u;
139}
140
141IGameInputDevice* device_of(IGameInputReading* r) { // devices live for the process: no reference is kept
142 IGameInputDevice* d = nullptr;
143 if (r) r->GetDevice(&d);
144 if (d) d->Release();
145 return d;
146}
147
148IGameInputReading* current(IGameInput* input, IGameInputDevice* device) {
149 IGameInputReading* r = nullptr;
150 input->GetCurrentReading(GameInputKindGamepad, device, &r);
151 return r;
152}
153
154// Steps GetNextReading (forward) or GetPreviousReading from ref until a step fails or limit readings are collected.
155// The readings are referenced; *last is the final result.
156std::vector<IGameInputReading*> walk(IGameInput* input, IGameInputReading* ref, IGameInputDevice* device, bool forward,
157 size_t limit, HRESULT* last = nullptr) {
158 std::vector<IGameInputReading*> out;
159 HRESULT hr = E_INVALIDARG;
160 for (IGameInputReading* at = ref; at && out.size() < limit; at = out.back()) {
161 IGameInputReading* r = nullptr;
162 hr = forward ? input->GetNextReading(at, GameInputKindGamepad, device, &r)
163 : input->GetPreviousReading(at, GameInputKindGamepad, device, &r);
164 if (hr != S_OK) break;
165 out.push_back(r);
166 }
167 if (last) *last = hr;
168 return out;
169}
170
171void release_all(std::vector<IGameInputReading*>& v) {
172 for (IGameInputReading* r : v) r->Release();
173 v.clear();
174}
175
135} // namespace 176} // namespace
136 177
137int main() { 178int main() {
@@ -445,6 +486,134 @@ int main() {
445 nullptr, on_device, &blocking_token); 486 nullptr, on_device, &blocking_token);
446 check(g_device_events.size() == before.size() + 2, "blocking enumeration reports both pads before returning"); 487 check(g_device_events.size() == before.size() + 2, "blocking enumeration reports both pads before returning");
447 488
489 // ---- reading history: GetNextReading and GetPreviousReading ----
490 // A tap of A on the Xbox pad, pressed and released before the title reads again.
491 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, false);
492 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, false);
493 rt::gameinput::poll();
494 IGameInputReading* idle = current(input, dev);
495 check(buttons_of(idle) == 0, "history: the reference reading is at rest");
496 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, true);
497 rt::gameinput::poll();
498 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, false);
499 rt::gameinput::poll();
500 HRESULT hr = S_OK;
501 std::vector<IGameInputReading*> seen = walk(input, idle, nullptr, true, 8, &hr);
502 check(seen.size() == 2 && buttons_of(seen[0]) == GameInputGamepadA && buttons_of(seen[1]) == 0 &&
503 hr == GAMEINPUT_E_READING_NOT_FOUND,
504 "history: GetNextReading gives a tap's press, its release, then READING_NOT_FOUND");
505 std::vector<IGameInputReading*> again = walk(input, seen.empty() ? nullptr : seen.back(), nullptr, false, 2);
506 check(again.size() == 2 && again[0] == seen[0] && again[1] == idle,
507 "history: GetPreviousReading walks back over the press to the reference");
508 release_all(again);
509 release_all(seen);
510 IGameInputReading* hit = nullptr;
511 check(input->GetNextReading(idle, GameInputKindGamepad | GameInputKindKeyboard, dev, &hit) == S_OK &&
512 buttons_of(hit) == GameInputGamepadA,
513 "history: a kind mask that includes the gamepad matches");
514 if (hit) hit->Release();
515 check(input->GetNextReading(idle, GameInputKindKeyboard, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
516 "history: keyboard alone finds no reading");
517 check(input->GetPreviousReading(idle, GameInputKindGamepad, dev, nullptr) == E_POINTER &&
518 input->GetPreviousReading(nullptr, GameInputKindGamepad, dev, &hit) == E_INVALIDARG && !hit,
519 "history: a null output is E_POINTER, a null reference E_INVALIDARG");
520
521 // Two pads in one stream, one poll each: A on the Xbox pad, B on the DualSense, Y on the Xbox pad.
522 IGameInputReading* start = current(input, dev);
523 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, true);
524 rt::gameinput::poll();
525 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, true);
526 rt::gameinput::poll();
527 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_NORTH, true);
528 rt::gameinput::poll();
529 seen = walk(input, start, nullptr, true, 8, &hr);
530 const bool interleaved = seen.size() == 3 && device_of(seen[0]) == dev && device_of(seen[1]) == dsdev &&
531 device_of(seen[2]) == dev && buttons_of(seen[1]) == GameInputGamepadB;
532 check(interleaved && hr == GAMEINPUT_E_READING_NOT_FOUND,
533 "history: with no device filter, both pads' readings come in the order they were taken");
534 if (interleaved) {
535 again = walk(input, seen[2], nullptr, false, 3);
536 check(again.size() == 3 && again[0] == seen[1] && again[1] == seen[0] && again[2] == start,
537 "history: GetPreviousReading gives the exact reverse");
538 release_all(again);
539 again = walk(input, start, dev, true, 8);
540 check(again.size() == 2 && again[0] == seen[0] && again[1] == seen[2], "history: a device filter skips the other pad");
541 release_all(again);
542 check(input->GetNextReading(seen[0], GameInputKindGamepad, dsdev, &hit) == S_OK && hit == seen[1],
543 "history: filtering on the other pad gives its next reading after the reference");
544 if (hit) hit->Release();
545 check(input->GetNextReading(seen[1], GameInputKindGamepad, dsdev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
546 "history: and never one of the reference's pad");
547 }
548 release_all(seen);
549 if (start) start->Release();
550
551 // Both pads change in one poll and their readings share a timestamp: each still comes once, in both directions.
552 IGameInputReading* pair_ref = current(input, dev);
553 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_NORTH, false);
554 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, false);
555 rt::gameinput::poll();
556 seen = walk(input, pair_ref, nullptr, true, 8, &hr);
557 const bool paired = seen.size() == 2 && seen[0]->GetTimestamp() == seen[1]->GetTimestamp() &&
558 device_of(seen[0]) != device_of(seen[1]);
559 check(paired && hr == GAMEINPUT_E_READING_NOT_FOUND, "history: two readings with one timestamp both come, once each");
560 if (paired) {
561 again = walk(input, seen[1], nullptr, false, 2);
562 check(again.size() == 2 && again[0] == seen[0] && again[1] == pair_ref, "history: and in reverse");
563 release_all(again);
564 }
565 release_all(seen);
566 if (pair_ref) pair_ref->Release();
567
568 // Ageing: a reading leaves the history once the reading after it is more than half a second old.
569 IGameInputReading* aged = current(input, dev);
570 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, true);
571 rt::gameinput::poll();
572 Sleep(600);
573 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, false);
574 rt::gameinput::poll();
575 const uint32_t a_ls = GameInputGamepadA | GameInputGamepadLeftShoulder;
576 check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == S_OK && buttons_of(hit) == a_ls,
577 "history: a reference that has aged out still leads to its successor while that is kept");
578 if (hit) hit->Release();
579 check(input->GetPreviousReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit,
580 "history: GetPreviousReading on an aged-out reference is REFERENCE_READING_TOO_OLD");
581 Sleep(600);
582 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, true);
583 rt::gameinput::poll();
584 check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit &&
585 input->GetNextReading(aged, GameInputKindGamepad, nullptr, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit,
586 "history: once a reading after the reference is dropped, GetNextReading is REFERENCE_READING_TOO_OLD");
587 check(buttons_of(aged) == GameInputGamepadA && device_of(aged) == dev,
588 "history: a reading the title holds stays readable after it leaves the history");
589 IGameInputReading* fresh = current(input, dev);
590 check(fresh && input->GetNextReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
591 "history: after GetCurrentReading the title is caught up");
592
593 // Unplugging drops the pad's history; plugged back in, its last reading leads to the reconnect snapshot.
594 const uint64_t last_seq = fresh ? fresh->GetSequenceNumber(GameInputKindGamepad) : 0;
595 detach(xbox2);
596 rt::gameinput::poll();
597 check(input->GetNextReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_DEVICE_DISCONNECTED && !hit &&
598 input->GetPreviousReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_DEVICE_DISCONNECTED && !hit,
599 "history: filtering on an unplugged pad is DEVICE_DISCONNECTED");
600 check(input->GetNextReading(fresh, GameInputKindGamepad, nullptr, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
601 "history: with no filter, nothing follows an unplugged pad's last reading");
602 check(buttons_of(fresh) == a_ls && device_of(fresh) == dev, "history: a held reading stays readable after unplugging");
603 attach(xbox2, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
604 rt::gameinput::poll();
605 IGameInputReading* reconnect = nullptr;
606 check(input->GetNextReading(fresh, GameInputKindGamepad, dev, &reconnect) == S_OK && device_of(reconnect) == dev &&
607 buttons_of(reconnect) == 0 && reconnect->GetSequenceNumber(GameInputKindGamepad) > last_seq,
608 "history: after replugging, the last reading before the unplug leads to the reconnect snapshot");
609 check(reconnect && input->GetPreviousReading(reconnect, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit,
610 "history: nothing comes before the reconnect snapshot");
611 check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit,
612 "history: an older reading from before the unplug is REFERENCE_READING_TOO_OLD");
613 if (reconnect) reconnect->Release();
614 check(idle && aged && fresh && idle->Release() == 0 && aged->Release() == 0 && fresh->Release() == 0,
615 "history: dropped readings are freed once the title releases them");
616
448 if (reading) reading->Release(); 617 if (reading) reading->Release();
449 dev->Release(); 618 dev->Release();
450 detach(xbox2); 619 detach(xbox2);