diff options
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 37 |
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 | |||
| 168 | SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback | 168 | SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback |
| 169 | bool g_requeue_down = false; | 169 | bool g_requeue_down = false; |
| 170 | 170 | ||
| 171 | // A reading callback that cancels itself with GAMEINPUT_CURRENT_CALLBACK_TOKEN_VALUE on its first call. | ||
| 172 | IGameInput* g_self_input = nullptr; | ||
| 173 | bool g_self_stop = false; // StopCallback instead of UnregisterCallback | ||
| 174 | bool g_self_result = false; // what UnregisterCallback returned inside the callback | ||
| 175 | int g_self_calls = 0; | ||
| 176 | void 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 | |||
| 171 | void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { | 182 | void 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); |