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:
authorkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 01:49:49 +0000
committerkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 02:54:55 +0000
commit0e59c75b7ba15013b5939b9177bfd2aa93c8f155 (patch)
tree545d9b6a58227fe1aa1f69b62e5e1fe5f6efd935 /runtime/test/gameinput_test.cpp
parent67204bf8c0471335d9046890288b33e8d5e2875b (diff)
Runtime: GameInput shim on SDL3 (any controller; DualSense trigger effects, lightbar)
GameInputCreate and the v0 IGameInput/IGameInputDevice/IGameInputReading/IGameInputDispatcher surface a title uses for gamepads, served from SDL3 gamepads so DualSense, DualShock 4, Switch Pro and Xbox pads all work. Rumble goes to SDL_RumbleGamepad, impulse-trigger rumble to SDL_RumbleGamepadTriggers, and on a DualSense to adaptive-trigger effects through SDL_SendGamepadEffect (LEONIDA_DS_TRIGGERS=off|rumble|resistance). Lightbar via LEONIDA_DS_LIGHTBAR. SDL3 (zlib) comes from an installed package or a pinned, hash-checked release tarball via FetchContent; -DLEONIDA_SDL3=OFF skips the shim. gameinput_test covers the shim on SDL virtual joysticks; gameinput_probe is the manual check on a real pad. docs/gameinput.md has the mapping, settings and VERIFY list. AI-assisted (Claude Code). 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.cpp458
1 files changed, 458 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
new file mode 100644
index 000000000..1a154a7a9
--- /dev/null
+++ b/runtime/test/gameinput_test.cpp
@@ -0,0 +1,458 @@
1// Checks the GameInput shim the way a title uses it, on SDL virtual joysticks, so no controller is needed.
2//
3// GameInputCreate is resolved through the shim registry like any import. Real controller backends are turned off
4// with SDL hints, then virtual pads are attached and driven: an Xbox-style pad with trigger rumble, a DualSense
5// (Sony vendor/product id, so SDL treats it as one and the shim takes the trigger-effect path) and a plain pad
6// without trigger motors. The test covers enumeration and device callbacks, readings and their button/axis mapping,
7// UI navigation, the guide button, rumble and trigger rumble as SDL receives them, the DualSense effect bytes for
8// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, and unplugging and replugging. No game data.
9#include <windows.h>
10
11#include <SDL3/SDL.h>
12
13#include <cmath>
14#include <cstdint>
15#include <cstdio>
16#include <cstring>
17#include <vector>
18
19#include "gameinput.h"
20#include "gameinput_abi.h"
21#include "shims.h"
22
23using namespace gi;
24
25namespace {
26
27int g_checks = 0;
28int g_failures = 0;
29
30void check(bool condition, const char* what) {
31 ++g_checks;
32 std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
33 if (!condition) ++g_failures;
34}
35
36bool approx(float a, float b) { return std::fabs(a - b) < 0.002f; }
37
38// What SDL handed to one virtual pad.
39struct Pad {
40 SDL_JoystickID id = 0;
41 SDL_Joystick* js = nullptr;
42 int rumble_calls = 0, trigger_calls = 0, effect_calls = 0, led_calls = 0;
43 uint16_t low = 0, high = 0, left = 0, right = 0;
44 uint8_t effect[64] = {};
45 int effect_size = 0;
46 uint8_t led[3] = {};
47 int player = -100;
48};
49
50bool SDLCALL on_rumble(void* u, Uint16 lo, Uint16 hi) {
51 auto* p = static_cast<Pad*>(u);
52 ++p->rumble_calls;
53 p->low = lo;
54 p->high = hi;
55 return true;
56}
57bool SDLCALL on_triggers(void* u, Uint16 l, Uint16 r) {
58 auto* p = static_cast<Pad*>(u);
59 ++p->trigger_calls;
60 p->left = l;
61 p->right = r;
62 return true;
63}
64bool SDLCALL on_led(void* u, Uint8 r, Uint8 g, Uint8 b) {
65 auto* p = static_cast<Pad*>(u);
66 ++p->led_calls;
67 p->led[0] = r;
68 p->led[1] = g;
69 p->led[2] = b;
70 return true;
71}
72bool SDLCALL on_effect(void* u, const void* data, int size) {
73 auto* p = static_cast<Pad*>(u);
74 ++p->effect_calls;
75 p->effect_size = size;
76 std::memcpy(p->effect, data, std::min<size_t>(size, sizeof p->effect));
77 return true;
78}
79void SDLCALL on_player(void* u, int index) { static_cast<Pad*>(u)->player = index; }
80
81void attach(Pad& pad, const char* name, uint16_t vendor, uint16_t product, bool trigger_motors) {
82 SDL_VirtualJoystickDesc d;
83 SDL_INIT_INTERFACE(&d);
84 d.type = SDL_JOYSTICK_TYPE_GAMEPAD;
85 d.vendor_id = vendor;
86 d.product_id = product;
87 d.naxes = SDL_GAMEPAD_AXIS_COUNT;
88 d.nbuttons = SDL_GAMEPAD_BUTTON_COUNT;
89 d.button_mask = (1u << SDL_GAMEPAD_BUTTON_COUNT) - 1;
90 d.axis_mask = (1u << SDL_GAMEPAD_AXIS_COUNT) - 1;
91 d.name = name;
92 d.userdata = &pad;
93 d.Rumble = on_rumble;
94 d.RumbleTriggers = trigger_motors ? on_triggers : nullptr;
95 d.SetLED = on_led;
96 d.SendEffect = on_effect;
97 d.SetPlayerIndex = on_player;
98 pad.id = SDL_AttachVirtualJoystick(&d);
99 pad.js = pad.id ? SDL_OpenJoystick(pad.id) : nullptr;
100 if (pad.js) { // triggers at rest: the full-range virtual axis maps -32768 to 0
101 SDL_SetJoystickVirtualAxis(pad.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, -32768);
102 SDL_SetJoystickVirtualAxis(pad.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, -32768);
103 }
104}
105
106void detach(Pad& pad) {
107 if (pad.js) SDL_CloseJoystick(pad.js);
108 SDL_DetachVirtualJoystick(pad.id);
109 pad.js = nullptr;
110}
111
112// Callback recorders.
113struct DeviceEvent {
114 IGameInputDevice* device;
115 uint32_t current, previous;
116 GameInputCallbackToken token;
117};
118std::vector<DeviceEvent> g_device_events;
119int g_reading_events = 0;
120std::vector<bool> g_guide_events;
121
122void CALLBACK on_device(GameInputCallbackToken token, void*, IGameInputDevice* device, uint64_t, uint32_t current,
123 uint32_t previous) {
124 g_device_events.push_back({device, current, previous, token});
125}
126void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) {
127 if (reading) ++g_reading_events;
128}
129void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_t, bool pressed) {
130 g_guide_events.push_back(pressed);
131}
132
133void set_env(const char* name, const char* value) { SetEnvironmentVariableA(name, value); }
134
135} // namespace
136
137int main() {
138 // Only virtual pads: no real controller on the test machine may show up.
139 for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI,
140 SDL_HINT_JOYSTICK_GAMEINPUT, SDL_HINT_XINPUT_ENABLED, SDL_HINT_JOYSTICK_DIRECTINPUT})
141 SDL_SetHintWithPriority(hint, "0", SDL_HINT_OVERRIDE);
142 set_env("LEONIDA_GAMEINPUT_THREAD", "0");
143 set_env("LEONIDA_DS_TRIGGERS", nullptr);
144 set_env("LEONIDA_DS_LIGHTBAR", nullptr);
145 set_env("LEONIDA_RUMBLE", nullptr);
146
147 auto create = reinterpret_cast<GameInputCreateFn>(rt::shims::find("gameinput.dll", "GameInputCreate"));
148 check(create != nullptr, "gameinput.dll!GameInputCreate is registered");
149 check(rt::shims::find("xgameruntime.dll", "GameInputCreate") == reinterpret_cast<void*>(create),
150 "xgameruntime.dll!GameInputCreate is the same function");
151 if (!create) return 1;
152
153 IGameInput* input = nullptr;
154 check(create(&input) == S_OK && input, "GameInputCreate returns an instance");
155 check(create(nullptr) == E_POINTER, "GameInputCreate(nullptr) is E_POINTER");
156 IUnknown* unk = nullptr;
157 check(input->QueryInterface(IID_IGameInput, reinterpret_cast<void**>(&unk)) == S_OK && unk == input,
158 "QueryInterface(IID_IGameInput)");
159 if (unk) unk->Release();
160
161 rt::gameinput::poll(); // initialises SDL on this thread
162 check(rt::gameinput::connected_count() == 0, "no gamepads before any is attached");
163
164 IGameInputReading* reading = nullptr;
165 check(input->GetCurrentReading(GameInputKindGamepad, nullptr, &reading) == GAMEINPUT_E_READING_NOT_FOUND && !reading,
166 "GetCurrentReading with no device is READING_NOT_FOUND");
167
168 GameInputCallbackToken device_token = 0, reading_token = 0, guide_token = 0;
169 check(input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceConnected,
170 GameInputBlockingEnumeration, nullptr, on_device, &device_token) == S_OK &&
171 device_token != 0,
172 "RegisterDeviceCallback (blocking enumeration, nothing connected)");
173 check(g_device_events.empty(), "no enumeration callback without devices");
174 check(input->RegisterReadingCallback(nullptr, GameInputKindGamepad, 0.0f, nullptr, on_reading, &reading_token) == S_OK,
175 "RegisterReadingCallback");
176 check(input->RegisterGuideButtonCallback(nullptr, nullptr, on_guide, &guide_token) == S_OK, "RegisterGuideButtonCallback");
177
178 // ---- an Xbox-style pad with impulse triggers ----
179 Pad xbox;
180 attach(xbox, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
181 check(xbox.js != nullptr, "virtual Xbox pad attached");
182 rt::gameinput::poll();
183 check(rt::gameinput::connected_count() == 1, "the shim opened it");
184 check(g_device_events.size() == 1 && g_device_events[0].previous == GameInputDeviceNoStatus &&
185 (g_device_events[0].current & GameInputDeviceConnected),
186 "device callback: connected");
187 IGameInputDevice* dev = g_device_events.empty() ? nullptr : g_device_events[0].device;
188 if (!dev) return 1;
189 dev->AddRef();
190
191 const GameInputDeviceInfo* info = dev->GetDeviceInfo();
192 check(info->infoSize == sizeof(GameInputDeviceInfo), "info.infoSize");
193 check(info->vendorId == 0x045E && info->productId == 0x0B12, "info vendor and product id");
194 check((info->supportedInput & GameInputKindGamepad) && (info->supportedInput & GameInputKindUiNavigation),
195 "info.supportedInput has Gamepad and UiNavigation");
196 check(info->supportedRumbleMotors == (GameInputRumbleLowFrequency | GameInputRumbleHighFrequency |
197 GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger),
198 "info.supportedRumbleMotors: both motors and both triggers");
199 check(info->controllerAxisCount == 6 && info->controllerAxisInfo && info->gamepadInfo && info->displayName &&
200 std::strcmp(info->displayName->data, "Leonida virtual Xbox pad") == 0,
201 "info axis table, gamepad info and display name");
202 check(dev->GetDeviceStatus() & GameInputDeviceConnected, "GetDeviceStatus: connected");
203 check(xbox.player == 0, "player index 0 for the first pad");
204
205 // Initial reading: everything at rest.
206 check(input->GetCurrentReading(GameInputKindGamepad, dev, &reading) == S_OK && reading, "initial reading");
207 GameInputGamepadState st{};
208 if (reading) {
209 check(reading->GetGamepadState(&st) && st.buttons == 0 && approx(st.leftTrigger, 0) && approx(st.leftThumbstickX, 0),
210 "initial gamepad state is at rest");
211 IGameInputDevice* rd = nullptr;
212 reading->GetDevice(&rd);
213 check(rd == dev, "reading->GetDevice is the device");
214 if (rd) rd->Release();
215 }
216 const uint64_t seq0 = reading ? reading->GetSequenceNumber(GameInputKindGamepad) : 0;
217
218 // Press A, push the left stick up-right, pull the left trigger, press the right shoulder and dpad down.
219 const int reading_events_before = g_reading_events;
220 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_SOUTH, true);
221 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, true);
222 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_DPAD_DOWN, true);
223 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_START, true);
224 SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTX, 32767);
225 SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFTY, -32768); // SDL: up is negative
226 SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_RIGHTX, -32768);
227 SDL_SetJoystickVirtualAxis(xbox.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 32767);
228 rt::gameinput::poll();
229 check(g_reading_events > reading_events_before, "reading callback after a change");
230
231 IGameInputReading* next = nullptr;
232 check(input->GetNextReading(reading, GameInputKindGamepad, nullptr, &next) == S_OK && next,
233 "GetNextReading after a change");
234 if (next) next->Release();
235 if (reading) reading->Release();
236 reading = nullptr;
237 check(input->GetCurrentReading(GameInputKindGamepad, nullptr, &reading) == S_OK && reading,
238 "GetCurrentReading(any device)");
239 if (reading) {
240 st = {};
241 reading->GetGamepadState(&st);
242 std::printf(" buttons=0x%x lt=%.3f rt=%.3f lx=%.3f ly=%.3f rx=%.3f ry=%.3f\n", st.buttons, st.leftTrigger,
243 st.rightTrigger, st.leftThumbstickX, st.leftThumbstickY, st.rightThumbstickX, st.rightThumbstickY);
244 check(st.buttons == (GameInputGamepadA | GameInputGamepadRightShoulder | GameInputGamepadDPadDown | GameInputGamepadMenu),
245 "buttons: A, right shoulder, dpad down, Menu (Start)");
246 check(approx(st.leftThumbstickX, 1) && approx(st.leftThumbstickY, 1), "left stick up-right is (+1, +1)");
247 check(approx(st.rightThumbstickX, -1) && approx(st.rightThumbstickY, 0), "right stick left is (-1, 0)");
248 check(approx(st.leftTrigger, 1) && approx(st.rightTrigger, 0), "left trigger 1, right trigger 0");
249 check(reading->GetSequenceNumber(GameInputKindGamepad) > seq0, "sequence number advanced");
250 GameInputUiNavigationState ui{};
251 check(reading->GetUiNavigationState(&ui), "GetUiNavigationState");
252 const uint32_t want_ui = GameInputUiNavigationAccept | GameInputUiNavigationMenu | GameInputUiNavigationDown |
253 GameInputUiNavigationUp | GameInputUiNavigationRight | GameInputUiNavigationPageRight |
254 GameInputUiNavigationPageUp | GameInputUiNavigationScrollLeft;
255 check(ui.buttons == want_ui, "UI navigation derived from the pad");
256 float axes[8] = {};
257 check(reading->GetControllerAxisState(8, axes) == 6 && approx(axes[0], 1) && approx(axes[3], 0.5f) && approx(axes[4], 1),
258 "controller axes (0..1, sticks centred at 0.5)");
259 bool buttons[64] = {};
260 check(reading->GetControllerButtonState(64, buttons) == SDL_GAMEPAD_BUTTON_COUNT && buttons[SDL_GAMEPAD_BUTTON_SOUTH] &&
261 !buttons[SDL_GAMEPAD_BUTTON_EAST],
262 "controller buttons");
263 check(!reading->GetMotionState(nullptr) && !reading->GetMouseState(nullptr), "no motion or mouse state");
264 IGameInputReading* none = nullptr;
265 check(input->GetNextReading(reading, GameInputKindGamepad, nullptr, &none) == GAMEINPUT_E_READING_NOT_FOUND && !none,
266 "GetNextReading at the newest reading is READING_NOT_FOUND");
267 }
268 // Nothing changed: no new reading, no callback.
269 const int events_now = g_reading_events;
270 rt::gameinput::poll();
271 check(g_reading_events == events_now, "no reading callback without a change");
272
273 // Guide button.
274 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_GUIDE, true);
275 rt::gameinput::poll();
276 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_GUIDE, false);
277 rt::gameinput::poll();
278 check(g_guide_events.size() == 2 && g_guide_events[0] && !g_guide_events[1], "guide button callback: down, up");
279
280 // Rumble: both motors and both trigger motors reach SDL.
281 GameInputRumbleParams rumble{0.5f, 1.0f, 0.25f, 0.75f};
282 dev->SetRumbleState(&rumble);
283 rt::gameinput::poll();
284 std::printf(" rumble low=%u high=%u left=%u right=%u\n", xbox.low, xbox.high, xbox.left, xbox.right);
285 check(xbox.low == 32768 && xbox.high == 65535, "SetRumbleState motors -> SDL_RumbleGamepad");
286 check(xbox.left == 16384 && xbox.right == 49151, "SetRumbleState triggers -> SDL_RumbleGamepadTriggers");
287 check(xbox.effect_calls == 0, "no DualSense effect for an Xbox pad");
288 const int rumble_calls = xbox.rumble_calls;
289 rt::gameinput::poll();
290 check(xbox.rumble_calls == rumble_calls, "unchanged rumble is not re-sent every poll");
291 dev->SetRumbleState(nullptr);
292 rt::gameinput::poll();
293 check(xbox.low == 0 && xbox.high == 0 && xbox.left == 0 && xbox.right == 0, "SetRumbleState(nullptr) stops rumble");
294
295 set_env("LEONIDA_RUMBLE", "0");
296 rt::gameinput::reload_config();
297 dev->SetRumbleState(&rumble);
298 rt::gameinput::poll();
299 check(xbox.low == 0 && xbox.left == 0, "LEONIDA_RUMBLE=0 keeps the motors off");
300 set_env("LEONIDA_RUMBLE", nullptr);
301 rt::gameinput::reload_config();
302 dev->SetRumbleState(nullptr);
303 rt::gameinput::poll();
304
305 // ---- a DualSense: trigger rumble becomes adaptive-trigger effects ----
306 set_env("LEONIDA_DS_LIGHTBAR", "ff8000");
307 rt::gameinput::reload_config();
308 Pad ds;
309 attach(ds, "Leonida virtual DualSense", 0x054C, 0x0CE6, true);
310 rt::gameinput::poll();
311 rt::gameinput::OutputState out{};
312 check(rt::gameinput::connected_count() == 2 && rt::gameinput::output_state(1, &out) && out.is_dualsense,
313 "a Sony 054c:0ce6 pad is handled as a DualSense");
314 check(ds.player == 1, "player index 1 for the second pad");
315 check(ds.led_calls >= 1 && ds.led[0] == 0xFF && ds.led[1] == 0x80 && ds.led[2] == 0x00, "LEONIDA_DS_LIGHTBAR=ff8000");
316 IGameInputDevice* dsdev = g_device_events.empty() ? nullptr : g_device_events.back().device;
317 check(dsdev && dsdev != dev && (dsdev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger),
318 "DualSense device reported with trigger motors");
319 if (!dsdev) return 1;
320
321 GameInputRumbleParams ds_rumble{0.0f, 0.0f, 1.0f, 0.5f};
322 dsdev->SetRumbleState(&ds_rumble);
323 rt::gameinput::poll();
324 std::printf(" effect size=%d bits=%02x R=%02x %02x %02x %02x L=%02x %02x %02x %02x\n", ds.effect_size,
325 ds.effect[0], ds.effect[10], ds.effect[11], ds.effect[12], ds.effect[13], ds.effect[21], ds.effect[22],
326 ds.effect[23], ds.effect[24]);
327 check(ds.effect_size == 47 && ds.effect[0] == 0x0C, "effect block: 47 bytes, right+left trigger flags");
328 check(ds.effect[10] == 0x06 && ds.effect[11] == 30 && ds.effect[12] == 128, "rumble mode: right trigger vibrates at half");
329 check(ds.effect[21] == 0x06 && ds.effect[23] == 255, "rumble mode: left trigger vibrates at full");
330 check(ds.trigger_calls == 0, "a DualSense gets effects, not SDL_RumbleGamepadTriggers");
331
332 set_env("LEONIDA_DS_TRIGGERS", "resistance");
333 rt::gameinput::reload_config();
334 ds_rumble = {0, 0, 0.0f, 1.0f};
335 dsdev->SetRumbleState(&ds_rumble);
336 rt::gameinput::poll();
337 check(ds.effect[10] == 0x01 && ds.effect[12] == 255 && ds.effect[21] == 0x05,
338 "resistance mode: right trigger resists, left trigger off");
339
340 set_env("LEONIDA_DS_TRIGGERS", "off");
341 rt::gameinput::reload_config();
342 const int effect_calls = ds.effect_calls;
343 ds_rumble = {0, 0, 1.0f, 1.0f};
344 dsdev->SetRumbleState(&ds_rumble);
345 rt::gameinput::poll();
346 check(ds.effect_calls == effect_calls, "LEONIDA_DS_TRIGGERS=off sends no trigger effect");
347 set_env("LEONIDA_DS_TRIGGERS", nullptr);
348 set_env("LEONIDA_DS_LIGHTBAR", nullptr);
349 rt::gameinput::reload_config();
350
351 // The most recent reading of any device: move the DualSense.
352 SDL_SetJoystickVirtualButton(ds.js, SDL_GAMEPAD_BUTTON_EAST, true);
353 rt::gameinput::poll();
354 if (reading) reading->Release();
355 reading = nullptr;
356 if (input->GetCurrentReading(GameInputKindGamepad, nullptr, &reading) == S_OK && reading) {
357 IGameInputDevice* rd = nullptr;
358 reading->GetDevice(&rd);
359 st = {};
360 reading->GetGamepadState(&st);
361 check(rd == dsdev && st.buttons == GameInputGamepadB, "GetCurrentReading(any) follows the latest change (B on the DualSense)");
362 if (rd) rd->Release();
363 } else {
364 check(false, "GetCurrentReading(any) with two pads");
365 }
366
367 // ---- a pad without trigger motors ----
368 Pad plain;
369 attach(plain, "Leonida virtual plain pad", 0x057E, 0x2009, false); // Switch Pro ids: rumble, no trigger motors
370 rt::gameinput::poll();
371 IGameInputDevice* plaindev = g_device_events.empty() ? nullptr : g_device_events.back().device;
372 check(plaindev && (plaindev->GetDeviceInfo()->supportedRumbleMotors & GameInputRumbleLeftTrigger) == 0,
373 "a pad without trigger motors does not report them");
374 if (plaindev) {
375 GameInputRumbleParams p{0.25f, 0.25f, 1.0f, 1.0f};
376 plaindev->SetRumbleState(&p);
377 rt::gameinput::poll();
378 check(plain.low == 16384 && plain.trigger_calls == 0 && plain.effect_calls == 0,
379 "its body motors rumble and the trigger request is dropped");
380 }
381 detach(plain);
382 rt::gameinput::poll();
383
384 // ---- dispatcher: callbacks wait for Dispatch ----
385 IGameInputDispatcher* dispatcher = nullptr;
386 check(input->CreateDispatcher(&dispatcher) == S_OK && dispatcher, "CreateDispatcher");
387 HANDLE wait = nullptr;
388 check(dispatcher && dispatcher->OpenWaitHandle(&wait) == S_OK && wait, "OpenWaitHandle");
389 const int before_dispatch = g_reading_events;
390 SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_NORTH, true);
391 rt::gameinput::poll();
392 check(g_reading_events == before_dispatch, "with a dispatcher, poll does not run callbacks");
393 check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled");
394 if (dispatcher) dispatcher->Dispatch(1000);
395 check(g_reading_events > before_dispatch, "Dispatch runs them");
396 if (wait) CloseHandle(wait);
397 if (dispatcher) dispatcher->Release();
398
399 // ---- unplug and replug ----
400 const size_t events_before_unplug = g_device_events.size();
401 detach(xbox);
402 rt::gameinput::poll();
403 check(g_device_events.size() == events_before_unplug + 1 && g_device_events.back().device == dev &&
404 g_device_events.back().current == GameInputDeviceNoStatus &&
405 (g_device_events.back().previous & GameInputDeviceConnected),
406 "device callback: disconnected");
407 check(dev->GetDeviceStatus() == GameInputDeviceNoStatus, "GetDeviceStatus after unplug");
408 IGameInputReading* gone = nullptr;
409 check(input->GetCurrentReading(GameInputKindGamepad, dev, &gone) == GAMEINPUT_E_DEVICE_DISCONNECTED && !gone,
410 "GetCurrentReading on an unplugged device is DEVICE_DISCONNECTED");
411 AppLocalDeviceId id = info->deviceId;
412 IGameInputDevice* found = nullptr;
413 check(input->FindDeviceFromId(&id, &found) == S_OK && found == dev, "FindDeviceFromId");
414 if (found) found->Release();
415
416 Pad xbox2;
417 attach(xbox2, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
418 rt::gameinput::poll();
419 check(g_device_events.back().device == dev && (g_device_events.back().current & GameInputDeviceConnected),
420 "the same pad plugged back in is the same device object");
421 check(xbox2.player == 0, "it takes the free player slot 0");
422
423 // Async enumeration delivers on the next poll; unregistering stops callbacks.
424 std::vector<DeviceEvent> before = g_device_events;
425 GameInputCallbackToken async_token = 0;
426 input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceConnected, GameInputAsyncEnumeration,
427 nullptr, on_device, &async_token);
428 check(g_device_events.size() == before.size(), "async enumeration does not call back before returning");
429 rt::gameinput::poll();
430 size_t async_events = 0;
431 for (size_t i = before.size(); i < g_device_events.size(); ++i) async_events += g_device_events[i].token == async_token;
432 check(async_events == 2, "async enumeration reports both connected pads to the new callback only");
433
434 check(input->UnregisterCallback(reading_token, 0), "UnregisterCallback");
435 check(!input->UnregisterCallback(reading_token, 0), "a second UnregisterCallback returns false");
436 const int after_unregister = g_reading_events;
437 SDL_SetJoystickVirtualButton(xbox2.js, SDL_GAMEPAD_BUTTON_WEST, true);
438 rt::gameinput::poll();
439 check(g_reading_events == after_unregister, "no reading callback after unregistering");
440
441 // Blocking enumeration with pads present runs before returning.
442 before = g_device_events;
443 GameInputCallbackToken blocking_token = 0;
444 input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceConnected, GameInputBlockingEnumeration,
445 nullptr, on_device, &blocking_token);
446 check(g_device_events.size() == before.size() + 2, "blocking enumeration reports both pads before returning");
447
448 if (reading) reading->Release();
449 dev->Release();
450 detach(xbox2);
451 detach(ds);
452 rt::gameinput::poll();
453 check(rt::gameinput::connected_count() == 0, "all pads closed after detaching");
454 input->Release();
455
456 std::printf("%d checks, %d failures\n", g_checks, g_failures);
457 return g_failures ? 1 : 0;
458}