upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/gameinput_test.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/test/gameinput_test.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/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 93c48518b..6dccb4660 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -168,6 +168,17 @@ DWORD g_reading_sleep_ms = 0; // makes on_reading slow, for the Dispatch quota
168SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback 168SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback
169bool g_requeue_down = false; 169bool g_requeue_down = false;
170 170
171// A reading callback that cancels itself with GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE on its first call.
172IGameInput* g_self_input = nullptr;
173bool g_self_stop = false; // StopCallback instead of UnregisterCallback
174bool g_self_result = false; // what UnregisterCallback returned inside the callback
175int g_self_calls = 0;
176void CALLBACK on_self_cancel(GameInputCallbackToken, void*, IGameInputReading*, bool) {
177 if (++g_self_calls != 1) return;
178 if (g_self_stop) g_self_input->StopCallback(kCurrentCallbackToken);
179 else g_self_result = g_self_input->UnregisterCallback(kCurrentCallbackToken, 0);
180}
181
171void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { 182void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) {
172 if (reading) ++g_reading_events; 183 if (reading) ++g_reading_events;
173 if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms); 184 if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms);
@@ -2056,6 +2067,32 @@ int main() {
2056 if (wait) CloseHandle(wait); 2067 if (wait) CloseHandle(wait);
2057 if (dispatcher) dispatcher->Release(); 2068 if (dispatcher) dispatcher->Release();
2058 2069
2070 // ---- GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE: the running callback inside one, nothing outside ----
2071 // Measured on the host's GameInput (0.2309, Windows 11): UnregisterCallback(CURRENT) inside a callback returns
2072 // true and removes it; outside a callback it returns false.
2073 check(!input->UnregisterCallback(kCurrentCallbackToken, 0), "current token: UnregisterCallback outside a callback is false");
2074 input->StopCallback(kCurrentCallbackToken); // outside a callback: names nothing, stops nothing
2075 g_self_input = input;
2076 for (bool stop : {false, true}) {
2077 g_self_stop = stop;
2078 g_self_calls = 0;
2079 g_self_result = false;
2080 GameInputCallbackToken self_token = 0;
2081 check(input->RegisterReadingCallback(dev, GameInputKindGamepad, 0.0f, nullptr, on_self_cancel, &self_token) == S_OK,
2082 "current token: register the self-cancelling callback");
2083 for (bool down : {true, false, true, false}) { // four changes: without the token a callback sees several
2084 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_NORTH, down);
2085 rt::gameinput::poll();
2086 }
2087 check(g_self_calls == 1, stop ? "current token: StopCallback inside the callback stops it (one call for four changes)"
2088 : "current token: UnregisterCallback inside the callback removes it (one call for four changes)");
2089 if (stop)
2090 check(input->UnregisterCallback(self_token, 0), "current token: the stopped callback stays registered");
2091 else
2092 check(g_self_result && !input->UnregisterCallback(self_token, 0),
2093 "current token: UnregisterCallback returned true and the real token is gone");
2094 }
2095
2059 // ---- unplug and replug ---- 2096 // ---- unplug and replug ----
2060 const size_t events_before_unplug = g_device_events.size(); 2097 const size_t events_before_unplug = g_device_events.size();
2061 detach(xbox); 2098 detach(xbox);