blob: b4dcb5da8033a23747877e809c8eb5ac5c525ed3 [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 Hughes07138192015-04-10 09:40:53 -070019#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>
Tao Bao0b1118d2017-01-14 07:46:10 -080025#include <sys/ioctl.h>
Tao Bao835bf092019-03-11 14:13:21 -070026#include <sys/types.h>
Elliott Hughes07138192015-04-10 09:40:53 -070027#include <unistd.h>
28
Tao Bao9468fc02017-03-17 00:57:37 -070029#include <functional>
Tao Bao835bf092019-03-11 14:13:21 -070030#include <memory>
31
32#include <android-base/unique_fd.h>
Elliott Hughes07138192015-04-10 09:40:53 -070033
Tao Bao0ecbd762017-01-16 21:16:58 -080034#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070035
Tao Bao835bf092019-03-11 14:13:21 -070036constexpr size_t MAX_DEVICES = 16;
37constexpr size_t MAX_MISC_FDS = 16;
Elliott Hughes07138192015-04-10 09:40:53 -070038
Tao Bao835bf092019-03-11 14:13:21 -070039constexpr size_t BITS_PER_LONG = sizeof(unsigned long) * 8;
40constexpr size_t BITS_TO_LONGS(size_t bits) {
41 return ((bits + BITS_PER_LONG - 1) / BITS_PER_LONG);
42}
Elliott Hughes07138192015-04-10 09:40:53 -070043
Tao Bao835bf092019-03-11 14:13:21 -070044struct FdInfo {
45 android::base::unique_fd fd;
Tao Bao0b1118d2017-01-14 07:46:10 -080046 ev_callback cb;
Ethan Yonker8373cfe2017-09-08 06:50:54 -050047#ifdef TW_USE_MINUI_WITH_DATA
48 void* data;
49#endif
Elliott Hughes07138192015-04-10 09:40:53 -070050};
51
Tao Bao835bf092019-03-11 14:13:21 -070052static android::base::unique_fd g_epoll_fd;
53static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS];
54static int g_polled_events_count;
Elliott Hughes07138192015-04-10 09:40:53 -070055
Tao Bao835bf092019-03-11 14:13:21 -070056static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070057
Tao Bao835bf092019-03-11 14:13:21 -070058static size_t g_ev_count = 0;
59static size_t g_ev_dev_count = 0;
60static size_t g_ev_misc_count = 0;
Elliott Hughes07138192015-04-10 09:40:53 -070061
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070062static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Tao Bao835bf092019-03-11 14:13:21 -070063 return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
Elliott Hughes07138192015-04-10 09:40:53 -070064}
65
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060066#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Tao Baoaf9f8b42017-07-28 00:05:40 -070067int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
bigbiff20edca82020-07-03 09:55:28 -040068#else
69#ifdef TW_USE_MINUI_WITH_DATA
70int ev_init(ev_callback input_cb, void* data) {
71#else
72int ev_init(ev_callback input_cb) {
73#endif
74 bool allow_touch_inputs = false;
75#endif
Tao Bao835bf092019-03-11 14:13:21 -070076 g_epoll_fd.reset();
77
78 android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
79 if (epoll_fd == -1) {
Tao Bao0b1118d2017-01-14 07:46:10 -080080 return -1;
81 }
82
Tao Bao835bf092019-03-11 14:13:21 -070083 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/dev/input"), closedir);
84 if (!dir) {
85 return -1;
86 }
Tao Bao0b1118d2017-01-14 07:46:10 -080087
Tao Bao835bf092019-03-11 14:13:21 -070088 bool epoll_ctl_failed = false;
89 dirent* de;
90 while ((de = readdir(dir.get())) != nullptr) {
91 if (strncmp(de->d_name, "event", 5)) continue;
92 android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC));
93 if (fd == -1) continue;
Tao Baoaf9f8b42017-07-28 00:05:40 -070094
Tao Bao835bf092019-03-11 14:13:21 -070095 // Use unsigned long to match ioctl's parameter type.
96 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
Tao Bao0b1118d2017-01-14 07:46:10 -080097
Tao Bao835bf092019-03-11 14:13:21 -070098 // Read the evbits of the input device.
99 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
100 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700101 }
102
Tao Bao835bf092019-03-11 14:13:21 -0700103 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
104 // allowed if allow_touch_inputs is set.
105 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
106 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
Elliott Hughes07138192015-04-10 09:40:53 -0700107 continue;
108 }
Tao Bao835bf092019-03-11 14:13:21 -0700109 }
Elliott Hughes07138192015-04-10 09:40:53 -0700110
Tao Bao835bf092019-03-11 14:13:21 -0700111 epoll_event ev;
112 ev.events = EPOLLIN | EPOLLWAKEUP;
113 ev.data.ptr = &ev_fdinfo[g_ev_count];
114 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
115 epoll_ctl_failed = true;
116 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700117 }
118
Tao Bao835bf092019-03-11 14:13:21 -0700119 ev_fdinfo[g_ev_count].fd.reset(fd.release());
120 ev_fdinfo[g_ev_count].cb = input_cb;
121 g_ev_count++;
122 g_ev_dev_count++;
123 if (g_ev_dev_count == MAX_DEVICES) break;
Tao Bao0b1118d2017-01-14 07:46:10 -0800124 }
Elliott Hughes07138192015-04-10 09:40:53 -0700125
Tao Bao835bf092019-03-11 14:13:21 -0700126 if (epoll_ctl_failed && !g_ev_count) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800127 return -1;
128 }
Elliott Hughes07138192015-04-10 09:40:53 -0700129
Tao Bao835bf092019-03-11 14:13:21 -0700130 g_epoll_fd.reset(epoll_fd.release());
Tao Bao0b1118d2017-01-14 07:46:10 -0800131 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700132}
133
134int ev_get_epollfd(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700135 return g_epoll_fd.get();
Elliott Hughes07138192015-04-10 09:40:53 -0700136}
137
Tao Bao835bf092019-03-11 14:13:21 -0700138int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) {
139 if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800140 return -1;
141 }
Elliott Hughes07138192015-04-10 09:40:53 -0700142
Tao Bao0b1118d2017-01-14 07:46:10 -0800143 epoll_event ev;
144 ev.events = EPOLLIN | EPOLLWAKEUP;
Tao Bao835bf092019-03-11 14:13:21 -0700145 ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]);
Tao Bao0b1118d2017-01-14 07:46:10 -0800146 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
147 if (!ret) {
Tao Bao835bf092019-03-11 14:13:21 -0700148 ev_fdinfo[g_ev_count].fd.reset(fd.release());
149 ev_fdinfo[g_ev_count].cb = std::move(cb);
150 g_ev_count++;
151 g_ev_misc_count++;
Tao Bao0b1118d2017-01-14 07:46:10 -0800152 }
Elliott Hughes07138192015-04-10 09:40:53 -0700153
Tao Bao0b1118d2017-01-14 07:46:10 -0800154 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700155}
156
157void ev_exit(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700158 while (g_ev_count > 0) {
159 ev_fdinfo[--g_ev_count].fd.reset();
160 }
161 g_ev_misc_count = 0;
162 g_ev_dev_count = 0;
163 g_epoll_fd.reset();
Elliott Hughes07138192015-04-10 09:40:53 -0700164}
165
166int ev_wait(int timeout) {
Tao Bao835bf092019-03-11 14:13:21 -0700167 g_polled_events_count = epoll_wait(g_epoll_fd, g_polled_events, g_ev_count, timeout);
168 if (g_polled_events_count <= 0) {
169 return -1;
170 }
171 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700172}
173
174void ev_dispatch(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700175 for (int n = 0; n < g_polled_events_count; n++) {
176 FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr);
Tao Bao0b1118d2017-01-14 07:46:10 -0800177 const ev_callback& cb = fdi->cb;
178 if (cb) {
Tao Bao835bf092019-03-11 14:13:21 -0700179 cb(fdi->fd, g_polled_events[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700180 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800181 }
Elliott Hughes07138192015-04-10 09:40:53 -0700182}
183
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700184int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700185 if (epevents & EPOLLIN) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700186 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700187 if (r == sizeof(*ev)) {
188 return 0;
189 }
190 }
191 return -1;
192}
193
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500194#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700195int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500196#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800197int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500198#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800199 // Use unsigned long to match ioctl's parameter type.
200 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
201 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700202
Tao Bao835bf092019-03-11 14:13:21 -0700203 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800204 memset(ev_bits, 0, sizeof(ev_bits));
205 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700206
Tao Bao0b1118d2017-01-14 07:46:10 -0800207 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
208 continue;
209 }
210 if (!test_bit(EV_KEY, ev_bits)) {
211 continue;
212 }
213 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
214 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700215 }
216
Tao Bao0b1118d2017-01-14 07:46:10 -0800217 for (int code = 0; code <= KEY_MAX; code++) {
218 if (test_bit(code, key_bits)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500219#ifdef TW_USE_MINUI_WITH_DATA
220 set_key_cb(code, 1, data);
221#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800222 set_key_cb(code, 1);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500223#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800224 }
225 }
226 }
227
228 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700229}
230
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700231void ev_iterate_available_keys(const std::function<void(int)>& f) {
Tao Bao835bf092019-03-11 14:13:21 -0700232 // Use unsigned long to match ioctl's parameter type.
233 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
234 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700235
Tao Bao835bf092019-03-11 14:13:21 -0700236 for (size_t i = 0; i < g_ev_dev_count; ++i) {
237 memset(ev_bits, 0, sizeof(ev_bits));
238 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700239
Tao Bao835bf092019-03-11 14:13:21 -0700240 // Does this device even have keys?
241 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
242 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700243 }
Tao Bao835bf092019-03-11 14:13:21 -0700244 if (!test_bit(EV_KEY, ev_bits)) {
245 continue;
246 }
247
248 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits) == -1) {
249 continue;
250 }
251
252 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
253 if (test_bit(key_code, key_bits)) {
254 f(key_code);
255 }
256 }
257 }
Elliott Hughes07138192015-04-10 09:40:53 -0700258}
Tao Baoaf9f8b42017-07-28 00:05:40 -0700259
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600260#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Tao Baoaf9f8b42017-07-28 00:05:40 -0700261void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
Tao Bao835bf092019-03-11 14:13:21 -0700262 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Baoaf9f8b42017-07-28 00:05:40 -0700263 // Use unsigned long to match ioctl's parameter type.
264 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
265 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
266 continue;
267 }
268 if (!test_bit(EV_ABS, ev_bits)) {
269 continue;
270 }
271
272 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
273 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
274 continue;
275 }
276
277 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
278 if (test_bit(key_code, key_bits)) {
279 action(key_code);
280 }
281 }
282 }
283}
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600284#endif