HidDevice.cs
changeset 63 4b8b058de215
parent 61 60bfe5083721
child 64 8c2380995bb7
     1.1 --- a/HidDevice.cs	Sun Feb 15 20:28:46 2015 +0100
     1.2 +++ b/HidDevice.cs	Sun Feb 15 21:48:07 2015 +0100
     1.3 @@ -14,7 +14,18 @@
     1.4      /// </summary>
     1.5      public class HidDevice: IDisposable
     1.6      {
     1.7 +        /// <summary>
     1.8 +        /// Unique name of that HID device.
     1.9 +        /// Notably used as input to CreateFile.
    1.10 +        /// </summary>
    1.11          public string Name { get; private set; }
    1.12 +
    1.13 +        /// <summary>
    1.14 +        /// Friendly name that people should be able to read.
    1.15 +        /// </summary>
    1.16 +        public string FriendlyName { get; private set; }
    1.17 +
    1.18 +        //
    1.19          public string Manufacturer { get; private set; }
    1.20          public string Product { get; private set; }
    1.21          public ushort VendorId { get; private set; }
    1.22 @@ -127,6 +138,8 @@
    1.23  
    1.24              handle.Close();
    1.25  
    1.26 +            SetFriendlyName();
    1.27 +
    1.28              //Get our HID descriptor pre-parsed data
    1.29              PreParsedData = Win32.RawInput.GetPreParsedData(hRawInputDevice);
    1.30  
    1.31 @@ -169,6 +182,68 @@
    1.32              }
    1.33  
    1.34  
    1.35 +            
    1.36 +
    1.37 +        }
    1.38 +
    1.39 +        /// <summary>
    1.40 +        /// 
    1.41 +        /// </summary>
    1.42 +        private void SetFriendlyName()
    1.43 +        {
    1.44 +            //Work out proper suffix for our device root node.
    1.45 +            //That allows users to see in a glance what kind of device this is.
    1.46 +            string suffix = "";
    1.47 +            Type usageCollectionType = null;
    1.48 +            if (Info.dwType == RawInputDeviceType.RIM_TYPEHID)
    1.49 +            {
    1.50 +                //Process usage page
    1.51 +                if (Enum.IsDefined(typeof(Hid.UsagePage), Info.hid.usUsagePage))
    1.52 +                {
    1.53 +                    //We know this usage page, add its name
    1.54 +                    Hid.UsagePage usagePage = (Hid.UsagePage)Info.hid.usUsagePage;
    1.55 +                    suffix += " ( " + usagePage.ToString() + ", ";
    1.56 +                    usageCollectionType = Hid.Utils.UsageCollectionType(usagePage);
    1.57 +                }
    1.58 +                else
    1.59 +                {
    1.60 +                    //We don't know this usage page, add its value
    1.61 +                    suffix += " ( 0x" + Info.hid.usUsagePage.ToString("X4") + ", ";
    1.62 +                }
    1.63 +
    1.64 +                //Process usage collection
    1.65 +                //We don't know this usage page, add its value
    1.66 +                if (usageCollectionType == null || !Enum.IsDefined(usageCollectionType, Info.hid.usUsage))
    1.67 +                {
    1.68 +                    //Show Hexa
    1.69 +                    suffix += "0x" + Info.hid.usUsage.ToString("X4") + " )";
    1.70 +                }
    1.71 +                else
    1.72 +                {
    1.73 +                    //We know this usage page, add its name
    1.74 +                    suffix += Enum.GetName(usageCollectionType, Info.hid.usUsage) + " )";
    1.75 +                }
    1.76 +            }
    1.77 +            else if (Info.dwType == RawInputDeviceType.RIM_TYPEKEYBOARD)
    1.78 +            {
    1.79 +                suffix = " - Keyboard";
    1.80 +            }
    1.81 +            else if (Info.dwType == RawInputDeviceType.RIM_TYPEMOUSE)
    1.82 +            {
    1.83 +                suffix = " - Mouse";
    1.84 +            }
    1.85 +
    1.86 +            if (Product != null && Product.Length > 1)
    1.87 +            {
    1.88 +                //Add the devices with a proper name at the top
    1.89 +                FriendlyName = Product + suffix;
    1.90 +            }
    1.91 +            else
    1.92 +            {
    1.93 +                //Add other once at the bottom
    1.94 +                FriendlyName = "0x" + ProductId.ToString("X4") + suffix;
    1.95 +            }
    1.96 +
    1.97          }
    1.98  
    1.99          /// <summary>