// Enumerates ds4 controllers in the system public static void findControllers() { lock (Devices) { IEnumerable <HidDevice> hDevices = HidDevices.EnumerateDS4(knownDevices); hDevices = hDevices.Where(dev => IsRealDS4(dev)).Select(dev => dev); //hDevices = from dev in hDevices where IsRealDS4(dev) select dev; // Sort Bluetooth first in case USB is also connected on the same controller. hDevices = hDevices.OrderBy <HidDevice, ConnectionType>((HidDevice d) => { return(DS4Device.HidConnectionType(d)); }); List <HidDevice> tempList = hDevices.ToList(); purgeHiddenExclusiveDevices(); tempList.AddRange(DisabledDevices); int devCount = tempList.Count(); string devicePlural = "device" + (devCount == 0 || devCount > 1 ? "s" : ""); //Log.LogToGui("Found " + devCount + " possible " + devicePlural + ". Examining " + devicePlural + ".", false); for (int i = 0; i < devCount; i++) //foreach (HidDevice hDevice in hDevices) { HidDevice hDevice = tempList[i]; if (hDevice.Description == "HID-compliant vendor-defined device") { continue; // ignore the Nacon Revolution Pro programming interface } else 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(30000)) { 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) { string serial = hDevice.readSerial(); bool validSerial = !serial.Equals(DS4Device.blankSerial); if (validSerial && deviceSerials.Contains(serial)) { // happens when the BT endpoint already is open and the USB is plugged into the same host if (isExclusiveMode && hDevice.IsExclusive && !DisabledDevices.Contains(hDevice)) { // Grab reference to exclusively opened HidDevice so device // stays hidden to other processes DisabledDevices.Add(hDevice); //DevicePaths.Add(hDevice.DevicePath); } continue; } else { DS4Device ds4Device = new DS4Device(hDevice); //ds4Device.Removal += On_Removal; if (!ds4Device.ExitOutputThread) { Devices.Add(hDevice.DevicePath, ds4Device); DevicePaths.Add(hDevice.DevicePath); deviceSerials.Add(serial); } } } } } }
public DS4Device(HidDevice hidDevice) { hDevice = hidDevice; conType = HidConnectionType(hDevice); Mac = hDevice.readSerial(); if (conType == ConnectionType.USB) { inputReport = new byte[64]; outputReport = new byte[hDevice.Capabilities.OutputReportByteLength]; outputReportBuffer = new byte[hDevice.Capabilities.OutputReportByteLength]; } else { btInputReport = new byte[BT_INPUT_REPORT_LENGTH]; inputReport = new byte[btInputReport.Length - 2]; outputReport = new byte[BT_OUTPUT_REPORT_LENGTH]; outputReportBuffer = new byte[BT_OUTPUT_REPORT_LENGTH]; } touchpad = new DS4Touchpad(); sixAxis = new DS4SixAxis(); }