-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchpadHelper.cs
482 lines (408 loc) · 11.8 KB
/
TouchpadHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace RawInput.Touchpad
{
internal static class TouchpadHelper
{
#region Win32
[DllImport("User32", SetLastError = true)]
private static extern uint GetRawInputDeviceList(
[Out] RAWINPUTDEVICELIST[] pRawInputDeviceList,
ref uint puiNumDevices,
uint cbSize);
[StructLayout(LayoutKind.Sequential)]
private struct RAWINPUTDEVICELIST
{
public IntPtr hDevice;
public uint dwType; // RIM_TYPEMOUSE or RIM_TYPEKEYBOARD or RIM_TYPEHID
}
private const uint RIM_TYPEMOUSE = 0;
private const uint RIM_TYPEKEYBOARD = 1;
private const uint RIM_TYPEHID = 2;
[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RegisterRawInputDevices(
RAWINPUTDEVICE[] pRawInputDevices,
uint uiNumDevices,
uint cbSize);
[StructLayout(LayoutKind.Sequential)]
private struct RAWINPUTDEVICE
{
public ushort usUsagePage;
public ushort usUsage;
public uint dwFlags; // RIDEV_INPUTSINK
public IntPtr hwndTarget;
}
private const uint RIDEV_INPUTSINK = 0x00000100;
[DllImport("User32.dll", SetLastError = true)]
private static extern uint GetRawInputData(
IntPtr hRawInput, // lParam in WM_INPUT
uint uiCommand, // RID_HEADER
IntPtr pData,
ref uint pcbSize,
uint cbSizeHeader);
private const uint RID_INPUT = 0x10000003;
[StructLayout(LayoutKind.Sequential)]
private struct RAWINPUT
{
public RAWINPUTHEADER Header;
public RAWHID Hid;
}
[StructLayout(LayoutKind.Sequential)]
private struct RAWINPUTHEADER
{
public uint dwType; // RIM_TYPEMOUSE or RIM_TYPEKEYBOARD or RIM_TYPEHID
public uint dwSize;
public IntPtr hDevice;
public IntPtr wParam; // wParam in WM_INPUT
}
[StructLayout(LayoutKind.Sequential)]
private struct RAWHID
{
public uint dwSizeHid;
public uint dwCount;
public IntPtr bRawData; // This is not for use.
}
[DllImport("User32.dll", SetLastError = true)]
private static extern uint GetRawInputDeviceInfo(
IntPtr hDevice, // hDevice by RAWINPUTHEADER
uint uiCommand, // RIDI_PREPARSEDDATA
IntPtr pData,
ref uint pcbSize);
[DllImport("User32.dll", SetLastError = true)]
private static extern uint GetRawInputDeviceInfo(
IntPtr hDevice, // hDevice by RAWINPUTDEVICELIST
uint uiCommand, // RIDI_DEVICEINFO
ref RID_DEVICE_INFO pData,
ref uint pcbSize);
private const uint RIDI_PREPARSEDDATA = 0x20000005;
private const uint RIDI_DEVICEINFO = 0x2000000b;
[StructLayout(LayoutKind.Sequential)]
private struct RID_DEVICE_INFO
{
public uint cbSize; // This is determined to accommodate RID_DEVICE_INFO_KEYBOARD.
public uint dwType;
public RID_DEVICE_INFO_HID hid;
}
[StructLayout(LayoutKind.Sequential)]
private struct RID_DEVICE_INFO_HID
{
public uint dwVendorId;
public uint dwProductId;
public uint dwVersionNumber;
public ushort usUsagePage;
public ushort usUsage;
}
[DllImport("Hid.dll", SetLastError = true)]
private static extern uint HidP_GetCaps(
IntPtr PreparsedData,
out HIDP_CAPS Capabilities);
private const uint HIDP_STATUS_SUCCESS = 0x00110000;
[StructLayout(LayoutKind.Sequential)]
private struct HIDP_CAPS
{
public ushort Usage;
public ushort UsagePage;
public ushort InputReportByteLength;
public ushort OutputReportByteLength;
public ushort FeatureReportByteLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
public ushort[] Reserved;
public ushort NumberLinkCollectionNodes;
public ushort NumberInputButtonCaps;
public ushort NumberInputValueCaps;
public ushort NumberInputDataIndices;
public ushort NumberOutputButtonCaps;
public ushort NumberOutputValueCaps;
public ushort NumberOutputDataIndices;
public ushort NumberFeatureButtonCaps;
public ushort NumberFeatureValueCaps;
public ushort NumberFeatureDataIndices;
}
[DllImport("Hid.dll", CharSet = CharSet.Auto)]
private static extern uint HidP_GetValueCaps(
HIDP_REPORT_TYPE ReportType,
[Out] HIDP_VALUE_CAPS[] ValueCaps,
ref ushort ValueCapsLength,
IntPtr PreparsedData);
private enum HIDP_REPORT_TYPE
{
HidP_Input,
HidP_Output,
HidP_Feature
}
[StructLayout(LayoutKind.Sequential)]
private struct HIDP_VALUE_CAPS
{
public ushort UsagePage;
public byte ReportID;
[MarshalAs(UnmanagedType.U1)]
public bool IsAlias;
public ushort BitField;
public ushort LinkCollection;
public ushort LinkUsage;
public ushort LinkUsagePage;
[MarshalAs(UnmanagedType.U1)]
public bool IsRange;
[MarshalAs(UnmanagedType.U1)]
public bool IsStringRange;
[MarshalAs(UnmanagedType.U1)]
public bool IsDesignatorRange;
[MarshalAs(UnmanagedType.U1)]
public bool IsAbsolute;
[MarshalAs(UnmanagedType.U1)]
public bool HasNull;
public byte Reserved;
public ushort BitSize;
public ushort ReportCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public ushort[] Reserved2;
public uint UnitsExp;
public uint Units;
public int LogicalMin;
public int LogicalMax;
public int PhysicalMin;
public int PhysicalMax;
// Range
public ushort UsageMin;
public ushort UsageMax;
public ushort StringMin;
public ushort StringMax;
public ushort DesignatorMin;
public ushort DesignatorMax;
public ushort DataIndexMin;
public ushort DataIndexMax;
// NotRange
public ushort Usage => UsageMin;
// ushort Reserved1;
public ushort StringIndex => StringMin;
// ushort Reserved2;
public ushort DesignatorIndex => DesignatorMin;
// ushort Reserved3;
public ushort DataIndex => DataIndexMin;
// ushort Reserved4;
}
[DllImport("Hid.dll", CharSet = CharSet.Auto)]
private static extern uint HidP_GetUsageValue(
HIDP_REPORT_TYPE ReportType,
ushort UsagePage,
ushort LinkCollection,
ushort Usage,
out uint UsageValue,
IntPtr PreparsedData,
IntPtr Report,
uint ReportLength);
#endregion
public static bool Exists()
{
uint deviceListCount = 0;
uint rawInputDeviceListSize = (uint)Marshal.SizeOf<RAWINPUTDEVICELIST>();
if (GetRawInputDeviceList(
null,
ref deviceListCount,
rawInputDeviceListSize) != 0)
{
return false;
}
var devices = new RAWINPUTDEVICELIST[deviceListCount];
if (GetRawInputDeviceList(
devices,
ref deviceListCount,
rawInputDeviceListSize) != deviceListCount)
{
return false;
}
foreach (var device in devices.Where(x => x.dwType == RIM_TYPEHID))
{
uint deviceInfoSize = 0;
if (GetRawInputDeviceInfo(
device.hDevice,
RIDI_DEVICEINFO,
IntPtr.Zero,
ref deviceInfoSize) != 0)
{
continue;
}
var deviceInfo = new RID_DEVICE_INFO { cbSize = deviceInfoSize };
if (GetRawInputDeviceInfo(
device.hDevice,
RIDI_DEVICEINFO,
ref deviceInfo,
ref deviceInfoSize) == unchecked((uint)-1))
{
continue;
}
if ((deviceInfo.hid.usUsagePage == 0x000D) &&
(deviceInfo.hid.usUsage == 0x0005))
{
return true;
}
}
return false;
}
public static bool RegisterInput(IntPtr windowHandle)
{
// Precision Touchpad (PTP) in HID Clients Supported in Windows
// https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/hid-architecture#hid-clients-supported-in-windows
var device = new RAWINPUTDEVICE
{
usUsagePage = 0x000D,
usUsage = 0x0005,
dwFlags = 0, // WM_INPUT messages come only when the window is in the foreground.
hwndTarget = windowHandle
};
return RegisterRawInputDevices(new[] { device }, 1, (uint)Marshal.SizeOf<RAWINPUTDEVICE>());
}
public const int WM_INPUT = 0x00FF;
public const int RIM_INPUT = 0;
public const int RIM_INPUTSINK = 1;
public static TouchpadContact[] ParseInput(IntPtr lParam)
{
// Get RAWINPUT.
uint rawInputSize = 0;
uint rawInputHeaderSize = (uint)Marshal.SizeOf<RAWINPUTHEADER>();
if (GetRawInputData(
lParam,
RID_INPUT,
IntPtr.Zero,
ref rawInputSize,
rawInputHeaderSize) != 0)
{
return null;
}
RAWINPUT rawInput;
byte[] rawHidRawData;
IntPtr rawInputPointer = IntPtr.Zero;
try
{
rawInputPointer = Marshal.AllocHGlobal((int)rawInputSize);
if (GetRawInputData(
lParam,
RID_INPUT,
rawInputPointer,
ref rawInputSize,
rawInputHeaderSize) != rawInputSize)
{
return null;
}
rawInput = Marshal.PtrToStructure<RAWINPUT>(rawInputPointer);
var rawInputData = new byte[rawInputSize];
Marshal.Copy(rawInputPointer, rawInputData, 0, rawInputData.Length);
rawHidRawData = new byte[rawInput.Hid.dwSizeHid * rawInput.Hid.dwCount];
int rawInputOffset = (int)rawInputSize - rawHidRawData.Length;
Buffer.BlockCopy(rawInputData, rawInputOffset, rawHidRawData, 0, rawHidRawData.Length);
}
finally
{
Marshal.FreeHGlobal(rawInputPointer);
}
// Parse RAWINPUT.
IntPtr rawHidRawDataPointer = Marshal.AllocHGlobal(rawHidRawData.Length);
Marshal.Copy(rawHidRawData, 0, rawHidRawDataPointer, rawHidRawData.Length);
IntPtr preparsedDataPointer = IntPtr.Zero;
try
{
uint preparsedDataSize = 0;
if (GetRawInputDeviceInfo(
rawInput.Header.hDevice,
RIDI_PREPARSEDDATA,
IntPtr.Zero,
ref preparsedDataSize) != 0)
{
return null;
}
preparsedDataPointer = Marshal.AllocHGlobal((int)preparsedDataSize);
if (GetRawInputDeviceInfo(
rawInput.Header.hDevice,
RIDI_PREPARSEDDATA,
preparsedDataPointer,
ref preparsedDataSize) != preparsedDataSize)
{
return null;
}
if (HidP_GetCaps(
preparsedDataPointer,
out HIDP_CAPS caps) != HIDP_STATUS_SUCCESS)
{
return null;
}
ushort valueCapsLength = caps.NumberInputValueCaps;
var valueCaps = new HIDP_VALUE_CAPS[valueCapsLength];
if (HidP_GetValueCaps(
HIDP_REPORT_TYPE.HidP_Input,
valueCaps,
ref valueCapsLength,
preparsedDataPointer) != HIDP_STATUS_SUCCESS)
{
return null;
}
uint scanTime = 0;
uint contactCount = 0;
TouchpadContactCreator creator = new();
List<TouchpadContact> contacts = new();
foreach (var valueCap in valueCaps.OrderBy(x => x.LinkCollection))
{
if (HidP_GetUsageValue(
HIDP_REPORT_TYPE.HidP_Input,
valueCap.UsagePage,
valueCap.LinkCollection,
valueCap.Usage,
out uint value,
preparsedDataPointer,
rawHidRawDataPointer,
(uint)rawHidRawData.Length) != HIDP_STATUS_SUCCESS)
{
continue;
}
// Usage Page and ID in Windows Precision Touchpad input reports
// https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-precision-touchpad-required-hid-top-level-collections#windows-precision-touchpad-input-reports
switch (valueCap.LinkCollection)
{
case 0:
switch (valueCap.UsagePage, valueCap.Usage)
{
case (0x0D, 0x56): // Scan Time
scanTime = value;
break;
case (0x0D, 0x54): // Contact Count
contactCount = value;
break;
}
break;
default:
switch (valueCap.UsagePage, valueCap.Usage)
{
case (0x0D, 0x51): // Contact ID
creator.ContactId = (int)value;
break;
case (0x01, 0x30): // X
creator.X = (int)value;
break;
case (0x01, 0x31): // Y
creator.Y = (int)value;
break;
}
break;
}
if (creator.TryCreate(out TouchpadContact contact))
{
contacts.Add(contact);
if (contacts.Count >= contactCount)
break;
creator.Clear();
}
}
return contacts.ToArray();
}
finally
{
Marshal.FreeHGlobal(rawHidRawDataPointer);
Marshal.FreeHGlobal(preparsedDataPointer);
}
}
}
}