upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.cpp
diff options
context:
space:
mode:
authorMoonEyes <ducoinbenjamin@yahoo.fr>2026-11-28 21:18:21 +0100
committerGitHub <noreply@github.com>2026-11-28 21:18:21 +0100
commit9b7944b4e53c3cda899be3952248ab97e3f5e76f (patch)
tree70ee4c67ef61c1d0919a28f099ec9f73af21f495 /runtime/shims/gameinput.cpp
parent90b39456a6f743fde1d02c38d53937436c787cf2 (diff)
parent045bb0fcec9ea44105ae118f8d958dc76fa1b4af (diff)
Merge pull request #1510 from ProjectLeonida/claude/gameinput-slow-reader
gameinput: report a missed press once to a slow reader (RT-GAMEINPUT-SLOW-READER)
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index b47b5a4fc..1e9b33041 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -85,6 +85,7 @@ constexpr size_t kHistoryCap = 512; // readings per pad, whatever the poll
85constexpr float kMaxAcceleration = 19.6133f; // 2 g in m/s^2 85constexpr float kMaxAcceleration = 19.6133f; // 2 g in m/s^2
86constexpr float kMaxAngularVelocity = 34.9066f; // 2000 deg/s in rad/s 86constexpr float kMaxAngularVelocity = 34.9066f; // 2000 deg/s in rad/s
87constexpr float kMotionSensorRate = 250.0f; // requested sample rate; the device clamps to what it has 87constexpr float kMotionSensorRate = 250.0f; // requested sample rate; the device clamps to what it has
88constexpr uint64_t kSlowReaderUs = 50'000; // a title reading a pad less often than this gets presses it would miss
88constexpr uint64_t kMotionReadingUs = 8'000; // a motion pad streams readings at ~125 Hz so the history stays sane 89constexpr uint64_t kMotionReadingUs = 8'000; // a motion pad streams readings at ~125 Hz so the history stays sane
89 90
90uint64_t now_us() { 91uint64_t now_us() {
@@ -539,6 +540,9 @@ public:
539 std::atomic<uint32_t> status{GameInputDeviceNoStatus}; 540 std::atomic<uint32_t> status{GameInputDeviceNoStatus};
540 std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading 541 std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading
541 uint64_t evicted_order = 0; // order of the newest reading dropped from history 542 uint64_t evicted_order = 0; // order of the newest reading dropped from history
543 Reading* missed = nullptr; // one reference: a press since the title's last GetCurrentReading here
544 uint32_t read_buttons = 0; // the buttons that call answered, and when
545 uint64_t read_us = 0;
542 Reading* latest() const { return history.empty() ? nullptr : history.back(); } 546 Reading* latest() const { return history.empty() ? nullptr : history.back(); }
543 uint64_t seq = 0; 547 uint64_t seq = 0;
544 Rumble wanted; 548 Rumble wanted;
@@ -1271,6 +1275,11 @@ Reading* push_reading(Device* d, const Snapshot& snap, uint64_t ts) {
1271 std::deque<Reading*>& h = d->history; 1275 std::deque<Reading*>& h = d->history;
1272 d->last_reading_us = ts; 1276 d->last_reading_us = ts;
1273 h.push_back(new Reading(d, snap, ++d->seq, ts, ++g().last_order)); 1277 h.push_back(new Reading(d, snap, ++d->seq, ts, ++g().last_order));
1278 if (d->read_us && (snap.gamepad.buttons & ~d->read_buttons)) {
1279 if (d->missed) d->missed->Release();
1280 d->missed = h.back();
1281 d->missed->AddRef();
1282 }
1274 while (h.size() > kHistoryCap || (h.size() > 1 && h[1]->GetTimestamp() + kHistoryUs < ts)) { 1283 while (h.size() > kHistoryCap || (h.size() > 1 && h[1]->GetTimestamp() + kHistoryUs < ts)) {
1275 d->evicted_order = h.front()->order(); 1284 d->evicted_order = h.front()->order();
1276 h.front()->Release(); 1285 h.front()->Release();
@@ -1531,6 +1540,10 @@ void close_pad(Device* d, uint64_t ts) {
1531 if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session 1540 if (!d->history.empty()) d->evicted_order = d->history.back()->order(); // the history ends with the session
1532 for (Reading* r : d->history) r->Release(); 1541 for (Reading* r : d->history) r->Release();
1533 d->history.clear(); 1542 d->history.clear();
1543 if (d->missed) d->missed->Release();
1544 d->missed = nullptr;
1545 d->read_buttons = 0;
1546 d->read_us = 0;
1534 s.connected.erase(std::remove(s.connected.begin(), s.connected.end(), d), s.connected.end()); 1547 s.connected.erase(std::remove(s.connected.begin(), s.connected.end(), d), s.connected.end());
1535 queue_event({CbType::Device, d, nullptr, ts, GameInputDeviceNoStatus, old, false, 0}); 1548 queue_event({CbType::Device, d, nullptr, ts, GameInputDeviceNoStatus, old, false, 0});
1536 std::fprintf(stderr, "[rt] gameinput: disconnected %s\n", d->name.c_str()); 1549 std::fprintf(stderr, "[rt] gameinput: disconnected %s\n", d->name.c_str());
@@ -2641,6 +2654,25 @@ public:
2641 if (kind != GameInputKindUnknown && !(kind & d->info.supportedInput)) 2654 if (kind != GameInputKindUnknown && !(kind & d->info.supportedInput))
2642 return GAMEINPUT_E_READING_NOT_FOUND; // the device does not do this kind (motion on a plain pad) 2655 return GAMEINPUT_E_READING_NOT_FOUND; // the device does not do this kind (motion on a plain pad)
2643 best = d->latest(); 2656 best = d->latest();
2657 // A title that reads a pad once a frame, at a few frames a second, would never see a press that came and
2658 // went between two reads: it gets the press once, the pad's real state on the next read.
2659 GameInputGamepadState now_state{}, press{};
2660 if (best) best->GetGamepadState(&now_state);
2661 const uint64_t now = now_us();
2662 if (best && d->missed && now - d->read_us > kSlowReaderUs && d->missed->GetGamepadState(&press) &&
2663 (press.buttons & ~now_state.buttons)) {
2664 best = d->missed;
2665 now_state = press;
2666 }
2667 d->read_buttons = now_state.buttons;
2668 d->read_us = now;
2669 if (best) {
2670 best->AddRef();
2671 *out = best;
2672 }
2673 if (d->missed) d->missed->Release();
2674 d->missed = nullptr;
2675 return *out ? S_OK : GAMEINPUT_E_READING_NOT_FOUND;
2644 } 2676 }
2645 } else { 2677 } else {
2646 for (Device* d : g().connected) // the most recent reading from any gamepad 2678 for (Device* d : g().connected) // the most recent reading from any gamepad