//enumerates ds4 controllers in the system public static void findControllers() { lock (Devices) { int[] pid = { 0x5C4 }; IEnumerable <HidDevice> hDevices = HidDevices.Enumerate(0x054C, pid); // Sort Bluetooth first in case USB is also connected on the same controller. hDevices = hDevices.OrderBy <HidDevice, ConnectionType>((HidDevice d) => { return(DS4Device.HidConnectionType(d)); }); foreach (HidDevice hDevice in hDevices) { if (DevicePaths.Contains(hDevice.DevicePath)) { continue; // BT/USB endpoint already open once } if (!hDevice.IsOpen) { hDevice.OpenDevice(isExclusiveMode); // TODO in exclusive mode, try to hold both open when both are connected if (isExclusiveMode && !hDevice.IsOpen) { hDevice.OpenDevice(false); } } if (hDevice.IsOpen) { if (Devices.ContainsKey(hDevice.readSerial())) { continue; // happens when the BT endpoint already is open and the USB is plugged into the same host } else { DS4Device ds4Device = new DS4Device(hDevice); ds4Device.Removal += On_Removal; Devices.Add(ds4Device.MacAddress, ds4Device); DevicePaths.Add(hDevice.DevicePath); ds4Device.StartUpdate(); } } } } }
//enumerates ds4 controllers in the system public static void findControllers() { lock (Devices) { int[] pid = { 0xBA0, 0x5C4, 0x09CC }; IEnumerable <HidDevice> hDevices = HidDevices.Enumerate(0x054C, pid); // Sort Bluetooth first in case USB is also connected on the same controller. hDevices = hDevices.OrderBy <HidDevice, ConnectionType>((HidDevice d) => { return(DS4Device.HidConnectionType(d)); }); foreach (HidDevice hDevice in hDevices) { if (DevicePaths.Contains(hDevice.DevicePath)) { continue; // BT/USB endpoint already open once } if (!hDevice.IsOpen) { hDevice.OpenDevice(isExclusiveMode); if (!hDevice.IsOpen && isExclusiveMode) { try { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); bool elevated = principal.IsInRole(WindowsBuiltInRole.Administrator); if (!elevated) { // Launches an elevated child process to re-enable device string exeName = Process.GetCurrentProcess().MainModule.FileName; ProcessStartInfo startInfo = new ProcessStartInfo(exeName); startInfo.Verb = "runas"; startInfo.Arguments = "re-enabledevice " + devicePathToInstanceId(hDevice.DevicePath); Process child = Process.Start(startInfo); if (!child.WaitForExit(5000)) { child.Kill(); } else if (child.ExitCode == 0) { hDevice.OpenDevice(isExclusiveMode); } } else { reEnableDevice(devicePathToInstanceId(hDevice.DevicePath)); hDevice.OpenDevice(isExclusiveMode); } } catch (Exception) { } } // TODO in exclusive mode, try to hold both open when both are connected if (isExclusiveMode && !hDevice.IsOpen) { hDevice.OpenDevice(false); } } if (hDevice.IsOpen) { if (Devices.ContainsKey(hDevice.readSerial())) { continue; // happens when the BT endpoint already is open and the USB is plugged into the same host } else { DS4Device ds4Device = new DS4Device(hDevice); ds4Device.Removal += On_Removal; Devices.Add(ds4Device.MacAddress, ds4Device); DevicePaths.Add(hDevice.DevicePath); ds4Device.StartUpdate(); } } } } }