mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 10:08:39 +00:00
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 main32a5d9698(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 plain32a5d9698too: 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).
This commit is contained in:
@ -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
|
||||
|
||||
@ -1339,10 +1339,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)
|
||||
|
||||
@ -47,6 +47,7 @@
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#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<char>(std::tolower(static_cast<unsigned char>(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<ScriptedPress> script = [] {
|
||||
std::vector<ScriptedPress> out;
|
||||
static const std::pair<const char*, SDL_GamepadButton> 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<SDL_GamepadButton>(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<Sint16>(lx * reach));
|
||||
SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach));
|
||||
|
||||
@ -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<void**>(&unk)) == S_OK && unk == input,
|
||||
|
||||
Reference in New Issue
Block a user