diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 07:39:13 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 07:39:13 -0500 |
| commit | c68f494160c8e79c76bfa109b2e99571a5c11358 (patch) | |
| tree | dd03d57136b41a752dfcf4a7a2f0befc670bfbe3 /runtime/shims/gameinput.cpp | |
| parent | 0d80bc5df7c873f40965f34f185cb96913786765 (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.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 171 |
1 files changed, 47 insertions, 124 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index db685e71f..47e9aa941 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -52,6 +52,7 @@ | |||
| 52 | 52 | ||
| 53 | #include "gameinput.h" | 53 | #include "gameinput.h" |
| 54 | #include "gameinput_abi.h" | 54 | #include "gameinput_abi.h" |
| 55 | #include "gameinput_script.h" | ||
| 55 | #include "shims.h" | 56 | #include "shims.h" |
| 56 | 57 | ||
| 57 | using namespace gi; | 58 | using namespace gi; |
| @@ -132,18 +133,30 @@ const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); } | |||
| 132 | 133 | ||
| 133 | // ---- configuration ---------------------------------------------------------------------------------------------- | 134 | // ---- configuration ---------------------------------------------------------------------------------------------- |
| 134 | 135 | ||
| 135 | std::string env(const char* name) { | 136 | std::string env(const char* name) { return rt::gameinput::script::env_lower(name); } |
| 136 | // Any length: an input script (LEONIDA_INPUT_SCRIPT) is easily longer than a fixed buffer, and a value that did | 137 | |
| 137 | // not fit used to read as unset. | 138 | // The script's button numbers are SDL's (gameinput_script.h shares them with the shim built without SDL3). |
| 138 | const DWORD need = GetEnvironmentVariableA(name, nullptr, 0); // with the terminator | 139 | constexpr bool script_buttons_match_sdl() { |
| 139 | if (need == 0) return {}; | 140 | const struct { |
| 140 | std::string s(need, '\0'); | 141 | uint32_t bit; |
| 141 | const DWORD n = GetEnvironmentVariableA(name, s.data(), need); | 142 | SDL_GamepadButton sdl; |
| 142 | if (n == 0 || n >= need) return {}; | 143 | } expected[] = {{GameInputGamepadA, SDL_GAMEPAD_BUTTON_SOUTH}, {GameInputGamepadB, SDL_GAMEPAD_BUTTON_EAST}, |
| 143 | s.resize(n); | 144 | {GameInputGamepadX, SDL_GAMEPAD_BUTTON_WEST}, {GameInputGamepadY, SDL_GAMEPAD_BUTTON_NORTH}, |
| 144 | for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); | 145 | {GameInputGamepadMenu, SDL_GAMEPAD_BUTTON_START}, {GameInputGamepadView, SDL_GAMEPAD_BUTTON_BACK}, |
| 145 | return s; | 146 | {GameInputGamepadLeftShoulder, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER}, |
| 147 | {GameInputGamepadRightShoulder, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER}, | ||
| 148 | {GameInputGamepadDPadUp, SDL_GAMEPAD_BUTTON_DPAD_UP}, {GameInputGamepadDPadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN}, | ||
| 149 | {GameInputGamepadDPadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT}, | ||
| 150 | {GameInputGamepadDPadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT}}; | ||
| 151 | if (std::size(expected) != std::size(rt::gameinput::script::kButtons)) return false; | ||
| 152 | for (const auto& e : expected) { | ||
| 153 | bool found = false; | ||
| 154 | for (const auto& b : rt::gameinput::script::kButtons) found |= b.bit == e.bit && b.sdl == e.sdl; | ||
| 155 | if (!found) return false; | ||
| 156 | } | ||
| 157 | return true; | ||
| 146 | } | 158 | } |
| 159 | static_assert(script_buttons_match_sdl(), "gameinput_script.h's SDL button numbers drifted from SDL_GamepadButton"); | ||
| 147 | 160 | ||
| 148 | rt::gameinput::Config read_config() { | 161 | rt::gameinput::Config read_config() { |
| 149 | rt::gameinput::Config c; | 162 | rt::gameinput::Config c; |
| @@ -1103,30 +1116,7 @@ Snapshot read_pad(Device* d) { | |||
| 1103 | } | 1116 | } |
| 1104 | } | 1117 | } |
| 1105 | 1118 | ||
| 1106 | // UI navigation derived from the gamepad (VERIFY against the console's own mapping). | 1119 | s.ui = rt::gameinput::script::ui_navigation(s.gamepad); |
| 1107 | const uint32_t b = s.gamepad.buttons; | ||
| 1108 | const float t = 0.5f; | ||
| 1109 | uint32_t& u = s.ui; | ||
| 1110 | if (b & GameInputGamepadMenu) u |= GameInputUiNavigationMenu; | ||
| 1111 | if (b & GameInputGamepadView) u |= GameInputUiNavigationView; | ||
| 1112 | if (b & GameInputGamepadA) u |= GameInputUiNavigationAccept; | ||
| 1113 | if (b & GameInputGamepadB) u |= GameInputUiNavigationCancel; | ||
| 1114 | if (b & GameInputGamepadX) u |= GameInputUiNavigationContext1; | ||
| 1115 | if (b & GameInputGamepadY) u |= GameInputUiNavigationContext2; | ||
| 1116 | if (b & GameInputGamepadLeftThumbstick) u |= GameInputUiNavigationContext3; | ||
| 1117 | if (b & GameInputGamepadRightThumbstick) u |= GameInputUiNavigationContext4; | ||
| 1118 | if ((b & GameInputGamepadDPadUp) || s.gamepad.leftThumbstickY > t) u |= GameInputUiNavigationUp; | ||
| 1119 | if ((b & GameInputGamepadDPadDown) || s.gamepad.leftThumbstickY < -t) u |= GameInputUiNavigationDown; | ||
| 1120 | if ((b & GameInputGamepadDPadLeft) || s.gamepad.leftThumbstickX < -t) u |= GameInputUiNavigationLeft; | ||
| 1121 | if ((b & GameInputGamepadDPadRight) || s.gamepad.leftThumbstickX > t) u |= GameInputUiNavigationRight; | ||
| 1122 | if (b & GameInputGamepadLeftShoulder) u |= GameInputUiNavigationPageLeft; | ||
| 1123 | if (b & GameInputGamepadRightShoulder) u |= GameInputUiNavigationPageRight; | ||
| 1124 | if (s.gamepad.leftTrigger > t) u |= GameInputUiNavigationPageUp; | ||
| 1125 | if (s.gamepad.rightTrigger > t) u |= GameInputUiNavigationPageDown; | ||
| 1126 | if (s.gamepad.rightThumbstickY > t) u |= GameInputUiNavigationScrollUp; | ||
| 1127 | if (s.gamepad.rightThumbstickY < -t) u |= GameInputUiNavigationScrollDown; | ||
| 1128 | if (s.gamepad.rightThumbstickX < -t) u |= GameInputUiNavigationScrollLeft; | ||
| 1129 | if (s.gamepad.rightThumbstickX > t) u |= GameInputUiNavigationScrollRight; | ||
| 1130 | return s; | 1120 | return s; |
| 1131 | } | 1121 | } |
| 1132 | 1122 | ||
| @@ -1897,80 +1887,29 @@ void update_keyboard_pad() { | |||
| 1897 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); | 1887 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); |
| 1898 | // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard | 1888 | // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard |
| 1899 | // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt | 1889 | // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt |
| 1900 | // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). Buttons: a b x y start back lb rb | 1890 | // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the |
| 1901 | // up down left right. The rsup and rsdown entries replay the right-stick throws logged below. | 1891 | // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3. |
| 1892 | namespace script = rt::gameinput::script; | ||
| 1902 | int right_y = g_keyboard.right_y; | 1893 | int right_y = g_keyboard.right_y; |
| 1903 | struct ScriptedPress { | 1894 | static const script::Script scripted = [] { |
| 1904 | SDL_GamepadButton button; | 1895 | script::Script s = script::parse(env("LEONIDA_INPUT_SCRIPT")); |
| 1905 | double at; | 1896 | if (!s.empty() || s.rejected) |
| 1906 | int right_y = 0; | 1897 | std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n", |
| 1907 | }; | 1898 | s.presses.size(), s.rejected); |
| 1908 | static const std::vector<ScriptedPress> script = [] { | 1899 | return s; |
| 1909 | std::vector<ScriptedPress> out; | ||
| 1910 | static const std::pair<const char*, SDL_GamepadButton> names[] = { | ||
| 1911 | {"a", SDL_GAMEPAD_BUTTON_SOUTH}, {"b", SDL_GAMEPAD_BUTTON_EAST}, | ||
| 1912 | {"x", SDL_GAMEPAD_BUTTON_WEST}, {"y", SDL_GAMEPAD_BUTTON_NORTH}, | ||
| 1913 | {"start", SDL_GAMEPAD_BUTTON_START}, {"back", SDL_GAMEPAD_BUTTON_BACK}, | ||
| 1914 | {"lb", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER}, {"rb", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER}, | ||
| 1915 | {"up", SDL_GAMEPAD_BUTTON_DPAD_UP}, {"down", SDL_GAMEPAD_BUTTON_DPAD_DOWN}, | ||
| 1916 | {"left", SDL_GAMEPAD_BUTTON_DPAD_LEFT}, {"right", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}}; | ||
| 1917 | const std::string text = env("LEONIDA_INPUT_SCRIPT"); | ||
| 1918 | if (text.empty()) return out; | ||
| 1919 | constexpr char whitespace[] = " \t\r\n\f\v"; | ||
| 1920 | size_t items = 0; | ||
| 1921 | for (size_t at = 0, end; at <= text.size(); at = end + 1) { | ||
| 1922 | ++items; | ||
| 1923 | end = std::min(text.find(',', at), text.size()); | ||
| 1924 | const std::string item = text.substr(at, end - at); | ||
| 1925 | const size_t first = item.find_first_not_of(whitespace); | ||
| 1926 | const size_t sep = item.find('@'); | ||
| 1927 | if (sep == std::string::npos || first >= sep) continue; | ||
| 1928 | const char* time = item.c_str() + sep + 1; | ||
| 1929 | char* tail; | ||
| 1930 | const double seconds = std::strtod(time, &tail); | ||
| 1931 | if (tail == time || !std::isfinite(seconds) || seconds < 0 || | ||
| 1932 | item.find_first_not_of(whitespace, tail - item.c_str()) != std::string::npos) continue; | ||
| 1933 | if (item.compare(first, sep - first, "rsup") == 0 || item.compare(first, sep - first, "rsdown") == 0) { | ||
| 1934 | const bool up = item.compare(first, sep - first, "rsup") == 0; | ||
| 1935 | out.push_back({SDL_GAMEPAD_BUTTON_INVALID, seconds, up ? -kStickFull : kStickFull}); | ||
| 1936 | continue; | ||
| 1937 | } | ||
| 1938 | for (const auto& [name, button] : names) { | ||
| 1939 | if (item.compare(first, sep - first, name) == 0) { | ||
| 1940 | out.push_back({button, seconds}); | ||
| 1941 | break; | ||
| 1942 | } | ||
| 1943 | } | ||
| 1944 | } | ||
| 1945 | std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n", | ||
| 1946 | out.size(), items - out.size()); | ||
| 1947 | return out; | ||
| 1948 | }(); | 1900 | }(); |
| 1949 | if (!script.empty()) { | 1901 | if (!scripted.empty()) { |
| 1950 | static const ULONGLONG origin = GetTickCount64(); | 1902 | static const ULONGLONG origin = GetTickCount64(); |
| 1951 | const double now = (GetTickCount64() - origin) / 1000.0; | 1903 | const double now = (GetTickCount64() - origin) / 1000.0; |
| 1952 | // One decision per button: a later entry for the same button must not release what an earlier entry holds | 1904 | const script::Held held = script::held_at(scripted, now); |
| 1953 | // (it did: with several entries only the last one ever reached the title). | 1905 | if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down |
| 1954 | bool scripted[SDL_GAMEPAD_BUTTON_COUNT] = {}; | 1906 | for (const script::Button& b : script::kButtons) { |
| 1955 | bool named[SDL_GAMEPAD_BUTTON_COUNT] = {}; | 1907 | if (!(scripted.named & b.bit)) continue; |
| 1956 | for (const auto& press : script) { | 1908 | const auto button = static_cast<SDL_GamepadButton>(b.sdl); |
| 1957 | const bool held = now >= press.at && now < press.at + 0.3; | 1909 | SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]); |
| 1958 | if (press.right_y) { | ||
| 1959 | if (held) right_y = press.right_y; | ||
| 1960 | } else { | ||
| 1961 | named[press.button] = true; | ||
| 1962 | scripted[press.button] |= held; | ||
| 1963 | } | ||
| 1964 | } | ||
| 1965 | for (int button = 0; button < SDL_GAMEPAD_BUTTON_COUNT; ++button) { | ||
| 1966 | if (!named[button]) continue; | ||
| 1967 | SDL_SetJoystickVirtualButton(g_keyboard.js, static_cast<SDL_GamepadButton>(button), | ||
| 1968 | scripted[button] || down[button]); | ||
| 1969 | static bool was[SDL_GAMEPAD_BUTTON_COUNT] = {}; | ||
| 1970 | if (scripted[button] && !was[button]) | ||
| 1971 | std::fprintf(stderr, "[rt] gameinput: scripted press of button %d at %.1f s\n", button, now); | ||
| 1972 | was[button] = scripted[button]; | ||
| 1973 | } | 1910 | } |
| 1911 | static uint32_t was = 0; | ||
| 1912 | script::log_scripted(held.buttons, &was, now); | ||
| 1974 | } | 1913 | } |
| 1975 | const int reach = lx && ly ? kStickDiagonal : kStickFull; | 1914 | const int reach = lx && ly ? kStickDiagonal : kStickFull; |
| 1976 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach)); | 1915 | SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach)); |
| @@ -2053,26 +1992,10 @@ void poll_once() { | |||
| 2053 | { | 1992 | { |
| 2054 | static const bool log_presses = env("LEONIDA_INPUT_LOG") != "0"; | 1993 | static const bool log_presses = env("LEONIDA_INPUT_LOG") != "0"; |
| 2055 | static const ULONGLONG log_origin = GetTickCount64(); | 1994 | static const ULONGLONG log_origin = GetTickCount64(); |
| 2056 | if (log_presses) { | 1995 | if (log_presses) |
| 2057 | static const std::pair<uint32_t, const char*> kNames[] = { | 1996 | rt::gameinput::script::log_presses(snap.gamepad.buttons & ~d->last_snap.gamepad.buttons, |
| 2058 | {GameInputGamepadA, "a"}, {GameInputGamepadB, "b"}, | 1997 | d->last_snap.gamepad.rightThumbstickY, snap.gamepad.rightThumbstickY, |
| 2059 | {GameInputGamepadX, "x"}, {GameInputGamepadY, "y"}, | 1998 | (GetTickCount64() - log_origin) / 1000.0, d->name.c_str()); |
| 2060 | {GameInputGamepadMenu, "start"}, {GameInputGamepadView, "back"}, | ||
| 2061 | {GameInputGamepadLeftShoulder, "lb"}, {GameInputGamepadRightShoulder, "rb"}, | ||
| 2062 | {GameInputGamepadDPadUp, "up"}, {GameInputGamepadDPadDown, "down"}, | ||
| 2063 | {GameInputGamepadDPadLeft, "left"}, {GameInputGamepadDPadRight, "right"}}; | ||
| 2064 | const double at = (GetTickCount64() - log_origin) / 1000.0; | ||
| 2065 | const uint32_t pressed = snap.gamepad.buttons & ~d->last_snap.gamepad.buttons; | ||
| 2066 | for (const auto& n : kNames) | ||
| 2067 | if (pressed & n.first) | ||
| 2068 | std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", n.second, at, d->name.c_str()); | ||
| 2069 | const auto throw_of = [](float y) { return y > 0.6f ? 1 : y < -0.6f ? -1 : 0; }; | ||
| 2070 | const int was = throw_of(d->last_snap.gamepad.rightThumbstickY); | ||
| 2071 | const int is = throw_of(snap.gamepad.rightThumbstickY); | ||
| 2072 | if (is != was && is != 0) | ||
| 2073 | std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", is > 0 ? "rsup" : "rsdown", at, | ||
| 2074 | d->name.c_str()); | ||
| 2075 | } | ||
| 2076 | } | 1999 | } |
| 2077 | d->last_snap = snap; | 2000 | d->last_snap = snap; |
| 2078 | // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button | 2001 | // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button |