private static void ProcessDevices(IList <DeviceInstance> devices, TelescopeControlSettings settings, bool announce, ref List <Guid> existingConnectedIds, ref List <Guid> connectedIDs, ref List <GameController> newControllers) { GameController controller; string announcement = ""; foreach (DeviceInstance deviceInstance in devices) { controller = settings.GameControllers.Where(c => c.Id == deviceInstance.InstanceGuid).FirstOrDefault(); if (controller == null) { controller = new GameController(deviceInstance.InstanceGuid, deviceInstance.ProductName); newControllers.Add(controller); } else { // Remove from list that should be disconnected existingConnectedIds.Remove(controller.Id); if (!controller.IsConnected) { if (controller.WasActiveGameController) { controller.IsActiveGameController = true; announcement = $"{controller.Name} is now connected and active."; } else { announcement = $"{controller.Name} is now connected."; } if (announce) { Messenger.Default.Send <AnnounceNotificationMessage>(new AnnounceNotificationMessage(announcement)); } } } controller.IsConnected = true; connectedIDs.Add(controller.Id); } }
public static void UpdateAvailableGameControllers(TelescopeControlSettings settings, bool announce = false) { bool setActiveController = false; string announcement = ""; List <Guid> existingConnectedIds = settings.GameControllers.Where(c => c.IsConnected).Select(c => c.Id).ToList(); List <Guid> connectedIds = new List <Guid>(); List <GameController> newControllers = new List <GameController>(); GameController controller; using (DirectInput directInput = new DirectInput()) { ProcessDevices(directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices), settings, announce, ref existingConnectedIds, ref connectedIds, ref newControllers); ProcessDevices(directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices), settings, announce, ref existingConnectedIds, ref connectedIds, ref newControllers); // Disconnect any remaining existing connections as they are not now connected foreach (Guid existingId in existingConnectedIds) { controller = settings.GameControllers.Where(c => c.Id == existingId).FirstOrDefault(); if (controller != null) { if (controller.IsActiveGameController) { // We'll need to set the active controller to the first new Controller setActiveController = true; // controller.WasActiveGameController = true MUST follow controller.IsActiveGameController otherwise the flag will be cleared in the IsActive... setter. controller.IsActiveGameController = false; controller.WasActiveGameController = true; announcement = $"The active {controller.Name} has disconnected."; } else { announcement = $"{controller.Name} has disconnected."; } controller.IsConnected = false; } if (announce) { Messenger.Default.Send <AnnounceNotificationMessage>(new AnnounceNotificationMessage(announcement)); } } // Add any new controllers foreach (GameController newController in newControllers) { settings.GameControllers.Add(newController); if (setActiveController) { newController.IsActiveGameController = true; setActiveController = false; announcement = $"{newController.Name} is now connected and active."; } else { announcement = $"{newController.Name} is now connected."; } if (announce) { Messenger.Default.Send <AnnounceNotificationMessage>(new AnnounceNotificationMessage(announcement)); } } } }