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