diff options
| author | DadaCap <167002618+DadaCap@users.noreply.github.com> | 2026-11-24 16:10:21 +0100 |
|---|---|---|
| committer | QemmHD <290605352+QemmHD@users.noreply.github.com> | 2026-11-24 21:45:32 -0500 |
| commit | 0e5e33bc497dee9b01963576aecc3cc49bf28a13 (patch) | |
| tree | 7bbfb1f9b79bc2700632a8c72602f41eced2342c /runtime/shims/gameinput.cpp | |
| parent | 2213d5cb8f68a874e9f885eaf799b6f7fb7192a1 (diff) | |
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index db685e71f..382cba145 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 | |||
| 81 | constexpr float kMaxAcceleration = 19.6133f; // 2 g in m/s^2 | 81 | constexpr float kMaxAcceleration = 19.6133f; // 2 g in m/s^2 |
| 82 | constexpr float kMaxAngularVelocity = 34.9066f; // 2000 deg/s in rad/s | 82 | constexpr float kMaxAngularVelocity = 34.9066f; // 2000 deg/s in rad/s |
| 83 | constexpr float kMotionSensorRate = 250.0f; // requested sample rate; the device clamps to what it has | 83 | constexpr float kMotionSensorRate = 250.0f; // requested sample rate; the device clamps to what it has |
| 84 | constexpr uint64_t kSlowReaderUs = 50'000; // a title reading a pad less often than this gets presses it would miss | ||
| 84 | constexpr uint64_t kMotionReadingUs = 8'000; // a motion pad streams readings at ~125 Hz so the history stays sane | 85 | constexpr uint64_t kMotionReadingUs = 8'000; // a motion pad streams readings at ~125 Hz so the history stays sane |
| 85 | 86 | ||
| 86 | uint64_t now_us() { | 87 | uint64_t now_us() { |
| @@ -523,6 +524,9 @@ public: | |||
| 523 | std::atomic<uint32_t> status{GameInputDeviceNoStatus}; | 524 | std::atomic<uint32_t> status{GameInputDeviceNoStatus}; |
| 524 | std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading | 525 | std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading |
| 525 | uint64_t evicted_order = 0; // order of the newest reading dropped from history | 526 | uint64_t evicted_order = 0; // order of the newest reading dropped from history |
| 527 | Reading* missed = nullptr; // one reference: a press since the title's last GetCurrentReading here | ||
| 528 | uint32_t read_buttons = 0; // the buttons that call answered, and when | ||
| 529 | uint64_t read_us = 0; | ||
| 526 | Reading* latest() const { return history.empty() ? nullptr : history.back(); } | 530 | Reading* latest() const { return history.empty() ? nullptr : history.back(); } |
| 527 | uint64_t seq = 0; | 531 | uint64_t seq = 0; |
| 528 | Rumble wanted; | 532 | Rumble wanted; |
| @@ -1254,6 +1258,11 @@ Reading* push_reading(Device* d, const Snapshot& snap, uint64_t ts) { | |||
| 1254 | std::deque<Reading*>& h = d->history; | 1258 | std::deque<Reading*>& h = d->history; |
| 1255 | d->last_reading_us = ts; | 1259 | d->last_reading_us = ts; |
| 1256 | h.push_back(new Reading(d, snap, ++d->seq, ts, ++g().last_order)); | 1260 | h.push_back(new Reading(d, snap, ++d->seq, ts, ++g().last_order)); |
| 1261 | if (d->read_us && (snap.gamepad.buttons & ~d->read_buttons)) { | ||
| 1262 | if (d->missed) d->missed->Release(); | ||
| 1263 | d->missed = h.back(); | ||
| 1264 | d->missed->AddRef(); | ||
| 1265 | } | ||
| 1257 | while (h.size() > kHistoryCap || (h.size() > 1 && h[1]->GetTimestamp() + kHistoryUs < ts)) { | 1266 | while (h.size() > kHistoryCap || (h.size() > 1 && h[1]->GetTimestamp() + kHistoryUs < ts)) { |
| 1258 | d->evicted_order = h.front()->order(); | 1267 | d->evicted_order = h.front()->order(); |
| 1259 | h.front()->Release(); | 1268 | h.front()->Release(); |
| @@ -1514,6 +1523,10 @@ void close_pad(Device* d, uint64_t ts) { | |||
| 1514 | if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session | 1523 | if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session |
| 1515 | for (Reading* r : d->history) r->Release(); | 1524 | for (Reading* r : d->history) r->Release(); |
| 1516 | d->history.clear(); | 1525 | d->history.clear(); |
| 1526 | if (d->missed) d->missed->Release(); | ||
| 1527 | d->missed = nullptr; | ||
| 1528 | d->read_buttons = 0; | ||
| 1529 | d->read_us = 0; | ||
| 1517 | s.connected.erase(std::remove(s.connected.begin(), s.connected.end(), d), s.connected.end()); | 1530 | s.connected.erase(std::remove(s.connected.begin(), s.connected.end(), d), s.connected.end()); |
| 1518 | queue_event({CbType::Device, d, nullptr, ts, GameInputDeviceNoStatus, old, false, 0}); | 1531 | queue_event({CbType::Device, d, nullptr, ts, GameInputDeviceNoStatus, old, false, 0}); |
| 1519 | std::fprintf(stderr, "[rt] gameinput: disconnected %s\n", d->name.c_str()); | 1532 | std::fprintf(stderr, "[rt] gameinput: disconnected %s\n", d->name.c_str()); |
| @@ -2509,6 +2522,25 @@ public: | |||
| 2509 | if (kind != GameInputKindUnknown && !(kind & d->info.supportedInput)) | 2522 | if (kind != GameInputKindUnknown && !(kind & d->info.supportedInput)) |
| 2510 | return GAMEINPUT_E_READING_NOT_FOUND; // the device does not do this kind (motion on a plain pad) | 2523 | return GAMEINPUT_E_READING_NOT_FOUND; // the device does not do this kind (motion on a plain pad) |
| 2511 | best = d->latest(); | 2524 | best = d->latest(); |
| 2525 | // A title that reads a pad once a frame, at a few frames a second, would never see a press that came and | ||
| 2526 | // went between two reads: it gets the press once, the pad's real state on the next read. | ||
| 2527 | GameInputGamepadState now_state{}, press{}; | ||
| 2528 | if (best) best->GetGamepadState(&now_state); | ||
| 2529 | const uint64_t now = now_us(); | ||
| 2530 | if (best && d->missed && now - d->read_us > kSlowReaderUs && d->missed->GetGamepadState(&press) && | ||
| 2531 | (press.buttons & ~now_state.buttons)) { | ||
| 2532 | best = d->missed; | ||
| 2533 | now_state = press; | ||
| 2534 | } | ||
| 2535 | d->read_buttons = now_state.buttons; | ||
| 2536 | d->read_us = now; | ||
| 2537 | if (best) { | ||
| 2538 | best->AddRef(); | ||
| 2539 | *out = best; | ||
| 2540 | } | ||
| 2541 | if (d->missed) d->missed->Release(); | ||
| 2542 | d->missed = nullptr; | ||
| 2543 | return *out ? S_OK : GAMEINPUT_E_READING_NOT_FOUND; | ||
| 2512 | } | 2544 | } |
| 2513 | } else { | 2545 | } else { |
| 2514 | for (Device* d : g().connected) // the most recent reading from any gamepad | 2546 | for (Device* d : g().connected) // the most recent reading from any gamepad |