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:
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 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
81constexpr float kMaxAcceleration = 19.6133f; // 2 g in m/s^2 81constexpr float kMaxAcceleration = 19.6133f; // 2 g in m/s^2
82constexpr float kMaxAngularVelocity = 34.9066f; // 2000 deg/s in rad/s 82constexpr float kMaxAngularVelocity = 34.9066f; // 2000 deg/s in rad/s
83constexpr float kMotionSensorRate = 250.0f; // requested sample rate; the device clamps to what it has 83constexpr float kMotionSensorRate = 250.0f; // requested sample rate; the device clamps to what it has
84constexpr uint64_t kSlowReaderUs = 50'000; // a title reading a pad less often than this gets presses it would miss
84constexpr uint64_t kMotionReadingUs = 8'000; // a motion pad streams readings at ~125 Hz so the history stays sane 85constexpr uint64_t kMotionReadingUs = 8'000; // a motion pad streams readings at ~125 Hz so the history stays sane
85 86
86uint64_t now_us() { 87uint64_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