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