Esempio n. 1
0
 private IInputDevice CreateDevice(DeviceInstance deviceInstance, List <string> uniqueIds)
 {
     try
     {
         if (!directInput.IsDeviceAttached(deviceInstance.InstanceGuid))
         {
             return(null);
         }
         var joystick = new Joystick(directInput, deviceInstance.InstanceGuid);
         if (joystick.Capabilities.AxeCount < 1 && joystick.Capabilities.ButtonCount < 1)
         {
             joystick.Dispose();
             return(null);
         }
         bool   isHid         = deviceInstance.IsHumanInterfaceDevice;
         string interfacePath = null;
         string uniqueIdBase;
         string hardwareId = null;
         if (isHid)
         {
             if (ignoredDeviceService.IsIgnored(joystick.Properties.InterfacePath))
             {
                 joystick.Dispose();
                 return(null);
             }
             interfacePath = joystick.Properties.InterfacePath;
             uniqueIdBase  = interfacePath;
             hardwareId    = IdHelper.GetHardwareId(interfacePath);
         }
         else
         {
             uniqueIdBase = string.Join(":", deviceInstance.ProductGuid.ToString(), deviceInstance.InstanceGuid.ToString());
         }
         string uniqueId = IdHelper.GetUniqueId(uniqueIdBase);
         if (uniqueIds.Any(uid => uid == uniqueId))
         {
             notificationService.Add(Notifications.DirectInputInstanceIdDuplication, new[] { uniqueId }, NotificationTypes.Warning);
         }
         uniqueIds.Add(uniqueId);
         if (currentDevices.Any(d => d.UniqueId == uniqueId))
         {
             joystick.Dispose();
             return(null);
         }
         joystick.Properties.BufferSize = 128;
         return(new DirectInputDevice(inputConfigManager, joystick, deviceInstance.InstanceGuid.ToString(), deviceInstance.ProductName,
                                      deviceInstance.ForceFeedbackDriverGuid != Guid.Empty, uniqueId, hardwareId, interfacePath));
     }
     catch (Exception ex)
     {
         logger.Error("Failed to create device " + deviceInstance.InstanceGuid + " " + deviceInstance.InstanceName + ex.ToString());
         return(null);
     }
 }
 public void SearchDevices()
 {
     lock (lockObject)
     {
         var           local     = DeviceList.Local;
         var           devices   = local.GetDevices(DeviceTypes.Hid).OfType <HidDevice>();
         List <string> instances = new List <string>();
         foreach (var device in devices)
         {
             if (ignoredDeviceService.IsIgnored(device.DevicePath))
             {
                 continue;
             }
             string uniqueId = IdHelper.GetUniqueId(device.DevicePath);
             instances.Add(uniqueId);
             if (currentDevices.Any(d => d.UniqueId == uniqueId))
             {
                 continue;
             }
             try
             {
                 HidStream hidStream;
                 if (device.TryOpen(out hidStream))
                 {
                     hidStream.ReadTimeout = Timeout.Infinite;
                     var reportDescriptor = device.GetReportDescriptor();
                     var deviceItems      = reportDescriptor.DeviceItems.Where(i => Match(i,
                                                                                          Usage.GenericDesktopGamepad, Usage.GenericDesktopJoystick, Usage.GenericDesktopMultiaxisController));
                     foreach (var deviceItem in deviceItems)
                     {
                         var inputDevice = new RawInputDevice(device, reportDescriptor, deviceItem, hidStream, uniqueId);
                         var config      = inputConfigManager.LoadConfig(inputDevice.UniqueId);
                         inputDevice.InputConfiguration = config;
                         currentDevices.Add(inputDevice);
                         Connected?.Invoke(this, new DeviceConnectedEventArgs(inputDevice));
                     }
                 }
             }
             catch (Exception)
             {
             }
         }
         foreach (var device in currentDevices.ToArray())
         {
             string guid = device.UniqueId;
             if (!instances.Any(i => i == guid))
             {
                 currentDevices.Remove(device);
                 device.Dispose();
                 Disconnected?.Invoke(this, new DeviceDisconnectedEventArgs(device));
             }
         }
     }
 }
 public void SearchDevices()
 {
     lock (lockObject)
     {
         IEnumerable <DeviceInstance> instances = directInput.GetDevices();
         if (allDevices)
         {
             instances = instances.Where(di => di.Type != DeviceType.Keyboard && di.Type != DeviceType.Mouse).ToList();
         }
         else
         {
             instances = instances.Where(di => di.Type == DeviceType.Joystick || di.Type == DeviceType.Gamepad || di.Type == DeviceType.FirstPerson).ToList();
         }
         foreach (var instance in instances)
         {
             string instanceGuid = instance.InstanceGuid.ToString();
             string productGuid  = instance.ProductGuid.ToString();
             if (!ignoredDeviceService.IsIgnored(productGuid, instanceGuid) && !currentDevices.Any(d => d.UniqueId == instanceGuid))
             {
                 var device = CreateDevice(instance);
                 if (device == null)
                 {
                     continue;
                 }
                 if (currentDevices.Any(d => d.UniqueId == device.UniqueId))
                 {
                     notificationService.Add(Notifications.DirectInputInstanceIdDuplication, new[] { device.UniqueId }, NotificationTypes.Warning);
                 }
                 var config = inputConfigManager.LoadConfig(device.UniqueId);
                 device.InputConfiguration = config;
                 currentDevices.Add(device);
                 Connected?.Invoke(this, new DeviceConnectedEventArgs(device));
             }
         }
         foreach (var device in currentDevices.ToArray())
         {
             string guid = device.UniqueId;
             if (!instances.Any(i => i.InstanceGuid.ToString() == guid))
             {
                 currentDevices.Remove(device);
                 device.Dispose();
                 Disconnected?.Invoke(this, new DeviceDisconnectedEventArgs(device));
             }
         }
     }
 }