From 6837c96f6971c187c7a65fd419f14a4301860669 Mon Sep 17 00:00:00 2001 From: mlopezsegura <90263997+mlopezsegura@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:01:53 +0100 Subject: [PATCH] Gamepads: keep reserving a controller number after its previous controller leaves The reservation of the next ViGEmBus controllers only protected the first connection after the service started. When a Moonlight client disconnected, the periodic scan tried to reserve the freed number before removing the old controller's jail entry, skipped it because that entry still existed, but recorded it as reserved anyway. Once the old entry was removed, that number had no reservation and the next controller appeared visible to the host again (seen in the log as a 5 s wait while Windows refused to restart it). Only reservations that were actually added are recorded now, and reservations are synced after removing the controllers that left. A log line says when reservations become active or are removed. Co-Authored-By: Claude Opus 5.5 --- Services/Gamepad/GamepadSessionIsolation.cs | 43 +++++++++++++++------ 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/Services/Gamepad/GamepadSessionIsolation.cs b/Services/Gamepad/GamepadSessionIsolation.cs index c9aba3b..1641fc7 100644 --- a/Services/Gamepad/GamepadSessionIsolation.cs +++ b/Services/Gamepad/GamepadSessionIsolation.cs @@ -484,8 +484,11 @@ private void ScanCore() ScanPhysical(); var running = _sessions.GetRunningSunshineSessions(); - SyncReservations(running.Count > 0); - if (running.Count == 0 && _pads.Count == 0) return; + if (running.Count == 0 && _pads.Count == 0) + { + SyncReservations(false); + return; + } // Mandos presentes = hijos directos de cada bus ViGEm var present = Buses().SelectMany(b => Cm.Children(b)).ToHashSet(StringComparer.OrdinalIgnoreCase); @@ -494,6 +497,8 @@ private void ScanCore() _pads.Remove(gone.InstanceId); Unhide(gone); } + // Después de retirar los que se fueron: su número de serie queda libre y se reserva ya. + SyncReservations(running.Count > 0); foreach (var id in _cursors.Keys.Where(id => running.All(s => s.Id != id)).ToList()) _cursors.Remove(id); @@ -730,16 +735,31 @@ private void SyncReservations(bool streams) var drop = _reserved.Where(id => !want.Contains(id) && !Cm.Exists(id)).ToList(); var add = want.Where(id => !_reserved.Contains(id)).ToList(); if (drop.Count == 0 && add.Count == 0) return; - if (!ReserveJails(add, drop)) return; + if (ReserveJails(add, drop) is not { } added) return; _reserved.ExceptWith(drop); - _reserved.UnionWith(add); + // Solo los que se pusieron de verdad: uno que aún tiene otro jail (el mando anterior con ese + // número, a punto de retirarse) se vuelve a intentar en la siguiente pasada. Antes se daba + // por reservado sin estarlo y, al quitarse la entrada vieja, ese número quedaba sin reserva. + _reserved.UnionWith(added); + + bool active = _reserved.Count > 0; + if (active != _reservationsLogged) + { + _reservationsLogged = active; + Logger.Log(active + ? $"[Gamepad] Próximos mandos de Moonlight ocultos de antemano hasta saber su sesión ({string.Join(", ", _reserved.Order())})." + : "[Gamepad] Sin streams en marcha: quitada la reserva de los próximos mandos."); + } } + private bool _reservationsLogged; + /// /// Pone o quita entradas de reserva (instance id con ). Solo toca las - /// suyas: un id que ya tiene otro jail (un puesto que lo reclamó) se deja como está. + /// suyas: un id que ya tiene otro jail (un puesto que lo reclamó, o el mando anterior con ese + /// número) se deja como está. Devuelve los ids reservados ahora, o null si HidHide falló. /// - private bool ReserveJails(IReadOnlyCollection add, IReadOnlyCollection remove) + private List? ReserveJails(IReadOnlyCollection add, IReadOnlyCollection remove) { try { @@ -748,17 +768,18 @@ private bool ReserveJails(IReadOnlyCollection add, IReadOnlyCollection().ToHashSet(StringComparer.OrdinalIgnoreCase); var dropEntries = remove.Select(id => HidHide.JailEntry(id, NoSession)).ToHashSet(StringComparer.OrdinalIgnoreCase); - var addEntries = add.Where(id => !jailed.Contains(id)).Select(id => HidHide.JailEntry(id, NoSession)).ToList(); - if (addEntries.Count == 0 && !list.Any(dropEntries.Contains)) return true; - HidHide.SetBlacklist(_hidHide!, list.Where(e => !dropEntries.Contains(e)).Concat(addEntries) + var added = add.Where(id => !jailed.Contains(id)).ToList(); + if (added.Count == 0 && !list.Any(dropEntries.Contains)) return added; + HidHide.SetBlacklist(_hidHide!, list.Where(e => !dropEntries.Contains(e)) + .Concat(added.Select(id => HidHide.JailEntry(id, NoSession))) .Distinct(StringComparer.OrdinalIgnoreCase).ToList()); - return true; + return added; } } catch (Win32Exception ex) { Logger.Warning($"[Gamepad] No se pudieron reservar los próximos mandos en HidHide: {ex.Message}"); - return false; + return null; } }