1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdint.h>
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <dirent.h>
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <fcntl.h>
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/ioctl.h>
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/inotify.h>
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/limits.h>
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/poll.h>
11f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown#include <linux/input.h>
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
14f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown#include "getevent.h"
15f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic struct pollfd *ufds;
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char **device_names;
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int nfds;
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectenum {
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    PRINT_DEVICE_ERRORS     = 1U << 0,
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    PRINT_DEVICE            = 1U << 1,
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    PRINT_DEVICE_NAME       = 1U << 2,
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    PRINT_DEVICE_INFO       = 1U << 3,
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    PRINT_VERSION           = 1U << 4,
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    PRINT_POSSIBLE_EVENTS   = 1U << 5,
27f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    PRINT_INPUT_PROPS       = 1U << 6,
284ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    PRINT_HID_DESCRIPTOR    = 1U << 7,
29f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
304ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    PRINT_ALL_INFO          = (1U << 8) - 1,
31f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
32f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    PRINT_LABELS            = 1U << 16,
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project};
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
35f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brownstatic const char *get_label(const struct label *labels, int value)
36f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown{
37f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    while(labels->name && value != labels->value) {
38f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        labels++;
39f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    }
40f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    return labels->name;
41f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown}
42f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
43f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brownstatic int print_input_props(int fd)
44f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown{
45f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    uint8_t bits[INPUT_PROP_CNT / 8];
46f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    int i, j;
47f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    int res;
48f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    int count;
49f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    const char *bit_label;
50f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
51f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    printf("  input props:\n");
52f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    res = ioctl(fd, EVIOCGPROP(sizeof(bits)), bits);
53f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    if(res < 0) {
54f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        printf("    <not available\n");
55f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        return 1;
56f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    }
57f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    count = 0;
58f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    for(i = 0; i < res; i++) {
59f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        for(j = 0; j < 8; j++) {
60f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            if (bits[i] & 1 << j) {
61f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_label = get_label(input_prop_labels, i * 8 + j);
62f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                if(bit_label)
63f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    printf("    %s\n", bit_label);
64f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                else
65f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    printf("    %04x\n", i * 8 + j);
66f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                count++;
67f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            }
68f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        }
69f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    }
70f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    if (!count)
71f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        printf("    <none>\n");
72f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    return 0;
73f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown}
74f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
75f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brownstatic int print_possible_events(int fd, int print_flags)
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    uint8_t *bits = NULL;
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ssize_t bits_size = 0;
79477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    const char* label;
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int i, j, k;
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int res, res2;
82f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    struct label* bit_labels;
83f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    const char *bit_label;
84f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    printf("  events:\n");
86f6d0f8af1768b3b9b10760bba5583c5e721b64eaJeff Brown    for(i = EV_KEY; i <= EV_MAX; i++) { // skip EV_SYN since we cannot query its available codes
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int count = 0;
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        while(1) {
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            res = ioctl(fd, EVIOCGBIT(i, bits_size), bits);
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(res < bits_size)
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                break;
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            bits_size = res + 16;
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            bits = realloc(bits, bits_size * 2);
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(bits == NULL) {
95f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                fprintf(stderr, "failed to allocate buffer of size %d\n", (int)bits_size);
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                return 1;
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
99477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn        res2 = 0;
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        switch(i) {
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            case EV_KEY:
102dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res2 = ioctl(fd, EVIOCGKEY(res), bits + bits_size);
103477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "KEY";
104f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = key_labels;
105477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                break;
106477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            case EV_REL:
107477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "REL";
108f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = rel_labels;
109477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                break;
110477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            case EV_ABS:
111477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "ABS";
112f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = abs_labels;
113477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                break;
114477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            case EV_MSC:
115477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "MSC";
116f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = msc_labels;
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                break;
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            case EV_LED:
119dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res2 = ioctl(fd, EVIOCGLED(res), bits + bits_size);
120477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "LED";
121f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = led_labels;
122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                break;
123dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            case EV_SND:
124dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res2 = ioctl(fd, EVIOCGSND(res), bits + bits_size);
125477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "SND";
126f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = snd_labels;
127dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                break;
128dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            case EV_SW:
129dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res2 = ioctl(fd, EVIOCGSW(bits_size), bits + bits_size);
130477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "SW ";
131f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = sw_labels;
132477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                break;
133477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            case EV_REP:
134477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "REP";
135f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = rep_labels;
136477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                break;
137477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            case EV_FF:
138477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "FF ";
139f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = ff_labels;
140477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                break;
141477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            case EV_PWR:
142477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "PWR";
143f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = NULL;
144f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
145f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_FF_STATUS:
146f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                label = "FFS";
147f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = ff_status_labels;
148dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                break;
149dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            default:
150dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                res2 = 0;
151477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                label = "???";
152f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                bit_labels = NULL;
153dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
154dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        for(j = 0; j < res; j++) {
155dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            for(k = 0; k < 8; k++)
156dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                if(bits[j] & 1 << k) {
157dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    char down;
158dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(j < res2 && (bits[j + bits_size] & 1 << k))
159dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        down = '*';
160dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    else
161dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        down = ' ';
162dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(count == 0)
163477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                        printf("    %s (%04x):", label, i);
164f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    else if((count & (print_flags & PRINT_LABELS ? 0x3 : 0x7)) == 0 || i == EV_ABS)
165477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                        printf("\n               ");
166f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    if(bit_labels && (print_flags & PRINT_LABELS)) {
167f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                        bit_label = get_label(bit_labels, j * 8 + k);
168f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                        if(bit_label)
169f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                            printf(" %.20s%c%*s", bit_label, down, 20 - strlen(bit_label), "");
170f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                        else
171f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                            printf(" %04x%c                ", j * 8 + k, down);
172f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    } else {
173f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                        printf(" %04x%c", j * 8 + k, down);
174f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    }
175dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(i == EV_ABS) {
176dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        struct input_absinfo abs;
177dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        if(ioctl(fd, EVIOCGABS(j * 8 + k), &abs) == 0) {
1789de370e165350010bc9086347cbd142aecef771eJeff Brown                            printf(" : value %d, min %d, max %d, fuzz %d, flat %d, resolution %d",
1799de370e165350010bc9086347cbd142aecef771eJeff Brown                                abs.value, abs.minimum, abs.maximum, abs.fuzz, abs.flat,
1809de370e165350010bc9086347cbd142aecef771eJeff Brown                                abs.resolution);
181dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        }
182dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    }
183dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    count++;
184dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                }
185dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
186dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(count)
187dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            printf("\n");
188dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
189dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    free(bits);
190dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
191dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
192dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
193f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brownstatic void print_event(int type, int code, int value, int print_flags)
194f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown{
195f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    const char *type_label, *code_label, *value_label;
196f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
197f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    if (print_flags & PRINT_LABELS) {
198f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        type_label = get_label(ev_labels, type);
199f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        code_label = NULL;
200f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        value_label = NULL;
201f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
202f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        switch(type) {
203f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_SYN:
204f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(syn_labels, code);
205f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
206f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_KEY:
207f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(key_labels, code);
208f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                value_label = get_label(key_value_labels, value);
209f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
210f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_REL:
211f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(rel_labels, code);
212f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
213f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_ABS:
214f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(abs_labels, code);
215f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                switch(code) {
216f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    case ABS_MT_TOOL_TYPE:
217f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                        value_label = get_label(mt_tool_labels, value);
218f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                }
219f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
220f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_MSC:
221f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(msc_labels, code);
222f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
223f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_LED:
224f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(led_labels, code);
225f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
226f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_SND:
227f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(snd_labels, code);
228f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
229f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_SW:
230f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(sw_labels, code);
231f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
232f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_REP:
233f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(rep_labels, code);
234f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
235f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_FF:
236f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(ff_labels, code);
237f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
238f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            case EV_FF_STATUS:
239f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                code_label = get_label(ff_status_labels, code);
240f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                break;
241f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        }
242f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
243f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        if (type_label)
244f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            printf("%-12.12s", type_label);
245f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        else
246f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            printf("%04x        ", type);
247f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        if (code_label)
248f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            printf(" %-20.20s", code_label);
249f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        else
250f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            printf(" %04x                ", code);
251f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        if (value_label)
252f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            printf(" %-20.20s", value_label);
253f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        else
254f6d0f8af1768b3b9b10760bba5583c5e721b64eaJeff Brown            printf(" %08x            ", value);
255f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    } else {
256f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        printf("%04x %04x %08x", type, code, value);
257f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    }
258f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown}
259f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
2604ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brownstatic void print_hid_descriptor(int bus, int vendor, int product)
2614ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown{
2624ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    const char *dirname = "/sys/kernel/debug/hid";
2634ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    char prefix[16];
2644ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    DIR *dir;
2654ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    struct dirent *de;
2664ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    char filename[PATH_MAX];
2674ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    FILE *file;
2684ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    char line[2048];
2694ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown
2704ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    snprintf(prefix, sizeof(prefix), "%04X:%04X:%04X.", bus, vendor, product);
2714ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown
2724ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    dir = opendir(dirname);
2734ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    if(dir == NULL)
2744ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown        return;
2754ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    while((de = readdir(dir))) {
2764ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown        if (strstr(de->d_name, prefix) == de->d_name) {
2774ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown            snprintf(filename, sizeof(filename), "%s/%s/rdesc", dirname, de->d_name);
2784ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown
2794ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown            file = fopen(filename, "r");
2804ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown            if (file) {
2814ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                printf("  HID descriptor: %s\n\n", de->d_name);
2824ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                while (fgets(line, sizeof(line), file)) {
2834ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                    fputs("    ", stdout);
2844ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                    fputs(line, stdout);
2854ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                }
2864ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                fclose(file);
2874ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                puts("");
2884ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown            }
2894ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown        }
2904ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    }
2914ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    closedir(dir);
2924ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown}
2934ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown
294dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int open_device(const char *device, int print_flags)
295dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
296dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int version;
297dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd;
298dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct pollfd *new_ufds;
299dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char **new_device_names;
300dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char name[80];
301dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char location[80];
302dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char idstr[80];
303dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct input_id id;
304dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
305dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = open(device, O_RDWR);
306dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(fd < 0) {
307dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(print_flags & PRINT_DEVICE_ERRORS)
308dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "could not open %s, %s\n", device, strerror(errno));
309dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
310dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
311dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
312dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(ioctl(fd, EVIOCGVERSION, &version)) {
313dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(print_flags & PRINT_DEVICE_ERRORS)
314dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "could not get driver version for %s, %s\n", device, strerror(errno));
315dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
316dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
317dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(ioctl(fd, EVIOCGID, &id)) {
318dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(print_flags & PRINT_DEVICE_ERRORS)
319dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "could not get driver id for %s, %s\n", device, strerror(errno));
320dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
321dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
322dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    name[sizeof(name) - 1] = '\0';
323dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    location[sizeof(location) - 1] = '\0';
324dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    idstr[sizeof(idstr) - 1] = '\0';
325dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
326dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        //fprintf(stderr, "could not get device name for %s, %s\n", device, strerror(errno));
327dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        name[0] = '\0';
328dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
329dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
330dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        //fprintf(stderr, "could not get location for %s, %s\n", device, strerror(errno));
331dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        location[0] = '\0';
332dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
333dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
334dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        //fprintf(stderr, "could not get idstring for %s, %s\n", device, strerror(errno));
335dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        idstr[0] = '\0';
336dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
337dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
338dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    new_ufds = realloc(ufds, sizeof(ufds[0]) * (nfds + 1));
339dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(new_ufds == NULL) {
340dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "out of memory\n");
341dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
342dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
343dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ufds = new_ufds;
344dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    new_device_names = realloc(device_names, sizeof(device_names[0]) * (nfds + 1));
345dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(new_device_names == NULL) {
346dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "out of memory\n");
347dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
348dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
349dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    device_names = new_device_names;
350dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
351dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(print_flags & PRINT_DEVICE)
352dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("add device %d: %s\n", nfds, device);
353dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(print_flags & PRINT_DEVICE_INFO)
354dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("  bus:      %04x\n"
355dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project               "  vendor    %04x\n"
356dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project               "  product   %04x\n"
357dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project               "  version   %04x\n",
358dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project               id.bustype, id.vendor, id.product, id.version);
359dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(print_flags & PRINT_DEVICE_NAME)
360dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("  name:     \"%s\"\n", name);
361dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(print_flags & PRINT_DEVICE_INFO)
362dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("  location: \"%s\"\n"
363dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project               "  id:       \"%s\"\n", location, idstr);
364dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(print_flags & PRINT_VERSION)
365dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("  version:  %d.%d.%d\n",
366dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project               version >> 16, (version >> 8) & 0xff, version & 0xff);
367dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
368dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(print_flags & PRINT_POSSIBLE_EVENTS) {
369f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        print_possible_events(fd, print_flags);
370f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    }
371f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown
372f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    if(print_flags & PRINT_INPUT_PROPS) {
373f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        print_input_props(fd);
374dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
3754ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    if(print_flags & PRINT_HID_DESCRIPTOR) {
3764ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown        print_hid_descriptor(id.bustype, id.vendor, id.product);
3774ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    }
378dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
379dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ufds[nfds].fd = fd;
380dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ufds[nfds].events = POLLIN;
381dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    device_names[nfds] = strdup(device);
382dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    nfds++;
383dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
384dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
385dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
386dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
387dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint close_device(const char *device, int print_flags)
388dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
389dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int i;
390dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(i = 1; i < nfds; i++) {
391dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(strcmp(device_names[i], device) == 0) {
392dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            int count = nfds - i - 1;
393dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(print_flags & PRINT_DEVICE)
394dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                printf("remove device %d: %s\n", i, device);
395dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            free(device_names[i]);
396dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            memmove(device_names + i, device_names + i + 1, sizeof(device_names[0]) * count);
397dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            memmove(ufds + i, ufds + i + 1, sizeof(ufds[0]) * count);
398dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            nfds--;
399dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 0;
400dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
401dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
402dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(print_flags & PRINT_DEVICE_ERRORS)
403dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "remote device: %s not found\n", device);
404dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return -1;
405dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
406dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
407dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int read_notify(const char *dirname, int nfd, int print_flags)
408dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
409dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int res;
410dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char devname[PATH_MAX];
411dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *filename;
412dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char event_buf[512];
413dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int event_size;
414dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int event_pos = 0;
415dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct inotify_event *event;
416dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
417dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    res = read(nfd, event_buf, sizeof(event_buf));
418dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(res < (int)sizeof(*event)) {
419dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(errno == EINTR)
420dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 0;
421dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "could not get event, %s\n", strerror(errno));
422dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
423dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
424dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    //printf("got %d bytes of event information\n", res);
425dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
426dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(devname, dirname);
427dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    filename = devname + strlen(devname);
428dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    *filename++ = '/';
429dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
430dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(res >= (int)sizeof(*event)) {
431dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        event = (struct inotify_event *)(event_buf + event_pos);
432dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
433dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(event->len) {
434dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            strcpy(filename, event->name);
435dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(event->mask & IN_CREATE) {
436dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                open_device(devname, print_flags);
437dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
438dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            else {
439dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                close_device(devname, print_flags);
440dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
441dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
442dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        event_size = sizeof(*event) + event->len;
443dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res -= event_size;
444dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        event_pos += event_size;
445dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
446dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
447dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
448dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
449dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int scan_dir(const char *dirname, int print_flags)
450dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
451dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char devname[PATH_MAX];
452dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *filename;
453dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    DIR *dir;
454dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct dirent *de;
455dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    dir = opendir(dirname);
456dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(dir == NULL)
457dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
458dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(devname, dirname);
459dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    filename = devname + strlen(devname);
460dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    *filename++ = '/';
461dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while((de = readdir(dir))) {
462dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(de->d_name[0] == '.' &&
463dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project           (de->d_name[1] == '\0' ||
464dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            (de->d_name[1] == '.' && de->d_name[2] == '\0')))
465dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            continue;
466dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcpy(filename, de->d_name);
467dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        open_device(devname, print_flags);
468dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
469dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    closedir(dir);
470dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
471dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
472dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
473dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void usage(int argc, char *argv[])
474dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
4754ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    fprintf(stderr, "Usage: %s [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]\n", argv[0]);
476477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -t: show time stamps\n");
477477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -n: don't print newlines\n");
478477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -s: print switch states for given bits\n");
479477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -S: print all switch states\n");
480f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    fprintf(stderr, "    -v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)\n");
4814ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown    fprintf(stderr, "    -d: show HID descriptor, if available\n");
482477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -p: show possible events (errs, dev, name, pos. events)\n");
483f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    fprintf(stderr, "    -i: show all device info and possible events\n");
484f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    fprintf(stderr, "    -l: label event types and names in plain text\n");
485477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -q: quiet (clear verbosity mask)\n");
486477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -c: print given number of events then exit\n");
487477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn    fprintf(stderr, "    -r: print rate events are received\n");
488dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
489dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
490dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint getevent_main(int argc, char *argv[])
491dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
492dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int c;
493dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int i;
494dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int res;
495dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int pollres;
496dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int get_time = 0;
497dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int print_device = 0;
498dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *newline = "\n";
499dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    uint16_t get_switch = 0;
500dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct input_event event;
501dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int version;
502f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    int print_flags = 0;
503dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int print_flags_set = 0;
504dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int dont_block = -1;
505dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int event_count = 0;
506dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int sync_rate = 0;
507dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int64_t last_sync_time = 0;
508dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const char *device = NULL;
509dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const char *device_path = "/dev/input";
510dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
511dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    opterr = 0;
512dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do {
5134ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown        c = getopt(argc, argv, "tns:Sv::dpilqc:rh");
514dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (c == EOF)
515dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
516dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        switch (c) {
517dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 't':
518dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            get_time = 1;
519dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
520dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'n':
521dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            newline = "";
522dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
523dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 's':
524dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            get_switch = strtoul(optarg, NULL, 0);
525dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(dont_block == -1)
526dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                dont_block = 1;
527dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
528dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'S':
529dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            get_switch = ~0;
530dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(dont_block == -1)
531dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                dont_block = 1;
532dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
533dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'v':
534dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(optarg)
535f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                print_flags |= strtoul(optarg, NULL, 0);
536dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            else
537dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                print_flags |= PRINT_DEVICE | PRINT_DEVICE_NAME | PRINT_DEVICE_INFO | PRINT_VERSION;
538dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            print_flags_set = 1;
539dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
5404ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown        case 'd':
5414ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown            print_flags |= PRINT_HID_DESCRIPTOR;
5424ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown            break;
543477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn        case 'p':
5444ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown            print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE
5454ac87154c1e659c474b8d70b4de7a7700c78b4c4Jeff Brown                    | PRINT_DEVICE_NAME | PRINT_POSSIBLE_EVENTS | PRINT_INPUT_PROPS;
546477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            print_flags_set = 1;
547477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            if(dont_block == -1)
548477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn                dont_block = 1;
549477b4307004f853bb89c62f562fc9dc90a5ae71aDianne Hackborn            break;
550f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        case 'i':
551f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            print_flags |= PRINT_ALL_INFO;
552f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            print_flags_set = 1;
553f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            if(dont_block == -1)
554f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                dont_block = 1;
555f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            break;
556f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        case 'l':
557f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            print_flags |= PRINT_LABELS;
558f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            break;
559dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'q':
560dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            print_flags_set = 1;
561dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
562dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'c':
563dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            event_count = atoi(optarg);
564dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            dont_block = 0;
565dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
566dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'r':
567dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            sync_rate = 1;
568dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
569dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case '?':
570dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "%s: invalid option -%c\n",
571dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argv[0], optopt);
572dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'h':
573dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            usage(argc, argv);
574dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            exit(1);
575dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
576dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } while (1);
577dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(dont_block == -1)
578dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        dont_block = 0;
579dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
580dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (optind + 1 == argc) {
581dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        device = argv[optind];
582dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        optind++;
583dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
584dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (optind != argc) {
585dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        usage(argc, argv);
586dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        exit(1);
587dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
588dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    nfds = 1;
589dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ufds = calloc(1, sizeof(ufds[0]));
590dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ufds[0].fd = inotify_init();
591dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ufds[0].events = POLLIN;
592dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(device) {
593dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(!print_flags_set)
594f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            print_flags |= PRINT_DEVICE_ERRORS;
595dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res = open_device(device, print_flags);
596dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(res < 0) {
597dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 1;
598dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
599f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown    } else {
600f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown        if(!print_flags_set)
601f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown            print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE | PRINT_DEVICE_NAME;
602dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        print_device = 1;
603dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE);
604dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(res < 0) {
605dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "could not add watch for %s, %s\n", device_path, strerror(errno));
606dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 1;
607dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
608dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        res = scan_dir(device_path, print_flags);
609dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(res < 0) {
610dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "scan dir failed for %s\n", device_path);
611dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 1;
612dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
613dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
614dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
615dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(get_switch) {
616dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        for(i = 1; i < nfds; i++) {
617dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            uint16_t sw;
618dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            res = ioctl(ufds[i].fd, EVIOCGSW(1), &sw);
619dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(res < 0) {
620dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                fprintf(stderr, "could not get switch state, %s\n", strerror(errno));
621dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                return 1;
622dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
623dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            sw &= get_switch;
624dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            printf("%04x%s", sw, newline);
625dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
626dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
627dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
628dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(dont_block)
629dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
630dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
631dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(1) {
632dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        pollres = poll(ufds, nfds, -1);
633dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        //printf("poll %d, returned %d\n", nfds, pollres);
634dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(ufds[0].revents & POLLIN) {
635dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            read_notify(device_path, ufds[0].fd, print_flags);
636dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
637dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        for(i = 1; i < nfds; i++) {
638dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(ufds[i].revents) {
639dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                if(ufds[i].revents & POLLIN) {
640dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    res = read(ufds[i].fd, &event, sizeof(event));
641dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(res < (int)sizeof(event)) {
642dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        fprintf(stderr, "could not get event\n");
643dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        return 1;
644dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    }
645dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(get_time) {
646e4e21796f72e09d4fab153992b31cb1c6f65d0b0Jeff Brown                        printf("[%8ld.%06ld] ", event.time.tv_sec, event.time.tv_usec);
647dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    }
648dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(print_device)
649dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        printf("%s: ", device_names[i]);
650f8754337d82d0a1ba509311bbadd281619c7d96eJeff Brown                    print_event(event.type, event.code, event.value, print_flags);
651dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(sync_rate && event.type == 0 && event.code == 0) {
652dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        int64_t now = event.time.tv_sec * 1000000LL + event.time.tv_usec;
653dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        if(last_sync_time)
654dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                            printf(" rate %lld", 1000000LL / (now - last_sync_time));
655dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        last_sync_time = now;
656dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    }
657dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    printf("%s", newline);
658dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                    if(event_count && --event_count == 0)
659dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        return 0;
660dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                }
661dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
662dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
663dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
664dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
665dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
666dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
667