From f9ba70b4de394f803273ab60adcbef9b15849ab8 Mon Sep 17 00:00:00 2001 From: Moriz82 <72836413+Moriz82@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:57:22 -0600 Subject: [PATCH] gameinput: keyboard pad input scripts of any length (RT-INPUT-SCRIPT-LENGTH) Main had no keyboard pad script scheduler. Add timed button presses with a full-length environment reader so entries beyond byte 63 reach the pad. Combine scheduled and physical presses. Reject malformed entries and report their count when the script is read on the first pad update. Build gta6rt and gameinput_test. Exercise a long script, rejected items, focus loss, configuration reload and release under Wine. Restore only the old environment reader to reproduce the missing presses. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> AI-assisted (Codex and Claude Code agents). Co-Authored-By: Claude Fable 5.2 --- docs/gameinput.md | 7 +-- runtime/CMakeLists.txt | 4 +- runtime/shims/gameinput.cpp | 79 ++++++++++++++++++++++++++++++--- runtime/test/gameinput_test.cpp | 36 ++++++++++++++- 4 files changed, 115 insertions(+), 11 deletions(-) diff --git a/docs/gameinput.md b/docs/gameinput.md index 39a4fd2cc6..28115d1031 100644 --- a/docs/gameinput.md +++ b/docs/gameinput.md @@ -125,9 +125,9 @@ owns the game's window, which is the title's, not the shim's. Keys are read with `GetAsyncKeyState`, which needs no window or message loop (SDL's keyboard is part of its video subsystem, which this build leaves out) and works under Wine. With `LEONIDA_KEYBOARD_PAD=1` keys count only while a -window of this process is in the foreground, so typing in another application presses nothing, and every button, -stick and trigger is released when focus moves away (the cursor is read afresh when focus returns, so the right -stick does not jump). `LEONIDA_KEYBOARD_PAD=always` reads them whatever has focus; a console tool such as +window of this process is in the foreground. Losing focus releases keyboard and mouse input. The cursor is read +afresh on return so the right stick does not jump. Scripted presses run with or without focus for unattended runs. +`LEONIDA_KEYBOARD_PAD=always` reads physical input whatever has focus; a console tool such as `gameinput_probe` needs it, because its console window belongs to the terminal, not to the tool. - Force feedback and haptics run through a fake haptic backend (`rt::gameinput::set_haptic_source`), because SDL's virtual joysticks report no haptics: the motor count, axes and effect flags a wheel reports, and a pad without SDL @@ -302,6 +302,7 @@ line is logged when it takes effect); tracking goes on underneath, so clearing i | `LEONIDA_PROMPTS_SWITCH_MS` | `300` | shortest time between two changes of the active input family, 0..2000 ms | | `LEONIDA_GAMEINPUT_THREAD` | `1` | `0` stops the background poll thread; only `rt::gameinput::poll()` then reads the pads (tests) | | `LEONIDA_KEYBOARD_PAD` | `off` | `1` adds the [keyboard pad](#keyboard-pad), read while this process has focus; `always` reads keys whatever has focus | +| `LEONIDA_INPUT_SCRIPT` | unset | With the keyboard pad enabled, set comma-separated `button@seconds` entries, e.g. `a@2,a@4,start@6`. The first pad update reads the script once at startup and starts the clock. `reload_config()` does not reread it. Each press lasts 300 ms. Buttons are `a b x y start back lb rb up down left right`. Spaces around entries are allowed. Empty entries, unknown buttons and invalid times (including negative or non-finite values) are counted and ignored. | | `LEONIDA_MOUSE_SENSITIVITY` | `1` | scales how far the mouse moves the keyboard pad's right stick, 0.1..10 | SDL's own hints (`SDL_JOYSTICK_*`, see SDL's `SDL_hints.h`) also apply, for example `SDL_JOYSTICK_HIDAPI_PS5=0` to diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3b334c7133..c67a42958c 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1328,10 +1328,12 @@ if(BUILD_TESTING) add_test(NAME active_input COMMAND active_input_test) if(WIN32 AND TARGET gameinput_test) add_test(NAME gameinput COMMAND gameinput_test) + add_test(NAME gameinput_script COMMAND gameinput_test --input-script) elseif(TARGET gta6rt) # Built without SDL3: listed as "Not Run (Disabled)" so it is visible that the shim was not tested. add_test(NAME gameinput COMMAND ${CMAKE_COMMAND} -E echo "built without SDL3") - set_tests_properties(gameinput PROPERTIES DISABLED TRUE) + add_test(NAME gameinput_script COMMAND ${CMAKE_COMMAND} -E echo "built without SDL3") + set_tests_properties(gameinput gameinput_script PROPERTIES DISABLED TRUE) endif() if(WIN32 AND TARGET gta6rt AND TARGET tiny_game) if(TARGET tiny_argv_default_a AND NOT CMAKE_CROSSCOMPILING) diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 402179cde8..7650b361ff 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include "gameinput.h" @@ -132,10 +133,14 @@ const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); } // ---- configuration ---------------------------------------------------------------------------------------------- std::string env(const char* name) { - char buf[64]; - const DWORD n = GetEnvironmentVariableA(name, buf, sizeof buf); - if (n == 0 || n >= sizeof buf) return {}; - std::string s(buf, n); + // Any length: an input script (LEONIDA_INPUT_SCRIPT) is easily longer than a fixed buffer, and a value that did + // not fit used to read as unset. + const DWORD need = GetEnvironmentVariableA(name, nullptr, 0); // with the terminator + if (need == 0) return {}; + std::string s(need, '\0'); + const DWORD n = GetEnvironmentVariableA(name, s.data(), need); + if (n == 0 || n >= need) return {}; + s.resize(n); for (auto& c : s) c = static_cast(std::tolower(static_cast(c))); return s; } @@ -1861,8 +1866,8 @@ void update_keyboard_pad() { bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; int lx = 0, ly = 0; bool aim = false, fire = false; - // Out of focus every button, stick and trigger is released, so a key held while switching windows does not stay - // pressed, and the cursor is read afresh on return, so moving it in another window does not swing the stick. + // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump. + // Scripted presses still run while the window is unfocused. const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()(); if (active) { const rt::gameinput::KeyDownFn key_down = g_key_down.load(); @@ -1890,6 +1895,68 @@ void update_keyboard_pad() { } clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); + // Timed presses on the keyboard pad, in seconds since its first update. Each press lasts 300 ms. + struct ScriptedPress { + SDL_GamepadButton button; + double at; + }; + static const std::vector script = [] { + std::vector out; + static const std::pair names[] = { + {"a", SDL_GAMEPAD_BUTTON_SOUTH}, {"b", SDL_GAMEPAD_BUTTON_EAST}, + {"x", SDL_GAMEPAD_BUTTON_WEST}, {"y", SDL_GAMEPAD_BUTTON_NORTH}, + {"start", SDL_GAMEPAD_BUTTON_START}, {"back", SDL_GAMEPAD_BUTTON_BACK}, + {"lb", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER}, {"rb", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER}, + {"up", SDL_GAMEPAD_BUTTON_DPAD_UP}, {"down", SDL_GAMEPAD_BUTTON_DPAD_DOWN}, + {"left", SDL_GAMEPAD_BUTTON_DPAD_LEFT}, {"right", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}}; + const std::string text = env("LEONIDA_INPUT_SCRIPT"); + if (text.empty()) return out; + constexpr char whitespace[] = " \t\r\n\f\v"; + size_t items = 0; + for (size_t at = 0, end; at <= text.size(); at = end + 1) { + ++items; + end = std::min(text.find(',', at), text.size()); + const std::string item = text.substr(at, end - at); + const size_t first = item.find_first_not_of(whitespace); + const size_t sep = item.find('@'); + if (sep == std::string::npos || first >= sep) continue; + const char* time = item.c_str() + sep + 1; + char* tail; + const double seconds = std::strtod(time, &tail); + if (tail == time || !std::isfinite(seconds) || seconds < 0 || + item.find_first_not_of(whitespace, tail - item.c_str()) != std::string::npos) continue; + for (const auto& [name, button] : names) { + if (item.compare(first, sep - first, name) == 0) { + out.push_back({button, seconds}); + break; + } + } + } + std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n", + out.size(), items - out.size()); + return out; + }(); + if (!script.empty()) { + static const ULONGLONG origin = GetTickCount64(); + const double now = (GetTickCount64() - origin) / 1000.0; + // One decision per button: a later entry for the same button must not release what an earlier entry holds + // (it did: with several entries only the last one ever reached the title). + bool scripted[SDL_GAMEPAD_BUTTON_COUNT] = {}; + bool named[SDL_GAMEPAD_BUTTON_COUNT] = {}; + for (const auto& press : script) { + named[press.button] = true; + if (now >= press.at && now < press.at + 0.3) scripted[press.button] = true; + } + for (int button = 0; button < SDL_GAMEPAD_BUTTON_COUNT; ++button) { + if (!named[button]) continue; + SDL_SetJoystickVirtualButton(g_keyboard.js, static_cast(button), + scripted[button] || down[button]); + static bool was[SDL_GAMEPAD_BUTTON_COUNT] = {}; + if (scripted[button] && !was[button]) + std::fprintf(stderr, "[rt] gameinput: scripted press of button %d at %.1f s\n", button, now); + was[button] = scripted[button]; + } + } const int reach = lx && ly ? kStickDiagonal : kStickFull; SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast(lx * reach)); SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast(ly * reach)); diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 93c48518b2..b6658f51cd 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -1693,12 +1693,14 @@ void test_haptic_motors(IGameInput* input) { } // namespace -int main() { +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, SDL_HINT_JOYSTICK_GAMEINPUT, SDL_HINT_XINPUT_ENABLED, SDL_HINT_JOYSTICK_DIRECTINPUT}) SDL_SetHintWithPriority(hint, "0", SDL_HINT_OVERRIDE); set_env("LEONIDA_GAMEINPUT_THREAD", "0"); + set_env("LEONIDA_KEYBOARD_PAD", nullptr); + set_env("LEONIDA_INPUT_SCRIPT", nullptr); set_env("LEONIDA_DS_TRIGGERS", nullptr); set_env("LEONIDA_DS_LIGHTBAR", nullptr); set_env("LEONIDA_RUMBLE", nullptr); @@ -1714,6 +1716,38 @@ int main() { IGameInput* input = nullptr; check(create(&input) == S_OK && input, "GameInputCreate returns an instance"); + if (argc == 2 && std::strcmp(argv[1], "--input-script") == 0) { + if (!input) return 1; + // Only the X after byte 63 is due now. Its later entry must not cancel it. + set_env("LEONIDA_INPUT_SCRIPT", + "start@3600,start@3600,start@3600,start@3600,start@3600,start@3600, X@0 \t,x@3600," + "unknown@0,a@,a@x,,a@-0.1,a@nan,a@inf,a@1e999,a@0tail, ,"); + set_env("LEONIDA_KEYBOARD_PAD", "1"); + rt::gameinput::set_keyboard_source(fake_key_down, fake_focus); + rt::gameinput::set_cursor_source(fake_cursor); + g_focus = true; + g_keys[VK_BACK] = true; + rt::gameinput::reload_config(); + rt::gameinput::poll(); + const auto buttons = pad_state(input, nullptr).buttons; + const auto expected = GameInputGamepadX | GameInputGamepadB; + check((buttons & expected) == expected, "long script: the spaced X after byte 63 and the physical B are held"); + check((buttons & ~expected) == 0, "malformed script entries hold no extra buttons"); + g_focus = false; + set_env("LEONIDA_INPUT_SCRIPT", "y@0"); + rt::gameinput::reload_config(); + rt::gameinput::poll(); + check(pad_state(input, nullptr).buttons == GameInputGamepadX, + "scripted X survives focus loss; reload does not replace it with Y"); + Sleep(350); + g_focus = true; + rt::gameinput::poll(); + check(pad_state(input, nullptr).buttons == GameInputGamepadB, + "long script: the timed press ends while the physical key stays held"); + input->Release(); + std::printf("%d checks, %d failures\n", g_checks, g_failures); + return g_failures ? 1 : 0; + } check(create(nullptr) == E_POINTER, "GameInputCreate(nullptr) is E_POINTER"); IUnknown* unk = nullptr; check(input->QueryInterface(IID_IGameInput, reinterpret_cast(&unk)) == S_OK && unk == input,