HidDevice.cs
author StephaneLenclud
Sun, 15 Feb 2015 13:30:54 +0100
changeset 59 89dfad9633b2
parent 56 cace0afae0fa
child 60 687cace560d2
permissions -rw-r--r--
Win32Hid: Fixing boolean bug and improving value caps union.
sl@25
     1
using System;
sl@25
     2
using System.Windows.Forms;
sl@25
     3
using System.Runtime.InteropServices;
sl@25
     4
using System.Diagnostics;
sl@25
     5
using System.Text;
sl@25
     6
using Microsoft.Win32.SafeHandles;
StephaneLenclud@53
     7
using Win32;
sl@25
     8
sl@25
     9
namespace Hid
sl@25
    10
{
sl@25
    11
    /// <summary>
sl@25
    12
    /// Represent a HID device.
sl@25
    13
    /// </summary>
StephaneLenclud@52
    14
    public class HidDevice: IDisposable
sl@25
    15
    {
sl@25
    16
        public string Name { get; private set; }
sl@25
    17
        public string Manufacturer { get; private set; }
sl@25
    18
        public string Product { get; private set; }
sl@25
    19
        public ushort VendorId { get; private set; }
sl@25
    20
        public ushort ProductId { get; private set; }
sl@25
    21
        public ushort Version { get; private set; }
StephaneLenclud@56
    22
        //Pre-parsed HID descriptor
StephaneLenclud@52
    23
        public IntPtr PreParsedData {get; private set;}
StephaneLenclud@56
    24
        //Info
StephaneLenclud@53
    25
        public RID_DEVICE_INFO Info { get {return iInfo;} }
StephaneLenclud@53
    26
        private RID_DEVICE_INFO iInfo;
StephaneLenclud@56
    27
        //Capabilities
StephaneLenclud@56
    28
        public HIDP_CAPS Capabilities { get { return iCapabilities; } }
StephaneLenclud@56
    29
        private HIDP_CAPS iCapabilities;
StephaneLenclud@56
    30
        //Input Button Capabilities
StephaneLenclud@56
    31
        public HIDP_BUTTON_CAPS[] InputButtonCapabilities { get { return iInputButtonCapabilities; } }
StephaneLenclud@56
    32
        private HIDP_BUTTON_CAPS[] iInputButtonCapabilities;
StephaneLenclud@58
    33
        //Input Value Capabilities
StephaneLenclud@58
    34
        public HIDP_VALUE_CAPS[] InputValueCapabilities { get { return iInputValueCapabilities; } }
StephaneLenclud@58
    35
        private HIDP_VALUE_CAPS[] iInputValueCapabilities;
StephaneLenclud@58
    36
StephaneLenclud@58
    37
        
StephaneLenclud@56
    38
        
sl@25
    39
sl@25
    40
        /// <summary>
sl@26
    41
        /// Class constructor will fetch this object properties from HID sub system.
sl@25
    42
        /// </summary>
sl@26
    43
        /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
sl@25
    44
        public HidDevice(IntPtr hRawInputDevice)
sl@25
    45
        {
StephaneLenclud@55
    46
            //Try construct and rollback if needed
StephaneLenclud@55
    47
            try
StephaneLenclud@55
    48
            {
StephaneLenclud@55
    49
                Construct(hRawInputDevice);
StephaneLenclud@55
    50
            }
StephaneLenclud@55
    51
            catch (System.Exception ex)
StephaneLenclud@55
    52
            {
StephaneLenclud@55
    53
                //Just rollback and propagate
StephaneLenclud@55
    54
                Dispose();
StephaneLenclud@55
    55
                throw ex;
StephaneLenclud@55
    56
            }
StephaneLenclud@55
    57
        }
StephaneLenclud@55
    58
StephaneLenclud@55
    59
StephaneLenclud@55
    60
        /// <summary>
StephaneLenclud@55
    61
        /// Make sure dispose is called even if the user forgot about it.
StephaneLenclud@55
    62
        /// </summary>
StephaneLenclud@55
    63
        ~HidDevice()
StephaneLenclud@55
    64
        {
StephaneLenclud@55
    65
            Dispose();
StephaneLenclud@55
    66
        }
StephaneLenclud@55
    67
StephaneLenclud@55
    68
        /// <summary>
StephaneLenclud@55
    69
        /// Private constructor.
StephaneLenclud@55
    70
        /// </summary>
StephaneLenclud@55
    71
        /// <param name="hRawInputDevice"></param>
StephaneLenclud@55
    72
        private void Construct(IntPtr hRawInputDevice)
StephaneLenclud@55
    73
        {
StephaneLenclud@52
    74
            PreParsedData = IntPtr.Zero;
StephaneLenclud@58
    75
            iInputButtonCapabilities = null;
StephaneLenclud@58
    76
            iInputValueCapabilities = null;
StephaneLenclud@58
    77
sl@25
    78
            //Fetch various information defining the given HID device
StephaneLenclud@52
    79
            Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
StephaneLenclud@52
    80
StephaneLenclud@52
    81
            //Get our HID descriptor pre-parsed data
StephaneLenclud@52
    82
            PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
StephaneLenclud@53
    83
            if (PreParsedData == IntPtr.Zero)
StephaneLenclud@53
    84
            {
StephaneLenclud@55
    85
                throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@53
    86
            }
StephaneLenclud@53
    87
StephaneLenclud@53
    88
            //Fetch device info
StephaneLenclud@53
    89
            iInfo = new RID_DEVICE_INFO();
StephaneLenclud@53
    90
            if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
StephaneLenclud@53
    91
            {
StephaneLenclud@55
    92
                throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@53
    93
            }
StephaneLenclud@55
    94
sl@25
    95
            //Open our device from the device name/path
StephaneLenclud@55
    96
            SafeFileHandle handle = Win32.Function.CreateFile(Name,
sl@25
    97
                Win32.FileAccess.NONE,
StephaneLenclud@55
    98
                Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
sl@25
    99
                IntPtr.Zero,
sl@25
   100
                Win32.CreationDisposition.OPEN_EXISTING,
sl@25
   101
                Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
sl@25
   102
                IntPtr.Zero
sl@25
   103
                );
sl@25
   104
StephaneLenclud@55
   105
            //Check if CreateFile worked
sl@25
   106
            if (handle.IsInvalid)
sl@25
   107
            {
StephaneLenclud@55
   108
                throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
sl@25
   109
            }
StephaneLenclud@55
   110
StephaneLenclud@55
   111
            //Get manufacturer string
StephaneLenclud@55
   112
            StringBuilder manufacturerString = new StringBuilder(256);
StephaneLenclud@55
   113
            if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
sl@25
   114
            {
StephaneLenclud@55
   115
                Manufacturer = manufacturerString.ToString();
StephaneLenclud@55
   116
            }
sl@25
   117
StephaneLenclud@55
   118
            //Get product string
StephaneLenclud@55
   119
            StringBuilder productString = new StringBuilder(256);
StephaneLenclud@55
   120
            if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
StephaneLenclud@55
   121
            {
StephaneLenclud@55
   122
                Product = productString.ToString();
StephaneLenclud@55
   123
            }
sl@25
   124
StephaneLenclud@55
   125
            //Get attributes
StephaneLenclud@55
   126
            Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
StephaneLenclud@55
   127
            if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
StephaneLenclud@55
   128
            {
StephaneLenclud@55
   129
                VendorId = attributes.VendorID;
StephaneLenclud@55
   130
                ProductId = attributes.ProductID;
StephaneLenclud@55
   131
                Version = attributes.VersionNumber;
StephaneLenclud@55
   132
            }
sl@25
   133
StephaneLenclud@55
   134
            handle.Close();
StephaneLenclud@56
   135
StephaneLenclud@56
   136
            //Get capabilities
StephaneLenclud@56
   137
            HidStatus status = Win32.Function.HidP_GetCaps(PreParsedData, ref iCapabilities);
StephaneLenclud@56
   138
            if (status != HidStatus.HIDP_STATUS_SUCCESS)
StephaneLenclud@56
   139
            {
StephaneLenclud@56
   140
                throw new Exception("HidDevice: HidP_GetCaps failed: " + status.ToString());
StephaneLenclud@56
   141
            }
StephaneLenclud@56
   142
StephaneLenclud@58
   143
            //Get input button caps if needed
StephaneLenclud@58
   144
            if (Capabilities.NumberInputButtonCaps > 0)
StephaneLenclud@56
   145
            {
StephaneLenclud@58
   146
                iInputButtonCapabilities = new HIDP_BUTTON_CAPS[Capabilities.NumberInputButtonCaps];
StephaneLenclud@58
   147
                ushort buttonCapabilitiesLength = Capabilities.NumberInputButtonCaps;
StephaneLenclud@58
   148
                status = Win32.Function.HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, iInputButtonCapabilities, ref buttonCapabilitiesLength, PreParsedData);
StephaneLenclud@58
   149
                if (status != HidStatus.HIDP_STATUS_SUCCESS || buttonCapabilitiesLength != Capabilities.NumberInputButtonCaps)
StephaneLenclud@58
   150
                {
StephaneLenclud@58
   151
                    throw new Exception("HidDevice: HidP_GetButtonCaps failed: " + status.ToString());
StephaneLenclud@58
   152
                }
StephaneLenclud@56
   153
            }
StephaneLenclud@58
   154
StephaneLenclud@58
   155
            //Get input value caps if needed
StephaneLenclud@58
   156
            if (Capabilities.NumberInputValueCaps > 0)
StephaneLenclud@58
   157
            {
StephaneLenclud@58
   158
                iInputValueCapabilities = new HIDP_VALUE_CAPS[Capabilities.NumberInputValueCaps];
StephaneLenclud@58
   159
                ushort valueCapabilitiesLength = Capabilities.NumberInputValueCaps;
StephaneLenclud@58
   160
                status = Win32.Function.HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, iInputValueCapabilities, ref valueCapabilitiesLength, PreParsedData);
StephaneLenclud@58
   161
                if (status != HidStatus.HIDP_STATUS_SUCCESS || valueCapabilitiesLength != Capabilities.NumberInputValueCaps)
StephaneLenclud@58
   162
                {
StephaneLenclud@58
   163
                    throw new Exception("HidDevice: HidP_GetValueCaps failed: " + status.ToString());
StephaneLenclud@58
   164
                }
StephaneLenclud@58
   165
            }
StephaneLenclud@58
   166
StephaneLenclud@56
   167
StephaneLenclud@52
   168
        }
StephaneLenclud@52
   169
StephaneLenclud@52
   170
        /// <summary>
StephaneLenclud@52
   171
        /// Dispose is just for unmanaged clean-up.
StephaneLenclud@52
   172
        /// Make sure calling disposed multiple times does not crash.
StephaneLenclud@52
   173
        /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
StephaneLenclud@52
   174
        /// </summary>
StephaneLenclud@52
   175
        public void Dispose()
StephaneLenclud@52
   176
        {
StephaneLenclud@52
   177
            Marshal.FreeHGlobal(PreParsedData);
StephaneLenclud@52
   178
            PreParsedData = IntPtr.Zero;
StephaneLenclud@52
   179
        }
StephaneLenclud@52
   180
sl@25
   181
        /// <summary>
sl@25
   182
        /// Print information about this device to our debug output.
sl@25
   183
        /// </summary>
sl@25
   184
        public void DebugWrite()
sl@25
   185
        {
sl@25
   186
            Debug.WriteLine("================ HID =========================================================================================");
sl@25
   187
            Debug.WriteLine("==== Name: " + Name);
sl@25
   188
            Debug.WriteLine("==== Manufacturer: " + Manufacturer);
sl@25
   189
            Debug.WriteLine("==== Product: " + Product);
sl@25
   190
            Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
sl@25
   191
            Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
sl@25
   192
            Debug.WriteLine("==== Version: " + Version.ToString());
sl@25
   193
            Debug.WriteLine("==============================================================================================================");
sl@25
   194
        }
sl@25
   195
sl@25
   196
    }
sl@25
   197
sl@25
   198
}