public static IEnumerable <HidDevice> EnumerateDS4(VidPidInfo[] devInfo) { //int iDebugDevCount = 0; List <HidDevice> foundDevs = new List <HidDevice>(); int devInfoLen = devInfo.Length; IEnumerable <DeviceInfo> temp = EnumerateDevices(); for (var devEnum = temp.GetEnumerator(); devEnum.MoveNext();) //for (int i = 0, len = temp.Count(); i < len; i++) { DeviceInfo x = devEnum.Current; //DeviceInfo x = temp.ElementAt(i); HidDevice tempDev = new HidDevice(x.Path, x.Description); //iDebugDevCount++; //AppLogger.LogToGui($"DEBUG: HID#{iDebugDevCount} Path={x.Path} Description={x.Description} VID={tempDev.Attributes.VendorHexId} PID={tempDev.Attributes.ProductHexId} Usage=0x{tempDev.Capabilities.Usage.ToString("X")} Version=0x{tempDev.Attributes.Version.ToString("X")}", false); bool found = false; for (int j = 0; !found && j < devInfoLen; j++) { VidPidInfo tempInfo = devInfo[j]; if (tempDev.Capabilities.Usage == 0x05 && tempDev.Attributes.VendorId == tempInfo.vid && tempDev.Attributes.ProductId == tempInfo.pid) { found = true; foundDevs.Add(tempDev); } } } return(foundDevs); }
public static IEnumerable <HidDevice> EnumerateDS4(VidPidInfo[] devInfo) { List <HidDevice> foundDevs = new List <HidDevice>(); int devInfoLen = devInfo.Length; IEnumerable <DeviceInfo> temp = EnumerateDevices(); for (var devEnum = temp.GetEnumerator(); devEnum.MoveNext();) //for (int i = 0, len = temp.Count(); i < len; i++) { DeviceInfo x = devEnum.Current; //DeviceInfo x = temp.ElementAt(i); HidDevice tempDev = new HidDevice(x.Path, x.Description); bool found = false; for (int j = 0; !found && j < devInfoLen; j++) { VidPidInfo tempInfo = devInfo[j]; if (tempDev.Capabilities.Usage == 0x05 && tempDev.Attributes.VendorId == tempInfo.vid && tempDev.Attributes.ProductId == tempInfo.pid) { found = true; foundDevs.Add(tempDev); } } } return(foundDevs); }
private static void OnAddSerial(HidDevice hDevice, VidPidInfo metainfo, string serial) { DS4Device ds4Device = new DS4Device(hDevice, metainfo.Name); //ds4Device.Removal += On_Removal; if (!ds4Device.ExitOutputThread) { Devices.Add(hDevice.DevicePath, ds4Device); DevicePaths.Add(hDevice.DevicePath); DeviceSerials.Add(serial); } }
public static IEnumerable <HidDevice> EnumerateDS4(VidPidInfo[] devInfo) { int iEnumeratedDevCount = 0; List <HidDevice> foundDevs = new List <HidDevice>(); int devInfoLen = devInfo.Length; IEnumerable <DeviceInfo> temp = EnumerateDevices(); for (var devEnum = temp.GetEnumerator(); devEnum.MoveNext();) //for (int i = 0, len = temp.Count(); i < len; i++) { DeviceInfo x = devEnum.Current; //DeviceInfo x = temp.ElementAt(i); HidDevice tempDev = new HidDevice(x.Path, x.Description, x.Parent); iEnumeratedDevCount++; bool found = false; for (int j = 0; !found && j < devInfoLen; j++) { VidPidInfo tempInfo = devInfo[j]; if ((tempDev.Capabilities.Usage == HID_USAGE_GAMEPAD || tempDev.Capabilities.Usage == HID_USAGE_JOYSTICK || tempInfo.featureSet.HasFlag(VidPidFeatureSet.VendorDefinedDevice)) && tempDev.Attributes.VendorId == tempInfo.vid && tempDev.Attributes.ProductId == tempInfo.pid) { found = true; foundDevs.Add(tempDev); } } if (Global.DeviceOptions.VerboseLogMessages) { if (found) { AppLogger.LogToGui($"HID#{iEnumeratedDevCount} CONNECTING to {x.Description} VID={tempDev.Attributes.VendorHexId} PID={tempDev.Attributes.ProductHexId} Usage=0x{tempDev.Capabilities.Usage.ToString("X")} Version=0x{tempDev.Attributes.Version.ToString("X")} Path={x.Path}", false); } else { AppLogger.LogToGui($"HID#{iEnumeratedDevCount} Unknown device {x.Description} VID={tempDev.Attributes.VendorHexId} PID={tempDev.Attributes.ProductHexId} Usage=0x{tempDev.Capabilities.Usage.ToString("X")} Version=0x{tempDev.Attributes.Version.ToString("X")} Path={x.Path}", false); } } } if (Global.DeviceOptions.VerboseLogMessages && iEnumeratedDevCount > 0) { // This EnumerateDS4 method is called 3-4 times when a gamepad is connected. Print out "separator" log msg line between different enumeration loops to make the logfile easier to read AppLogger.LogToGui($"-------------------------", false); } return(foundDevs); }
private static void EvalHid(HidDevice hDevice) { if (IgnoreDevice(hDevice)) { return; } if (!hDevice.IsOpen) { OpenDevice(hDevice); } if (!hDevice.IsOpen) { return; } string serial = hDevice.ReadSerial(); if (DS4Device.IsValidSerial(serial)) { if (DeviceSerials.Contains(serial)) { OnSerialExists(hDevice); } else { try { VidPidInfo metainfo = KnownDevices.Single(x => x.Vid == hDevice.Attributes.VendorId && x.Pid == hDevice.Attributes.ProductId); if (metainfo != null) { OnAddSerial(hDevice, metainfo, serial); } } catch { // Single() may throw an exception } } } }
// 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 } VidPidInfo metainfo = knownDevices.Single(x => x.vid == hDevice.Attributes.VendorId && x.pid == hDevice.Attributes.ProductId); 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, metainfo.name); //ds4Device.Removal += On_Removal; if (!ds4Device.ExitOutputThread) { Devices.Add(hDevice.DevicePath, ds4Device); DevicePaths.Add(hDevice.DevicePath); deviceSerials.Add(serial); } } } } } }