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-24 22:35:34 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 22:35:34 -0500
commit6ed716a99a7416f1e8a60c2e38e128876ab752ec (patch)
tree3c35526aa8b96594252c82c278ab19b9aa3a98ba /runtime/shims/gameinput.cpp
parent028a414fade7058a96e5933d1325f374a0026588 (diff)
parentbfef489cb6307fa4e1dc6ed2c40bfc0ce120d066 (diff)
Merge main into RT-QUEUE-SIGNAL (#430)
Everything below this rung (#414, #416, #427) and the rung's own routing are in main already, through #984 and #939, in main's form; what is left of #430 is its test of queue slot 17, ported onto main's layout detection. Conflicts, all resolved to main's form: - runtime/shims/d3d12_xs_device.cpp: main's queue slot 16/17 rule (a fence there is the newer layout's Signal/Wait, for any caller) replaces the rung's caller-gated rule; main's PresentX routing (slot 30, or 32 once the newer layout is detected, LEONIDA_XS_PRESENT_X_SLOT) and lazily built presenter replace the fixed slot 32 of #414; main's list proxy already translates the CPU handles of console list slots 50 to 54 (#416) and drops unordered access from a depth-stencil texture at slot 76 (#427). - runtime/shims/d3d12_xs_device.h, runtime/shims/xbox_present.cpp, runtime/shims/xbox_present.h, runtime/src/frame_pacer.h: main's shared FrameTokenSource (Logan's piece of #414, as #984 carried it). - runtime/shims/d3d12_xs_desc_clean.h, runtime/test/device_proxy_test.cpp: main's without_depth_stencil_uav and its slot 25/76 test (#427). - runtime/test/xs_proxy_mock_test.cpp: main's file, which already has check_list_cpu_handles (#416's cases and more); the rung's slot 16/17 case is ported to main's form at the end of check_xbox_queue_slots: slot 16 and 17 with a fence reach PC Signal and Wait with the real fence, not the PC getters, and the fence proxy's vtable pointer is unchanged. The rung's host-caller and no-fence expectations are dropped: under main's rule a fence at 16 proves the newer layout, after which those calls answer S_OK. Auto-merged, then set to main's content: - runtime/test/queue_proxy_test.cpp: #414's call of queue slot 32 as an overridden slot; on main slot 32 is routed only on the newer layout and the sweep of the console tail already calls it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp123
1 files changed, 116 insertions, 7 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 402179cde..db685e71f 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -47,6 +47,7 @@
47#include <mutex> 47#include <mutex>
48#include <string> 48#include <string>
49#include <thread> 49#include <thread>
50#include <utility>
50#include <vector> 51#include <vector>
51 52
52#include "gameinput.h" 53#include "gameinput.h"
@@ -132,10 +133,14 @@ const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); }
132// ---- configuration ---------------------------------------------------------------------------------------------- 133// ---- configuration ----------------------------------------------------------------------------------------------
133 134
134std::string env(const char* name) { 135std::string env(const char* name) {
135 char buf[64]; 136 // Any length: an input script (LEONIDA_INPUT_SCRIPT) is easily longer than a fixed buffer, and a value that did
136 const DWORD n = GetEnvironmentVariableA(name, buf, sizeof buf); 137 // not fit used to read as unset.
137 if (n == 0 || n >= sizeof buf) return {}; 138 const DWORD need = GetEnvironmentVariableA(name, nullptr, 0); // with the terminator
138 std::string s(buf, n); 139 if (need == 0) return {};
140 std::string s(need, '\0');
141 const DWORD n = GetEnvironmentVariableA(name, s.data(), need);
142 if (n == 0 || n >= need) return {};
143 s.resize(n);
139 for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); 144 for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
140 return s; 145 return s;
141} 146}
@@ -1861,8 +1866,8 @@ void update_keyboard_pad() {
1861 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1866 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {};
1862 int lx = 0, ly = 0; 1867 int lx = 0, ly = 0;
1863 bool aim = false, fire = false; 1868 bool aim = false, fire = false;
1864 // Out of focus every button, stick and trigger is released, so a key held while switching windows does not stay 1869 // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump.
1865 // pressed, and the cursor is read afresh on return, so moving it in another window does not swing the stick. 1870 // Scripted presses still run while the window is unfocused.
1866 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()(); 1871 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()();
1867 if (active) { 1872 if (active) {
1868 const rt::gameinput::KeyDownFn key_down = g_key_down.load(); 1873 const rt::gameinput::KeyDownFn key_down = g_key_down.load();
@@ -1890,11 +1895,88 @@ void update_keyboard_pad() {
1890 } 1895 }
1891 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); 1896 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours());
1892 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); 1897 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
1899 // 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
1901 // up down left right. The rsup and rsdown entries replay the right-stick throws logged below.
1902 int right_y = g_keyboard.right_y;
1903 struct ScriptedPress {
1904 SDL_GamepadButton button;
1905 double at;
1906 int right_y = 0;
1907 };
1908 static const std::vector<ScriptedPress> script = [] {
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 }();
1949 if (!script.empty()) {
1950 static const ULONGLONG origin = GetTickCount64();
1951 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
1953 // (it did: with several entries only the last one ever reached the title).
1954 bool scripted[SDL_GAMEPAD_BUTTON_COUNT] = {};
1955 bool named[SDL_GAMEPAD_BUTTON_COUNT] = {};
1956 for (const auto& press : script) {
1957 const bool held = now >= press.at && now < press.at + 0.3;
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 }
1974 }
1893 const int reach = lx && ly ? kStickDiagonal : kStickFull; 1975 const int reach = lx && ly ? kStickDiagonal : kStickFull;
1894 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach)); 1976 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach));
1895 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach)); 1977 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach));
1896 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTX, static_cast<Sint16>(g_keyboard.right_x)); 1978 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTX, static_cast<Sint16>(g_keyboard.right_x));
1897 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTY, static_cast<Sint16>(g_keyboard.right_y)); 1979 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTY, static_cast<Sint16>(right_y));
1898 // SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled, 32767 full. 1980 // SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled, 32767 full.
1899 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, aim ? 32767 : -32768); 1981 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, aim ? 32767 : -32768);
1900 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, fire ? 32767 : -32768); 1982 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, fire ? 32767 : -32768);
@@ -1965,6 +2047,33 @@ void poll_once() {
1965 read_raw(d, &snap); 2047 read_raw(d, &snap);
1966 const bool guide_changed = d->last_snap.guide != snap.guide; 2048 const bool guide_changed = d->last_snap.guide != snap.guide;
1967 const bool changed = !(d->last_snap == snap); 2049 const bool changed = !(d->last_snap == snap);
2050 // Every press is logged in LEONIDA_INPUT_SCRIPT's own form (name@seconds), so a session played by hand can
2051 // be replayed unattended: join the logged items with commas. The right stick's vertical throws (what scrolls
2052 // a page) are logged as rsup/rsdown. LEONIDA_INPUT_LOG=0 turns the lines off.
2053 {
2054 static const bool log_presses = env("LEONIDA_INPUT_LOG") != "0";
2055 static const ULONGLONG log_origin = GetTickCount64();
2056 if (log_presses) {
2057 static const std::pair<uint32_t, const char*> kNames[] = {
2058 {GameInputGamepadA, "a"}, {GameInputGamepadB, "b"},
2059 {GameInputGamepadX, "x"}, {GameInputGamepadY, "y"},
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 }
1968 d->last_snap = snap; 2077 d->last_snap = snap;
1969 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button 2078 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button
1970 // held changes nothing. 2079 // held changes nothing.