blob: e3ab41a66c1d5981981131f8672826b2a57d7c33 [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>
13
Jeff Brownf8754332011-06-15 17:44:52 -070014#include "getevent.h"
15
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016static struct pollfd *ufds;
17static char **device_names;
18static int nfds;
19
20enum {
21 PRINT_DEVICE_ERRORS = 1U << 0,
22 PRINT_DEVICE = 1U << 1,
23 PRINT_DEVICE_NAME = 1U << 2,
24 PRINT_DEVICE_INFO = 1U << 3,
25 PRINT_VERSION = 1U << 4,
26 PRINT_POSSIBLE_EVENTS = 1U << 5,
Jeff Brownf8754332011-06-15 17:44:52 -070027 PRINT_INPUT_PROPS = 1U << 6,
Jeff Brown4ac87152011-07-24 12:22:58 -070028 PRINT_HID_DESCRIPTOR = 1U << 7,
Jeff Brownf8754332011-06-15 17:44:52 -070029
Jeff Brown4ac87152011-07-24 12:22:58 -070030 PRINT_ALL_INFO = (1U << 8) - 1,
Jeff Brownf8754332011-06-15 17:44:52 -070031
32 PRINT_LABELS = 1U << 16,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033};
34
Jeff Brownf8754332011-06-15 17:44:52 -070035static const char *get_label(const struct label *labels, int value)
36{
37 while(labels->name && value != labels->value) {
38 labels++;
39 }
40 return labels->name;
41}
42
43static int print_input_props(int fd)
44{
45 uint8_t bits[INPUT_PROP_CNT / 8];
46 int i, j;
47 int res;
48 int count;
49 const char *bit_label;
50
51 printf(" input props:\n");
52 res = ioctl(fd, EVIOCGPROP(sizeof(bits)), bits);
53 if(res < 0) {
54 printf(" <not available\n");
55 return 1;
56 }
57 count = 0;
58 for(i = 0; i < res; i++) {
59 for(j = 0; j < 8; j++) {
60 if (bits[i] & 1 << j) {
61 bit_label = get_label(input_prop_labels, i * 8 + j);
62 if(bit_label)
63 printf(" %s\n", bit_label);
64 else
65 printf(" %04x\n", i * 8 + j);
66 count++;
67 }
68 }
69 }
70 if (!count)
71 printf(" <none>\n");
72 return 0;
73}
74
75static int print_possible_events(int fd, int print_flags)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076{
77 uint8_t *bits = NULL;
78 ssize_t bits_size = 0;
Dianne Hackborn477b4302009-04-08 15:43:31 -070079 const char* label;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 int i, j, k;
81 int res, res2;
Jeff Brownf8754332011-06-15 17:44:52 -070082 struct label* bit_labels;
83 const char *bit_label;
84
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 printf(" events:\n");
Jeff Brownf6d0f8a2011-06-29 20:52:08 -070086 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 -080087 int count = 0;
88 while(1) {
89 res = ioctl(fd, EVIOCGBIT(i, bits_size), bits);
90 if(res < bits_size)
91 break;
92 bits_size = res + 16;
93 bits = realloc(bits, bits_size * 2);
94 if(bits == NULL) {
Jeff Brownf8754332011-06-15 17:44:52 -070095 fprintf(stderr, "failed to allocate buffer of size %d\n", (int)bits_size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 return 1;
97 }
98 }
Dianne Hackborn477b4302009-04-08 15:43:31 -070099 res2 = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 switch(i) {
101 case EV_KEY:
102 res2 = ioctl(fd, EVIOCGKEY(res), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700103 label = "KEY";
Jeff Brownf8754332011-06-15 17:44:52 -0700104 bit_labels = key_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700105 break;
106 case EV_REL:
107 label = "REL";
Jeff Brownf8754332011-06-15 17:44:52 -0700108 bit_labels = rel_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700109 break;
110 case EV_ABS:
111 label = "ABS";
Jeff Brownf8754332011-06-15 17:44:52 -0700112 bit_labels = abs_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700113 break;
114 case EV_MSC:
115 label = "MSC";
Jeff Brownf8754332011-06-15 17:44:52 -0700116 bit_labels = msc_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 break;
118 case EV_LED:
119 res2 = ioctl(fd, EVIOCGLED(res), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700120 label = "LED";
Jeff Brownf8754332011-06-15 17:44:52 -0700121 bit_labels = led_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 break;
123 case EV_SND:
124 res2 = ioctl(fd, EVIOCGSND(res), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700125 label = "SND";
Jeff Brownf8754332011-06-15 17:44:52 -0700126 bit_labels = snd_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 break;
128 case EV_SW:
129 res2 = ioctl(fd, EVIOCGSW(bits_size), bits + bits_size);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700130 label = "SW ";
Jeff Brownf8754332011-06-15 17:44:52 -0700131 bit_labels = sw_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700132 break;
133 case EV_REP:
134 label = "REP";
Jeff Brownf8754332011-06-15 17:44:52 -0700135 bit_labels = rep_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700136 break;
137 case EV_FF:
138 label = "FF ";
Jeff Brownf8754332011-06-15 17:44:52 -0700139 bit_labels = ff_labels;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700140 break;
141 case EV_PWR:
142 label = "PWR";
Jeff Brownf8754332011-06-15 17:44:52 -0700143 bit_labels = NULL;
144 break;
145 case EV_FF_STATUS:
146 label = "FFS";
147 bit_labels = ff_status_labels;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 break;
149 default:
150 res2 = 0;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700151 label = "???";
Jeff Brownf8754332011-06-15 17:44:52 -0700152 bit_labels = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 }
154 for(j = 0; j < res; j++) {
155 for(k = 0; k < 8; k++)
156 if(bits[j] & 1 << k) {
157 char down;
158 if(j < res2 && (bits[j + bits_size] & 1 << k))
159 down = '*';
160 else
161 down = ' ';
162 if(count == 0)
Dianne Hackborn477b4302009-04-08 15:43:31 -0700163 printf(" %s (%04x):", label, i);
Jeff Brownf8754332011-06-15 17:44:52 -0700164 else if((count & (print_flags & PRINT_LABELS ? 0x3 : 0x7)) == 0 || i == EV_ABS)
Dianne Hackborn477b4302009-04-08 15:43:31 -0700165 printf("\n ");
Jeff Brownf8754332011-06-15 17:44:52 -0700166 if(bit_labels && (print_flags & PRINT_LABELS)) {
167 bit_label = get_label(bit_labels, j * 8 + k);
168 if(bit_label)
169 printf(" %.20s%c%*s", bit_label, down, 20 - strlen(bit_label), "");
170 else
171 printf(" %04x%c ", j * 8 + k, down);
172 } else {
173 printf(" %04x%c", j * 8 + k, down);
174 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 if(i == EV_ABS) {
176 struct input_absinfo abs;
177 if(ioctl(fd, EVIOCGABS(j * 8 + k), &abs) == 0) {
Jeff Brownf8754332011-06-15 17:44:52 -0700178 printf(" : value %d, min %d, max %d, fuzz %d flat %d", abs.value, abs.minimum, abs.maximum, abs.fuzz, abs.flat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 }
180 }
181 count++;
182 }
183 }
184 if(count)
185 printf("\n");
186 }
187 free(bits);
188 return 0;
189}
190
Jeff Brownf8754332011-06-15 17:44:52 -0700191static void print_event(int type, int code, int value, int print_flags)
192{
193 const char *type_label, *code_label, *value_label;
194
195 if (print_flags & PRINT_LABELS) {
196 type_label = get_label(ev_labels, type);
197 code_label = NULL;
198 value_label = NULL;
199
200 switch(type) {
201 case EV_SYN:
202 code_label = get_label(syn_labels, code);
203 break;
204 case EV_KEY:
205 code_label = get_label(key_labels, code);
206 value_label = get_label(key_value_labels, value);
207 break;
208 case EV_REL:
209 code_label = get_label(rel_labels, code);
210 break;
211 case EV_ABS:
212 code_label = get_label(abs_labels, code);
213 switch(code) {
214 case ABS_MT_TOOL_TYPE:
215 value_label = get_label(mt_tool_labels, value);
216 }
217 break;
218 case EV_MSC:
219 code_label = get_label(msc_labels, code);
220 break;
221 case EV_LED:
222 code_label = get_label(led_labels, code);
223 break;
224 case EV_SND:
225 code_label = get_label(snd_labels, code);
226 break;
227 case EV_SW:
228 code_label = get_label(sw_labels, code);
229 break;
230 case EV_REP:
231 code_label = get_label(rep_labels, code);
232 break;
233 case EV_FF:
234 code_label = get_label(ff_labels, code);
235 break;
236 case EV_FF_STATUS:
237 code_label = get_label(ff_status_labels, code);
238 break;
239 }
240
241 if (type_label)
242 printf("%-12.12s", type_label);
243 else
244 printf("%04x ", type);
245 if (code_label)
246 printf(" %-20.20s", code_label);
247 else
248 printf(" %04x ", code);
249 if (value_label)
250 printf(" %-20.20s", value_label);
251 else
Jeff Brownf6d0f8a2011-06-29 20:52:08 -0700252 printf(" %08x ", value);
Jeff Brownf8754332011-06-15 17:44:52 -0700253 } else {
254 printf("%04x %04x %08x", type, code, value);
255 }
256}
257
Jeff Brown4ac87152011-07-24 12:22:58 -0700258static void print_hid_descriptor(int bus, int vendor, int product)
259{
260 const char *dirname = "/sys/kernel/debug/hid";
261 char prefix[16];
262 DIR *dir;
263 struct dirent *de;
264 char filename[PATH_MAX];
265 FILE *file;
266 char line[2048];
267
268 snprintf(prefix, sizeof(prefix), "%04X:%04X:%04X.", bus, vendor, product);
269
270 dir = opendir(dirname);
271 if(dir == NULL)
272 return;
273 while((de = readdir(dir))) {
274 if (strstr(de->d_name, prefix) == de->d_name) {
275 snprintf(filename, sizeof(filename), "%s/%s/rdesc", dirname, de->d_name);
276
277 file = fopen(filename, "r");
278 if (file) {
279 printf(" HID descriptor: %s\n\n", de->d_name);
280 while (fgets(line, sizeof(line), file)) {
281 fputs(" ", stdout);
282 fputs(line, stdout);
283 }
284 fclose(file);
285 puts("");
286 }
287 }
288 }
289 closedir(dir);
290}
291
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292static int open_device(const char *device, int print_flags)
293{
294 int version;
295 int fd;
296 struct pollfd *new_ufds;
297 char **new_device_names;
298 char name[80];
299 char location[80];
300 char idstr[80];
301 struct input_id id;
302
303 fd = open(device, O_RDWR);
304 if(fd < 0) {
305 if(print_flags & PRINT_DEVICE_ERRORS)
306 fprintf(stderr, "could not open %s, %s\n", device, strerror(errno));
307 return -1;
308 }
309
310 if(ioctl(fd, EVIOCGVERSION, &version)) {
311 if(print_flags & PRINT_DEVICE_ERRORS)
312 fprintf(stderr, "could not get driver version for %s, %s\n", device, strerror(errno));
313 return -1;
314 }
315 if(ioctl(fd, EVIOCGID, &id)) {
316 if(print_flags & PRINT_DEVICE_ERRORS)
317 fprintf(stderr, "could not get driver id for %s, %s\n", device, strerror(errno));
318 return -1;
319 }
320 name[sizeof(name) - 1] = '\0';
321 location[sizeof(location) - 1] = '\0';
322 idstr[sizeof(idstr) - 1] = '\0';
323 if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
324 //fprintf(stderr, "could not get device name for %s, %s\n", device, strerror(errno));
325 name[0] = '\0';
326 }
327 if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
328 //fprintf(stderr, "could not get location for %s, %s\n", device, strerror(errno));
329 location[0] = '\0';
330 }
331 if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
332 //fprintf(stderr, "could not get idstring for %s, %s\n", device, strerror(errno));
333 idstr[0] = '\0';
334 }
335
336 new_ufds = realloc(ufds, sizeof(ufds[0]) * (nfds + 1));
337 if(new_ufds == NULL) {
338 fprintf(stderr, "out of memory\n");
339 return -1;
340 }
341 ufds = new_ufds;
342 new_device_names = realloc(device_names, sizeof(device_names[0]) * (nfds + 1));
343 if(new_device_names == NULL) {
344 fprintf(stderr, "out of memory\n");
345 return -1;
346 }
347 device_names = new_device_names;
348
349 if(print_flags & PRINT_DEVICE)
350 printf("add device %d: %s\n", nfds, device);
351 if(print_flags & PRINT_DEVICE_INFO)
352 printf(" bus: %04x\n"
353 " vendor %04x\n"
354 " product %04x\n"
355 " version %04x\n",
356 id.bustype, id.vendor, id.product, id.version);
357 if(print_flags & PRINT_DEVICE_NAME)
358 printf(" name: \"%s\"\n", name);
359 if(print_flags & PRINT_DEVICE_INFO)
360 printf(" location: \"%s\"\n"
361 " id: \"%s\"\n", location, idstr);
362 if(print_flags & PRINT_VERSION)
363 printf(" version: %d.%d.%d\n",
364 version >> 16, (version >> 8) & 0xff, version & 0xff);
365
366 if(print_flags & PRINT_POSSIBLE_EVENTS) {
Jeff Brownf8754332011-06-15 17:44:52 -0700367 print_possible_events(fd, print_flags);
368 }
369
370 if(print_flags & PRINT_INPUT_PROPS) {
371 print_input_props(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 }
Jeff Brown4ac87152011-07-24 12:22:58 -0700373 if(print_flags & PRINT_HID_DESCRIPTOR) {
374 print_hid_descriptor(id.bustype, id.vendor, id.product);
375 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
377 ufds[nfds].fd = fd;
378 ufds[nfds].events = POLLIN;
379 device_names[nfds] = strdup(device);
380 nfds++;
381
382 return 0;
383}
384
385int close_device(const char *device, int print_flags)
386{
387 int i;
388 for(i = 1; i < nfds; i++) {
389 if(strcmp(device_names[i], device) == 0) {
390 int count = nfds - i - 1;
391 if(print_flags & PRINT_DEVICE)
392 printf("remove device %d: %s\n", i, device);
393 free(device_names[i]);
394 memmove(device_names + i, device_names + i + 1, sizeof(device_names[0]) * count);
395 memmove(ufds + i, ufds + i + 1, sizeof(ufds[0]) * count);
396 nfds--;
397 return 0;
398 }
399 }
400 if(print_flags & PRINT_DEVICE_ERRORS)
401 fprintf(stderr, "remote device: %s not found\n", device);
402 return -1;
403}
404
405static int read_notify(const char *dirname, int nfd, int print_flags)
406{
407 int res;
408 char devname[PATH_MAX];
409 char *filename;
410 char event_buf[512];
411 int event_size;
412 int event_pos = 0;
413 struct inotify_event *event;
414
415 res = read(nfd, event_buf, sizeof(event_buf));
416 if(res < (int)sizeof(*event)) {
417 if(errno == EINTR)
418 return 0;
419 fprintf(stderr, "could not get event, %s\n", strerror(errno));
420 return 1;
421 }
422 //printf("got %d bytes of event information\n", res);
423
424 strcpy(devname, dirname);
425 filename = devname + strlen(devname);
426 *filename++ = '/';
427
428 while(res >= (int)sizeof(*event)) {
429 event = (struct inotify_event *)(event_buf + event_pos);
430 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
431 if(event->len) {
432 strcpy(filename, event->name);
433 if(event->mask & IN_CREATE) {
434 open_device(devname, print_flags);
435 }
436 else {
437 close_device(devname, print_flags);
438 }
439 }
440 event_size = sizeof(*event) + event->len;
441 res -= event_size;
442 event_pos += event_size;
443 }
444 return 0;
445}
446
447static int scan_dir(const char *dirname, int print_flags)
448{
449 char devname[PATH_MAX];
450 char *filename;
451 DIR *dir;
452 struct dirent *de;
453 dir = opendir(dirname);
454 if(dir == NULL)
455 return -1;
456 strcpy(devname, dirname);
457 filename = devname + strlen(devname);
458 *filename++ = '/';
459 while((de = readdir(dir))) {
460 if(de->d_name[0] == '.' &&
461 (de->d_name[1] == '\0' ||
462 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
463 continue;
464 strcpy(filename, de->d_name);
465 open_device(devname, print_flags);
466 }
467 closedir(dir);
468 return 0;
469}
470
471static void usage(int argc, char *argv[])
472{
Jeff Brown4ac87152011-07-24 12:22:58 -0700473 fprintf(stderr, "Usage: %s [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]\n", argv[0]);
Dianne Hackborn477b4302009-04-08 15:43:31 -0700474 fprintf(stderr, " -t: show time stamps\n");
475 fprintf(stderr, " -n: don't print newlines\n");
476 fprintf(stderr, " -s: print switch states for given bits\n");
477 fprintf(stderr, " -S: print all switch states\n");
Jeff Brownf8754332011-06-15 17:44:52 -0700478 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 -0700479 fprintf(stderr, " -d: show HID descriptor, if available\n");
Dianne Hackborn477b4302009-04-08 15:43:31 -0700480 fprintf(stderr, " -p: show possible events (errs, dev, name, pos. events)\n");
Jeff Brownf8754332011-06-15 17:44:52 -0700481 fprintf(stderr, " -i: show all device info and possible events\n");
482 fprintf(stderr, " -l: label event types and names in plain text\n");
Dianne Hackborn477b4302009-04-08 15:43:31 -0700483 fprintf(stderr, " -q: quiet (clear verbosity mask)\n");
484 fprintf(stderr, " -c: print given number of events then exit\n");
485 fprintf(stderr, " -r: print rate events are received\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486}
487
488int getevent_main(int argc, char *argv[])
489{
490 int c;
491 int i;
492 int res;
493 int pollres;
494 int get_time = 0;
495 int print_device = 0;
496 char *newline = "\n";
497 uint16_t get_switch = 0;
498 struct input_event event;
499 int version;
Jeff Brownf8754332011-06-15 17:44:52 -0700500 int print_flags = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 int print_flags_set = 0;
502 int dont_block = -1;
503 int event_count = 0;
504 int sync_rate = 0;
505 int64_t last_sync_time = 0;
506 const char *device = NULL;
507 const char *device_path = "/dev/input";
508
509 opterr = 0;
510 do {
Jeff Brown4ac87152011-07-24 12:22:58 -0700511 c = getopt(argc, argv, "tns:Sv::dpilqc:rh");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 if (c == EOF)
513 break;
514 switch (c) {
515 case 't':
516 get_time = 1;
517 break;
518 case 'n':
519 newline = "";
520 break;
521 case 's':
522 get_switch = strtoul(optarg, NULL, 0);
523 if(dont_block == -1)
524 dont_block = 1;
525 break;
526 case 'S':
527 get_switch = ~0;
528 if(dont_block == -1)
529 dont_block = 1;
530 break;
531 case 'v':
532 if(optarg)
Jeff Brownf8754332011-06-15 17:44:52 -0700533 print_flags |= strtoul(optarg, NULL, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 else
535 print_flags |= PRINT_DEVICE | PRINT_DEVICE_NAME | PRINT_DEVICE_INFO | PRINT_VERSION;
536 print_flags_set = 1;
537 break;
Jeff Brown4ac87152011-07-24 12:22:58 -0700538 case 'd':
539 print_flags |= PRINT_HID_DESCRIPTOR;
540 break;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700541 case 'p':
Jeff Brown4ac87152011-07-24 12:22:58 -0700542 print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE
543 | PRINT_DEVICE_NAME | PRINT_POSSIBLE_EVENTS | PRINT_INPUT_PROPS;
Dianne Hackborn477b4302009-04-08 15:43:31 -0700544 print_flags_set = 1;
545 if(dont_block == -1)
546 dont_block = 1;
547 break;
Jeff Brownf8754332011-06-15 17:44:52 -0700548 case 'i':
549 print_flags |= PRINT_ALL_INFO;
550 print_flags_set = 1;
551 if(dont_block == -1)
552 dont_block = 1;
553 break;
554 case 'l':
555 print_flags |= PRINT_LABELS;
556 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 case 'q':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 print_flags_set = 1;
559 break;
560 case 'c':
561 event_count = atoi(optarg);
562 dont_block = 0;
563 break;
564 case 'r':
565 sync_rate = 1;
566 break;
567 case '?':
568 fprintf(stderr, "%s: invalid option -%c\n",
569 argv[0], optopt);
570 case 'h':
571 usage(argc, argv);
572 exit(1);
573 }
574 } while (1);
575 if(dont_block == -1)
576 dont_block = 0;
577
578 if (optind + 1 == argc) {
579 device = argv[optind];
580 optind++;
581 }
582 if (optind != argc) {
583 usage(argc, argv);
584 exit(1);
585 }
586 nfds = 1;
587 ufds = calloc(1, sizeof(ufds[0]));
588 ufds[0].fd = inotify_init();
589 ufds[0].events = POLLIN;
590 if(device) {
591 if(!print_flags_set)
Jeff Brownf8754332011-06-15 17:44:52 -0700592 print_flags |= PRINT_DEVICE_ERRORS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 res = open_device(device, print_flags);
594 if(res < 0) {
595 return 1;
596 }
Jeff Brownf8754332011-06-15 17:44:52 -0700597 } else {
598 if(!print_flags_set)
599 print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE | PRINT_DEVICE_NAME;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 print_device = 1;
601 res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE);
602 if(res < 0) {
603 fprintf(stderr, "could not add watch for %s, %s\n", device_path, strerror(errno));
604 return 1;
605 }
606 res = scan_dir(device_path, print_flags);
607 if(res < 0) {
608 fprintf(stderr, "scan dir failed for %s\n", device_path);
609 return 1;
610 }
611 }
612
613 if(get_switch) {
614 for(i = 1; i < nfds; i++) {
615 uint16_t sw;
616 res = ioctl(ufds[i].fd, EVIOCGSW(1), &sw);
617 if(res < 0) {
618 fprintf(stderr, "could not get switch state, %s\n", strerror(errno));
619 return 1;
620 }
621 sw &= get_switch;
622 printf("%04x%s", sw, newline);
623 }
624 }
625
626 if(dont_block)
627 return 0;
628
629 while(1) {
630 pollres = poll(ufds, nfds, -1);
631 //printf("poll %d, returned %d\n", nfds, pollres);
632 if(ufds[0].revents & POLLIN) {
633 read_notify(device_path, ufds[0].fd, print_flags);
634 }
635 for(i = 1; i < nfds; i++) {
636 if(ufds[i].revents) {
637 if(ufds[i].revents & POLLIN) {
638 res = read(ufds[i].fd, &event, sizeof(event));
639 if(res < (int)sizeof(event)) {
640 fprintf(stderr, "could not get event\n");
641 return 1;
642 }
643 if(get_time) {
644 printf("%ld-%ld: ", event.time.tv_sec, event.time.tv_usec);
645 }
646 if(print_device)
647 printf("%s: ", device_names[i]);
Jeff Brownf8754332011-06-15 17:44:52 -0700648 print_event(event.type, event.code, event.value, print_flags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 if(sync_rate && event.type == 0 && event.code == 0) {
650 int64_t now = event.time.tv_sec * 1000000LL + event.time.tv_usec;
651 if(last_sync_time)
652 printf(" rate %lld", 1000000LL / (now - last_sync_time));
653 last_sync_time = now;
654 }
655 printf("%s", newline);
656 if(event_count && --event_count == 0)
657 return 0;
658 }
659 }
660 }
661 }
662
663 return 0;
664}