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.cpp62
1 files changed, 57 insertions, 5 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index fa5889d6a..5abb2a2fe 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 }