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