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