sl@27
|
1 |
using System;
|
sl@27
|
2 |
using System.Windows.Forms;
|
sl@27
|
3 |
using System.Runtime.InteropServices;
|
sl@27
|
4 |
using System.Diagnostics;
|
sl@27
|
5 |
using System.Text;
|
sl@27
|
6 |
using Microsoft.Win32.SafeHandles;
|
sl@27
|
7 |
using Win32;
|
sl@27
|
8 |
using System.Collections.Generic;
|
sl@27
|
9 |
|
sl@27
|
10 |
|
sl@27
|
11 |
namespace Hid
|
sl@27
|
12 |
{
|
sl@27
|
13 |
/// <summary>
|
sl@27
|
14 |
/// Represent a HID event.
|
sl@27
|
15 |
/// </summary>
|
sl@27
|
16 |
class HidEvent
|
sl@27
|
17 |
{
|
sl@27
|
18 |
public bool IsValid { get; private set; }
|
sl@27
|
19 |
public bool IsForeground { get; private set; }
|
sl@27
|
20 |
public bool IsBackground { get{return !IsForeground;} }
|
sl@27
|
21 |
public bool IsMouse { get; private set; }
|
sl@27
|
22 |
public bool IsKeyboard { get; private set; }
|
sl@27
|
23 |
public bool IsGeneric { get; private set; }
|
sl@29
|
24 |
public bool IsButtonDown { get { return Usages.Count == 1 && Usages[0] != 0; } }
|
sl@29
|
25 |
public bool IsButtonUp { get { return Usages.Count == 1 && Usages[0] == 0; } }
|
sl@27
|
26 |
|
sl@27
|
27 |
public HidDevice Device { get; private set; }
|
sl@27
|
28 |
|
sl@27
|
29 |
public ushort UsagePage { get; private set; }
|
sl@27
|
30 |
public ushort UsageCollection { get; private set; }
|
sl@31
|
31 |
public uint UsageId { get { return ((uint)UsagePage << 16 | (uint)UsageCollection); } }
|
sl@27
|
32 |
public List<ushort> Usages { get; private set; }
|
sl@27
|
33 |
|
sl@27
|
34 |
|
sl@27
|
35 |
/// <summary>
|
sl@27
|
36 |
/// Initialize an HidEvent from a WM_INPUT message
|
sl@27
|
37 |
/// </summary>
|
sl@27
|
38 |
/// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
|
sl@27
|
39 |
public HidEvent(Message aMessage)
|
sl@27
|
40 |
{
|
sl@27
|
41 |
IsValid = false;
|
sl@27
|
42 |
IsKeyboard = false;
|
sl@27
|
43 |
IsGeneric = false;
|
sl@27
|
44 |
|
sl@27
|
45 |
Usages = new List<ushort>();
|
sl@27
|
46 |
|
sl@27
|
47 |
if (aMessage.Msg != Const.WM_INPUT)
|
sl@27
|
48 |
{
|
sl@27
|
49 |
//Has to be a WM_INPUT message
|
sl@27
|
50 |
return;
|
sl@27
|
51 |
}
|
sl@27
|
52 |
|
sl@27
|
53 |
if (Macro.GET_RAWINPUT_CODE_WPARAM(aMessage.WParam) == Const.RIM_INPUT)
|
sl@27
|
54 |
{
|
sl@27
|
55 |
IsForeground = true;
|
sl@27
|
56 |
}
|
sl@27
|
57 |
else if (Macro.GET_RAWINPUT_CODE_WPARAM(aMessage.WParam) == Const.RIM_INPUTSINK)
|
sl@27
|
58 |
{
|
sl@27
|
59 |
IsForeground = false;
|
sl@27
|
60 |
}
|
sl@27
|
61 |
|
sl@27
|
62 |
//Declare some pointers
|
sl@27
|
63 |
IntPtr rawInputBuffer = IntPtr.Zero;
|
sl@27
|
64 |
//My understanding is that this is basically our HID descriptor
|
sl@27
|
65 |
IntPtr preParsedData = IntPtr.Zero;
|
sl@27
|
66 |
|
sl@27
|
67 |
try
|
sl@27
|
68 |
{
|
sl@27
|
69 |
//Fetch raw input
|
sl@27
|
70 |
RAWINPUT rawInput = new RAWINPUT();
|
sl@27
|
71 |
if (!RawInput.GetRawInputData(aMessage.LParam, ref rawInput, ref rawInputBuffer))
|
sl@27
|
72 |
{
|
sl@27
|
73 |
return;
|
sl@27
|
74 |
}
|
sl@27
|
75 |
|
sl@27
|
76 |
//Fetch device info
|
sl@27
|
77 |
RID_DEVICE_INFO deviceInfo = new RID_DEVICE_INFO();
|
sl@27
|
78 |
if (!RawInput.GetDeviceInfo(rawInput.header.hDevice, ref deviceInfo))
|
sl@27
|
79 |
{
|
sl@27
|
80 |
return;
|
sl@27
|
81 |
}
|
sl@27
|
82 |
|
sl@27
|
83 |
//Get various information about this HID device
|
sl@27
|
84 |
Device = new Hid.HidDevice(rawInput.header.hDevice);
|
sl@27
|
85 |
|
sl@27
|
86 |
if (rawInput.header.dwType == Const.RIM_TYPEHID) //Check that our raw input is HID
|
sl@27
|
87 |
{
|
sl@27
|
88 |
IsGeneric = true;
|
sl@27
|
89 |
|
sl@27
|
90 |
Debug.WriteLine("WM_INPUT source device is HID.");
|
sl@27
|
91 |
//Get Usage Page and Usage
|
sl@27
|
92 |
//Debug.WriteLine("Usage Page: 0x" + deviceInfo.hid.usUsagePage.ToString("X4") + " Usage ID: 0x" + deviceInfo.hid.usUsage.ToString("X4"));
|
sl@27
|
93 |
UsagePage = deviceInfo.hid.usUsagePage;
|
sl@27
|
94 |
UsageCollection = deviceInfo.hid.usUsage;
|
sl@27
|
95 |
|
sl@27
|
96 |
preParsedData = RawInput.GetPreParsedData(rawInput.header.hDevice);
|
sl@27
|
97 |
|
sl@27
|
98 |
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
|
sl@27
|
99 |
&& rawInput.hid.dwCount > 0)) //Check that we have at least one HID msg
|
sl@27
|
100 |
{
|
sl@27
|
101 |
return;
|
sl@27
|
102 |
}
|
sl@27
|
103 |
|
sl@27
|
104 |
//Allocate a buffer for one HID input
|
sl@27
|
105 |
byte[] hidInputReport = new byte[rawInput.hid.dwSizeHid];
|
sl@27
|
106 |
|
sl@27
|
107 |
Debug.WriteLine("Raw input contains " + rawInput.hid.dwCount + " HID input report(s)");
|
sl@27
|
108 |
|
sl@27
|
109 |
//For each HID input report in our raw input
|
sl@27
|
110 |
for (int i = 0; i < rawInput.hid.dwCount; i++)
|
sl@27
|
111 |
{
|
sl@27
|
112 |
//Compute the address from which to copy our HID input
|
sl@27
|
113 |
int hidInputOffset = 0;
|
sl@27
|
114 |
unsafe
|
sl@27
|
115 |
{
|
sl@27
|
116 |
byte* source = (byte*)rawInputBuffer;
|
sl@27
|
117 |
source += sizeof(RAWINPUTHEADER) + sizeof(RAWHID) + (rawInput.hid.dwSizeHid * i);
|
sl@27
|
118 |
hidInputOffset = (int)source;
|
sl@27
|
119 |
}
|
sl@27
|
120 |
|
sl@27
|
121 |
//Copy HID input into our buffer
|
sl@27
|
122 |
Marshal.Copy(new IntPtr(hidInputOffset), hidInputReport, 0, (int)rawInput.hid.dwSizeHid);
|
sl@27
|
123 |
|
sl@27
|
124 |
//Print HID input report in our debug output
|
sl@27
|
125 |
string hidDump = "HID input report: ";
|
sl@27
|
126 |
foreach (byte b in hidInputReport)
|
sl@27
|
127 |
{
|
sl@27
|
128 |
hidDump += b.ToString("X2");
|
sl@27
|
129 |
}
|
sl@27
|
130 |
Debug.WriteLine(hidDump);
|
sl@27
|
131 |
|
sl@27
|
132 |
//Proper parsing now
|
sl@27
|
133 |
uint usageCount = 1; //Assuming a single usage per input report. Is that correct?
|
sl@27
|
134 |
Win32.USAGE_AND_PAGE[] usages = new Win32.USAGE_AND_PAGE[usageCount];
|
sl@27
|
135 |
Win32.HidStatus status = Win32.Function.HidP_GetUsagesEx(Win32.HIDP_REPORT_TYPE.HidP_Input, 0, usages, ref usageCount, preParsedData, hidInputReport, (uint)hidInputReport.Length);
|
sl@27
|
136 |
if (status != Win32.HidStatus.HIDP_STATUS_SUCCESS)
|
sl@27
|
137 |
{
|
sl@27
|
138 |
Debug.WriteLine("Could not parse HID data!");
|
sl@27
|
139 |
}
|
sl@27
|
140 |
else
|
sl@27
|
141 |
{
|
sl@27
|
142 |
//Debug.WriteLine("UsagePage: 0x" + usages[0].UsagePage.ToString("X4"));
|
sl@27
|
143 |
//Debug.WriteLine("Usage: 0x" + usages[0].Usage.ToString("X4"));
|
sl@27
|
144 |
//Add this usage to our list
|
sl@27
|
145 |
Usages.Add(usages[0].Usage);
|
sl@27
|
146 |
}
|
sl@27
|
147 |
}
|
sl@27
|
148 |
|
sl@27
|
149 |
}
|
sl@27
|
150 |
else if (rawInput.header.dwType == Const.RIM_TYPEMOUSE)
|
sl@27
|
151 |
{
|
sl@27
|
152 |
IsMouse = true;
|
sl@27
|
153 |
|
sl@27
|
154 |
Debug.WriteLine("WM_INPUT source device is Mouse.");
|
sl@27
|
155 |
// do mouse handling...
|
sl@27
|
156 |
}
|
sl@27
|
157 |
else if (rawInput.header.dwType == Const.RIM_TYPEKEYBOARD)
|
sl@27
|
158 |
{
|
sl@27
|
159 |
IsKeyboard = true;
|
sl@27
|
160 |
|
sl@27
|
161 |
Debug.WriteLine("WM_INPUT source device is Keyboard.");
|
sl@27
|
162 |
// do keyboard handling...
|
sl@27
|
163 |
Debug.WriteLine("Type: " + deviceInfo.keyboard.dwType.ToString());
|
sl@27
|
164 |
Debug.WriteLine("SubType: " + deviceInfo.keyboard.dwSubType.ToString());
|
sl@27
|
165 |
Debug.WriteLine("Mode: " + deviceInfo.keyboard.dwKeyboardMode.ToString());
|
sl@27
|
166 |
Debug.WriteLine("Number of function keys: " + deviceInfo.keyboard.dwNumberOfFunctionKeys.ToString());
|
sl@27
|
167 |
Debug.WriteLine("Number of indicators: " + deviceInfo.keyboard.dwNumberOfIndicators.ToString());
|
sl@27
|
168 |
Debug.WriteLine("Number of keys total: " + deviceInfo.keyboard.dwNumberOfKeysTotal.ToString());
|
sl@27
|
169 |
}
|
sl@27
|
170 |
}
|
sl@27
|
171 |
finally
|
sl@27
|
172 |
{
|
sl@27
|
173 |
//Always executed when leaving our try block
|
sl@27
|
174 |
Marshal.FreeHGlobal(rawInputBuffer);
|
sl@27
|
175 |
Marshal.FreeHGlobal(preParsedData);
|
sl@27
|
176 |
}
|
sl@27
|
177 |
|
sl@27
|
178 |
IsValid = true;
|
sl@27
|
179 |
}
|
sl@27
|
180 |
|
sl@27
|
181 |
/// <summary>
|
sl@27
|
182 |
/// Print information about this device to our debug output.
|
sl@27
|
183 |
/// </summary>
|
sl@27
|
184 |
public void DebugWrite()
|
sl@27
|
185 |
{
|
sl@27
|
186 |
if (!IsValid)
|
sl@27
|
187 |
{
|
sl@27
|
188 |
Debug.WriteLine("==== Invalid HidEvent");
|
sl@27
|
189 |
return;
|
sl@27
|
190 |
}
|
sl@27
|
191 |
Device.DebugWrite();
|
sl@27
|
192 |
if (IsGeneric) Debug.WriteLine("==== Generic");
|
sl@27
|
193 |
if (IsKeyboard) Debug.WriteLine("==== Keyboard");
|
sl@27
|
194 |
if (IsMouse) Debug.WriteLine("==== Mouse");
|
sl@27
|
195 |
Debug.WriteLine("==== Foreground: " + IsForeground.ToString());
|
sl@27
|
196 |
Debug.WriteLine("==== UsagePage: 0x" + UsagePage.ToString("X4"));
|
sl@27
|
197 |
Debug.WriteLine("==== UsageCollection: 0x" + UsageCollection.ToString("X4"));
|
sl@27
|
198 |
foreach (ushort usage in Usages)
|
sl@27
|
199 |
{
|
sl@27
|
200 |
Debug.WriteLine("==== Usage: 0x" + usage.ToString("X4"));
|
sl@27
|
201 |
}
|
sl@27
|
202 |
}
|
sl@27
|
203 |
|
sl@27
|
204 |
|
sl@27
|
205 |
|
sl@27
|
206 |
|
sl@27
|
207 |
}
|
sl@27
|
208 |
|
sl@27
|
209 |
} |