RawInput.cs
author sl
Sat, 06 Dec 2014 00:59:59 +0100
changeset 19 d066e3999973
parent 17 8f7e35c3bfd1
child 22 8b2a3da54187
permissions -rw-r--r--
Starting to remove special case for app commands.
Adding a whole bunch of consumer controls.
     1 using System;
     2 using System.Runtime.InteropServices;
     3 using System.Diagnostics;
     4 
     5 
     6 namespace Win32
     7 {
     8     /// <summary>
     9     /// Provide some utility functions for raw input handling.
    10     /// </summary>
    11     static class RawInput
    12     {
    13         /// <summary>
    14         /// 
    15         /// </summary>
    16         /// <param name="aRawInputHandle"></param>
    17         /// <param name="aRawInput"></param>
    18         /// <param name="rawInputBuffer">Caller must free up memory on the pointer using Marshal.FreeHGlobal</param>
    19         /// <returns></returns>
    20         public static bool GetRawInputData(IntPtr aRawInputHandle, ref RAWINPUT aRawInput, ref IntPtr rawInputBuffer)
    21         {
    22             bool success = true;
    23             rawInputBuffer = IntPtr.Zero;
    24 
    25             try
    26             {
    27                 uint dwSize = 0;
    28                 uint sizeOfHeader = (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER));
    29 
    30                 //Get the size of our raw input data.
    31                 Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, IntPtr.Zero, ref dwSize, sizeOfHeader);
    32 
    33                 //Allocate a large enough buffer
    34                  rawInputBuffer = Marshal.AllocHGlobal((int)dwSize);
    35 
    36                 //Now read our RAWINPUT data
    37                 if (Win32.Function.GetRawInputData(aRawInputHandle, Const.RID_INPUT, rawInputBuffer, ref dwSize, (uint)Marshal.SizeOf(typeof(RAWINPUTHEADER))) != dwSize)
    38                 {
    39                     return false;
    40                 }
    41 
    42                 //Cast our buffer
    43                 aRawInput = (RAWINPUT)Marshal.PtrToStructure(rawInputBuffer, typeof(RAWINPUT));
    44             }
    45             catch
    46             {
    47                 Debug.WriteLine("GetRawInputData failed!");
    48                 success = false;
    49             }
    50 
    51             return success;
    52         }
    53 
    54         /// <summary>
    55         /// 
    56         /// </summary>
    57         /// <param name="aRawInputHandle"></param>
    58         /// <param name="aUsagePage"></param>
    59         /// <param name="aUsage"></param>
    60         /// <returns></returns>
    61         public static bool GetDeviceInfo(IntPtr hDevice, ref RID_DEVICE_INFO deviceInfo)
    62         {
    63             bool success = true;
    64             IntPtr deviceInfoBuffer = IntPtr.Zero;
    65             try
    66             {
    67                 //Get Device Info
    68                 uint deviceInfoSize = (uint)Marshal.SizeOf(typeof(RID_DEVICE_INFO));
    69                 deviceInfoBuffer = Marshal.AllocHGlobal((int)deviceInfoSize);
    70 
    71                 int res = Win32.Function.GetRawInputDeviceInfo(hDevice, Const.RIDI_DEVICEINFO, deviceInfoBuffer, ref deviceInfoSize);
    72                 if (res <= 0)
    73                 {
    74                     Debug.WriteLine("WM_INPUT could not read device info: " + Marshal.GetLastWin32Error().ToString());
    75                     return false;
    76                 }
    77 
    78                 //Cast our buffer
    79                 deviceInfo = (RID_DEVICE_INFO)Marshal.PtrToStructure(deviceInfoBuffer, typeof(RID_DEVICE_INFO));
    80             }
    81             catch
    82             {
    83                 Debug.WriteLine("GetRawInputData failed!");
    84                 success = false;
    85             }
    86             finally
    87             {
    88                 //Always executes, prevents memory leak
    89                 Marshal.FreeHGlobal(deviceInfoBuffer);
    90             }
    91 
    92             
    93             return success;
    94         }
    95 
    96         /// <summary>
    97         /// Fetch pre-parsed data corresponding to HID descriptor for the given HID device.
    98         /// </summary>
    99         /// <param name="device"></param>
   100         /// <returns></returns>
   101         public static IntPtr GetPreParsedData(IntPtr hDevice)
   102         {
   103             uint ppDataSize = 256;
   104             int result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, IntPtr.Zero, ref ppDataSize);
   105             if (result != 0)
   106             {
   107                 Debug.WriteLine("Failed to get raw input pre-parsed data size" + result + Marshal.GetLastWin32Error());
   108                 return IntPtr.Zero;
   109             }
   110 
   111             IntPtr ppData = Marshal.AllocHGlobal((int)ppDataSize);
   112             result = Win32.Function.GetRawInputDeviceInfo(hDevice, Win32.Const.RIDI_PREPARSEDDATA, ppData, ref ppDataSize);
   113             if (result <= 0)
   114             {
   115                 Debug.WriteLine("Failed to get raw input pre-parsed data" + result + Marshal.GetLastWin32Error());
   116                 return IntPtr.Zero;
   117             }
   118             return ppData;
   119         }
   120 
   121 
   122     }
   123 }