diff options
| author | MoonEyes <14891700+MoonEyes@users.noreply.github.com> | 2026-11-28 21:25:16 +0100 |
|---|---|---|
| committer | MoonEyes <14891700+MoonEyes@users.noreply.github.com> | 2026-11-28 21:25:16 +0100 |
| commit | 2f99fd2caed4524217306d19c40801e7c9dafedc (patch) | |
| tree | e8284cbe62e68a459a795e29fb40b311808eb58d /runtime/test/gameinput_test.cpp | |
| parent | 9a96b8ba6aaf2fc8ed3f3ad5177d0e3f420dc62c (diff) | |
| parent | 30fc2d68cc509ae9eeb2e39a900dbe1a98b2e745 (diff) | |
Merge main into carry/old-9966 (resolve conflict in runtime/test/gameinput_test.cpp)
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 | 165 |
1 files changed, 158 insertions, 7 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 1cf7112d0..a155fee5a 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -19,6 +19,7 @@ | |||
| 19 | #include <cstdio> | 19 | #include <cstdio> |
| 20 | #include <cstring> | 20 | #include <cstring> |
| 21 | #include <deque> | 21 | #include <deque> |
| 22 | #include <limits> | ||
| 22 | #include <string> | 23 | #include <string> |
| 23 | #include <thread> | 24 | #include <thread> |
| 24 | #include <vector> | 25 | #include <vector> |
| @@ -168,6 +169,17 @@ DWORD g_reading_sleep_ms = 0; // makes on_reading slow, for the Dispatch quota | |||
| 168 | SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback | 169 | SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback |
| 169 | bool g_requeue_down = false; | 170 | bool g_requeue_down = false; |
| 170 | 171 | ||
| 172 | // A reading callback that cancels itself with GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE on its first call. | ||
| 173 | IGameInput* g_self_input = nullptr; | ||
| 174 | bool g_self_stop = false; // StopCallback instead of UnregisterCallback | ||
| 175 | bool g_self_result = false; // what UnregisterCallback returned inside the callback | ||
| 176 | int g_self_calls = 0; | ||
| 177 | void CALLBACK on_self_cancel(GameInputCallbackToken, void*, IGameInputReading*, bool) { | ||
| 178 | if (++g_self_calls != 1) return; | ||
| 179 | if (g_self_stop) g_self_input->StopCallback(kCurrentCallbackToken); | ||
| 180 | else g_self_result = g_self_input->UnregisterCallback(kCurrentCallbackToken, 0); | ||
| 181 | } | ||
| 182 | |||
| 171 | void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { | 183 | void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { |
| 172 | if (reading) ++g_reading_events; | 184 | if (reading) ++g_reading_events; |
| 173 | if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms); | 185 | if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms); |
| @@ -234,6 +246,11 @@ bool fake_cursor(int* x, int* y) { | |||
| 234 | *y = g_cursor_y; | 246 | *y = g_cursor_y; |
| 235 | return true; | 247 | return true; |
| 236 | } | 248 | } |
| 249 | bool fake_cursor_with_offset(int* x, int* y) { | ||
| 250 | *x = g_cursor_x + 4096; | ||
| 251 | *y = g_cursor_y; | ||
| 252 | return true; | ||
| 253 | } | ||
| 237 | 254 | ||
| 238 | GameInputGamepadState pad_state(IGameInput* input, IGameInputDevice* dev, uint32_t* ui = nullptr) { | 255 | GameInputGamepadState pad_state(IGameInput* input, IGameInputDevice* dev, uint32_t* ui = nullptr) { |
| 239 | GameInputGamepadState st{}; | 256 | GameInputGamepadState st{}; |
| @@ -777,6 +794,27 @@ void test_keyboard_pad_sticks(IGameInput* input, IGameInputDevice* kb) { | |||
| 777 | check(rt::gameinput::config().mouse_sensitivity == 1.0f && rt::gameinput::config().ds_trigger_gain == 1.0f, | 794 | check(rt::gameinput::config().mouse_sensitivity == 1.0f && rt::gameinput::config().ds_trigger_gain == 1.0f, |
| 778 | "keyboard pad: mouse sensitivity defaults to 1 and leaves the trigger gain alone"); | 795 | "keyboard pad: mouse sensitivity defaults to 1 and leaves the trigger gain alone"); |
| 779 | 796 | ||
| 797 | g_cursor_x += 3'000'000; | ||
| 798 | rt::gameinput::poll(); | ||
| 799 | check(approx(pad_state(input, kb).rightThumbstickX, 1), | ||
| 800 | "keyboard pad: a positive jump beyond LONG_MAX axis units stays full right"); | ||
| 801 | g_cursor_x -= 3'000'000; | ||
| 802 | rt::gameinput::poll(); | ||
| 803 | check(approx(pad_state(input, kb).rightThumbstickX, -1), | ||
| 804 | "keyboard pad: a negative jump beyond LONG_MIN axis units stays full left"); | ||
| 805 | |||
| 806 | const int prior_x = g_cursor_x; | ||
| 807 | g_cursor_x = std::numeric_limits<int>::max() - 1; | ||
| 808 | rt::gameinput::poll(); | ||
| 809 | settle(); | ||
| 810 | g_cursor_x = std::numeric_limits<int>::min() + 2; | ||
| 811 | rt::gameinput::poll(); | ||
| 812 | check(approx(pad_state(input, kb).rightThumbstickX, 4096 / 32767.0f), | ||
| 813 | "keyboard pad: the raw cursor counter wrap moves four counts right"); | ||
| 814 | g_cursor_x = prior_x; | ||
| 815 | rt::gameinput::poll(); | ||
| 816 | settle(); | ||
| 817 | |||
| 780 | g_keys[VK_RBUTTON] = true; | 818 | g_keys[VK_RBUTTON] = true; |
| 781 | rt::gameinput::poll(); | 819 | rt::gameinput::poll(); |
| 782 | st = pad_state(input, kb); | 820 | st = pad_state(input, kb); |
| @@ -805,6 +843,19 @@ void test_keyboard_pad_sticks(IGameInput* input, IGameInputDevice* kb) { | |||
| 805 | g_focus = true; | 843 | g_focus = true; |
| 806 | rt::gameinput::poll(); | 844 | rt::gameinput::poll(); |
| 807 | check(sticks_at_rest(pad_state(input, kb)), "keyboard pad: no mouse jump when focus returns"); | 845 | check(sticks_at_rest(pad_state(input, kb)), "keyboard pad: no mouse jump when focus returns"); |
| 846 | |||
| 847 | rt::gameinput::set_cursor_source(fake_cursor_with_offset); | ||
| 848 | rt::gameinput::poll(); | ||
| 849 | check(sticks_at_rest(pad_state(input, kb)), "keyboard pad: a new cursor source sets a baseline"); | ||
| 850 | g_cursor_x += 4; | ||
| 851 | rt::gameinput::poll(); | ||
| 852 | check(approx(pad_state(input, kb).rightThumbstickX, 4096 / 32767.0f), | ||
| 853 | "keyboard pad: motion from the new cursor source reaches the stick"); | ||
| 854 | rt::gameinput::set_cursor_source(fake_cursor); | ||
| 855 | rt::gameinput::poll(); | ||
| 856 | check(approx(pad_state(input, kb).rightThumbstickX, 3584 / 32767.0f), | ||
| 857 | "keyboard pad: changing cursor source lets previous motion decay without a jump"); | ||
| 858 | settle(); | ||
| 808 | } | 859 | } |
| 809 | 860 | ||
| 810 | // Runs with no other pad connected and the device callback from main still registered. | 861 | // Runs with no other pad connected and the device callback from main still registered. |
| @@ -1734,6 +1785,65 @@ void test_buffered_gamepad_input(IGameInput* input) { | |||
| 1734 | 1785 | ||
| 1735 | } // namespace | 1786 | } // namespace |
| 1736 | 1787 | ||
| 1788 | // A pad plugged in after the title started, with the title holding a dispatcher (RT-GAMEINPUT-LATE-PAD). The title | ||
| 1789 | // registers its device callback with no pad present, a first pad arrives and is moved on every poll (the keyboard pad's | ||
| 1790 | // mouse stick), and the title runs one queued item a frame (Dispatch(0)). No reading callback is registered, so those | ||
| 1791 | // readings are work no callback wants: they must not queue ahead of the second pad's connect event. | ||
| 1792 | void test_late_pad_behind_dispatcher(IGameInput* input) { | ||
| 1793 | check(rt::gameinput::connected_count() == 0, "late pad: nothing connected at the start"); | ||
| 1794 | IGameInputDispatcher* dispatcher = nullptr; | ||
| 1795 | check(input->CreateDispatcher(&dispatcher) == S_OK && dispatcher, "late pad: CreateDispatcher"); | ||
| 1796 | if (!dispatcher) return; | ||
| 1797 | GameInputCallbackToken token = 0; | ||
| 1798 | check(input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceConnected, | ||
| 1799 | GameInputBlockingEnumeration, nullptr, on_device, &token) == S_OK, | ||
| 1800 | "late pad: RegisterDeviceCallback with no pad present"); | ||
| 1801 | const auto connects_to_us = [&](IGameInputDevice** last) { | ||
| 1802 | size_t n = 0; | ||
| 1803 | for (const DeviceEvent& e : g_device_events) | ||
| 1804 | if (e.token == token && e.previous == GameInputDeviceNoStatus && (e.current & GameInputDeviceConnected)) { | ||
| 1805 | ++n; | ||
| 1806 | if (last) *last = e.device; | ||
| 1807 | } | ||
| 1808 | return n; | ||
| 1809 | }; | ||
| 1810 | Pad first, late; | ||
| 1811 | attach(first, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true); | ||
| 1812 | rt::gameinput::poll(); | ||
| 1813 | while (dispatcher->Dispatch(0)) {} | ||
| 1814 | check(connects_to_us(nullptr) == 1, "late pad: the first pad's connect reaches the callback"); | ||
| 1815 | |||
| 1816 | int16_t x = 0; | ||
| 1817 | const auto frame = [&] { // four polls with the first pad's stick moving, then one item dispatched | ||
| 1818 | for (int i = 0; i < 4; ++i) { | ||
| 1819 | x = static_cast<int16_t>(x == 8000 ? -8000 : 8000); | ||
| 1820 | SDL_SetJoystickVirtualAxis(first.js, SDL_GAMEPAD_AXIS_RIGHTX, x); | ||
| 1821 | rt::gameinput::poll(); | ||
| 1822 | } | ||
| 1823 | dispatcher->Dispatch(0); | ||
| 1824 | }; | ||
| 1825 | for (int f = 0; f < 50; ++f) frame(); | ||
| 1826 | attach(late, "Leonida virtual DualSense", 0x054C, 0x0CE6, false, true); | ||
| 1827 | for (int f = 0; f < 10 && connects_to_us(nullptr) < 2; ++f) frame(); | ||
| 1828 | IGameInputDevice* late_dev = nullptr; | ||
| 1829 | check(connects_to_us(&late_dev) == 2 && late_dev && late_dev->GetDeviceInfo()->vendorId == 0x054C, | ||
| 1830 | "late pad: a pad connected later reaches the callback within a few frames"); | ||
| 1831 | if (late_dev && late.js) { | ||
| 1832 | SDL_SetJoystickVirtualButton(late.js, SDL_GAMEPAD_BUTTON_SOUTH, true); | ||
| 1833 | rt::gameinput::poll(); | ||
| 1834 | IGameInputReading* r = current(input, late_dev); | ||
| 1835 | check(buttons_of(r) & GameInputGamepadA, "late pad: its A press reads through the device it was handed"); | ||
| 1836 | if (r) r->Release(); | ||
| 1837 | } | ||
| 1838 | input->UnregisterCallback(token, 0); | ||
| 1839 | while (dispatcher->Dispatch(UINT64_MAX)) {} | ||
| 1840 | dispatcher->Release(); | ||
| 1841 | detach(first); | ||
| 1842 | detach(late); | ||
| 1843 | rt::gameinput::poll(); | ||
| 1844 | check(rt::gameinput::connected_count() == 0, "late pad: both pads closed"); | ||
| 1845 | } | ||
| 1846 | |||
| 1737 | int main(int argc, char** argv) { | 1847 | int main(int argc, char** argv) { |
| 1738 | // Only virtual pads: no real controller on the test machine may show up. | 1848 | // Only virtual pads: no real controller on the test machine may show up. |
| 1739 | for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI, | 1849 | for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI, |
| @@ -1759,10 +1869,12 @@ int main(int argc, char** argv) { | |||
| 1759 | check(create(&input) == S_OK && input, "GameInputCreate returns an instance"); | 1869 | check(create(&input) == S_OK && input, "GameInputCreate returns an instance"); |
| 1760 | if (argc == 2 && std::strcmp(argv[1], "--input-script") == 0) { | 1870 | if (argc == 2 && std::strcmp(argv[1], "--input-script") == 0) { |
| 1761 | if (!input) return 1; | 1871 | if (!input) return 1; |
| 1762 | // Only the X after byte 63 is due now. Its later entry must not cancel it. | 1872 | // Only the X after byte 63 and the Y held 0.6 s are due now. X's later entry must not cancel it; the |
| 1873 | // malformed holds at the end hold nothing. | ||
| 1763 | set_env("LEONIDA_INPUT_SCRIPT", | 1874 | set_env("LEONIDA_INPUT_SCRIPT", |
| 1764 | "start@3600,start@3600,start@3600,start@3600,start@3600,start@3600, X@0 \t,x@3600," | 1875 | "start@3600,start@3600,start@3600,start@3600,start@3600,start@3600, X@0 \t,x@3600," |
| 1765 | "unknown@0,a@,a@x,,a@-0.1,a@nan,a@inf,a@1e999,a@0tail, ,"); | 1876 | "unknown@0,a@,a@x,,a@-0.1,a@nan,a@inf,a@1e999,a@0tail, ,y@0:0.6," |
| 1877 | "a@0:0,a@0:-1,a@0:,a@0:x,a@0:inf,a@0:601,a@0:1tail"); | ||
| 1766 | set_env("LEONIDA_KEYBOARD_PAD", "1"); | 1878 | set_env("LEONIDA_KEYBOARD_PAD", "1"); |
| 1767 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); | 1879 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); |
| 1768 | rt::gameinput::set_cursor_source(fake_cursor); | 1880 | rt::gameinput::set_cursor_source(fake_cursor); |
| @@ -1771,20 +1883,32 @@ int main(int argc, char** argv) { | |||
| 1771 | rt::gameinput::reload_config(); | 1883 | rt::gameinput::reload_config(); |
| 1772 | rt::gameinput::poll(); | 1884 | rt::gameinput::poll(); |
| 1773 | const auto buttons = pad_state(input, nullptr).buttons; | 1885 | const auto buttons = pad_state(input, nullptr).buttons; |
| 1774 | const auto expected = GameInputGamepadX | GameInputGamepadB; | 1886 | const auto expected = GameInputGamepadX | GameInputGamepadB | GameInputGamepadY; |
| 1775 | check((buttons & expected) == expected, "long script: the spaced X after byte 63 and the physical B are held"); | 1887 | check((buttons & expected) == expected, |
| 1888 | "long script: the spaced X after byte 63, the held Y and the physical B are held"); | ||
| 1776 | check((buttons & ~expected) == 0, "malformed script entries hold no extra buttons"); | 1889 | check((buttons & ~expected) == 0, "malformed script entries hold no extra buttons"); |
| 1777 | g_focus = false; | 1890 | g_focus = false; |
| 1778 | set_env("LEONIDA_INPUT_SCRIPT", "y@0"); | 1891 | set_env("LEONIDA_INPUT_SCRIPT", "y@0"); |
| 1779 | rt::gameinput::reload_config(); | 1892 | rt::gameinput::reload_config(); |
| 1780 | rt::gameinput::poll(); | 1893 | rt::gameinput::poll(); |
| 1781 | check(pad_state(input, nullptr).buttons == GameInputGamepadX, | 1894 | check(pad_state(input, nullptr).buttons == (GameInputGamepadX | GameInputGamepadY), |
| 1782 | "scripted X survives focus loss; reload does not replace it with Y"); | 1895 | "scripted X and Y survive focus loss; reload does not replace the script with y@0"); |
| 1783 | Sleep(350); | 1896 | Sleep(350); |
| 1784 | g_focus = true; | 1897 | g_focus = true; |
| 1785 | rt::gameinput::poll(); | 1898 | rt::gameinput::poll(); |
| 1899 | // A scripted press lasts its hold (300 ms, or button@seconds:hold) and then until the title has read the pads | ||
| 1900 | // six times since it began (update_keyboard_pad: kReads), so a title that polls slowly still sees it. Each | ||
| 1901 | // pad_state() is one GetCurrentReading, and this is the third since X and Y began. | ||
| 1902 | check(pad_state(input, nullptr).buttons == (GameInputGamepadX | GameInputGamepadB | GameInputGamepadY), | ||
| 1903 | "long script: past its 300 ms X is still held while the title has read the pads fewer than six times"); | ||
| 1904 | for (int i = 0; i < 6; ++i) pad_state(input, nullptr); | ||
| 1905 | rt::gameinput::poll(); | ||
| 1906 | check(pad_state(input, nullptr).buttons == (GameInputGamepadB | GameInputGamepadY), | ||
| 1907 | "long script: X ends once the title has read it, y@0:0.6 is still held for its time, the key stays held"); | ||
| 1908 | Sleep(400); | ||
| 1909 | rt::gameinput::poll(); | ||
| 1786 | check(pad_state(input, nullptr).buttons == GameInputGamepadB, | 1910 | check(pad_state(input, nullptr).buttons == GameInputGamepadB, |
| 1787 | "long script: the timed press ends while the physical key stays held"); | 1911 | "long script: Y is released after its 0.6 s hold, the title having read it six times"); |
| 1788 | input->Release(); | 1912 | input->Release(); |
| 1789 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | 1913 | std::printf("%d checks, %d failures\n", g_checks, g_failures); |
| 1790 | return g_failures ? 1 : 0; | 1914 | return g_failures ? 1 : 0; |
| @@ -2131,6 +2255,32 @@ int main(int argc, char** argv) { | |||
| 2131 | if (wait) CloseHandle(wait); | 2255 | if (wait) CloseHandle(wait); |
| 2132 | if (dispatcher) dispatcher->Release(); | 2256 | if (dispatcher) dispatcher->Release(); |
| 2133 | 2257 | ||
| 2258 | // ---- GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE: the running callback inside one, nothing outside ---- | ||
| 2259 | // Measured on the host's GameInput (0.2309, Windows 11): UnregisterCallback(CURRENT) inside a callback returns | ||
| 2260 | // true and removes it; outside a callback it returns false. | ||
| 2261 | check(!input->UnregisterCallback(kCurrentCallbackToken, 0), "current token: UnregisterCallback outside a callback is false"); | ||
| 2262 | input->StopCallback(kCurrentCallbackToken); // outside a callback: names nothing, stops nothing | ||
| 2263 | g_self_input = input; | ||
| 2264 | for (bool stop : {false, true}) { | ||
| 2265 | g_self_stop = stop; | ||
| 2266 | g_self_calls = 0; | ||
| 2267 | g_self_result = false; | ||
| 2268 | GameInputCallbackToken self_token = 0; | ||
| 2269 | check(input->RegisterReadingCallback(dev, GameInputKindGamepad, 0.0f, nullptr, on_self_cancel, &self_token) == S_OK, | ||
| 2270 | "current token: register the self-cancelling callback"); | ||
| 2271 | for (bool down : {true, false, true, false}) { // four changes: without the token a callback sees several | ||
| 2272 | SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_NORTH, down); | ||
| 2273 | rt::gameinput::poll(); | ||
| 2274 | } | ||
| 2275 | check(g_self_calls == 1, stop ? "current token: StopCallback inside the callback stops it (one call for four changes)" | ||
| 2276 | : "current token: UnregisterCallback inside the callback removes it (one call for four changes)"); | ||
| 2277 | if (stop) | ||
| 2278 | check(input->UnregisterCallback(self_token, 0), "current token: the stopped callback stays registered"); | ||
| 2279 | else | ||
| 2280 | check(g_self_result && !input->UnregisterCallback(self_token, 0), | ||
| 2281 | "current token: UnregisterCallback returned true and the real token is gone"); | ||
| 2282 | } | ||
| 2283 | |||
| 2134 | // ---- unplug and replug ---- | 2284 | // ---- unplug and replug ---- |
| 2135 | const size_t events_before_unplug = g_device_events.size(); | 2285 | const size_t events_before_unplug = g_device_events.size(); |
| 2136 | detach(xbox); | 2286 | detach(xbox); |
| @@ -2336,6 +2486,7 @@ int main(int argc, char** argv) { | |||
| 2336 | test_raw_hid(input); | 2486 | test_raw_hid(input); |
| 2337 | test_raw_report_during_reconnect(input); | 2487 | test_raw_report_during_reconnect(input); |
| 2338 | test_buffered_gamepad_input(input); | 2488 | test_buffered_gamepad_input(input); |
| 2489 | test_late_pad_behind_dispatcher(input); | ||
| 2339 | input->Release(); | 2490 | input->Release(); |
| 2340 | 2491 | ||
| 2341 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | 2492 | std::printf("%d checks, %d failures\n", g_checks, g_failures); |