/// <summary> /// Function to retrieve a list of human interface devices (HID). /// </summary> /// <returns>A read only list containing information about each human interface device.</returns> public IReadOnlyList <GorgonRawHIDInfo> EnumerateHumanInterfaceDevices() { RAWINPUTDEVICELIST[] devices = RawInputApi.EnumerateRawInputDevices(RawInputType.HID); var result = new List <GorgonRawHIDInfo>(); for (int i = 0; i < devices.Length; i++) { GorgonRawHIDInfo info = GetDeviceInfo <GorgonRawHIDInfo>(ref devices[i]); if (info == null) { _log.Print("WARNING: Could not retrieve the class and device name. Skipping this device.", LoggingLevel.Verbose); continue; } _log.Print("Found human interface device: '{0}' on HID path {1}, class {2}.", LoggingLevel.Verbose, info.Description, info.HIDPath, info.DeviceClass); result.Add(info); } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="GorgonRawHID"/> class. /// </summary> /// <param name="hidInfo">The human interface device information used to determine which keyboard to use.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="hidInfo"/> is <b>null</b>.</exception> public GorgonRawHID(GorgonRawHIDInfo hidInfo) => Info = hidInfo ?? throw new ArgumentNullException(nameof(hidInfo));
/// <summary> /// Function to get the human readable name for a gaming device from a <see cref="GorgonRawHIDInfo"/> object. /// </summary> /// <param name="hidDeviceInfo">The human interface device information to evaluate.</param> /// <returns>A string containing the human readable name for the gaming device, or <b>null</b> if the device name could not be determined.</returns> /// <remarks> /// <para> /// This will retrieve the friendly name for a gaming device from a <see cref="GorgonRawHIDInfo"/> object. If the <see cref="GorgonRawHIDInfo.Usage"/> is not a <see cref="HIDUsage.Gamepad"/> or /// <see cref="HIDUsage.Joystick"/>, or it does not have a <see cref="GorgonRawHIDInfo.HIDPath"/>, this method will return <b>null</b>. /// </para> /// <para> /// This is meant for raw input gaming human interface devices. As such, if this is used on an XInput controller, then the device will not be given a name and <b>null</b> will be returned. If /// use of an XInput controller is required, then use the Gorgon XInput driver to provide access to those devices. /// </para> /// </remarks> public static string GetGamingDeviceName(GorgonRawHIDInfo hidDeviceInfo) { if (string.IsNullOrWhiteSpace(hidDeviceInfo?.HIDPath) || (hidDeviceInfo.UsagePage != HIDUsagePage.Generic) || ((hidDeviceInfo.Usage != HIDUsage.Gamepad) && (hidDeviceInfo.Usage != HIDUsage.Joystick))) { return(null); } // Take the HID path, and split it out until we get the appropriate sub key name. string[] parts = hidDeviceInfo.HIDPath.Split(new[] { '#' }, StringSplitOptions.RemoveEmptyEntries); if ((parts.Length < 2) || (string.IsNullOrWhiteSpace(parts[1]))) { return(null); } string subKeyName = parts[1]; // The XBOX 360 controller has an &IG_ at the end of its PID, then ignore it. // Use the XInput driver. All other game devices through HID will be handled by us, but not the XInput devices. while (subKeyName.Count(item => item == '&') > 1) { int lastAmp = subKeyName.LastIndexOf("&", StringComparison.OrdinalIgnoreCase); if (lastAmp == -1) { return(null); } subKeyName = subKeyName.Substring(lastAmp); if (subKeyName.IndexOf("&IG_", StringComparison.OrdinalIgnoreCase) == 0) { return(null); } } if (string.IsNullOrWhiteSpace(subKeyName)) { return(string.Empty); } // Find the key and open it. // The original example code this is based on uses HKEY_LOCAL_MACHINE instead of CURRENT_USER. This may be a difference // between operating systems. const string regKeyPath = @"System\CurrentControlSet\Control\MediaProperties\PrivateProperties\Joystick\OEM\"; using (RegistryKey joystickOemKey = Registry.CurrentUser.OpenSubKey(regKeyPath, false)) { string joystickDeviceKeyName = joystickOemKey?.GetSubKeyNames().First(item => parts[1].StartsWith(item, StringComparison.OrdinalIgnoreCase)); if (string.IsNullOrWhiteSpace(joystickDeviceKeyName)) { return(string.Empty); } using (RegistryKey joystickVidKey = joystickOemKey.OpenSubKey(subKeyName, false)) { object value = joystickVidKey?.GetValue("OEMName"); return(value?.ToString() ?? string.Empty); } } }