blob: f67684b8b4a9057bd4f5f059808eff54c82d9193 [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>
Elliott Hughes07138192015-04-10 09:40:53 -070026#include <unistd.h>
Ethan Yonker8373cfe2017-09-08 06:50:54 -050027#include <errno.h>
Elliott Hughes07138192015-04-10 09:40:53 -070028
Tao Bao9468fc02017-03-17 00:57:37 -070029#include <functional>
Elliott Hughes07138192015-04-10 09:40:53 -070030
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;
Ethan Yonker8373cfe2017-09-08 06:50:54 -050042#ifdef TW_USE_MINUI_WITH_DATA
43 void* data;
44#endif
Elliott Hughes07138192015-04-10 09:40:53 -070045};
46
47static int g_epoll_fd;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070048static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070049static int npolledevents;
50
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070051static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070052
53static unsigned ev_count = 0;
54static unsigned ev_dev_count = 0;
55static unsigned ev_misc_count = 0;
56
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070057static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -070058 return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
59}
60
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060061#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Tao Baoaf9f8b42017-07-28 00:05:40 -070062int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060063#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -050064#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -070065int ev_init(ev_callback input_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050066#else
Tao Bao0b1118d2017-01-14 07:46:10 -080067int ev_init(ev_callback input_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050068#endif
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060069 bool allow_touch_inputs = false;
70#endif
Elliott Hughes07138192015-04-10 09:40:53 -070071
Tao Bao0b1118d2017-01-14 07:46:10 -080072 g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
73 if (g_epoll_fd == -1) {
74 return -1;
75 }
76
Tao Baoaf9f8b42017-07-28 00:05:40 -070077 bool epollctlfail = false;
Tao Bao0b1118d2017-01-14 07:46:10 -080078 DIR* dir = opendir("/dev/input");
Tao Baoaf9f8b42017-07-28 00:05:40 -070079 if (dir != nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -080080 dirent* de;
81 while ((de = readdir(dir))) {
Tao Bao0b1118d2017-01-14 07:46:10 -080082 if (strncmp(de->d_name, "event", 5)) continue;
83 int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
84 if (fd == -1) continue;
85
Tao Baoaf9f8b42017-07-28 00:05:40 -070086 // Use unsigned long to match ioctl's parameter type.
87 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
88
Tao Bao0b1118d2017-01-14 07:46:10 -080089 // Read the evbits of the input device.
90 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
91 close(fd);
92 continue;
93 }
94
Tao Baoaf9f8b42017-07-28 00:05:40 -070095 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
96 // allowed if allow_touch_inputs is set.
Tao Bao0b1118d2017-01-14 07:46:10 -080097 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
Tao Baoaf9f8b42017-07-28 00:05:40 -070098 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
99 close(fd);
100 continue;
101 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800102 }
103
104 epoll_event ev;
105 ev.events = EPOLLIN | EPOLLWAKEUP;
106 ev.data.ptr = &ev_fdinfo[ev_count];
107 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
108 close(fd);
109 epollctlfail = true;
110 continue;
111 }
112
113 ev_fdinfo[ev_count].fd = fd;
114 ev_fdinfo[ev_count].cb = std::move(input_cb);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500115#ifdef TW_USE_MINUI_WITH_DATA
116 ev_fdinfo[ev_count].data = data;
117#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800118 ev_count++;
119 ev_dev_count++;
120 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -0700121 }
122
Tao Bao0b1118d2017-01-14 07:46:10 -0800123 closedir(dir);
124 }
Elliott Hughes07138192015-04-10 09:40:53 -0700125
Tao Bao0b1118d2017-01-14 07:46:10 -0800126 if (epollctlfail && !ev_count) {
127 close(g_epoll_fd);
128 g_epoll_fd = -1;
129 return -1;
130 }
Elliott Hughes07138192015-04-10 09:40:53 -0700131
Tao Bao0b1118d2017-01-14 07:46:10 -0800132 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700133}
134
135int ev_get_epollfd(void) {
136 return g_epoll_fd;
137}
138
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500139#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700140int ev_add_fd(int fd, ev_callback cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500141#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800142int ev_add_fd(int fd, ev_callback cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500143#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800144 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
145 return -1;
146 }
Elliott Hughes07138192015-04-10 09:40:53 -0700147
Tao Bao0b1118d2017-01-14 07:46:10 -0800148 epoll_event ev;
149 ev.events = EPOLLIN | EPOLLWAKEUP;
150 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
151 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
152 if (!ret) {
153 ev_fdinfo[ev_count].fd = fd;
154 ev_fdinfo[ev_count].cb = std::move(cb);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500155#ifdef TW_USE_MINUI_WITH_DATA
156 ev_fdinfo[ev_count].data = data;
157#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800158 ev_count++;
159 ev_misc_count++;
160 }
Elliott Hughes07138192015-04-10 09:40:53 -0700161
Tao Bao0b1118d2017-01-14 07:46:10 -0800162 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700163}
164
165void ev_exit(void) {
166 while (ev_count > 0) {
167 close(ev_fdinfo[--ev_count].fd);
168 }
169 ev_misc_count = 0;
170 ev_dev_count = 0;
171 close(g_epoll_fd);
172}
173
174int ev_wait(int timeout) {
175 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
176 if (npolledevents <= 0) {
177 return -1;
178 }
179 return 0;
180}
181
182void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800183 for (int n = 0; n < npolledevents; n++) {
184 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
185 const ev_callback& cb = fdi->cb;
186 if (cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500187#ifdef TW_USE_MINUI_WITH_DATA
188 cb(fdi->fd, polledevents[n].events, fdi->data);
189#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800190 cb(fdi->fd, polledevents[n].events);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500191#endif
Elliott Hughes07138192015-04-10 09:40:53 -0700192 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800193 }
Elliott Hughes07138192015-04-10 09:40:53 -0700194}
195
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700196int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700197 if (epevents & EPOLLIN) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700198 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700199 if (r == sizeof(*ev)) {
200 return 0;
201 }
202 }
203 return -1;
204}
205
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500206#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700207int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500208#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800209int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500210#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800211 // Use unsigned long to match ioctl's parameter type.
212 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
213 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700214
Tao Bao0b1118d2017-01-14 07:46:10 -0800215 for (size_t i = 0; i < ev_dev_count; ++i) {
216 memset(ev_bits, 0, sizeof(ev_bits));
217 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700218
Tao Bao0b1118d2017-01-14 07:46:10 -0800219 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
220 continue;
221 }
222 if (!test_bit(EV_KEY, ev_bits)) {
223 continue;
224 }
225 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
226 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700227 }
228
Tao Bao0b1118d2017-01-14 07:46:10 -0800229 for (int code = 0; code <= KEY_MAX; code++) {
230 if (test_bit(code, key_bits)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500231#ifdef TW_USE_MINUI_WITH_DATA
232 set_key_cb(code, 1, data);
233#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800234 set_key_cb(code, 1);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500235#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800236 }
237 }
238 }
239
240 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700241}
242
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700243void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700244 // Use unsigned long to match ioctl's parameter type.
245 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
246 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700247
248 for (size_t i = 0; i < ev_dev_count; ++i) {
249 memset(ev_bits, 0, sizeof(ev_bits));
250 memset(key_bits, 0, sizeof(key_bits));
251
252 // Does this device even have keys?
253 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
254 continue;
255 }
256 if (!test_bit(EV_KEY, ev_bits)) {
257 continue;
258 }
259
260 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
261 if (rc == -1) {
262 continue;
263 }
264
265 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
266 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700267 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700268 }
269 }
270 }
271}
Tao Baoaf9f8b42017-07-28 00:05:40 -0700272
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600273#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Tao Baoaf9f8b42017-07-28 00:05:40 -0700274void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
275 for (size_t i = 0; i < ev_dev_count; ++i) {
276 // Use unsigned long to match ioctl's parameter type.
277 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
278 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
279 continue;
280 }
281 if (!test_bit(EV_ABS, ev_bits)) {
282 continue;
283 }
284
285 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
286 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
287 continue;
288 }
289
290 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
291 if (test_bit(key_code, key_bits)) {
292 action(key_code);
293 }
294 }
295 }
296}
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600297#endif