blob: 7d0250e97a786450ae059c37917bcc3efbf9a398 [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>
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>
Tao Bao9468fc02017-03-17 00:57:37 -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;
Elliott Hughes07138192015-04-10 09:40:53 -070047};
48
Tao Bao835bf092019-03-11 14:13:21 -070049static android::base::unique_fd g_epoll_fd;
50static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS];
51static int g_polled_events_count;
Elliott Hughes07138192015-04-10 09:40:53 -070052
Tao Bao835bf092019-03-11 14:13:21 -070053static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070054
Tao Bao835bf092019-03-11 14:13:21 -070055static size_t g_ev_count = 0;
56static size_t g_ev_dev_count = 0;
57static size_t g_ev_misc_count = 0;
Elliott Hughes07138192015-04-10 09:40:53 -070058
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070059static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Tao Bao835bf092019-03-11 14:13:21 -070060 return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
Elliott Hughes07138192015-04-10 09:40:53 -070061}
62
Tao Bao5f8dd992017-07-28 00:05:40 -070063int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Tao Bao835bf092019-03-11 14:13:21 -070064 g_epoll_fd.reset();
65
66 android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
67 if (epoll_fd == -1) {
Tao Bao0b1118d2017-01-14 07:46:10 -080068 return -1;
69 }
70
Tao Bao835bf092019-03-11 14:13:21 -070071 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/dev/input"), closedir);
72 if (!dir) {
73 return -1;
74 }
Tao Bao0b1118d2017-01-14 07:46:10 -080075
Tao Bao835bf092019-03-11 14:13:21 -070076 bool epoll_ctl_failed = false;
77 dirent* de;
78 while ((de = readdir(dir.get())) != nullptr) {
79 if (strncmp(de->d_name, "event", 5)) continue;
80 android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC));
81 if (fd == -1) continue;
Tao Bao5f8dd992017-07-28 00:05:40 -070082
Tao Bao835bf092019-03-11 14:13:21 -070083 // Use unsigned long to match ioctl's parameter type.
84 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
Tao Bao0b1118d2017-01-14 07:46:10 -080085
Tao Bao835bf092019-03-11 14:13:21 -070086 // Read the evbits of the input device.
87 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
88 continue;
Elliott Hughes07138192015-04-10 09:40:53 -070089 }
90
Tao Bao835bf092019-03-11 14:13:21 -070091 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
92 // allowed if allow_touch_inputs is set.
93 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
94 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
95 continue;
96 }
97 }
98
99 epoll_event ev;
100 ev.events = EPOLLIN | EPOLLWAKEUP;
101 ev.data.ptr = &ev_fdinfo[g_ev_count];
102 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
103 epoll_ctl_failed = true;
104 continue;
105 }
106
107 ev_fdinfo[g_ev_count].fd.reset(fd.release());
108 ev_fdinfo[g_ev_count].cb = input_cb;
109 g_ev_count++;
110 g_ev_dev_count++;
111 if (g_ev_dev_count == MAX_DEVICES) break;
Tao Bao0b1118d2017-01-14 07:46:10 -0800112 }
Elliott Hughes07138192015-04-10 09:40:53 -0700113
Tao Bao835bf092019-03-11 14:13:21 -0700114 if (epoll_ctl_failed && !g_ev_count) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800115 return -1;
116 }
Elliott Hughes07138192015-04-10 09:40:53 -0700117
Tao Bao835bf092019-03-11 14:13:21 -0700118 g_epoll_fd.reset(epoll_fd.release());
Tao Bao0b1118d2017-01-14 07:46:10 -0800119 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700120}
121
122int ev_get_epollfd(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700123 return g_epoll_fd.get();
Elliott Hughes07138192015-04-10 09:40:53 -0700124}
125
Tao Bao835bf092019-03-11 14:13:21 -0700126int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) {
127 if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800128 return -1;
129 }
Elliott Hughes07138192015-04-10 09:40:53 -0700130
Tao Bao0b1118d2017-01-14 07:46:10 -0800131 epoll_event ev;
132 ev.events = EPOLLIN | EPOLLWAKEUP;
Tao Bao835bf092019-03-11 14:13:21 -0700133 ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]);
Tao Bao0b1118d2017-01-14 07:46:10 -0800134 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
135 if (!ret) {
Tao Bao835bf092019-03-11 14:13:21 -0700136 ev_fdinfo[g_ev_count].fd.reset(fd.release());
137 ev_fdinfo[g_ev_count].cb = std::move(cb);
138 g_ev_count++;
139 g_ev_misc_count++;
Tao Bao0b1118d2017-01-14 07:46:10 -0800140 }
Elliott Hughes07138192015-04-10 09:40:53 -0700141
Tao Bao0b1118d2017-01-14 07:46:10 -0800142 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700143}
144
145void ev_exit(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700146 while (g_ev_count > 0) {
147 ev_fdinfo[--g_ev_count].fd.reset();
148 }
149 g_ev_misc_count = 0;
150 g_ev_dev_count = 0;
151 g_epoll_fd.reset();
Elliott Hughes07138192015-04-10 09:40:53 -0700152}
153
154int ev_wait(int timeout) {
Tao Bao835bf092019-03-11 14:13:21 -0700155 g_polled_events_count = epoll_wait(g_epoll_fd, g_polled_events, g_ev_count, timeout);
156 if (g_polled_events_count <= 0) {
157 return -1;
158 }
159 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700160}
161
162void ev_dispatch(void) {
Tao Bao835bf092019-03-11 14:13:21 -0700163 for (int n = 0; n < g_polled_events_count; n++) {
164 FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr);
Tao Bao0b1118d2017-01-14 07:46:10 -0800165 const ev_callback& cb = fdi->cb;
166 if (cb) {
Tao Bao835bf092019-03-11 14:13:21 -0700167 cb(fdi->fd, g_polled_events[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700168 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800169 }
Elliott Hughes07138192015-04-10 09:40:53 -0700170}
171
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700172int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700173 if (epevents & EPOLLIN) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700174 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700175 if (r == sizeof(*ev)) {
176 return 0;
177 }
178 }
179 return -1;
180}
181
Tao Bao0b1118d2017-01-14 07:46:10 -0800182int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
183 // Use unsigned long to match ioctl's parameter type.
184 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
185 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700186
Tao Bao835bf092019-03-11 14:13:21 -0700187 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800188 memset(ev_bits, 0, sizeof(ev_bits));
189 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700190
Tao Bao0b1118d2017-01-14 07:46:10 -0800191 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
192 continue;
193 }
194 if (!test_bit(EV_KEY, ev_bits)) {
195 continue;
196 }
197 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
198 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700199 }
200
Tao Bao0b1118d2017-01-14 07:46:10 -0800201 for (int code = 0; code <= KEY_MAX; code++) {
202 if (test_bit(code, key_bits)) {
203 set_key_cb(code, 1);
204 }
205 }
206 }
207
208 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700209}
210
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700211void ev_iterate_available_keys(const std::function<void(int)>& f) {
Tao Bao835bf092019-03-11 14:13:21 -0700212 // Use unsigned long to match ioctl's parameter type.
213 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
214 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700215
Tao Bao835bf092019-03-11 14:13:21 -0700216 for (size_t i = 0; i < g_ev_dev_count; ++i) {
217 memset(ev_bits, 0, sizeof(ev_bits));
218 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700219
Tao Bao835bf092019-03-11 14:13:21 -0700220 // Does this device even have keys?
221 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
222 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700223 }
Tao Bao835bf092019-03-11 14:13:21 -0700224 if (!test_bit(EV_KEY, ev_bits)) {
225 continue;
226 }
227
228 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits) == -1) {
229 continue;
230 }
231
232 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
233 if (test_bit(key_code, key_bits)) {
234 f(key_code);
235 }
236 }
237 }
Elliott Hughes07138192015-04-10 09:40:53 -0700238}
Tao Bao5f8dd992017-07-28 00:05:40 -0700239
240void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
Tao Bao835bf092019-03-11 14:13:21 -0700241 for (size_t i = 0; i < g_ev_dev_count; ++i) {
Tao Bao5f8dd992017-07-28 00:05:40 -0700242 // Use unsigned long to match ioctl's parameter type.
243 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
244 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
245 continue;
246 }
247 if (!test_bit(EV_ABS, ev_bits)) {
248 continue;
249 }
250
251 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
252 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
253 continue;
254 }
255
256 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
257 if (test_bit(key_code, key_bits)) {
258 action(key_code);
259 }
260 }
261 }
262}