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.cpp54
1 files changed, 52 insertions, 2 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index fa5889d6a..454923457 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -1899,7 +1899,21 @@ void update_keyboard_pad() {
1899 s.presses.size(), s.rejected); 1899 s.presses.size(), s.rejected);
1900 return s; 1900 return s;
1901 }(); 1901 }();
1902 if (!scripted.empty()) { 1902 // LEONIDA_INPUT_FILE=<path> (local, for headless runs whose display takes no keyboard input): every line appended
1903 // to the file names one button of gameinput_script.h's table ("a", "down", ...) and becomes a press at once, held
1904 // like a scripted one (300 ms and six readings, at most 5 s). The file is read again at most every 250 ms.
1905 struct FilePress {
1906 uint32_t bit;
1907 double at;
1908 uint64_t began;
1909 bool started;
1910 };
1911 static std::vector<FilePress> file_presses;
1912 static const std::string input_file = [] {
1913 const char* v = std::getenv("LEONIDA_INPUT_FILE");
1914 return std::string(v ? v : "");
1915 }();
1916 if (!scripted.empty() || !input_file.empty()) {
1903 static const ULONGLONG origin = GetTickCount64(); 1917 static const ULONGLONG origin = GetTickCount64();
1904 const double now = (GetTickCount64() - origin) / 1000.0; 1918 const double now = (GetTickCount64() - origin) / 1000.0;
1905 script::Held held{}; 1919 script::Held held{};
@@ -1908,6 +1922,42 @@ void update_keyboard_pad() {
1908 static std::vector<uint64_t> began(scripted.presses.size(), 0); 1922 static std::vector<uint64_t> began(scripted.presses.size(), 0);
1909 static std::vector<char> started(scripted.presses.size(), 0); 1923 static std::vector<char> started(scripted.presses.size(), 0);
1910 const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed); 1924 const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed);
1925 if (!input_file.empty()) {
1926 static ULONGLONG last_read = 0;
1927 static std::size_t lines_done = 0;
1928 const ULONGLONG tick = GetTickCount64();
1929 if (tick - last_read >= 250) {
1930 last_read = tick;
1931 if (FILE* f = std::fopen(input_file.c_str(), "r")) {
1932 char line[64];
1933 std::size_t n = 0;
1934 while (std::fgets(line, sizeof(line), f)) {
1935 if (++n <= lines_done) continue;
1936 lines_done = n;
1937 std::string name(line);
1938 while (!name.empty() && (name.back() == '\n' || name.back() == '\r' || name.back() == ' '))
1939 name.pop_back();
1940 bool known = false;
1941 for (const script::Button& b : script::kButtons)
1942 if (name == b.name) {
1943 file_presses.push_back({b.bit, now, 0, false});
1944 known = true;
1945 }
1946 std::fprintf(stderr, "[rt] gameinput: input file line %zu \"%s\" at %.1f s%s\n", n,
1947 name.c_str(), now, known ? "" : " (unknown button, ignored)");
1948 }
1949 std::fclose(f);
1950 }
1951 }
1952 for (auto& p : file_presses) {
1953 if (now < p.at || now >= p.at + kMaxHold) continue;
1954 if (!p.started) {
1955 p.started = true;
1956 p.began = calls;
1957 }
1958 if (now < p.at + 0.3 || calls - p.began < kReads) held.buttons |= p.bit;
1959 }
1960 }
1911 for (std::size_t i = 0; i < scripted.presses.size(); ++i) { 1961 for (std::size_t i = 0; i < scripted.presses.size(); ++i) {
1912 const auto& press = scripted.presses[i]; 1962 const auto& press = scripted.presses[i];
1913 bool active = false; 1963 bool active = false;
@@ -1924,7 +1974,7 @@ void update_keyboard_pad() {
1924 } 1974 }
1925 if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down 1975 if (held.stick) right_y = held.stick > 0 ? -kStickFull : kStickFull; // SDL's +Y is down
1926 for (const script::Button& b : script::kButtons) { 1976 for (const script::Button& b : script::kButtons) {
1927 if (!(scripted.named & b.bit)) continue; 1977 if (!(scripted.named & b.bit) && input_file.empty()) continue;
1928 const auto button = static_cast<SDL_GamepadButton>(b.sdl); 1978 const auto button = static_cast<SDL_GamepadButton>(b.sdl);
1929 SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]); 1979 SDL_SetJoystickVirtualButton(g_keyboard.js, button, (held.buttons & b.bit) || down[button]);
1930 } 1980 }