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