diff options
| author | QemmHD <stephenlethco2@gmail.com> | 2026-11-24 15:44:44 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-24 15:44:44 -0500 |
| commit | 66d4f5aa36bb6bcd006fa4b311c7a84d809fdbad (patch) | |
| tree | 5bd248f77fd28157fb2fb831e1e91924160a464f /runtime | |
| parent | 22d044f60a7c7eb721a03d3f03b01f936308ff00 (diff) | |
| parent | f9ba70b4de394f803273ab60adcbef9b15849ab8 (diff) | |
Merge pull request #1074 from Moriz82/split/rt-input-script-length
Merging.
**Why merged:** RT-INPUT-SCRIPT-LENGTH: the shim's env() reader now handles values of any length (64 characters or more used to read as unset). The PR also adds the LEONIDA_INPUT_SCRIPT keyboard-pad scheduler (`button@seconds`, 300 ms presses), documented in docs/gameinput.md. That's a second topic, but the body says so, and both parts were reviewed. sodove checked 14/14 runs across lengths, with the control failing 12/12, and kawaii-shakal's harness follow-ups were addressed.
**Why it's good:** gameinput_host, gameinput and the new gameinput_script pass; with validation, gameinput_script passes and logs "8 accepted / 11 rejected". The parsing is bounded (strtod with end-pointer and finiteness checks) and reads only its own env var.
Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Merged alone onto main, its ctests pass both plain and with LEONIDA_VK_VALIDATION=1. Cumulative: #517, #907, #927, #954 and #1074 merged onto main 32a5d9698 (after aslan2142's runtime merges). There the repo-consistency set gives 573 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 314/316 with the same 11 skips. The 2 failures are d3d12_depth_resolve (pre-existing) and d3d12_close_parity, which fails on plain 32a5d9698 too: a regression from #510, reported there, not from these PRs. git merge-tree against current main: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes.
AI-assisted (Claude Code, Claude Opus 5.5).
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | runtime/shims/gameinput.cpp | 79 | ||||
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 36 |
3 files changed, 111 insertions, 8 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f5c84646a..d81708e6c 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -1339,10 +1339,12 @@ if(BUILD_TESTING) | |||
| 1339 | add_test(NAME active_input COMMAND active_input_test) | 1339 | add_test(NAME active_input COMMAND active_input_test) |
| 1340 | if(WIN32 AND TARGET gameinput_test) | 1340 | if(WIN32 AND TARGET gameinput_test) |
| 1341 | add_test(NAME gameinput COMMAND gameinput_test) | 1341 | add_test(NAME gameinput COMMAND gameinput_test) |
| 1342 | add_test(NAME gameinput_script COMMAND gameinput_test --input-script) | ||
| 1342 | elseif(TARGET gta6rt) | 1343 | elseif(TARGET gta6rt) |
| 1343 | # Built without SDL3: listed as "Not Run (Disabled)" so it is visible that the shim was not tested. | 1344 | # Built without SDL3: listed as "Not Run (Disabled)" so it is visible that the shim was not tested. |
| 1344 | add_test(NAME gameinput COMMAND ${CMAKE_COMMAND} -E echo "built without SDL3") | 1345 | add_test(NAME gameinput COMMAND ${CMAKE_COMMAND} -E echo "built without SDL3") |
| 1345 | set_tests_properties(gameinput PROPERTIES DISABLED TRUE) | 1346 | add_test(NAME gameinput_script COMMAND ${CMAKE_COMMAND} -E echo "built without SDL3") |
| 1347 | set_tests_properties(gameinput gameinput_script PROPERTIES DISABLED TRUE) | ||
| 1346 | endif() | 1348 | endif() |
| 1347 | if(WIN32 AND TARGET gta6rt AND TARGET tiny_game) | 1349 | if(WIN32 AND TARGET gta6rt AND TARGET tiny_game) |
| 1348 | if(TARGET tiny_argv_default_a AND NOT CMAKE_CROSSCOMPILING) | 1350 | if(TARGET tiny_argv_default_a AND NOT CMAKE_CROSSCOMPILING) |
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 402179cde..7650b361f 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -47,6 +47,7 @@ | |||
| 47 | #include <mutex> | 47 | #include <mutex> |
| 48 | #include <string> | 48 | #include <string> |
| 49 | #include <thread> | 49 | #include <thread> |
| 50 | #include <utility> | ||
| 50 | #include <vector> | 51 | #include <vector> |
| 51 | 52 | ||
| 52 | #include "gameinput.h" | 53 | #include "gameinput.h" |
| @@ -132,10 +133,14 @@ const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); } | |||
| 132 | // ---- configuration ---------------------------------------------------------------------------------------------- | 133 | // ---- configuration ---------------------------------------------------------------------------------------------- |
| 133 | 134 | ||
| 134 | std::string env(const char* name) { | 135 | std::string env(const char* name) { |
| 135 | char buf[64]; | 136 | // Any length: an input script (LEONIDA_INPUT_SCRIPT) is easily longer than a fixed buffer, and a value that did |
| 136 | const DWORD n = GetEnvironmentVariableA(name, buf, sizeof buf); | 137 | // not fit used to read as unset. |
| 137 | if (n == 0 || n >= sizeof buf) return {}; | 138 | const DWORD need = GetEnvironmentVariableA(name, nullptr, 0); // with the terminator |
| 138 | std::string s(buf, n); | 139 | if (need == 0) return {}; |
| 140 | std::string s(need, '\0'); | ||
| 141 | const DWORD n = GetEnvironmentVariableA(name, s.data(), need); | ||
| 142 | if (n == 0 || n >= need) return {}; | ||
| 143 | s.resize(n); | ||
| 139 | for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); | 144 | for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); |
| 140 | return s; | 145 | return s; |
| 141 | } | 146 | } |
| @@ -1861,8 +1866,8 @@ void update_keyboard_pad() { | |||
| 1861 | bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; | 1866 | bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; |
| 1862 | int lx = 0, ly = 0; | 1867 | int lx = 0, ly = 0; |
| 1863 | bool aim = false, fire = false; | 1868 | bool aim = false, fire = false; |
| 1864 | // Out of focus every button, stick and trigger is released, so a key held while switching windows does not stay | 1869 | // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump. |
| 1865 | // pressed, and the cursor is read afresh on return, so moving it in another window does not swing the stick. | 1870 | // Scripted presses still run while the window is unfocused. |
| 1866 | const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()(); | 1871 | const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()(); |
| 1867 | if (active) { | 1872 | if (active) { |
| 1868 | const rt::gameinput::KeyDownFn key_down = g_key_down.load(); | 1873 | const rt::gameinput::KeyDownFn key_down = g_key_down.load(); |
| @@ -1890,6 +1895,68 @@ void update_keyboard_pad() { | |||
| 1890 | } | 1895 | } |
| 1891 | clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); | 1896 | clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); |
| 1892 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); | 1897 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); |
| 1898 | // Timed presses on the keyboard pad, in seconds since its first update. Each press lasts 300 ms. | ||
| 1899 | struct ScriptedPress { | ||
| 1900 | SDL_GamepadButton button; | ||
| 1901 | double at; | ||
| 1902 | }; | ||
| 1903 | static const std::vector<ScriptedPress> script = [] { | ||
| 1904 | std::vector<ScriptedPress> out; | ||
| 1905 | static const std::pair<const char*, SDL_GamepadButton> names[] = { | ||
| 1906 | {"a", SDL_GAMEPAD_BUTTON_SOUTH}, {"b", SDL_GAMEPAD_BUTTON_EAST}, | ||
| 1907 | {"x", SDL_GAMEPAD_BUTTON_WEST}, {"y", SDL_GAMEPAD_BUTTON_NORTH}, | ||
| 1908 | {"start", SDL_GAMEPAD_BUTTON_START}, {"back", SDL_GAMEPAD_BUTTON_BACK}, | ||
| 1909 | {"lb", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER}, {"rb", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER}, | ||
| 1910 | {"up", SDL_GAMEPAD_BUTTON_DPAD_UP}, {"down", SDL_GAMEPAD_BUTTON_DPAD_DOWN}, | ||
| 1911 | {"left", SDL_GAMEPAD_BUTTON_DPAD_LEFT}, {"right", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}}; | ||
| 1912 | const std::string text = env("LEONIDA_INPUT_SCRIPT"); | ||
| 1913 | if (text.empty()) return out; | ||
| 1914 | constexpr char whitespace[] = " \t\r\n\f\v"; | ||
| 1915 | size_t items = 0; | ||
| 1916 | for (size_t at = 0, end; at <= text.size(); at = end + 1) { | ||
| 1917 | ++items; | ||
| 1918 | end = std::min(text.find(',', at), text.size()); | ||
| 1919 | const std::string item = text.substr(at, end - at); | ||
| 1920 | const size_t first = item.find_first_not_of(whitespace); | ||
| 1921 | const size_t sep = item.find('@'); | ||
| 1922 | if (sep == std::string::npos || first >= sep) continue; | ||
| 1923 | const char* time = item.c_str() + sep + 1; | ||
| 1924 | char* tail; | ||
| 1925 | const double seconds = std::strtod(time, &tail); | ||
| 1926 | if (tail == time || !std::isfinite(seconds) || seconds < 0 || | ||
| 1927 | item.find_first_not_of(whitespace, tail - item.c_str()) != std::string::npos) continue; | ||
| 1928 | for (const auto& [name, button] : names) { | ||
| 1929 | if (item.compare(first, sep - first, name) == 0) { | ||
| 1930 | out.push_back({button, seconds}); | ||
| 1931 | break; | ||
| 1932 | } | ||
| 1933 | } | ||
| 1934 | } | ||
| 1935 | std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n", | ||
| 1936 | out.size(), items - out.size()); | ||
| 1937 | return out; | ||
| 1938 | }(); | ||
| 1939 | if (!script.empty()) { | ||
| 1940 | static const ULONGLONG origin = GetTickCount64(); | ||
| 1941 | const double now = (GetTickCount64() - origin) / 1000.0; | ||
| 1942 | // One decision per button: a later entry for the same button must not release what an earlier entry holds | ||
| 1943 | // (it did: with several entries only the last one ever reached the title). | ||
| 1944 | bool scripted[SDL_GAMEPAD_BUTTON_COUNT] = {}; | ||
| 1945 | bool named[SDL_GAMEPAD_BUTTON_COUNT] = {}; | ||
| 1946 | for (const auto& press : script) { | ||
| 1947 | named[press.button] = true; | ||
| 1948 | if (now >= press.at && now < press.at + 0.3) scripted[press.button] = true; | ||
| 1949 | } | ||
| 1950 | for (int button = 0; button < SDL_GAMEPAD_BUTTON_COUNT; ++button) { | ||
| 1951 | if (!named[button]) continue; | ||
| 1952 | SDL_SetJoystickVirtualButton(g_keyboard.js, static_cast<SDL_GamepadButton>(button), | ||
| 1953 | scripted[button] || down[button]); | ||
| 1954 | static bool was[SDL_GAMEPAD_BUTTON_COUNT] = {}; | ||
| 1955 | if (scripted[button] && !was[button]) | ||
| 1956 | std::fprintf(stderr, "[rt] gameinput: scripted press of button %d at %.1f s\n", button, now); | ||
| 1957 | was[button] = scripted[button]; | ||
| 1958 | } | ||
| 1959 | } | ||
| 1893 | const int reach = lx && ly ? kStickDiagonal : kStickFull; | 1960 | const int reach = lx && ly ? kStickDiagonal : kStickFull; |
| 1894 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach)); | 1961 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach)); |
| 1895 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach)); | 1962 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach)); |
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 93c48518b..b6658f51c 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -1693,12 +1693,14 @@ void test_haptic_motors(IGameInput* input) { | |||
| 1693 | 1693 | ||
| 1694 | } // namespace | 1694 | } // namespace |
| 1695 | 1695 | ||
| 1696 | int main() { | 1696 | int main(int argc, char** argv) { |
| 1697 | // Only virtual pads: no real controller on the test machine may show up. | 1697 | // 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, | 1698 | for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI, |
| 1699 | SDL_HINT_JOYSTICK_GAMEINPUT, SDL_HINT_XINPUT_ENABLED, SDL_HINT_JOYSTICK_DIRECTINPUT}) | 1699 | SDL_HINT_JOYSTICK_GAMEINPUT, SDL_HINT_XINPUT_ENABLED, SDL_HINT_JOYSTICK_DIRECTINPUT}) |
| 1700 | SDL_SetHintWithPriority(hint, "0", SDL_HINT_OVERRIDE); | 1700 | SDL_SetHintWithPriority(hint, "0", SDL_HINT_OVERRIDE); |
| 1701 | set_env("LEONIDA_GAMEINPUT_THREAD", "0"); | 1701 | set_env("LEONIDA_GAMEINPUT_THREAD", "0"); |
| 1702 | set_env("LEONIDA_KEYBOARD_PAD", nullptr); | ||
| 1703 | set_env("LEONIDA_INPUT_SCRIPT", nullptr); | ||
| 1702 | set_env("LEONIDA_DS_TRIGGERS", nullptr); | 1704 | set_env("LEONIDA_DS_TRIGGERS", nullptr); |
| 1703 | set_env("LEONIDA_DS_LIGHTBAR", nullptr); | 1705 | set_env("LEONIDA_DS_LIGHTBAR", nullptr); |
| 1704 | set_env("LEONIDA_RUMBLE", nullptr); | 1706 | set_env("LEONIDA_RUMBLE", nullptr); |
| @@ -1714,6 +1716,38 @@ int main() { | |||
| 1714 | 1716 | ||
| 1715 | IGameInput* input = nullptr; | 1717 | IGameInput* input = nullptr; |
| 1716 | check(create(&input) == S_OK && input, "GameInputCreate returns an instance"); | 1718 | check(create(&input) == S_OK && input, "GameInputCreate returns an instance"); |
| 1719 | if (argc == 2 && std::strcmp(argv[1], "--input-script") == 0) { | ||
| 1720 | if (!input) return 1; | ||
| 1721 | // Only the X after byte 63 is due now. Its later entry must not cancel it. | ||
| 1722 | set_env("LEONIDA_INPUT_SCRIPT", | ||
| 1723 | "start@3600,start@3600,start@3600,start@3600,start@3600,start@3600, X@0 \t,x@3600," | ||
| 1724 | "unknown@0,a@,a@x,,a@-0.1,a@nan,a@inf,a@1e999,a@0tail, ,"); | ||
| 1725 | set_env("LEONIDA_KEYBOARD_PAD", "1"); | ||
| 1726 | rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); | ||
| 1727 | rt::gameinput::set_cursor_source(fake_cursor); | ||
| 1728 | g_focus = true; | ||
| 1729 | g_keys[VK_BACK] = true; | ||
| 1730 | rt::gameinput::reload_config(); | ||
| 1731 | rt::gameinput::poll(); | ||
| 1732 | const auto buttons = pad_state(input, nullptr).buttons; | ||
| 1733 | const auto expected = GameInputGamepadX | GameInputGamepadB; | ||
| 1734 | check((buttons & expected) == expected, "long script: the spaced X after byte 63 and the physical B are held"); | ||
| 1735 | check((buttons & ~expected) == 0, "malformed script entries hold no extra buttons"); | ||
| 1736 | g_focus = false; | ||
| 1737 | set_env("LEONIDA_INPUT_SCRIPT", "y@0"); | ||
| 1738 | rt::gameinput::reload_config(); | ||
| 1739 | rt::gameinput::poll(); | ||
| 1740 | check(pad_state(input, nullptr).buttons == GameInputGamepadX, | ||
| 1741 | "scripted X survives focus loss; reload does not replace it with Y"); | ||
| 1742 | Sleep(350); | ||
| 1743 | g_focus = true; | ||
| 1744 | rt::gameinput::poll(); | ||
| 1745 | check(pad_state(input, nullptr).buttons == GameInputGamepadB, | ||
| 1746 | "long script: the timed press ends while the physical key stays held"); | ||
| 1747 | input->Release(); | ||
| 1748 | std::printf("%d checks, %d failures\n", g_checks, g_failures); | ||
| 1749 | return g_failures ? 1 : 0; | ||
| 1750 | } | ||
| 1717 | check(create(nullptr) == E_POINTER, "GameInputCreate(nullptr) is E_POINTER"); | 1751 | check(create(nullptr) == E_POINTER, "GameInputCreate(nullptr) is E_POINTER"); |
| 1718 | IUnknown* unk = nullptr; | 1752 | IUnknown* unk = nullptr; |
| 1719 | check(input->QueryInterface(IID_IGameInput, reinterpret_cast<void**>(&unk)) == S_OK && unk == input, | 1753 | check(input->QueryInterface(IID_IGameInput, reinterpret_cast<void**>(&unk)) == S_OK && unk == input, |