blob: b307a4977197722b075ef4374bb2c68887c0f84e [file] [log] [blame]
Elliott Hughes07138192015-04-10 09:40:53 -07001/*
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 Albertc8686b42017-10-11 11:47:54 -070018#include <errno.h>
Elliott Hughes25a29d42017-02-23 10:45:42 -080019#include <fcntl.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080020#include <linux/input.h>
Elliott Hughes07138192015-04-10 09:40:53 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/epoll.h>
Xihua Chena7952ac2018-01-12 13:42:18 +080025#include <sys/inotify.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080026#include <sys/ioctl.h>
Tao Bao835bf092019-03-11 14:13:21 -070027#include <sys/types.h>
Elliott Hughes07138192015-04-10 09:40:53 -070028#include <unistd.h>
29
Tao Bao9468fc02017-03-17 00:57:37 -070030#include <functional>
Tao Bao835bf092019-03-11 14:13:21 -070031#include <memory>
Christopher Ferrisf0a760b2021-10-08 12:05:31 -070032#include <string>
Tao Bao835bf092019-03-11 14:13:21 -070033
Christopher Ferrisf0a760b2021-10-08 12:05:31 -070034#include <android-base/strings.h>
Tao Bao835bf092019-03-11 14:13:21 -070035#include <android-base/unique_fd.h>
Tao Bao9468fc02017-03-17 00:57:37 -070036
Tao Bao0ecbd762017-01-16 21:16:58 -080037#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070038
Xihua Chena7952ac2018-01-12 13:42:18 +080039constexpr const char* INPUT_DEV_DIR = "/dev/input";
40
Tao Bao835bf092019-03-11 14:13:21 -070041constexpr size_t MAX_DEVICES = 16;
42constexpr size_t MAX_MISC_FDS = 16;
Elliott Hughes07138192015-04-10 09:40:53 -070043
Tao Bao835bf092019-03-11 14:13:21 -070044constexpr size_t BITS_PER_LONG = sizeof(unsigned long) * 8;
45constexpr size_t BITS_TO_LONGS(size_t bits) {
46 return ((bits + BITS_PER_LONG - 1) / BITS_PER_LONG);
47}
Elliott Hughes07138192015-04-10 09:40:53 -070048
Tao Bao835bf092019-03-11 14:13:21 -070049struct FdInfo {
50 android::base::unique_fd fd;
Tao Bao0b1118d2017-01-14 07:46:10 -080051 ev_callback cb;
Elliott Hughes07138192015-04-10 09:40:53 -070052};
53
Xihua Chena7952ac2018-01-12 13:42:18 +080054static bool g_allow_touch_inputs = true;
55static ev_callback g_saved_input_cb;
Tao Bao835bf092019-03-11 14:13:21 -070056static android::base::unique_fd g_epoll_fd;
57static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS];
58static int g_polled_events_count;
Elliott Hughes07138192015-04-10 09:40:53 -070059
Tao Bao835bf092019-03-11 14:13:21 -070060static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070061
Tao Bao835bf092019-03-11 14:13:21 -070062static size_t g_ev_count = 0;
63static size_t g_ev_dev_count = 0;
64static size_t g_ev_misc_count = 0;
Elliott Hughes07138192015-04-10 09:40:53 -070065
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070066static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Tao Bao835bf092019-03-11 14:13:21 -070067 return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
Elliott Hughes07138192015-04-10 09:40:53 -070068}
69
Xihua Chena7952ac2018-01-12 13:42:18 +080070static bool should_add_input_device(int fd, bool allow_touch_inputs) {
71 // Use unsigned long to match ioctl's parameter type.
72 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
73
74 // Read the evbits of the input device.
75 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
76 return false;
77 }
78
79 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
80 // allowed if allow_touch_inputs is set.
81 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
82 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
83 return false;
84 }
85 }
86
87 return true;
88}
89
90static int inotify_cb(int fd, __unused uint32_t epevents) {
91 if (g_saved_input_cb == nullptr) return -1;
92
93 // The inotify will put one or several complete events.
94 // Should not read part of one event.
Stephane Lee12952cd2020-01-13 15:46:36 -080095 int event_len_int;
96 int ret = ioctl(fd, FIONREAD, &event_len_int);
Xihua Chena7952ac2018-01-12 13:42:18 +080097 if (ret != 0) return -1;
Stephane Lee12952cd2020-01-13 15:46:36 -080098 if (event_len_int < 0) return -1;
99 size_t event_len = event_len_int;
Xihua Chena7952ac2018-01-12 13:42:18 +0800100
101 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir);
102 if (!dir) {
103 return -1;
104 }
105
106 std::vector<int8_t> buf(event_len);
107
108 ssize_t r = TEMP_FAILURE_RETRY(read(fd, buf.data(), event_len));
109 if (r != event_len) {
110 return -1;
111 }
112
113 size_t offset = 0;
114 while (offset < event_len) {
115 struct inotify_event* pevent = reinterpret_cast<struct inotify_event*>(buf.data() + offset);
116 if (offset + sizeof(inotify_event) + pevent->len > event_len) {
117 // The pevent->len is too large and buffer will over flow.
118 // In general, should not happen, just make more stable.
119 return -1;
120 }
121 offset += sizeof(inotify_event) + pevent->len;
122
Christopher Ferrisf0a760b2021-10-08 12:05:31 -0700123 std::string event_name(pevent->name, pevent->len);
124 if (!android::base::StartsWith(event_name, "event")) {
Xihua Chena7952ac2018-01-12 13:42:18 +0800125 continue;
126 }
127
Christopher Ferrisf0a760b2021-10-08 12:05:31 -0700128 android::base::unique_fd dfd(openat(dirfd(dir.get()), event_name.c_str(), O_RDONLY));
Xihua Chena7952ac2018-01-12 13:42:18 +0800129 if (dfd == -1) {
130 break;
131 }
132
133 if (!should_add_input_device(dfd, g_allow_touch_inputs)) {
134 continue;
135 }
136
137 // Only add, we assume the user will not plug out and plug in USB device again and again :)
138 ev_add_fd(std::move(dfd), g_saved_input_cb);
139 }
140
141 return 0;
142}
143
Tao Bao5f8dd992017-07-28 00:05:40 -0700144int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Tao Bao835bf092019-03-11 14:13:21 -0700145 g_epoll_fd.reset();
146
147 android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
148 if (epoll_fd == -1) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800149 return -1;
150 }
151
Xihua Chena7952ac2018-01-12 13:42:18 +0800152 android::base::unique_fd inotify_fd(inotify_init1(IN_CLOEXEC));
153 if (inotify_fd.get() == -1) {
154 return -1;
155 }
156
157 if (inotify_add_watch(inotify_fd, INPUT_DEV_DIR, IN_CREATE) < 0) {
158 return -1;
159 }
160
161 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir);
Tao Bao835bf092019-03-11 14:13:21 -0700162 if (!dir) {
163 return -1;
164 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800165
Tao Bao835bf092019-03-11 14:13:21 -0700166 bool epoll_ctl_failed = false;
167 dirent* de;
168 while ((de = readdir(dir.get())) != nullptr) {
169 if (strncmp(de->d_name, "event", 5)) continue;
170 android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC));
171 if (fd == -1) continue;
Tao Bao5f8dd992017-07-28 00:05:40 -0700172
Xihua Chena7952ac2018-01-12 13:42:18 +0800173 if (!should_add_input_device(fd, allow_touch_inputs)) {
Tao Bao835bf092019-03-11 14:13:21 -0700174 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700175 }
176
Tao Bao835bf092019-03-11 14:13:21 -0700177 epoll_event ev;
178 ev.events = EPOLLIN | EPOLLWAKEUP;
179 ev.data.ptr = &ev_fdinfo[g_ev_count];
180 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
181 epoll_ctl_failed = true;
182 continue;
183 }
184
185 ev_fdinfo[g_ev_count].fd.reset(fd.release());
186 ev_fdinfo[g_ev_count].cb = input_cb;
187 g_ev_count++;
188 g_ev_dev_count++;
189 if (g_ev_dev_count == MAX_DEVICES) break;
Tao Bao0b1118d2017-01-14 07:46:10 -0800190 }
Elliott Hughes07138192015-04-10 09:40:53 -0700191
Tao Bao835bf092019-03-11 14:13:21 -0700192 if (epoll_ctl_failed && !g_ev_count) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800193 return -1;
194 }
Elliott Hughes07138192015-04-10 09:40:53 -0700195
Tao Bao835bf092019-03-11 14:13:21 -0700196 g_epoll_fd.reset(epoll_fd.release());
Xihua Chena7952ac2018-01-12 13:42:18 +0800197
198 g_saved_input_cb = input_cb;
199 g_allow_touch_inputs = allow_touch_inputs;
200 ev_add_fd(std::move(inotify_fd), inotify_cb);
201
Tao Bao0b1118d2017-01-14 07:46:10 -0800202 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700203}
204
205int ev_get_epollfd(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700206 return g_epoll_fd.get();
Elliott Hughes07138192015-04-10 09:40:53 -0700207}
208
Tao Bao835bf092019-03-11 14:13:21 -0700209int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) {
210 if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800211 return -1;
212 }
Elliott Hughes07138192015-04-10 09:40:53 -0700213
Tao Bao0b1118d2017-01-14 07:46:10 -0800214 epoll_event ev;
215 ev.events = EPOLLIN | EPOLLWAKEUP;
Tao Bao835bf092019-03-11 14:13:21 -0700216 ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]);
Tao Bao0b1118d2017-01-14 07:46:10 -0800217 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
218 if (!ret) {
Tao Bao835bf092019-03-11 14:13:21 -0700219 ev_fdinfo[g_ev_count].fd.reset(fd.release());
220 ev_fdinfo[g_ev_count].cb = std::move(cb);
221 g_ev_count++;
222 g_ev_misc_count++;
Tao Bao0b1118d2017-01-14 07:46:10 -0800223 }
Elliott Hughes07138192015-04-10 09:40:53 -0700224
Tao Bao0b1118d2017-01-14 07:46:10 -0800225 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700226}
227
228void ev_exit(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700229 while (g_ev_count > 0) {
230 ev_fdinfo[--g_ev_count].fd.reset();
231 }
232 g_ev_misc_count = 0;
233 g_ev_dev_count = 0;
Xihua Chena7952ac2018-01-12 13:42:18 +0800234 g_saved_input_cb = nullptr;
Tao Bao835bf092019-03-11 14:13:21 -0700235 g_epoll_fd.reset();
Elliott Hughes07138192015-04-10 09:40:53 -0700236}
237
238int ev_wait(int timeout) {
Tao Bao835bf092019-03-11 14:13:21 -0700239 g_polled_events_count = epoll_wait(g_epoll_fd, g_polled_events, g_ev_count, timeout);
240 if (g_polled_events_count <= 0) {
241 return -1;
242 }
243 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700244}
245
246void ev_dispatch(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700247 for (int n = 0; n < g_polled_events_count; n++) {
248 FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr);
Tao Bao0b1118d2017-01-14 07:46:10 -0800249 const ev_callback& cb = fdi->cb;
250 if (cb) {
Tao Bao835bf092019-03-11 14:13:21 -0700251 cb(fdi->fd, g_polled_events[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700252 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800253 }
Elliott Hughes07138192015-04-10 09:40:53 -0700254}
255
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700256int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Xihua Chena7952ac2018-01-12 13:42:18 +0800257 if (epevents & EPOLLIN) {
258 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
259 if (r == sizeof(*ev)) {
260 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700261 }
Xihua Chena7952ac2018-01-12 13:42:18 +0800262 }
263 if (epevents & EPOLLHUP) {
264 // Delete this watch
265 epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
266 }
267 return -1;
Elliott Hughes07138192015-04-10 09:40:53 -0700268}
269
Jack Wu90b94b72021-09-27 17:47:36 +0800270int ev_sync_sw_state(const ev_set_sw_callback& set_sw_cb) {
271 // Use unsigned long to match ioctl's parameter type.
272 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
273 unsigned long sw_bits[BITS_TO_LONGS(SW_MAX)]; // NOLINT
274
275 for (size_t i = 0; i < g_ev_dev_count; ++i) {
276 memset(ev_bits, 0, sizeof(ev_bits));
277 memset(sw_bits, 0, sizeof(sw_bits));
278
279 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
280 continue;
281 }
282 if (!test_bit(EV_SW, ev_bits)) {
283 continue;
284 }
285 if (ioctl(ev_fdinfo[i].fd, EVIOCGSW(sizeof(sw_bits)), sw_bits) == -1) {
286 continue;
287 }
288
289 for (int code = 0; code <= SW_MAX; code++) {
290 if (test_bit(code, sw_bits)) {
291 set_sw_cb(code, 1);
292 }
293 }
294 }
295
296 return 0;
297}
298
Tao Bao0b1118d2017-01-14 07:46:10 -0800299int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
300 // Use unsigned long to match ioctl's parameter type.
301 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
302 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700303
Tao Bao835bf092019-03-11 14:13:21 -0700304 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800305 memset(ev_bits, 0, sizeof(ev_bits));
306 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700307
Tao Bao0b1118d2017-01-14 07:46:10 -0800308 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
309 continue;
310 }
311 if (!test_bit(EV_KEY, ev_bits)) {
312 continue;
313 }
314 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
315 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700316 }
317
Tao Bao0b1118d2017-01-14 07:46:10 -0800318 for (int code = 0; code <= KEY_MAX; code++) {
319 if (test_bit(code, key_bits)) {
320 set_key_cb(code, 1);
321 }
322 }
323 }
324
325 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700326}
327
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700328void ev_iterate_available_keys(const std::function<void(int)>& f) {
Tao Bao835bf092019-03-11 14:13:21 -0700329 // Use unsigned long to match ioctl's parameter type.
330 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
331 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700332
Tao Bao835bf092019-03-11 14:13:21 -0700333 for (size_t i = 0; i < g_ev_dev_count; ++i) {
334 memset(ev_bits, 0, sizeof(ev_bits));
335 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700336
Tao Bao835bf092019-03-11 14:13:21 -0700337 // Does this device even have keys?
338 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
339 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700340 }
Tao Bao835bf092019-03-11 14:13:21 -0700341 if (!test_bit(EV_KEY, ev_bits)) {
342 continue;
343 }
344
345 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits) == -1) {
346 continue;
347 }
348
349 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
350 if (test_bit(key_code, key_bits)) {
351 f(key_code);
352 }
353 }
354 }
Elliott Hughes07138192015-04-10 09:40:53 -0700355}
Tao Bao5f8dd992017-07-28 00:05:40 -0700356
357void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
Tao Bao835bf092019-03-11 14:13:21 -0700358 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700359 // Use unsigned long to match ioctl's parameter type.
360 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
361 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
362 continue;
363 }
364 if (!test_bit(EV_ABS, ev_bits)) {
365 continue;
366 }
367
368 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
369 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
370 continue;
371 }
372
373 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
374 if (test_bit(key_code, key_bits)) {
375 action(key_code);
376 }
377 }
378 }
379}