blob: c58eb5d1c50fdfaf10e4c4713523c5add2bb14cc [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdint.h>
5#include <dirent.h>
6#include <fcntl.h>
7#include <sys/ioctl.h>
8#include <sys/inotify.h>
9#include <sys/limits.h>
10#include <sys/poll.h>
Jeff Brownf8754332011-06-15 17:44:52 -070011#include <linux/input.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012#include <errno.h>
Elliott Hughes0badbd62014-12-29 12:24:25 -080013#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014
Jeff Brownf8754332011-06-15 17:44:52 -070015#include "getevent.h"
16
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017static struct pollfd *ufds;
18static char **device_names;
19static int nfds;
20
21enum {
22 PRINT_DEVICE_ERRORS = 1U << 0,
23 PRINT_DEVICE = 1U << 1,
24 PRINT_DEVICE_NAME = 1U << 2,
25 PRINT_DEVICE_INFO = 1U << 3,
26 PRINT_VERSION = 1U << 4,
27 PRINT_POSSIBLE_EVENTS = 1U << 5,
Jeff Brownf8754332011-06-15 17:44:52 -070028 PRINT_INPUT_PROPS = 1U << 6,
Jeff Brown4ac87152011-07-24 12:22:58 -070029 PRINT_HID_DESCRIPTOR = 1U << 7,
Jeff Brownf8754332011-06-15 17:44:52 -070030
Jeff Brown4ac87152011-07-24 12:22:58 -070031 PRINT_ALL_INFO = (1U << 8) - 1,
Jeff Brownf8754332011-06-15 17:44:52 -070032
33 PRINT_LABELS = 1U << 16,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034};
35
Jeff Brownf8754332011-06-15 17:44:52 -070036static const char *get_label(const struct label *labels, int value)
37{
38 while(labels->name && value != labels->value) {
39 labels++;
40 }
41 return labels->name;
42}
43
44static int print_input_props(int fd)
45{
46 uint8_t bits[INPUT_PROP_CNT / 8];
47 int i, j;
48 int res;
49 int count;
50 const char *bit_label;
51
52 printf(" input props:\n");
53 res = ioctl(fd, EVIOCGPROP(sizeof(bits)), bits);
54 if(res < 0) {
55 printf(" <not available\n");
56 return 1;
57 }
58 count = 0;
59 for(i = 0; i < res; i++) {
60 for(j = 0; j < 8; j++) {
61 if (bits[i] & 1 << j) {
62 bit_label = get_label(input_prop_labels, i * 8 + j);
63 if(bit_label)
64 printf(" %s\n", bit_label);
65 else
66 printf(" %04x\n", i * 8 + j);
67 count++;
68 }
69 }
70 }
71 if (!count)
72 printf(" <none>\n");
73 return 0;
74}
75
76static int print_possible_events(int fd, int print_flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077{
78 uint8_t *bits = NULL;
79 ssize_t bits_size = 0;
Dianne Hackborn477b4302009-04-08 15:43:31 -070080 const char* label;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 int i, j, k;
82 int res, res2;
Jeff Brownf8754332011-06-15 17:44:52 -070083 struct label* bit_labels;
84 const char *bit_label;
85
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 printf(" events:\n");
Jeff Brownf6d0f8a2011-06-29 20:52:08 -070087 for(i = EV_KEY; i <= EV_MAX; i++) { // skip EV_SYN since we cannot query its available codes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088 int count = 0;
89 while(1) {
90 res = ioctl(fd, EVIOCGBIT(i, bits_size), bits);
91 if(res < bits_size)
92 break;
93 bits_size = res + 16;
94 bits = realloc(bits, bits_size * 2);
95 if(bits == NULL) {
Jeff Brownf8754332011-06-15 17:44:52 -070096 fprintf(stderr, "failed to allocate buffer of size %d\n", (int)bits_size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097 return 1;
98 }
99 }
Dianne Hackborn477b4302009-04-08 15:43:31 -0700100 res2 = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 switch(i) {
102 case EV_KEY:
103 res2 = ioctl(fd, EVIOCGKEY(res), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700104 label = "KEY";
Jeff Brownf8754332011-06-15 17:44:52 -0700105 bit_labels = key_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700106 break;
107 case EV_REL:
108 label = "REL";
Jeff Brownf8754332011-06-15 17:44:52 -0700109 bit_labels = rel_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700110 break;
111 case EV_ABS:
112 label = "ABS";
Jeff Brownf8754332011-06-15 17:44:52 -0700113 bit_labels = abs_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700114 break;
115 case EV_MSC:
116 label = "MSC";
Jeff Brownf8754332011-06-15 17:44:52 -0700117 bit_labels = msc_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 break;
119 case EV_LED:
120 res2 = ioctl(fd, EVIOCGLED(res), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700121 label = "LED";
Jeff Brownf8754332011-06-15 17:44:52 -0700122 bit_labels = led_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 break;
124 case EV_SND:
125 res2 = ioctl(fd, EVIOCGSND(res), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700126 label = "SND";
Jeff Brownf8754332011-06-15 17:44:52 -0700127 bit_labels = snd_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 break;
129 case EV_SW:
130 res2 = ioctl(fd, EVIOCGSW(bits_size), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700131 label = "SW ";
Jeff Brownf8754332011-06-15 17:44:52 -0700132 bit_labels = sw_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700133 break;
134 case EV_REP:
135 label = "REP";
Jeff Brownf8754332011-06-15 17:44:52 -0700136 bit_labels = rep_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700137 break;
138 case EV_FF:
139 label = "FF ";
Jeff Brownf8754332011-06-15 17:44:52 -0700140 bit_labels = ff_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700141 break;
142 case EV_PWR:
143 label = "PWR";
Jeff Brownf8754332011-06-15 17:44:52 -0700144 bit_labels = NULL;
145 break;
146 case EV_FF_STATUS:
147 label = "FFS";
148 bit_labels = ff_status_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 break;
150 default:
151 res2 = 0;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700152 label = "???";
Jeff Brownf8754332011-06-15 17:44:52 -0700153 bit_labels = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 }
155 for(j = 0; j < res; j++) {
156 for(k = 0; k < 8; k++)
157 if(bits[j] & 1 << k) {
158 char down;
159 if(j < res2 && (bits[j + bits_size] & 1 << k))
160 down = '*';
161 else
162 down = ' ';
163 if(count == 0)
Dianne Hackborn477b4302009-04-08 15:43:31 -0700164 printf(" %s (%04x):", label, i);
Jeff Brownf8754332011-06-15 17:44:52 -0700165 else if((count & (print_flags & PRINT_LABELS ? 0x3 : 0x7)) == 0 || i == EV_ABS)
Dianne Hackborn477b4302009-04-08 15:43:31 -0700166 printf("\n ");
Jeff Brownf8754332011-06-15 17:44:52 -0700167 if(bit_labels && (print_flags & PRINT_LABELS)) {
168 bit_label = get_label(bit_labels, j * 8 + k);
169 if(bit_label)
Elliott Hughesccecf142014-01-16 10:53:11 -0800170 printf(" %.20s%c%*s", bit_label, down, (int) (20 - strlen(bit_label)), "");
Jeff Brownf8754332011-06-15 17:44:52 -0700171 else
172 printf(" %04x%c ", j * 8 + k, down);
173 } else {
174 printf(" %04x%c", j * 8 + k, down);
175 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 if(i == EV_ABS) {
177 struct input_absinfo abs;
178 if(ioctl(fd, EVIOCGABS(j * 8 + k), &abs) == 0) {
Jeff Brown9de370e2011-08-16 17:18:54 -0700179 printf(" : value %d, min %d, max %d, fuzz %d, flat %d, resolution %d",
180 abs.value, abs.minimum, abs.maximum, abs.fuzz, abs.flat,
181 abs.resolution);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 }
183 }
184 count++;
185 }
186 }
187 if(count)
188 printf("\n");
189 }
190 free(bits);
191 return 0;
192}
193
Jeff Brownf8754332011-06-15 17:44:52 -0700194static void print_event(int type, int code, int value, int print_flags)
195{
196 const char *type_label, *code_label, *value_label;
197
198 if (print_flags & PRINT_LABELS) {
199 type_label = get_label(ev_labels, type);
200 code_label = NULL;
201 value_label = NULL;
202
203 switch(type) {
204 case EV_SYN:
205 code_label = get_label(syn_labels, code);
206 break;
207 case EV_KEY:
208 code_label = get_label(key_labels, code);
209 value_label = get_label(key_value_labels, value);
210 break;
211 case EV_REL:
212 code_label = get_label(rel_labels, code);
213 break;
214 case EV_ABS:
215 code_label = get_label(abs_labels, code);
216 switch(code) {
217 case ABS_MT_TOOL_TYPE:
218 value_label = get_label(mt_tool_labels, value);
219 }
220 break;
221 case EV_MSC:
222 code_label = get_label(msc_labels, code);
223 break;
224 case EV_LED:
225 code_label = get_label(led_labels, code);
226 break;
227 case EV_SND:
228 code_label = get_label(snd_labels, code);
229 break;
230 case EV_SW:
231 code_label = get_label(sw_labels, code);
232 break;
233 case EV_REP:
234 code_label = get_label(rep_labels, code);
235 break;
236 case EV_FF:
237 code_label = get_label(ff_labels, code);
238 break;
239 case EV_FF_STATUS:
240 code_label = get_label(ff_status_labels, code);
241 break;
242 }
243
244 if (type_label)
245 printf("%-12.12s", type_label);
246 else
247 printf("%04x ", type);
248 if (code_label)
249 printf(" %-20.20s", code_label);
250 else
251 printf(" %04x ", code);
252 if (value_label)
253 printf(" %-20.20s", value_label);
254 else
Jeff Brownf6d0f8a2011-06-29 20:52:08 -0700255 printf(" %08x ", value);
Jeff Brownf8754332011-06-15 17:44:52 -0700256 } else {
257 printf("%04x %04x %08x", type, code, value);
258 }
259}
260
Jeff Brown4ac87152011-07-24 12:22:58 -0700261static void print_hid_descriptor(int bus, int vendor, int product)
262{
263 const char *dirname = "/sys/kernel/debug/hid";
264 char prefix[16];
265 DIR *dir;
266 struct dirent *de;
267 char filename[PATH_MAX];
268 FILE *file;
269 char line[2048];
270
271 snprintf(prefix, sizeof(prefix), "%04X:%04X:%04X.", bus, vendor, product);
272
273 dir = opendir(dirname);
274 if(dir == NULL)
275 return;
276 while((de = readdir(dir))) {
277 if (strstr(de->d_name, prefix) == de->d_name) {
278 snprintf(filename, sizeof(filename), "%s/%s/rdesc", dirname, de->d_name);
279
280 file = fopen(filename, "r");
281 if (file) {
282 printf(" HID descriptor: %s\n\n", de->d_name);
283 while (fgets(line, sizeof(line), file)) {
284 fputs(" ", stdout);
285 fputs(line, stdout);
286 }
287 fclose(file);
288 puts("");
289 }
290 }
291 }
292 closedir(dir);
293}
294
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295static int open_device(const char *device, int print_flags)
296{
297 int version;
298 int fd;
Sasha Levitskiy140b1352013-10-23 11:31:02 -0700299 int clkid = CLOCK_MONOTONIC;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 struct pollfd *new_ufds;
301 char **new_device_names;
302 char name[80];
303 char location[80];
304 char idstr[80];
305 struct input_id id;
306
307 fd = open(device, O_RDWR);
308 if(fd < 0) {
309 if(print_flags & PRINT_DEVICE_ERRORS)
310 fprintf(stderr, "could not open %s, %s\n", device, strerror(errno));
311 return -1;
312 }
313
314 if(ioctl(fd, EVIOCGVERSION, &version)) {
315 if(print_flags & PRINT_DEVICE_ERRORS)
316 fprintf(stderr, "could not get driver version for %s, %s\n", device, strerror(errno));
317 return -1;
318 }
319 if(ioctl(fd, EVIOCGID, &id)) {
320 if(print_flags & PRINT_DEVICE_ERRORS)
321 fprintf(stderr, "could not get driver id for %s, %s\n", device, strerror(errno));
322 return -1;
323 }
324 name[sizeof(name) - 1] = '\0';
325 location[sizeof(location) - 1] = '\0';
326 idstr[sizeof(idstr) - 1] = '\0';
327 if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
328 //fprintf(stderr, "could not get device name for %s, %s\n", device, strerror(errno));
329 name[0] = '\0';
330 }
331 if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
332 //fprintf(stderr, "could not get location for %s, %s\n", device, strerror(errno));
333 location[0] = '\0';
334 }
335 if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
336 //fprintf(stderr, "could not get idstring for %s, %s\n", device, strerror(errno));
337 idstr[0] = '\0';
338 }
339
Sasha Levitskiy140b1352013-10-23 11:31:02 -0700340 if (ioctl(fd, EVIOCSCLOCKID, &clkid) != 0) {
341 fprintf(stderr, "Can't enable monotonic clock reporting: %s\n", strerror(errno));
342 // a non-fatal error
343 }
344
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 new_ufds = realloc(ufds, sizeof(ufds[0]) * (nfds + 1));
346 if(new_ufds == NULL) {
347 fprintf(stderr, "out of memory\n");
348 return -1;
349 }
350 ufds = new_ufds;
351 new_device_names = realloc(device_names, sizeof(device_names[0]) * (nfds + 1));
352 if(new_device_names == NULL) {
353 fprintf(stderr, "out of memory\n");
354 return -1;
355 }
356 device_names = new_device_names;
357
358 if(print_flags & PRINT_DEVICE)
359 printf("add device %d: %s\n", nfds, device);
360 if(print_flags & PRINT_DEVICE_INFO)
361 printf(" bus: %04x\n"
362 " vendor %04x\n"
363 " product %04x\n"
364 " version %04x\n",
365 id.bustype, id.vendor, id.product, id.version);
366 if(print_flags & PRINT_DEVICE_NAME)
367 printf(" name: \"%s\"\n", name);
368 if(print_flags & PRINT_DEVICE_INFO)
369 printf(" location: \"%s\"\n"
370 " id: \"%s\"\n", location, idstr);
371 if(print_flags & PRINT_VERSION)
372 printf(" version: %d.%d.%d\n",
373 version >> 16, (version >> 8) & 0xff, version & 0xff);
374
375 if(print_flags & PRINT_POSSIBLE_EVENTS) {
Jeff Brownf8754332011-06-15 17:44:52 -0700376 print_possible_events(fd, print_flags);
377 }
378
379 if(print_flags & PRINT_INPUT_PROPS) {
380 print_input_props(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 }
Jeff Brown4ac87152011-07-24 12:22:58 -0700382 if(print_flags & PRINT_HID_DESCRIPTOR) {
383 print_hid_descriptor(id.bustype, id.vendor, id.product);
384 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
386 ufds[nfds].fd = fd;
387 ufds[nfds].events = POLLIN;
388 device_names[nfds] = strdup(device);
389 nfds++;
390
391 return 0;
392}
393
394int close_device(const char *device, int print_flags)
395{
396 int i;
397 for(i = 1; i < nfds; i++) {
398 if(strcmp(device_names[i], device) == 0) {
399 int count = nfds - i - 1;
400 if(print_flags & PRINT_DEVICE)
401 printf("remove device %d: %s\n", i, device);
402 free(device_names[i]);
403 memmove(device_names + i, device_names + i + 1, sizeof(device_names[0]) * count);
404 memmove(ufds + i, ufds + i + 1, sizeof(ufds[0]) * count);
405 nfds--;
406 return 0;
407 }
408 }
409 if(print_flags & PRINT_DEVICE_ERRORS)
410 fprintf(stderr, "remote device: %s not found\n", device);
411 return -1;
412}
413
414static int read_notify(const char *dirname, int nfd, int print_flags)
415{
416 int res;
417 char devname[PATH_MAX];
418 char *filename;
419 char event_buf[512];
420 int event_size;
421 int event_pos = 0;
422 struct inotify_event *event;
423
424 res = read(nfd, event_buf, sizeof(event_buf));
425 if(res < (int)sizeof(*event)) {
426 if(errno == EINTR)
427 return 0;
428 fprintf(stderr, "could not get event, %s\n", strerror(errno));
429 return 1;
430 }
431 //printf("got %d bytes of event information\n", res);
432
433 strcpy(devname, dirname);
434 filename = devname + strlen(devname);
435 *filename++ = '/';
436
437 while(res >= (int)sizeof(*event)) {
438 event = (struct inotify_event *)(event_buf + event_pos);
439 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
440 if(event->len) {
441 strcpy(filename, event->name);
442 if(event->mask & IN_CREATE) {
443 open_device(devname, print_flags);
444 }
445 else {
446 close_device(devname, print_flags);
447 }
448 }
449 event_size = sizeof(*event) + event->len;
450 res -= event_size;
451 event_pos += event_size;
452 }
453 return 0;
454}
455
456static int scan_dir(const char *dirname, int print_flags)
457{
458 char devname[PATH_MAX];
459 char *filename;
460 DIR *dir;
461 struct dirent *de;
462 dir = opendir(dirname);
463 if(dir == NULL)
464 return -1;
465 strcpy(devname, dirname);
466 filename = devname + strlen(devname);
467 *filename++ = '/';
468 while((de = readdir(dir))) {
469 if(de->d_name[0] == '.' &&
470 (de->d_name[1] == '\0' ||
471 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
472 continue;
473 strcpy(filename, de->d_name);
474 open_device(devname, print_flags);
475 }
476 closedir(dir);
477 return 0;
478}
479
Sasha Levitskiy140b1352013-10-23 11:31:02 -0700480static void usage(char *name)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481{
Sasha Levitskiy140b1352013-10-23 11:31:02 -0700482 fprintf(stderr, "Usage: %s [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]\n", name);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700483 fprintf(stderr, " -t: show time stamps\n");
484 fprintf(stderr, " -n: don't print newlines\n");
485 fprintf(stderr, " -s: print switch states for given bits\n");
486 fprintf(stderr, " -S: print all switch states\n");
Jeff Brownf8754332011-06-15 17:44:52 -0700487 fprintf(stderr, " -v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)\n");
Jeff Brown4ac87152011-07-24 12:22:58 -0700488 fprintf(stderr, " -d: show HID descriptor, if available\n");
Dianne Hackborn477b4302009-04-08 15:43:31 -0700489 fprintf(stderr, " -p: show possible events (errs, dev, name, pos. events)\n");
Jeff Brownf8754332011-06-15 17:44:52 -0700490 fprintf(stderr, " -i: show all device info and possible events\n");
491 fprintf(stderr, " -l: label event types and names in plain text\n");
Dianne Hackborn477b4302009-04-08 15:43:31 -0700492 fprintf(stderr, " -q: quiet (clear verbosity mask)\n");
493 fprintf(stderr, " -c: print given number of events then exit\n");
494 fprintf(stderr, " -r: print rate events are received\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495}
496
497int getevent_main(int argc, char *argv[])
498{
499 int c;
500 int i;
501 int res;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 int get_time = 0;
503 int print_device = 0;
504 char *newline = "\n";
505 uint16_t get_switch = 0;
506 struct input_event event;
Jeff Brownf8754332011-06-15 17:44:52 -0700507 int print_flags = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 int print_flags_set = 0;
509 int dont_block = -1;
510 int event_count = 0;
511 int sync_rate = 0;
512 int64_t last_sync_time = 0;
513 const char *device = NULL;
514 const char *device_path = "/dev/input";
515
516 opterr = 0;
517 do {
Jeff Brown4ac87152011-07-24 12:22:58 -0700518 c = getopt(argc, argv, "tns:Sv::dpilqc:rh");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 if (c == EOF)
520 break;
521 switch (c) {
522 case 't':
523 get_time = 1;
524 break;
525 case 'n':
526 newline = "";
527 break;
528 case 's':
529 get_switch = strtoul(optarg, NULL, 0);
530 if(dont_block == -1)
531 dont_block = 1;
532 break;
533 case 'S':
534 get_switch = ~0;
535 if(dont_block == -1)
536 dont_block = 1;
537 break;
538 case 'v':
539 if(optarg)
Jeff Brownf8754332011-06-15 17:44:52 -0700540 print_flags |= strtoul(optarg, NULL, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 else
542 print_flags |= PRINT_DEVICE | PRINT_DEVICE_NAME | PRINT_DEVICE_INFO | PRINT_VERSION;
543 print_flags_set = 1;
544 break;
Jeff Brown4ac87152011-07-24 12:22:58 -0700545 case 'd':
546 print_flags |= PRINT_HID_DESCRIPTOR;
547 break;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700548 case 'p':
Jeff Brown4ac87152011-07-24 12:22:58 -0700549 print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE
550 | PRINT_DEVICE_NAME | PRINT_POSSIBLE_EVENTS | PRINT_INPUT_PROPS;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700551 print_flags_set = 1;
552 if(dont_block == -1)
553 dont_block = 1;
554 break;
Jeff Brownf8754332011-06-15 17:44:52 -0700555 case 'i':
556 print_flags |= PRINT_ALL_INFO;
557 print_flags_set = 1;
558 if(dont_block == -1)
559 dont_block = 1;
560 break;
561 case 'l':
562 print_flags |= PRINT_LABELS;
563 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 case 'q':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565 print_flags_set = 1;
566 break;
567 case 'c':
568 event_count = atoi(optarg);
569 dont_block = 0;
570 break;
571 case 'r':
572 sync_rate = 1;
573 break;
574 case '?':
575 fprintf(stderr, "%s: invalid option -%c\n",
576 argv[0], optopt);
577 case 'h':
Sasha Levitskiy140b1352013-10-23 11:31:02 -0700578 usage(argv[0]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 exit(1);
580 }
581 } while (1);
582 if(dont_block == -1)
583 dont_block = 0;
584
585 if (optind + 1 == argc) {
586 device = argv[optind];
587 optind++;
588 }
589 if (optind != argc) {
Sasha Levitskiy140b1352013-10-23 11:31:02 -0700590 usage(argv[0]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591 exit(1);
592 }
593 nfds = 1;
594 ufds = calloc(1, sizeof(ufds[0]));
595 ufds[0].fd = inotify_init();
596 ufds[0].events = POLLIN;
597 if(device) {
598 if(!print_flags_set)
Jeff Brownf8754332011-06-15 17:44:52 -0700599 print_flags |= PRINT_DEVICE_ERRORS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 res = open_device(device, print_flags);
601 if(res < 0) {
602 return 1;
603 }
Jeff Brownf8754332011-06-15 17:44:52 -0700604 } else {
605 if(!print_flags_set)
606 print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE | PRINT_DEVICE_NAME;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 print_device = 1;
608 res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE);
609 if(res < 0) {
610 fprintf(stderr, "could not add watch for %s, %s\n", device_path, strerror(errno));
611 return 1;
612 }
613 res = scan_dir(device_path, print_flags);
614 if(res < 0) {
615 fprintf(stderr, "scan dir failed for %s\n", device_path);
616 return 1;
617 }
618 }
619
620 if(get_switch) {
621 for(i = 1; i < nfds; i++) {
622 uint16_t sw;
623 res = ioctl(ufds[i].fd, EVIOCGSW(1), &sw);
624 if(res < 0) {
625 fprintf(stderr, "could not get switch state, %s\n", strerror(errno));
626 return 1;
627 }
628 sw &= get_switch;
629 printf("%04x%s", sw, newline);
630 }
631 }
632
633 if(dont_block)
634 return 0;
635
636 while(1) {
Mark Salyzynaa907762014-05-08 09:31:43 -0700637 //int pollres =
638 poll(ufds, nfds, -1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 //printf("poll %d, returned %d\n", nfds, pollres);
640 if(ufds[0].revents & POLLIN) {
641 read_notify(device_path, ufds[0].fd, print_flags);
642 }
643 for(i = 1; i < nfds; i++) {
644 if(ufds[i].revents) {
645 if(ufds[i].revents & POLLIN) {
646 res = read(ufds[i].fd, &event, sizeof(event));
647 if(res < (int)sizeof(event)) {
648 fprintf(stderr, "could not get event\n");
649 return 1;
650 }
651 if(get_time) {
Jeff Browne4e21792011-11-23 15:02:41 -0800652 printf("[%8ld.%06ld] ", event.time.tv_sec, event.time.tv_usec);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 }
654 if(print_device)
655 printf("%s: ", device_names[i]);
Jeff Brownf8754332011-06-15 17:44:52 -0700656 print_event(event.type, event.code, event.value, print_flags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 if(sync_rate && event.type == 0 && event.code == 0) {
658 int64_t now = event.time.tv_sec * 1000000LL + event.time.tv_usec;
659 if(last_sync_time)
660 printf(" rate %lld", 1000000LL / (now - last_sync_time));
661 last_sync_time = now;
662 }
663 printf("%s", newline);
664 if(event_count && --event_count == 0)
665 return 0;
666 }
667 }
668 }
669 }
670
671 return 0;
672}