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:
authorkawaii-shakal <deusex1998@gmail.com>2026-11-25 19:49:32 +0500
committerkawaii-shakal <deusex1998@gmail.com>2026-11-25 19:49:32 +0500
commitd2158084cc6659aa6f149fd3ae2511d1402f3d57 (patch)
tree7f15fce2b935e90cdca56fcecd85c43a22262147 /runtime/shims/gameinput.cpp
parente6aa63b187246f62acfe249b7ac177a5ef8f7bef (diff)
parent71a769d7d85208f86c6e2e50263aca7df1becce9 (diff)
Merge main into input-out-of-the-box: the input script row from #1843 with the keyboard pad on by default
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp171
1 files changed, 47 insertions, 124 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 5e9674380..e9fe818b7 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;
@@ -1106,30 +1119,7 @@ Snapshot read_pad(Device* d) {
1106 } 1119 }
1107 } 1120 }
1108 1121
1109 // UI navigation derived from the gamepad (VERIFY against the console's own mapping). 1122 s.ui = rt::gameinput::script::ui_navigation(s.gamepad);
1110 const uint32_t b = s.gamepad.buttons;
1111 const float t = 0.5f;
1112 uint32_t& u = s.ui;
1113 if (b & GameInputGamepadMenu) u |= GameInputUiNavigationMenu;
1114 if (b & GameInputGamepadView) u |= GameInputUiNavigationView;
1115 if (b & GameInputGamepadA) u |= GameInputUiNavigationAccept;
1116 if (b & GameInputGamepadB) u |= GameInputUiNavigationCancel;
1117 if (b & GameInputGamepadX) u |= GameInputUiNavigationContext1;
1118 if (b & GameInputGamepadY) u |= GameInputUiNavigationContext2;
1119 if (b & GameInputGamepadLeftThumbstick) u |= GameInputUiNavigationContext3;
1120 if (b & GameInputGamepadRightThumbstick) u |= GameInputUiNavigationContext4;
1121 if ((b & GameInputGamepadDPadUp) || s.gamepad.leftThumbstickY > t) u |= GameInputUiNavigationUp;
1122 if ((b & GameInputGamepadDPadDown) || s.gamepad.leftThumbstickY < -t) u |= GameInputUiNavigationDown;
1123 if ((b & GameInputGamepadDPadLeft) || s.gamepad.leftThumbstickX < -t) u |= GameInputUiNavigationLeft;
1124 if ((b & GameInputGamepadDPadRight) || s.gamepad.leftThumbstickX > t) u |= GameInputUiNavigationRight;
1125 if (b & GameInputGamepadLeftShoulder) u |= GameInputUiNavigationPageLeft;
1126 if (b & GameInputGamepadRightShoulder) u |= GameInputUiNavigationPageRight;
1127 if (s.gamepad.leftTrigger > t) u |= GameInputUiNavigationPageUp;
1128 if (s.gamepad.rightTrigger > t) u |= GameInputUiNavigationPageDown;
1129 if (s.gamepad.rightThumbstickY > t) u |= GameInputUiNavigationScrollUp;
1130 if (s.gamepad.rightThumbstickY < -t) u |= GameInputUiNavigationScrollDown;
1131 if (s.gamepad.rightThumbstickX < -t) u |= GameInputUiNavigationScrollLeft;
1132 if (s.gamepad.rightThumbstickX > t) u |= GameInputUiNavigationScrollRight;
1133 return s; 1123 return s;
1134} 1124}
1135 1125
@@ -1900,80 +1890,29 @@ void update_keyboard_pad() {
1900 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); 1890 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]);
1901 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard 1891 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard
1902 // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt 1892 // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt
1903 // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). Buttons: a b x y start back lb rb 1893 // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the
1904 // up down left right. The rsup and rsdown entries replay the right-stick throws logged below. 1894 // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3.
1895 namespace script = rt::gameinput::script;
1905 int right_y = g_keyboard.right_y; 1896 int right_y = g_keyboard.right_y;
1906 struct ScriptedPress { 1897 static const script::Script scripted = [] {
1907 SDL_GamepadButton button; 1898 script::Script s = script::parse(env("LEONIDA_INPUT_SCRIPT"));
1908 double at; 1899 if (!s.empty() || s.rejected)
1909 int right_y = 0; 1900 std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n",
1910 }; 1901 s.presses.size(), s.rejected);
1911 static const std::vector<ScriptedPress> script = [] { 1902 return s;
1912 std::vector<ScriptedPress> out;
1913 static const std::pair<const char*, SDL_GamepadButton> names[] = {
1914 {"a", SDL_GAMEPAD_BUTTON_SOUTH}, {"b", SDL_GAMEPAD_BUTTON_EAST},
1915 {"x", SDL_GAMEPAD_BUTTON_WEST}, {"y", SDL_GAMEPAD_BUTTON_NORTH},
1916 {"start", SDL_GAMEPAD_BUTTON_START}, {"back", SDL_GAMEPAD_BUTTON_BACK},
1917 {"lb", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER}, {"rb", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER},
1918 {"up", SDL_GAMEPAD_BUTTON_DPAD_UP}, {"down", SDL_GAMEPAD_BUTTON_DPAD_DOWN},
1919 {"left", SDL_GAMEPAD_BUTTON_DPAD_LEFT}, {"right", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}};
1920 const std::string text = env("LEONIDA_INPUT_SCRIPT");
1921 if (text.empty()) return out;
1922 constexpr char whitespace[] = " \t\r\n\f\v";
1923 size_t items = 0;
1924 for (size_t at = 0, end; at <= text.size(); at = end + 1) {
1925 ++items;
1926 end = std::min(text.find(',', at), text.size());
1927 const std::string item = text.substr(at, end - at);
1928 const size_t first = item.find_first_not_of(whitespace);
1929 const size_t sep = item.find('@');
1930 if (sep == std::string::npos || first >= sep) continue;
1931 const char* time = item.c_str() + sep + 1;
1932 char* tail;
1933 const double seconds = std::strtod(time, &tail);
1934 if (tail == time || !std::isfinite(seconds) || seconds < 0 ||
1935 item.find_first_not_of(whitespace, tail - item.c_str()) != std::string::npos) continue;
1936 if (item.compare(first, sep - first, "rsup") == 0 || item.compare(first, sep - first, "rsdown") == 0) {
1937 const bool up = item.compare(first, sep - first, "rsup") == 0;
1938 out.push_back({SDL_GAMEPAD_BUTTON_INVALID, seconds, up ? -kStickFull : kStickFull});
1939 continue;
1940 }
1941 for (const auto& [name, button] : names) {
1942 if (item.compare(first, sep - first, name) == 0) {
1943 out.push_back({button, seconds});
1944 break;
1945 }
1946 }
1947 }
1948 std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n",
1949 out.size(), items - out.size());
1950 return out;
1951 }(); 1903 }();
1952 if (!script.empty()) { 1904 if (!scripted.empty()) {
1953 static const ULONGLONG origin = GetTickCount64(); 1905 static const ULONGLONG origin = GetTickCount64();
1954 const double now = (GetTickCount64() - origin) / 1000.0; 1906 const double now = (GetTickCount64() - origin) / 1000.0;
1955 // One decision per button: a later entry for the same button must not release what an earlier entry holds 1907 const script::Held held = script::held_at(scripted, now);
1956 // (it did: with several entries only the last one ever reached the title). 1908 if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down
1957 bool scripted[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1909 for (const script::Button& b : script::kButtons) {
1958 bool named[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1910 if (!(scripted.named & b.bit)) continue;
1959 for (const auto& press : script) { 1911 const auto button = static_cast<SDL_GamepadButton>(b.sdl);
1960 const bool held = now >= press.at && now < press.at + 0.3; 1912 SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]);
1961 if (press.right_y) {
1962 if (held) right_y = press.right_y;
1963 } else {
1964 named[press.button] = true;
1965 scripted[press.button] |= held;
1966 }
1967 }
1968 for (int button = 0; button < SDL_GAMEPAD_BUTTON_COUNT; ++button) {
1969 if (!named[button]) continue;
1970 SDL_SetJoystickVirtualButton(g_keyboard.js, static_cast<SDL_GamepadButton>(button),
1971 scripted[button] || down[button]);
1972 static bool was[SDL_GAMEPAD_BUTTON_COUNT] = {};
1973 if (scripted[button] && !was[button])
1974 std::fprintf(stderr, "[rt] gameinput: scripted press of button %d at %.1f s\n", button, now);
1975 was[button] = scripted[button];
1976 } 1913 }
1914 static uint32_t was = 0;
1915 script::log_scripted(held.buttons, &was, now);
1977 } 1916 }
1978 const int reach = lx && ly ? kStickDiagonal : kStickFull; 1917 const int reach = lx && ly ? kStickDiagonal : kStickFull;
1979 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach)); 1918 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach));
@@ -2056,26 +1995,10 @@ void poll_once() {
2056 { 1995 {
2057 static const bool log_presses = env("LEONIDA_INPUT_LOG") != "0"; 1996 static const bool log_presses = env("LEONIDA_INPUT_LOG") != "0";
2058 static const ULONGLONG log_origin = GetTickCount64(); 1997 static const ULONGLONG log_origin = GetTickCount64();
2059 if (log_presses) { 1998 if (log_presses)
2060 static const std::pair<uint32_t, const char*> kNames[] = { 1999 rt::gameinput::script::log_presses(snap.gamepad.buttons & ~d->last_snap.gamepad.buttons,
2061 {GameInputGamepadA, "a"}, {GameInputGamepadB, "b"}, 2000 d->last_snap.gamepad.rightThumbstickY, snap.gamepad.rightThumbstickY,
2062 {GameInputGamepadX, "x"}, {GameInputGamepadY, "y"}, 2001 (GetTickCount64() - log_origin) / 1000.0, d->name.c_str());
2063 {GameInputGamepadMenu, "start"}, {GameInputGamepadView, "back"},
2064 {GameInputGamepadLeftShoulder, "lb"}, {GameInputGamepadRightShoulder, "rb"},
2065 {GameInputGamepadDPadUp, "up"}, {GameInputGamepadDPadDown, "down"},
2066 {GameInputGamepadDPadLeft, "left"}, {GameInputGamepadDPadRight, "right"}};
2067 const double at = (GetTickCount64() - log_origin) / 1000.0;
2068 const uint32_t pressed = snap.gamepad.buttons & ~d->last_snap.gamepad.buttons;
2069 for (const auto& n : kNames)
2070 if (pressed & n.first)
2071 std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", n.second, at, d->name.c_str());
2072 const auto throw_of = [](float y) { return y > 0.6f ? 1 : y < -0.6f ? -1 : 0; };
2073 const int was = throw_of(d->last_snap.gamepad.rightThumbstickY);
2074 const int is = throw_of(snap.gamepad.rightThumbstickY);
2075 if (is != was && is != 0)
2076 std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", is > 0 ? "rsup" : "rsdown", at,
2077 d->name.c_str());
2078 }
2079 } 2002 }
2080 d->last_snap = snap; 2003 d->last_snap = snap;
2081 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button 2004 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button