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:
authornicolas <44988633+nschlaepfer@users.noreply.github.com>2026-11-21 22:58:39 -0700
committernicolas <44988633+nschlaepfer@users.noreply.github.com>2026-11-21 22:59:21 -0700
commit9eaf37e96f362d39ddb5a96e6660b37972839153 (patch)
treef1cba023f6cb0d19809c01fe9d81b6a1c73a652f /runtime/test/gameinput_test.cpp
parent43d470316ec6cbcd64002bc3792998d9f5745dac (diff)
Distinguish identical connected gamepads in device-ID lookup
Diffstat (limited to 'runtime/test/gameinput_test.cpp')
-rw-r--r--runtime/test/gameinput_test.cpp114
1 files changed, 112 insertions, 2 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp
index 531268d1a..9ffe2d337 100644
--- a/runtime/test/gameinput_test.cpp
+++ b/runtime/test/gameinput_test.cpp
@@ -5,8 +5,8 @@
5// (Sony vendor/product id, so SDL treats it as one and the shim takes the trigger-effect path) and a plain pad 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, 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 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, unplugging and replugging, and the reading history behind 8// each LEONIDA_DS_TRIGGERS mode, the lightbar, a dispatcher, unplug/replug, reading history,
9// GetNextReading/GetPreviousReading. No game data. 9// and identification of two or three otherwise indistinguishable pads. No game data.
10#include <windows.h> 10#include <windows.h>
11 11
12#include <SDL3/SDL.h> 12#include <SDL3/SDL.h>
@@ -15,6 +15,7 @@
15#include <cstdint> 15#include <cstdint>
16#include <cstdio> 16#include <cstdio>
17#include <cstring> 17#include <cstring>
18#include <string>
18#include <vector> 19#include <vector>
19 20
20#include "gameinput.h" 21#include "gameinput.h"
@@ -304,6 +305,114 @@ void test_keyboard_pad(IGameInput* input) {
304 rt::gameinput::set_keyboard_source(nullptr, nullptr); 305 rt::gameinput::set_keyboard_source(nullptr, nullptr);
305} 306}
306 307
308bool same_id(const AppLocalDeviceId& a, const AppLocalDeviceId& b) { return std::memcmp(&a, &b, sizeof a) == 0; }
309
310// The id the shim derives from SDL: the GUID, then two FNV-1a hashes of "s:" serial, "p:" path or nothing, with
311// "t:<n>:" before it for twin n. Twin 0 is the id every pad had before twins were told apart.
312AppLocalDeviceId expected_id(const Pad& pad, int twin) {
313 const char* serial = SDL_GetJoystickSerial(pad.js);
314 const char* path = SDL_GetJoystickPath(pad.js);
315 std::string identity = (serial && *serial) ? std::string("s:") + serial : (path && *path) ? std::string("p:") + path : "";
316 if (twin) identity = "t:" + std::to_string(twin) + ":" + identity;
317 AppLocalDeviceId id{};
318 const SDL_GUID guid = SDL_GetJoystickGUIDForID(pad.id);
319 std::memcpy(id.value, guid.data, 16);
320 for (int half = 0; half < 2; ++half) {
321 uint64_t h = half ? 0x84222325cbf29ce4ull : 0xcbf29ce484222325ull;
322 for (unsigned char c : identity) h = (h ^ c) * 0x100000001b3ull;
323 std::memcpy(id.value + 16 + half * 8, &h, 8);
324 }
325 return id;
326}
327
328// Attaches one more identical twin and returns the device the shim reported for it.
329IGameInputDevice* plug(Pad& pad) {
330 const size_t before = g_device_events.size();
331 attach(pad, "Leonida virtual twin pad", 0x0F0D, 0x00C1, false);
332 rt::gameinput::poll();
333 return g_device_events.size() > before ? g_device_events.back().device : nullptr;
334}
335
336bool found_is(IGameInput* input, IGameInputDevice* dev) {
337 if (!dev) return false;
338 IGameInputDevice* found = nullptr;
339 const bool ok = input->FindDeviceFromId(&dev->GetDeviceInfo()->deviceId, &found) == S_OK && found == dev;
340 if (found) found->Release();
341 return ok;
342}
343
344// Pads SDL cannot tell apart: the same name, vendor and product, no serial, no path. Runs with no other pad connected.
345void test_twins(IGameInput* input) {
346 Pad pads[3];
347 IGameInputDevice* dev[3] = {};
348 AppLocalDeviceId id[3] = {};
349 for (int i = 0; i < 2; ++i) dev[i] = plug(pads[i]);
350 check(dev[0] && dev[1] && dev[0] != dev[1], "twins: two identical pads are two device objects");
351 if (!dev[0] || !dev[1]) {
352 for (Pad& p : pads) detach(p);
353 rt::gameinput::poll();
354 return;
355 }
356 const AppLocalDeviceId legacy = expected_id(pads[0], 0);
357 check(same_id(legacy, expected_id(pads[1], 0)), "twins: SDL gives them nothing to tell them apart");
358 for (int i = 0; i < 2; ++i) id[i] = dev[i]->GetDeviceInfo()->deviceId;
359 check(same_id(id[0], legacy), "twins: the first keeps the id a single pad has always had");
360 check(!same_id(id[0], id[1]) && same_id(id[1], expected_id(pads[1], 1)),
361 "twins: the second gets twin id 1, same GUID bytes, different from the first");
362 check(found_is(input, dev[0]) && found_is(input, dev[1]), "twins: FindDeviceFromId returns each twin for its own id");
363
364 SDL_SetJoystickVirtualButton(pads[0].js, SDL_GAMEPAD_BUTTON_SOUTH, true);
365 SDL_SetJoystickVirtualButton(pads[1].js, SDL_GAMEPAD_BUTTON_EAST, true);
366 rt::gameinput::poll();
367 check(pad_state(input, dev[0]).buttons == GameInputGamepadA && pad_state(input, dev[1]).buttons == GameInputGamepadB,
368 "twins: a reading filtered to each twin has only its own button");
369 IGameInputDevice* found = nullptr;
370 input->FindDeviceFromId(&id[1], &found);
371 check(found && pad_state(input, found).buttons == GameInputGamepadB,
372 "twins: the device found from the second id reads the second pad");
373 if (found) found->Release();
374
375 dev[2] = plug(pads[2]);
376 if (dev[2]) id[2] = dev[2]->GetDeviceInfo()->deviceId;
377 check(dev[2] && dev[2] != dev[0] && dev[2] != dev[1] && same_id(id[2], expected_id(pads[2], 2)) &&
378 !same_id(id[2], id[0]) && !same_id(id[2], id[1]) && found_is(input, dev[2]),
379 "twins: a third gets a third object and twin id 2");
380
381 // One twin unplugged: it gets its object and id back.
382 detach(pads[1]);
383 rt::gameinput::poll();
384 check(dev[1]->GetDeviceStatus() == GameInputDeviceNoStatus && found_is(input, dev[1]),
385 "twins: an unplugged twin is still found from its id");
386 check(plug(pads[1]) == dev[1] && same_id(dev[1]->GetDeviceInfo()->deviceId, id[1]),
387 "twins: the middle twin plugged back in gets its object and id back");
388 detach(pads[0]);
389 rt::gameinput::poll();
390 check(plug(pads[0]) == dev[0] && same_id(dev[0]->GetDeviceInfo()->deviceId, legacy),
391 "twins: so does the first, with the legacy id");
392
393 // Two unplugged: nothing tells them apart, so the first back takes the lower twin whichever pad it is.
394 detach(pads[1]);
395 detach(pads[2]);
396 rt::gameinput::poll();
397 IGameInputDevice* first_back = plug(pads[2]);
398 IGameInputDevice* second_back = plug(pads[1]);
399 check(first_back == dev[1] && second_back == dev[2], "twins: with two unplugged, objects follow arrival order");
400
401 // All unplugged and plugged in again: the same objects and ids in creation order, no new object.
402 for (Pad& p : pads) detach(p);
403 rt::gameinput::poll();
404 bool same = true;
405 for (int i = 0; i < 3; ++i) {
406 IGameInputDevice* d = plug(pads[i]);
407 same &= d && d == dev[i] && same_id(d->GetDeviceInfo()->deviceId, id[i]);
408 }
409 check(same && rt::gameinput::connected_count() == 3, "twins: all three replugged get the same objects and ids in order");
410
411 for (Pad& p : pads) detach(p);
412 rt::gameinput::poll();
413 check(rt::gameinput::connected_count() == 0, "twins: removed at the end");
414}
415
307} // namespace 416} // namespace
308 417
309int main() { 418int main() {
@@ -752,6 +861,7 @@ int main() {
752 rt::gameinput::poll(); 861 rt::gameinput::poll();
753 check(rt::gameinput::connected_count() == 0, "all pads closed after detaching"); 862 check(rt::gameinput::connected_count() == 0, "all pads closed after detaching");
754 863
864 test_twins(input);
755 test_keyboard_pad(input); 865 test_keyboard_pad(input);
756 input->Release(); 866 input->Release();
757 867