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