upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 13:13:43 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 13:13:43 -0500
commit95d18113f37cfd42f8d8264fc3223efd7f2d60cb (patch)
treee72bae2b6ad4c2b446e9c07646e9b6c5ef93923f /runtime
parent8a600e14ebb7e4b1df31fb004c1c137b5797566b (diff)
parentee470192461a2911aa204cfecf9fdccb265a8b5f (diff)
Merge #2372 (ZroIsHere): advance scripted input and improve boot diagnostics
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/gameinput.cpp15
-rw-r--r--runtime/shims/gameinput_no_sdl.cpp82
2 files changed, 87 insertions, 10 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 5abb2a2fe..c989daff4 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -2495,13 +2495,13 @@ public:
2495 struct Stats { 2495 struct Stats {
2496 std::mutex mu; 2496 std::mutex mu;
2497 uint64_t calls[3][3] = {}; // method x (null, aggregate, a device) 2497 uint64_t calls[3][3] = {}; // method x (null, aggregate, a device)
2498 uint64_t found[3] = {}, pressed[3] = {}; 2498 uint64_t found[3] = {}, pressed[3] = {}, a_pressed[3] = {};
2499 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)
2500 uint64_t since = 0; 2500 uint64_t since = 0;
2501 }; 2501 };
2502 static Stats st; 2502 static Stats st;
2503 std::string name; 2503 std::string name;
2504 bool buttons = false; 2504 bool buttons = false, a_button = false;
2505 if (SUCCEEDED(hr) && got) { 2505 if (SUCCEEDED(hr) && got) {
2506 IGameInputDevice* d = nullptr; 2506 IGameInputDevice* d = nullptr;
2507 got->GetDevice(&d); 2507 got->GetDevice(&d);
@@ -2510,7 +2510,10 @@ public:
2510 d->Release(); 2510 d->Release();
2511 } 2511 }
2512 GameInputGamepadState g{}; 2512 GameInputGamepadState g{};
2513 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 }
2514 } 2517 }
2515 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;
2516 std::lock_guard lock(st.mu); 2519 std::lock_guard lock(st.mu);
@@ -2518,6 +2521,7 @@ public:
2518 if (SUCCEEDED(hr) && got) { 2521 if (SUCCEEDED(hr) && got) {
2519 ++st.found[method]; 2522 ++st.found[method];
2520 st.pressed[method] += buttons; 2523 st.pressed[method] += buttons;
2524 st.a_pressed[method] += a_button;
2521 auto& e = st.by_device[name]; 2525 auto& e = st.by_device[name];
2522 ++e.first; 2526 ++e.first;
2523 e.second += buttons; 2527 e.second += buttons;
@@ -2529,10 +2533,10 @@ public:
2529 std::string line; 2533 std::string line;
2530 for (int m = 0; m < 3; ++m) { 2534 for (int m = 0; m < 3; ++m) {
2531 char t[200]; 2535 char t[200];
2532 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],
2533 (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],
2534 (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],
2535 (unsigned long long)st.pressed[m]); 2539 (unsigned long long)st.pressed[m], (unsigned long long)st.a_pressed[m]);
2536 line += t; 2540 line += t;
2537 } 2541 }
2538 for (const auto& [n, c] : st.by_device) { 2542 for (const auto& [n, c] : st.by_device) {
@@ -2545,6 +2549,7 @@ public:
2545 std::memset(st.calls, 0, sizeof(st.calls)); 2549 std::memset(st.calls, 0, sizeof(st.calls));
2546 std::memset(st.found, 0, sizeof(st.found)); 2550 std::memset(st.found, 0, sizeof(st.found));
2547 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));
2548 st.by_device.clear(); 2553 st.by_device.clear();
2549 st.since = now; 2554 st.since = now;
2550 } 2555 }
diff --git a/runtime/shims/gameinput_no_sdl.cpp b/runtime/shims/gameinput_no_sdl.cpp
index 77b7ebccf..43f7b6b20 100644
--- a/runtime/shims/gameinput_no_sdl.cpp
+++ b/runtime/shims/gameinput_no_sdl.cpp
@@ -16,9 +16,9 @@
16// title waits at its first "press A" prompt forever. The pad is an Xbox One gamepad that is connected from the 16// title waits at its first "press A" prompt forever. The pad is an Xbox One gamepad that is connected from the
17// first GameInputCreate on: device enumeration through RegisterDeviceCallback (blocking, async, and through a 17// first GameInputCreate on: device enumeration through RegisterDeviceCallback (blocking, async, and through a
18// dispatcher), GetCurrentReading, GetNextReading and GetPreviousReading, and readings whose GetGamepadState and 18// dispatcher), GetCurrentReading, GetNextReading and GetPreviousReading, and readings whose GetGamepadState and
19// GetUiNavigationState follow the script. A reading is taken when the title asks for one, so no thread runs; 19// GetUiNavigationState follow the script. A clock thread advances the script every 8 ms and stores each state
20// reading callbacks, aggregate devices, rumble and every other device still need SDL3. Controllers and the 20// change so GetNextReading can observe edges while the title waits between reads; reading callbacks, aggregate
21// keyboard pad need SDL3 too. 21// devices, rumble and every other device still need SDL3. Controllers and the keyboard pad need SDL3 too.
22#include <windows.h> 22#include <windows.h>
23 23
24#include <algorithm> 24#include <algorithm>
@@ -101,6 +101,8 @@ constexpr uint32_t kAxisCount = 6; // LX, LY, RX, RY, LT, RT, as the SDL3 sh
101constexpr uint32_t kButtonCount = 15; // SDL's standard gamepad buttons, numbered as the script's Button::sdl 101constexpr uint32_t kButtonCount = 15; // SDL's standard gamepad buttons, numbered as the script's Button::sdl
102constexpr size_t kHistoryCap = 64; // readings kept for GetNextReading/GetPreviousReading; one per change 102constexpr size_t kHistoryCap = 64; // readings kept for GetNextReading/GetPreviousReading; one per change
103constexpr char kPadName[] = "Scripted pad (Leonida)"; 103constexpr char kPadName[] = "Scripted pad (Leonida)";
104std::atomic<bool> g_trace_readings{false};
105std::atomic<uint64_t> g_gamepad_state_queries{0};
104 106
105uint64_t qpc_now_us() { 107uint64_t qpc_now_us() {
106 static const LARGE_INTEGER freq = [] { LARGE_INTEGER f; QueryPerformanceFrequency(&f); return f; }(); 108 static const LARGE_INTEGER freq = [] { LARGE_INTEGER f; QueryPerformanceFrequency(&f); return f; }();
@@ -151,7 +153,14 @@ public:
151 return r; 153 return r;
152 } 154 }
153 155
154 uint32_t STDMETHODCALLTYPE GetInputKind() override { return kKinds; } 156 uint32_t STDMETHODCALLTYPE GetInputKind() override {
157 static std::atomic<uint64_t> queries{0};
158 const uint64_t query = queries.fetch_add(1, std::memory_order_relaxed) + 1;
159 if (g_trace_readings.load(std::memory_order_relaxed) && query <= 8)
160 std::fprintf(stderr, "[rt] gameinput: GetInputKind #%llu seq=%llu kinds=0x%08x\n",
161 static_cast<unsigned long long>(query), static_cast<unsigned long long>(seq_), kKinds);
162 return kKinds;
163 }
155 uint64_t STDMETHODCALLTYPE GetSequenceNumber(uint32_t) override { return seq_; } 164 uint64_t STDMETHODCALLTYPE GetSequenceNumber(uint32_t) override { return seq_; }
156 uint64_t STDMETHODCALLTYPE GetTimestamp() override { return ts_; } 165 uint64_t STDMETHODCALLTYPE GetTimestamp() override { return ts_; }
157 void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override; 166 void STDMETHODCALLTYPE GetDevice(IGameInputDevice** device) override;
@@ -168,6 +177,11 @@ public:
168 state_.leftTrigger, state_.rightTrigger}; 177 state_.leftTrigger, state_.rightTrigger};
169 const uint32_t n = std::min(count, kAxisCount); 178 const uint32_t n = std::min(count, kAxisCount);
170 std::memcpy(out, axes, n * sizeof(float)); 179 std::memcpy(out, axes, n * sizeof(float));
180 static std::atomic<uint64_t> queries{0};
181 const uint64_t query = queries.fetch_add(1, std::memory_order_relaxed) + 1;
182 if (g_trace_readings.load(std::memory_order_relaxed) && query <= 8)
183 std::fprintf(stderr, "[rt] gameinput: GetControllerAxisState #%llu seq=%llu count=%u returned=%u\n",
184 static_cast<unsigned long long>(query), static_cast<unsigned long long>(seq_), count, n);
171 return n; 185 return n;
172 } 186 }
173 uint32_t STDMETHODCALLTYPE GetControllerButtonCount() override { return kButtonCount; } 187 uint32_t STDMETHODCALLTYPE GetControllerButtonCount() override { return kButtonCount; }
@@ -177,6 +191,12 @@ public:
177 for (const script::Button& b : script::kButtons) buttons[b.sdl] = (state_.buttons & b.bit) != 0; 191 for (const script::Button& b : script::kButtons) buttons[b.sdl] = (state_.buttons & b.bit) != 0;
178 const uint32_t n = std::min(count, kButtonCount); 192 const uint32_t n = std::min(count, kButtonCount);
179 std::memcpy(out, buttons, n * sizeof(bool)); 193 std::memcpy(out, buttons, n * sizeof(bool));
194 static std::atomic<uint64_t> queries{0};
195 const uint64_t query = queries.fetch_add(1, std::memory_order_relaxed) + 1;
196 if (g_trace_readings.load(std::memory_order_relaxed) && query <= 8)
197 std::fprintf(stderr, "[rt] gameinput: GetControllerButtonState #%llu seq=%llu count=%u returned=%u buttons=0x%08x\n",
198 static_cast<unsigned long long>(query), static_cast<unsigned long long>(seq_), count, n,
199 state_.buttons);
180 return n; 200 return n;
181 } 201 }
182 uint32_t STDMETHODCALLTYPE GetControllerSwitchCount() override { return 0; } 202 uint32_t STDMETHODCALLTYPE GetControllerSwitchCount() override { return 0; }
@@ -192,12 +212,21 @@ public:
192 bool STDMETHODCALLTYPE GetGamepadState(GameInputGamepadState* state) override { 212 bool STDMETHODCALLTYPE GetGamepadState(GameInputGamepadState* state) override {
193 if (!state) return false; 213 if (!state) return false;
194 *state = state_; 214 *state = state_;
215 const uint64_t query = g_gamepad_state_queries.fetch_add(1, std::memory_order_relaxed) + 1;
216 if (g_trace_readings.load(std::memory_order_relaxed) && (query == 1 || state_.buttons || query % 1000 == 0))
217 std::fprintf(stderr, "[rt] gameinput: GetGamepadState #%llu seq=%llu buttons=0x%08x\n",
218 static_cast<unsigned long long>(query), static_cast<unsigned long long>(seq_), state_.buttons);
195 return true; 219 return true;
196 } 220 }
197 bool STDMETHODCALLTYPE GetRacingWheelState(void*) override { return false; } 221 bool STDMETHODCALLTYPE GetRacingWheelState(void*) override { return false; }
198 bool STDMETHODCALLTYPE GetUiNavigationState(GameInputUiNavigationState* state) override { 222 bool STDMETHODCALLTYPE GetUiNavigationState(GameInputUiNavigationState* state) override {
199 if (!state) return false; 223 if (!state) return false;
200 state->buttons = script::ui_navigation(state_); 224 state->buttons = script::ui_navigation(state_);
225 static std::atomic<uint64_t> queries{0};
226 const uint64_t query = queries.fetch_add(1, std::memory_order_relaxed) + 1;
227 if (g_trace_readings.load(std::memory_order_relaxed) && query <= 8)
228 std::fprintf(stderr, "[rt] gameinput: GetUiNavigationState #%llu seq=%llu buttons=0x%08x\n",
229 static_cast<unsigned long long>(query), static_cast<unsigned long long>(seq_), state->buttons);
201 return true; 230 return true;
202 } 231 }
203 232
@@ -278,7 +307,13 @@ public:
278 ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } 307 ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; }
279 ULONG STDMETHODCALLTYPE Release() override { return release_floor(refs_); } 308 ULONG STDMETHODCALLTYPE Release() override { return release_floor(refs_); }
280 309
281 const GameInputDeviceInfo* STDMETHODCALLTYPE GetDeviceInfo() override { return &info_; } 310 const GameInputDeviceInfo* STDMETHODCALLTYPE GetDeviceInfo() override {
311 static std::atomic<bool> logged{false};
312 if (g_trace_readings.load(std::memory_order_relaxed) && !logged.exchange(true, std::memory_order_relaxed))
313 std::fprintf(stderr, "[rt] gameinput: GetDeviceInfo family=%d input=0x%08x status=0x%08x\n",
314 info_.deviceFamily, info_.supportedInput, kConnectedStatus);
315 return &info_;
316 }
282 uint32_t STDMETHODCALLTYPE GetDeviceStatus() override { return kConnectedStatus; } 317 uint32_t STDMETHODCALLTYPE GetDeviceStatus() override { return kConnectedStatus; }
283 void STDMETHODCALLTYPE GetBatteryState(GameInputBatteryState* state) override { 318 void STDMETHODCALLTYPE GetBatteryState(GameInputBatteryState* state) override {
284 if (state) *state = {0, 0, 0, 0, GameInputBatteryUnknown}; 319 if (state) *state = {0, 0, 0, 0, GameInputBatteryUnknown};
@@ -356,7 +391,9 @@ struct State {
356 script::Script script; 391 script::Script script;
357 uint64_t origin_us = 0; // the script's clock starts at the first GameInputCreate 392 uint64_t origin_us = 0; // the script's clock starts at the first GameInputCreate
358 bool log_presses = true; // LEONIDA_INPUT_LOG 393 bool log_presses = true; // LEONIDA_INPUT_LOG
394 bool trace_reads = false; // LEONIDA_INPUT_TRACE
359 uint32_t logged_held = 0; // what log_scripted saw last 395 uint32_t logged_held = 0; // what log_scripted saw last
396 uint64_t reading_queries = 0;
360 std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading 397 std::deque<Reading*> history; // one reference each, oldest first; the newest is the current reading
361 uint64_t evicted_seq = 0; // sequence number of the newest reading dropped from history 398 uint64_t evicted_seq = 0; // sequence number of the newest reading dropped from history
362 uint64_t seq = 0; 399 uint64_t seq = 0;
@@ -395,7 +432,19 @@ void update(uint64_t now) {
395 } 432 }
396} 433}
397 434
435void trace_reading(State& s, const char* api, uint32_t kind, const Reading* reading) {
436 const uint64_t query = ++s.reading_queries;
437 const uint32_t buttons = reading ? reading->state().buttons : 0;
438 if (s.trace_reads && (query <= 128 || buttons || query % 1000 == 0))
439 std::fprintf(stderr, "[rt] gameinput: %s #%llu kind=0x%08x seq=%llu buttons=0x%08x\n", api,
440 static_cast<unsigned long long>(query), kind,
441 static_cast<unsigned long long>(reading ? reading->seq() : 0), buttons);
442}
443
398void call_device(const Callback& c, uint64_t timestamp) { 444void call_device(const Callback& c, uint64_t timestamp) {
445 if (g().trace_reads)
446 std::fprintf(stderr, "[rt] gameinput: device callback token=%llu timestamp=%llu status=0x%08x\n",
447 static_cast<unsigned long long>(c.token), static_cast<unsigned long long>(timestamp), kConnectedStatus);
399 reinterpret_cast<GameInputDeviceCallback>(c.fn)(c.token, c.context, &pad(), timestamp, kConnectedStatus, 448 reinterpret_cast<GameInputDeviceCallback>(c.fn)(c.token, c.context, &pad(), timestamp, kConnectedStatus,
400 GameInputDeviceNoStatus); 449 GameInputDeviceNoStatus);
401} 450}
@@ -504,6 +553,8 @@ HRESULT find_reading(IGameInputReading* ref, uint32_t kind, IGameInputReading**
504 std::lock_guard lock(s.mu); 553 std::lock_guard lock(s.mu);
505 update(now_us()); 554 update(now_us());
506 const uint64_t r = static_cast<Reading*>(ref)->seq(); 555 const uint64_t r = static_cast<Reading*>(ref)->seq();
556 trace_reading(s, forward ? "GetNextReading" : "GetPreviousReading", kind,
557 s.history.empty() ? nullptr : s.history.back());
507 if (forward ? s.evicted_seq > r : s.evicted_seq >= r) return GAMEINPUT_E_REFERENCE_READING_TOO_OLD; 558 if (forward ? s.evicted_seq > r : s.evicted_seq >= r) return GAMEINPUT_E_REFERENCE_READING_TOO_OLD;
508 Reading* best = nullptr; 559 Reading* best = nullptr;
509 for (Reading* c : s.history) { 560 for (Reading* c : s.history) {
@@ -547,6 +598,7 @@ public:
547 std::lock_guard lock(s.mu); 598 std::lock_guard lock(s.mu);
548 update(now_us()); 599 update(now_us());
549 Reading* r = s.history.back(); 600 Reading* r = s.history.back();
601 trace_reading(s, "GetCurrentReading", kind, r);
550 r->AddRef(); 602 r->AddRef();
551 *out = r; 603 *out = r;
552 return S_OK; 604 return S_OK;
@@ -595,6 +647,10 @@ public:
595 later = true; 647 later = true;
596 } 648 }
597 } 649 }
650 if (s.trace_reads)
651 std::fprintf(stderr,
652 "[rt] gameinput: RegisterDeviceCallback kind=0x%08x filter=0x%08x enumeration=%d report=%d\n",
653 kind, status_filter, static_cast<int>(enumeration), report ? 1 : 0);
598 if (token) *token = c.token; 654 if (token) *token = c.token;
599 // Blocking enumeration: the pad is reported before this returns, on the caller's thread. 655 // Blocking enumeration: the pad is reported before this returns, on the caller's thread.
600 if (report && enumeration == GameInputBlockingEnumeration) call_device(c, now_us()); 656 if (report && enumeration == GameInputBlockingEnumeration) call_device(c, now_us());
@@ -683,9 +739,25 @@ ScriptedInput* scripted() {
683 std::lock_guard lock(s.mu); 739 std::lock_guard lock(s.mu);
684 s.script = script::parse(text); 740 s.script = script::parse(text);
685 s.log_presses = script::env_lower("LEONIDA_INPUT_LOG") != "0"; 741 s.log_presses = script::env_lower("LEONIDA_INPUT_LOG") != "0";
742 s.trace_reads = script::env_lower("LEONIDA_INPUT_TRACE") == "1";
743 g_trace_readings = s.trace_reads;
686 s.wait_event = CreateEventW(nullptr, FALSE, FALSE, nullptr); 744 s.wait_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
687 s.origin_us = now_us(); 745 s.origin_us = now_us();
688 update(s.origin_us); // the first reading: nothing held unless the script presses at 0 s 746 update(s.origin_us); // the first reading: nothing held unless the script presses at 0 s
747 // Advance timed presses even when the title is waiting between GameInput queries. Keep each edge in the
748 // reading history so GetNextReading can observe it; GetCurrentReading still returns the state at query time.
749 try {
750 std::thread([] {
751 for (;;) {
752 Sleep(8);
753 State& state = g();
754 std::lock_guard lock(state.mu);
755 update(now_us());
756 }
757 }).detach();
758 } catch (...) {
759 std::fprintf(stderr, "[rt] gameinput: could not start the scripted input clock; updates will follow title reads\n");
760 }
689 std::fprintf(stderr, 761 std::fprintf(stderr,
690 "[rt] gameinput: served without SDL3: one scripted pad for LEONIDA_INPUT_SCRIPT (%zu scripted " 762 "[rt] gameinput: served without SDL3: one scripted pad for LEONIDA_INPUT_SCRIPT (%zu scripted "
691 "press(es), %zu rejected); controllers and the keyboard pad need SDL3\n", 763 "press(es), %zu rejected); controllers and the keyboard pad need SDL3\n",