upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput_script.h
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 07:39:13 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 07:39:13 -0500
commitc68f494160c8e79c76bfa109b2e99571a5c11358 (patch)
treedd03d57136b41a752dfcf4a7a2f0befc670bfbe3 /runtime/shims/gameinput_script.h
parent0d80bc5df7c873f40965f34f185cb96913786765 (diff)
RT-INPUT-SCRIPT-NO-SDL3: serve LEONIDA_INPUT_SCRIPT without SDL3
A build without SDL3 answered GameInputCreate with no devices and E_NOTIMPL for every other slot, so the title waited at its first "press A" prompt forever: no CI artifact or offline build could get past it. With LEONIDA_INPUT_SCRIPT set, gameinput_no_sdl.cpp now serves one scripted pad (an Xbox One gamepad) driven by the script alone: device enumeration through RegisterDeviceCallback (blocking, async, dispatcher), FindDeviceFromId, and GetCurrentReading/GetNextReading/GetPreviousReading whose gamepad and UI navigation state follow the script. Readings are taken when the title asks, so no thread polls. Without the variable it behaves as before. The parser, the 300 ms timeline, the button names, the press log lines and the UI navigation mapping move from gameinput.cpp into gameinput_script.h, which both shims use; nothing in it needs SDL. gameinput.cpp checks the shared SDL button numbers against SDL_GamepadButton at compile time. gameinput_no_sdl_test --input-script (CTest gameinput_no_sdl_script) checks the scripted pad on a fake clock. Docs: runtime/README.md, docs/gameinput.md, docs/linux_proton.md, docs/README.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput_script.h')
-rw-r--r--runtime/shims/gameinput_script.h180
1 files changed, 180 insertions, 0 deletions
diff --git a/runtime/shims/gameinput_script.h b/runtime/shims/gameinput_script.h
new file mode 100644
index 000000000..759506750
--- /dev/null
+++ b/runtime/shims/gameinput_script.h
@@ -0,0 +1,180 @@
1// LEONIDA_INPUT_SCRIPT, shared by both GameInput shims: the SDL3 one (gameinput.cpp, where the script drives the
2// keyboard pad) and the one built without SDL3 (gameinput_no_sdl.cpp, where it drives a scripted pad of its own).
3// The parser, the press timeline, the button names and the press log lines live here, once; nothing in this file
4// needs SDL. docs/gameinput.md, "Scripted input", has the format.
5//
6// LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120"
7//
8// Each entry is button@seconds, the seconds counted from the shim's first update. A press is held for kHoldSeconds,
9// then released. Buttons: a b x y start back lb rb up down left right; rsup and rsdown throw the right stick fully up
10// or down for the same time (they replay the stick throws the press log writes). Spaces around an entry are allowed;
11// empty entries, unknown buttons and invalid times (negative, non-finite, trailing text) are counted as rejected.
12#pragma once
13#include <algorithm>
14#include <cctype>
15#include <cmath>
16#include <cstdint>
17#include <cstdio>
18#include <cstdlib>
19#include <string>
20#include <vector>
21
22#include "gameinput_abi.h"
23
24namespace rt::gameinput::script {
25
26constexpr double kHoldSeconds = 0.3;
27
28// One scriptable button: its name in the script and the press log, its GameInput gamepad bit, and its SDL3
29// SDL_GamepadButton number, which the "scripted press of button <n>" line has always carried (tools/stage_timeline.py
30// reads it). gameinput.cpp checks the numbers against SDL's enum.
31struct Button {
32 const char* name;
33 uint32_t bit;
34 int sdl;
35};
36inline constexpr Button kButtons[] = {
37 {"a", gi::GameInputGamepadA, 0}, {"b", gi::GameInputGamepadB, 1},
38 {"x", gi::GameInputGamepadX, 2}, {"y", gi::GameInputGamepadY, 3},
39 {"start", gi::GameInputGamepadMenu, 6}, {"back", gi::GameInputGamepadView, 4},
40 {"lb", gi::GameInputGamepadLeftShoulder, 9}, {"rb", gi::GameInputGamepadRightShoulder, 10},
41 {"up", gi::GameInputGamepadDPadUp, 11}, {"down", gi::GameInputGamepadDPadDown, 12},
42 {"left", gi::GameInputGamepadDPadLeft, 13}, {"right", gi::GameInputGamepadDPadRight, 14},
43};
44
45struct Press {
46 uint32_t button = 0; // a GameInput gamepad bit, or 0 for a stick throw
47 int stick = 0; // +1 rsup, -1 rsdown (GameInput's +Y is up)
48 double at = 0; // seconds
49};
50
51struct Script {
52 std::vector<Press> presses;
53 size_t rejected = 0;
54 uint32_t named = 0; // every button the script presses at least once
55 bool empty() const { return presses.empty(); }
56};
57
58// The script in `text`, matched without regard to case.
59inline Script parse(std::string text) {
60 for (auto& c : text) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
61 Script out;
62 if (text.empty()) return out;
63 constexpr char whitespace[] = " \t\r\n\f\v";
64 size_t items = 0;
65 for (size_t at = 0, end; at <= text.size(); at = end + 1) {
66 ++items;
67 end = std::min(text.find(',', at), text.size());
68 const std::string item = text.substr(at, end - at);
69 const size_t first = item.find_first_not_of(whitespace);
70 const size_t sep = item.find('@');
71 if (sep == std::string::npos || first >= sep) continue;
72 const char* time = item.c_str() + sep + 1;
73 char* tail;
74 const double seconds = std::strtod(time, &tail);
75 if (tail == time || !std::isfinite(seconds) || seconds < 0 ||
76 item.find_first_not_of(whitespace, static_cast<size_t>(tail - item.c_str())) != std::string::npos)
77 continue;
78 const auto is = [&](const char* name) { return item.compare(first, sep - first, name) == 0; };
79 if (is("rsup") || is("rsdown")) {
80 out.presses.push_back({0, is("rsup") ? 1 : -1, seconds});
81 continue;
82 }
83 for (const Button& b : kButtons) {
84 if (is(b.name)) {
85 out.presses.push_back({b.bit, 0, seconds});
86 out.named |= b.bit;
87 break;
88 }
89 }
90 }
91 out.rejected = items - out.presses.size();
92 return out;
93}
94
95// What the script holds `seconds` after its clock started: the buttons (GameInput bits) and the right stick throw.
96// One decision per button: a later entry for the same button must not release what an earlier entry holds (it did:
97// with several entries only the last one ever reached the title).
98struct Held {
99 uint32_t buttons = 0;
100 int stick = 0; // +1 up, -1 down, 0 at rest
101};
102inline Held held_at(const Script& script, double seconds) {
103 Held h;
104 for (const Press& p : script.presses) {
105 if (seconds < p.at || seconds >= p.at + kHoldSeconds) continue;
106 if (p.stick) h.stick = p.stick;
107 else h.buttons |= p.button;
108 }
109 return h;
110}
111
112inline const Button* button_of(uint32_t bit) {
113 for (const Button& b : kButtons)
114 if (b.bit == bit) return &b;
115 return nullptr;
116}
117
118// "[rt] gameinput: scripted press of button <n> at <t> s" for every button the script starts holding: `was` is the
119// previous call's held set and is updated.
120inline void log_scripted(uint32_t held, uint32_t* was, double seconds) {
121 for (const Button& b : kButtons)
122 if ((held & b.bit) && !(*was & b.bit))
123 std::fprintf(stderr, "[rt] gameinput: scripted press of button %d at %.1f s\n", b.sdl, seconds);
124 *was = held;
125}
126
127// The press log (LEONIDA_INPUT_LOG, on unless 0): every newly pressed button and every new right-stick throw in the
128// script's own form, name@seconds, so a session played by hand can be replayed by joining the items with commas.
129inline int stick_throw(float y) { return y > 0.6f ? 1 : y < -0.6f ? -1 : 0; }
130inline void log_presses(uint32_t pressed, float right_y_was, float right_y, double seconds, const char* device) {
131 for (const Button& b : kButtons)
132 if (pressed & b.bit) std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", b.name, seconds, device);
133 const int was = stick_throw(right_y_was), is = stick_throw(right_y);
134 if (is != was && is != 0)
135 std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", is > 0 ? "rsup" : "rsdown", seconds, device);
136}
137
138// UI navigation derived from a gamepad state, the same for every pad (VERIFY against the console's own mapping).
139inline uint32_t ui_navigation(const gi::GameInputGamepadState& g) {
140 using namespace gi;
141 const uint32_t b = g.buttons;
142 const float t = 0.5f;
143 uint32_t u = 0;
144 if (b & GameInputGamepadMenu) u |= GameInputUiNavigationMenu;
145 if (b & GameInputGamepadView) u |= GameInputUiNavigationView;
146 if (b & GameInputGamepadA) u |= GameInputUiNavigationAccept;
147 if (b & GameInputGamepadB) u |= GameInputUiNavigationCancel;
148 if (b & GameInputGamepadX) u |= GameInputUiNavigationContext1;
149 if (b & GameInputGamepadY) u |= GameInputUiNavigationContext2;
150 if (b & GameInputGamepadLeftThumbstick) u |= GameInputUiNavigationContext3;
151 if (b & GameInputGamepadRightThumbstick) u |= GameInputUiNavigationContext4;
152 if ((b & GameInputGamepadDPadUp) || g.leftThumbstickY > t) u |= GameInputUiNavigationUp;
153 if ((b & GameInputGamepadDPadDown) || g.leftThumbstickY < -t) u |= GameInputUiNavigationDown;
154 if ((b & GameInputGamepadDPadLeft) || g.leftThumbstickX < -t) u |= GameInputUiNavigationLeft;
155 if ((b & GameInputGamepadDPadRight) || g.leftThumbstickX > t) u |= GameInputUiNavigationRight;
156 if (b & GameInputGamepadLeftShoulder) u |= GameInputUiNavigationPageLeft;
157 if (b & GameInputGamepadRightShoulder) u |= GameInputUiNavigationPageRight;
158 if (g.leftTrigger > t) u |= GameInputUiNavigationPageUp;
159 if (g.rightTrigger > t) u |= GameInputUiNavigationPageDown;
160 if (g.rightThumbstickY > t) u |= GameInputUiNavigationScrollUp;
161 if (g.rightThumbstickY < -t) u |= GameInputUiNavigationScrollDown;
162 if (g.rightThumbstickX < -t) u |= GameInputUiNavigationScrollLeft;
163 if (g.rightThumbstickX > t) u |= GameInputUiNavigationScrollRight;
164 return u;
165}
166
167// An environment variable in lower case, of any length (a script is easily longer than a fixed buffer, and a value
168// that did not fit used to read as unset). Empty when unset.
169inline std::string env_lower(const char* name) {
170 const DWORD need = GetEnvironmentVariableA(name, nullptr, 0); // with the terminator
171 if (need == 0) return {};
172 std::string s(need, '\0');
173 const DWORD n = GetEnvironmentVariableA(name, s.data(), need);
174 if (n == 0 || n >= need) return {};
175 s.resize(n);
176 for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
177 return s;
178}
179
180} // namespace rt::gameinput::script