HidDevice.cs
author StephaneLenclud
Sun, 15 Feb 2015 11:53:36 +0100
changeset 56 cace0afae0fa
parent 55 08c70af8af84
child 58 7ef0f9dc229c
permissions -rw-r--r--
HidDevice now supports capabilities and button capabilities.
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@56
    33
        
sl@25
    34
sl@25
    35
        /// <summary>
sl@26
    36
        /// Class constructor will fetch this object properties from HID sub system.
sl@25
    37
        /// </summary>
sl@26
    38
        /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
sl@25
    39
        public HidDevice(IntPtr hRawInputDevice)
sl@25
    40
        {
StephaneLenclud@55
    41
            //Try construct and rollback if needed
StephaneLenclud@55
    42
            try
StephaneLenclud@55
    43
            {
StephaneLenclud@55
    44
                Construct(hRawInputDevice);
StephaneLenclud@55
    45
            }
StephaneLenclud@55
    46
            catch (System.Exception ex)
StephaneLenclud@55
    47
            {
StephaneLenclud@55
    48
                //Just rollback and propagate
StephaneLenclud@55
    49
                Dispose();
StephaneLenclud@55
    50
                throw ex;
StephaneLenclud@55
    51
            }
StephaneLenclud@55
    52
        }
StephaneLenclud@55
    53
StephaneLenclud@55
    54
StephaneLenclud@55
    55
        /// <summary>
StephaneLenclud@55
    56
        /// Make sure dispose is called even if the user forgot about it.
StephaneLenclud@55
    57
        /// </summary>
StephaneLenclud@55
    58
        ~HidDevice()
StephaneLenclud@55
    59
        {
StephaneLenclud@55
    60
            Dispose();
StephaneLenclud@55
    61
        }
StephaneLenclud@55
    62
StephaneLenclud@55
    63
        /// <summary>
StephaneLenclud@55
    64
        /// Private constructor.
StephaneLenclud@55
    65
        /// </summary>
StephaneLenclud@55
    66
        /// <param name="hRawInputDevice"></param>
StephaneLenclud@55
    67
        private void Construct(IntPtr hRawInputDevice)
StephaneLenclud@55
    68
        {
StephaneLenclud@52
    69
            PreParsedData = IntPtr.Zero;
sl@25
    70
            //Fetch various information defining the given HID device
StephaneLenclud@52
    71
            Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
StephaneLenclud@52
    72
StephaneLenclud@52
    73
            //Get our HID descriptor pre-parsed data
StephaneLenclud@52
    74
            PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
StephaneLenclud@53
    75
            if (PreParsedData == IntPtr.Zero)
StephaneLenclud@53
    76
            {
StephaneLenclud@55
    77
                throw new Exception("HidDevice: GetPreParsedData failed: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@53
    78
            }
StephaneLenclud@53
    79
StephaneLenclud@53
    80
            //Fetch device info
StephaneLenclud@53
    81
            iInfo = new RID_DEVICE_INFO();
StephaneLenclud@53
    82
            if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo))
StephaneLenclud@53
    83
            {
StephaneLenclud@55
    84
                throw new Exception("HidDevice: GetDeviceInfo failed: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@53
    85
            }
StephaneLenclud@55
    86
sl@25
    87
            //Open our device from the device name/path
StephaneLenclud@55
    88
            SafeFileHandle handle = Win32.Function.CreateFile(Name,
sl@25
    89
                Win32.FileAccess.NONE,
StephaneLenclud@55
    90
                Win32.FileShare.FILE_SHARE_READ | Win32.FileShare.FILE_SHARE_WRITE,
sl@25
    91
                IntPtr.Zero,
sl@25
    92
                Win32.CreationDisposition.OPEN_EXISTING,
sl@25
    93
                Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
sl@25
    94
                IntPtr.Zero
sl@25
    95
                );
sl@25
    96
StephaneLenclud@55
    97
            //Check if CreateFile worked
sl@25
    98
            if (handle.IsInvalid)
sl@25
    99
            {
StephaneLenclud@55
   100
                throw new Exception("HidDevice: CreateFile failed: " + Marshal.GetLastWin32Error().ToString());
sl@25
   101
            }
StephaneLenclud@55
   102
StephaneLenclud@55
   103
            //Get manufacturer string
StephaneLenclud@55
   104
            StringBuilder manufacturerString = new StringBuilder(256);
StephaneLenclud@55
   105
            if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
sl@25
   106
            {
StephaneLenclud@55
   107
                Manufacturer = manufacturerString.ToString();
StephaneLenclud@55
   108
            }
sl@25
   109
StephaneLenclud@55
   110
            //Get product string
StephaneLenclud@55
   111
            StringBuilder productString = new StringBuilder(256);
StephaneLenclud@55
   112
            if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
StephaneLenclud@55
   113
            {
StephaneLenclud@55
   114
                Product = productString.ToString();
StephaneLenclud@55
   115
            }
sl@25
   116
StephaneLenclud@55
   117
            //Get attributes
StephaneLenclud@55
   118
            Win32.HIDD_ATTRIBUTES attributes = new Win32.HIDD_ATTRIBUTES();
StephaneLenclud@55
   119
            if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
StephaneLenclud@55
   120
            {
StephaneLenclud@55
   121
                VendorId = attributes.VendorID;
StephaneLenclud@55
   122
                ProductId = attributes.ProductID;
StephaneLenclud@55
   123
                Version = attributes.VersionNumber;
StephaneLenclud@55
   124
            }
sl@25
   125
StephaneLenclud@55
   126
            handle.Close();
StephaneLenclud@56
   127
StephaneLenclud@56
   128
            //Get capabilities
StephaneLenclud@56
   129
            HidStatus status = Win32.Function.HidP_GetCaps(PreParsedData, ref iCapabilities);
StephaneLenclud@56
   130
            if (status != HidStatus.HIDP_STATUS_SUCCESS)
StephaneLenclud@56
   131
            {
StephaneLenclud@56
   132
                throw new Exception("HidDevice: HidP_GetCaps failed: " + status.ToString());
StephaneLenclud@56
   133
            }
StephaneLenclud@56
   134
StephaneLenclud@56
   135
            //Get input button caps
StephaneLenclud@56
   136
            iInputButtonCapabilities = new HIDP_BUTTON_CAPS[Capabilities.NumberInputButtonCaps];
StephaneLenclud@56
   137
            ushort buttonCapabilitiesLength = Capabilities.NumberInputButtonCaps;
StephaneLenclud@56
   138
            status = Win32.Function.HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, iInputButtonCapabilities, ref buttonCapabilitiesLength, PreParsedData);
StephaneLenclud@56
   139
            if (status != HidStatus.HIDP_STATUS_SUCCESS || buttonCapabilitiesLength != Capabilities.NumberInputButtonCaps)
StephaneLenclud@56
   140
            {
StephaneLenclud@56
   141
                throw new Exception("HidDevice: HidP_GetButtonCaps failed: " + status.ToString());
StephaneLenclud@56
   142
            }
StephaneLenclud@56
   143
            
StephaneLenclud@56
   144
StephaneLenclud@52
   145
        }
StephaneLenclud@52
   146
StephaneLenclud@52
   147
        /// <summary>
StephaneLenclud@52
   148
        /// Dispose is just for unmanaged clean-up.
StephaneLenclud@52
   149
        /// Make sure calling disposed multiple times does not crash.
StephaneLenclud@52
   150
        /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
StephaneLenclud@52
   151
        /// </summary>
StephaneLenclud@52
   152
        public void Dispose()
StephaneLenclud@52
   153
        {
StephaneLenclud@52
   154
            Marshal.FreeHGlobal(PreParsedData);
StephaneLenclud@52
   155
            PreParsedData = IntPtr.Zero;
StephaneLenclud@52
   156
        }
StephaneLenclud@52
   157
sl@25
   158
        /// <summary>
sl@25
   159
        /// Print information about this device to our debug output.
sl@25
   160
        /// </summary>
sl@25
   161
        public void DebugWrite()
sl@25
   162
        {
sl@25
   163
            Debug.WriteLine("================ HID =========================================================================================");
sl@25
   164
            Debug.WriteLine("==== Name: " + Name);
sl@25
   165
            Debug.WriteLine("==== Manufacturer: " + Manufacturer);
sl@25
   166
            Debug.WriteLine("==== Product: " + Product);
sl@25
   167
            Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
sl@25
   168
            Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
sl@25
   169
            Debug.WriteLine("==== Version: " + Version.ToString());
sl@25
   170
            Debug.WriteLine("==============================================================================================================");
sl@25
   171
        }
sl@25
   172
sl@25
   173
    }
sl@25
   174
sl@25
   175
}