# HG changeset patch
# User sl
# Date 1417905242 -3600
# Node ID 7679a5ab194bdc579341298b043af05d68bdf8b4
# Parent 6af1cbb3beb4cc5b3ce674075083a9a5d0528344
Adding HID handler.
diff -r 6af1cbb3beb4 -r 7679a5ab194b HidDevice.cs
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HidDevice.cs Sat Dec 06 23:34:02 2014 +0100
@@ -0,0 +1,94 @@
+using System;
+using System.Windows.Forms;
+using System.Runtime.InteropServices;
+using System.Diagnostics;
+using System.Text;
+using Microsoft.Win32.SafeHandles;
+
+namespace Hid
+{
+ ///
+ /// Represent a HID device.
+ ///
+ class HidDevice
+ {
+ public string Name { get; private set; }
+ public string Manufacturer { get; private set; }
+ public string Product { get; private set; }
+ public ushort VendorId { get; private set; }
+ public ushort ProductId { get; private set; }
+ public ushort Version { get; private set; }
+
+ ///
+ /// Class constructor will fetch this object properties from HID sub system.
+ ///
+ /// Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice
+ public HidDevice(IntPtr hRawInputDevice)
+ {
+ //Fetch various information defining the given HID device
+ Name = Win32.RawInput.GetDeviceName(hRawInputDevice);
+
+ //Open our device from the device name/path
+ SafeFileHandle handle=Win32.Function.CreateFile(Name,
+ Win32.FileAccess.NONE,
+ Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
+ IntPtr.Zero,
+ Win32.CreationDisposition.OPEN_EXISTING,
+ Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
+ IntPtr.Zero
+ );
+
+ if (handle.IsInvalid)
+ {
+ Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
+ }
+ else
+ {
+ //Get manufacturer string
+ StringBuilder manufacturerString = new StringBuilder(256);
+ if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
+ {
+ Manufacturer = manufacturerString.ToString();
+ }
+
+ //Get product string
+ StringBuilder productString = new StringBuilder(256);
+ if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
+ {
+ Product = productString.ToString();
+ }
+
+ //Get attributes
+ Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES();
+ if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
+ {
+ VendorId = attributes.VendorID;
+ ProductId = attributes.ProductID;
+ Version = attributes.VersionNumber;
+ }
+
+ handle.Close();
+ }
+ }
+
+ ///
+ /// Print information about this device to our debug output.
+ ///
+ public void DebugWrite()
+ {
+ Debug.WriteLine("================ HID =========================================================================================");
+ Debug.WriteLine("==== Name: " + Name);
+ Debug.WriteLine("==== Manufacturer: " + Manufacturer);
+ Debug.WriteLine("==== Product: " + Product);
+ Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
+ Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
+ Debug.WriteLine("==== Version: " + Version.ToString());
+ Debug.WriteLine("==============================================================================================================");
+ }
+
+
+
+
+ }
+
+}
\ No newline at end of file
diff -r 6af1cbb3beb4 -r 7679a5ab194b HidEvent.cs
--- a/HidEvent.cs Sat Dec 06 22:59:55 2014 +0100
+++ b/HidEvent.cs Sat Dec 06 23:34:02 2014 +0100
@@ -21,6 +21,8 @@
public bool IsMouse { get; private set; }
public bool IsKeyboard { get; private set; }
public bool IsGeneric { get; private set; }
+ public bool IsButtonDown { get { return Usages.Count == 1 && Usages[0] != 0; } }
+ public bool IsButtonUp { get { return Usages.Count == 1 && Usages[0] == 0; } }
public HidDevice Device { get; private set; }
diff -r 6af1cbb3beb4 -r 7679a5ab194b HidHandler.cs
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HidHandler.cs Sat Dec 06 23:34:02 2014 +0100
@@ -0,0 +1,48 @@
+using System;
+using System.Windows.Forms;
+using System.Runtime.InteropServices;
+using System.Diagnostics;
+using System.Text;
+using Microsoft.Win32.SafeHandles;
+using Win32;
+using System.Collections.Generic;
+
+
+namespace Hid
+{
+
+
+ ///
+ /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
+ ///
+ class HidHandler
+ {
+ public delegate void HidEventHandler(object aSender, HidEvent aHidEvent);
+ public event HidEventHandler OnHidEvent;
+
+ public bool IsRegistered { get; private set; }
+
+ public HidHandler(RAWINPUTDEVICE[] aRawInputDevices)
+ {
+ IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
+ }
+
+
+ public void ProcessInput(Message aMessage)
+ {
+ Hid.HidEvent hidEvent = new Hid.HidEvent(aMessage);
+ hidEvent.DebugWrite();
+
+ if (!hidEvent.IsValid || !hidEvent.IsGeneric)
+ {
+ Debug.WriteLine("Skipping HID message.");
+ return;
+ }
+
+ //Broadcast our events
+ OnHidEvent(this, hidEvent);
+ }
+
+ }
+
+}
\ No newline at end of file
diff -r 6af1cbb3beb4 -r 7679a5ab194b HidUtil.cs
--- a/HidUtil.cs Sat Dec 06 22:59:55 2014 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-using System;
-using System.Windows.Forms;
-using System.Runtime.InteropServices;
-using System.Diagnostics;
-using System.Text;
-using Microsoft.Win32.SafeHandles;
-
-namespace Hid
-{
- ///
- /// Represent a HID device.
- ///
- class HidDevice
- {
- public string Name { get; private set; }
- public string Manufacturer { get; private set; }
- public string Product { get; private set; }
- public ushort VendorId { get; private set; }
- public ushort ProductId { get; private set; }
- public ushort Version { get; private set; }
-
- ///
- /// Class constructor will fetch this object properties from HID sub system.
- ///
- /// Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice
- public HidDevice(IntPtr hRawInputDevice)
- {
- //Fetch various information defining the given HID device
- Name = Win32.RawInput.GetDeviceName(hRawInputDevice);
-
- //Open our device from the device name/path
- SafeFileHandle handle=Win32.Function.CreateFile(Name,
- Win32.FileAccess.NONE,
- Win32.FileShare.FILE_SHARE_READ|Win32.FileShare.FILE_SHARE_WRITE,
- IntPtr.Zero,
- Win32.CreationDisposition.OPEN_EXISTING,
- Win32.FileFlagsAttributes.FILE_FLAG_OVERLAPPED,
- IntPtr.Zero
- );
-
- if (handle.IsInvalid)
- {
- Debug.WriteLine("Failed to CreateFile from device name " + Marshal.GetLastWin32Error().ToString());
- }
- else
- {
- //Get manufacturer string
- StringBuilder manufacturerString = new StringBuilder(256);
- if (Win32.Function.HidD_GetManufacturerString(handle, manufacturerString, manufacturerString.Capacity))
- {
- Manufacturer = manufacturerString.ToString();
- }
-
- //Get product string
- StringBuilder productString = new StringBuilder(256);
- if (Win32.Function.HidD_GetProductString(handle, productString, productString.Capacity))
- {
- Product = productString.ToString();
- }
-
- //Get attributes
- Win32.HIDD_ATTRIBUTES attributes=new Win32.HIDD_ATTRIBUTES();
- if (Win32.Function.HidD_GetAttributes(handle, ref attributes))
- {
- VendorId = attributes.VendorID;
- ProductId = attributes.ProductID;
- Version = attributes.VersionNumber;
- }
-
- handle.Close();
- }
- }
-
- ///
- /// Print information about this device to our debug output.
- ///
- public void DebugWrite()
- {
- Debug.WriteLine("================ HID =========================================================================================");
- Debug.WriteLine("==== Name: " + Name);
- Debug.WriteLine("==== Manufacturer: " + Manufacturer);
- Debug.WriteLine("==== Product: " + Product);
- Debug.WriteLine("==== VendorID: 0x" + VendorId.ToString("X4"));
- Debug.WriteLine("==== ProductID: 0x" + ProductId.ToString("X4"));
- Debug.WriteLine("==== Version: " + Version.ToString());
- Debug.WriteLine("==============================================================================================================");
- }
-
-
-
-
- }
-
-}
\ No newline at end of file
diff -r 6af1cbb3beb4 -r 7679a5ab194b RemoteControlDevice.cs
--- a/RemoteControlDevice.cs Sat Dec 06 22:59:55 2014 +0100
+++ b/RemoteControlDevice.cs Sat Dec 06 23:34:02 2014 +0100
@@ -166,6 +166,8 @@
///
public delegate bool HidUsageHandler(ushort aUsage);
+ Hid.HidHandler iHidHandler;
+
//-------------------------------------------------------------
// constructors
@@ -216,10 +218,12 @@
//rid[i].hwndTarget = aHWND;
- if (!Function.RegisterRawInputDevices(rid, (uint)rid.Length, (uint)Marshal.SizeOf(rid[0])))
+ iHidHandler = new Hid.HidHandler(rid);
+ if (!iHidHandler.IsRegistered)
{
- throw new ApplicationException("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
+ Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
}
+ iHidHandler.OnHidEvent += HandleHidEvent;
}
@@ -367,31 +371,35 @@
}
}
-
+ ///
+ ///
+ ///
+ ///
private void ProcessInputCommand(ref Message message)
{
//We received a WM_INPUT message
Debug.WriteLine("================WM_INPUT================");
- Hid.HidEvent hidEvent = new Hid.HidEvent(message);
- hidEvent.DebugWrite();
+ iHidHandler.ProcessInput(message);
- if (!hidEvent.IsValid || !hidEvent.IsGeneric)
- {
- Debug.WriteLine("Skipping HID message.");
- return;
- }
+ }
-
+ ///
+ ///
+ ///
+ ///
+ ///
+ void HandleHidEvent(object aSender, Hid.HidEvent aHidEvent)
+ {
HidUsageHandler usagePageHandler = null;
//Check if this an MCE remote HID message
- if (hidEvent.UsagePage == (ushort)Hid.UsagePage.MceRemote && hidEvent.UsageCollection == (ushort)Hid.UsageIdMce.MceRemote)
+ if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.MceRemote && aHidEvent.UsageCollection == (ushort)Hid.UsageIdMce.MceRemote)
{
usagePageHandler = HidMceRemoteHandler;
}
//Check if this is a consumer control HID message
- else if (hidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer && hidEvent.UsageCollection == (ushort)Hid.UsageCollectionConsumer.ConsumerControl)
+ else if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer && aHidEvent.UsageCollection == (ushort)Hid.UsageCollectionConsumer.ConsumerControl)
{
usagePageHandler = HidConsumerDeviceHandler;
}
@@ -402,7 +410,7 @@
return;
}
- foreach (ushort usage in hidEvent.Usages)
+ foreach (ushort usage in aHidEvent.Usages)
{
usagePageHandler(usage);
}
diff -r 6af1cbb3beb4 -r 7679a5ab194b RemoteControlSample.csproj
--- a/RemoteControlSample.csproj Sat Dec 06 22:59:55 2014 +0100
+++ b/RemoteControlSample.csproj Sat Dec 06 23:34:02 2014 +0100
@@ -127,9 +127,10 @@
Form
+
+
-
Code