blob: f331ed68a2910370c2d5030f255a9d3e5310af9b [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>
32
33#include <android-base/unique_fd.h>
Tao Bao9468fc02017-03-17 00:57:37 -070034
Tao Bao0ecbd762017-01-16 21:16:58 -080035#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070036
Xihua Chena7952ac2018-01-12 13:42:18 +080037constexpr const char* INPUT_DEV_DIR = "/dev/input";
38
Tao Bao835bf092019-03-11 14:13:21 -070039constexpr size_t MAX_DEVICES = 16;
40constexpr size_t MAX_MISC_FDS = 16;
Elliott Hughes07138192015-04-10 09:40:53 -070041
Tao Bao835bf092019-03-11 14:13:21 -070042constexpr size_t BITS_PER_LONG = sizeof(unsigned long) * 8;
43constexpr size_t BITS_TO_LONGS(size_t bits) {
44 return ((bits + BITS_PER_LONG - 1) / BITS_PER_LONG);
45}
Elliott Hughes07138192015-04-10 09:40:53 -070046
Tao Bao835bf092019-03-11 14:13:21 -070047struct FdInfo {
48 android::base::unique_fd fd;
Tao Bao0b1118d2017-01-14 07:46:10 -080049 ev_callback cb;
Elliott Hughes07138192015-04-10 09:40:53 -070050};
51
Xihua Chena7952ac2018-01-12 13:42:18 +080052static bool g_allow_touch_inputs = true;
53static ev_callback g_saved_input_cb;
Tao Bao835bf092019-03-11 14:13:21 -070054static android::base::unique_fd g_epoll_fd;
55static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS];
56static int g_polled_events_count;
Elliott Hughes07138192015-04-10 09:40:53 -070057
Tao Bao835bf092019-03-11 14:13:21 -070058static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070059
Tao Bao835bf092019-03-11 14:13:21 -070060static size_t g_ev_count = 0;
61static size_t g_ev_dev_count = 0;
62static size_t g_ev_misc_count = 0;
Elliott Hughes07138192015-04-10 09:40:53 -070063
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070064static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Tao Bao835bf092019-03-11 14:13:21 -070065 return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
Elliott Hughes07138192015-04-10 09:40:53 -070066}
67
Xihua Chena7952ac2018-01-12 13:42:18 +080068static bool should_add_input_device(int fd, bool allow_touch_inputs) {
69 // Use unsigned long to match ioctl's parameter type.
70 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
71
72 // Read the evbits of the input device.
73 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
74 return false;
75 }
76
77 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
78 // allowed if allow_touch_inputs is set.
79 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
80 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
81 return false;
82 }
83 }
84
85 return true;
86}
87
88static int inotify_cb(int fd, __unused uint32_t epevents) {
89 if (g_saved_input_cb == nullptr) return -1;
90
91 // The inotify will put one or several complete events.
92 // Should not read part of one event.
93 size_t event_len;
94 int ret = ioctl(fd, FIONREAD, &event_len);
95 if (ret != 0) return -1;
96
97 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir);
98 if (!dir) {
99 return -1;
100 }
101
102 std::vector<int8_t> buf(event_len);
103
104 ssize_t r = TEMP_FAILURE_RETRY(read(fd, buf.data(), event_len));
105 if (r != event_len) {
106 return -1;
107 }
108
109 size_t offset = 0;
110 while (offset < event_len) {
111 struct inotify_event* pevent = reinterpret_cast<struct inotify_event*>(buf.data() + offset);
112 if (offset + sizeof(inotify_event) + pevent->len > event_len) {
113 // The pevent->len is too large and buffer will over flow.
114 // In general, should not happen, just make more stable.
115 return -1;
116 }
117 offset += sizeof(inotify_event) + pevent->len;
118
119 pevent->name[pevent->len] = '\0';
120 if (strncmp(pevent->name, "event", 5)) {
121 continue;
122 }
123
124 android::base::unique_fd dfd(openat(dirfd(dir.get()), pevent->name, O_RDONLY));
125 if (dfd == -1) {
126 break;
127 }
128
129 if (!should_add_input_device(dfd, g_allow_touch_inputs)) {
130 continue;
131 }
132
133 // Only add, we assume the user will not plug out and plug in USB device again and again :)
134 ev_add_fd(std::move(dfd), g_saved_input_cb);
135 }
136
137 return 0;
138}
139
Tao Bao5f8dd992017-07-28 00:05:40 -0700140int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Tao Bao835bf092019-03-11 14:13:21 -0700141 g_epoll_fd.reset();
142
143 android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
144 if (epoll_fd == -1) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800145 return -1;
146 }
147
Xihua Chena7952ac2018-01-12 13:42:18 +0800148 android::base::unique_fd inotify_fd(inotify_init1(IN_CLOEXEC));
149 if (inotify_fd.get() == -1) {
150 return -1;
151 }
152
153 if (inotify_add_watch(inotify_fd, INPUT_DEV_DIR, IN_CREATE) < 0) {
154 return -1;
155 }
156
157 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(INPUT_DEV_DIR), closedir);
Tao Bao835bf092019-03-11 14:13:21 -0700158 if (!dir) {
159 return -1;
160 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800161
Tao Bao835bf092019-03-11 14:13:21 -0700162 bool epoll_ctl_failed = false;
163 dirent* de;
164 while ((de = readdir(dir.get())) != nullptr) {
165 if (strncmp(de->d_name, "event", 5)) continue;
166 android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC));
167 if (fd == -1) continue;
Tao Bao5f8dd992017-07-28 00:05:40 -0700168
Xihua Chena7952ac2018-01-12 13:42:18 +0800169 if (!should_add_input_device(fd, allow_touch_inputs)) {
Tao Bao835bf092019-03-11 14:13:21 -0700170 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700171 }
172
Tao Bao835bf092019-03-11 14:13:21 -0700173 epoll_event ev;
174 ev.events = EPOLLIN | EPOLLWAKEUP;
175 ev.data.ptr = &ev_fdinfo[g_ev_count];
176 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
177 epoll_ctl_failed = true;
178 continue;
179 }
180
181 ev_fdinfo[g_ev_count].fd.reset(fd.release());
182 ev_fdinfo[g_ev_count].cb = input_cb;
183 g_ev_count++;
184 g_ev_dev_count++;
185 if (g_ev_dev_count == MAX_DEVICES) break;
Tao Bao0b1118d2017-01-14 07:46:10 -0800186 }
Elliott Hughes07138192015-04-10 09:40:53 -0700187
Tao Bao835bf092019-03-11 14:13:21 -0700188 if (epoll_ctl_failed && !g_ev_count) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800189 return -1;
190 }
Elliott Hughes07138192015-04-10 09:40:53 -0700191
Tao Bao835bf092019-03-11 14:13:21 -0700192 g_epoll_fd.reset(epoll_fd.release());
Xihua Chena7952ac2018-01-12 13:42:18 +0800193
194 g_saved_input_cb = input_cb;
195 g_allow_touch_inputs = allow_touch_inputs;
196 ev_add_fd(std::move(inotify_fd), inotify_cb);
197
Tao Bao0b1118d2017-01-14 07:46:10 -0800198 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700199}
200
201int ev_get_epollfd(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700202 return g_epoll_fd.get();
Elliott Hughes07138192015-04-10 09:40:53 -0700203}
204
Tao Bao835bf092019-03-11 14:13:21 -0700205int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) {
206 if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800207 return -1;
208 }
Elliott Hughes07138192015-04-10 09:40:53 -0700209
Tao Bao0b1118d2017-01-14 07:46:10 -0800210 epoll_event ev;
211 ev.events = EPOLLIN | EPOLLWAKEUP;
Tao Bao835bf092019-03-11 14:13:21 -0700212 ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]);
Tao Bao0b1118d2017-01-14 07:46:10 -0800213 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
214 if (!ret) {
Tao Bao835bf092019-03-11 14:13:21 -0700215 ev_fdinfo[g_ev_count].fd.reset(fd.release());
216 ev_fdinfo[g_ev_count].cb = std::move(cb);
217 g_ev_count++;
218 g_ev_misc_count++;
Tao Bao0b1118d2017-01-14 07:46:10 -0800219 }
Elliott Hughes07138192015-04-10 09:40:53 -0700220
Tao Bao0b1118d2017-01-14 07:46:10 -0800221 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700222}
223
224void ev_exit(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700225 while (g_ev_count > 0) {
226 ev_fdinfo[--g_ev_count].fd.reset();
227 }
228 g_ev_misc_count = 0;
229 g_ev_dev_count = 0;
Xihua Chena7952ac2018-01-12 13:42:18 +0800230 g_saved_input_cb = nullptr;
Tao Bao835bf092019-03-11 14:13:21 -0700231 g_epoll_fd.reset();
Elliott Hughes07138192015-04-10 09:40:53 -0700232}
233
234int ev_wait(int timeout) {
Tao Bao835bf092019-03-11 14:13:21 -0700235 g_polled_events_count = epoll_wait(g_epoll_fd, g_polled_events, g_ev_count, timeout);
236 if (g_polled_events_count <= 0) {
237 return -1;
238 }
239 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700240}
241
242void ev_dispatch(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700243 for (int n = 0; n < g_polled_events_count; n++) {
244 FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr);
Tao Bao0b1118d2017-01-14 07:46:10 -0800245 const ev_callback& cb = fdi->cb;
246 if (cb) {
Tao Bao835bf092019-03-11 14:13:21 -0700247 cb(fdi->fd, g_polled_events[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700248 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800249 }
Elliott Hughes07138192015-04-10 09:40:53 -0700250}
251
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700252int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Xihua Chena7952ac2018-01-12 13:42:18 +0800253 if (epevents & EPOLLIN) {
254 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
255 if (r == sizeof(*ev)) {
256 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700257 }
Xihua Chena7952ac2018-01-12 13:42:18 +0800258 }
259 if (epevents & EPOLLHUP) {
260 // Delete this watch
261 epoll_ctl(g_epoll_fd, EPOLL_CTL_DEL, fd, nullptr);
262 }
263 return -1;
Elliott Hughes07138192015-04-10 09:40:53 -0700264}
265
Tao Bao0b1118d2017-01-14 07:46:10 -0800266int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
267 // Use unsigned long to match ioctl's parameter type.
268 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
269 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700270
Tao Bao835bf092019-03-11 14:13:21 -0700271 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800272 memset(ev_bits, 0, sizeof(ev_bits));
273 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700274
Tao Bao0b1118d2017-01-14 07:46:10 -0800275 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
276 continue;
277 }
278 if (!test_bit(EV_KEY, ev_bits)) {
279 continue;
280 }
281 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
282 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700283 }
284
Tao Bao0b1118d2017-01-14 07:46:10 -0800285 for (int code = 0; code <= KEY_MAX; code++) {
286 if (test_bit(code, key_bits)) {
287 set_key_cb(code, 1);
288 }
289 }
290 }
291
292 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700293}
294
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700295void ev_iterate_available_keys(const std::function<void(int)>& f) {
Tao Bao835bf092019-03-11 14:13:21 -0700296 // Use unsigned long to match ioctl's parameter type.
297 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
298 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700299
Tao Bao835bf092019-03-11 14:13:21 -0700300 for (size_t i = 0; i < g_ev_dev_count; ++i) {
301 memset(ev_bits, 0, sizeof(ev_bits));
302 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700303
Tao Bao835bf092019-03-11 14:13:21 -0700304 // Does this device even have keys?
305 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
306 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700307 }
Tao Bao835bf092019-03-11 14:13:21 -0700308 if (!test_bit(EV_KEY, ev_bits)) {
309 continue;
310 }
311
312 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits) == -1) {
313 continue;
314 }
315
316 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
317 if (test_bit(key_code, key_bits)) {
318 f(key_code);
319 }
320 }
321 }
Elliott Hughes07138192015-04-10 09:40:53 -0700322}
Tao Bao5f8dd992017-07-28 00:05:40 -0700323
324void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
Tao Bao835bf092019-03-11 14:13:21 -0700325 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700326 // Use unsigned long to match ioctl's parameter type.
327 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
328 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
329 continue;
330 }
331 if (!test_bit(EV_ABS, ev_bits)) {
332 continue;
333 }
334
335 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
336 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
337 continue;
338 }
339
340 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
341 if (test_bit(key_code, key_bits)) {
342 action(key_code);
343 }
344 }
345 }
346}