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