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
17 changes: 15 additions & 2 deletions dm/host/pad_slots.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Player slots of the physical controllers (front_mac.m; pure, so tests/t_hid.c checks it without controllers): each controller holds one slot, slot p is player p + 1
// (hle_hid.c dm_player_npad gives its npad). A connecting controller takes, in this order: a free slot whose last holder had its name (the same controller reconnected gets its
// player back; GameController gives a controller no identity across a reconnect, so two controllers of one model may trade places), player 1 when it is free, the lowest slot
// (hle_hid.c dm_player_npad gives its npad). A connecting controller takes, in this order: a free slot whose last holder had its model name (a reconnect hint, not identity:
// GameController gives no persistent identity, so identical models may trade places), player 1 when it is free, the lowest slot
// no controller has held yet (so a slot stays kept for a controller that went away while others are in use), then the lowest free slot. With every slot held it gets none (-1).
// A disconnected controller frees its slot and leaves its name on it. The owner is any pointer that stays the same while the controller is connected (the front end keeps the
// GCController); a holder that never disconnects (the keyboard with DM_KBD_PLAYER=own) takes slot 0 first and keeps it.
Expand Down Expand Up @@ -31,6 +31,19 @@ static int pad_slot_free(pad_slots *s, const void *owner) {
int p = pad_slot_of(s, owner); if (p >= 0) s->owner[p] = NULL;
return p;
}
// Explicit session assignment: move to a free slot or swap its current owner.
// Reconnect hints follow the assigned controllers, not the old player numbers.
static int pad_slot_assign(pad_slots *s, const void *owner, int target) {
int from = pad_slot_of(s, owner);
if (from < 0 || target < 0 || target >= DM_PLAYERS) return -1;
if (from == target) return from;
char name[sizeof s->last[0]]; memcpy(name, s->last[from], sizeof name);
s->owner[from] = s->owner[target]; s->owner[target] = owner;
if (s->owner[from]) memcpy(s->last[from], s->last[target], sizeof name);
else memset(s->last[from], 0, sizeof name);
memcpy(s->last[target], name, sizeof name);
return from;
}
static uint32_t pad_slot_mask(const pad_slots *s) { // bit p: slot p is held
uint32_t m = 0; for (int i = 0; i < DM_PLAYERS; i++) if (s->owner[i]) m |= 1u << i;
return m;
Expand Down
15 changes: 15 additions & 0 deletions dm/host/tests/t_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ static void t_slots(void) {
CHECK(pad_slot_mask(&s) == (1u << DM_PLAYERS) - 1, "every slot held");
CHECK(pad_slot_take(&s, &many[DM_PLAYERS - 3], "Pro Controller") == -1, "a ninth controller gets no player");
CHECK(pad_slot_take(&s, NULL, "x") == -1 && pad_slot_free(&s, NULL) == -1, "no owner, no slot");
CHECK(pad_slot_assign(&s, &a, 1) == 0 && pad_slot_of(&s, &a) == 1 && pad_slot_of(&s, &b2) == 0, "explicit assignment swaps occupied players even when every slot is held");
CHECK(!strcmp(s.last[1], "Xbox Wireless Controller") && !strcmp(s.last[0], "DualSense Wireless Controller"), "reconnect names follow both swapped controllers");
CHECK(pad_slot_mask(&s) == (1u << DM_PLAYERS) - 1, "a full swap loses no connected player");
pad_slot_free(&s, &a);
CHECK(pad_slot_take(&s, &a, "Xbox Wireless Controller") == 1, "a different-model reconnect follows its explicit assignment");
pad_slots before = s;
CHECK(pad_slot_assign(&s, &a, -1) == -1 && pad_slot_assign(&s, &a, DM_PLAYERS) == -1 && pad_slot_assign(&s, NULL, 0) == -1 && !memcmp(&s, &before, sizeof s), "invalid assignments leave every slot and reconnect hint unchanged");
CHECK(pad_slot_assign(&s, &a, 1) == 1 && !memcmp(&s, &before, sizeof s), "assigning the current player is a no-op");
pad_slot_free(&s, &b2);
CHECK(pad_slot_assign(&s, &a, 0) == 1 && !s.owner[1] && !s.last[1][0] && pad_slot_of(&s, &a) == 0, "moving to a free player clears the old reservation");
memset(&s, 0, sizeof s);
pad_slot_take(&s, &a, "Same model"); pad_slot_take(&s, &b, "Same model");
CHECK(pad_slot_assign(&s, &a, 1) == 0 && pad_slot_of(&s, &a) == 1 && pad_slot_of(&s, &b) == 0, "identical model names can be explicitly swapped by live connection");
pad_slot_free(&s, &a); pad_slot_free(&s, &b);
CHECK(pad_slot_take(&s, &b2, "Same model") == 0, "duplicate-name reconnect is a free-name hint, not persistent identity");
memset(&s, 0, sizeof s); // DM_KBD_PLAYER=own: the keyboard holds player 1 for good
CHECK(pad_slot_take(&s, &kbd, "keyboard") == 0 && pad_slot_take(&s, &a, "Xbox Wireless Controller") == 1, "with the keyboard on its own the first controller is player 2");
}
Expand Down
Loading