blob: 3db58aefc479afb05560c0b0a32065ef384f82a5 [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 Chen8f693492018-01-12 13:42:18 +080025#include <sys/inotify.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080026#include <sys/ioctl.h>
Elliott Hughes07138192015-04-10 09:40:53 -070027#include <unistd.h>
28
Tao Bao9468fc02017-03-17 00:57:37 -070029#include <functional>
30
Tao Bao0ecbd762017-01-16 21:16:58 -080031#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070032
33#define MAX_DEVICES 16
34#define MAX_MISC_FDS 16
35
36#define BITS_PER_LONG (sizeof(unsigned long) * 8)
37#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
38
39struct fd_info {
Tao Bao0b1118d2017-01-14 07:46:10 -080040 int fd;
41 ev_callback cb;
Elliott Hughes07138192015-04-10 09:40:53 -070042};
43
Xihua Chen8f693492018-01-12 13:42:18 +080044static ev_callback saved_input_cb;
Elliott Hughes07138192015-04-10 09:40:53 -070045static int g_epoll_fd;
Xihua Chen8f693492018-01-12 13:42:18 +080046static int g_inotify_fd;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070047static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070048static int npolledevents;
49
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070050static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070051
52static unsigned ev_count = 0;
53static unsigned ev_dev_count = 0;
54static unsigned ev_misc_count = 0;
55
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070056static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -070057 return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
58}
59
Xihua Chen8f693492018-01-12 13:42:18 +080060static int inotify_cb(int fd, __unused uint32_t epevents) {
61 struct inotify_event* pevent;
62 char* buf;
63 DIR* dir;
64 size_t event_len;
65 size_t offset;
66
67 if (saved_input_cb == nullptr) return -1;
68
69 // The inotify will put one or several complete events.
70 // Should not read part of one event.
71 int ret = ioctl(fd, FIONREAD, &event_len);
72 if (ret != 0) return -1;
73
74 dir = opendir("/dev/input");
75 if (dir == nullptr) return -1;
76
77 buf = (char*)malloc(event_len);
78 if (buf == nullptr) {
79 closedir(dir);
80 return -1;
81 }
82
83 ret = read(fd, buf, event_len);
84 if (ret != (int)event_len) {
85 free(buf);
86 closedir(dir);
87 return -1;
88 }
89
90 offset = 0;
91 while (offset < event_len) {
92 pevent = (struct inotify_event*)(buf + offset);
93 if (offset + sizeof(inotify_event) + pevent->len > event_len) {
94 // The pevent->len is too large and buffer will over flow.
95 // In general, should not happen, just make more stable.
96 free(buf);
97 closedir(dir);
98 return -1;
99 }
100 offset += sizeof(inotify_event) + pevent->len;
101
102 pevent->name[pevent->len] = '\0';
103 if (strncmp(pevent->name, "event", 5)) continue;
104
105 int dfd = openat(dirfd(dir), pevent->name, O_RDONLY);
106 if (dfd == -1) break;
107
108 // Read the evbits of the input device.
109 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
110 if (ioctl(dfd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
111 close(dfd);
112 continue;
113 }
114 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed.
115 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
116 close(dfd);
117 continue;
118 }
119
120 // Only add, we assume the user will not plug out and plug in USB device again and again :)
121 ev_add_fd(dfd, saved_input_cb);
122 }
123
124 free(buf);
125 closedir(dir);
126 return 0;
127}
128
Tao Bao5f8dd992017-07-28 00:05:40 -0700129int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800130 g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
131 if (g_epoll_fd == -1) {
132 return -1;
133 }
134
Xihua Chen8f693492018-01-12 13:42:18 +0800135 g_inotify_fd = inotify_init();
136 if (g_inotify_fd >= 0) {
137 inotify_add_watch(g_inotify_fd, "/dev/input", IN_CREATE);
138 ev_add_fd(g_inotify_fd, inotify_cb);
139 }
140
Tao Bao5f8dd992017-07-28 00:05:40 -0700141 bool epollctlfail = false;
Tao Bao0b1118d2017-01-14 07:46:10 -0800142 DIR* dir = opendir("/dev/input");
Tao Bao5f8dd992017-07-28 00:05:40 -0700143 if (dir != nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800144 dirent* de;
145 while ((de = readdir(dir))) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800146 if (strncmp(de->d_name, "event", 5)) continue;
147 int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
148 if (fd == -1) continue;
149
Tao Bao5f8dd992017-07-28 00:05:40 -0700150 // Use unsigned long to match ioctl's parameter type.
151 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
152
Tao Bao0b1118d2017-01-14 07:46:10 -0800153 // Read the evbits of the input device.
154 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
155 close(fd);
156 continue;
157 }
158
Vitalii Kulikov9d569012017-01-16 22:36:29 +0100159 // We assume that only EV_KEY, and EV_SW event types are ever needed. EV_ABS is also
Tao Bao5f8dd992017-07-28 00:05:40 -0700160 // allowed if allow_touch_inputs is set.
Vitalii Kulikov9d569012017-01-16 22:36:29 +0100161 // EV_REL should be enabled explicitly in device tree.
162 if (!test_bit(EV_KEY, ev_bits) &&
163#ifdef BOARD_RECOVERY_NEEDS_REL_INPUT
164 !test_bit(EV_REL, ev_bits) &&
165#endif
166 !test_bit(EV_SW, ev_bits)) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700167 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
168 close(fd);
169 continue;
170 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800171 }
172
173 epoll_event ev;
174 ev.events = EPOLLIN | EPOLLWAKEUP;
175 ev.data.ptr = &ev_fdinfo[ev_count];
176 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
177 close(fd);
178 epollctlfail = true;
179 continue;
180 }
181
182 ev_fdinfo[ev_count].fd = fd;
Tao Bao232ba3f2019-03-08 16:20:07 -0800183 ev_fdinfo[ev_count].cb = input_cb;
Tao Bao0b1118d2017-01-14 07:46:10 -0800184 ev_count++;
185 ev_dev_count++;
186 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -0700187 }
188
Tao Bao0b1118d2017-01-14 07:46:10 -0800189 closedir(dir);
190 }
Elliott Hughes07138192015-04-10 09:40:53 -0700191
Tao Bao0b1118d2017-01-14 07:46:10 -0800192 if (epollctlfail && !ev_count) {
193 close(g_epoll_fd);
194 g_epoll_fd = -1;
195 return -1;
196 }
Elliott Hughes07138192015-04-10 09:40:53 -0700197
Xihua Chen8f693492018-01-12 13:42:18 +0800198 saved_input_cb = input_cb;
199
Tao Bao0b1118d2017-01-14 07:46:10 -0800200 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700201}
202
203int ev_get_epollfd(void) {
204 return g_epoll_fd;
205}
206
Tao Bao0b1118d2017-01-14 07:46:10 -0800207int ev_add_fd(int fd, ev_callback cb) {
208 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
209 return -1;
210 }
Elliott Hughes07138192015-04-10 09:40:53 -0700211
Tao Bao0b1118d2017-01-14 07:46:10 -0800212 epoll_event ev;
213 ev.events = EPOLLIN | EPOLLWAKEUP;
214 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
215 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
216 if (!ret) {
217 ev_fdinfo[ev_count].fd = fd;
218 ev_fdinfo[ev_count].cb = std::move(cb);
219 ev_count++;
220 ev_misc_count++;
221 }
Elliott Hughes07138192015-04-10 09:40:53 -0700222
Tao Bao0b1118d2017-01-14 07:46:10 -0800223 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700224}
225
226void ev_exit(void) {
227 while (ev_count > 0) {
228 close(ev_fdinfo[--ev_count].fd);
229 }
230 ev_misc_count = 0;
231 ev_dev_count = 0;
Xihua Chen8f693492018-01-12 13:42:18 +0800232 saved_input_cb = nullptr;
Elliott Hughes07138192015-04-10 09:40:53 -0700233 close(g_epoll_fd);
234}
235
236int ev_wait(int timeout) {
237 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
238 if (npolledevents <= 0) {
239 return -1;
240 }
241 return 0;
242}
243
244void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800245 for (int n = 0; n < npolledevents; n++) {
246 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
247 const ev_callback& cb = fdi->cb;
248 if (cb) {
249 cb(fdi->fd, polledevents[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700250 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800251 }
Elliott Hughes07138192015-04-10 09:40:53 -0700252}
253
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700254int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700255 if (epevents & EPOLLIN) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700256 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700257 if (r == sizeof(*ev)) {
258 return 0;
259 }
260 }
261 return -1;
262}
263
Tao Bao0b1118d2017-01-14 07:46:10 -0800264int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
265 // Use unsigned long to match ioctl's parameter type.
266 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
267 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700268
Tao Bao0b1118d2017-01-14 07:46:10 -0800269 for (size_t i = 0; i < ev_dev_count; ++i) {
270 memset(ev_bits, 0, sizeof(ev_bits));
271 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700272
Tao Bao0b1118d2017-01-14 07:46:10 -0800273 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
274 continue;
275 }
276 if (!test_bit(EV_KEY, ev_bits)) {
277 continue;
278 }
279 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
280 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700281 }
282
Tao Bao0b1118d2017-01-14 07:46:10 -0800283 for (int code = 0; code <= KEY_MAX; code++) {
284 if (test_bit(code, key_bits)) {
285 set_key_cb(code, 1);
286 }
287 }
288 }
289
290 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700291}
292
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700293void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700294 // Use unsigned long to match ioctl's parameter type.
295 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
296 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700297
298 for (size_t i = 0; i < ev_dev_count; ++i) {
299 memset(ev_bits, 0, sizeof(ev_bits));
300 memset(key_bits, 0, sizeof(key_bits));
301
302 // Does this device even have keys?
303 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
304 continue;
305 }
306 if (!test_bit(EV_KEY, ev_bits)) {
307 continue;
308 }
309
310 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
311 if (rc == -1) {
312 continue;
313 }
314
315 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
316 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700317 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700318 }
319 }
320 }
321}
Tao Bao5f8dd992017-07-28 00:05:40 -0700322
Tom Marshall74f48242017-08-24 13:50:01 +0000323void ev_iterate_touch_inputs(const std::function<void(int)>& touch_device_detected,
324 const std::function<void(int)>& key_detected) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700325 for (size_t i = 0; i < ev_dev_count; ++i) {
326 // 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
Tom Marshall74f48242017-08-24 13:50:01 +0000340 touch_device_detected(ev_fdinfo[i].fd);
341
Tao Bao5f8dd992017-07-28 00:05:40 -0700342 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
343 if (test_bit(key_code, key_bits)) {
Tom Marshallc0d6e1e2017-08-24 12:57:27 +0000344 key_detected(key_code);
Tao Bao5f8dd992017-07-28 00:05:40 -0700345 }
346 }
347 }
348}