Skip to content
Closed
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
2 changes: 1 addition & 1 deletion docs/gameinput.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,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` | `1` | the [keyboard pad](#keyboard-pad), read while this process has focus; `always` reads keys whatever has focus; `off` removes it |
| `LEONIDA_INPUT_SCRIPT` | unset | Comma-separated `button@seconds` entries, e.g. `a@2,a@4,start@6`. With SDL3 they drive the keyboard pad (on unless `LEONIDA_KEYBOARD_PAD=off`); 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_INPUT_SCRIPT` | unset | Comma-separated `button@seconds` entries, e.g. `a@2,a@4,start@6`. A state-relative entry can use `button@game=<hex>+<seconds>` or `button@session=<hex>+<seconds>` (for example `a@session=d+5:3`); it starts its hold after the named state has been observed for that many seconds. State values are the low dword in hexadecimal. This form needs both locally supplied `LEONIDA_GAME_STATE_RVA` and `LEONIDA_SESSION_STATE_RVA`; when configured, the runtime samples them once a second. Without those values, relative entries stay inactive. With SDL3 scripts drive the keyboard pad (on unless `LEONIDA_KEYBOARD_PAD=off`); 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 has `: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
4 changes: 2 additions & 2 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_TITLE_WATCHDOG_KICK` | unset | `<trap RVA>,<kick RVA>` (hex, measured locally and not kept in the repository): where the watchdog's trap instruction is and the byte the game's main loop sets. Used only with `LEONIDA_TITLE_WATCHDOG=0`, and only when the watchdog's trap instruction is at the first RVA; set without `LEONIDA_TITLE_WATCHDOG=0`, the watchdog stays armed and the loader says so once. |
| `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 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_PEEK_SECONDS` | `10` | *Diagnostic.* The period of the `LEONIDA_PEEK` thread (`src/main.cpp`), which prints 8-byte values of the title's image; with both state RVAs configured, state-relative input sampling uses a 1 s period. |
| `LEONIDA_INPUT_SCRIPT` | unset | Timed pad presses, `button@seconds` or state-relative `button@game=<hex>+seconds` / `button@session=<hex>+seconds`, comma-separated; each held 300 ms or its own `:hold`. State-relative entries need both local state RVAs and sample once a second. 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_INPUT_TRACE` | unset | `1` (diagnostic, the GameInput shim built without SDL3) logs the title's reading calls on the scripted pad: the first 128, then each one with a button down and every 1000th. Details in [docs/gameinput.md](../docs/gameinput.md#scripted-input-without-sdl3). |
| `LEONIDA_LOG_ALLOCINFO` | unset | *Diagnostic.* `1` logs every device slot 25 call (`GetResourceAllocationInfo`) with its thread, description and sizes (`shims/d3d12_xs_device.cpp`). |
Expand Down
24 changes: 16 additions & 8 deletions runtime/shims/gameinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1990,11 +1990,15 @@ void update_keyboard_pad() {
}();
if (!scripted.empty() || !input_file.empty()) {
static const ULONGLONG origin = GetTickCount64();
const double now = (GetTickCount64() - origin) / 1000.0;
const ULONGLONG tick = GetTickCount64();
const uint64_t origin_us = origin * 1000;
const double now = (tick - origin) / 1000.0;
const script::TitleState title = script::current_title_state();
script::Held held{};
static constexpr uint64_t kReads = 6;
static constexpr double kMaxHold = 5.0;
static std::vector<uint64_t> began(scripted.presses.size(), 0);
static std::vector<double> scheduled(scripted.presses.size(), 0);
static std::vector<char> started(scripted.presses.size(), 0);
const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed);
if (!input_file.empty()) {
Expand Down Expand Up @@ -2035,14 +2039,14 @@ 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 + std::max(press.hold, kMaxHold)) {
if (!started[i]) {
started[i] = 1;
began[i] = calls;
}
active = now < press.at + press.hold || calls - began[i] < kReads;
if (!started[i]) {
const std::optional<double> due = script::start_at(press, origin_us, title);
if (!due || now < *due || now >= *due + std::max(press.hold, kMaxHold)) continue;
started[i] = 1;
scheduled[i] = *due;
began[i] = calls;
}
const bool active = now < scheduled[i] + press.hold || calls - began[i] < kReads;
if (!active) continue;
if (press.stick) held.stick = press.stick;
else held.buttons |= press.button;
Expand Down Expand Up @@ -2979,6 +2983,10 @@ void poll() {
poll_and_deliver();
}

void update_title_state(uint64_t game, uint64_t session, uint64_t observed_us) {
script::update_title_state(game, session, observed_us);
}

int connected_count() {
std::lock_guard lock(g().mu);
return static_cast<int>(g().connected.size());
Expand Down
3 changes: 3 additions & 0 deletions runtime/shims/gameinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const Config& config();
// Re-reads the environment (tests). Takes effect for the next rumble update.
void reload_config();

// Publishes the locally peeked title states for state-relative LEONIDA_INPUT_SCRIPT entries. Safe from any thread.
void update_title_state(uint64_t game, uint64_t session, uint64_t observed_us);

// One step of the backend, on the calling thread: initialise SDL if needed, open new gamepads, close removed ones,
// take a reading from each, apply pending rumble/trigger output, and deliver queued callbacks unless a dispatcher
// owns them. With the background thread on (the default) this runs every 2 ms by itself; with it off
Expand Down
48 changes: 45 additions & 3 deletions runtime/shims/gameinput_no_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <utility>
#include <vector>

#include "gameinput.h"
#include "gameinput_abi.h"
#include "gameinput_no_sdl.h"
#include "gameinput_script.h"
Expand Down Expand Up @@ -393,6 +394,9 @@ struct State {
script::Script script;
uint64_t origin_us = 0; // the script's clock starts at the first GameInputCreate
double updated_s = -1; // the script time of the last update; its changes up to here are in the history
std::vector<double> scheduled;
std::vector<bool> started;
bool script_loaded = false;
bool log_presses = true; // LEONIDA_INPUT_LOG
bool trace_reads = false; // LEONIDA_INPUT_TRACE
uint32_t logged_held = 0; // what log_scripted saw last
Expand All @@ -415,7 +419,14 @@ State& g() {
// Takes a reading when the script's state `seconds` into it differs from the current one. Under g().mu.
void take_reading(double seconds, uint64_t timestamp) {
State& s = g();
const script::Held held = script::held_at(s.script, seconds);
script::Held held;
for (size_t i = 0; i < s.script.presses.size(); ++i) {
if (!s.started[i]) continue;
const script::Press& press = s.script.presses[i];
if (seconds < s.scheduled[i] || seconds >= s.scheduled[i] + press.hold) continue;
if (press.stick) held.stick = press.stick;
else held.buttons |= press.button;
}
GameInputGamepadState state{};
state.buttons = held.buttons;
state.rightThumbstickY = static_cast<float>(held.stick);
Expand All @@ -441,11 +452,29 @@ void take_reading(double seconds, uint64_t timestamp) {
void update(uint64_t now) {
State& s = g();
const double seconds = now > s.origin_us ? (now - s.origin_us) / 1e6 : 0.0;
if (s.scheduled.size() != s.script.presses.size()) {
s.scheduled.assign(s.script.presses.size(), 0.0);
s.started.assign(s.script.presses.size(), false);
}
const script::TitleState title = script::current_title_state();
for (size_t i = 0; i < s.script.presses.size(); ++i) {
if (s.started[i]) continue;
const script::Press& press = s.script.presses[i];
const std::optional<double> due = script::start_at(press, s.origin_us, title);
if (due && seconds >= *due && seconds < *due + std::max(press.hold, script::kMaxHoldSeconds)) {
s.scheduled[i] = *due;
s.started[i] = true;
}
}
if (seconds > s.updated_s) {
std::vector<double> edges;
for (const script::Press& p : s.script.presses)
for (const double edge : {p.at, p.at + p.hold})
for (size_t i = 0; i < s.script.presses.size(); ++i) {
if (!s.started[i]) continue;
const double start = s.scheduled[i];
const double hold = s.script.presses[i].hold;
for (const double edge : {start, start + hold})
if (edge > s.updated_s && edge < seconds) edges.push_back(edge);
}
std::sort(edges.begin(), edges.end());
edges.erase(std::unique(edges.begin(), edges.end()), edges.end());
for (const double edge : edges) take_reading(edge, s.origin_us + static_cast<uint64_t>(edge * 1e6 + 0.5));
Expand Down Expand Up @@ -767,6 +796,9 @@ ScriptedInput* scripted() {
State& s = g();
std::lock_guard lock(s.mu);
s.script = script::parse(text);
s.script_loaded = true;
s.scheduled.assign(s.script.presses.size(), 0.0);
s.started.assign(s.script.presses.size(), false);
s.log_presses = script::env_lower("LEONIDA_INPUT_LOG") != "0";
s.trace_reads = script::env_lower("LEONIDA_INPUT_TRACE") == "1";
g_trace_readings = s.trace_reads;
Expand Down Expand Up @@ -799,6 +831,16 @@ HRESULT WINAPI GameInputCreate(void** out) {

} // namespace

namespace rt::gameinput {
void update_title_state(uint64_t game, uint64_t session, uint64_t observed_us) {
(void)observed_us; // the scripted pad's clock is QPC-based (and replaceable in tests)
script::update_title_state(game, session, now_us());
State& s = g();
std::lock_guard lock(s.mu);
if (s.script_loaded) update(now_us());
}
} // namespace rt::gameinput

namespace rt::gameinput_no_sdl {
void set_clock(ClockFn now) { g_clock.store(now ? now : &qpc_now_us); }
} // namespace rt::gameinput_no_sdl
Expand Down
Loading
Loading