-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhid_read.c
302 lines (279 loc) · 8.52 KB
/
hid_read.c
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
/*
* Usage: hid_read [options] device
* read data from HID devices.
-m X Device mode (scancode|keycode)
-o X Output (char|line)
-s X Skip first X chars
-l X Print only first X chars (can be used with -s)
-v Be verbose
-vv Be more verbose
-d Show debug messages
-t X If the device disappears, wait X µs before retry
-r If the device disappears, retry X times before crashing
-h Show this message
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <signal.h>
#include <linux/input.h>
#define EV_PRESSED 1
#define EV_RELEASED 0
#define EV_REPEAT 2
short verbose=0;
short debug=0;
char* devname;
int max_retry=3;
int timeout=10;
short loop=1;
int retry=-1;
/* 0 = scancode, 1 = keycode */
int mode=0;
/* 0 = line, 1 = char */
int outmode=0;
/* Start from the beginning */
int start=0;
/* Max length */
int length=0;
/* KBDUS means US Keyboard Layout. This is a scancode table
* used to layout a standard US keyboard. I have left some
* comments in to give you an idea of what key is what, even
* though I set it's array index to 0. You can change that to
* whatever you want using a macro, if you wish! */
unsigned char kbdus[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
0,
0,
0,
0,
0,
0,
'\n' /* Enter key */
};
int prepareDev(char* devname){
int fd = sizeof(struct input_event);
char name[256] = "Unknown";
/* Open Device */
if ((fd = open (devname, O_RDONLY)) == -1){
fprintf(stderr,"%s is not a valid device.\n", devname);
perror("error: ");
if (retry > -1 && retry < max_retry){
/* If the device disappeared, try to recover */
retry++;
fprintf(stderr,"Trying to recover...\n");
usleep(timeout);
return prepareDev(devname);
}
exit(1);
}
if (verbose > 0){
/* Print Device Name */
ioctl (fd, EVIOCGNAME (sizeof (name)), name);
fprintf (stderr,"Reading From : %s (%s)\n", devname, name);
}
/* Uso esclusivo */
ioctl (fd, EVIOCGRAB, 1);
if (retry > -1){
/* The device is now usable */
retry=0;
fprintf(stderr,"Crash counter set to 0.\n");
}
return fd;
}
int main (int argc, char *argv[]){
int fd;
int yalv;
/* how many bytes were read */
size_t rb;
/* the events (up to 64 at once) */
struct input_event ev[64];
/* Entire line */
char* key;
/* Buffer */
char buffer[2];
int c;
/* Load settings from commandline */
while ((c = getopt (argc, argv, "r:t:m:o:s:l:vdh")) != -1){
switch (c){
case 'r':
max_retry = atoi(optarg);
break;
case 't':
timeout = atoi(optarg);
break;
case 'm':
if (strcmp(optarg,"scancode")==0)
mode=0;
else if (strcmp(optarg,"keycode")==0)
mode=1;
else
mode=2;
break;
case 'o':
if (strcmp(optarg,"line")==0)
outmode=0;
else
outmode=1;
break;
case 's':
start = atoi(optarg);
break;
case 'l':
length = atoi(optarg);
break;
case 'v':
verbose++;
break;
case 'd':
debug++;
break;
case 'h':
printf("Usage: hid_read [options] device\n"
"read data from HID devices.\n\n"
"-m X\t\tDevice mode (scancode|keycode)\n"
"-o X\t\tOutput (char|line)\n"
"-s X\t\tSkip first X chars\n"
"-l X\t\tPrint only first X chars (can be used with -s)\n"
"-v\t\tBe verbose\n"
"-vv\t\tBe more verbose\n"
"-d\t\tShow debug messages\n"
"-t X\t\tIf the device disappears, wait X µs before retry\n"
"-r\t\tIf the device disappears, retry X times before crashing\n"
"-h\t\tShow this message\n\n"
);
exit (1);
}
}
/* The next argument is the device name */
devname=argv[optind];
/* Setup check */
if (devname == NULL){
fprintf(stderr,"Please specify the path to the dev event interface device\n");
exit(1);
}
/* Open device and grab it */
fd=prepareDev(devname);
/* Initialize key */
key=calloc(1,sizeof(char));
while (loop){
/* Read events from device */
rb=read(fd,ev,sizeof(struct input_event)*64);
/* Check for errors */
if (rb == -1){
if (errno==EINTR){
break;
}
else{
retry++;
perror("read error: ");
usleep(timeout);
fd=prepareDev(devname);
continue;
}
}
else if (rb < (int) sizeof(struct input_event)){
retry++;
perror("evtest: short read");
usleep(timeout);
fd=prepareDev(devname);
continue;
}
/* Parse events */
for (yalv = 0; yalv < (int) (rb / sizeof(struct input_event)); yalv++){
if (EV_KEY == ev[yalv].type){
/* Stampa solo al keyup */
if (ev[yalv].value==EV_RELEASED){
if (debug > 0){
fprintf(stderr,"debug: key released: %d | %c | %d\n", ev[yalv].code,kbdus[ev[yalv].code],kbdus[ev[yalv].code]);
}
if (outmode == 0){
if (kbdus[ev[yalv].code] == '\n'){
if (length == 0){
printf("%s\n",key+start);
}
else{
printf("%.*s\n",length,key+start);
}
fflush(stdout);
/* Clean old key */
free(key);
key=calloc(1,sizeof(char));
}
else{
key=realloc(key,(strlen(key)+2)*sizeof(char));
buffer[0]=kbdus[ev[yalv].code];
buffer[1]='\0';
strcat(key,buffer);
}
}
else{
if (mode==0)
printf("%3d\n", ev[yalv].code);
else if (mode==1)
printf("%3d\n", kbdus[ev[yalv].code]);
else
printf("%3c\n", kbdus[ev[yalv].code]);
fflush(stdout);
if (verbose > 1){
fprintf(stderr,"%d | %c | %d\n", ev[yalv].code,kbdus[ev[yalv].code],kbdus[ev[yalv].code]);
}
}
}
else if (debug > 1){
fprintf(stderr,"debug (not ev_released: %d): %d | %c | %d\n", ev[yalv].value, ev[yalv].code,kbdus[ev[yalv].code],kbdus[ev[yalv].code]);
}
}
else if (debug > 1){
fprintf(stderr,"debug (not a key: %d): %d | %c | %d\n", ev[yalv].type, ev[yalv].code, kbdus[ev[yalv].code],kbdus[ev[yalv].code]);
}
}
}
free(key);
close(fd);
return 0;
}