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 | 25a29d4 | 2017-02-23 10:45:42 -0800 | [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> |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 25 | #include <sys/inotify.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Tao Bao | 9468fc0 | 2017-03-17 00:57:37 -0700 | [diff] [blame] | 30 | #include <functional> |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 31 | #include <memory> |
Christopher Ferris | f0a760b | 2021-10-08 12:05:31 -0700 | [diff] [blame] | 32 | #include <string> |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 33 | |
Vitalii Kulikov | f938089 | 2017-01-16 22:36:29 +0100 | [diff] [blame] | 34 | #include <android-base/properties.h> |
Christopher Ferris | f0a760b | 2021-10-08 12:05:31 -0700 | [diff] [blame] | 35 | #include <android-base/strings.h> |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> |
Tao Bao | 9468fc0 | 2017-03-17 00:57:37 -0700 | [diff] [blame] | 37 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 38 | #include "minui/minui.h" |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 39 | |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 40 | constexpr const char* INPUT_DEV_DIR = "/dev/input"; |
| 41 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 42 | constexpr size_t MAX_DEVICES = 16; |
| 43 | constexpr size_t MAX_MISC_FDS = 16; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 44 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 45 | constexpr size_t BITS_PER_LONG = sizeof(unsigned long) * 8; |
| 46 | constexpr size_t BITS_TO_LONGS(size_t bits) { |
| 47 | return ((bits + BITS_PER_LONG - 1) / BITS_PER_LONG); |
| 48 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 49 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 50 | struct FdInfo { |
| 51 | android::base::unique_fd fd; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 52 | ev_callback cb; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 55 | static bool g_allow_touch_inputs = true; |
| 56 | static ev_callback g_saved_input_cb; |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 57 | static android::base::unique_fd g_epoll_fd; |
| 58 | static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS]; |
| 59 | static int g_polled_events_count; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 60 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 61 | static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 62 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 63 | static size_t g_ev_count = 0; |
| 64 | static size_t g_ev_dev_count = 0; |
| 65 | static size_t g_ev_misc_count = 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 66 | |
Vitalii Kulikov | f938089 | 2017-01-16 22:36:29 +0100 | [diff] [blame] | 67 | static bool should_skip_ev_rel() { |
| 68 | static bool prop = android::base::GetBoolProperty("ro.recovery.skip_ev_rel_input", false); |
| 69 | return prop; |
| 70 | } |
| 71 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 72 | static bool test_bit(size_t bit, unsigned long* array) { // NOLINT |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 73 | return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 76 | static bool should_add_input_device(int fd, bool allow_touch_inputs) { |
| 77 | // Use unsigned long to match ioctl's parameter type. |
| 78 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 79 | |
| 80 | // Read the evbits of the input device. |
| 81 | if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 82 | return false; |
| 83 | } |
| 84 | |
Vitalii Kulikov | f938089 | 2017-01-16 22:36:29 +0100 | [diff] [blame] | 85 | // We assume that only EV_ABS, EV_KEY, EV_REL, and EV_SW event types are ever needed. |
| 86 | // EV_ABS is only allowed if allow_touch_inputs is set. |
| 87 | // EV_REL can be explicitly disallowed. This is needed to skip sensor inputs on some devices. |
| 88 | if (!test_bit(EV_KEY, ev_bits) && |
| 89 | !test_bit(EV_SW, ev_bits) && |
| 90 | (should_skip_ev_rel() || !test_bit(EV_REL, ev_bits))) { |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 91 | if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) { |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | static int inotify_cb(int fd, __unused uint32_t epevents) { |
| 100 | if (g_saved_input_cb == nullptr) return -1; |
| 101 | |
| 102 | // The inotify will put one or several complete events. |
| 103 | // Should not read part of one event. |
Stephane Lee | 12952cd | 2020-01-13 15:46:36 -0800 | [diff] [blame] | 104 | int event_len_int; |
| 105 | int ret = ioctl(fd, FIONREAD, &event_len_int); |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 106 | if (ret != 0) return -1; |
Stephane Lee | 12952cd | 2020-01-13 15:46:36 -0800 | [diff] [blame] | 107 | if (event_len_int < 0) return -1; |
| 108 | size_t event_len = event_len_int; |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 109 | |
| 110 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir); |
| 111 | if (!dir) { |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | std::vector<int8_t> buf(event_len); |
| 116 | |
| 117 | ssize_t r = TEMP_FAILURE_RETRY(read(fd, buf.data(), event_len)); |
| 118 | if (r != event_len) { |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | size_t offset = 0; |
| 123 | while (offset < event_len) { |
| 124 | struct inotify_event* pevent = reinterpret_cast<struct inotify_event*>(buf.data() + offset); |
| 125 | if (offset + sizeof(inotify_event) + pevent->len > event_len) { |
| 126 | // The pevent->len is too large and buffer will over flow. |
| 127 | // In general, should not happen, just make more stable. |
| 128 | return -1; |
| 129 | } |
| 130 | offset += sizeof(inotify_event) + pevent->len; |
| 131 | |
Christopher Ferris | f0a760b | 2021-10-08 12:05:31 -0700 | [diff] [blame] | 132 | std::string event_name(pevent->name, pevent->len); |
| 133 | if (!android::base::StartsWith(event_name, "event")) { |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 134 | continue; |
| 135 | } |
| 136 | |
Christopher Ferris | f0a760b | 2021-10-08 12:05:31 -0700 | [diff] [blame] | 137 | android::base::unique_fd dfd(openat(dirfd(dir.get()), event_name.c_str(), O_RDONLY)); |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 138 | if (dfd == -1) { |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | if (!should_add_input_device(dfd, g_allow_touch_inputs)) { |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | // Only add, we assume the user will not plug out and plug in USB device again and again :) |
| 147 | ev_add_fd(std::move(dfd), g_saved_input_cb); |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 153 | int ev_init(ev_callback input_cb, bool allow_touch_inputs) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 154 | g_epoll_fd.reset(); |
| 155 | |
| 156 | android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC)); |
| 157 | if (epoll_fd == -1) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 158 | return -1; |
| 159 | } |
| 160 | |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 161 | android::base::unique_fd inotify_fd(inotify_init1(IN_CLOEXEC)); |
| 162 | if (inotify_fd.get() == -1) { |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | if (inotify_add_watch(inotify_fd, INPUT_DEV_DIR, IN_CREATE) < 0) { |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir); |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 171 | if (!dir) { |
| 172 | return -1; |
| 173 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 174 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 175 | bool epoll_ctl_failed = false; |
| 176 | dirent* de; |
| 177 | while ((de = readdir(dir.get())) != nullptr) { |
| 178 | if (strncmp(de->d_name, "event", 5)) continue; |
| 179 | android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC)); |
| 180 | if (fd == -1) continue; |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 181 | |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 182 | if (!should_add_input_device(fd, allow_touch_inputs)) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 183 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 186 | epoll_event ev; |
| 187 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 188 | ev.data.ptr = &ev_fdinfo[g_ev_count]; |
| 189 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 190 | epoll_ctl_failed = true; |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | ev_fdinfo[g_ev_count].fd.reset(fd.release()); |
| 195 | ev_fdinfo[g_ev_count].cb = input_cb; |
| 196 | g_ev_count++; |
| 197 | g_ev_dev_count++; |
| 198 | if (g_ev_dev_count == MAX_DEVICES) break; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 199 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 200 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 201 | if (epoll_ctl_failed && !g_ev_count) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 202 | return -1; |
| 203 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 204 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 205 | g_epoll_fd.reset(epoll_fd.release()); |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 206 | |
| 207 | g_saved_input_cb = input_cb; |
| 208 | g_allow_touch_inputs = allow_touch_inputs; |
| 209 | ev_add_fd(std::move(inotify_fd), inotify_cb); |
| 210 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 211 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | int ev_get_epollfd(void) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 215 | return g_epoll_fd.get(); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 218 | int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) { |
| 219 | if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 220 | return -1; |
| 221 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 222 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 223 | epoll_event ev; |
| 224 | ev.events = EPOLLIN | EPOLLWAKEUP; |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 225 | ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 226 | int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 227 | if (!ret) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 228 | ev_fdinfo[g_ev_count].fd.reset(fd.release()); |
| 229 | ev_fdinfo[g_ev_count].cb = std::move(cb); |
| 230 | g_ev_count++; |
| 231 | g_ev_misc_count++; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 232 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 233 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 234 | return ret; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void ev_exit(void) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 238 | while (g_ev_count > 0) { |
| 239 | ev_fdinfo[--g_ev_count].fd.reset(); |
| 240 | } |
| 241 | g_ev_misc_count = 0; |
| 242 | g_ev_dev_count = 0; |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 243 | g_saved_input_cb = nullptr; |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 244 | g_epoll_fd.reset(); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | int ev_wait(int timeout) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 248 | g_polled_events_count = epoll_wait(g_epoll_fd, g_polled_events, g_ev_count, timeout); |
| 249 | if (g_polled_events_count <= 0) { |
| 250 | return -1; |
| 251 | } |
| 252 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void ev_dispatch(void) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 256 | for (int n = 0; n < g_polled_events_count; n++) { |
| 257 | FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 258 | const ev_callback& cb = fdi->cb; |
| 259 | if (cb) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 260 | cb(fdi->fd, g_polled_events[n].events); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 261 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 262 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 265 | int ev_get_input(int fd, uint32_t epevents, input_event* ev) { |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 266 | if (epevents & EPOLLIN) { |
| 267 | ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev))); |
| 268 | if (r == sizeof(*ev)) { |
| 269 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 270 | } |
Xihua Chen | a7952ac | 2018-01-12 13:42:18 +0800 | [diff] [blame] | 271 | } |
| 272 | if (epevents & EPOLLHUP) { |
| 273 | // Delete this watch |
| 274 | epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, fd, nullptr); |
| 275 | } |
| 276 | return -1; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jack Wu | 90b94b7 | 2021-09-27 17:47:36 +0800 | [diff] [blame] | 279 | int ev_sync_sw_state(const ev_set_sw_callback& set_sw_cb) { |
| 280 | // Use unsigned long to match ioctl's parameter type. |
| 281 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 282 | unsigned long sw_bits[BITS_TO_LONGS(SW_MAX)]; // NOLINT |
| 283 | |
| 284 | for (size_t i = 0; i < g_ev_dev_count; ++i) { |
| 285 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 286 | memset(sw_bits, 0, sizeof(sw_bits)); |
| 287 | |
| 288 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 289 | continue; |
| 290 | } |
| 291 | if (!test_bit(EV_SW, ev_bits)) { |
| 292 | continue; |
| 293 | } |
| 294 | if (ioctl(ev_fdinfo[i].fd, EVIOCGSW(sizeof(sw_bits)), sw_bits) == -1) { |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | for (int code = 0; code <= SW_MAX; code++) { |
| 299 | if (test_bit(code, sw_bits)) { |
| 300 | set_sw_cb(code, 1); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 308 | int ev_sync_key_state(const ev_set_key_callback& set_key_cb) { |
| 309 | // Use unsigned long to match ioctl's parameter type. |
| 310 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 311 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 312 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 313 | for (size_t i = 0; i < g_ev_dev_count; ++i) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 314 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 315 | memset(key_bits, 0, sizeof(key_bits)); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 316 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 317 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 318 | continue; |
| 319 | } |
| 320 | if (!test_bit(EV_KEY, ev_bits)) { |
| 321 | continue; |
| 322 | } |
| 323 | if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) { |
| 324 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 327 | for (int code = 0; code <= KEY_MAX; code++) { |
| 328 | if (test_bit(code, key_bits)) { |
| 329 | set_key_cb(code, 1); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Tom Marshall | 758b441 | 2017-08-24 12:57:27 +0000 | [diff] [blame] | 337 | void ev_iterate_available_keys(const std::function<void(int)>& key_detected) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 338 | // Use unsigned long to match ioctl's parameter type. |
| 339 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 340 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 341 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 342 | for (size_t i = 0; i < g_ev_dev_count; ++i) { |
| 343 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 344 | memset(key_bits, 0, sizeof(key_bits)); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 345 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 346 | // Does this device even have keys? |
| 347 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 348 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 349 | } |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 350 | if (!test_bit(EV_KEY, ev_bits)) { |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits) == -1) { |
| 355 | continue; |
| 356 | } |
| 357 | |
| 358 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 359 | if (test_bit(key_code, key_bits)) { |
Tom Marshall | 758b441 | 2017-08-24 12:57:27 +0000 | [diff] [blame] | 360 | key_detected(key_code); |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 364 | } |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 365 | |
Tom Marshall | 9ebce58 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 366 | void ev_iterate_touch_inputs(const std::function<void(int)>& touch_device_detected, |
| 367 | const std::function<void(int)>& key_detected) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 368 | for (size_t i = 0; i < g_ev_dev_count; ++i) { |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 369 | // Use unsigned long to match ioctl's parameter type. |
| 370 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT |
| 371 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 372 | continue; |
| 373 | } |
| 374 | if (!test_bit(EV_ABS, ev_bits)) { |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT |
| 379 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) { |
| 380 | continue; |
| 381 | } |
| 382 | |
Tom Marshall | 9ebce58 | 2017-08-24 13:50:01 +0000 | [diff] [blame] | 383 | touch_device_detected(ev_fdinfo[i].fd); |
| 384 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 385 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 386 | if (test_bit(key_code, key_bits)) { |
Tom Marshall | 758b441 | 2017-08-24 12:57:27 +0000 | [diff] [blame] | 387 | key_detected(key_code); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |