diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 4a42afbcfc..490e8ed23e 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -849,7 +849,19 @@ bool matches(const Callback& c, const Event& e) { } } +// The token of the callback running on this thread, for kCurrentCallbackToken; 0 outside a callback. +thread_local GameInputCallbackToken t_running_token = kInvalidCallbackToken; + +GameInputCallbackToken resolve_token(GameInputCallbackToken token) { + return token == kCurrentCallbackToken ? t_running_token : token; +} + void call(const Callback& c, const Event& e) { + struct Running { // restored on return, so a callback that drives another delivery keeps its own token + GameInputCallbackToken saved = t_running_token; + explicit Running(GameInputCallbackToken t) { t_running_token = t; } + ~Running() { t_running_token = saved; } + } running(c.token); switch (c.type) { case CbType::Device: reinterpret_cast(c.fn)(c.token, c.context, e.device, e.timestamp, e.current, e.previous); @@ -2429,6 +2441,8 @@ class GameInput final : public IGameInput { } void STDMETHODCALLTYPE StopCallback(GameInputCallbackToken token) override { + token = resolve_token(token); + if (token == kInvalidCallbackToken) return; std::lock_guard lock(g().mu); for (Callback& c : g().callbacks) if (c.token == token) c.active = false; @@ -2437,6 +2451,8 @@ class GameInput final : public IGameInput { // Removes the callback and, unless called from inside a callback, waits for one that is running to return. // VERIFY: the timeout is not honoured; the wait is for at most one callback. bool STDMETHODCALLTYPE UnregisterCallback(GameInputCallbackToken token, uint64_t) override { + token = resolve_token(token); + if (token == kInvalidCallbackToken) return false; State& s = g(); bool found = false; { diff --git a/runtime/shims/gameinput_abi.h b/runtime/shims/gameinput_abi.h index 83f2f5cbb5..86a0fa3a56 100644 --- a/runtime/shims/gameinput_abi.h +++ b/runtime/shims/gameinput_abi.h @@ -180,6 +180,9 @@ enum GameInputFocusPolicy : uint32_t { GameInputDefaultFocusPolicy = 0 }; using GameInputCallbackToken = uint64_t; constexpr GameInputCallbackToken kInvalidCallbackToken = 0; +// GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE: inside a callback, StopCallback / UnregisterCallback take it as the callback +// that is running; outside one it names nothing. +constexpr GameInputCallbackToken kCurrentCallbackToken = 0xFFFFFFFFFFFFFFFFull; // Error codes (facility 0x38A). constexpr HRESULT GAMEINPUT_E_DEVICE_DISCONNECTED = static_cast(0x838A0001L); diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 7217dc37b3..bcdbd80c8d 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp @@ -139,6 +139,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); @@ -1918,6 +1929,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);