public string RemoveDevice(string UniqueKey)
        {
            lock (Controllers)
            {
                if (!DeviceToControllerKeyMap.ContainsKey(UniqueKey))
                {
                    return(null);
                }

                Guid DeviceParent = DeviceToControllerKeyMap[UniqueKey];

                foreach (string DeviceKey in ControllerToDeviceKeyMap[DeviceParent])
                {
                    if (DeviceToControllerKeyMap.ContainsKey(DeviceKey))
                    {
                        DeviceToControllerKeyMap.Remove(DeviceKey);
                    }
                }

                BetopController ctrl = Controllers[DeviceParent];
                string          UniqueControllerId = ctrl.ConnectionUniqueID;

                ctrl.DeInitalize();
                ctrl.Dispose();
                Controllers.Remove(DeviceParent);
                ControllerToDeviceKeyMap.Remove(DeviceParent);

                return(UniqueControllerId);
            }
        }
        public IController NewDevice(IDevice device)
        {
            HidDevice    deviceHid    = device as HidDevice;
            XInputDevice deviceXInput = device as XInputDevice;

            // We dont understand this device type
            if (deviceHid == null && deviceXInput == null)
            {
                return(null);
            }

            if (deviceXInput != null && this.AccessMode != AccessMode.FullControl)
            {
                return(null);
            }

            if (deviceHid != null)
            {
                if (deviceHid.VendorId == 0x045E && deviceHid.ProductId == 0x028E)
                {
                    if (this.AccessMode != AccessMode.FullControl)
                    {
                        return(null);
                    }

                    if (deviceHid.Properties["ProductName"].Contains(" A2 GAMEPAD ") || // ASURA3
                        deviceHid.Properties["ProductName"].Contains(" BD4E "))
                    {
                        // hold logo to switch modes, or allow this code below to run (they don't have a choice)
                        lock (CandidateXInputLock)
                            SawCandidateHidDeviceForXInput = DateTime.UtcNow;
                        CheckXInputData();
                    }
                    else if (deviceHid.Properties["ProductName"].Contains(" A2 RECEIVER ")) // ASURA3_DONGLE
                    {
                        // hold logo to switch modes
                    }
                    return(null);
                }
            }
            else if (deviceXInput != null)
            {
                if (this.AccessMode != AccessMode.FullControl)
                {
                    return(null);
                }

                lock (CandidateXInputLock)
                {
                    // Keep a weak reference on all 4 XInput controllers. Note that this weak reference will fail to work properly if there is no XInput controller.
                    CandidateXInputDevicesX[(byte)deviceXInput.UserIndex]        = new WeakReference <XInputDevice>(deviceXInput);
                    CandidateXInputDevicesLastSeen[(byte)deviceXInput.UserIndex] = DateTime.UtcNow;
                }
                CheckXInputData();
                return(null);
            }

            if (device.VendorId == BetopController.VENDOR_BETOP && (device.ProductId == BetopController.PRODUCT_BETOP_ASURA3 ||
                                                                    device.ProductId == BetopController.PRODUCT_BETOP_ASURA3_DONGLE))
            {
                uint[] Usages = device.Properties.ContainsKey("Usages") ? device.Properties["Usages"] as uint[] : null;
                if (Usages != null && (Usages.Contains(0xff000003u) || Usages.Contains(0x00010005u)))
                //if (device.DevicePath.Contains("&col05") || device.DevicePath.Contains("&col04"))
                {
                    {
                        string deviceInstanceId = DevPKey.PnpDevicePropertyAPI.devicePathToInstanceId(deviceHid.DevicePath);
                        Guid?  ContrainerID     = DevPKey.PnpDevicePropertyAPI.GetDeviceContainerId(deviceInstanceId);
                        if (ContrainerID.HasValue)
                        {
                            lock (Controllers)
                            {
                                BetopController ctrl = null;
                                if (Controllers.ContainsKey(ContrainerID.Value))
                                {
                                    ctrl = Controllers[ContrainerID.Value];
                                    ctrl.AddDevice(deviceHid);
                                }
                                else
                                {
                                    Controllers[ContrainerID.Value] = new BetopController(ContrainerID.Value.ToString(), AccessMode, deviceHid);
                                    ctrl = Controllers[ContrainerID.Value];
                                }

                                DeviceToControllerKeyMap[device.UniqueKey] = ContrainerID.Value;
                                if (!ControllerToDeviceKeyMap.ContainsKey(ContrainerID.Value))
                                {
                                    ControllerToDeviceKeyMap[ContrainerID.Value] = new HashSet <string>();
                                }
                                ControllerToDeviceKeyMap[ContrainerID.Value].Add(device.UniqueKey);

                                return(ctrl);


                                /*if (Usages.Contains(0xff000003u))
                                 * //if (device.DevicePath.Contains("&col05"))
                                 * {
                                 *  Controllers[ContrainerID.Value].bVendor = true;
                                 *  Controllers[ContrainerID.Value].Vendor = new WeakReference<HidDevice>(deviceHid);
                                 * }
                                 * if (Usages.Contains(0x00010005u))
                                 * //if (device.DevicePath.Contains("&col04"))
                                 * {
                                 *  Controllers[ContrainerID.Value].bGamepad = true;
                                 *  Controllers[ContrainerID.Value].Gamepad = new WeakReference<HidDevice>(deviceHid);
                                 * }
                                 *
                                 * Console.WriteLine(ContrainerID.Value.ToString());
                                 *
                                 * BetopController ctrl = null;
                                 * if (Controllers[ContrainerID.Value].bGamepad && Controllers[ContrainerID.Value].bVendor)
                                 * {
                                 *  HidDevice deviceVendor = null;
                                 *  HidDevice deviceGamepad = null;
                                 *  Controllers[ContrainerID.Value]?.Vendor.TryGetTarget(out deviceVendor);
                                 *  Controllers[ContrainerID.Value]?.Gamepad.TryGetTarget(out deviceGamepad);
                                 *
                                 *  if (deviceVendor != null && deviceGamepad != null)
                                 *  {
                                 *      ctrl = new BetopController(AccessMode, deviceVendor, deviceGamepad);
                                 *  }
                                 *  else
                                 *  {
                                 *      Controllers.Remove(ContrainerID.Value); // wtf this should never happen
                                 *  }
                                 * }
                                 *
                                 * // clear any dead refs
                                 * foreach (Guid key in Controllers.Keys.ToList()) // ToList to clone the key list so we can modify it
                                 * {
                                 *  ControllerPair candidate = Controllers[key];
                                 *  if ((!candidate.bVendor && !candidate.bGamepad) || ((!candidate.Vendor?.TryGetTarget(out _) ?? false) && (!candidate.Gamepad?.TryGetTarget(out _) ?? false)))
                                 *      Controllers.Remove(key);
                                 * }
                                 *
                                 * return ctrl;*/
                            }
                        }
                    }
                }
            }

            return(null);
        }