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:
authorLuc <145881229+Joaco9020@users.noreply.github.com>2026-11-23 19:22:42 -0300
committersodove <mixercraft@gmail.com>2026-11-24 23:16:01 +0500
commit49f7133d96c8f9cbee2f5e04132a0f47ed728c4a (patch)
tree4397ba05197eab7383d0c12b83789c6450099e8c /runtime/shims/gameinput.cpp
parent3aa975421d470b76da1aa986a91282c59e20d642 (diff)
gameinput: StopCallback and UnregisterCallback take the current-callback token
GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE (all ones) names the callback that is running on the calling thread, as the host's GameInput does (measured by @Maraco91 on GameInput 0.2309): inside a callback, UnregisterCallback(CURRENT) removes it and returns true and StopCallback(CURRENT) stops it; outside one the token names nothing and UnregisterCallback returns false. The shim compared it with registered tokens, matched none, and the callback kept firing. call() records the running token in a thread_local, restored on return, and both methods resolve the constant through it. gameinput_test drives a self-cancelling reading callback through four button changes on the virtual Xbox pad, once per method. Finding: https://github.com/ProjectLeonida/Leonida/issues/9677#issuecomment-7115359105 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 402179cde..8b428d689 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -865,7 +865,19 @@ bool matches(const Callback& c, const Event& e) {
865 } 865 }
866} 866}
867 867
868// The token of the callback running on this thread, for kCurrentCallbackToken; 0 outside a callback.
869thread_local GameInputCallbackToken t_running_token = kInvalidCallbackToken;
870
871GameInputCallbackToken resolve_token(GameInputCallbackToken token) {
872 return token == kCurrentCallbackToken ? t_running_token : token;
873}
874
868void call(const Callback& c, const Event& e) { 875void call(const Callback& c, const Event& e) {
876 struct Running { // restored on return, so a callback that drives another delivery keeps its own token
877 GameInputCallbackToken saved = t_running_token;
878 explicit Running(GameInputCallbackToken t) { t_running_token = t; }
879 ~Running() { t_running_token = saved; }
880 } running(c.token);
869 switch (c.type) { 881 switch (c.type) {
870 case CbType::Device: 882 case CbType::Device:
871 reinterpret_cast<GameInputDeviceCallback>(c.fn)(c.token, c.context, e.device, e.timestamp, e.current, e.previous); 883 reinterpret_cast<GameInputDeviceCallback>(c.fn)(c.token, c.context, e.device, e.timestamp, e.current, e.previous);
@@ -2486,6 +2498,8 @@ public:
2486 } 2498 }
2487 2499
2488 void STDMETHODCALLTYPE StopCallback(GameInputCallbackToken token) override { 2500 void STDMETHODCALLTYPE StopCallback(GameInputCallbackToken token) override {
2501 token = resolve_token(token);
2502 if (token == kInvalidCallbackToken) return;
2489 std::lock_guard lock(g().mu); 2503 std::lock_guard lock(g().mu);
2490 for (Callback& c : g().callbacks) 2504 for (Callback& c : g().callbacks)
2491 if (c.token == token) c.active = false; 2505 if (c.token == token) c.active = false;
@@ -2494,6 +2508,8 @@ public:
2494 // Removes the callback and, unless called from inside a callback, waits for one that is running to return. 2508 // Removes the callback and, unless called from inside a callback, waits for one that is running to return.
2495 // VERIFY: the timeout is not honoured; the wait is for at most one callback. 2509 // VERIFY: the timeout is not honoured; the wait is for at most one callback.
2496 bool STDMETHODCALLTYPE UnregisterCallback(GameInputCallbackToken token, uint64_t) override { 2510 bool STDMETHODCALLTYPE UnregisterCallback(GameInputCallbackToken token, uint64_t) override {
2511 token = resolve_token(token);
2512 if (token == kInvalidCallbackToken) return false;
2497 State& s = g(); 2513 State& s = g();
2498 bool found = false; 2514 bool found = false;
2499 { 2515 {