From 49f7133d96c8f9cbee2f5e04132a0f47ed728c4a Mon Sep 17 00:00:00 2001 From: Luc <145881229+Joaco9020@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:22:42 -0300 Subject: 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 --- runtime/test/gameinput_test.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'runtime/test/gameinput_test.cpp') 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 SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback bool g_requeue_down = false; +// A reading callback that cancels itself with GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE on its first call. +IGameInput* g_self_input = nullptr; +bool g_self_stop = false; // StopCallback instead of UnregisterCallback +bool g_self_result = false; // what UnregisterCallback returned inside the callback +int g_self_calls = 0; +void CALLBACK on_self_cancel(GameInputCallbackToken, void*, IGameInputReading*, bool) { + if (++g_self_calls != 1) return; + if (g_self_stop) g_self_input->StopCallback(kCurrentCallbackToken); + else g_self_result = g_self_input->UnregisterCallback(kCurrentCallbackToken, 0); +} + void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { if (reading) ++g_reading_events; if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms); @@ -2056,6 +2067,32 @@ int main() { if (wait) CloseHandle(wait); if (dispatcher) dispatcher->Release(); + // ---- GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE: the running callback inside one, nothing outside ---- + // Measured on the host's GameInput (0.2309, Windows 11): UnregisterCallback(CURRENT) inside a callback returns + // true and removes it; outside a callback it returns false. + check(!input->UnregisterCallback(kCurrentCallbackToken, 0), "current token: UnregisterCallback outside a callback is false"); + input->StopCallback(kCurrentCallbackToken); // outside a callback: names nothing, stops nothing + g_self_input = input; + for (bool stop : {false, true}) { + g_self_stop = stop; + g_self_calls = 0; + g_self_result = false; + GameInputCallbackToken self_token = 0; + check(input->RegisterReadingCallback(dev, GameInputKindGamepad, 0.0f, nullptr, on_self_cancel, &self_token) == S_OK, + "current token: register the self-cancelling callback"); + for (bool down : {true, false, true, false}) { // four changes: without the token a callback sees several + SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_NORTH, down); + rt::gameinput::poll(); + } + check(g_self_calls == 1, stop ? "current token: StopCallback inside the callback stops it (one call for four changes)" + : "current token: UnregisterCallback inside the callback removes it (one call for four changes)"); + if (stop) + check(input->UnregisterCallback(self_token, 0), "current token: the stopped callback stays registered"); + else + check(g_self_result && !input->UnregisterCallback(self_token, 0), + "current token: UnregisterCallback returned true and the real token is gone"); + } + // ---- unplug and replug ---- const size_t events_before_unplug = g_device_events.size(); detach(xbox); -- cgit v1.2.3