upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.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/shims/gameinput.cpp
parent43d470316ec6cbcd64002bc3792998d9f5745dac (diff)
Distinguish identical connected gamepads in device-ID lookup
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp33
1 files changed, 29 insertions, 4 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 1d4c35b32..ecb2702ff 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -288,6 +288,7 @@ public:
288 uint64_t seq = 0; 288 uint64_t seq = 0;
289 Rumble wanted; 289 Rumble wanted;
290 GameInputBatteryState battery{0, 0, 0, 0, GameInputBatteryUnknown}; 290 GameInputBatteryState battery{0, 0, 0, 0, GameInputBatteryUnknown};
291 AppLocalDeviceId base_id{}; // id_bytes of the identity alone: twins share it, info.deviceId tells them apart
291 292
292 // Poll-thread state. 293 // Poll-thread state.
293 SDL_Gamepad* pad = nullptr; 294 SDL_Gamepad* pad = nullptr;
@@ -521,6 +522,12 @@ void id_bytes(const std::string& identity, const SDL_GUID& guid, AppLocalDeviceI
521 } 522 }
522} 523}
523 524
525// Twin n of an identity. Twin 0 is the plain id, so a pad without twins keeps the id it always had. Real identities
526// start with "s:", "p:" or are empty, never "t:", so a twin id cannot be another pad's own id.
527void id_bytes(const std::string& identity, const SDL_GUID& guid, uint32_t twin, AppLocalDeviceId* out) {
528 id_bytes(twin ? "t:" + std::to_string(twin) + ":" + identity : identity, guid, out);
529}
530
524void fill_info(Device* d, SDL_Gamepad* p, const AppLocalDeviceId& id) { 531void fill_info(Device* d, SDL_Gamepad* p, const AppLocalDeviceId& id) {
525 GameInputDeviceInfo& i = d->info; 532 GameInputDeviceInfo& i = d->info;
526 i = {}; 533 i = {};
@@ -595,16 +602,34 @@ void open_pad(SDL_JoystickID jid, uint64_t ts) {
595 const char* serial = SDL_GetGamepadSerial(p); 602 const char* serial = SDL_GetGamepadSerial(p);
596 const char* path = SDL_GetGamepadPath(p); 603 const char* path = SDL_GetGamepadPath(p);
597 const std::string identity = (serial && *serial) ? std::string("s:") + serial : (path && *path) ? std::string("p:") + path : ""; 604 const std::string identity = (serial && *serial) ? std::string("s:") + serial : (path && *path) ? std::string("p:") + path : "";
598 AppLocalDeviceId id{}; 605 const SDL_GUID guid = SDL_GetGamepadGUIDForID(jid);
599 id_bytes(identity, SDL_GetGamepadGUIDForID(jid), &id); 606 AppLocalDeviceId base{};
607 id_bytes(identity, guid, &base);
600 608
601 const SDL_PropertiesID props = SDL_GetGamepadProperties(p); 609 const SDL_PropertiesID props = SDL_GetGamepadProperties(p);
602 std::lock_guard lock(s.mu); 610 std::lock_guard lock(s.mu);
611 // The same pad coming back gets its old object. Pads with the same GUID and serial or path (identical pads with
612 // neither, mostly) are twins sharing one base id, told apart by order alone: the lowest disconnected twin is
613 // reused, and a new one gets the next free twin id. Which physical pad was which cannot be recovered, so twins
614 // that return in a different order swap objects and ids (docs/gameinput.md).
603 Device* d = nullptr; 615 Device* d = nullptr;
604 for (Device* old : s.devices) // the same pad coming back gets its old object 616 uint32_t twins = 0;
605 if (old->status == GameInputDeviceNoStatus && std::memcmp(&old->info.deviceId, &id, sizeof id) == 0) d = old; 617 for (Device* old : s.devices) { // creation order, so twins in twin order
618 if (std::memcmp(&old->base_id, &base, sizeof base) != 0) continue;
619 ++twins;
620 if (!d && old->status == GameInputDeviceNoStatus) d = old;
621 }
622 AppLocalDeviceId id = d ? d->info.deviceId : base; // a copy: fill_info clears info before it writes the id
606 if (!d) { 623 if (!d) {
624 const auto taken = [&s](const AppLocalDeviceId& v) {
625 for (Device* o : s.devices)
626 if (std::memcmp(&o->info.deviceId, &v, sizeof v) == 0) return true;
627 return false;
628 };
629 id_bytes(identity, guid, twins, &id);
630 for (uint32_t n = twins + 1; taken(id); ++n) id_bytes(identity, guid, n, &id);
607 d = new Device; 631 d = new Device;
632 d->base_id = base;
608 s.devices.push_back(d); 633 s.devices.push_back(d);
609 } 634 }
610 d->pad = p; 635 d->pad = p;