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> |
Elliott Hughes | 25a29d4 | 2017-02-23 10:45:42 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 19 | #include <linux/input.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/epoll.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Tao Bao | 9468fc0 | 2017-03-17 00:57:37 -0700 | [diff] [blame] | 27 | #include <functional> |
| 28 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 29 | #include "minui/minui.h" |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 30 | |
| 31 | #define MAX_DEVICES 16 |
| 32 | #define MAX_MISC_FDS 16 |
| 33 | |
| 34 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) |
| 35 | #define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) |
| 36 | |
| 37 | struct fd_info { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 38 | int fd; |
| 39 | ev_callback cb; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static int g_epoll_fd; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 43 | static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 44 | static int npolledevents; |
| 45 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 46 | static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 47 | |
| 48 | static unsigned ev_count = 0; |
| 49 | static unsigned ev_dev_count = 0; |
| 50 | static unsigned ev_misc_count = 0; |
| 51 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 52 | static bool test_bit(size_t bit, unsigned long* array) { // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 53 | return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; |
| 54 | } |
| 55 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 56 | int ev_init(ev_callback input_cb, bool allow_touch_inputs) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 57 | g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); |
| 58 | if (g_epoll_fd == -1) { |
| 59 | return -1; |
| 60 | } |
| 61 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 62 | bool epollctlfail = false; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 63 | DIR* dir = opendir("/dev/input"); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 64 | if (dir != nullptr) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 65 | dirent* de; |
| 66 | while ((de = readdir(dir))) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 67 | if (strncmp(de->d_name, "event", 5)) continue; |
| 68 | int fd = openat(dirfd(dir), de->d_name, O_RDONLY); |
| 69 | if (fd == -1) continue; |
| 70 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 71 | // Use unsigned long to match ioctl's parameter type. |
| 72 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 73 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 74 | // Read the evbits of the input device. |
| 75 | if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 76 | close(fd); |
| 77 | continue; |
| 78 | } |
| 79 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 80 | // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also |
| 81 | // allowed if allow_touch_inputs is set. |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 82 | if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 83 | if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) { |
| 84 | close(fd); |
| 85 | continue; |
| 86 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | epoll_event ev; |
| 90 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 91 | ev.data.ptr = &ev_fdinfo[ev_count]; |
| 92 | if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 93 | close(fd); |
| 94 | epollctlfail = true; |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | ev_fdinfo[ev_count].fd = fd; |
| 99 | ev_fdinfo[ev_count].cb = std::move(input_cb); |
| 100 | ev_count++; |
| 101 | ev_dev_count++; |
| 102 | if (ev_dev_count == MAX_DEVICES) break; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 105 | closedir(dir); |
| 106 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 107 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 108 | if (epollctlfail && !ev_count) { |
| 109 | close(g_epoll_fd); |
| 110 | g_epoll_fd = -1; |
| 111 | return -1; |
| 112 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 113 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 114 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | int ev_get_epollfd(void) { |
| 118 | return g_epoll_fd; |
| 119 | } |
| 120 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 121 | int ev_add_fd(int fd, ev_callback cb) { |
| 122 | if (ev_misc_count == MAX_MISC_FDS || cb == NULL) { |
| 123 | return -1; |
| 124 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 125 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 126 | epoll_event ev; |
| 127 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 128 | ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]); |
| 129 | int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 130 | if (!ret) { |
| 131 | ev_fdinfo[ev_count].fd = fd; |
| 132 | ev_fdinfo[ev_count].cb = std::move(cb); |
| 133 | ev_count++; |
| 134 | ev_misc_count++; |
| 135 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 136 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 137 | return ret; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void ev_exit(void) { |
| 141 | while (ev_count > 0) { |
| 142 | close(ev_fdinfo[--ev_count].fd); |
| 143 | } |
| 144 | ev_misc_count = 0; |
| 145 | ev_dev_count = 0; |
| 146 | close(g_epoll_fd); |
| 147 | } |
| 148 | |
| 149 | int ev_wait(int timeout) { |
| 150 | npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout); |
| 151 | if (npolledevents <= 0) { |
| 152 | return -1; |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | void ev_dispatch(void) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 158 | for (int n = 0; n < npolledevents; n++) { |
| 159 | fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr); |
| 160 | const ev_callback& cb = fdi->cb; |
| 161 | if (cb) { |
| 162 | cb(fdi->fd, polledevents[n].events); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 163 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 164 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 167 | int ev_get_input(int fd, uint32_t epevents, input_event* ev) { |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 168 | if (epevents & EPOLLIN) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 169 | ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev))); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 170 | if (r == sizeof(*ev)) { |
| 171 | return 0; |
| 172 | } |
| 173 | } |
| 174 | return -1; |
| 175 | } |
| 176 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 177 | int ev_sync_key_state(const ev_set_key_callback& set_key_cb) { |
| 178 | // Use unsigned long to match ioctl's parameter type. |
| 179 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 180 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 181 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 182 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 183 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 184 | memset(key_bits, 0, sizeof(key_bits)); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 185 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 186 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 187 | continue; |
| 188 | } |
| 189 | if (!test_bit(EV_KEY, ev_bits)) { |
| 190 | continue; |
| 191 | } |
| 192 | if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) { |
| 193 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 196 | for (int code = 0; code <= KEY_MAX; code++) { |
| 197 | if (test_bit(code, key_bits)) { |
| 198 | set_key_cb(code, 1); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 206 | void ev_iterate_available_keys(const std::function<void(int)>& f) { |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 207 | // Use unsigned long to match ioctl's parameter type. |
| 208 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 209 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 210 | |
| 211 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 212 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 213 | memset(key_bits, 0, sizeof(key_bits)); |
| 214 | |
| 215 | // Does this device even have keys? |
| 216 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 217 | continue; |
| 218 | } |
| 219 | if (!test_bit(EV_KEY, ev_bits)) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits); |
| 224 | if (rc == -1) { |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 229 | if (test_bit(key_code, key_bits)) { |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 230 | f(key_code); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 235 | |
| 236 | void ev_iterate_touch_inputs(const std::function<void(int)>& action) { |
| 237 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 238 | // Use unsigned long to match ioctl's parameter type. |
| 239 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT |
| 240 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 241 | continue; |
| 242 | } |
| 243 | if (!test_bit(EV_ABS, ev_bits)) { |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT |
| 248 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) { |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 253 | if (test_bit(key_code, key_bits)) { |
| 254 | action(key_code); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |