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:
authorkawaii-shakal <deusex1998@gmail.com>2026-11-25 09:29:04 +0500
committerkawaii-shakal <deusex1998@gmail.com>2026-11-25 09:29:04 +0500
commita05f1f3cdbb534d3261f8c557f39b637ac240ca8 (patch)
tree88f191dcd4e54e532b128bbbd1af8e1516408e5a /runtime/shims/gameinput.cpp
parentc7e82e26eadeac237da66949b64e7a52b718361e (diff)
gameinput: queue only events a callback wants, so a later pad's connect is not stuck behind a backlog (RT-GAMEINPUT-LATE-PAD)
A pad connected after the title started was seen by the shim but not by the title, while pads present at start worked. Pads present at start reach a title through blocking enumeration, which runs synchronously in RegisterDeviceCallback. A later pad reaches it only through a queued connect event. queue_event queued every reading and guide change whether or not any callback listened. With a dispatcher held, a title that runs one item a frame (Dispatch(0), the pattern Microsoft's Dispatch page suggests) falls further behind every frame, because a moving stick or a sensor pad's 125 Hz stream queues items faster. The later pad's connect event then never comes out of the queue. queue_event now drops an event no registered callback matches, as GameInput has no work to dispatch then. The first few RegisterDeviceCallback calls and the first CreateDispatcher and Dispatch calls are logged, so a retail run shows whether the title holds a dispatcher (not yet confirmed for the retail title). Test: test_late_pad_behind_dispatcher. A title with a dispatcher registers its device callback with no pad, a first pad moves on every poll, and a pad attached later must reach the callback within ten frames of Dispatch(0). Without the fix it fails (2 of 371 checks); with it, 371 checks pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp31
1 files changed, 29 insertions, 2 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index db685e71f..f5a9322b5 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -849,10 +849,19 @@ State& g() {
849 return *s; 849 return *s;
850} 850}
851 851
852bool matches(const Callback& c, const Event& e);
853
854// Under g().mu. Queues an event only when a registered callback wants it: GameInput dispatches callbacks, and with
855// none listening there is no work. Otherwise every reading (a pad's moving stick, a sensor pad's 125 Hz stream) would
856// queue an item nobody hears, and a title holding a dispatcher that runs one item a frame (Dispatch(0)) would fall
857// further behind every frame: a pad plugged in after start would have its connect event stuck behind that backlog and
858// never reach the title (RT-GAMEINPUT-LATE-PAD).
852void queue_event(const Event& e) { 859void queue_event(const Event& e) {
860 State& s = g();
861 if (std::none_of(s.callbacks.begin(), s.callbacks.end(), [&e](const Callback& c) { return matches(c, e); })) return;
853 if (e.device) e.device->AddRef(); 862 if (e.device) e.device->AddRef();
854 if (e.reading) e.reading->AddRef(); 863 if (e.reading) e.reading->AddRef();
855 g().queue.push_back(e); 864 s.queue.push_back(e);
856} 865}
857 866
858bool matches(const Callback& c, const Event& e) { 867bool matches(const Callback& c, const Event& e) {
@@ -2403,7 +2412,13 @@ public:
2403 return r; 2412 return r;
2404 } 2413 }
2405 // Runs the queued callbacks on the caller's thread within the quota (see deliver_within). 2414 // Runs the queued callbacks on the caller's thread within the quota (see deliver_within).
2406 bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override { return deliver_within(quotaInMicroseconds); } 2415 bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override {
2416 static std::atomic<bool> s_logged{false};
2417 if (!s_logged.exchange(true))
2418 std::fprintf(stderr, "[rt] gameinput: Dispatch first call (quota %llu us)\n",
2419 static_cast<unsigned long long>(quotaInMicroseconds));
2420 return deliver_within(quotaInMicroseconds);
2421 }
2407 HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override { 2422 HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override {
2408 if (!handle) return E_POINTER; 2423 if (!handle) return E_POINTER;
2409 *handle = nullptr; 2424 *handle = nullptr;
@@ -2550,6 +2565,15 @@ public:
2550 if (token) *token = kInvalidCallbackToken; 2565 if (token) *token = kInvalidCallbackToken;
2551 if (!fn) return E_INVALIDARG; 2566 if (!fn) return E_INVALIDARG;
2552 if (device && as_device(device)->info.deviceFamily == GameInputFamilyAggregate) return E_NOTIMPL; 2567 if (device && as_device(device)->info.deviceFamily == GameInputFamilyAggregate) return E_NOTIMPL;
2568 // How a title learns of its pads decides whether one plugged in later reaches it, so the first few
2569 // registrations are logged.
2570 static std::atomic<int> s_logged{0};
2571 if (s_logged.fetch_add(1) < 8)
2572 std::fprintf(stderr, "[rt] gameinput: RegisterDeviceCallback(%s, kind 0x%x, status 0x%x, %s enumeration)\n",
2573 device ? "one device" : "all devices", kind, status_filter,
2574 enumeration == GameInputBlockingEnumeration ? "blocking"
2575 : enumeration == GameInputAsyncEnumeration ? "async"
2576 : "no");
2553 State& s = g(); 2577 State& s = g();
2554 GameInputCallbackToken t; 2578 GameInputCallbackToken t;
2555 std::vector<Event> now; 2579 std::vector<Event> now;
@@ -2619,6 +2643,9 @@ public:
2619 2643
2620 HRESULT STDMETHODCALLTYPE CreateDispatcher(IGameInputDispatcher** out) override { 2644 HRESULT STDMETHODCALLTYPE CreateDispatcher(IGameInputDispatcher** out) override {
2621 if (!out) return E_POINTER; 2645 if (!out) return E_POINTER;
2646 static std::atomic<bool> s_logged{false};
2647 if (!s_logged.exchange(true))
2648 std::fprintf(stderr, "[rt] gameinput: CreateDispatcher first call (callbacks now run in Dispatch)\n");
2622 *out = new Dispatcher; 2649 *out = new Dispatcher;
2623 return S_OK; 2650 return S_OK;
2624 } 2651 }