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.cpp77
1 files changed, 67 insertions, 10 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 478d1826c..238c98ba6 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -1890,7 +1890,9 @@ void update_keyboard_pad() {
1890 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); 1890 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours());
1891 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); 1891 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]);
1892 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard 1892 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard
1893 // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt 1893 // pad, in seconds since the pad's first update, each held 300 ms or its own button@seconds:hold, and then until the
1894 // title has read the pads kReads times since it began (never past kMaxHold seconds, or the hold if it is longer, so
1895 // a title that stops reading does not keep the button). For unattended boots: the title's first prompt
1894 // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the 1896 // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). The parser, the timeline and the
1895 // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3. 1897 // button names are gameinput_script.h's, shared with the scripted pad of a build without SDL3.
1896 namespace script = rt::gameinput::script; 1898 namespace script = rt::gameinput::script;
@@ -1902,7 +1904,21 @@ void update_keyboard_pad() {
1902 s.presses.size(), s.rejected); 1904 s.presses.size(), s.rejected);
1903 return s; 1905 return s;
1904 }(); 1906 }();
1905 if (!scripted.empty()) { 1907 // LEONIDA_INPUT_FILE=<path> (local, for headless runs whose display takes no keyboard input): every line appended
1908 // to the file names one button of gameinput_script.h's table ("a", "down", ...) and becomes a press at once, held
1909 // like a scripted one (300 ms and six readings, at most 5 s). The file is read again at most every 250 ms.
1910 struct FilePress {
1911 uint32_t bit;
1912 double at;
1913 uint64_t began;
1914 bool started;
1915 };
1916 static std::vector<FilePress> file_presses;
1917 static const std::string input_file = [] {
1918 const char* v = std::getenv("LEONIDA_INPUT_FILE");
1919 return std::string(v ? v : "");
1920 }();
1921 if (!scripted.empty() || !input_file.empty()) {
1906 static const ULONGLONG origin = GetTickCount64(); 1922 static const ULONGLONG origin = GetTickCount64();
1907 const double now = (GetTickCount64() - origin) / 1000.0; 1923 const double now = (GetTickCount64() - origin) / 1000.0;
1908 script::Held held{}; 1924 script::Held held{};
@@ -1911,15 +1927,51 @@ void update_keyboard_pad() {
1911 static std::vector<uint64_t> began(scripted.presses.size(), 0); 1927 static std::vector<uint64_t> began(scripted.presses.size(), 0);
1912 static std::vector<char> started(scripted.presses.size(), 0); 1928 static std::vector<char> started(scripted.presses.size(), 0);
1913 const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed); 1929 const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed);
1930 if (!input_file.empty()) {
1931 static ULONGLONG last_read = 0;
1932 static std::size_t lines_done = 0;
1933 const ULONGLONG tick = GetTickCount64();
1934 if (tick - last_read >= 250) {
1935 last_read = tick;
1936 if (FILE* f = std::fopen(input_file.c_str(), "r")) {
1937 char line[64];
1938 std::size_t n = 0;
1939 while (std::fgets(line, sizeof(line), f)) {
1940 if (++n <= lines_done) continue;
1941 lines_done = n;
1942 std::string name(line);
1943 while (!name.empty() && (name.back() == '\n' || name.back() == '\r' || name.back() == ' '))
1944 name.pop_back();
1945 bool known = false;
1946 for (const script::Button& b : script::kButtons)
1947 if (name == b.name) {
1948 file_presses.push_back({b.bit, now, 0, false});
1949 known = true;
1950 }
1951 std::fprintf(stderr, "[rt] gameinput: input file line %zu \"%s\" at %.1f s%s\n", n,
1952 name.c_str(), now, known ? "" : " (unknown button, ignored)");
1953 }
1954 std::fclose(f);
1955 }
1956 }
1957 for (auto& p : file_presses) {
1958 if (now < p.at || now >= p.at + kMaxHold) continue;
1959 if (!p.started) {
1960 p.started = true;
1961 p.began = calls;
1962 }
1963 if (now < p.at + 0.3 || calls - p.began < kReads) held.buttons |= p.bit;
1964 }
1965 }
1914 for (std::size_t i = 0; i < scripted.presses.size(); ++i) { 1966 for (std::size_t i = 0; i < scripted.presses.size(); ++i) {
1915 const auto& press = scripted.presses[i]; 1967 const auto& press = scripted.presses[i];
1916 bool active = false; 1968 bool active = false;
1917 if (now >= press.at && now < press.at + kMaxHold) { 1969 if (now >= press.at && now < press.at + std::max(press.hold, kMaxHold)) {
1918 if (!started[i]) { 1970 if (!started[i]) {
1919 started[i] = 1; 1971 started[i] = 1;
1920 began[i] = calls; 1972 began[i] = calls;
1921 } 1973 }
1922 active = now < press.at + 0.3 || calls - began[i] < kReads; 1974 active = now < press.at + press.hold || calls - began[i] < kReads;
1923 } 1975 }
1924 if (!active) continue; 1976 if (!active) continue;
1925 if (press.stick) held.stick = press.stick; 1977 if (press.stick) held.stick = press.stick;
@@ -1927,7 +1979,7 @@ void update_keyboard_pad() {
1927 } 1979 }
1928 if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down 1980 if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down
1929 for (const script::Button& b : script::kButtons) { 1981 for (const script::Button& b : script::kButtons) {
1930 if (!(scripted.named & b.bit)) continue; 1982 if (!(scripted.named & b.bit) && input_file.empty()) continue;
1931 const auto button = static_cast<SDL_GamepadButton>(b.sdl); 1983 const auto button = static_cast<SDL_GamepadButton>(b.sdl);
1932 SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]); 1984 SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]);
1933 } 1985 }
@@ -2446,13 +2498,13 @@ public:
2446 struct Stats { 2498 struct Stats {
2447 std::mutex mu; 2499 std::mutex mu;
2448 uint64_t calls[3][3] = {}; // method x (null, aggregate, a device) 2500 uint64_t calls[3][3] = {}; // method x (null, aggregate, a device)
2449 uint64_t found[3] = {}, pressed[3] = {}; 2501 uint64_t found[3] = {}, pressed[3] = {}, a_pressed[3] = {};
2450 std::map<std::string, std::pair<uint64_t, uint64_t>> by_device; // name -> (readings, with buttons) 2502 std::map<std::string, std::pair<uint64_t, uint64_t>> by_device; // name -> (readings, with buttons)
2451 uint64_t since = 0; 2503 uint64_t since = 0;
2452 }; 2504 };
2453 static Stats st; 2505 static Stats st;
2454 std::string name; 2506 std::string name;
2455 bool buttons = false; 2507 bool buttons = false, a_button = false;
2456 if (SUCCEEDED(hr) && got) { 2508 if (SUCCEEDED(hr) && got) {
2457 IGameInputDevice* d = nullptr; 2509 IGameInputDevice* d = nullptr;
2458 got->GetDevice(&d); 2510 got->GetDevice(&d);
@@ -2461,7 +2513,10 @@ public:
2461 d->Release(); 2513 d->Release();
2462 } 2514 }
2463 GameInputGamepadState g{}; 2515 GameInputGamepadState g{};
2464 if (got->GetGamepadState(&g)) buttons = g.buttons != 0; 2516 if (got->GetGamepadState(&g)) {
2517 buttons = g.buttons != 0;
2518 a_button = (g.buttons & GameInputGamepadA) != 0;
2519 }
2465 } 2520 }
2466 const int arg = !asked ? 0 : as_device(asked)->info.deviceFamily == GameInputFamilyAggregate ? 1 : 2; 2521 const int arg = !asked ? 0 : as_device(asked)->info.deviceFamily == GameInputFamilyAggregate ? 1 : 2;
2467 std::lock_guard lock(st.mu); 2522 std::lock_guard lock(st.mu);
@@ -2469,6 +2524,7 @@ public:
2469 if (SUCCEEDED(hr) && got) { 2524 if (SUCCEEDED(hr) && got) {
2470 ++st.found[method]; 2525 ++st.found[method];
2471 st.pressed[method] += buttons; 2526 st.pressed[method] += buttons;
2527 st.a_pressed[method] += a_button;
2472 auto& e = st.by_device[name]; 2528 auto& e = st.by_device[name];
2473 ++e.first; 2529 ++e.first;
2474 e.second += buttons; 2530 e.second += buttons;
@@ -2480,10 +2536,10 @@ public:
2480 std::string line; 2536 std::string line;
2481 for (int m = 0; m < 3; ++m) { 2537 for (int m = 0; m < 3; ++m) {
2482 char t[200]; 2538 char t[200];
2483 std::snprintf(t, sizeof(t), " %s %llu/%llu/%llu found %llu pressed %llu;", methods[m], 2539 std::snprintf(t, sizeof(t), " %s %llu/%llu/%llu found %llu pressed %llu A %llu;", methods[m],
2484 (unsigned long long)st.calls[m][0], (unsigned long long)st.calls[m][1], 2540 (unsigned long long)st.calls[m][0], (unsigned long long)st.calls[m][1],
2485 (unsigned long long)st.calls[m][2], (unsigned long long)st.found[m], 2541 (unsigned long long)st.calls[m][2], (unsigned long long)st.found[m],
2486 (unsigned long long)st.pressed[m]); 2542 (unsigned long long)st.pressed[m], (unsigned long long)st.a_pressed[m]);
2487 line += t; 2543 line += t;
2488 } 2544 }
2489 for (const auto& [n, c] : st.by_device) { 2545 for (const auto& [n, c] : st.by_device) {
@@ -2496,6 +2552,7 @@ public:
2496 std::memset(st.calls, 0, sizeof(st.calls)); 2552 std::memset(st.calls, 0, sizeof(st.calls));
2497 std::memset(st.found, 0, sizeof(st.found)); 2553 std::memset(st.found, 0, sizeof(st.found));
2498 std::memset(st.pressed, 0, sizeof(st.pressed)); 2554 std::memset(st.pressed, 0, sizeof(st.pressed));
2555 std::memset(st.a_pressed, 0, sizeof(st.a_pressed));
2499 st.by_device.clear(); 2556 st.by_device.clear();
2500 st.since = now; 2557 st.since = now;
2501 } 2558 }