Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/gameinput.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ scripted pad, driven by the script alone:
controller axis and controller button state. A reading is taken when the title asks for one and the script's state
has changed since the last, so no thread polls; the history keeps the last 64 readings.
- **The timeline:** the same as with SDL3 (`runtime/shims/gameinput_script.h` holds the parser, the timeline and the
log lines for both): seconds from the first `GameInputCreate`, each press held 300 ms, then released.
log lines for both): seconds from the first `GameInputCreate`, each press held 300 ms (or its own `:hold`, see
`LEONIDA_INPUT_SCRIPT` below), then released.
- **Not served:** reading callbacks, aggregate devices, rumble and every other device. Those slots answer `E_NOTIMPL`
and log the slot once. Controllers and the [keyboard pad](#keyboard-pad) still need SDL3.

Expand Down Expand Up @@ -335,7 +336,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 | Comma-separated `button@seconds` entries, e.g. `a@2,a@4,start@6`. With SDL3 they drive the keyboard pad, which has to be enabled; without SDL3 they drive [one scripted pad](#scripted-input-without-sdl3), the only device. The first pad update (without SDL3, the first `GameInputCreate`) 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_INPUT_SCRIPT` | unset | Comma-separated `button@seconds` entries, e.g. `a@2,a@4,start@6`. With SDL3 they drive the keyboard pad, which has to be enabled; without SDL3 they drive [one scripted pad](#scripted-input-without-sdl3), the only device. The first pad update (without SDL3, the first `GameInputCreate`) reads the script once at startup and starts the clock. `reload_config()` does not reread it. Each press lasts 300 ms, or `hold` seconds when the entry is `button@seconds:hold` (more than 0, at most 600). With SDL3 a press then stays down until the title has read the pads six times since it began, but never past 5 s from its start (or its hold, if longer). A title reads its pad once a frame, so at a few seconds a frame a 300 ms press can fall between two reads and never arrive: on the retail title's "Couldn't autosave" prompt, three 300 ms `b` presses did nothing, while 120 presses 0.5 s apart got through; one `b@1100:3` replaces them. Buttons are `a b x y start back lb rb up down left right`. Spaces around entries are allowed. Empty entries, unknown buttons, invalid times (including negative or non-finite values) and invalid holds (0 or less, more than 600, not a number) 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
Expand Down
2 changes: 1 addition & 1 deletion runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_POOL_DIAG_AT` | unset | *Diagnostic.* `<module>+0x<offset>` of the title's transient-pool assert, as the trace prints it (measured locally): the trace dumps the pool's resource list when that int3 is hit (`shims/startup_trace.h`). |
| `LEONIDA_PEEK` | unset | *Diagnostic.* `<rva>[:n],<rva>,...` (hex): a thread prints the 8-byte values at these RVAs of the title's image (`:n` = n consecutive values) every `LEONIDA_PEEK_SECONDS` (`src/main.cpp`). The RVAs are measured locally and not kept in the repository. `scripts/windows-boot/boot.ps1` no longer sets the game-state and session-state RVAs by default: it sets `LEONIDA_PEEK` only from `LEONIDA_GAME_STATE_RVA` and `LEONIDA_SESSION_STATE_RVA`, and without them its RESULT states read `?` ([scripts/windows-boot/README.md](../scripts/windows-boot/README.md)). |
| `LEONIDA_PEEK_SECONDS` | `10` | *Diagnostic.* The period of the `LEONIDA_PEEK` thread (`src/main.cpp`), which prints 8-byte values of the title's image. |
| `LEONIDA_INPUT_SCRIPT` | unset | Timed pad presses, `button@seconds` separated by commas (`a@70,a@85,start@120`), each held 300 ms; the clock starts with the shim. Works with and without SDL3: with SDL3 it drives the keyboard pad (`LEONIDA_KEYBOARD_PAD=always`), without SDL3 one scripted pad that is the only device. Controllers and the keyboard pad still need SDL3. Format and buttons in [docs/gameinput.md](../docs/gameinput.md#environment-variables). |
| `LEONIDA_INPUT_SCRIPT` | unset | Timed pad presses, `button@seconds` separated by commas (`a@70,a@85,start@120`), each held 300 ms or its own `button@seconds:hold`; the clock starts with the shim. Works with and without SDL3: with SDL3 it drives the keyboard pad (`LEONIDA_KEYBOARD_PAD=always`), without SDL3 one scripted pad that is the only device. Controllers and the keyboard pad still need SDL3. Format and buttons in [docs/gameinput.md](../docs/gameinput.md#environment-variables). |
| `LEONIDA_INPUT_LOG` | on | The GameInput shim logs every press in `LEONIDA_INPUT_SCRIPT`'s form (`name@seconds`), so a session played by hand can be replayed. `0` turns the lines off. |
| `LEONIDA_LOG_ALLOCINFO` | unset | *Diagnostic.* `1` logs every device slot 25 call (`GetResourceAllocationInfo`) with its thread, description and sizes (`shims/d3d12_xs_device.cpp`). |
| `LEONIDA_LOG_EXE_PIPELINES` | unset | *Diagnostic.* When set, one line per distinct graphics pipeline that uses a program translated from the executable's inline containers. |
Expand Down
8 changes: 5 additions & 3 deletions runtime/shims/gameinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,9 @@ 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]);
// LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard
// pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt
// pad, in seconds since the pad's first update, each held 300 ms or its own button@seconds:hold, and then until the
// title has read the pads kReads times since it began (never past kMaxHold seconds, or the hold if it is longer, so
// a title that stops reading does not keep the button). For unattended boots: the title's first prompt
// waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the
// button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3.
namespace script = rt::gameinput::script;
Expand All @@ -1911,12 +1913,12 @@ void update_keyboard_pad() {
for (std::size_t i = 0; i < scripted.presses.size(); ++i) {
const auto& press = scripted.presses[i];
bool active = false;
if (now >= press.at && now < press.at + kMaxHold) {
if (now >= press.at && now < press.at + std::max(press.hold, kMaxHold)) {
if (!started[i]) {
started[i] = 1;
began[i] = calls;
}
active = now < press.at + 0.3 || calls - began[i] < kReads;
active = now < press.at + press.hold || calls - began[i] < kReads;
}
if (!active) continue;
if (press.stick) held.stick = press.stick;
Expand Down
30 changes: 20 additions & 10 deletions runtime/shims/gameinput_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
//
// LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120"
//
// Each entry is button@seconds, the seconds counted from the shim's first update. A press is held for kHoldSeconds,
// then released. Buttons: a b x y start back lb rb up down left right; rsup and rsdown throw the right stick fully up
// or down for the same time (they replay the stick throws the press log writes). Spaces around an entry are allowed;
// empty entries, unknown buttons and invalid times (negative, non-finite, trailing text) are counted as rejected.
// Each entry is button@seconds[:hold], the seconds counted from the shim's first update. A press is held for `hold`
// seconds (kHoldSeconds when the entry has none), then released: a longer hold reaches a title that reads its pad
// only every few seconds, as the retail title does at world-start frame rates. Buttons: a b x y start back lb rb
// up down left right; rsup and rsdown throw the right stick fully up or down for the same time (they replay the
// stick throws the press log writes). Spaces around an entry are allowed; empty entries, unknown buttons, invalid
// times (negative, non-finite, trailing text) and invalid holds (zero, negative, non-finite, more than
// kMaxHoldSeconds) are counted as rejected.
#pragma once
#include <algorithm>
#include <cctype>
Expand All @@ -24,6 +27,7 @@
namespace rt::gameinput::script {

constexpr double kHoldSeconds = 0.3;
constexpr double kMaxHoldSeconds = 600; // a typo such as b@10:1100 should not hold B for the whole run

// One scriptable button: its name in the script and the press log, its GameInput gamepad bit, and its SDL3
// SDL_GamepadButton number, which the "scripted press of button <n>" line has always carried (tools/stage_timeline.py
Expand All @@ -46,6 +50,7 @@ struct Press {
uint32_t button = 0; // a GameInput gamepad bit, or 0 for a stick throw
int stick = 0; // +1 rsup, -1 rsdown (GameInput's +Y is up)
double at = 0; // seconds
double hold = kHoldSeconds;
};

struct Script {
Expand All @@ -72,17 +77,22 @@ inline Script parse(std::string text) {
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, static_cast<size_t>(tail - item.c_str())) != std::string::npos)
continue;
if (tail == time || !std::isfinite(seconds) || seconds < 0) continue;
double hold = kHoldSeconds;
if (*tail == ':') {
const char* hold_text = tail + 1;
hold = std::strtod(hold_text, &tail);
if (tail == hold_text || !std::isfinite(hold) || hold <= 0 || hold > kMaxHoldSeconds) continue;
}
if (item.find_first_not_of(whitespace, static_cast<size_t>(tail - item.c_str())) != std::string::npos) continue;
const auto is = [&](const char* name) { return item.compare(first, sep - first, name) == 0; };
if (is("rsup") || is("rsdown")) {
out.presses.push_back({0, is("rsup") ? 1 : -1, seconds});
out.presses.push_back({0, is("rsup") ? 1 : -1, seconds, hold});
continue;
}
for (const Button& b : kButtons) {
if (is(b.name)) {
out.presses.push_back({b.bit, 0, seconds});
out.presses.push_back({b.bit, 0, seconds, hold});
out.named |= b.bit;
break;
}
Expand All @@ -102,7 +112,7 @@ struct Held {
inline Held held_at(const Script& script, double seconds) {
Held h;
for (const Press& p : script.presses) {
if (seconds < p.at || seconds >= p.at + kHoldSeconds) continue;
if (seconds < p.at || seconds >= p.at + p.hold) continue;
if (p.stick) h.stick = p.stick;
else h.buttons |= p.button;
}
Expand Down
10 changes: 8 additions & 2 deletions runtime/test/gameinput_no_sdl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ Pad read_at(IGameInput* input, IGameInputDevice* device, double seconds) {
}

int scripted_pad(CreateFn create) {
// Four presses (two overlapping A presses, Menu, a right-stick throw) and three rejected entries.
SetEnvironmentVariableA("LEONIDA_INPUT_SCRIPT", "a@1, START@2.5 ,a@1.2,rsup@4,bogus@1,a@-1,");
// Five presses (two overlapping A presses, Menu, B held 0.8 s, a right-stick throw) and seven rejected entries
// (four of them holds that are zero, negative, missing or too long).
SetEnvironmentVariableA("LEONIDA_INPUT_SCRIPT",
"a@1, START@2.5 ,a@1.2,b@3:0.8 ,rsup@4,bogus@1,a@-1,,x@3:0,x@3:-1,x@3:,x@3:601");
rt::gameinput_no_sdl::set_clock(fake_clock);
at(0);

Expand Down Expand Up @@ -201,6 +203,10 @@ int scripted_pad(CreateFn create) {
check(menu.buttons == GameInputGamepadMenu && menu.ui == GameInputUiNavigationMenu,
"script: START (any case) holds Menu at 2.6 s");
check(read_at(input, device, 2.85).buttons == 0, "script: Menu is released at 2.85 s");
check(read_at(input, device, 3.05).buttons == GameInputGamepadB, "script: b@3:0.8 holds B at 3.05 s");
check(read_at(input, device, 3.7).buttons == GameInputGamepadB,
"script: B is still held at 3.7 s, past the 0.3 s default");
check(read_at(input, device, 3.85).buttons == 0, "script: B is released after its 0.8 s hold");
const Pad up = read_at(input, device, 4.1);
check(up.buttons == 0 && up.right_y == 1.0f && up.ui == GameInputUiNavigationScrollUp,
"script: rsup throws the right stick up at 4.1 s");
Expand Down
31 changes: 19 additions & 12 deletions runtime/test/gameinput_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,10 +1718,12 @@ int main(int argc, char** argv) {
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.
// 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
// malformed holds at the end hold nothing.
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, ,");
"unknown@0,a@,a@x,,a@-0.1,a@nan,a@inf,a@1e999,a@0tail, ,y@0:0.6,"
"a@0:0,a@0:-1,a@0:,a@0:x,a@0:inf,a@0:601,a@0:1tail");
set_env("LEONIDA_KEYBOARD_PAD", "1");
rt::gameinput::set_keyboard_source(fake_key_down, fake_focus);
rt::gameinput::set_cursor_source(fake_cursor);
Expand All @@ -1730,27 +1732,32 @@ int main(int argc, char** argv) {
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");
const auto expected = GameInputGamepadX | GameInputGamepadB | GameInputGamepadY;
check((buttons & expected) == expected,
"long script: the spaced X after byte 63, the held Y 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");
check(pad_state(input, nullptr).buttons == (GameInputGamepadX | GameInputGamepadY),
"scripted X and Y survive focus loss; reload does not replace the script with y@0");
Sleep(350);
g_focus = true;
rt::gameinput::poll();
// A scripted press lasts its 300 ms and then until the title has read the pads six times since it began
// (update_keyboard_pad: kReads, at most kMaxHold seconds), so a title that polls slowly still sees it. Each
// pad_state() is one GetCurrentReading, and this is the third since the X began.
check(pad_state(input, nullptr).buttons == (GameInputGamepadX | GameInputGamepadB),
"long script: past its 300 ms the press is still held while the title has read the pads fewer than six times");
// A scripted press lasts its hold (300 ms, or button@seconds:hold) and then until the title has read the pads
// six times since it began (update_keyboard_pad: kReads), so a title that polls slowly still sees it. Each
// pad_state() is one GetCurrentReading, and this is the third since X and Y began.
check(pad_state(input, nullptr).buttons == (GameInputGamepadX | GameInputGamepadB | GameInputGamepadY),
"long script: past its 300 ms X is still held while the title has read the pads fewer than six times");
for (int i = 0; i < 6; ++i) pad_state(input, nullptr);
rt::gameinput::poll();
check(pad_state(input, nullptr).buttons == (GameInputGamepadB | GameInputGamepadY),
"long script: X ends once the title has read it, y@0:0.6 is still held for its time, the key stays held");
Sleep(400);
rt::gameinput::poll();
check(pad_state(input, nullptr).buttons == GameInputGamepadB,
"long script: the timed press ends once the title has read it, while the physical key stays held");
"long script: Y is released after its 0.6 s hold, the title having read it six times");
input->Release();
std::printf("%d checks, %d failures\n", g_checks, g_failures);
return g_failures ? 1 : 0;
Expand Down
3 changes: 2 additions & 1 deletion scripts/windows-boot/boot.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@

.PARAMETER InputScript
LEONIDA_INPUT_SCRIPT: timed pad presses, seconds since the pad's first poll (button@seconds, comma-separated;
buttons a b x y start back lb rb up down left right). Sets LEONIDA_KEYBOARD_PAD=always. Builds older than
buttons a b x y start back lb rb up down left right). Each press is held 300 ms; button@seconds:hold holds it
longer, e.g. b@1100:3 for a title that reads its pad only every few seconds. Sets LEONIDA_KEYBOARD_PAD=always. Builds older than
main @ 8146ef690 ignore a script longer than 63 characters. Default 'a@45,a@60,a@75,a@90'.

.PARAMETER NoInput
Expand Down
Loading