Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions runtime/shims/gameinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameInputDeviceCallback>(c.fn)(c.token, c.context, e.device, e.timestamp, e.current, e.previous);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
{
Expand Down
3 changes: 3 additions & 0 deletions runtime/shims/gameinput_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HRESULT>(0x838A0001L);
Expand Down
37 changes: 37 additions & 0 deletions runtime/test/gameinput_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading