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