blob: ddd6d603f99a741e5cd04860208964cf414c2cc3 [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;
Ethan Yonker8373cfe2017-09-08 06:50:54 -050052#ifdef TW_USE_MINUI_WITH_DATA
53 void* data;
54#endif
Elliott Hughes07138192015-04-10 09:40:53 -070055};
56
Xihua Chena7952ac2018-01-12 13:42:18 +080057static bool g_allow_touch_inputs = true;
58static ev_callback g_saved_input_cb;
Tao Bao835bf092019-03-11 14:13:21 -070059static android::base::unique_fd g_epoll_fd;
60static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS];
61static int g_polled_events_count;
Elliott Hughes07138192015-04-10 09:40:53 -070062
Tao Bao835bf092019-03-11 14:13:21 -070063static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070064
Tao Bao835bf092019-03-11 14:13:21 -070065static size_t g_ev_count = 0;
66static size_t g_ev_dev_count = 0;
67static size_t g_ev_misc_count = 0;
Elliott Hughes07138192015-04-10 09:40:53 -070068
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070069static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Tao Bao835bf092019-03-11 14:13:21 -070070 return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
Elliott Hughes07138192015-04-10 09:40:53 -070071}
72
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060073#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Xihua Chena7952ac2018-01-12 13:42:18 +080074static bool should_add_input_device(int fd, bool allow_touch_inputs) {
75 // Use unsigned long to match ioctl's parameter type.
76 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
77
78 // Read the evbits of the input device.
79 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
80 return false;
81 }
82
83 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
84 // allowed if allow_touch_inputs is set.
85 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
86 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
87 return false;
88 }
89 }
90
91 return true;
92}
93
94static int inotify_cb(int fd, __unused uint32_t epevents) {
95 if (g_saved_input_cb == nullptr) return -1;
96
97 // The inotify will put one or several complete events.
98 // Should not read part of one event.
Stephane Lee12952cd2020-01-13 15:46:36 -080099 int event_len_int;
100 int ret = ioctl(fd, FIONREAD, &event_len_int);
Xihua Chena7952ac2018-01-12 13:42:18 +0800101 if (ret != 0) return -1;
Stephane Lee12952cd2020-01-13 15:46:36 -0800102 if (event_len_int < 0) return -1;
103 size_t event_len = event_len_int;
Xihua Chena7952ac2018-01-12 13:42:18 +0800104
105 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir);
106 if (!dir) {
107 return -1;
108 }
109
110 std::vector<int8_t> buf(event_len);
111
112 ssize_t r = TEMP_FAILURE_RETRY(read(fd, buf.data(), event_len));
113 if (r != event_len) {
114 return -1;
115 }
116
117 size_t offset = 0;
118 while (offset < event_len) {
119 struct inotify_event* pevent = reinterpret_cast<struct inotify_event*>(buf.data() + offset);
120 if (offset + sizeof(inotify_event) + pevent->len > event_len) {
121 // The pevent->len is too large and buffer will over flow.
122 // In general, should not happen, just make more stable.
123 return -1;
124 }
125 offset += sizeof(inotify_event) + pevent->len;
126
Christopher Ferrisf0a760b2021-10-08 12:05:31 -0700127 std::string event_name(pevent->name, pevent->len);
128 if (!android::base::StartsWith(event_name, "event")) {
Xihua Chena7952ac2018-01-12 13:42:18 +0800129 continue;
130 }
131
Christopher Ferrisf0a760b2021-10-08 12:05:31 -0700132 android::base::unique_fd dfd(openat(dirfd(dir.get()), event_name.c_str(), O_RDONLY));
Xihua Chena7952ac2018-01-12 13:42:18 +0800133 if (dfd == -1) {
134 break;
135 }
136
137 if (!should_add_input_device(dfd, g_allow_touch_inputs)) {
138 continue;
139 }
140
141 // Only add, we assume the user will not plug out and plug in USB device again and again :)
142 ev_add_fd(std::move(dfd), g_saved_input_cb);
143 }
144
145 return 0;
146}
147
Tao Bao5f8dd992017-07-28 00:05:40 -0700148int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
bigbiff20edca82020-07-03 09:55:28 -0400149#else
150#ifdef TW_USE_MINUI_WITH_DATA
151int ev_init(ev_callback input_cb, void* data) {
152#else
153int ev_init(ev_callback input_cb) {
154#endif
155 bool allow_touch_inputs = false;
156#endif
Tao Bao835bf092019-03-11 14:13:21 -0700157 g_epoll_fd.reset();
158
159 android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
160 if (epoll_fd == -1) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800161 return -1;
162 }
163
Xihua Chena7952ac2018-01-12 13:42:18 +0800164 android::base::unique_fd inotify_fd(inotify_init1(IN_CLOEXEC));
165 if (inotify_fd.get() == -1) {
166 return -1;
167 }
168
169 if (inotify_add_watch(inotify_fd, INPUT_DEV_DIR, IN_CREATE) < 0) {
170 return -1;
171 }
172
173 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir);
Tao Bao835bf092019-03-11 14:13:21 -0700174 if (!dir) {
175 return -1;
176 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800177
Tao Bao835bf092019-03-11 14:13:21 -0700178 bool epoll_ctl_failed = false;
179 dirent* de;
180 while ((de = readdir(dir.get())) != nullptr) {
181 if (strncmp(de->d_name, "event", 5)) continue;
182 android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC));
183 if (fd == -1) continue;
Tao Bao5f8dd992017-07-28 00:05:40 -0700184
bigbiff673c7ae2020-12-02 19:44:56 -0500185#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Xihua Chena7952ac2018-01-12 13:42:18 +0800186 if (!should_add_input_device(fd, allow_touch_inputs)) {
Tao Bao835bf092019-03-11 14:13:21 -0700187 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700188 }
bigbiff673c7ae2020-12-02 19:44:56 -0500189#endif
Elliott Hughes07138192015-04-10 09:40:53 -0700190
Tao Bao835bf092019-03-11 14:13:21 -0700191 epoll_event ev;
192 ev.events = EPOLLIN | EPOLLWAKEUP;
193 ev.data.ptr = &ev_fdinfo[g_ev_count];
194 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
195 epoll_ctl_failed = true;
196 continue;
197 }
198
199 ev_fdinfo[g_ev_count].fd.reset(fd.release());
200 ev_fdinfo[g_ev_count].cb = input_cb;
201 g_ev_count++;
202 g_ev_dev_count++;
203 if (g_ev_dev_count == MAX_DEVICES) break;
Tao Bao0b1118d2017-01-14 07:46:10 -0800204 }
Elliott Hughes07138192015-04-10 09:40:53 -0700205
Tao Bao835bf092019-03-11 14:13:21 -0700206 if (epoll_ctl_failed && !g_ev_count) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800207 return -1;
208 }
Elliott Hughes07138192015-04-10 09:40:53 -0700209
Tao Bao835bf092019-03-11 14:13:21 -0700210 g_epoll_fd.reset(epoll_fd.release());
Xihua Chena7952ac2018-01-12 13:42:18 +0800211
212 g_saved_input_cb = input_cb;
213 g_allow_touch_inputs = allow_touch_inputs;
bigbiff673c7ae2020-12-02 19:44:56 -0500214#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Xihua Chena7952ac2018-01-12 13:42:18 +0800215 ev_add_fd(std::move(inotify_fd), inotify_cb);
bigbiff673c7ae2020-12-02 19:44:56 -0500216#endif
Xihua Chena7952ac2018-01-12 13:42:18 +0800217
Tao Bao0b1118d2017-01-14 07:46:10 -0800218 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700219}
220
221int ev_get_epollfd(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700222 return g_epoll_fd.get();
Elliott Hughes07138192015-04-10 09:40:53 -0700223}
224
Tao Bao835bf092019-03-11 14:13:21 -0700225int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) {
226 if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800227 return -1;
228 }
Elliott Hughes07138192015-04-10 09:40:53 -0700229
Tao Bao0b1118d2017-01-14 07:46:10 -0800230 epoll_event ev;
231 ev.events = EPOLLIN | EPOLLWAKEUP;
Tao Bao835bf092019-03-11 14:13:21 -0700232 ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]);
Tao Bao0b1118d2017-01-14 07:46:10 -0800233 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
234 if (!ret) {
Tao Bao835bf092019-03-11 14:13:21 -0700235 ev_fdinfo[g_ev_count].fd.reset(fd.release());
236 ev_fdinfo[g_ev_count].cb = std::move(cb);
237 g_ev_count++;
238 g_ev_misc_count++;
Tao Bao0b1118d2017-01-14 07:46:10 -0800239 }
Elliott Hughes07138192015-04-10 09:40:53 -0700240
Tao Bao0b1118d2017-01-14 07:46:10 -0800241 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700242}
243
244void ev_exit(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700245 while (g_ev_count > 0) {
246 ev_fdinfo[--g_ev_count].fd.reset();
247 }
248 g_ev_misc_count = 0;
249 g_ev_dev_count = 0;
Xihua Chena7952ac2018-01-12 13:42:18 +0800250 g_saved_input_cb = nullptr;
Tao Bao835bf092019-03-11 14:13:21 -0700251 g_epoll_fd.reset();
Elliott Hughes07138192015-04-10 09:40:53 -0700252}
253
254int ev_wait(int timeout) {
Tao Bao835bf092019-03-11 14:13:21 -0700255 g_polled_events_count = epoll_wait(g_epoll_fd, g_polled_events, g_ev_count, timeout);
256 if (g_polled_events_count <= 0) {
257 return -1;
258 }
259 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700260}
261
262void ev_dispatch(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700263 for (int n = 0; n < g_polled_events_count; n++) {
264 FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr);
Tao Bao0b1118d2017-01-14 07:46:10 -0800265 const ev_callback& cb = fdi->cb;
266 if (cb) {
Tao Bao835bf092019-03-11 14:13:21 -0700267 cb(fdi->fd, g_polled_events[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700268 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800269 }
Elliott Hughes07138192015-04-10 09:40:53 -0700270}
271
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700272int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Xihua Chena7952ac2018-01-12 13:42:18 +0800273 if (epevents & EPOLLIN) {
274 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
275 if (r == sizeof(*ev)) {
276 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700277 }
Xihua Chena7952ac2018-01-12 13:42:18 +0800278 }
279 if (epevents & EPOLLHUP) {
280 // Delete this watch
281 epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
282 }
283 return -1;
Elliott Hughes07138192015-04-10 09:40:53 -0700284}
285
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500286#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700287int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500288#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800289int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500290#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800291 // Use unsigned long to match ioctl's parameter type.
292 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
293 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700294
Tao Bao835bf092019-03-11 14:13:21 -0700295 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800296 memset(ev_bits, 0, sizeof(ev_bits));
297 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700298
Tao Bao0b1118d2017-01-14 07:46:10 -0800299 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
300 continue;
301 }
302 if (!test_bit(EV_KEY, ev_bits)) {
303 continue;
304 }
305 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
306 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700307 }
308
Tao Bao0b1118d2017-01-14 07:46:10 -0800309 for (int code = 0; code <= KEY_MAX; code++) {
310 if (test_bit(code, key_bits)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500311#ifdef TW_USE_MINUI_WITH_DATA
312 set_key_cb(code, 1, data);
313#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800314 set_key_cb(code, 1);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500315#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800316 }
317 }
318 }
319
320 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700321}
322
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700323void ev_iterate_available_keys(const std::function<void(int)>& f) {
Tao Bao835bf092019-03-11 14:13:21 -0700324 // Use unsigned long to match ioctl's parameter type.
325 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
326 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700327
Tao Bao835bf092019-03-11 14:13:21 -0700328 for (size_t i = 0; i < g_ev_dev_count; ++i) {
329 memset(ev_bits, 0, sizeof(ev_bits));
330 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700331
Tao Bao835bf092019-03-11 14:13:21 -0700332 // Does this device even have keys?
333 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
334 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700335 }
Tao Bao835bf092019-03-11 14:13:21 -0700336 if (!test_bit(EV_KEY, ev_bits)) {
337 continue;
338 }
339
340 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits) == -1) {
341 continue;
342 }
343
344 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
345 if (test_bit(key_code, key_bits)) {
346 f(key_code);
347 }
348 }
349 }
Elliott Hughes07138192015-04-10 09:40:53 -0700350}
Tao Bao5f8dd992017-07-28 00:05:40 -0700351
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600352#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Tao Bao5f8dd992017-07-28 00:05:40 -0700353void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
Tao Bao835bf092019-03-11 14:13:21 -0700354 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700355 // Use unsigned long to match ioctl's parameter type.
356 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
357 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
358 continue;
359 }
360 if (!test_bit(EV_ABS, ev_bits)) {
361 continue;
362 }
363
364 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
365 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
366 continue;
367 }
368
369 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
370 if (test_bit(key_code, key_bits)) {
371 action(key_code);
372 }
373 }
374 }
375}
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600376#endif