Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <dirent.h> |
Dan Albert | c8686b4 | 2017-10-11 11:47:54 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | 25a29d4 | 2017-02-23 10:45:42 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 20 | #include <linux/input.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/epoll.h> |
Xihua Chen | 8f69349 | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 25 | #include <sys/inotify.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Tao Bao | 9468fc0 | 2017-03-17 00:57:37 -0700 | [diff] [blame] | 29 | #include <functional> |
| 30 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 31 | #include "minui/minui.h" |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 32 | |
| 33 | #define MAX_DEVICES 16 |
| 34 | #define MAX_MISC_FDS 16 |
| 35 | |
| 36 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) |
| 37 | #define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) |
| 38 | |
| 39 | struct fd_info { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 40 | int fd; |
| 41 | ev_callback cb; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Xihua Chen | 8f69349 | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 44 | static ev_callback saved_input_cb; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 45 | static int g_epoll_fd; |
Xihua Chen | 8f69349 | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 46 | static int g_inotify_fd; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 47 | static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 48 | static int npolledevents; |
| 49 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 50 | static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 51 | |
| 52 | static unsigned ev_count = 0; |
| 53 | static unsigned ev_dev_count = 0; |
| 54 | static unsigned ev_misc_count = 0; |
| 55 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 56 | static bool test_bit(size_t bit, unsigned long* array) { // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 57 | return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; |
| 58 | } |
| 59 | |
Xihua Chen | 8f69349 | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 60 | static int inotify_cb(int fd, __unused uint32_t epevents) { |
| 61 | struct inotify_event* pevent; |
| 62 | char* buf; |
| 63 | DIR* dir; |
| 64 | size_t event_len; |
| 65 | size_t offset; |
| 66 | |
| 67 | if (saved_input_cb == nullptr) return -1; |
| 68 | |
| 69 | // The inotify will put one or several complete events. |
| 70 | // Should not read part of one event. |
| 71 | int ret = ioctl(fd, FIONREAD, &event_len); |
| 72 | if (ret != 0) return -1; |
| 73 | |
| 74 | dir = opendir("/dev/input"); |
| 75 | if (dir == nullptr) return -1; |
| 76 | |
| 77 | buf = (char*)malloc(event_len); |
| 78 | if (buf == nullptr) { |
| 79 | closedir(dir); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | ret = read(fd, buf, event_len); |
| 84 | if (ret != (int)event_len) { |
| 85 | free(buf); |
| 86 | closedir(dir); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | offset = 0; |
| 91 | while (offset < event_len) { |
| 92 | pevent = (struct inotify_event*)(buf + offset); |
| 93 | if (offset + sizeof(inotify_event) + pevent->len > event_len) { |
| 94 | // The pevent->len is too large and buffer will over flow. |
| 95 | // In general, should not happen, just make more stable. |
| 96 | free(buf); |
| 97 | closedir(dir); |
| 98 | return -1; |
| 99 | } |
| 100 | offset += sizeof(inotify_event) + pevent->len; |
| 101 | |
| 102 | pevent->name[pevent->len] = '\0'; |
| 103 | if (strncmp(pevent->name, "event", 5)) continue; |
| 104 | |
| 105 | int dfd = openat(dirfd(dir), pevent->name, O_RDONLY); |
| 106 | if (dfd == -1) break; |
| 107 | |
| 108 | // Read the evbits of the input device. |
| 109 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; |
| 110 | if (ioctl(dfd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 111 | close(dfd); |
| 112 | continue; |
| 113 | } |
| 114 | // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. |
| 115 | if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { |
| 116 | close(dfd); |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | // Only add, we assume the user will not plug out and plug in USB device again and again :) |
| 121 | ev_add_fd(dfd, saved_input_cb); |
| 122 | } |
| 123 | |
| 124 | free(buf); |
| 125 | closedir(dir); |
| 126 | return 0; |
| 127 | } |
| 128 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 129 | int ev_init(ev_callback input_cb, bool allow_touch_inputs) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 130 | g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); |
| 131 | if (g_epoll_fd == -1) { |
| 132 | return -1; |
| 133 | } |
| 134 | |
Xihua Chen | 8f69349 | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 135 | g_inotify_fd = inotify_init(); |
| 136 | if (g_inotify_fd >= 0) { |
| 137 | inotify_add_watch(g_inotify_fd, "/dev/input", IN_CREATE); |
| 138 | ev_add_fd(g_inotify_fd, inotify_cb); |
| 139 | } |
| 140 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 141 | bool epollctlfail = false; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 142 | DIR* dir = opendir("/dev/input"); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 143 | if (dir != nullptr) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 144 | dirent* de; |
| 145 | while ((de = readdir(dir))) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 146 | if (strncmp(de->d_name, "event", 5)) continue; |
| 147 | int fd = openat(dirfd(dir), de->d_name, O_RDONLY); |
| 148 | if (fd == -1) continue; |
| 149 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 150 | // Use unsigned long to match ioctl's parameter type. |
| 151 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 152 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 153 | // Read the evbits of the input device. |
| 154 | if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 155 | close(fd); |
| 156 | continue; |
| 157 | } |
| 158 | |
Vitalii Kulikov | 9d56901 | 2017-01-16 22:36:29 +0100 | [diff] [blame] | 159 | // We assume that only EV_KEY, and EV_SW event types are ever needed. EV_ABS is also |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 160 | // allowed if allow_touch_inputs is set. |
Vitalii Kulikov | 9d56901 | 2017-01-16 22:36:29 +0100 | [diff] [blame] | 161 | // EV_REL should be enabled explicitly in device tree. |
| 162 | if (!test_bit(EV_KEY, ev_bits) && |
| 163 | #ifdef BOARD_RECOVERY_NEEDS_REL_INPUT |
| 164 | !test_bit(EV_REL, ev_bits) && |
| 165 | #endif |
| 166 | !test_bit(EV_SW, ev_bits)) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 167 | if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) { |
| 168 | close(fd); |
| 169 | continue; |
| 170 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | epoll_event ev; |
| 174 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 175 | ev.data.ptr = &ev_fdinfo[ev_count]; |
| 176 | if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 177 | close(fd); |
| 178 | epollctlfail = true; |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | ev_fdinfo[ev_count].fd = fd; |
Tao Bao | 232ba3f | 2019-03-08 16:20:07 -0800 | [diff] [blame] | 183 | ev_fdinfo[ev_count].cb = input_cb; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 184 | ev_count++; |
| 185 | ev_dev_count++; |
| 186 | if (ev_dev_count == MAX_DEVICES) break; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 189 | closedir(dir); |
| 190 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 191 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 192 | if (epollctlfail && !ev_count) { |
| 193 | close(g_epoll_fd); |
| 194 | g_epoll_fd = -1; |
| 195 | return -1; |
| 196 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 197 | |
Xihua Chen | 8f69349 | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 198 | saved_input_cb = input_cb; |
| 199 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 200 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int ev_get_epollfd(void) { |
| 204 | return g_epoll_fd; |
| 205 | } |
| 206 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 207 | int ev_add_fd(int fd, ev_callback cb) { |
| 208 | if (ev_misc_count == MAX_MISC_FDS || cb == NULL) { |
| 209 | return -1; |
| 210 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 211 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 212 | epoll_event ev; |
| 213 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 214 | ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]); |
| 215 | int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 216 | if (!ret) { |
| 217 | ev_fdinfo[ev_count].fd = fd; |
| 218 | ev_fdinfo[ev_count].cb = std::move(cb); |
| 219 | ev_count++; |
| 220 | ev_misc_count++; |
| 221 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 222 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 223 | return ret; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void ev_exit(void) { |
| 227 | while (ev_count > 0) { |
| 228 | close(ev_fdinfo[--ev_count].fd); |
| 229 | } |
| 230 | ev_misc_count = 0; |
| 231 | ev_dev_count = 0; |
Xihua Chen | 8f69349 | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 232 | saved_input_cb = nullptr; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 233 | close(g_epoll_fd); |
| 234 | } |
| 235 | |
| 236 | int ev_wait(int timeout) { |
| 237 | npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout); |
| 238 | if (npolledevents <= 0) { |
| 239 | return -1; |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | void ev_dispatch(void) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 245 | for (int n = 0; n < npolledevents; n++) { |
| 246 | fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr); |
| 247 | const ev_callback& cb = fdi->cb; |
| 248 | if (cb) { |
| 249 | cb(fdi->fd, polledevents[n].events); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 250 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 251 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 254 | int ev_get_input(int fd, uint32_t epevents, input_event* ev) { |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 255 | if (epevents & EPOLLIN) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 256 | ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev))); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 257 | if (r == sizeof(*ev)) { |
| 258 | return 0; |
| 259 | } |
| 260 | } |
| 261 | return -1; |
| 262 | } |
| 263 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 264 | int ev_sync_key_state(const ev_set_key_callback& set_key_cb) { |
| 265 | // Use unsigned long to match ioctl's parameter type. |
| 266 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 267 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 268 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 269 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 270 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 271 | memset(key_bits, 0, sizeof(key_bits)); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 272 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 273 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 274 | continue; |
| 275 | } |
| 276 | if (!test_bit(EV_KEY, ev_bits)) { |
| 277 | continue; |
| 278 | } |
| 279 | if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) { |
| 280 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 283 | for (int code = 0; code <= KEY_MAX; code++) { |
| 284 | if (test_bit(code, key_bits)) { |
| 285 | set_key_cb(code, 1); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 293 | void ev_iterate_available_keys(const std::function<void(int)>& f) { |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 294 | // Use unsigned long to match ioctl's parameter type. |
| 295 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 296 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 297 | |
| 298 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 299 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 300 | memset(key_bits, 0, sizeof(key_bits)); |
| 301 | |
| 302 | // Does this device even have keys? |
| 303 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 304 | continue; |
| 305 | } |
| 306 | if (!test_bit(EV_KEY, ev_bits)) { |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits); |
| 311 | if (rc == -1) { |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 316 | if (test_bit(key_code, key_bits)) { |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 317 | f(key_code); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 322 | |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 323 | void ev_iterate_touch_inputs(const std::function<void(int)>& touch_device_detected, |
| 324 | const std::function<void(int)>& key_detected) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 325 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 326 | // Use unsigned long to match ioctl's parameter type. |
| 327 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT |
| 328 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 329 | continue; |
| 330 | } |
| 331 | if (!test_bit(EV_ABS, ev_bits)) { |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT |
| 336 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) { |
| 337 | continue; |
| 338 | } |
| 339 | |
Tom Marshall | 74f4824 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 340 | touch_device_detected(ev_fdinfo[i].fd); |
| 341 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 342 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 343 | if (test_bit(key_code, key_bits)) { |
Tom Marshall | c0d6e1e | 2017-08-24 12:57:27 +0000 | [diff] [blame] | 344 | key_detected(key_code); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |