upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 16:38:20 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 16:38:20 -0500
commitc116c3fa28983444cd47feea0461e1e7eaac83b5 (patch)
treef2f4821b744b678f0cf1ce98638e981ea737b0c6 /runtime/shims/gameinput.cpp
parent5866d6974e86fc2d39e6ce0691af63b354f6c8b9 (diff)
parent24d8af66af151f024b0b520fb397d899924f5486 (diff)
Merge main into t-drive-overlay
docs/boot.md: main added LEONIDA_WRITE_DIR / LEONIDA_SAVE_DIR to the --overlay bullet and a new "Under Wine on Linux" section; keep both and the T: paragraph of this branch after main's sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp151
1 files changed, 58 insertions, 93 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 7650b361f..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
57using namespace gi; 58using namespace gi;
@@ -132,18 +133,30 @@ const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); }
132 133
133// ---- configuration ---------------------------------------------------------------------------------------------- 134// ---- configuration ----------------------------------------------------------------------------------------------
134 135
135std::string env(const char* name) { 136std::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 139constexpr 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}
159static_assert(script_buttons_match_sdl(), "gameinput_script.h's SDL button numbers drifted from SDL_GamepadButton");
147 160
148rt::gameinput::Config read_config() { 161rt::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
@@ -1895,73 +1885,37 @@ void update_keyboard_pad() {
1895 } 1885 }
1896 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); 1886 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours());
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 // Timed presses on the keyboard pad, in seconds since its first update. Each press lasts 300 ms. 1888 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard
1899 struct ScriptedPress { 1889 // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt
1900 SDL_GamepadButton button; 1890 // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the
1901 double at; 1891 // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3.
1902 }; 1892 namespace script = rt::gameinput::script;
1903 static const std::vector<ScriptedPress> script = [] { 1893 int right_y = g_keyboard.right_y;
1904 std::vector<ScriptedPress> out; 1894 static const script::Script scripted = [] {
1905 static const std::pair<const char*, SDL_GamepadButton> names[] = { 1895 script::Script s = script::parse(env("LEONIDA_INPUT_SCRIPT"));
1906 {"a", SDL_GAMEPAD_BUTTON_SOUTH}, {"b", SDL_GAMEPAD_BUTTON_EAST}, 1896 if (!s.empty() || s.rejected)
1907 {"x", SDL_GAMEPAD_BUTTON_WEST}, {"y", SDL_GAMEPAD_BUTTON_NORTH}, 1897 std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n",
1908 {"start", SDL_GAMEPAD_BUTTON_START}, {"back", SDL_GAMEPAD_BUTTON_BACK}, 1898 s.presses.size(), s.rejected);
1909 {"lb", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER}, {"rb", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER}, 1899 return s;
1910 {"up", SDL_GAMEPAD_BUTTON_DPAD_UP}, {"down", SDL_GAMEPAD_BUTTON_DPAD_DOWN},
1911 {"left", SDL_GAMEPAD_BUTTON_DPAD_LEFT}, {"right", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}};
1912 const std::string text = env("LEONIDA_INPUT_SCRIPT");
1913 if (text.empty()) return out;
1914 constexpr char whitespace[] = " \t\r\n\f\v";
1915 size_t items = 0;
1916 for (size_t at = 0, end; at <= text.size(); at = end + 1) {
1917 ++items;
1918 end = std::min(text.find(',', at), text.size());
1919 const std::string item = text.substr(at, end - at);
1920 const size_t first = item.find_first_not_of(whitespace);
1921 const size_t sep = item.find('@');
1922 if (sep == std::string::npos || first >= sep) continue;
1923 const char* time = item.c_str() + sep + 1;
1924 char* tail;
1925 const double seconds = std::strtod(time, &tail);
1926 if (tail == time || !std::isfinite(seconds) || seconds < 0 ||
1927 item.find_first_not_of(whitespace, tail - item.c_str()) != std::string::npos) continue;
1928 for (const auto& [name, button] : names) {
1929 if (item.compare(first, sep - first, name) == 0) {
1930 out.push_back({button, seconds});
1931 break;
1932 }
1933 }
1934 }
1935 std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n",
1936 out.size(), items - out.size());
1937 return out;
1938 }(); 1900 }();
1939 if (!script.empty()) { 1901 if (!scripted.empty()) {
1940 static const ULONGLONG origin = GetTickCount64(); 1902 static const ULONGLONG origin = GetTickCount64();
1941 const double now = (GetTickCount64() - origin) / 1000.0; 1903 const double now = (GetTickCount64() - origin) / 1000.0;
1942 // 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);
1943 // (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
1944 bool scripted[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1906 for (const script::Button& b : script::kButtons) {
1945 bool named[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1907 if (!(scripted.named & b.bit)) continue;
1946 for (const auto& press : script) { 1908 const auto button = static_cast<SDL_GamepadButton>(b.sdl);
1947 named[press.button] = true; 1909 SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]);
1948 if (now >= press.at && now < press.at + 0.3) scripted[press.button] = true;
1949 }
1950 for (int button = 0; button < SDL_GAMEPAD_BUTTON_COUNT; ++button) {
1951 if (!named[button]) continue;
1952 SDL_SetJoystickVirtualButton(g_keyboard.js, static_cast<SDL_GamepadButton>(button),
1953 scripted[button] || down[button]);
1954 static bool was[SDL_GAMEPAD_BUTTON_COUNT] = {};
1955 if (scripted[button] && !was[button])
1956 std::fprintf(stderr, "[rt] gameinput: scripted press of button %d at %.1f s\n", button, now);
1957 was[button] = scripted[button];
1958 } 1910 }
1911 static uint32_t was = 0;
1912 script::log_scripted(held.buttons, &was, now);
1959 } 1913 }
1960 const int reach = lx && ly ? kStickDiagonal : kStickFull; 1914 const int reach = lx && ly ? kStickDiagonal : kStickFull;
1961 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));
1962 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach)); 1916 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach));
1963 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTX, static_cast<Sint16>(g_keyboard.right_x)); 1917 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTX, static_cast<Sint16>(g_keyboard.right_x));
1964 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTY, static_cast<Sint16>(g_keyboard.right_y)); 1918 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTY, static_cast<Sint16>(right_y));
1965 // SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled, 32767 full. 1919 // SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled, 32767 full.
1966 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, aim ? 32767 : -32768); 1920 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, aim ? 32767 : -32768);
1967 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, fire ? 32767 : -32768); 1921 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, fire ? 32767 : -32768);
@@ -2032,6 +1986,17 @@ void poll_once() {
2032 read_raw(d, &snap); 1986 read_raw(d, &snap);
2033 const bool guide_changed = d->last_snap.guide != snap.guide; 1987 const bool guide_changed = d->last_snap.guide != snap.guide;
2034 const bool changed = !(d->last_snap == snap); 1988 const bool changed = !(d->last_snap == snap);
1989 // Every press is logged in LEONIDA_INPUT_SCRIPT's own form (name@seconds), so a session played by hand can
1990 // be replayed unattended: join the logged items with commas. The right stick's vertical throws (what scrolls
1991 // a page) are logged as rsup/rsdown. LEONIDA_INPUT_LOG=0 turns the lines off.
1992 {
1993 static const bool log_presses = env("LEONIDA_INPUT_LOG") != "0";
1994 static const ULONGLONG log_origin = GetTickCount64();
1995 if (log_presses)
1996 rt::gameinput::script::log_presses(snap.gamepad.buttons & ~d->last_snap.gamepad.buttons,
1997 d->last_snap.gamepad.rightThumbstickY, snap.gamepad.rightThumbstickY,
1998 (GetTickCount64() - log_origin) / 1000.0, d->name.c_str());
1999 }
2035 d->last_snap = snap; 2000 d->last_snap = snap;
2036 // 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
2037 // held changes nothing. 2002 // held changes nothing.