forked from pik33/ultibo-retro-sid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.pas.old
2407 lines (1993 loc) · 69.6 KB
/
mouse.pas.old
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Ultibo Mouse interface unit.
Copyright (C) 2015 - SoftOz Pty Ltd.
Arch
====
<All>
Boards
======
<All>
Licence
=======
LGPLv2.1 with static linking exception (See COPYING.modifiedLGPL.txt)
Credits
=======
Information for this unit was obtained from:
References
==========
USB HID Device Class Definition 1_11.pdf
http://www.usb.org/developers/hidpage/HID1_11.pdf
USB HID Usage Tables 1_12v2.pdf
http://www.usb.org/developers/hidpage/Hut1_12v2.pdf
Mouse Devices
=============
This unit provides both the Mouse device interface and the generic USB HID mouse driver.
USB Mouse Devices
=================
}
{$mode delphi} {Default to Delphi compatible syntax}
{$H+} {Default to AnsiString}
{$inline on} {Allow use of Inline procedures}
unit Mouse;
interface
uses GlobalConfig,GlobalConst,GlobalTypes,Platform,Threads,Devices,USB,SysUtils;
{==============================================================================}
{Global definitions}
{$INCLUDE GlobalDefines.inc}
{==============================================================================}
const
{Mouse specific constants}
MOUSE_NAME_PREFIX = 'Mouse'; {Name prefix for Mouse Devices}
{Mouse Device Types}
MOUSE_TYPE_NONE = 0;
MOUSE_TYPE_USB = 1;
MOUSE_TYPE_PS2 = 2;
MOUSE_TYPE_SERIAL = 3;
MOUSE_TYPE_MAX = 3;
{Mouse Type Names}
MOUSE_TYPE_NAMES:array[MOUSE_TYPE_NONE..MOUSE_TYPE_MAX] of String = (
'MOUSE_TYPE_NONE',
'MOUSE_TYPE_USB',
'MOUSE_TYPE_PS2',
'MOUSE_TYPE_SERIAL');
{Mouse Device States}
MOUSE_STATE_DETACHED = 0;
MOUSE_STATE_DETACHING = 1;
MOUSE_STATE_ATTACHING = 2;
MOUSE_STATE_ATTACHED = 3;
MOUSE_STATE_MAX = 3;
{Mouse State Names}
MOUSE_STATE_NAMES:array[MOUSE_STATE_DETACHED..MOUSE_STATE_MAX] of String = (
'MOUSE_STATE_DETACHED',
'MOUSE_STATE_DETACHING',
'MOUSE_STATE_ATTACHING',
'MOUSE_STATE_ATTACHED');
{Mouse Device Flags}
MOUSE_FLAG_NONE = $00000000;
MOUSE_FLAG_NON_BLOCK = $00000001; {If set device reads are non blocking (Also supported in Flags parameter of MouseReadEx)}
MOUSE_FLAG_DIRECT_READ = $00000002; {If set device writes mouse data to its local buffer and which must be read using MouseDeviceRead}
MOUSE_FLAG_SWAP_BUTTONS = $00000004; {If set left and right mouse buttons will be swapped in mouse data}
MOUSE_FLAG_PEEK_BUFFER = $00000008; {Peek at the buffer to see if any data is available, don't remove it (Used only in Flags parameter of MouseReadEx)}
{Flags supported by MOUSE_CONTROL_GET/SET/CLEAR_FLAG}
MOUSE_FLAG_MASK = MOUSE_FLAG_NON_BLOCK or MOUSE_FLAG_DIRECT_READ or MOUSE_FLAG_SWAP_BUTTONS;
{Mouse Device Control Codes}
MOUSE_CONTROL_GET_FLAG = 1; {Get Flag}
MOUSE_CONTROL_SET_FLAG = 2; {Set Flag}
MOUSE_CONTROL_CLEAR_FLAG = 3; {Clear Flag}
MOUSE_CONTROL_FLUSH_BUFFER = 4; {Flush Buffer}
//To Do //Acceleration etc
{Mouse Buffer Size}
MOUSE_BUFFER_SIZE = 512;
{Mouse Data Definitions (Values for TMouseData.Buttons)}
MOUSE_LEFT_BUTTON = $0001; {The Left mouse button is pressed}
MOUSE_RIGHT_BUTTON = $0002; {The Right mouse button is pressed}
MOUSE_MIDDLE_BUTTON = $0004; {The Middle mouse button is pressed}
MOUSE_SIDE_BUTTON = $0008; {The Side mouse button is pressed}
MOUSE_EXTRA_BUTTON = $0010; {The Extra mouse button is pressed}
MOUSE_TOUCH_BUTTON = $0020; {The Touch screen is being touched}
MOUSE_ABSOLUTE_X = $0040; {The OffsetX value is absolute not relative}
MOUSE_ABSOLUTE_Y = $0080; {The OffsetY value is absolute not relative}
MOUSE_ABSOLUTE_WHEEL = $0100; {The OffsetWheel value is absolute not relative}
{Mouse logging}
MOUSE_LOG_LEVEL_DEBUG = LOG_LEVEL_DEBUG; {Mouse debugging messages}
MOUSE_LOG_LEVEL_INFO = LOG_LEVEL_INFO; {Mouse informational messages, such as a device being attached or detached}
MOUSE_LOG_LEVEL_ERROR = LOG_LEVEL_ERROR; {Mouse error messages}
MOUSE_LOG_LEVEL_NONE = LOG_LEVEL_NONE; {No Mouse messages}
var
MOUSE_DEFAULT_LOG_LEVEL:LongWord = MOUSE_LOG_LEVEL_DEBUG; {Minimum level for Mouse messages. Only messages with level greater than or equal to this will be printed}
var
{Mouse logging}
MOUSE_LOG_ENABLED:Boolean;
mouserecord:array[0..7] of shortint;
mouserecordb:array[0..7] of byte absolute mouserecord;
mousetype:integer=0;
{==============================================================================}
const
{USB Mouse specific constants}
USBMOUSE_DRIVER_NAME = 'USB Mouse Driver (HID boot protocol)'; {Name of USB mouse driver}
USBMOUSE_MOUSE_DESCRIPTION = 'USB HID Mouse'; {Description of USB mouse device}
{HID Interface Subclass types (See USB HID v1.11 specification)}
USB_HID_SUBCLASS_BOOT = 1; {Section 4.2}
{HID Interface Protocol types (See USB HID v1.11 specification)}
USB_HID_BOOT_PROTOCOL_KEYBOARD = 1; {Section 4.3}
USB_HID_BOOT_PROTOCOL_MOUSE = 2; {Section 4.3}
{HID Request types}
USB_HID_REQUEST_GET_REPORT = $01;
USB_HID_REQUEST_GET_IDLE = $02;
USB_HID_REQUEST_GET_PROTOCOL = $03; {Section 7.2}
USB_HID_REQUEST_SET_REPORT = $09;
USB_HID_REQUEST_SET_IDLE = $0A;
USB_HID_REQUEST_SET_PROTOCOL = $0B; {Section 7.2}
{HID Protocol types}
USB_HID_PROTOCOL_BOOT = 0; {Section 7.2.5}
USB_HID_PROTOCOL_REPORT = 1; {Section 7.2.5}
{HID Report types}
USB_HID_REPORT_INPUT = 1; {Section 7.2.1}
USB_HID_REPORT_OUTPUT = 2; {Section 7.2.1}
USB_HID_REPORT_FEATURE = 3; {Section 7.2.1}
{HID Report IDs}
USB_HID_REPORTID_NONE = 0; {Section 7.2.1}
{HID Boot Protocol Button bits}
USB_HID_BOOT_LEFT_BUTTON = (1 shl 0);
USB_HID_BOOT_RIGHT_BUTTON = (1 shl 1);
USB_HID_BOOT_MIDDLE_BUTTON = (1 shl 2);
USB_HID_BOOT_SIDE_BUTTON = (1 shl 3);
USB_HID_BOOT_EXTRA_BUTTON = (1 shl 4);
{HID Boot Protocol Report data}
USB_HID_BOOT_REPORT_SIZE = 3; {Appendix B of HID Device Class Definition 1.11}
USB_HID_BOOT_DATA_SIZE = 8; {Allocate more than the minimum to allow for extra data}
{==============================================================================}
type
{Mouse specific types}
{Mouse Data}
PMouseData = ^TMouseData;
TMouseData = record
Buttons:Word;
OffsetX:integer;
OffsetY:integer;
OffsetWheel:integer;
end;
{Mouse Buffer}
PMouseBuffer = ^TMouseBuffer;
TMouseBuffer = record
Wait:TSemaphoreHandle; {Buffer ready semaphore}
Start:LongWord; {Index of first buffer ready}
Count:LongWord; {Number of entries ready in buffer}
Buffer:array[0..(MOUSE_BUFFER_SIZE - 1)] of TMouseData;
end;
{Mouse Device}
PMouseDevice = ^TMouseDevice;
{Mouse Enumeration Callback}
TMouseEnumerate = function(Mouse:PMouseDevice;Data:Pointer):LongWord;
{Mouse Notification Callback}
TMouseNotification = function(Device:PDevice;Data:Pointer;Notification:LongWord):LongWord;
{Mouse Device Methods}
TMouseDeviceRead = function(Mouse:PMouseDevice;Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
TMouseDeviceControl = function(Mouse:PMouseDevice;Request:Integer;Argument1:LongWord;var Argument2:LongWord):LongWord;
TMouseDevice = record
{Device Properties}
Device:TDevice; {The Device entry for this Mouse}
{Mouse Properties}
MouseId:LongWord; {Unique Id of this Mouse in the Mouse table}
MouseState:LongWord; {Mouse state (eg MOUSE_STATE_ATTACHED)}
DeviceRead:TMouseDeviceRead; {A Device specific DeviceRead method implementing a standard Mouse device interface (Or nil if the default method is suitable)}
DeviceControl:TMouseDeviceControl; {A Device specific DeviceControl method implementing a standard Mouse device interface (Or nil if the default method is suitable)}
{Driver Properties}
Lock:TMutexHandle; {Mouse lock}
Buffer:TMouseBuffer; {Mouse input buffer}
{Statistics Properties}
ReceiveCount:LongWord;
ReceiveErrors:LongWord;
BufferOverruns:LongWord;
{Internal Properties}
Prev:PMouseDevice; {Previous entry in Mouse table}
Next:PMouseDevice; {Next entry in Mouse table}
end;
{==============================================================================}
type
{USB Mouse specific types}
PUSBMouseDevice = ^TUSBMouseDevice;
TUSBMouseDevice = record
{Mouse Properties}
Mouse:TMouseDevice;
{USB Properties}
HIDInterface:PUSBInterface; {USB HID Mouse Interface}
ReportRequest:PUSBRequest; {USB request for mouse report data}
ReportEndpoint:PUSBEndpointDescriptor; {USB Mouse Interrupt IN Endpoint}
PendingCount:LongWord; {Number of USB requests pending for this mouse}
WaiterThread:TThreadId; {Thread waiting for pending requests to complete (for mouse detachment)}
end;
{==============================================================================}
{var}
{Mouse specific variables}
{==============================================================================}
{var}
{USB Mouse specific variables}
{==============================================================================}
{Initialization Functions}
procedure MouseInit;
{==============================================================================}
{Mouse Functions}
function MousePeek:LongWord;
function MouseRead(Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
function MouseReadEx(Buffer:Pointer;Size,Flags:LongWord;var Count:LongWord):LongWord;
function MouseWrite(Buffer:Pointer;Size,Count:LongWord):LongWord;
function MouseFlush:LongWord;
function MouseDeviceRead(Mouse:PMouseDevice;Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
function MouseDeviceControl(Mouse:PMouseDevice;Request:Integer;Argument1:LongWord;var Argument2:LongWord):LongWord;
function MouseDeviceSetState(Mouse:PMouseDevice;State:LongWord):LongWord;
function MouseDeviceCreate:PMouseDevice;
function MouseDeviceCreateEx(Size:LongWord):PMouseDevice;
function MouseDeviceDestroy(Mouse:PMouseDevice):LongWord;
function MouseDeviceRegister(Mouse:PMouseDevice):LongWord;
function MouseDeviceDeregister(Mouse:PMouseDevice):LongWord;
function MouseDeviceFind(MouseId:LongWord):PMouseDevice;
function MouseDeviceFindByName(const Name:String):PMouseDevice; inline;
function MouseDeviceFindByDescription(const Description:String):PMouseDevice; inline;
function MouseDeviceEnumerate(Callback:TMouseEnumerate;Data:Pointer):LongWord;
function MouseDeviceNotification(Mouse:PMouseDevice;Callback:TMouseNotification;Data:Pointer;Notification,Flags:LongWord):LongWord;
{==============================================================================}
{RTL Console Functions}
function SysConsoleHideMouse(AUserData:Pointer):Boolean;
function SysConsoleShowMouse(X,Y:LongWord;AUserData:Pointer):Boolean;
function SysConsoleReadMouse(var X,Y,Buttons:LongWord;AUserData:Pointer):Boolean;
{==============================================================================}
{USB Mouse Functions}
function USBMouseDeviceRead(Mouse:PMouseDevice;Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
function USBMouseDeviceControl(Mouse:PMouseDevice;Request:Integer;Argument1:LongWord;var Argument2:LongWord):LongWord;
function USBMouseDriverBind(Device:PUSBDevice;Interrface:PUSBInterface):LongWord;
function USBMouseDriverUnbind(Device:PUSBDevice;Interrface:PUSBInterface):LongWord;
procedure USBMouseReportWorker(Request:PUSBRequest);
procedure USBMouseReportComplete(Request:PUSBRequest);
{==============================================================================}
{Mouse Helper Functions}
function MouseGetCount:LongWord; inline;
function MouseDeviceCheck(Mouse:PMouseDevice):PMouseDevice;
function MouseDeviceTypeToString(MouseType:LongWord):String;
function MouseDeviceStateToString(MouseState:LongWord):String;
function MouseDeviceStateToNotification(State:LongWord):LongWord;
procedure MouseLog(Level:LongWord;Mouse:PMouseDevice;const AText:String);
procedure MouseLogInfo(Mouse:PMouseDevice;const AText:String);
procedure MouseLogError(Mouse:PMouseDevice;const AText:String);
procedure MouseLogDebug(Mouse:PMouseDevice;const AText:String);
{==============================================================================}
{USB Mouse Helper Functions}
function USBMouseDeviceSetProtocol(Mouse:PUSBMouseDevice;Protocol:Byte):LongWord;
{==============================================================================}
{==============================================================================}
// ---------------------------------patch added by pik33 @ 20170103 ------------
type TMousereport=array[0..7] of byte;
var mouse_report_buffer: array[0..1023] of byte;
mouse_rb_start:integer=0;
mouse_rb_end:integer=0;
mouse_report_buffer_active:boolean=false;
chuj:integer;
function getmousereport:TMousereport;
procedure startmousereportbuffer;
procedure stopmousereportbuffer;
implementation
procedure stopmousereportbuffer;
begin
mouse_report_buffer_active:=false;
end;
procedure startmousereportbuffer;
begin
mouse_report_buffer_active:=true;
end;
function getmousereport:Tmousereport;
var ii:integer;
begin
if mouse_rb_end <>mouse_rb_start then begin
for ii:=0 to 7 do result[ii]:=mouse_report_buffer[8*mouse_rb_start+ii];
mouse_rb_start:=(mouse_rb_start+1) and $3F;
end
else
for ii:=0 to 7 do result[ii]:=255;
end;
// --------- end of patch ------------------------------------------------------
{==============================================================================}
{==============================================================================}
var
{Mouse specific variables}
MouseInitialized:Boolean;
MouseTable:PMouseDevice;
MouseTableLock:TCriticalSectionHandle = INVALID_HANDLE_VALUE;
MouseTableCount:LongWord;
MouseBuffer:PMouseBuffer; {Global mouse input buffer}
MouseBufferLock:TMutexHandle = INVALID_HANDLE_VALUE; {Global mouse buffer lock}
{==============================================================================}
{==============================================================================}
var
{USB Mouse specific variables}
USBMouseDriver:PUSBDriver; {USB Mouse Driver interface (Set by MouseInit)}
{==============================================================================}
{==============================================================================}
{Initialization Functions}
procedure MouseInit;
{Initialize the mouse unit, device table and USB mouse driver}
{Note: Called only during system startup}
var
Status:LongWord;
begin
{}
{Check Initialized}
if MouseInitialized then Exit;
{Initialize Logging}
MOUSE_LOG_ENABLED:=(MOUSE_DEFAULT_LOG_LEVEL <> MOUSE_LOG_LEVEL_NONE);
{Initialize Mouse Table}
MouseTable:=nil;
MouseTableLock:=CriticalSectionCreate;
MouseTableCount:=0;
if MouseTableLock = INVALID_HANDLE_VALUE then
begin
if MOUSE_LOG_ENABLED then MouseLogError(nil,'Failed to create mouse table lock');
end;
{Initialize Mouse Buffer}
MouseBuffer:=AllocMem(SizeOf(TMouseBuffer));
MouseBufferLock:=INVALID_HANDLE_VALUE;
if MouseBuffer = nil then
begin
if MOUSE_LOG_ENABLED then MouseLogError(nil,'Failed to allocate mouse buffer');
end
else
begin
{Create Semaphore}
MouseBuffer.Wait:=SemaphoreCreate(0);
if MouseBuffer.Wait = INVALID_HANDLE_VALUE then
begin
if MOUSE_LOG_ENABLED then MouseLogError(nil,'Failed to create mouse buffer semaphore');
end;
{Create Lock}
MouseBufferLock:=MutexCreate;
if MouseBufferLock = INVALID_HANDLE_VALUE then
begin
if MOUSE_LOG_ENABLED then MouseLogError(nil,'Failed to create mouse buffer lock');
end;
end;
{Create USB Mouse Driver}
USBMouseDriver:=USBDriverCreate;
if USBMouseDriver <> nil then
begin
{Update USB Mouse Driver}
{Driver}
USBMouseDriver.Driver.DriverName:=USBMOUSE_DRIVER_NAME;
{USB}
USBMouseDriver.DriverBind:=USBMouseDriverBind;
USBMouseDriver.DriverUnbind:=USBMouseDriverUnbind;
{Register USB Mouse Driver}
Status:=USBDriverRegister(USBMouseDriver);
if Status <> USB_STATUS_SUCCESS then
begin
if USB_LOG_ENABLED then USBLogError(nil,'Mouse: Failed to register USB mouse driver: ' + USBStatusToString(Status));
end;
end
else
begin
if MOUSE_LOG_ENABLED then MouseLogError(nil,'Failed to create USB mouse driver');
end;
{Setup Platform Console Handlers}
ConsoleHideMouseHandler:=SysConsoleHideMouse;
ConsoleShowMouseHandler:=SysConsoleShowMouse;
ConsoleReadMouseHandler:=SysConsoleReadMouse;
MouseInitialized:=True;
end;
{==============================================================================}
{==============================================================================}
{Mouse Functions}
function MousePeek:LongWord;
{Peek at the global mouse buffer to see if any data packets are ready}
{Return: ERROR_SUCCESS if packets are ready, ERROR_NO_MORE_ITEMS if not or another error code on failure}
var
Count:LongWord;
Data:TMouseData;
begin
{}
Result:=MouseReadEx(@Data,SizeOf(TMouseData),MOUSE_FLAG_NON_BLOCK or MOUSE_FLAG_PEEK_BUFFER,Count);
end;
{==============================================================================}
function MouseRead(Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
{Read mouse data packets from the global mouse buffer}
{Buffer: Pointer to a buffer to copy the mouse data packets to}
{Size: The size of the buffer in bytes (Must be at least TMouseData or greater)}
{Count: The number of mouse data packets copied to the buffer}
{Return: ERROR_SUCCESS if completed or another error code on failure}
begin
{}
Result:=MouseReadEx(Buffer,Size,MOUSE_FLAG_NONE,Count);
end;
{==============================================================================}
function MouseReadEx(Buffer:Pointer;Size,Flags:LongWord;var Count:LongWord):LongWord;
{Read mouse data packets from the global mouse buffer}
{Buffer: Pointer to a buffer to copy the mouse data packets to}
{Size: The size of the buffer in bytes (Must be at least TMouseData or greater)}
{Flags: The flags for the behaviour of the read (eg MOUSE_FLAG_NON_BLOCK)}
{Count: The number of mouse data packets copied to the buffer}
{Return: ERROR_SUCCESS if completed or another error code on failure}
var
Offset:PtrUInt;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check Buffer}
if Buffer = nil then Exit;
{Check Size}
if Size < SizeOf(TMouseData) then Exit;
{$IFDEF MOUSE_DEBUG}
if MOUSE_LOG_ENABLED then MouseLogDebug(nil,'Attempting to read ' + IntToStr(Size) + ' bytes from mouse');
{$ENDIF}
{Read to Buffer}
Count:=0;
Offset:=0;
while Size >= SizeOf(TMouseData) do
begin
{Check Non Blocking}
if ((Flags and MOUSE_FLAG_NON_BLOCK) <> 0) and (MouseBuffer.Count = 0) then
begin
if Count = 0 then Result:=ERROR_NO_MORE_ITEMS;
Break;
end;
{Check Peek Buffer}
if (Flags and MOUSE_FLAG_PEEK_BUFFER) <> 0 then
begin
{Acquire the Lock}
if MutexLock(MouseBufferLock) = ERROR_SUCCESS then
begin
try
if MouseBuffer.Count > 0 then
begin
{Copy Data}
PMouseData(PtrUInt(Buffer) + Offset)^:=MouseBuffer.Buffer[MouseBuffer.Start];
{Update Count}
Inc(Count);
Result:=ERROR_SUCCESS;
Break;
end
else
begin
Result:=ERROR_NO_MORE_ITEMS;
Break;
end;
finally
{Release the Lock}
MutexUnlock(MouseBufferLock);
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end
else
begin
{Wait for Mouse Data}
if SemaphoreWait(MouseBuffer.Wait) = ERROR_SUCCESS then
begin
{Acquire the Lock}
if MutexLock(MouseBufferLock) = ERROR_SUCCESS then
begin
try
{Copy Data}
PMouseData(PtrUInt(Buffer) + Offset)^:=MouseBuffer.Buffer[MouseBuffer.Start];
{Update Start}
MouseBuffer.Start:=(MouseBuffer.Start + 1) mod MOUSE_BUFFER_SIZE;
{Update Count}
Dec(MouseBuffer.Count);
{Update Count}
Inc(Count);
{Upate Size and Offset}
Dec(Size,SizeOf(TMouseData));
Inc(Offset,SizeOf(TMouseData));
finally
{Release the Lock}
MutexUnlock(MouseBufferLock);
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
{$IFDEF MOUSE_DEBUG}
if MOUSE_LOG_ENABLED then MouseLogDebug(nil,'Return count=' + IntToStr(Count));
{$ENDIF}
end;
{==============================================================================}
function MouseWrite(Buffer:Pointer;Size,Count:LongWord):LongWord;
{Write mouse data packets to the global mouse buffer}
{Buffer: Pointer to a buffer to copy the mouse data packets from}
{Size: The size of the buffer in bytes (Must be at least TMouseData or greater)}
{Count: The number of mouse data packets to copy from the buffer}
{Return: ERROR_SUCCESS if completed or another error code on failure}
var
Offset:PtrUInt;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check Buffer}
if Buffer = nil then Exit;
{Check Size}
if Size < SizeOf(TMouseData) then Exit;
{Check Count}
if Count < 1 then Exit;
{$IFDEF MOUSE_DEBUG}
if MOUSE_LOG_ENABLED then MouseLogDebug(nil,'Attempting to write ' + IntToStr(Size) + ' bytes to mouse');
{$ENDIF}
{Write from Buffer}
Offset:=0;
while (Size >= SizeOf(TMouseData)) and (Count > 0) do
begin
{Acquire the Lock}
if MutexLock(MouseBufferLock) = ERROR_SUCCESS then
begin
try
{Check Buffer}
if (MouseBuffer.Count < MOUSE_BUFFER_SIZE) then
begin
{Copy Data}
MouseBuffer.Buffer[(MouseBuffer.Start + MouseBuffer.Count) mod MOUSE_BUFFER_SIZE]:=PMouseData(PtrUInt(Buffer) + Offset)^;
{Update Count}
Inc(MouseBuffer.Count);
{Update Count}
Dec(Count);
{Upate Size and Offset}
Dec(Size,SizeOf(TMouseData));
Inc(Offset,SizeOf(TMouseData));
{Signal Data Received}
SemaphoreSignal(MouseBuffer.Wait);
end
else
begin
Result:=ERROR_INSUFFICIENT_BUFFER;
Exit;
end;
finally
{Release the Lock}
MutexUnlock(MouseBufferLock);
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
end;
{==============================================================================}
function MouseFlush:LongWord;
{Flush the contents of the global mouse buffer}
{Return: ERROR_SUCCESS if completed or another error code on failure}
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Acquire the Lock}
if MutexLock(MouseBufferLock) = ERROR_SUCCESS then
begin
try
while MouseBuffer.Count > 0 do
begin
{Wait for Data (Should not Block)}
if SemaphoreWait(MouseBuffer.Wait) = ERROR_SUCCESS then
begin
{Update Start}
MouseBuffer.Start:=(MouseBuffer.Start + 1) mod MOUSE_BUFFER_SIZE;
{Update Count}
Dec(MouseBuffer.Count);
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end;
{Return Result}
Result:=ERROR_SUCCESS;
finally
{Release the Lock}
MutexUnlock(MouseBufferLock);
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end;
{==============================================================================}
function MouseDeviceRead(Mouse:PMouseDevice;Buffer:Pointer;Size:LongWord;var Count:LongWord):LongWord;
{Read mouse data packets from the buffer of the specified mouse}
{Mouse: The mouse device to read from}
{Buffer: Pointer to a buffer to copy the mouse data packets to}
{Size: The size of the buffer in bytes (Must be at least TMouseData or greater)}
{Count: The number of mouse data packets copied to the buffer}
{Return: ERROR_SUCCESS if completed or another error code on failure}
var
Offset:PtrUInt;
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check Mouse}
if Mouse = nil then Exit;
if Mouse.Device.Signature <> DEVICE_SIGNATURE then Exit;
{Check Buffer}
if Buffer = nil then Exit;
{Check Size}
if Size < SizeOf(TMouseData) then Exit;
{Check Method}
if Assigned(Mouse.DeviceRead) then
begin
{Provided Method}
Result:=Mouse.DeviceRead(Mouse,Buffer,Size,Count);
end
else
begin
{Default Method}
{Check Mouse Attached}
if Mouse.MouseState <> MOUSE_STATE_ATTACHED then Exit;
{$IFDEF MOUSE_DEBUG}
if MOUSE_LOG_ENABLED then MouseLogDebug(Mouse,'Attempting to read ' + IntToStr(Size) + ' bytes from mouse');
{$ENDIF}
{Read to Buffer}
Count:=0;
Offset:=0;
while Size >= SizeOf(TMouseData) do
begin
{Check Non Blocking}
if ((Mouse.Device.DeviceFlags and MOUSE_FLAG_NON_BLOCK) <> 0) and (Mouse.Buffer.Count = 0) then
begin
if Count = 0 then Result:=ERROR_NO_MORE_ITEMS;
Break;
end;
{Wait for Mouse Data}
if SemaphoreWait(Mouse.Buffer.Wait) = ERROR_SUCCESS then
begin
{Acquire the Lock}
if MutexLock(Mouse.Lock) = ERROR_SUCCESS then
begin
try
{Copy Data}
PMouseData(PtrUInt(Buffer) + Offset)^:=Mouse.Buffer.Buffer[Mouse.Buffer.Start];
{Update Start}
Mouse.Buffer.Start:=(Mouse.Buffer.Start + 1) mod MOUSE_BUFFER_SIZE;
{Update Count}
Dec(Mouse.Buffer.Count);
{Update Count}
Inc(Count);
{Upate Size and Offset}
Dec(Size,SizeOf(TMouseData));
Inc(Offset,SizeOf(TMouseData));
finally
{Release the Lock}
MutexUnlock(Mouse.Lock);
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
{$IFDEF MOUSE_DEBUG}
if MOUSE_LOG_ENABLED then MouseLogDebug(Mouse,'Return count=' + IntToStr(Count));
{$ENDIF}
end;
end;
{==============================================================================}
function MouseDeviceControl(Mouse:PMouseDevice;Request:Integer;Argument1:LongWord;var Argument2:LongWord):LongWord;
{Perform a control request on the specified mouse device}
{Mouse: The mouse device to control}
{Request: The request code for the operation (eg MOUSE_CONTROL_GET_FLAG)}
{Argument1: The first argument for the operation (Dependent on request code)}
{Argument2: The second argument for the operation (Dependent on request code)}
{Return: ERROR_SUCCESS if completed or another error code on failure}
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check Mouse}
if Mouse = nil then Exit;
if Mouse.Device.Signature <> DEVICE_SIGNATURE then Exit;
{Check Method}
if Assigned(Mouse.DeviceControl) then
begin
{Provided Method}
Result:=Mouse.DeviceControl(Mouse,Request,Argument1,Argument2);
end
else
begin
{Default Method}
{Check Mouse Attached}
if Mouse.MouseState <> MOUSE_STATE_ATTACHED then Exit;
{Acquire the Lock}
if MutexLock(Mouse.Lock) = ERROR_SUCCESS then
begin
try
case Request of
MOUSE_CONTROL_GET_FLAG:begin
{Get Flag}
LongBool(Argument2):=False;
if (Mouse.Device.DeviceFlags and Argument1) <> 0 then
begin
LongBool(Argument2):=True;
{Return Result}
Result:=ERROR_SUCCESS;
end;
end;
MOUSE_CONTROL_SET_FLAG:begin
{Set Flag}
if (Argument1 and not(MOUSE_FLAG_MASK)) = 0 then
begin
Mouse.Device.DeviceFlags:=(Mouse.Device.DeviceFlags or Argument1);
{Return Result}
Result:=ERROR_SUCCESS;
end;
end;
MOUSE_CONTROL_CLEAR_FLAG:begin
{Clear Flag}
if (Argument1 and not(MOUSE_FLAG_MASK)) = 0 then
begin
Mouse.Device.DeviceFlags:=(Mouse.Device.DeviceFlags and not(Argument1));
{Return Result}
Result:=ERROR_SUCCESS;
end;
end;
MOUSE_CONTROL_FLUSH_BUFFER:begin
{Flush Buffer}
while Mouse.Buffer.Count > 0 do
begin
{Wait for Data (Should not Block)}
if SemaphoreWait(Mouse.Buffer.Wait) = ERROR_SUCCESS then
begin
{Update Start}
Mouse.Buffer.Start:=(Mouse.Buffer.Start + 1) mod MOUSE_BUFFER_SIZE;
{Update Count}
Dec(Mouse.Buffer.Count);
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end;
{Return Result}
Result:=ERROR_SUCCESS;
end;
end;
finally
{Release the Lock}
MutexUnlock(Mouse.Lock);
end;
end
else
begin
Result:=ERROR_CAN_NOT_COMPLETE;
Exit;
end;
end;
end;
{==============================================================================}
function MouseDeviceSetState(Mouse:PMouseDevice;State:LongWord):LongWord;
{Set the state of the specified mouse and send a notification}
{Mouse: The mouse to set the state for}
{State: The new state to set and notify}
{Return: ERROR_SUCCESS if completed or another error code on failure}
begin
{}
Result:=ERROR_INVALID_PARAMETER;
{Check Mouse}
if Mouse = nil then Exit;
if Mouse.Device.Signature <> DEVICE_SIGNATURE then Exit;
{Check State}
if State > MOUSE_STATE_ATTACHED then Exit;
{Check State}
if Mouse.MouseState = State then
begin
{Return Result}
Result:=ERROR_SUCCESS;
end
else
begin
{Acquire the Lock}
if MutexLock(Mouse.Lock) = ERROR_SUCCESS then
begin
try
{Set State}
Mouse.MouseState:=State;
{Notify State}
NotifierNotify(@Mouse.Device,MouseDeviceStateToNotification(State));
{Return Result}
Result:=ERROR_SUCCESS;
finally
{Release the Lock}
MutexUnlock(Mouse.Lock);
end;
end