diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
| commit | df92abca8ee2a8f9093efea7fcaa4dadc3ddaebc (patch) | |
| tree | 97ef8f95544fe263049d3f7360823f16100de209 /runtime/shims/gameinput.cpp | |
| parent | 49a0b69fcdb80db1f2a44ce7ca0ab8dfdec1e403 (diff) | |
| parent | e5a7c8942678b323f655939c81f5d8855d20c248 (diff) | |
Merge main e5a7c8942 into shadow-aliased-guard (#1867)
One conflict, runtime/README.md: this branch rewrote the LEONIDA_SHADOW_ALIAS_MS
row, main (#2163) added the LEONIDA_SHADOW_KEEP row after it. Both rows kept;
the keep row's "aliased" now reads "aliased while they can't be guarded at every
address", since aliased pages are no longer copied without a sign of a write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index fa5889d6a..c989daff4 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -1887,7 +1887,9 @@ void update_keyboard_pad() { | |||
| 1887 | clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); | 1887 | clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); |
| 1888 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); | 1888 | for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); |
| 1889 | // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard | 1889 | // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard |
| 1890 | // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt | 1890 | // pad, in seconds since the pad's first update, each held 300 ms or its own button@seconds:hold, and then until the |
| 1891 | // title has read the pads kReads times since it began (never past kMaxHold seconds, or the hold if it is longer, so | ||
| 1892 | // a title that stops reading does not keep the button). For unattended boots: the title's first prompt | ||
| 1891 | // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the | 1893 | // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the |
| 1892 | // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3. | 1894 | // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3. |
| 1893 | namespace script = rt::gameinput::script; | 1895 | namespace script = rt::gameinput::script; |
| @@ -1899,7 +1901,21 @@ void update_keyboard_pad() { | |||
| 1899 | s.presses.size(), s.rejected); | 1901 | s.presses.size(), s.rejected); |
| 1900 | return s; | 1902 | return s; |
| 1901 | }(); | 1903 | }(); |
| 1902 | if (!scripted.empty()) { | 1904 | // LEONIDA_INPUT_FILE=<path> (local, for headless runs whose display takes no keyboard input): every line appended |
| 1905 | // to the file names one button of gameinput_script.h's table ("a", "down", ...) and becomes a press at once, held | ||
| 1906 | // like a scripted one (300 ms and six readings, at most 5 s). The file is read again at most every 250 ms. | ||
| 1907 | struct FilePress { | ||
| 1908 | uint32_t bit; | ||
| 1909 | double at; | ||
| 1910 | uint64_t began; | ||
| 1911 | bool started; | ||
| 1912 | }; | ||
| 1913 | static std::vector<FilePress> file_presses; | ||
| 1914 | static const std::string input_file = [] { | ||
| 1915 | const char* v = std::getenv("LEONIDA_INPUT_FILE"); | ||
| 1916 | return std::string(v ? v : ""); | ||
| 1917 | }(); | ||
| 1918 | if (!scripted.empty() || !input_file.empty()) { | ||
| 1903 | static const ULONGLONG origin = GetTickCount64(); | 1919 | static const ULONGLONG origin = GetTickCount64(); |
| 1904 | const double now = (GetTickCount64() - origin) / 1000.0; | 1920 | const double now = (GetTickCount64() - origin) / 1000.0; |
| 1905 | script::Held held{}; | 1921 | script::Held held{}; |
| @@ -1908,15 +1924,51 @@ void update_keyboard_pad() { | |||
| 1908 | static std::vector<uint64_t> began(scripted.presses.size(), 0); | 1924 | static std::vector<uint64_t> began(scripted.presses.size(), 0); |
| 1909 | static std::vector<char> started(scripted.presses.size(), 0); | 1925 | static std::vector<char> started(scripted.presses.size(), 0); |
| 1910 | const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed); | 1926 | const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed); |
| 1927 | if (!input_file.empty()) { | ||
| 1928 | static ULONGLONG last_read = 0; | ||
| 1929 | static std::size_t lines_done = 0; | ||
| 1930 | const ULONGLONG tick = GetTickCount64(); | ||
| 1931 | if (tick - last_read >= 250) { | ||
| 1932 | last_read = tick; | ||
| 1933 | if (FILE* f = std::fopen(input_file.c_str(), "r")) { | ||
| 1934 | char line[64]; | ||
| 1935 | std::size_t n = 0; | ||
| 1936 | while (std::fgets(line, sizeof(line), f)) { | ||
| 1937 | if (++n <= lines_done) continue; | ||
| 1938 | lines_done = n; | ||
| 1939 | std::string name(line); | ||
| 1940 | while (!name.empty() && (name.back() == '\n' || name.back() == '\r' || name.back() == ' ')) | ||
| 1941 | name.pop_back(); | ||
| 1942 | bool known = false; | ||
| 1943 | for (const script::Button& b : script::kButtons) | ||
| 1944 | if (name == b.name) { | ||
| 1945 | file_presses.push_back({b.bit, now, 0, false}); | ||
| 1946 | known = true; | ||
| 1947 | } | ||
| 1948 | std::fprintf(stderr, "[rt] gameinput: input file line %zu \"%s\" at %.1f s%s\n", n, | ||
| 1949 | name.c_str(), now, known ? "" : " (unknown button, ignored)"); | ||
| 1950 | } | ||
| 1951 | std::fclose(f); | ||
| 1952 | } | ||
| 1953 | } | ||
| 1954 | for (auto& p : file_presses) { | ||
| 1955 | if (now < p.at || now >= p.at + kMaxHold) continue; | ||
| 1956 | if (!p.started) { | ||
| 1957 | p.started = true; | ||
| 1958 | p.began = calls; | ||
| 1959 | } | ||
| 1960 | if (now < p.at + 0.3 || calls - p.began < kReads) held.buttons |= p.bit; | ||
| 1961 | } | ||
| 1962 | } | ||
| 1911 | for (std::size_t i = 0; i < scripted.presses.size(); ++i) { | 1963 | for (std::size_t i = 0; i < scripted.presses.size(); ++i) { |
| 1912 | const auto& press = scripted.presses[i]; | 1964 | const auto& press = scripted.presses[i]; |
| 1913 | bool active = false; | 1965 | bool active = false; |
| 1914 | if (now >= press.at && now < press.at + kMaxHold) { | 1966 | if (now >= press.at && now < press.at + std::max(press.hold, kMaxHold)) { |
| 1915 | if (!started[i]) { | 1967 | if (!started[i]) { |
| 1916 | started[i] = 1; | 1968 | started[i] = 1; |
| 1917 | began[i] = calls; | 1969 | began[i] = calls; |
| 1918 | } | 1970 | } |
| 1919 | active = now < press.at + 0.3 || calls - began[i] < kReads; | 1971 | active = now < press.at + press.hold || calls - began[i] < kReads; |
| 1920 | } | 1972 | } |
| 1921 | if (!active) continue; | 1973 | if (!active) continue; |
| 1922 | if (press.stick) held.stick = press.stick; | 1974 | if (press.stick) held.stick = press.stick; |
| @@ -1924,7 +1976,7 @@ void update_keyboard_pad() { | |||
| 1924 | } | 1976 | } |
| 1925 | if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down | 1977 | if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down |
| 1926 | for (const script::Button& b : script::kButtons) { | 1978 | for (const script::Button& b : script::kButtons) { |
| 1927 | if (!(scripted.named & b.bit)) continue; | 1979 | if (!(scripted.named & b.bit) && input_file.empty()) continue; |
| 1928 | const auto button = static_cast<SDL_GamepadButton>(b.sdl); | 1980 | const auto button = static_cast<SDL_GamepadButton>(b.sdl); |
| 1929 | SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]); | 1981 | SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]); |
| 1930 | } | 1982 | } |
| @@ -2443,13 +2495,13 @@ public: | |||
| 2443 | struct Stats { | 2495 | struct Stats { |
| 2444 | std::mutex mu; | 2496 | std::mutex mu; |
| 2445 | uint64_t calls[3][3] = {}; // method x (null, aggregate, a device) | 2497 | uint64_t calls[3][3] = {}; // method x (null, aggregate, a device) |
| 2446 | uint64_t found[3] = {}, pressed[3] = {}; | 2498 | uint64_t found[3] = {}, pressed[3] = {}, a_pressed[3] = {}; |
| 2447 | std::map<std::string, std::pair<uint64_t, uint64_t>> by_device; // name -> (readings, with buttons) | 2499 | std::map<std::string, std::pair<uint64_t, uint64_t>> by_device; // name -> (readings, with buttons) |
| 2448 | uint64_t since = 0; | 2500 | uint64_t since = 0; |
| 2449 | }; | 2501 | }; |
| 2450 | static Stats st; | 2502 | static Stats st; |
| 2451 | std::string name; | 2503 | std::string name; |
| 2452 | bool buttons = false; | 2504 | bool buttons = false, a_button = false; |
| 2453 | if (SUCCEEDED(hr) && got) { | 2505 | if (SUCCEEDED(hr) && got) { |
| 2454 | IGameInputDevice* d = nullptr; | 2506 | IGameInputDevice* d = nullptr; |
| 2455 | got->GetDevice(&d); | 2507 | got->GetDevice(&d); |
| @@ -2458,7 +2510,10 @@ public: | |||
| 2458 | d->Release(); | 2510 | d->Release(); |
| 2459 | } | 2511 | } |
| 2460 | GameInputGamepadState g{}; | 2512 | GameInputGamepadState g{}; |
| 2461 | if (got->GetGamepadState(&g)) buttons = g.buttons != 0; | 2513 | if (got->GetGamepadState(&g)) { |
| 2514 | buttons = g.buttons != 0; | ||
| 2515 | a_button = (g.buttons & GameInputGamepadA) != 0; | ||
| 2516 | } | ||
| 2462 | } | 2517 | } |
| 2463 | const int arg = !asked ? 0 : as_device(asked)->info.deviceFamily == GameInputFamilyAggregate ? 1 : 2; | 2518 | const int arg = !asked ? 0 : as_device(asked)->info.deviceFamily == GameInputFamilyAggregate ? 1 : 2; |
| 2464 | std::lock_guard lock(st.mu); | 2519 | std::lock_guard lock(st.mu); |
| @@ -2466,6 +2521,7 @@ public: | |||
| 2466 | if (SUCCEEDED(hr) && got) { | 2521 | if (SUCCEEDED(hr) && got) { |
| 2467 | ++st.found[method]; | 2522 | ++st.found[method]; |
| 2468 | st.pressed[method] += buttons; | 2523 | st.pressed[method] += buttons; |
| 2524 | st.a_pressed[method] += a_button; | ||
| 2469 | auto& e = st.by_device[name]; | 2525 | auto& e = st.by_device[name]; |
| 2470 | ++e.first; | 2526 | ++e.first; |
| 2471 | e.second += buttons; | 2527 | e.second += buttons; |
| @@ -2477,10 +2533,10 @@ public: | |||
| 2477 | std::string line; | 2533 | std::string line; |
| 2478 | for (int m = 0; m < 3; ++m) { | 2534 | for (int m = 0; m < 3; ++m) { |
| 2479 | char t[200]; | 2535 | char t[200]; |
| 2480 | std::snprintf(t, sizeof(t), " %s %llu/%llu/%llu found %llu pressed %llu;", methods[m], | 2536 | std::snprintf(t, sizeof(t), " %s %llu/%llu/%llu found %llu pressed %llu A %llu;", methods[m], |
| 2481 | (unsigned long long)st.calls[m][0], (unsigned long long)st.calls[m][1], | 2537 | (unsigned long long)st.calls[m][0], (unsigned long long)st.calls[m][1], |
| 2482 | (unsigned long long)st.calls[m][2], (unsigned long long)st.found[m], | 2538 | (unsigned long long)st.calls[m][2], (unsigned long long)st.found[m], |
| 2483 | (unsigned long long)st.pressed[m]); | 2539 | (unsigned long long)st.pressed[m], (unsigned long long)st.a_pressed[m]); |
| 2484 | line += t; | 2540 | line += t; |
| 2485 | } | 2541 | } |
| 2486 | for (const auto& [n, c] : st.by_device) { | 2542 | for (const auto& [n, c] : st.by_device) { |
| @@ -2493,6 +2549,7 @@ public: | |||
| 2493 | std::memset(st.calls, 0, sizeof(st.calls)); | 2549 | std::memset(st.calls, 0, sizeof(st.calls)); |
| 2494 | std::memset(st.found, 0, sizeof(st.found)); | 2550 | std::memset(st.found, 0, sizeof(st.found)); |
| 2495 | std::memset(st.pressed, 0, sizeof(st.pressed)); | 2551 | std::memset(st.pressed, 0, sizeof(st.pressed)); |
| 2552 | std::memset(st.a_pressed, 0, sizeof(st.a_pressed)); | ||
| 2496 | st.by_device.clear(); | 2553 | st.by_device.clear(); |
| 2497 | st.since = now; | 2554 | st.since = now; |
| 2498 | } | 2555 | } |