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..d34f0b7ceb 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -2229,6 +2229,52 @@ 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. + // 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(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(); + { + 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(); + } + + // 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); @@ -2278,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);