-
Notifications
You must be signed in to change notification settings - Fork 2
/
RawInputManager.pas
785 lines (687 loc) · 28.3 KB
/
RawInputManager.pas
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
RawInput managing library
RawInput manager class
©František Milt 2018-10-22
Version 0.9.3
Dependencies:
AuxTypes - github.com/ncs-sniper/Lib.AuxTypes
AuxClasses - github.com/ncs-sniper/Lib.AuxClasses
BitOps - github.com/ncs-sniper/Lib.BitOps
MulticastEvent - github.com/ncs-sniper/Lib.MulticastEvent
WndAlloc - github.com/ncs-sniper/Lib.WndAlloc
WinRawInput - github.com/ncs-sniper/Lib.WinRawInput
BitVector - github.com/ncs-sniper/Lib.BitVector
UtilityWindow - github.com/ncs-sniper/Lib.UtilityWindow
DefRegistry - github.com/ncs-sniper/Lib.DefRegistry
StrRect - github.com/ncs-sniper/Lib.StrRect
* SimpleCPUID - github.com/ncs-sniper/Lib.SimpleCPUID
SimpleCPUID might not be needed, see BitOps library for details.
===============================================================================}
unit RawInputManager;
{$INCLUDE 'RawInput_defs.inc'}
interface
uses
Windows, Messages, Classes,
WinRawInput, UtilityWindow, AuxClasses,
RawInputCommon, RawInputKeyboard;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputManager }
{------------------------------------------------------------------------------}
{==============================================================================}
type
TRawInputManagerSettings = set of (
rmoFlagNoLegacyMessages,
rmoFlagNoHotKeys,
rmoFlagAppKeys,
rmoFlagCaptureMouse,
rmoFlagInputSink,
rmoRegisterForKeyboard,
rmoRegisterForMouse,
rmoAdditionalDeviceInfo,
rmoPerDeviceProcessing,
rmoChannelEventsToMaster);
{==============================================================================}
{ TRawInputManager - declaration }
{==============================================================================}
TRawInputManager = class(TCustomObject)
private
fTargetHWND: HWND;
fActive: Boolean;
fSettings: TRawInputManagerSettings;
fRawInputBufferSize: UINT;
fRawInputBuffer: Pointer;
fWoW64Padding: Boolean;
fDevices: TDeviceList;
fRegisteredDevices: TRegisteredDevicesList;
fKeyboard: TRawInputKeyboardMaster;
//fMouse: TRawInputMouseMaster;
//fHID TRawInputHIDMaster;
fOnDevicesListChange: TNotifyEvent;
fOnRegisteredDevicesListChange: TNotifyEvent;
fOnRawInput: TRawInputEvent;
Function GetDeviceCount: Integer;
Function GetRegisteredDeviceCount: Integer;
Function GetDevice(Index: Integer): TDeviceListItem;
Function GetRegisteredDevice(Index: Integer): TRawInputDevice;
protected
procedure RegisterDevice(UsagePage,Usage: USHORT; Flags: DWORD); overload; virtual;
procedure RegisterDevice(UsagePage,Usage: USHORT); overload; virtual;
procedure UnregisterDevice(UsagePage,Usage: USHORT); virtual;
procedure ProcessDevices; virtual;
procedure RegisterRawInput; virtual;
procedure UnregisterRawInput; virtual;
procedure InitializeForWoW64; virtual;
procedure InitializeObjects; virtual;
procedure FinalizeObjects; virtual;
procedure ProcessRawInput(RawInput: PRawInput); overload; virtual;
procedure ProcessRawInput(lParam: lParam; wParam: wParam); overload; virtual;
procedure ProcessRawInputDeviceChange(lParam: lParam; wParam: wParam); virtual;
procedure UnknownDeviceIntercepted(Sender: TObject; DeviceHandle: THandle); virtual;
procedure GetDeviceAdditionalInfo(const DeviceName: String; var AdditionalInfo: TAdditionalInfo); virtual;
procedure FillDeviceInfo(DeviceHandle: THandle; var DeviceInfo: TDeviceListItem); virtual;
public
constructor Create(Settings: TRawInputManagerSettings; TargetHWND: HWND = 0);
destructor Destroy; override;
Function ProcessBufferedInput: Integer; virtual;
procedure EnumerateDevices; virtual;
procedure EnumerateRegisteredDevices; virtual;
Function IndexOfDevice(DeviceHandle: THandle): Integer; overload; virtual;
Function IndexOfDevice(DeviceName: String): Integer; overload; virtual;
Function RegisterHIDDevice(UsagePage, Usage: USHORT; Flags: DWORD = 0): Boolean; overload; virtual;
Function RegisterHIDDevice(Index: Integer; Flags: DWORD = 0): Boolean; overload; virtual;
procedure UnregisterHIDDevice(UsagePage, Usage: USHORT); overload; virtual;
procedure UnregisterHIDDevice(Index: Integer); overload; virtual;
property Devices[Index: Integer]: TDeviceListItem read GetDevice;
property RegisteredDevices[Index: Integer]: TRawInputDevice read GetRegisteredDevice;
property TargetHWND: HWND read fTargetHWND;
property Active: Boolean read fActive write fActive;
property Settings: TRawInputManagerSettings read fSettings;
property DeviceCount: Integer read GetDeviceCount;
property RegisteredDeviceCount: Integer read GetRegisteredDeviceCount;
property Keyboard: TRawInputKeyboardMaster read fKeyboard;
//property Mouse: TRawInputMouseMaster read fMouse;
//property HID: TRawInputHIDMaster read fHID;
property OnDevicesListChange: TNotifyEvent read fOnDevicesListChange write fOnDevicesListChange;
property OnRegisteredDevicesListChange: TNotifyEvent read fOnRegisteredDevicesListChange write fOnRegisteredDevicesListChange;
property OnRawInput: TRawInputEvent read fOnRawInput write fOnRawInput;
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputManagerEx }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputManagerEx - declaration }
{==============================================================================}
TRawInputManagerEx = class(TRawInputManager)
private
fOwnsUtilityWindow: Boolean;
fUtilityWindow: TUtilityWindow;
protected
procedure MessagesHandler(var Msg: TMessage; var Handled: Boolean); virtual;
public
constructor Create(Settings: TRawInputManagerSettings; UtilityWindow: TUtilityWindow = nil);
destructor Destroy; override;
property UtilityWindow: TUtilityWindow read fUtilityWindow;
end;
implementation
uses
SysUtils, StrUtils, DefRegistry, StrRect;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputManager }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputManager - implementation }
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputManager - private methods }
{------------------------------------------------------------------------------}
Function TRawInputManager.GetDeviceCount: Integer;
begin
Result := Length(fDevices);
end;
//------------------------------------------------------------------------------
Function TRawInputManager.GetRegisteredDeviceCount: Integer;
begin
Result := Length(fRegisteredDevices);
end;
//------------------------------------------------------------------------------
Function TRawInputManager.GetDevice(Index: Integer): TDeviceListItem;
begin
If (Index >= Low(fDevices)) and (Index <= High(fDevices)) then
Result := fDevices[Index]
else
raise Exception.CreateFmt('TRawInputManager.GetDevice: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TRawInputManager.GetRegisteredDevice(Index: Integer): TRawInputDevice;
begin
If (Index >= Low(fRegisteredDevices)) and (Index <= High(fRegisteredDevices)) then
Result := fRegisteredDevices[Index]
else
raise Exception.CreateFmt('TRawInputManager.GetRegisteredDevice: Index (%d) out of bounds.',[Index]);
end;
{------------------------------------------------------------------------------}
{ TRawInputManager - protected methods }
{------------------------------------------------------------------------------}
procedure TRawInputManager.RegisterDevice(UsagePage,Usage: USHORT; Flags: DWORD);
var
RawInputDevice: PRawInputDevice;
begin
New(RawInputDevice);
try
RawInputDevice^.usUsagePage := UsagePage;
RawInputDevice^.usUsage := Usage;
RawInputDevice^.dwFlags := Flags;
RawInputDevice^.hwndTarget := TargetHWND;
If not RegisterRawInputDevices(RawInputDevice,1,SizeOf(TRawInputDevice)) then
raise Exception.CreateFmt('TRawInputManager.RegisterDevice: Raw input registration failed (0x%.8x).',[GetLastError]);
finally
Dispose(RawInputDevice);
end;
end;
// --- --- --- --- --- --- --- --- --- --- --- --- ---
procedure TRawInputManager.RegisterDevice(UsagePage,Usage: USHORT);
var
Flags: DWORD;
begin
If Win32MajorVersion >= 6 {Vista+} then
Flags := RIDEV_DEVNOTIFY
else
Flags := 0;
If rmoFlagNoLegacyMessages in fSettings then
Flags := Flags or RIDEV_NOLEGACY;
If (rmoFlagNoHotKeys in fSettings) and (UsagePage = $01) and (Usage = $06) then
Flags := Flags or RIDEV_NOHOTKEYS;
If rmoFlagInputSink in fSettings then
Flags := Flags or RIDEV_INPUTSINK;
If (rmoFlagAppKeys in fSettings) and (UsagePage = $01) and (Usage = $06) then
Flags := Flags or RIDEV_APPKEYS;
If (rmoFlagCaptureMouse in fSettings) and (rmoFlagNoLegacyMessages in fSettings) and (UsagePage = $01) and (Usage = $02) then
Flags := Flags or RIDEV_CAPTUREMOUSE;
RegisterDevice(UsagePage,Usage,Flags);
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.UnregisterDevice(UsagePage,Usage: USHORT);
var
RawInputDevice: PRawInputDevice;
begin
New(RawInputDevice);
try
RawInputDevice^.usUsagePage := UsagePage;
RawInputDevice^.usUsage := Usage;
RawInputDevice^.dwFlags := RIDEV_REMOVE;
RawInputDevice^.hwndTarget := 0;
If not RegisterRawInputDevices(RawInputDevice,1,SizeOf(TRawInputDevice)) then
raise Exception.CreateFmt('TRawInputManager.UnregisterRawInput.UnregisterDevice: Raw input unregistration failed (0x%.8x).',[GetLastError]);
finally
Dispose(RawInputDevice);
end;
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.ProcessDevices;
var
i: Integer;
begin
For i := Low(fDevices) to High(fDevices) do
case fDevices[i].Info.dwType of
RIM_TYPEMOUSE:;
RIM_TYPEKEYBOARD:
If fKeyboard.IndexOfDevice(fDevices[i].Handle) < 0 then
TRawInputKeyboardMasterInternal(fKeyboard).AddDevice(Addr(fDevices[i]),rmoChannelEventsToMaster in fSettings);
RIM_TYPEHID:;
end;
For i := Pred(fKeyboard.DeviceCount) downto 0 do
If IndexOfDevice(fKeyboard[i].DeviceInfo.Handle) < 0 then
TRawInputKeyboardMasterInternal(fKeyboard).DeleteDevice(i);
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.RegisterRawInput;
begin
If rmoRegisterForKeyboard in fSettings then
RegisterDevice($01,$06);
If rmoRegisterForMouse in fSettings then
RegisterDevice($01,$02);
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.UnregisterRawInput;
var
i: Integer;
begin
For i := 0 to Pred(RegisteredDeviceCount) do
UnregisterDevice(RegisteredDevices[i].usUsagePage,RegisteredDevices[i].usUsage);
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.InitializeForWoW64;
type
TIsWoW64Process = Function(hProcess: THandle; Wow64Process: PBOOL): BOOL; stdcall;
var
ModuleHandle: THandle;
IsWow64Process: TIsWoW64Process;
ResultValue: BOOL;
begin
fWoW64Padding := False;
ModuleHandle := GetModuleHandle('kernel32.dll');
If ModuleHandle <> 0 then
begin
IsWoW64Process := GetProcAddress(ModuleHandle,'IsWow64Process');
If Assigned(IsWoW64Process) then
If IsWoW64Process(GetCurrentProcess,@ResultValue) then
fWoW64Padding := ResultValue;
end
else raise Exception.CreateFmt('TRawInputManager.InitializeForWoW64: Unable to get handle to module kernel32.dll (%.8x).',[GetLastError]);
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.InitializeObjects;
begin
fKeyboard := TRawInputKeyboardMasterInternal.Create;
If rmoPerDeviceProcessing in fSettings then
TRawInputKeyboardMasterInternal(fKeyboard).OnUnknownDeviceIntercepted := UnknownDeviceIntercepted;
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.FinalizeObjects;
begin
fKeyboard.Free;
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.ProcessRawInput(RawInput: PRawInput);
begin
If Assigned(fOnRawInput) then fOnRawInput(Self,RawInput,RawInput^.header.dwSize);
case RawInput^.header.dwType of
RIM_TYPEMOUSE:;
RIM_TYPEKEYBOARD: fKeyboard.ProcessRawInput(RawInput);
RIM_TYPEHID:;
else
DefRawInputProc(@RawInput,1,SizeOf(RAWINPUTHEADER));
end;
end;
// --- --- --- --- --- --- --- --- --- --- --- --- ---
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TRawInputManager.ProcessRawInput(lParam: lParam; wParam: wParam);
var
RawInputSize: UINT;
begin
If GetRawInputData(HRAWINPUT(lParam),RID_INPUT,nil,@RawInputSize,SizeOf(TRawInputHeader)) <> UINT(-1) then
If RawInputSize > 0 then
begin
If RawInputSize > fRawInputBufferSize then
begin
fRawInputBufferSize := RawInputSize;
ReallocMem(fRawInputBuffer,RawInputSize)
end;
If GetRawInputData(HRAWINPUT(lParam),RID_INPUT,fRawInputBuffer,@RawInputSize,SizeOf(TRawInputHeader)) = RawInputSize then
ProcessRawInput(fRawInputBuffer);
end;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TRawInputManager.ProcessRawInputDeviceChange(lParam: lParam; wParam: wParam);
var
Index, i: Integer;
begin
case wParam of
GIDC_ARRIVAL:
If IndexOfDevice(THandle(lParam)) < 0 then
begin
SetLength(fDevices,Length(fDevices) + 1);
FillDeviceInfo(THandle(lParam),fDevices[High(fDevices)]);
ProcessDevices;
If Assigned(fOnDevicesListChange) then fOnDevicesListChange(Self);
end;
GIDC_REMOVAL:
begin
Index := IndexOfDevice(THandle(lParam));
If Index >= 0 then
begin
For i := Index to Pred(High(fDevices)) do
fDevices[i] := fDevices[i + 1];
SetLength(fDevices,Length(fDevices) - 1);
ProcessDevices;
If Assigned(fOnDevicesListChange) then fOnDevicesListChange(Self);
end;
end;
end;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TRawInputManager.UnknownDeviceIntercepted(Sender: TObject; DeviceHandle: THandle);
begin
If IndexOfDevice(DeviceHandle) < 0 then
begin
SetLength(fDevices,Length(fDevices) + 1);
FillDeviceInfo(DeviceHandle,fDevices[High(fDevices)]);
ProcessDevices;
If Assigned(fOnDevicesListChange) then fOnDevicesListChange(Self);
end;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TRawInputManager.GetDeviceAdditionalInfo(const DeviceName: String; var AdditionalInfo: TAdditionalInfo);
var
BeginPos: Integer;
EndPos: Integer;
Reg: TDefRegistry;
Function GetLastPosition(const Str: String; Char: Char): Integer;
var
i: Integer;
begin
Result := 0;
For i := Length(Str) downto 1 do
If Str[i] = Char then
begin
Result := i;
Break;
end;
end;
Function RectifyStr(const Str: String): String;
begin
If AnsiContainsText(Str,';') then
Result := Copy(Str,Succ(GetLastPosition(Str,';')),Length(Str))
else
Result := Str;
end;
begin
BeginPos := GetLastPosition(DeviceName,'\');
EndPos := GetLastPosition(DeviceName,'#');
If (BeginPos > 0) and (EndPos > 0) and (BeginPos < EndPos) then
begin
Reg := TDefRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
If Reg.OpenKeyReadOnly('System\CurrentControlSet\Enum\' + AnsiReplaceText(Copy(DeviceName,Succ(BeginPos),EndPos - BeginPos),'#','\')) then
begin
AdditionalInfo.Description := RectifyStr(Reg.ReadStringDef('DeviceDesc',''));
AdditionalInfo.Manufacturer := RectifyStr(Reg.ReadStringDef('Mfg',''));
AdditionalInfo.DeviceClass := Reg.ReadStringDef('Class','');
AdditionalInfo.ClassGUID := StringToGUID(Reg.ReadStringDef('ClassGUID','{00000000-0000-0000-0000-000000000000}'));
AdditionalInfo.Description := WinToStr(AdditionalInfo.Description);
AdditionalInfo.Manufacturer := WinToStr(AdditionalInfo.Manufacturer);
AdditionalInfo.DeviceClass := WinToStr(AdditionalInfo.DeviceClass);
Reg.CloseKey;
end;
finally
Reg.Free;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.FillDeviceInfo(DeviceHandle: THandle; var DeviceInfo: TDeviceListItem);
var
BufferSize: UINT;
begin
DeviceInfo.Handle := DeviceHandle;
GetRawInputDeviceInfo(DeviceHandle,RIDI_DEVICENAME,nil,@BufferSize);
SetLength(DeviceInfo.Name,BufferSize);
GetRawInputDeviceInfo(DeviceHandle,RIDI_DEVICENAME,PChar(DeviceInfo.Name),@BufferSize);
SetLength(DeviceInfo.Name,StrLen(PChar(DeviceInfo.Name)));
DeviceInfo.Name := WinToStr(DeviceInfo.Name);
BufferSize := SizeOf(RID_DEVICE_INFO);
DeviceInfo.Info.cbSize := SizeOf(RID_DEVICE_INFO);
GetRawInputDeviceInfo(DeviceHandle,RIDI_DEVICEINFO,Addr(DeviceInfo.Info),@BufferSize);
GetRawInputDeviceInfo(DeviceHandle,RIDI_PREPARSEDDATA,nil,@BufferSize);
SetLength(DeviceInfo.PreparsedData,BufferSize);
If Length(DeviceInfo.PreparsedData) > 0 then
GetRawInputDeviceInfo(DeviceHandle,RIDI_PREPARSEDDATA,Addr(DeviceInfo.PreparsedData[0]),@BufferSize);
If rmoAdditionalDeviceInfo in fSettings then
GetDeviceAdditionalInfo(DeviceInfo.Name,DeviceInfo.AdditionalInfo);
end;
{------------------------------------------------------------------------------}
{ TRawInputManager - public methods }
{------------------------------------------------------------------------------}
constructor TRawInputManager.Create(Settings: TRawInputManagerSettings; TargetHWND: HWND = 0);
begin
inherited Create;
fTargetHWND := TargetHWND;
fActive := False;
fSettings := Settings;
fRawInputBufferSize := SizeOf(TRawInput) * 128;
fRawInputBuffer := AllocMem(fRawInputBufferSize);
InitializeForWoW64;
InitializeObjects;
EnumerateDevices;
RegisterRawInput;
EnumerateRegisteredDevices;
end;
//------------------------------------------------------------------------------
destructor TRawInputManager.Destroy;
begin
UnregisterRawInput;
FinalizeObjects;
FreeMem(fRawInputBuffer,fRawInputBufferSize);
inherited;
end;
//------------------------------------------------------------------------------
Function TRawInputManager.ProcessBufferedInput: Integer;
var
BufferSize: UINT;
Count: Integer;
CurrentBuffer: Pointer;
i: Integer;
begin
Result := 0;
repeat
BufferSize := 0;
If GetRawInputBuffer(nil,@BufferSize,SizeOf(RAWINPUTHEADER)) = 0 then
If BufferSize > 0 then
begin
If BufferSize >= fRawInputBufferSize then
begin
fRawInputBufferSize := BufferSize * 2;
ReallocMem(fRawInputBuffer,fRawInputBufferSize);
end;
Count := Integer(GetRawInputBuffer(fRawInputBuffer,@fRawInputBufferSize,SizeOf(RAWINPUTHEADER)));
If Count > 0 then
begin
Inc(Result,Count);
CurrentBuffer := fRawInputBuffer;
For i := 1 to Count do
begin
{$IFDEF CPU32bit}
If fWoW64Padding then
WoW64Conversion(CurrentBuffer);
{$ENDIF}
ProcessRawInput(PRAWINPUT(CurrentBuffer));
CurrentBuffer := NEXTRAWINPUTBLOCK(CurrentBuffer);
end;
end;
end;
until BufferSize <= 0;
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.EnumerateDevices;
var
DevicesCount: UINT;
Devices: PRAWINPUTDEVICELIST;
DevicePtr: PRAWINPUTDEVICELIST;
i: Integer;
begin
If GetRawInputDeviceList(nil,@DevicesCount,SizeOf(RAWINPUTDEVICELIST)) <> UINT(-1) then
begin
Devices := AllocMem(DevicesCount * SizeOf(RAWINPUTDEVICELIST));
try
If GetRawInputDeviceList(Devices,@DevicesCount,SizeOf(RAWINPUTDEVICELIST)) = DevicesCount then
begin
SetLength(fDevices,DevicesCount);
DevicePtr := Devices;
For i := Low(fDevices) to High(fDevices) do
begin
FillDeviceInfo(DevicePtr^.hDevice,fDevices[i]);
Inc(DevicePtr);
end;
If Assigned(fOnDevicesListChange) then fOnDevicesListChange(Self);
end
else raise Exception.CreateFmt('TRawInputManager.EnumerateDevices: Cannot enumerate devices (0x%.8x).',[GetLastError]);
finally
FreeMem(Devices,DevicesCount * SizeOf(RAWINPUTDEVICELIST));
end;
end
else raise Exception.CreateFmt('TRawInputManager.EnumerateDevices: Cannot get number of devices (0x%.8x).',[GetLastError]);
If rmoPerDeviceProcessing in fSettings then ProcessDevices;
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.EnumerateRegisteredDevices;
var
DevicesCount: UINT;
DevicesBuffer: PRAWINPUTDEVICE;
DevicePtr: PRAWINPUTDEVICE;
i: Integer;
begin
GetRegisteredRawInputDevices(nil,@DevicesCount,SizeOf(RAWINPUTDEVICE));
DevicesBuffer := AllocMem(DevicesCount * SizeOf(RAWINPUTDEVICE));
try
If GetRegisteredRawInputDevices(DevicesBuffer,@DevicesCount,SizeOf(RAWINPUTDEVICE)) = DevicesCount then
begin
SetLength(fRegisteredDevices,DevicesCount);
DevicePtr := DevicesBuffer;
For i := Low(fRegisteredDevices) to High(fRegisteredDevices) do
begin
fRegisteredDevices[i] := DevicePtr^;
Inc(DevicePtr);
end;
If Assigned(fOnRegisteredDevicesListChange) then fOnRegisteredDevicesListChange(Self);
end
else raise Exception.CreateFmt('TRawInputManager.EnumerateRegisteredDevices: Cannot enumerate registered devices (0x%.8x).',[GetLastError]);
finally
FreeMem(DevicesBuffer,DevicesCount * SizeOf(RAWINPUTDEVICE));
end;
end;
//------------------------------------------------------------------------------
Function TRawInputManager.IndexOfDevice(DeviceHandle: THandle): Integer;
var
i: Integer;
begin
Result := -1;
For i := Low(fDevices) to High(fDevices) do
If fDevices[i].Handle = DeviceHandle then
begin
Result := i;
Break;
end;
end;
//------------------------------------------------------------------------------
Function TRawInputManager.IndexOfDevice(DeviceName: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := Low(fDevices) to High(fDevices) do
If AnsiSameText(fDevices[i].Name,DeviceName) then
begin
Result := i;
Break;
end;
end;
//------------------------------------------------------------------------------
Function TRawInputManager.RegisterHIDDevice(UsagePage, Usage: USHORT; Flags: DWORD = 0): Boolean;
begin
If (UsagePage <> 0) and (Usage <> 0) then
begin
RegisterDevice(UsagePage,Usage,Flags);
EnumerateRegisteredDevices;
Result := True;
end
else Result := False;
end;
// --- --- --- --- --- --- --- --- --- --- --- --- ---
Function TRawInputManager.RegisterHIDDevice(Index: Integer; Flags: DWORD = 0): Boolean;
begin
Result := False;
If (Index >= 0) and (Index < DeviceCount) then
begin
If Devices[Index].Info.dwType = RIM_TYPEHID then
Result := RegisterHIDDevice(Devices[Index].Info.hid.usUsagePage,Devices[Index].Info.hid.usUsage,Flags);
end
else raise Exception.CreateFmt('TRawInputManager.RegisterHIDDevice: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
procedure TRawInputManager.UnregisterHIDDevice(UsagePage, Usage: USHORT);
begin
UnregisterDevice(UsagePage,Usage);
EnumerateRegisteredDevices;
end;
// --- --- --- --- --- --- --- --- --- --- --- --- ---
procedure TRawInputManager.UnregisterHIDDevice(Index: Integer);
begin
If (Index >= 0) and (Index < RegisteredDeviceCount) then
UnregisterHIDDevice(RegisteredDevices[Index].usUsagePage,RegisteredDevices[Index].usUsage)
else
raise Exception.CreateFmt('TRawInputManager.UnregisterHIDDevice: Index (%d) out of bounds.',[Index]);
end;
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputManagerEx }
{------------------------------------------------------------------------------}
{==============================================================================}
{==============================================================================}
{ TRawInputManagerEx - implementation }
{==============================================================================}
{------------------------------------------------------------------------------}
{ TRawInputManagerEx - protected methods }
{------------------------------------------------------------------------------}
procedure TRawInputManagerEx.MessagesHandler(var Msg: TMessage; var Handled: Boolean);
begin
If Active then
begin
case Msg.Msg of
WM_INPUT:
begin
ProcessRawInput(Msg.LParam,Msg.WParam);
Msg.Result := 0;
Handled := True;
end;
WM_INPUT_DEVICE_CHANGE:
begin
ProcessRawInputDeviceChange(Msg.LParam,Msg.WParam);
Msg.Result := 0;
Handled := True;
end;
else
Handled := False;
end;
end
else Handled := False;
end;
{------------------------------------------------------------------------------}
{ TRawInputManagerEx - public methods }
{------------------------------------------------------------------------------}
constructor TRawInputManagerEx.Create(Settings: TRawInputManagerSettings; UtilityWindow: TUtilityWindow = nil);
var
TempObject: TUtilityWindow;
begin
If Assigned(UtilityWindow) then TempObject := UtilityWindow
else TempObject := TUtilityWindow.Create;
try
inherited Create(Settings,TempObject.WindowHandle);
except
If not Assigned(UtilityWindow) then
TempObject.Free;
raise;
end;
fOwnsUtilityWindow := not Assigned(UtilityWindow);
fUtilityWindow := TempObject;
fUtilityWindow.OnMessage.Add(MessagesHandler);
end;
//------------------------------------------------------------------------------
destructor TRawInputManagerEx.Destroy;
begin
If Assigned(fUtilityWindow) then
begin
fUtilityWindow.OnMessage.Remove(MessagesHandler);
If fOwnsUtilityWindow then
fUtilityWindow.Free;
end;
inherited;
end;
end.