From 0e5e33bc497dee9b01963576aecc3cc49bf28a13 Mon Sep 17 00:00:00 2001 From: DadaCap <167002618+DadaCap@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:10:21 +0100 Subject: [PATCH 1/2] gameinput: report a missed press once to a slow reader (RT-GAMEINPUT-SLOW-READER) A title that reads a pad with GetCurrentReading once a frame, at a few frames a second, never sees a press that came and went between two reads. A pad now keeps the newest reading that pressed a button the last read did not report; a read more than kSlowReaderUs (50 ms) after the previous one answers that press once, and the next read the pad's real state. Carried from DadaCap's boot-prototype fa497b930, split to the GameInput part (runtime/shims/gameinput.cpp and its test in runtime/test/gameinput_test.cpp); the other shims in that commit are not included. Applied unchanged. AI-assisted. Run on Windows 11, RTX 5080. No game data. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf --- runtime/shims/gameinput.cpp | 32 ++++++++++++++++++++++++++++++++ runtime/test/gameinput_test.cpp | 16 ++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index db685e71f5..382cba1459 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -81,6 +81,7 @@ constexpr size_t kHistoryCap = 512; // readings per pad, whatever the poll constexpr float kMaxAcceleration = 19.6133f; // 2 g in m/s^2 constexpr float kMaxAngularVelocity = 34.9066f; // 2000 deg/s in rad/s constexpr float kMotionSensorRate = 250.0f; // requested sample rate; the device clamps to what it has +constexpr uint64_t kSlowReaderUs = 50'000; // a title reading a pad less often than this gets presses it would miss constexpr uint64_t kMotionReadingUs = 8'000; // a motion pad streams readings at ~125 Hz so the history stays sane uint64_t now_us() { @@ -523,6 +524,9 @@ class Device final : public IGameInputDevice { std::atomic status{GameInputDeviceNoStatus}; std::deque history; // one reference each, oldest first; the newest is the current reading uint64_t evicted_order = 0; // order of the newest reading dropped from history + Reading* missed = nullptr; // one reference: a press since the title's last GetCurrentReading here + uint32_t read_buttons = 0; // the buttons that call answered, and when + uint64_t read_us = 0; Reading* latest() const { return history.empty() ? nullptr : history.back(); } uint64_t seq = 0; Rumble wanted; @@ -1254,6 +1258,11 @@ Reading* push_reading(Device* d, const Snapshot& snap, uint64_t ts) { std::deque& h = d->history; d->last_reading_us = ts; h.push_back(new Reading(d, snap, ++d->seq, ts, ++g().last_order)); + if (d->read_us && (snap.gamepad.buttons & ~d->read_buttons)) { + if (d->missed) d->missed->Release(); + d->missed = h.back(); + d->missed->AddRef(); + } while (h.size() > kHistoryCap || (h.size() > 1 && h[1]->GetTimestamp() + kHistoryUs < ts)) { d->evicted_order = h.front()->order(); h.front()->Release(); @@ -1514,6 +1523,10 @@ void close_pad(Device* d, uint64_t ts) { if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session for (Reading* r : d->history) r->Release(); d->history.clear(); + if (d->missed) d->missed->Release(); + d->missed = nullptr; + d->read_buttons = 0; + d->read_us = 0; s.connected.erase(std::remove(s.connected.begin(), s.connected.end(), d), s.connected.end()); queue_event({CbType::Device, d, nullptr, ts, GameInputDeviceNoStatus, old, false, 0}); std::fprintf(stderr, "[rt] gameinput: disconnected %s\n", d->name.c_str()); @@ -2509,6 +2522,25 @@ class GameInput final : public IGameInput { if (kind != GameInputKindUnknown && !(kind & d->info.supportedInput)) return GAMEINPUT_E_READING_NOT_FOUND; // the device does not do this kind (motion on a plain pad) best = d->latest(); + // A title that reads a pad once a frame, at a few frames a second, would never see a press that came and + // went between two reads: it gets the press once, the pad's real state on the next read. + GameInputGamepadState now_state{}, press{}; + if (best) best->GetGamepadState(&now_state); + const uint64_t now = now_us(); + if (best && d->missed && now - d->read_us > kSlowReaderUs && d->missed->GetGamepadState(&press) && + (press.buttons & ~now_state.buttons)) { + best = d->missed; + now_state = press; + } + d->read_buttons = now_state.buttons; + d->read_us = now; + if (best) { + best->AddRef(); + *out = best; + } + if (d->missed) d->missed->Release(); + d->missed = nullptr; + return *out ? S_OK : GAMEINPUT_E_READING_NOT_FOUND; } } else { for (Device* d : g().connected) // the most recent reading from any gamepad diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index b6658f51cd..bb63e2df90 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -2229,6 +2229,22 @@ int main(int argc, char** argv) { release_all(seen); if (pair_ref) pair_ref->Release(); + // A slow reader: a press that came and went between two reads of a pad is answered once, then the real state. + if (IGameInputReading* r = current(input, dev)) r->Release(); + Sleep(80); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, true); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, false); + rt::gameinput::poll(); + { + IGameInputReading* first = current(input, dev); + IGameInputReading* second = current(input, dev); + check(first && second && (buttons_of(first) & GameInputGamepadX) && !(buttons_of(second) & GameInputGamepadX), + "a slow reader sees a missed press once"); + if (first) first->Release(); + if (second) second->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); From 045bb0fcec9ea44105ae118f8d958dc76fa1b4af Mon Sep 17 00:00:00 2001 From: QemmHD <290605352+QemmHD@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:40:06 -0500 Subject: [PATCH 2/2] gameinput_test: close_pad resets a missed press, pads keep their own, wider slow-reader margin Review follow-up for the kSlowReaderUs missed-press logic: - close_pad: a press missed by a slow reader, then an unplug/replug, must not come back on the first slow read after reconnecting. - two pads: a missed press on the Xbox pad and one on the DualSense are each reported only on their own pad, once. - the slow-reader sleeps are now 150 ms (3x kSlowReaderUs) for slow CI. Tested: ctest -R ^gameinput passes plain and with LEONIDA_VK_VALIDATION=1 (VS 2022, Ninja). Against the pre-fix shim the slow-reader and both two-pad checks fail (the close_pad check passes trivially there); with the fix minus close_pad's missed reset, the close_pad check fails. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf --- runtime/test/gameinput_test.cpp | 51 ++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index bb63e2df90..d34f0b7ceb 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -2230,8 +2230,9 @@ int main(int argc, char** argv) { if (pair_ref) pair_ref->Release(); // A slow reader: a press that came and went between two reads of a pad is answered once, then the real state. + // Sleeps are 150 ms, three times kSlowReaderUs (50 ms), so a loaded CI machine cannot make the reader look fast. if (IGameInputReading* r = current(input, dev)) r->Release(); - Sleep(80); + Sleep(150); SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, true); rt::gameinput::poll(); SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, false); @@ -2245,6 +2246,35 @@ int main(int argc, char** argv) { if (second) second->Release(); } + // Two slow-read pads: each one's missed press comes back on its own pad only. + if (IGameInputReading* r = current(input, dev)) r->Release(); + if (IGameInputReading* r = current(input, dsdev)) r->Release(); + Sleep(150); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, true); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, false); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_NORTH, true); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_NORTH, false); + rt::gameinput::poll(); + { + IGameInputReading* ds_first = current(input, dsdev); + IGameInputReading* x_first = current(input, dev); + IGameInputReading* ds_second = current(input, dsdev); + IGameInputReading* x_second = current(input, dev); + check(ds_first && device_of(ds_first) == dsdev && (buttons_of(ds_first) & GameInputGamepadY) && + !(buttons_of(ds_first) & GameInputGamepadX), + "two slow-read pads: the DualSense gets its own missed press, not the Xbox pad's"); + check(x_first && device_of(x_first) == dev && (buttons_of(x_first) & GameInputGamepadX) && + !(buttons_of(x_first) & GameInputGamepadY), + "two slow-read pads: the Xbox pad gets its own missed press, not the DualSense's"); + check(ds_second && x_second && !(buttons_of(ds_second) & GameInputGamepadY) && !(buttons_of(x_second) & GameInputGamepadX), + "two slow-read pads: each missed press comes once"); + for (IGameInputReading* r : {ds_first, x_first, ds_second, x_second}) + if (r) r->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); @@ -2294,6 +2324,25 @@ int main(int argc, char** argv) { check(idle && aged && fresh && idle->Release() == 0 && aged->Release() == 0 && fresh->Release() == 0, "history: dropped readings are freed once the title releases them"); + // Unplugging drops a pending missed press: the first slow read after replugging is the pad's real state. + if (IGameInputReading* r = current(input, dev)) r->Release(); + Sleep(150); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, true); + rt::gameinput::poll(); + SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, false); + rt::gameinput::poll(); + detach(xbox2); + rt::gameinput::poll(); + attach(xbox2, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true); + rt::gameinput::poll(); + Sleep(150); + { + IGameInputReading* after = current(input, dev); + check(after && device_of(after) == dev && !(buttons_of(after) & GameInputGamepadX), + "close_pad drops a pending missed press: no stale press after replugging"); + if (after) after->Release(); + } + if (reading) reading->Release(); dev->Release(); detach(xbox2);