-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.dpr
256 lines (225 loc) · 7.92 KB
/
event.dpr
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
{
Linux Event Tester
1.0 - 2019.04.24 - Nicola Perotto <[email protected]>
Initial release, translated from evtest.c
1.0.1 - 2019.04.27 - Nicola Perotto <[email protected]>
Little cleanup and not blocking read
}
{$I+,R+,Q+}
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
Program Event;
uses
{$IFDEF FPC}CThreads, CMem, BaseUnix,{$ELSE}CompileInDelphi,{$ENDIF} Classes, SysUtils, RtlConsts,
IOCtl, Input, InputEventCodes, InputUtils
;
Const
sIOCtlError = 'IOCtl failed. Request code %d.';
MaxNumEvents = 64;
EventNames : array[0..EV_MAX] of string = (
'Sync', // EV_SYN
'Key', // EV_KEY
'Relative', // EV_REL
'Absolute', // EV_ABS
'Misc', // EV_MSC
'Software', // EV_SW
'', // 06
'', // 07
'', // 08
'', // 09
'', // 0a
'', // 0b
'', // 0c
'', // 0d
'', // 0e
'', // 0f
'', // 10
'LED', // EV_LED
'Sound', // EV_SND
'', // 13
'Repeat', // EV_REP
'ForceFeedback', // EV_FF
'Power', // EV_PWR
'ForceFeedbackStatus', // EV_FF_STATUS
'', // 18
'', // 19
'', // 1a
'', // 1b
'', // 1c
'', // 1d
'', // 1e
'' // EV_MAX
);
AbsNames : array[0..5] of string = ('Value', 'Min', 'Max', 'Fuzz', 'Flat', 'Resolution');
RelativeNames : array[0..REL_MAX] of string = (
'REL_X',
'REL_Y',
'REL_Z',
'REL_RX',
'REL_RY',
'REL_RZ',
'REL_HWHEEL',
'REL_DIAL',
'REL_WHEEL',
'REL_MISC',
'REL_RESERVED',
'REL_WHEEL_HI_RES',
'REL_HWHEEL_HI_RES',
'',
'',
''
);
Var
SigOA, SigNA : PSigActionRec;
Terminated : Boolean;
MyRotary : string; //event file name
i, j, k, r, fHandle : integer;
Version : integer;
ID : TInput_ID;
StringBuffer : string;
Name : string;
Active : LongWord;
EBits : array[0..(InputEventCodes.KEY_CNT div 8)-1] of byte; //one bit each Key code
EAbs : TInput_Absinfo;
sl : TStringList;
ev : array[0..MaxNumEvents -1] of TInput_Event;
function GetBit(Value: QWord; Index: Byte): Boolean;
begin
Result := ((Value shr Index) and 1) = 1;
end;
{$IFDEF FPC}
procedure DoIoCtlError(Ndx:LongWord); //inline;
var //uses LongWord in place of TIOCtlRequest
e: EOSError;
begin
e := EOSError.CreateFmt(sIOCtlError, [Ndx]);
e.ErrorCode := baseunix.errno;
raise e;
end;
{$ENDIF}
Procedure DoSig(sig:cint); {$IFDEF FPC}Cdecl;{$ENDIF}
begin //Wait for SigTerm: kill pid
Terminated := True;
writeln('Got Signal!');
end; //DoSig
Procedure Init;
begin
Terminated := False;
{$IFDEF FPC}
New(SigNA);
New(SigOA);
SigNA^.sa_Handler := SigActionHandler(@DoSig);
Fillchar(SigNA^.Sa_Mask, sizeof(SigNA^.sa_mask), #0);
SigNA^.Sa_Flags := 0;
SigNA^.Sa_Restorer := Nil;
if fpSigAction(SigTerm, SigNA, SigOA) <> 0 then begin
writeln('Signal Handler Error: ', fpgeterrno);
Halt(1);
end;
{$ENDIF}
end; //Init
begin
Init;
if ParamCount = 0 then begin
sl := TStringList.Create;
EventFileList(sl);
for i := 0 to sl.Count -1 do
writeln(sl[i]);
FreeAndNil(sl);
Exit;
end;
writeln('Linux Event System Test');
MyRotary := EventFileSearch(ParamStr(1)); //or rotary@11 (11 = pin for A input in hex -> BCM 17)
if MyRotary = '' then begin
writeln('No Event Found.');
Halt(1);
end;
writeln(MyRotary);
fHandle := FpOpen(MyRotary, O_RDONLY);
if fHandle < 0 then
raise EFOpenError.CreateFmt(SFOpenError, [MyRotary]);
if FpIOCtl(fHandle, Integer(EVIOCGVERSION), @Version) = -1 then
DoIoCtlError(EVIOCGVERSION);
writeln(Format('Input driver version is %d.%d.%d', [Version shr 16, (Version shr 8) and $ff, Version and $ff]));
if FpIOCtl(fHandle, Integer(EVIOCGID), @ID) = -1 then
DoIoCtlError(EVIOCGID);
writeln(Format('Input device ID: bus $%2.2x vendor $%2.2x product $%2.2x version $%2.2x',
[id.bustype, id.vendor, id.product, id.version]));
SetLength(StringBuffer, EVIOCGNAME_size);
if FpIOCtl(fHandle, Integer(EVIOCGNAME), PChar(StringBuffer)) = -1 then
DoIoCtlError(EVIOCGNAME);
Name := Copy(StringBuffer, 1, Pos(#0, StringBuffer)); //copies from a C string to a wonderful Pascal string!
writeln('Input device name: ' + Name);
r := FpIOCtl(fHandle, Compose_IOC(_IOC_READ, Ord('E'), $20 + 0, SizeOf(LongWord)), @Active);
if r < 0 then
DoIoCtlError(EVIOCGBIT);
writeln('Supported events:');
for i := 0 to EV_MAX do begin
if GetBit(Active, i) then begin
writeln(Format(' Event type %d (%s)', [i, EventNames[i]]));
if i = 0 then continue; //EV_SYN no need to read further
FillChar(EBits, SizeOf(EBits), #0);
k := FpIOCtl(fHandle, Compose_IOC(_IOC_READ, Ord('E'), $20 + i, SizeOf(EBits)), @EBits);
if k < 0 then
DoIoCtlError(EVIOCGBIT);
//print EVIOCGBIT data
//for j := 0 to k -1 do write(Format('%2.2x ', [EBits[j]])); writeln;
for j := 0 to k * 8 -1 do begin //check only effective loaded bits
if GetBit(EBits[j div 8], j and 7) then begin
case i of
EV_KEY : begin
WriteLn(Format(' Event code %d $%4.4x (%d)', [j, j, j])); //the last it's the name... in the future!
end;
EV_ABS : begin
WriteLn(Format(' Event code %d $%4.4x (%d)', [j, j, j])); //the last it's the name... in the future!
r := FpIOCtl(fHandle, Compose_IOC(_IOC_READ, Ord('E'), $40 + j, SizeOf(EAbs)), @EAbs);
if r < 0 then
DoIoCtlError(EVIOCGABS);
writeln(Format(' %-10s %6d', [AbsNames[0], EAbs.Value]));
writeln(Format(' %-10s %6d', [AbsNames[1], EAbs.minimum]));
writeln(Format(' %-10s %6d', [AbsNames[2], EAbs.maximum]));
if EAbs.fuzz <> 0 then
writeln(Format(' %-10s %6d', [AbsNames[3], EAbs.fuzz]));
if EAbs.flat <> 0 then
writeln(Format(' %-10s %6d', [AbsNames[4], EAbs.flat]));
if EAbs.resolution <> 0 then
writeln(Format(' %-10s %6d', [AbsNames[5], EAbs.resolution]));
end;
EV_REL : begin //Rotary
WriteLn(Format(' Event code %d $%4.4x (%s)', [j, j, RelativeNames[j]]));
end;
else begin
WriteLn(Format(' Event code %d $%4.4x (%d)', [j, j, j])); //the last it's the name... in the future!
end;
end; //case
end; //if
end; //for j
end; //if used
end; //for i
WriteLn;
WriteLn('Testing ... (CTRL-C to exit)');
//the read will be non blocking, very important!
FpFcntl(fHandle, F_SETFL, O_NONBLOCK);
Terminated := False;
while (not Terminated) do begin
r := FpRead(fHandle, ev, sizeof(TInput_Event) * MaxNumEvents);
{
Because the non blocking read r can be negative, this is not an error!
Adjust the sleep to the frequency needed.
}
if r <= 0 then Sleep(0);
for i := 0 to (r div Sizeof(TInput_Event) - 1) do
if (ev[i].etype = EV_SYN) then begin
writeln(Format('Event: time %d.%6d, -------------- %d ------------ EV_SYN',
[ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code]));
end else if ((ev[i].etype = EV_MSC) and ((ev[i].code = MSC_RAW) or (ev[i].code = MSC_SCAN))) then begin
writeln(Format('Event: time %d.%6d, type %d (%s), code %d (%s), value %2x',
[ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].etype, 'evname', ev[i].code, 'evtype', ev[i].value]));
end else begin
writeln(Format('Event: time %d.%6d, type %d (%s), code %d (%s), value %d',
[ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].etype, 'evtype', ev[i].code, 'evname', ev[i].value]));
end; //if
end; //while
FpClose(fHandle);
end.