# HG changeset patch # User StephaneLenclud # Date 1423953672 -3600 # Node ID e7831b7815124a1e5be9ce9d9ca635b9f91c80c4 # Parent 2f34ceaf0692f1b892cf4972a7242fef949ad168 Device Info moves where it belongs into our device class. diff -r 2f34ceaf0692 -r e7831b781512 HidDevice.cs --- a/HidDevice.cs Sat Feb 14 23:23:41 2015 +0100 +++ b/HidDevice.cs Sat Feb 14 23:41:12 2015 +0100 @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Text; using Microsoft.Win32.SafeHandles; +using Win32; namespace Hid { @@ -19,6 +20,8 @@ public ushort ProductId { get; private set; } public ushort Version { get; private set; } public IntPtr PreParsedData {get; private set;} + public RID_DEVICE_INFO Info { get {return iInfo;} } + private RID_DEVICE_INFO iInfo; /// /// Class constructor will fetch this object properties from HID sub system. @@ -32,6 +35,17 @@ //Get our HID descriptor pre-parsed data PreParsedData = Win32.Utils.RawInput.GetPreParsedData(hRawInputDevice); + if (PreParsedData == IntPtr.Zero) + { + throw new Exception("HidDevice: GetPreParsedData failed!"); + } + + //Fetch device info + iInfo = new RID_DEVICE_INFO(); + if (!Win32.Utils.RawInput.GetDeviceInfo(hRawInputDevice, ref iInfo)) + { + throw new Exception("HidDevice: GetDeviceInfo failed!"); + } //Open our device from the device name/path SafeFileHandle handle=Win32.Function.CreateFile(Name, @@ -43,6 +57,7 @@ IntPtr.Zero ); + //TODO: should we throw instead? if (handle.IsInvalid) { Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString()); diff -r 2f34ceaf0692 -r e7831b781512 HidEvent.cs --- a/HidEvent.cs Sat Feb 14 23:23:41 2015 +0100 +++ b/HidEvent.cs Sat Feb 14 23:41:12 2015 +0100 @@ -109,13 +109,6 @@ return; } - //TODO: move this into our device class - //Fetch device info - RID_DEVICE_INFO deviceInfo = new RID_DEVICE_INFO(); - if (!Win32.Utils.RawInput.GetDeviceInfo(rawInput.header.hDevice, ref deviceInfo)) - { - return; - } //Get various information about this HID device Device = new Hid.HidDevice(rawInput.header.hDevice); @@ -127,8 +120,8 @@ Debug.WriteLine("WM_INPUT source device is HID."); //Get Usage Page and Usage //Debug.WriteLine("Usage Page: 0x" + deviceInfo.hid.usUsagePage.ToString("X4") + " Usage ID: 0x" + deviceInfo.hid.usUsage.ToString("X4")); - UsagePage = deviceInfo.hid.usUsagePage; - UsageCollection = deviceInfo.hid.usUsage; + UsagePage = Device.Info.hid.usUsagePage; + UsageCollection = Device.Info.hid.usUsage; 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 && rawInput.hid.dwCount > 0)) //Check that we have at least one HID msg @@ -209,12 +202,12 @@ Debug.WriteLine("WM_INPUT source device is Keyboard."); // do keyboard handling... - Debug.WriteLine("Type: " + deviceInfo.keyboard.dwType.ToString()); - Debug.WriteLine("SubType: " + deviceInfo.keyboard.dwSubType.ToString()); - Debug.WriteLine("Mode: " + deviceInfo.keyboard.dwKeyboardMode.ToString()); - Debug.WriteLine("Number of function keys: " + deviceInfo.keyboard.dwNumberOfFunctionKeys.ToString()); - Debug.WriteLine("Number of indicators: " + deviceInfo.keyboard.dwNumberOfIndicators.ToString()); - Debug.WriteLine("Number of keys total: " + deviceInfo.keyboard.dwNumberOfKeysTotal.ToString()); + Debug.WriteLine("Type: " + Device.Info.keyboard.dwType.ToString()); + Debug.WriteLine("SubType: " + Device.Info.keyboard.dwSubType.ToString()); + Debug.WriteLine("Mode: " + Device.Info.keyboard.dwKeyboardMode.ToString()); + Debug.WriteLine("Number of function keys: " + Device.Info.keyboard.dwNumberOfFunctionKeys.ToString()); + Debug.WriteLine("Number of indicators: " + Device.Info.keyboard.dwNumberOfIndicators.ToString()); + Debug.WriteLine("Number of keys total: " + Device.Info.keyboard.dwNumberOfKeysTotal.ToString()); } } finally