Much improved raw input device list.
authorStephaneLenclud
Sun, 15 Feb 2015 19:53:27 +0100
changeset 6160bfe5083721
parent 60 687cace560d2
child 62 bc92e2648220
Much improved raw input device list.
HidDevice.cs
HidUsageTables.cs
MainForm.Designer.cs
RawInput.cs
     1.1 --- a/HidDevice.cs	Sun Feb 15 18:15:41 2015 +0100
     1.2 +++ b/HidDevice.cs	Sun Feb 15 19:53:27 2015 +0100
     1.3 @@ -79,13 +79,6 @@
     1.4              //Fetch various information defining the given HID device
     1.5              Name = Win32.RawInput.GetDeviceName(hRawInputDevice);
     1.6  
     1.7 -            //Get our HID descriptor pre-parsed data
     1.8 -            PreParsedData = Win32.RawInput.GetPreParsedData(hRawInputDevice);
     1.9 -            if (PreParsedData == IntPtr.Zero)
    1.10 -            {
    1.11 -                throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString());
    1.12 -            }
    1.13 -
    1.14              //Fetch device info
    1.15              iInfo = new RID_DEVICE_INFO();
    1.16              if (!Win32.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
    1.17 @@ -134,6 +127,16 @@
    1.18  
    1.19              handle.Close();
    1.20  
    1.21 +            //Get our HID descriptor pre-parsed data
    1.22 +            PreParsedData = Win32.RawInput.GetPreParsedData(hRawInputDevice);
    1.23 +
    1.24 +            if (PreParsedData == IntPtr.Zero)
    1.25 +            {
    1.26 +                //We are done then.
    1.27 +                //Some devices don't have pre-parsed data.
    1.28 +                return;
    1.29 +            }
    1.30 +
    1.31              //Get capabilities
    1.32              HidStatus status = Win32.Function.HidP_GetCaps(PreParsedData, ref iCapabilities);
    1.33              if (status != HidStatus.HIDP_STATUS_SUCCESS)
     2.1 --- a/HidUsageTables.cs	Sun Feb 15 18:15:41 2015 +0100
     2.2 +++ b/HidUsageTables.cs	Sun Feb 15 19:53:27 2015 +0100
     2.3 @@ -48,6 +48,9 @@
     2.4          TerraTecRemote = 0xffcc
     2.5      }
     2.6  
     2.7 +    /// <summary>
     2.8 +    /// Usage Collection for usage page GenericDesktopControls.
     2.9 +    /// </summary>
    2.10      public enum UsageCollectionGenericDesktop : ushort
    2.11      {
    2.12          Pointer = 0x01,
    2.13 @@ -61,6 +64,9 @@
    2.14          SystemControl = 0x80
    2.15      }
    2.16  
    2.17 +    /// <summary>
    2.18 +    /// Usage Collection for usage page Consumer.
    2.19 +    /// </summary>
    2.20      public enum UsageCollectionConsumer : ushort
    2.21      {
    2.22          ConsumerControl = 0x01,
     3.1 --- a/MainForm.Designer.cs	Sun Feb 15 18:15:41 2015 +0100
     3.2 +++ b/MainForm.Designer.cs	Sun Feb 15 19:53:27 2015 +0100
     3.3 @@ -36,7 +36,7 @@
     3.4              // 
     3.5              this.labelButtonName.AutoSize = true;
     3.6              this.labelButtonName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     3.7 -            this.labelButtonName.Location = new System.Drawing.Point(788, 150);
     3.8 +            this.labelButtonName.Location = new System.Drawing.Point(785, 53);
     3.9              this.labelButtonName.Name = "labelButtonName";
    3.10              this.labelButtonName.Size = new System.Drawing.Size(103, 20);
    3.11              this.labelButtonName.TabIndex = 0;
    3.12 @@ -46,7 +46,7 @@
    3.13              // 
    3.14              this.labelDeviceName.AutoSize = true;
    3.15              this.labelDeviceName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    3.16 -            this.labelDeviceName.Location = new System.Drawing.Point(788, 130);
    3.17 +            this.labelDeviceName.Location = new System.Drawing.Point(785, 33);
    3.18              this.labelDeviceName.Name = "labelDeviceName";
    3.19              this.labelDeviceName.Size = new System.Drawing.Size(103, 20);
    3.20              this.labelDeviceName.TabIndex = 1;
    3.21 @@ -54,7 +54,7 @@
    3.22              // 
    3.23              // buttonClear
    3.24              // 
    3.25 -            this.buttonClear.Location = new System.Drawing.Point(816, 104);
    3.26 +            this.buttonClear.Location = new System.Drawing.Point(813, 6);
    3.27              this.buttonClear.Name = "buttonClear";
    3.28              this.buttonClear.Size = new System.Drawing.Size(75, 23);
    3.29              this.buttonClear.TabIndex = 3;
     4.1 --- a/RawInput.cs	Sun Feb 15 18:15:41 2015 +0100
     4.2 +++ b/RawInput.cs	Sun Feb 15 19:53:27 2015 +0100
     4.3 @@ -175,15 +175,66 @@
     4.4                  return;
     4.5              }
     4.6  
     4.7 -            //For each our device add an node to our treeview
     4.8 +            //For each our device add a node to our treeview
     4.9              foreach (RAWINPUTDEVICELIST device in ridList)
    4.10              {
    4.11 -                string name=GetDeviceName(device.hDevice);
    4.12 -                aTreeView.Nodes.Add(name, name);
    4.13 +                Hid.HidDevice hidDevice=new Hid.HidDevice(device.hDevice);
    4.14 +
    4.15 +                //Work out proper suffix for our device root node.
    4.16 +                //That allows users to see in a glance what kind of device this is.
    4.17 +                string suffix = "";
    4.18 +                if (hidDevice.Info.dwType == RawInputDeviceType.RIM_TYPEHID)
    4.19 +                {
    4.20 +                    //Process usage page
    4.21 +                    if (Enum.IsDefined(typeof(Hid.UsagePage), hidDevice.Info.hid.usUsagePage))
    4.22 +                    {
    4.23 +                        //We know this usage page, add its name
    4.24 +                        Hid.UsagePage usagePage = (Hid.UsagePage)hidDevice.Info.hid.usUsagePage;
    4.25 +                        suffix += " ( " + usagePage.ToString() + ", ";
    4.26 +                    }
    4.27 +                    else
    4.28 +                    {
    4.29 +                        //We don't know this usage page, add its value
    4.30 +                        suffix += " ( 0x" + hidDevice.Info.hid.usUsagePage.ToString("X4") + ", ";
    4.31 +                    }
    4.32 +
    4.33 +                    //Process usage
    4.34 +                    //We don't know this usage page, add its value
    4.35 +                    suffix += "0x" + hidDevice.Info.hid.usUsage.ToString("X4") + " )";
    4.36 +                }
    4.37 +                else if (hidDevice.Info.dwType == RawInputDeviceType.RIM_TYPEKEYBOARD)
    4.38 +                {
    4.39 +                    suffix = " - Keyboard";
    4.40 +                }
    4.41 +                else if (hidDevice.Info.dwType == RawInputDeviceType.RIM_TYPEMOUSE)
    4.42 +                {
    4.43 +                    suffix = " - Mouse";
    4.44 +                }
    4.45 +
    4.46 +                TreeNode node = null;
    4.47 +                if (hidDevice.Product != null && hidDevice.Product.Length > 1)
    4.48 +                {
    4.49 +                    //Add the devices with a proper name at the top
    4.50 +                    node = aTreeView.Nodes.Insert(0, hidDevice.Name, hidDevice.Product + suffix);
    4.51 +                }
    4.52 +                else
    4.53 +                {
    4.54 +                    //Add other once at the bottom
    4.55 +                    node = aTreeView.Nodes.Add(hidDevice.Name, "0x" + hidDevice.ProductId.ToString("X4") + suffix);
    4.56 +                }
    4.57 +
    4.58 +                node.Nodes.Add("Manufacturer: " + hidDevice.Manufacturer);
    4.59 +                node.Nodes.Add("Product ID: 0x" + hidDevice.ProductId.ToString("X4"));
    4.60 +                node.Nodes.Add("Vendor ID: 0x" + hidDevice.VendorId.ToString("X4"));
    4.61 +                node.Nodes.Add("Version: " + hidDevice.Version);
    4.62 +                node.Nodes.Add(hidDevice.Info.dwType.ToString());
    4.63 +                if (hidDevice.Info.dwType == RawInputDeviceType.RIM_TYPEHID)
    4.64 +                {
    4.65 +                    node.Nodes.Add("UsagePage / UsageCollection: 0x" + hidDevice.Info.hid.usUsagePage.ToString("X4") + " / 0x" + hidDevice.Info.hid.usUsage.ToString("X4"));
    4.66 +                }
    4.67 +
    4.68 +                node.Nodes.Add(hidDevice.Name);
    4.69              }
    4.70 -
    4.71 -            //TreeNode node = null;
    4.72 -
    4.73          }
    4.74  
    4.75      }