Moving pre-parsed data to our device class.
1.1 --- a/HidDevice.cs Sat Feb 14 22:13:31 2015 +0100
1.2 +++ b/HidDevice.cs Sat Feb 14 23:23:41 2015 +0100
1.3 @@ -10,7 +10,7 @@
1.4 /// <summary>
1.5 /// Represent a HID device.
1.6 /// </summary>
1.7 - public class HidDevice
1.8 + public class HidDevice: IDisposable
1.9 {
1.10 public string Name { get; private set; }
1.11 public string Manufacturer { get; private set; }
1.12 @@ -18,6 +18,7 @@
1.13 public ushort VendorId { get; private set; }
1.14 public ushort ProductId { get; private set; }
1.15 public ushort Version { get; private set; }
1.16 + public IntPtr PreParsedData {get; private set;}
1.17
1.18 /// <summary>
1.19 /// Class constructor will fetch this object properties from HID sub system.
1.20 @@ -25,8 +26,12 @@
1.21 /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
1.22 public HidDevice(IntPtr hRawInputDevice)
1.23 {
1.24 + PreParsedData = IntPtr.Zero;
1.25 //Fetch various information defining the given HID device
1.26 - Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
1.27 + Name = Win32.Utils.RawInput.GetDeviceName(hRawInputDevice);
1.28 +
1.29 + //Get our HID descriptor pre-parsed data
1.30 + PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice);
1.31
1.32 //Open our device from the device name/path
1.33 SafeFileHandle handle=Win32.Function.CreateFile(Name,
1.34 @@ -71,6 +76,26 @@
1.35 }
1.36 }
1.37
1.38 +
1.39 + /// <summary>
1.40 + /// Make sure dispose is called even if the user forgot about it.
1.41 + /// </summary>
1.42 + ~HidDevice()
1.43 + {
1.44 + Dispose();
1.45 + }
1.46 +
1.47 + /// <summary>
1.48 + /// Dispose is just for unmanaged clean-up.
1.49 + /// Make sure calling disposed multiple times does not crash.
1.50 + /// See: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238
1.51 + /// </summary>
1.52 + public void Dispose()
1.53 + {
1.54 + Marshal.FreeHGlobal(PreParsedData);
1.55 + PreParsedData = IntPtr.Zero;
1.56 + }
1.57 +
1.58 /// <summary>
1.59 /// Print information about this device to our debug output.
1.60 /// </summary>
1.61 @@ -86,9 +111,6 @@
1.62 Debug.WriteLine("==============================================================================================================");
1.63 }
1.64
1.65 -
1.66 -
1.67 -
1.68 }
1.69
1.70 }
1.71 \ No newline at end of file
2.1 --- a/HidEvent.cs Sat Feb 14 22:13:31 2015 +0100
2.2 +++ b/HidEvent.cs Sat Feb 14 23:23:41 2015 +0100
2.3 @@ -99,8 +99,6 @@
2.4
2.5 //Declare some pointers
2.6 IntPtr rawInputBuffer = IntPtr.Zero;
2.7 - //My understanding is that this is basically our HID descriptor
2.8 - IntPtr preParsedData = IntPtr.Zero;
2.9
2.10 try
2.11 {
2.12 @@ -111,6 +109,7 @@
2.13 return;
2.14 }
2.15
2.16 + //TODO: move this into our device class
2.17 //Fetch device info
2.18 RID_DEVICE_INFO deviceInfo = new RID_DEVICE_INFO();
2.19 if (!Win32.Utils.RawInput.GetDeviceInfo(rawInput.header.hDevice, ref deviceInfo))
2.20 @@ -131,8 +130,6 @@
2.21 UsagePage = deviceInfo.hid.usUsagePage;
2.22 UsageCollection = deviceInfo.hid.usUsage;
2.23
2.24 - preParsedData = Win32.Utils.RawInput.GetPreParsedData(rawInput.header.hDevice);
2.25 -
2.26 if (!(rawInput.hid.dwSizeHid > 1 //Make sure our HID msg size more than 1. In fact the first ushort is irrelevant to us for now
2.27 && rawInput.hid.dwCount > 0)) //Check that we have at least one HID msg
2.28 {
2.29 @@ -167,13 +164,13 @@
2.30 //First query our usage count
2.31 uint usageCount = 0;
2.32 Win32.USAGE_AND_PAGE[] usages = null;
2.33 - Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, InputReport, (uint)InputReport.Length);
2.34 + Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, Device.PreParsedData, InputReport, (uint)InputReport.Length);
2.35 if (status == Win32.HidStatus.HIDP_STATUS_BUFFER_TOO_SMALL)
2.36 {
2.37 //Allocate a large enough buffer
2.38 usages = new Win32.USAGE_AND_PAGE[usageCount];
2.39 //...and fetch our usages
2.40 - status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, InputReport, (uint)InputReport.Length);
2.41 + status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, Device.PreParsedData, InputReport, (uint)InputReport.Length);
2.42 if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
2.43 {
2.44 Debug.WriteLine("Second pass could not parse HID data: " + status.ToString());
2.45 @@ -186,6 +183,7 @@
2.46
2.47 Debug.WriteLine("Usage count: " + usageCount.ToString());
2.48
2.49 + //Copy usages into this event
2.50 if (usages != null)
2.51 {
2.52 foreach (USAGE_AND_PAGE up in usages)
2.53 @@ -195,7 +193,7 @@
2.54 //Add this usage to our list
2.55 Usages.Add(up.Usage);
2.56 }
2.57 - }
2.58 + }
2.59 }
2.60 }
2.61 else if (rawInput.header.dwType == Const.RIM_TYPEMOUSE)
2.62 @@ -223,7 +221,6 @@
2.63 {
2.64 //Always executed when leaving our try block
2.65 Marshal.FreeHGlobal(rawInputBuffer);
2.66 - Marshal.FreeHGlobal(preParsedData);
2.67 }
2.68
2.69 //