diff options
| author | kawaii-shakal <deusex1998@gmail.com> | 2026-11-25 09:29:04 +0500 |
|---|---|---|
| committer | kawaii-shakal <deusex1998@gmail.com> | 2026-11-25 09:29:04 +0500 |
| commit | a05f1f3cdbb534d3261f8c557f39b637ac240ca8 (patch) | |
| tree | 88f191dcd4e54e532b128bbbd1af8e1516408e5a /runtime/test/gameinput_test.cpp | |
| parent | c7e82e26eadeac237da66949b64e7a52b718361e (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/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index b6658f51c..dee7f7d82 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -1693,6 +1693,65 @@ void test_haptic_motors(IGameInput* input) { | |||
| 1693 | 1693 | ||
| 1694 | } // namespace | 1694 | } // namespace |
| 1695 | 1695 | ||
| 1696 | // A pad plugged in after the title started, with the title holding a dispatcher (RT-GAMEINPUT-LATE-PAD). The title | ||
| 1697 | // registers its device callback with no pad present, a first pad arrives and is moved on every poll (the keyboard pad's | ||
| 1698 | // mouse stick), and the title runs one queued item a frame (Dispatch(0)). No reading callback is registered, so those | ||
| 1699 | // readings are work no callback wants: they must not queue ahead of the second pad's connect event. | ||
| 1700 | void test_late_pad_behind_dispatcher(IGameInput* input) { | ||
| 1701 | check(rt::gameinput::connected_count() == 0, "late pad: nothing connected at the start"); | ||
| 1702 | IGameInputDispatcher* dispatcher = nullptr; | ||
| 1703 | check(input->CreateDispatcher(&dispatcher) == S_OK && dispatcher, "late pad: CreateDispatcher"); | ||
| 1704 | if (!dispatcher) return; | ||
| 1705 | GameInputCallbackToken token = 0; | ||
| 1706 | check(input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceConnected, | ||
| 1707 | GameInputBlockingEnumeration, nullptr, on_device, &token) == S_OK, | ||
| 1708 | "late pad: RegisterDeviceCallback with no pad present"); | ||
| 1709 | const auto connects_to_us = [&](IGameInputDevice** last) { | ||
| 1710 | size_t n = 0; | ||
| 1711 | for (const DeviceEvent& e : g_device_events) | ||
| 1712 | if (e.token == token && e.previous == GameInputDeviceNoStatus && (e.current & GameInputDeviceConnected)) { | ||
| 1713 | ++n; | ||
| 1714 | if (last) *last = e.device; | ||
| 1715 | } | ||
| 1716 | return n; | ||
| 1717 | }; | ||
| 1718 | Pad first, late; | ||
| 1719 | attach(first, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true); | ||
| 1720 | rt::gameinput::poll(); | ||
| 1721 | while (dispatcher->Dispatch(0)) {} | ||
| 1722 | check(connects_to_us(nullptr) == 1, "late pad: the first pad's connect reaches the callback"); | ||
| 1723 | |||
| 1724 | int16_t x = 0; | ||
| 1725 | const auto frame = [&] { // four polls with the first pad's stick moving, then one item dispatched | ||
| 1726 | for (int i = 0; i < 4; ++i) { | ||
| 1727 | x = static_cast<int16_t>(x == 8000 ? -8000 : 8000); | ||
| 1728 | SDL_SetJoystickVirtualAxis(first.js, SDL_GAMEPAD_AXIS_RIGHTX, x); | ||
| 1729 | rt::gameinput::poll(); | ||
| 1730 | } | ||
| 1731 | dispatcher->Dispatch(0); | ||
| 1732 | }; | ||
| 1733 | for (int f = 0; f < 50; ++f) frame(); | ||
| 1734 | attach(late, "Leonida virtual DualSense", 0x054C, 0x0CE6, false, true); | ||
| 1735 | for (int f = 0; f < 10 && connects_to_us(nullptr) < 2; ++f) frame(); | ||
| 1736 | IGameInputDevice* late_dev = nullptr; | ||
| 1737 | check(connects_to_us(&late_dev) == 2 && late_dev && late_dev->GetDeviceInfo()->vendorId == 0x054C, | ||
| 1738 | "late pad: a pad connected later reaches the callback within a few frames"); | ||
| 1739 | if (late_dev && late.js) { | ||
| 1740 | SDL_SetJoystickVirtualButton(late.js, SDL_GAMEPAD_BUTTON_SOUTH, true); | ||
| 1741 | rt::gameinput::poll(); | ||
| 1742 | IGameInputReading* r = current(input, late_dev); | ||
| 1743 | check(buttons_of(r) & GameInputGamepadA, "late pad: its A press reads through the device it was handed"); | ||
| 1744 | if (r) r->Release(); | ||
| 1745 | } | ||
| 1746 | input->UnregisterCallback(token, 0); | ||
| 1747 | while (dispatcher->Dispatch(UINT64_MAX)) {} | ||
| 1748 | dispatcher->Release(); | ||
| 1749 | detach(first); | ||
| 1750 | detach(late); | ||
| 1751 | rt::gameinput::poll(); | ||
| 1752 | check(rt::gameinput::connected_count() == 0, "late pad: both pads closed"); | ||
| 1753 | } | ||
| 1754 | |||
| 1696 | int main(int argc, char** argv) { | 1755 | int main(int argc, char** argv) { |
| 1697 | // Only virtual pads: no real controller on the test machine may show up. | 1756 | // Only virtual pads: no real controller on the test machine may show up. |
| 1698 | for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI, | 1757 | for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI, |
| @@ -2294,6 +2353,7 @@ int main(int argc, char** argv) { | |||
| 2294 | test_haptic_motors(input); | 2353 | test_haptic_motors(input); |
| 2295 | test_raw_hid(input); | 2354 | test_raw_hid(input); |
| 2296 | test_raw_report_during_reconnect(input); | 2355 | test_raw_report_during_reconnect(input); |
| 2356 | test_late_pad_behind_dispatcher(input); | ||
| 2297 | input->Release(); | 2357 | input->Release(); |
| 2298 | 2358 | ||
| 2299 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | 2359 | std::printf("%d checks, %d failures\n", g_checks, g_failures); |