From b72fc5a4fec776ffa4b19d37c7bf8d62836e3df5 Mon Sep 17 00:00:00 2001 From: nicolas <44988633+nschlaepfer@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:38:20 -0700 Subject: Keep gamepad readings in order between title polls --- runtime/test/gameinput_test.cpp | 171 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 170 insertions(+), 1 deletion(-) (limited to 'runtime/test/gameinput_test.cpp') 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 @@ // (Sony vendor/product id, so SDL treats it as one and the shim takes the trigger-effect path) and a plain pad // without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping, // UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for -// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, and unplugging and replugging. No game data. +// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, unplugging and replugging, and the reading history behind +// GetNextReading/GetPreviousReading. No game data. #include #include @@ -132,6 +133,46 @@ void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_ void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); } +uint32_t buttons_of(IGameInputReading* r) { + GameInputGamepadState s{}; + return r && r->GetGamepadState(&s) ? s.buttons : ~0u; +} + +IGameInputDevice* device_of(IGameInputReading* r) { // devices live for the process: no reference is kept + IGameInputDevice* d = nullptr; + if (r) r->GetDevice(&d); + if (d) d->Release(); + return d; +} + +IGameInputReading* current(IGameInput* input, IGameInputDevice* device) { + IGameInputReading* r = nullptr; + input->GetCurrentReading(GameInputKindGamepad, device, &r); + return r; +} + +// Steps GetNextReading (forward) or GetPreviousReading from ref until a step fails or limit readings are collected. +// The readings are referenced; *last is the final result. +std::vector walk(IGameInput* input, IGameInputReading* ref, IGameInputDevice* device, bool forward, + size_t limit, HRESULT* last = nullptr) { + std::vector out; + HRESULT hr = E_INVALIDARG; + for (IGameInputReading* at = ref; at && out.size() < limit; at = out.back()) { + IGameInputReading* r = nullptr; + hr = forward ? input->GetNextReading(at, GameInputKindGamepad, device, &r) + : input->GetPreviousReading(at, GameInputKindGamepad, device, &r); + if (hr != S_OK) break; + out.push_back(r); + } + if (last) *last = hr; + return out; +} + +void release_all(std::vector& v) { + for (IGameInputReading* r : v) r->Release(); + v.clear(); +} + } // namespace int main() { @@ -445,6 +486,134 @@ int main() { nullptr, on_device, &blocking_token); check(g_device_events.size() == before.size() + 2, "blocking enumeration reports both pads before returning"); + // ---- reading history: GetNextReading and GetPreviousReading ---- + // A tap of A on the Xbox pad, pressed and released before the title reads again. + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, false); + SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, false); + rt::gameinput::poll(); + IGameInputReading* idle = current(input, dev); + check(buttons_of(idle) == 0, "history: the reference reading is at rest"); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, true); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, false); + rt::gameinput::poll(); + HRESULT hr = S_OK; + std::vector seen = walk(input, idle, nullptr, true, 8, &hr); + check(seen.size() == 2 && buttons_of(seen[0]) == GameInputGamepadA && buttons_of(seen[1]) == 0 && + hr == GAMEINPUT_E_READING_NOT_FOUND, + "history: GetNextReading gives a tap's press, its release, then READING_NOT_FOUND"); + std::vector again = walk(input, seen.empty() ? nullptr : seen.back(), nullptr, false, 2); + check(again.size() == 2 && again[0] == seen[0] && again[1] == idle, + "history: GetPreviousReading walks back over the press to the reference"); + release_all(again); + release_all(seen); + IGameInputReading* hit = nullptr; + check(input->GetNextReading(idle, GameInputKindGamepad | GameInputKindKeyboard, dev, &hit) == S_OK && + buttons_of(hit) == GameInputGamepadA, + "history: a kind mask that includes the gamepad matches"); + if (hit) hit->Release(); + check(input->GetNextReading(idle, GameInputKindKeyboard, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit, + "history: keyboard alone finds no reading"); + check(input->GetPreviousReading(idle, GameInputKindGamepad, dev, nullptr) == E_POINTER && + input->GetPreviousReading(nullptr, GameInputKindGamepad, dev, &hit) == E_INVALIDARG && !hit, + "history: a null output is E_POINTER, a null reference E_INVALIDARG"); + + // Two pads in one stream, one poll each: A on the Xbox pad, B on the DualSense, Y on the Xbox pad. + IGameInputReading* start = current(input, dev); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_SOUTH, true); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, true); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_NORTH, true); + rt::gameinput::poll(); + seen = walk(input, start, nullptr, true, 8, &hr); + const bool interleaved = seen.size() == 3 && device_of(seen[0]) == dev && device_of(seen[1]) == dsdev && + device_of(seen[2]) == dev && buttons_of(seen[1]) == GameInputGamepadB; + check(interleaved && hr == GAMEINPUT_E_READING_NOT_FOUND, + "history: with no device filter, both pads' readings come in the order they were taken"); + if (interleaved) { + again = walk(input, seen[2], nullptr, false, 3); + check(again.size() == 3 && again[0] == seen[1] && again[1] == seen[0] && again[2] == start, + "history: GetPreviousReading gives the exact reverse"); + release_all(again); + again = walk(input, start, dev, true, 8); + check(again.size() == 2 && again[0] == seen[0] && again[1] == seen[2], "history: a device filter skips the other pad"); + release_all(again); + check(input->GetNextReading(seen[0], GameInputKindGamepad, dsdev, &hit) == S_OK && hit == seen[1], + "history: filtering on the other pad gives its next reading after the reference"); + if (hit) hit->Release(); + check(input->GetNextReading(seen[1], GameInputKindGamepad, dsdev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit, + "history: and never one of the reference's pad"); + } + release_all(seen); + if (start) start->Release(); + + // Both pads change in one poll and their readings share a timestamp: each still comes once, in both directions. + IGameInputReading* pair_ref = current(input, dev); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_NORTH, false); + SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, false); + rt::gameinput::poll(); + seen = walk(input, pair_ref, nullptr, true, 8, &hr); + const bool paired = seen.size() == 2 && seen[0]->GetTimestamp() == seen[1]->GetTimestamp() && + device_of(seen[0]) != device_of(seen[1]); + check(paired && hr == GAMEINPUT_E_READING_NOT_FOUND, "history: two readings with one timestamp both come, once each"); + if (paired) { + again = walk(input, seen[1], nullptr, false, 2); + check(again.size() == 2 && again[0] == seen[0] && again[1] == pair_ref, "history: and in reverse"); + release_all(again); + } + release_all(seen); + if (pair_ref) pair_ref->Release(); + + // Ageing: a reading leaves the history once the reading after it is more than half a second old. + IGameInputReading* aged = current(input, dev); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, true); + rt::gameinput::poll(); + Sleep(600); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, false); + rt::gameinput::poll(); + const uint32_t a_ls = GameInputGamepadA | GameInputGamepadLeftShoulder; + check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == S_OK && buttons_of(hit) == a_ls, + "history: a reference that has aged out still leads to its successor while that is kept"); + if (hit) hit->Release(); + check(input->GetPreviousReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit, + "history: GetPreviousReading on an aged-out reference is REFERENCE_READING_TOO_OLD"); + Sleep(600); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, true); + rt::gameinput::poll(); + check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit && + input->GetNextReading(aged, GameInputKindGamepad, nullptr, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit, + "history: once a reading after the reference is dropped, GetNextReading is REFERENCE_READING_TOO_OLD"); + check(buttons_of(aged) == GameInputGamepadA && device_of(aged) == dev, + "history: a reading the title holds stays readable after it leaves the history"); + IGameInputReading* fresh = current(input, dev); + check(fresh && input->GetNextReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit, + "history: after GetCurrentReading the title is caught up"); + + // Unplugging drops the pad's history; plugged back in, its last reading leads to the reconnect snapshot. + const uint64_t last_seq = fresh ? fresh->GetSequenceNumber(GameInputKindGamepad) : 0; + detach(xbox2); + rt::gameinput::poll(); + check(input->GetNextReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_DEVICE_DISCONNECTED && !hit && + input->GetPreviousReading(fresh, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_DEVICE_DISCONNECTED && !hit, + "history: filtering on an unplugged pad is DEVICE_DISCONNECTED"); + check(input->GetNextReading(fresh, GameInputKindGamepad, nullptr, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit, + "history: with no filter, nothing follows an unplugged pad's last reading"); + check(buttons_of(fresh) == a_ls && device_of(fresh) == dev, "history: a held reading stays readable after unplugging"); + attach(xbox2, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true); + rt::gameinput::poll(); + IGameInputReading* reconnect = nullptr; + check(input->GetNextReading(fresh, GameInputKindGamepad, dev, &reconnect) == S_OK && device_of(reconnect) == dev && + buttons_of(reconnect) == 0 && reconnect->GetSequenceNumber(GameInputKindGamepad) > last_seq, + "history: after replugging, the last reading before the unplug leads to the reconnect snapshot"); + check(reconnect && input->GetPreviousReading(reconnect, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_READING_NOT_FOUND && !hit, + "history: nothing comes before the reconnect snapshot"); + check(input->GetNextReading(aged, GameInputKindGamepad, dev, &hit) == GAMEINPUT_E_REFERENCE_READING_TOO_OLD && !hit, + "history: an older reading from before the unplug is REFERENCE_READING_TOO_OLD"); + if (reconnect) reconnect->Release(); + check(idle && aged && fresh && idle->Release() == 0 && aged->Release() == 0 && fresh->Release() == 0, + "history: dropped readings are freed once the title releases them"); + if (reading) reading->Release(); dev->Release(); detach(xbox2); -- cgit v1.2.3