diff --git a/docs/gameinput.md b/docs/gameinput.md index 0d89255531..9b9975a69a 100644 --- a/docs/gameinput.md +++ b/docs/gameinput.md @@ -363,7 +363,11 @@ button) run on that thread, as GameInput runs them on its own threads. Once the `IGameInputDispatcher::Dispatch` runs them on the title's thread, oldest first, within its quota: at least one queued event per call (a quota of 0 runs exactly one), no further event once the quota has passed, and only the events queued when the call started (later ones wait for the next call, so even an unlimited quota returns). The wait -handle is signalled again while events remain. +handle is signalled again while events remain. An event is queued only when a registered callback wants it (a +reading with no reading callback, say, is no work), so readings nobody hears cannot build a backlog that a title +running one item a frame (`Dispatch(0)`) never catches up on, with a later pad's connect event stuck behind it. The +first few `RegisterDeviceCallback` calls (filter, kind, status filter, enumeration) and the first `CreateDispatcher` +and `Dispatch` calls are logged, since they decide how the title hears of a pad plugged in after start. The API surface: diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index ef3f3b69a7..9cfe1231b8 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -865,10 +865,19 @@ State& g() { return *s; } +bool matches(const Callback& c, const Event& e); + +// Under g().mu. Queues an event only when a registered callback wants it: GameInput dispatches callbacks, and with +// none listening there is no work. Otherwise every reading (a pad's moving stick, a sensor pad's 125 Hz stream) would +// queue an item nobody hears, and a title holding a dispatcher that runs one item a frame (Dispatch(0)) would fall +// further behind every frame: a pad plugged in after start would have its connect event stuck behind that backlog and +// never reach the title (RT-GAMEINPUT-LATE-PAD). void queue_event(const Event& e) { + State& s = g(); + if (std::none_of(s.callbacks.begin(), s.callbacks.end(), [&e](const Callback& c) { return matches(c, e); })) return; if (e.device) e.device->AddRef(); if (e.reading) e.reading->AddRef(); - g().queue.push_back(e); + s.queue.push_back(e); } bool matches(const Callback& c, const Event& e) { @@ -2424,7 +2433,13 @@ class Dispatcher final : public IGameInputDispatcher { return r; } // Runs the queued callbacks on the caller's thread within the quota (see deliver_within). - bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override { return deliver_within(quotaInMicroseconds); } + bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override { + static std::atomic s_logged{false}; + if (!s_logged.exchange(true)) + std::fprintf(stderr, "[rt] gameinput: Dispatch first call (quota %llu us)\n", + static_cast(quotaInMicroseconds)); + return deliver_within(quotaInMicroseconds); + } HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override { if (!handle) return E_POINTER; *handle = nullptr; @@ -2648,6 +2663,15 @@ class GameInput final : public IGameInput { if (token) *token = kInvalidCallbackToken; if (!fn) return E_INVALIDARG; if (device && as_device(device)->info.deviceFamily == GameInputFamilyAggregate) return E_NOTIMPL; + // How a title learns of its pads decides whether one plugged in later reaches it, so the first few + // registrations are logged. + static std::atomic s_logged{0}; + if (s_logged.fetch_add(1) < 8) + std::fprintf(stderr, "[rt] gameinput: RegisterDeviceCallback(%s, kind 0x%x, status 0x%x, %s enumeration)\n", + device ? "one device" : "all devices", kind, status_filter, + enumeration == GameInputBlockingEnumeration ? "blocking" + : enumeration == GameInputAsyncEnumeration ? "async" + : "no"); State& s = g(); GameInputCallbackToken t; std::vector now; @@ -2717,6 +2741,9 @@ class GameInput final : public IGameInput { HRESULT STDMETHODCALLTYPE CreateDispatcher(IGameInputDispatcher** out) override { if (!out) return E_POINTER; + static std::atomic s_logged{false}; + if (!s_logged.exchange(true)) + std::fprintf(stderr, "[rt] gameinput: CreateDispatcher first call (callbacks now run in Dispatch)\n"); *out = new Dispatcher; return S_OK; } diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 9aae6a4872..314d9293e8 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -1715,6 +1715,65 @@ void test_haptic_motors(IGameInput* input) { } // namespace +// A pad plugged in after the title started, with the title holding a dispatcher (RT-GAMEINPUT-LATE-PAD). The title +// registers its device callback with no pad present, a first pad arrives and is moved on every poll (the keyboard pad's +// mouse stick), and the title runs one queued item a frame (Dispatch(0)). No reading callback is registered, so those +// readings are work no callback wants: they must not queue ahead of the second pad's connect event. +void test_late_pad_behind_dispatcher(IGameInput* input) { + check(rt::gameinput::connected_count() == 0, "late pad: nothing connected at the start"); + IGameInputDispatcher* dispatcher = nullptr; + check(input->CreateDispatcher(&dispatcher) == S_OK && dispatcher, "late pad: CreateDispatcher"); + if (!dispatcher) return; + GameInputCallbackToken token = 0; + check(input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceConnected, + GameInputBlockingEnumeration, nullptr, on_device, &token) == S_OK, + "late pad: RegisterDeviceCallback with no pad present"); + const auto connects_to_us = [&](IGameInputDevice** last) { + size_t n = 0; + for (const DeviceEvent& e : g_device_events) + if (e.token == token && e.previous == GameInputDeviceNoStatus && (e.current & GameInputDeviceConnected)) { + ++n; + if (last) *last = e.device; + } + return n; + }; + Pad first, late; + attach(first, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true); + rt::gameinput::poll(); + while (dispatcher->Dispatch(0)) {} + check(connects_to_us(nullptr) == 1, "late pad: the first pad's connect reaches the callback"); + + int16_t x = 0; + const auto frame = [&] { // four polls with the first pad's stick moving, then one item dispatched + for (int i = 0; i < 4; ++i) { + x = static_cast(x == 8000 ? -8000 : 8000); + SDL_SetJoystickVirtualAxis(first.js, SDL_GAMEPAD_AXIS_RIGHTX, x); + rt::gameinput::poll(); + } + dispatcher->Dispatch(0); + }; + for (int f = 0; f < 50; ++f) frame(); + attach(late, "Leonida virtual DualSense", 0x054C, 0x0CE6, false, true); + for (int f = 0; f < 10 && connects_to_us(nullptr) < 2; ++f) frame(); + IGameInputDevice* late_dev = nullptr; + check(connects_to_us(&late_dev) == 2 && late_dev && late_dev->GetDeviceInfo()->vendorId == 0x054C, + "late pad: a pad connected later reaches the callback within a few frames"); + if (late_dev && late.js) { + SDL_SetJoystickVirtualButton(late.js, SDL_GAMEPAD_BUTTON_SOUTH, true); + rt::gameinput::poll(); + IGameInputReading* r = current(input, late_dev); + check(buttons_of(r) & GameInputGamepadA, "late pad: its A press reads through the device it was handed"); + if (r) r->Release(); + } + input->UnregisterCallback(token, 0); + while (dispatcher->Dispatch(UINT64_MAX)) {} + dispatcher->Release(); + detach(first); + detach(late); + rt::gameinput::poll(); + check(rt::gameinput::connected_count() == 0, "late pad: both pads closed"); +} + int main(int argc, char** argv) { // Only virtual pads: no real controller on the test machine may show up. for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI, @@ -2330,6 +2389,7 @@ int main(int argc, char** argv) { test_haptic_motors(input); test_raw_hid(input); test_raw_report_during_reconnect(input); + test_late_pad_behind_dispatcher(input); input->Release(); std::printf("%d checks, %d failures\n", g_checks, g_failures);