blob: 592b58c9c4ce2d295a866cf2a311c3ac5da0c143 [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
Vitalii Kulikovf9380892017-01-16 22:36:29 +010034#include <android-base/properties.h>
Christopher Ferrisf0a760b2021-10-08 12:05:31 -070035#include <android-base/strings.h>
Tao Bao835bf092019-03-11 14:13:21 -070036#include <android-base/unique_fd.h>
Tao Bao9468fc02017-03-17 00:57:37 -070037
Tao Bao0ecbd762017-01-16 21:16:58 -080038#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070039
Xihua Chena7952ac2018-01-12 13:42:18 +080040constexpr const char* INPUT_DEV_DIR = "/dev/input";
41
Tao Bao835bf092019-03-11 14:13:21 -070042constexpr size_t MAX_DEVICES = 16;
43constexpr size_t MAX_MISC_FDS = 16;
Elliott Hughes07138192015-04-10 09:40:53 -070044
Tao Bao835bf092019-03-11 14:13:21 -070045constexpr size_t BITS_PER_LONG = sizeof(unsigned long) * 8;
46constexpr size_t BITS_TO_LONGS(size_t bits) {
47 return ((bits + BITS_PER_LONG - 1) / BITS_PER_LONG);
48}
Elliott Hughes07138192015-04-10 09:40:53 -070049
Tao Bao835bf092019-03-11 14:13:21 -070050struct FdInfo {
51 android::base::unique_fd fd;
Tao Bao0b1118d2017-01-14 07:46:10 -080052 ev_callback cb;
Elliott Hughes07138192015-04-10 09:40:53 -070053};
54
Xihua Chena7952ac2018-01-12 13:42:18 +080055static bool g_allow_touch_inputs = true;
56static ev_callback g_saved_input_cb;
Tao Bao835bf092019-03-11 14:13:21 -070057static android::base::unique_fd g_epoll_fd;
58static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS];
59static int g_polled_events_count;
Elliott Hughes07138192015-04-10 09:40:53 -070060
Tao Bao835bf092019-03-11 14:13:21 -070061static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070062
Tao Bao835bf092019-03-11 14:13:21 -070063static size_t g_ev_count = 0;
64static size_t g_ev_dev_count = 0;
65static size_t g_ev_misc_count = 0;
Elliott Hughes07138192015-04-10 09:40:53 -070066
Vitalii Kulikovf9380892017-01-16 22:36:29 +010067static 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 Hsieh54a27472016-04-18 11:30:55 -070072static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Tao Bao835bf092019-03-11 14:13:21 -070073 return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
Elliott Hughes07138192015-04-10 09:40:53 -070074}
75
Xihua Chena7952ac2018-01-12 13:42:18 +080076static 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 Kulikovf9380892017-01-16 22:36:29 +010085 // 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 Chena7952ac2018-01-12 13:42:18 +080091 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
92 return false;
93 }
94 }
95
96 return true;
97}
98
99static 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 Lee12952cd2020-01-13 15:46:36 -0800104 int event_len_int;
105 int ret = ioctl(fd, FIONREAD, &event_len_int);
Xihua Chena7952ac2018-01-12 13:42:18 +0800106 if (ret != 0) return -1;
Stephane Lee12952cd2020-01-13 15:46:36 -0800107 if (event_len_int < 0) return -1;
108 size_t event_len = event_len_int;
Xihua Chena7952ac2018-01-12 13:42:18 +0800109
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 Ferrisf0a760b2021-10-08 12:05:31 -0700132 std::string event_name(pevent->name, pevent->len);
133 if (!android::base::StartsWith(event_name, "event")) {
Xihua Chena7952ac2018-01-12 13:42:18 +0800134 continue;
135 }
136
Christopher Ferrisf0a760b2021-10-08 12:05:31 -0700137 android::base::unique_fd dfd(openat(dirfd(dir.get()), event_name.c_str(), O_RDONLY));
Xihua Chena7952ac2018-01-12 13:42:18 +0800138 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 Bao5f8dd992017-07-28 00:05:40 -0700153int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Tao Bao835bf092019-03-11 14:13:21 -0700154 g_epoll_fd.reset();
155
156 android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
157 if (epoll_fd == -1) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800158 return -1;
159 }
160
Xihua Chena7952ac2018-01-12 13:42:18 +0800161 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 Bao835bf092019-03-11 14:13:21 -0700171 if (!dir) {
172 return -1;
173 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800174
Tao Bao835bf092019-03-11 14:13:21 -0700175 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 Bao5f8dd992017-07-28 00:05:40 -0700181
Xihua Chena7952ac2018-01-12 13:42:18 +0800182 if (!should_add_input_device(fd, allow_touch_inputs)) {
Tao Bao835bf092019-03-11 14:13:21 -0700183 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700184 }
185
Tao Bao835bf092019-03-11 14:13:21 -0700186 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 Bao0b1118d2017-01-14 07:46:10 -0800199 }
Elliott Hughes07138192015-04-10 09:40:53 -0700200
Tao Bao835bf092019-03-11 14:13:21 -0700201 if (epoll_ctl_failed && !g_ev_count) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800202 return -1;
203 }
Elliott Hughes07138192015-04-10 09:40:53 -0700204
Tao Bao835bf092019-03-11 14:13:21 -0700205 g_epoll_fd.reset(epoll_fd.release());
Xihua Chena7952ac2018-01-12 13:42:18 +0800206
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 Bao0b1118d2017-01-14 07:46:10 -0800211 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700212}
213
214int ev_get_epollfd(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700215 return g_epoll_fd.get();
Elliott Hughes07138192015-04-10 09:40:53 -0700216}
217
Tao Bao835bf092019-03-11 14:13:21 -0700218int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) {
219 if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800220 return -1;
221 }
Elliott Hughes07138192015-04-10 09:40:53 -0700222
Tao Bao0b1118d2017-01-14 07:46:10 -0800223 epoll_event ev;
224 ev.events = EPOLLIN | EPOLLWAKEUP;
Tao Bao835bf092019-03-11 14:13:21 -0700225 ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]);
Tao Bao0b1118d2017-01-14 07:46:10 -0800226 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
227 if (!ret) {
Tao Bao835bf092019-03-11 14:13:21 -0700228 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 Bao0b1118d2017-01-14 07:46:10 -0800232 }
Elliott Hughes07138192015-04-10 09:40:53 -0700233
Tao Bao0b1118d2017-01-14 07:46:10 -0800234 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700235}
236
237void ev_exit(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700238 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 Chena7952ac2018-01-12 13:42:18 +0800243 g_saved_input_cb = nullptr;
Tao Bao835bf092019-03-11 14:13:21 -0700244 g_epoll_fd.reset();
Elliott Hughes07138192015-04-10 09:40:53 -0700245}
246
247int ev_wait(int timeout) {
Tao Bao835bf092019-03-11 14:13:21 -0700248 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 Hughes07138192015-04-10 09:40:53 -0700253}
254
255void ev_dispatch(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700256 for (int n = 0; n < g_polled_events_count; n++) {
257 FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr);
Tao Bao0b1118d2017-01-14 07:46:10 -0800258 const ev_callback& cb = fdi->cb;
259 if (cb) {
Tao Bao835bf092019-03-11 14:13:21 -0700260 cb(fdi->fd, g_polled_events[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700261 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800262 }
Elliott Hughes07138192015-04-10 09:40:53 -0700263}
264
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700265int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Xihua Chena7952ac2018-01-12 13:42:18 +0800266 if (epevents & EPOLLIN) {
267 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
268 if (r == sizeof(*ev)) {
269 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700270 }
Xihua Chena7952ac2018-01-12 13:42:18 +0800271 }
272 if (epevents & EPOLLHUP) {
273 // Delete this watch
274 epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
275 }
276 return -1;
Elliott Hughes07138192015-04-10 09:40:53 -0700277}
278
Jack Wu90b94b72021-09-27 17:47:36 +0800279int 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 Bao0b1118d2017-01-14 07:46:10 -0800308int 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 Hughes07138192015-04-10 09:40:53 -0700312
Tao Bao835bf092019-03-11 14:13:21 -0700313 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800314 memset(ev_bits, 0, sizeof(ev_bits));
315 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700316
Tao Bao0b1118d2017-01-14 07:46:10 -0800317 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 Hughes07138192015-04-10 09:40:53 -0700325 }
326
Tao Bao0b1118d2017-01-14 07:46:10 -0800327 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 Hughes07138192015-04-10 09:40:53 -0700335}
336
Tom Marshall758b4412017-08-24 12:57:27 +0000337void ev_iterate_available_keys(const std::function<void(int)>& key_detected) {
Tao Bao835bf092019-03-11 14:13:21 -0700338 // 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 Hughes07138192015-04-10 09:40:53 -0700341
Tao Bao835bf092019-03-11 14:13:21 -0700342 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 Hughes07138192015-04-10 09:40:53 -0700345
Tao Bao835bf092019-03-11 14:13:21 -0700346 // Does this device even have keys?
347 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
348 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700349 }
Tao Bao835bf092019-03-11 14:13:21 -0700350 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 Marshall758b4412017-08-24 12:57:27 +0000360 key_detected(key_code);
Tao Bao835bf092019-03-11 14:13:21 -0700361 }
362 }
363 }
Elliott Hughes07138192015-04-10 09:40:53 -0700364}
Tao Bao5f8dd992017-07-28 00:05:40 -0700365
Tom Marshall9ebce582017-08-24 13:50:01 +0000366void ev_iterate_touch_inputs(const std::function<void(int)>& touch_device_detected,
367 const std::function<void(int)>& key_detected) {
Tao Bao835bf092019-03-11 14:13:21 -0700368 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700369 // 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 Marshall9ebce582017-08-24 13:50:01 +0000383 touch_device_detected(ev_fdinfo[i].fd);
384
Tao Bao5f8dd992017-07-28 00:05:40 -0700385 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
386 if (test_bit(key_code, key_bits)) {
Tom Marshall758b4412017-08-24 12:57:27 +0000387 key_detected(key_code);
Tao Bao5f8dd992017-07-28 00:05:40 -0700388 }
389 }
390 }
391}