| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #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 Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 11 | #include <linux/input.h> | 
| Ting-Yuan Huang | 1c2f612 | 2016-11-15 16:13:42 -0800 | [diff] [blame] | 12 | #include <err.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | #include <errno.h> | 
| Elliott Hughes | 0badbd6 | 2014-12-29 12:24:25 -0800 | [diff] [blame] | 14 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 |  | 
| Elliott Hughes | c7f3c5c | 2015-03-25 16:40:13 -0700 | [diff] [blame] | 16 | struct label { | 
 | 17 |     const char *name; | 
 | 18 |     int value; | 
 | 19 | }; | 
 | 20 |  | 
 | 21 | #define LABEL(constant) { #constant, constant } | 
 | 22 | #define LABEL_END { NULL, -1 } | 
 | 23 |  | 
 | 24 | static struct label key_value_labels[] = { | 
 | 25 |         { "UP", 0 }, | 
 | 26 |         { "DOWN", 1 }, | 
 | 27 |         { "REPEAT", 2 }, | 
 | 28 |         LABEL_END, | 
 | 29 | }; | 
 | 30 |  | 
 | 31 | #include "input.h-labels.h" | 
 | 32 |  | 
 | 33 | #undef LABEL | 
 | 34 | #undef LABEL_END | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 35 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | static struct pollfd *ufds; | 
 | 37 | static char **device_names; | 
 | 38 | static int nfds; | 
 | 39 |  | 
 | 40 | enum { | 
 | 41 |     PRINT_DEVICE_ERRORS     = 1U << 0, | 
 | 42 |     PRINT_DEVICE            = 1U << 1, | 
 | 43 |     PRINT_DEVICE_NAME       = 1U << 2, | 
 | 44 |     PRINT_DEVICE_INFO       = 1U << 3, | 
 | 45 |     PRINT_VERSION           = 1U << 4, | 
 | 46 |     PRINT_POSSIBLE_EVENTS   = 1U << 5, | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 47 |     PRINT_INPUT_PROPS       = 1U << 6, | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 48 |     PRINT_HID_DESCRIPTOR    = 1U << 7, | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 49 |  | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 50 |     PRINT_ALL_INFO          = (1U << 8) - 1, | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 51 |  | 
 | 52 |     PRINT_LABELS            = 1U << 16, | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | }; | 
 | 54 |  | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 55 | static const char *get_label(const struct label *labels, int value) | 
 | 56 | { | 
 | 57 |     while(labels->name && value != labels->value) { | 
 | 58 |         labels++; | 
 | 59 |     } | 
 | 60 |     return labels->name; | 
 | 61 | } | 
 | 62 |  | 
 | 63 | static int print_input_props(int fd) | 
 | 64 | { | 
 | 65 |     uint8_t bits[INPUT_PROP_CNT / 8]; | 
 | 66 |     int i, j; | 
 | 67 |     int res; | 
 | 68 |     int count; | 
 | 69 |     const char *bit_label; | 
 | 70 |  | 
 | 71 |     printf("  input props:\n"); | 
 | 72 |     res = ioctl(fd, EVIOCGPROP(sizeof(bits)), bits); | 
 | 73 |     if(res < 0) { | 
 | 74 |         printf("    <not available\n"); | 
 | 75 |         return 1; | 
 | 76 |     } | 
 | 77 |     count = 0; | 
 | 78 |     for(i = 0; i < res; i++) { | 
 | 79 |         for(j = 0; j < 8; j++) { | 
 | 80 |             if (bits[i] & 1 << j) { | 
 | 81 |                 bit_label = get_label(input_prop_labels, i * 8 + j); | 
 | 82 |                 if(bit_label) | 
 | 83 |                     printf("    %s\n", bit_label); | 
 | 84 |                 else | 
 | 85 |                     printf("    %04x\n", i * 8 + j); | 
 | 86 |                 count++; | 
 | 87 |             } | 
 | 88 |         } | 
 | 89 |     } | 
 | 90 |     if (!count) | 
 | 91 |         printf("    <none>\n"); | 
 | 92 |     return 0; | 
 | 93 | } | 
 | 94 |  | 
 | 95 | static int print_possible_events(int fd, int print_flags) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | { | 
 | 97 |     uint8_t *bits = NULL; | 
 | 98 |     ssize_t bits_size = 0; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 99 |     const char* label; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 |     int i, j, k; | 
 | 101 |     int res, res2; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 102 |     struct label* bit_labels; | 
 | 103 |     const char *bit_label; | 
 | 104 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 |     printf("  events:\n"); | 
| Jeff Brown | f6d0f8a | 2011-06-29 20:52:08 -0700 | [diff] [blame] | 106 |     for(i = EV_KEY; i <= EV_MAX; i++) { // skip EV_SYN since we cannot query its available codes | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 |         int count = 0; | 
 | 108 |         while(1) { | 
 | 109 |             res = ioctl(fd, EVIOCGBIT(i, bits_size), bits); | 
 | 110 |             if(res < bits_size) | 
 | 111 |                 break; | 
 | 112 |             bits_size = res + 16; | 
 | 113 |             bits = realloc(bits, bits_size * 2); | 
| Ting-Yuan Huang | 1c2f612 | 2016-11-15 16:13:42 -0800 | [diff] [blame] | 114 |             if(bits == NULL) | 
 | 115 |                 err(1, "failed to allocate buffer of size %d\n", (int)bits_size); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 |         } | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 117 |         res2 = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 |         switch(i) { | 
 | 119 |             case EV_KEY: | 
 | 120 |                 res2 = ioctl(fd, EVIOCGKEY(res), bits + bits_size); | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 121 |                 label = "KEY"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 122 |                 bit_labels = key_labels; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 123 |                 break; | 
 | 124 |             case EV_REL: | 
 | 125 |                 label = "REL"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 126 |                 bit_labels = rel_labels; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 127 |                 break; | 
 | 128 |             case EV_ABS: | 
 | 129 |                 label = "ABS"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 130 |                 bit_labels = abs_labels; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 131 |                 break; | 
 | 132 |             case EV_MSC: | 
 | 133 |                 label = "MSC"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 134 |                 bit_labels = msc_labels; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 |                 break; | 
 | 136 |             case EV_LED: | 
 | 137 |                 res2 = ioctl(fd, EVIOCGLED(res), bits + bits_size); | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 138 |                 label = "LED"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 139 |                 bit_labels = led_labels; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 |                 break; | 
 | 141 |             case EV_SND: | 
 | 142 |                 res2 = ioctl(fd, EVIOCGSND(res), bits + bits_size); | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 143 |                 label = "SND"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 144 |                 bit_labels = snd_labels; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 |                 break; | 
 | 146 |             case EV_SW: | 
 | 147 |                 res2 = ioctl(fd, EVIOCGSW(bits_size), bits + bits_size); | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 148 |                 label = "SW "; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 149 |                 bit_labels = sw_labels; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 150 |                 break; | 
 | 151 |             case EV_REP: | 
 | 152 |                 label = "REP"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 153 |                 bit_labels = rep_labels; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 154 |                 break; | 
 | 155 |             case EV_FF: | 
 | 156 |                 label = "FF "; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 157 |                 bit_labels = ff_labels; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 158 |                 break; | 
 | 159 |             case EV_PWR: | 
 | 160 |                 label = "PWR"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 161 |                 bit_labels = NULL; | 
 | 162 |                 break; | 
 | 163 |             case EV_FF_STATUS: | 
 | 164 |                 label = "FFS"; | 
 | 165 |                 bit_labels = ff_status_labels; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 |                 break; | 
 | 167 |             default: | 
 | 168 |                 res2 = 0; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 169 |                 label = "???"; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 170 |                 bit_labels = NULL; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 171 |         } | 
 | 172 |         for(j = 0; j < res; j++) { | 
 | 173 |             for(k = 0; k < 8; k++) | 
 | 174 |                 if(bits[j] & 1 << k) { | 
 | 175 |                     char down; | 
 | 176 |                     if(j < res2 && (bits[j + bits_size] & 1 << k)) | 
 | 177 |                         down = '*'; | 
 | 178 |                     else | 
 | 179 |                         down = ' '; | 
 | 180 |                     if(count == 0) | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 181 |                         printf("    %s (%04x):", label, i); | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 182 |                     else if((count & (print_flags & PRINT_LABELS ? 0x3 : 0x7)) == 0 || i == EV_ABS) | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 183 |                         printf("\n               "); | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 184 |                     if(bit_labels && (print_flags & PRINT_LABELS)) { | 
 | 185 |                         bit_label = get_label(bit_labels, j * 8 + k); | 
 | 186 |                         if(bit_label) | 
| Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 187 |                             printf(" %.20s%c%*s", bit_label, down, (int) (20 - strlen(bit_label)), ""); | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 188 |                         else | 
 | 189 |                             printf(" %04x%c                ", j * 8 + k, down); | 
 | 190 |                     } else { | 
 | 191 |                         printf(" %04x%c", j * 8 + k, down); | 
 | 192 |                     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 |                     if(i == EV_ABS) { | 
 | 194 |                         struct input_absinfo abs; | 
 | 195 |                         if(ioctl(fd, EVIOCGABS(j * 8 + k), &abs) == 0) { | 
| Jeff Brown | 9de370e | 2011-08-16 17:18:54 -0700 | [diff] [blame] | 196 |                             printf(" : value %d, min %d, max %d, fuzz %d, flat %d, resolution %d", | 
 | 197 |                                 abs.value, abs.minimum, abs.maximum, abs.fuzz, abs.flat, | 
 | 198 |                                 abs.resolution); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 |                         } | 
 | 200 |                     } | 
 | 201 |                     count++; | 
 | 202 |                 } | 
 | 203 |         } | 
 | 204 |         if(count) | 
 | 205 |             printf("\n"); | 
 | 206 |     } | 
 | 207 |     free(bits); | 
 | 208 |     return 0; | 
 | 209 | } | 
 | 210 |  | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 211 | static void print_event(int type, int code, int value, int print_flags) | 
 | 212 | { | 
 | 213 |     const char *type_label, *code_label, *value_label; | 
 | 214 |  | 
 | 215 |     if (print_flags & PRINT_LABELS) { | 
 | 216 |         type_label = get_label(ev_labels, type); | 
 | 217 |         code_label = NULL; | 
 | 218 |         value_label = NULL; | 
 | 219 |  | 
 | 220 |         switch(type) { | 
 | 221 |             case EV_SYN: | 
 | 222 |                 code_label = get_label(syn_labels, code); | 
 | 223 |                 break; | 
 | 224 |             case EV_KEY: | 
 | 225 |                 code_label = get_label(key_labels, code); | 
 | 226 |                 value_label = get_label(key_value_labels, value); | 
 | 227 |                 break; | 
 | 228 |             case EV_REL: | 
 | 229 |                 code_label = get_label(rel_labels, code); | 
 | 230 |                 break; | 
 | 231 |             case EV_ABS: | 
 | 232 |                 code_label = get_label(abs_labels, code); | 
 | 233 |                 switch(code) { | 
 | 234 |                     case ABS_MT_TOOL_TYPE: | 
 | 235 |                         value_label = get_label(mt_tool_labels, value); | 
 | 236 |                 } | 
 | 237 |                 break; | 
 | 238 |             case EV_MSC: | 
 | 239 |                 code_label = get_label(msc_labels, code); | 
 | 240 |                 break; | 
 | 241 |             case EV_LED: | 
 | 242 |                 code_label = get_label(led_labels, code); | 
 | 243 |                 break; | 
 | 244 |             case EV_SND: | 
 | 245 |                 code_label = get_label(snd_labels, code); | 
 | 246 |                 break; | 
 | 247 |             case EV_SW: | 
 | 248 |                 code_label = get_label(sw_labels, code); | 
 | 249 |                 break; | 
 | 250 |             case EV_REP: | 
 | 251 |                 code_label = get_label(rep_labels, code); | 
 | 252 |                 break; | 
 | 253 |             case EV_FF: | 
 | 254 |                 code_label = get_label(ff_labels, code); | 
 | 255 |                 break; | 
 | 256 |             case EV_FF_STATUS: | 
 | 257 |                 code_label = get_label(ff_status_labels, code); | 
 | 258 |                 break; | 
 | 259 |         } | 
 | 260 |  | 
 | 261 |         if (type_label) | 
 | 262 |             printf("%-12.12s", type_label); | 
 | 263 |         else | 
 | 264 |             printf("%04x        ", type); | 
 | 265 |         if (code_label) | 
 | 266 |             printf(" %-20.20s", code_label); | 
 | 267 |         else | 
 | 268 |             printf(" %04x                ", code); | 
 | 269 |         if (value_label) | 
 | 270 |             printf(" %-20.20s", value_label); | 
 | 271 |         else | 
| Jeff Brown | f6d0f8a | 2011-06-29 20:52:08 -0700 | [diff] [blame] | 272 |             printf(" %08x            ", value); | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 273 |     } else { | 
 | 274 |         printf("%04x %04x %08x", type, code, value); | 
 | 275 |     } | 
 | 276 | } | 
 | 277 |  | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 278 | static void print_hid_descriptor(int bus, int vendor, int product) | 
 | 279 | { | 
 | 280 |     const char *dirname = "/sys/kernel/debug/hid"; | 
 | 281 |     char prefix[16]; | 
 | 282 |     DIR *dir; | 
 | 283 |     struct dirent *de; | 
 | 284 |     char filename[PATH_MAX]; | 
 | 285 |     FILE *file; | 
 | 286 |     char line[2048]; | 
 | 287 |  | 
 | 288 |     snprintf(prefix, sizeof(prefix), "%04X:%04X:%04X.", bus, vendor, product); | 
 | 289 |  | 
 | 290 |     dir = opendir(dirname); | 
 | 291 |     if(dir == NULL) | 
 | 292 |         return; | 
 | 293 |     while((de = readdir(dir))) { | 
 | 294 |         if (strstr(de->d_name, prefix) == de->d_name) { | 
 | 295 |             snprintf(filename, sizeof(filename), "%s/%s/rdesc", dirname, de->d_name); | 
 | 296 |  | 
 | 297 |             file = fopen(filename, "r"); | 
 | 298 |             if (file) { | 
 | 299 |                 printf("  HID descriptor: %s\n\n", de->d_name); | 
 | 300 |                 while (fgets(line, sizeof(line), file)) { | 
 | 301 |                     fputs("    ", stdout); | 
 | 302 |                     fputs(line, stdout); | 
 | 303 |                 } | 
 | 304 |                 fclose(file); | 
 | 305 |                 puts(""); | 
 | 306 |             } | 
 | 307 |         } | 
 | 308 |     } | 
 | 309 |     closedir(dir); | 
 | 310 | } | 
 | 311 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 312 | static int open_device(const char *device, int print_flags) | 
 | 313 | { | 
 | 314 |     int version; | 
 | 315 |     int fd; | 
| Sasha Levitskiy | 140b135 | 2013-10-23 11:31:02 -0700 | [diff] [blame] | 316 |     int clkid = CLOCK_MONOTONIC; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 |     struct pollfd *new_ufds; | 
 | 318 |     char **new_device_names; | 
 | 319 |     char name[80]; | 
 | 320 |     char location[80]; | 
 | 321 |     char idstr[80]; | 
 | 322 |     struct input_id id; | 
 | 323 |  | 
| Nick Kralevich | 38e9f23 | 2018-08-24 12:16:17 -0700 | [diff] [blame] | 324 |     fd = open(device, O_RDONLY | O_CLOEXEC); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 |     if(fd < 0) { | 
 | 326 |         if(print_flags & PRINT_DEVICE_ERRORS) | 
 | 327 |             fprintf(stderr, "could not open %s, %s\n", device, strerror(errno)); | 
 | 328 |         return -1; | 
 | 329 |     } | 
 | 330 |      | 
 | 331 |     if(ioctl(fd, EVIOCGVERSION, &version)) { | 
 | 332 |         if(print_flags & PRINT_DEVICE_ERRORS) | 
 | 333 |             fprintf(stderr, "could not get driver version for %s, %s\n", device, strerror(errno)); | 
 | 334 |         return -1; | 
 | 335 |     } | 
 | 336 |     if(ioctl(fd, EVIOCGID, &id)) { | 
 | 337 |         if(print_flags & PRINT_DEVICE_ERRORS) | 
 | 338 |             fprintf(stderr, "could not get driver id for %s, %s\n", device, strerror(errno)); | 
 | 339 |         return -1; | 
 | 340 |     } | 
 | 341 |     name[sizeof(name) - 1] = '\0'; | 
 | 342 |     location[sizeof(location) - 1] = '\0'; | 
 | 343 |     idstr[sizeof(idstr) - 1] = '\0'; | 
 | 344 |     if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) { | 
 | 345 |         //fprintf(stderr, "could not get device name for %s, %s\n", device, strerror(errno)); | 
 | 346 |         name[0] = '\0'; | 
 | 347 |     } | 
 | 348 |     if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) { | 
 | 349 |         //fprintf(stderr, "could not get location for %s, %s\n", device, strerror(errno)); | 
 | 350 |         location[0] = '\0'; | 
 | 351 |     } | 
 | 352 |     if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) { | 
 | 353 |         //fprintf(stderr, "could not get idstring for %s, %s\n", device, strerror(errno)); | 
 | 354 |         idstr[0] = '\0'; | 
 | 355 |     } | 
 | 356 |  | 
| Sasha Levitskiy | 140b135 | 2013-10-23 11:31:02 -0700 | [diff] [blame] | 357 |     if (ioctl(fd, EVIOCSCLOCKID, &clkid) != 0) { | 
 | 358 |         fprintf(stderr, "Can't enable monotonic clock reporting: %s\n", strerror(errno)); | 
 | 359 |         // a non-fatal error | 
 | 360 |     } | 
 | 361 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 362 |     new_ufds = realloc(ufds, sizeof(ufds[0]) * (nfds + 1)); | 
 | 363 |     if(new_ufds == NULL) { | 
 | 364 |         fprintf(stderr, "out of memory\n"); | 
 | 365 |         return -1; | 
 | 366 |     } | 
 | 367 |     ufds = new_ufds; | 
 | 368 |     new_device_names = realloc(device_names, sizeof(device_names[0]) * (nfds + 1)); | 
 | 369 |     if(new_device_names == NULL) { | 
 | 370 |         fprintf(stderr, "out of memory\n"); | 
 | 371 |         return -1; | 
 | 372 |     } | 
 | 373 |     device_names = new_device_names; | 
 | 374 |  | 
 | 375 |     if(print_flags & PRINT_DEVICE) | 
 | 376 |         printf("add device %d: %s\n", nfds, device); | 
 | 377 |     if(print_flags & PRINT_DEVICE_INFO) | 
 | 378 |         printf("  bus:      %04x\n" | 
 | 379 |                "  vendor    %04x\n" | 
 | 380 |                "  product   %04x\n" | 
 | 381 |                "  version   %04x\n", | 
 | 382 |                id.bustype, id.vendor, id.product, id.version); | 
 | 383 |     if(print_flags & PRINT_DEVICE_NAME) | 
 | 384 |         printf("  name:     \"%s\"\n", name); | 
 | 385 |     if(print_flags & PRINT_DEVICE_INFO) | 
 | 386 |         printf("  location: \"%s\"\n" | 
 | 387 |                "  id:       \"%s\"\n", location, idstr); | 
 | 388 |     if(print_flags & PRINT_VERSION) | 
 | 389 |         printf("  version:  %d.%d.%d\n", | 
 | 390 |                version >> 16, (version >> 8) & 0xff, version & 0xff); | 
 | 391 |  | 
 | 392 |     if(print_flags & PRINT_POSSIBLE_EVENTS) { | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 393 |         print_possible_events(fd, print_flags); | 
 | 394 |     } | 
 | 395 |  | 
 | 396 |     if(print_flags & PRINT_INPUT_PROPS) { | 
 | 397 |         print_input_props(fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 |     } | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 399 |     if(print_flags & PRINT_HID_DESCRIPTOR) { | 
 | 400 |         print_hid_descriptor(id.bustype, id.vendor, id.product); | 
 | 401 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 402 |  | 
 | 403 |     ufds[nfds].fd = fd; | 
 | 404 |     ufds[nfds].events = POLLIN; | 
 | 405 |     device_names[nfds] = strdup(device); | 
 | 406 |     nfds++; | 
 | 407 |  | 
 | 408 |     return 0; | 
 | 409 | } | 
 | 410 |  | 
 | 411 | int close_device(const char *device, int print_flags) | 
 | 412 | { | 
 | 413 |     int i; | 
 | 414 |     for(i = 1; i < nfds; i++) { | 
 | 415 |         if(strcmp(device_names[i], device) == 0) { | 
 | 416 |             int count = nfds - i - 1; | 
 | 417 |             if(print_flags & PRINT_DEVICE) | 
 | 418 |                 printf("remove device %d: %s\n", i, device); | 
 | 419 |             free(device_names[i]); | 
 | 420 |             memmove(device_names + i, device_names + i + 1, sizeof(device_names[0]) * count); | 
 | 421 |             memmove(ufds + i, ufds + i + 1, sizeof(ufds[0]) * count); | 
 | 422 |             nfds--; | 
 | 423 |             return 0; | 
 | 424 |         } | 
 | 425 |     } | 
 | 426 |     if(print_flags & PRINT_DEVICE_ERRORS) | 
 | 427 |         fprintf(stderr, "remote device: %s not found\n", device); | 
 | 428 |     return -1; | 
 | 429 | } | 
 | 430 |  | 
 | 431 | static int read_notify(const char *dirname, int nfd, int print_flags) | 
 | 432 | { | 
 | 433 |     int res; | 
 | 434 |     char devname[PATH_MAX]; | 
 | 435 |     char *filename; | 
 | 436 |     char event_buf[512]; | 
 | 437 |     int event_size; | 
 | 438 |     int event_pos = 0; | 
 | 439 |     struct inotify_event *event; | 
 | 440 |  | 
 | 441 |     res = read(nfd, event_buf, sizeof(event_buf)); | 
 | 442 |     if(res < (int)sizeof(*event)) { | 
 | 443 |         if(errno == EINTR) | 
 | 444 |             return 0; | 
 | 445 |         fprintf(stderr, "could not get event, %s\n", strerror(errno)); | 
 | 446 |         return 1; | 
 | 447 |     } | 
 | 448 |     //printf("got %d bytes of event information\n", res); | 
 | 449 |  | 
 | 450 |     strcpy(devname, dirname); | 
 | 451 |     filename = devname + strlen(devname); | 
 | 452 |     *filename++ = '/'; | 
 | 453 |  | 
 | 454 |     while(res >= (int)sizeof(*event)) { | 
 | 455 |         event = (struct inotify_event *)(event_buf + event_pos); | 
 | 456 |         //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); | 
 | 457 |         if(event->len) { | 
 | 458 |             strcpy(filename, event->name); | 
 | 459 |             if(event->mask & IN_CREATE) { | 
 | 460 |                 open_device(devname, print_flags); | 
 | 461 |             } | 
 | 462 |             else { | 
 | 463 |                 close_device(devname, print_flags); | 
 | 464 |             } | 
 | 465 |         } | 
 | 466 |         event_size = sizeof(*event) + event->len; | 
 | 467 |         res -= event_size; | 
 | 468 |         event_pos += event_size; | 
 | 469 |     } | 
 | 470 |     return 0; | 
 | 471 | } | 
 | 472 |  | 
 | 473 | static int scan_dir(const char *dirname, int print_flags) | 
 | 474 | { | 
 | 475 |     char devname[PATH_MAX]; | 
 | 476 |     char *filename; | 
 | 477 |     DIR *dir; | 
 | 478 |     struct dirent *de; | 
 | 479 |     dir = opendir(dirname); | 
 | 480 |     if(dir == NULL) | 
 | 481 |         return -1; | 
 | 482 |     strcpy(devname, dirname); | 
 | 483 |     filename = devname + strlen(devname); | 
 | 484 |     *filename++ = '/'; | 
 | 485 |     while((de = readdir(dir))) { | 
 | 486 |         if(de->d_name[0] == '.' && | 
 | 487 |            (de->d_name[1] == '\0' || | 
 | 488 |             (de->d_name[1] == '.' && de->d_name[2] == '\0'))) | 
 | 489 |             continue; | 
 | 490 |         strcpy(filename, de->d_name); | 
 | 491 |         open_device(devname, print_flags); | 
 | 492 |     } | 
 | 493 |     closedir(dir); | 
 | 494 |     return 0; | 
 | 495 | } | 
 | 496 |  | 
| Sasha Levitskiy | 140b135 | 2013-10-23 11:31:02 -0700 | [diff] [blame] | 497 | static void usage(char *name) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 498 | { | 
| Sasha Levitskiy | 140b135 | 2013-10-23 11:31:02 -0700 | [diff] [blame] | 499 |     fprintf(stderr, "Usage: %s [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]\n", name); | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 500 |     fprintf(stderr, "    -t: show time stamps\n"); | 
 | 501 |     fprintf(stderr, "    -n: don't print newlines\n"); | 
 | 502 |     fprintf(stderr, "    -s: print switch states for given bits\n"); | 
 | 503 |     fprintf(stderr, "    -S: print all switch states\n"); | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 504 |     fprintf(stderr, "    -v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)\n"); | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 505 |     fprintf(stderr, "    -d: show HID descriptor, if available\n"); | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 506 |     fprintf(stderr, "    -p: show possible events (errs, dev, name, pos. events)\n"); | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 507 |     fprintf(stderr, "    -i: show all device info and possible events\n"); | 
 | 508 |     fprintf(stderr, "    -l: label event types and names in plain text\n"); | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 509 |     fprintf(stderr, "    -q: quiet (clear verbosity mask)\n"); | 
 | 510 |     fprintf(stderr, "    -c: print given number of events then exit\n"); | 
 | 511 |     fprintf(stderr, "    -r: print rate events are received\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 512 | } | 
 | 513 |  | 
 | 514 | int getevent_main(int argc, char *argv[]) | 
 | 515 | { | 
 | 516 |     int c; | 
 | 517 |     int i; | 
 | 518 |     int res; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 |     int get_time = 0; | 
 | 520 |     int print_device = 0; | 
 | 521 |     char *newline = "\n"; | 
 | 522 |     uint16_t get_switch = 0; | 
 | 523 |     struct input_event event; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 524 |     int print_flags = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 525 |     int print_flags_set = 0; | 
 | 526 |     int dont_block = -1; | 
 | 527 |     int event_count = 0; | 
 | 528 |     int sync_rate = 0; | 
 | 529 |     int64_t last_sync_time = 0; | 
 | 530 |     const char *device = NULL; | 
 | 531 |     const char *device_path = "/dev/input"; | 
 | 532 |  | 
| Gary Bisson | 8cbf6bb | 2018-12-07 17:14:52 +0100 | [diff] [blame] | 533 |     /* disable buffering on stdout */ | 
 | 534 |     setbuf(stdout, NULL); | 
 | 535 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 536 |     opterr = 0; | 
 | 537 |     do { | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 538 |         c = getopt(argc, argv, "tns:Sv::dpilqc:rh"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 539 |         if (c == EOF) | 
 | 540 |             break; | 
 | 541 |         switch (c) { | 
 | 542 |         case 't': | 
 | 543 |             get_time = 1; | 
 | 544 |             break; | 
 | 545 |         case 'n': | 
 | 546 |             newline = ""; | 
 | 547 |             break; | 
 | 548 |         case 's': | 
 | 549 |             get_switch = strtoul(optarg, NULL, 0); | 
 | 550 |             if(dont_block == -1) | 
 | 551 |                 dont_block = 1; | 
 | 552 |             break; | 
 | 553 |         case 'S': | 
 | 554 |             get_switch = ~0; | 
 | 555 |             if(dont_block == -1) | 
 | 556 |                 dont_block = 1; | 
 | 557 |             break; | 
 | 558 |         case 'v': | 
 | 559 |             if(optarg) | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 560 |                 print_flags |= strtoul(optarg, NULL, 0); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 |             else | 
 | 562 |                 print_flags |= PRINT_DEVICE | PRINT_DEVICE_NAME | PRINT_DEVICE_INFO | PRINT_VERSION; | 
 | 563 |             print_flags_set = 1; | 
 | 564 |             break; | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 565 |         case 'd': | 
 | 566 |             print_flags |= PRINT_HID_DESCRIPTOR; | 
 | 567 |             break; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 568 |         case 'p': | 
| Jeff Brown | 4ac8715 | 2011-07-24 12:22:58 -0700 | [diff] [blame] | 569 |             print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE | 
 | 570 |                     | PRINT_DEVICE_NAME | PRINT_POSSIBLE_EVENTS | PRINT_INPUT_PROPS; | 
| Dianne Hackborn | 477b430 | 2009-04-08 15:43:31 -0700 | [diff] [blame] | 571 |             print_flags_set = 1; | 
 | 572 |             if(dont_block == -1) | 
 | 573 |                 dont_block = 1; | 
 | 574 |             break; | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 575 |         case 'i': | 
 | 576 |             print_flags |= PRINT_ALL_INFO; | 
 | 577 |             print_flags_set = 1; | 
 | 578 |             if(dont_block == -1) | 
 | 579 |                 dont_block = 1; | 
 | 580 |             break; | 
 | 581 |         case 'l': | 
 | 582 |             print_flags |= PRINT_LABELS; | 
 | 583 |             break; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 584 |         case 'q': | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 585 |             print_flags_set = 1; | 
 | 586 |             break; | 
 | 587 |         case 'c': | 
 | 588 |             event_count = atoi(optarg); | 
 | 589 |             dont_block = 0; | 
 | 590 |             break; | 
 | 591 |         case 'r': | 
 | 592 |             sync_rate = 1; | 
 | 593 |             break; | 
 | 594 |         case '?': | 
 | 595 |             fprintf(stderr, "%s: invalid option -%c\n", | 
 | 596 |                 argv[0], optopt); | 
 | 597 |         case 'h': | 
| Sasha Levitskiy | 140b135 | 2013-10-23 11:31:02 -0700 | [diff] [blame] | 598 |             usage(argv[0]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 599 |             exit(1); | 
 | 600 |         } | 
 | 601 |     } while (1); | 
 | 602 |     if(dont_block == -1) | 
 | 603 |         dont_block = 0; | 
 | 604 |  | 
 | 605 |     if (optind + 1 == argc) { | 
 | 606 |         device = argv[optind]; | 
 | 607 |         optind++; | 
 | 608 |     } | 
 | 609 |     if (optind != argc) { | 
| Sasha Levitskiy | 140b135 | 2013-10-23 11:31:02 -0700 | [diff] [blame] | 610 |         usage(argv[0]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 611 |         exit(1); | 
 | 612 |     } | 
 | 613 |     nfds = 1; | 
 | 614 |     ufds = calloc(1, sizeof(ufds[0])); | 
 | 615 |     ufds[0].fd = inotify_init(); | 
 | 616 |     ufds[0].events = POLLIN; | 
 | 617 |     if(device) { | 
 | 618 |         if(!print_flags_set) | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 619 |             print_flags |= PRINT_DEVICE_ERRORS; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 |         res = open_device(device, print_flags); | 
 | 621 |         if(res < 0) { | 
 | 622 |             return 1; | 
 | 623 |         } | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 624 |     } else { | 
 | 625 |         if(!print_flags_set) | 
 | 626 |             print_flags |= PRINT_DEVICE_ERRORS | PRINT_DEVICE | PRINT_DEVICE_NAME; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 627 |         print_device = 1; | 
 | 628 | 		res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE); | 
 | 629 |         if(res < 0) { | 
 | 630 |             fprintf(stderr, "could not add watch for %s, %s\n", device_path, strerror(errno)); | 
 | 631 |             return 1; | 
 | 632 |         } | 
 | 633 |         res = scan_dir(device_path, print_flags); | 
 | 634 |         if(res < 0) { | 
 | 635 |             fprintf(stderr, "scan dir failed for %s\n", device_path); | 
 | 636 |             return 1; | 
 | 637 |         } | 
 | 638 |     } | 
 | 639 |  | 
 | 640 |     if(get_switch) { | 
 | 641 |         for(i = 1; i < nfds; i++) { | 
 | 642 |             uint16_t sw; | 
 | 643 |             res = ioctl(ufds[i].fd, EVIOCGSW(1), &sw); | 
 | 644 |             if(res < 0) { | 
 | 645 |                 fprintf(stderr, "could not get switch state, %s\n", strerror(errno)); | 
 | 646 |                 return 1; | 
 | 647 |             } | 
 | 648 |             sw &= get_switch; | 
 | 649 |             printf("%04x%s", sw, newline); | 
 | 650 |         } | 
 | 651 |     } | 
 | 652 |  | 
 | 653 |     if(dont_block) | 
 | 654 |         return 0; | 
 | 655 |  | 
 | 656 |     while(1) { | 
| Mark Salyzyn | aa90776 | 2014-05-08 09:31:43 -0700 | [diff] [blame] | 657 |         //int pollres = | 
 | 658 |         poll(ufds, nfds, -1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 659 |         //printf("poll %d, returned %d\n", nfds, pollres); | 
 | 660 |         if(ufds[0].revents & POLLIN) { | 
 | 661 |             read_notify(device_path, ufds[0].fd, print_flags); | 
 | 662 |         } | 
 | 663 |         for(i = 1; i < nfds; i++) { | 
 | 664 |             if(ufds[i].revents) { | 
 | 665 |                 if(ufds[i].revents & POLLIN) { | 
 | 666 |                     res = read(ufds[i].fd, &event, sizeof(event)); | 
 | 667 |                     if(res < (int)sizeof(event)) { | 
 | 668 |                         fprintf(stderr, "could not get event\n"); | 
 | 669 |                         return 1; | 
 | 670 |                     } | 
 | 671 |                     if(get_time) { | 
| Jeff Brown | e4e2179 | 2011-11-23 15:02:41 -0800 | [diff] [blame] | 672 |                         printf("[%8ld.%06ld] ", event.time.tv_sec, event.time.tv_usec); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 673 |                     } | 
 | 674 |                     if(print_device) | 
 | 675 |                         printf("%s: ", device_names[i]); | 
| Jeff Brown | f875433 | 2011-06-15 17:44:52 -0700 | [diff] [blame] | 676 |                     print_event(event.type, event.code, event.value, print_flags); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 677 |                     if(sync_rate && event.type == 0 && event.code == 0) { | 
 | 678 |                         int64_t now = event.time.tv_sec * 1000000LL + event.time.tv_usec; | 
 | 679 |                         if(last_sync_time) | 
 | 680 |                             printf(" rate %lld", 1000000LL / (now - last_sync_time)); | 
 | 681 |                         last_sync_time = now; | 
 | 682 |                     } | 
 | 683 |                     printf("%s", newline); | 
 | 684 |                     if(event_count && --event_count == 0) | 
 | 685 |                         return 0; | 
 | 686 |                 } | 
 | 687 |             } | 
 | 688 |         } | 
 | 689 |     } | 
 | 690 |  | 
 | 691 |     return 0; | 
 | 692 | } |