blob: c7779961a05785e61d31567a18fe7dd9c022c59a [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>
27
Tao Bao9468fc02017-03-17 00:57:37 -070028#include <functional>
Elliott Hughes07138192015-04-10 09:40:53 -070029
Tao Bao0ecbd762017-01-16 21:16:58 -080030#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070031
32#define MAX_DEVICES 16
33#define MAX_MISC_FDS 16
34
35#define BITS_PER_LONG (sizeof(unsigned long) * 8)
36#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
37
38struct fd_info {
Tao Bao0b1118d2017-01-14 07:46:10 -080039 int fd;
40 ev_callback cb;
Ethan Yonker8373cfe2017-09-08 06:50:54 -050041#ifdef TW_USE_MINUI_WITH_DATA
42 void* data;
43#endif
Elliott Hughes07138192015-04-10 09:40:53 -070044};
45
46static int g_epoll_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
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060060#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Tao Baoaf9f8b42017-07-28 00:05:40 -070061int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060062#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -050063#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -070064int ev_init(ev_callback input_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050065#else
Tao Bao0b1118d2017-01-14 07:46:10 -080066int ev_init(ev_callback input_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050067#endif
Ethan Yonkerecbd3e82017-12-14 14:43:59 -060068 bool allow_touch_inputs = false;
69#endif
Elliott Hughes07138192015-04-10 09:40:53 -070070
Tao Bao0b1118d2017-01-14 07:46:10 -080071 g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
72 if (g_epoll_fd == -1) {
73 return -1;
74 }
75
Tao Baoaf9f8b42017-07-28 00:05:40 -070076 bool epollctlfail = false;
Tao Bao0b1118d2017-01-14 07:46:10 -080077 DIR* dir = opendir("/dev/input");
Tao Baoaf9f8b42017-07-28 00:05:40 -070078 if (dir != nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -080079 dirent* de;
80 while ((de = readdir(dir))) {
Tao Bao0b1118d2017-01-14 07:46:10 -080081 if (strncmp(de->d_name, "event", 5)) continue;
82 int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
83 if (fd == -1) continue;
84
Tao Baoaf9f8b42017-07-28 00:05:40 -070085 // Use unsigned long to match ioctl's parameter type.
86 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
87
Tao Bao0b1118d2017-01-14 07:46:10 -080088 // Read the evbits of the input device.
89 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
90 close(fd);
91 continue;
92 }
93
Tao Baoaf9f8b42017-07-28 00:05:40 -070094 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
95 // allowed if allow_touch_inputs is set.
Tao Bao0b1118d2017-01-14 07:46:10 -080096 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 -070097 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
98 close(fd);
99 continue;
100 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800101 }
102
103 epoll_event ev;
104 ev.events = EPOLLIN | EPOLLWAKEUP;
105 ev.data.ptr = &ev_fdinfo[ev_count];
106 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
107 close(fd);
108 epollctlfail = true;
109 continue;
110 }
111
112 ev_fdinfo[ev_count].fd = fd;
113 ev_fdinfo[ev_count].cb = std::move(input_cb);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500114#ifdef TW_USE_MINUI_WITH_DATA
115 ev_fdinfo[ev_count].data = data;
116#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800117 ev_count++;
118 ev_dev_count++;
119 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -0700120 }
121
Tao Bao0b1118d2017-01-14 07:46:10 -0800122 closedir(dir);
123 }
Elliott Hughes07138192015-04-10 09:40:53 -0700124
Tao Bao0b1118d2017-01-14 07:46:10 -0800125 if (epollctlfail && !ev_count) {
126 close(g_epoll_fd);
127 g_epoll_fd = -1;
128 return -1;
129 }
Elliott Hughes07138192015-04-10 09:40:53 -0700130
Tao Bao0b1118d2017-01-14 07:46:10 -0800131 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700132}
133
134int ev_get_epollfd(void) {
135 return g_epoll_fd;
136}
137
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500138#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700139int ev_add_fd(int fd, ev_callback cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500140#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800141int ev_add_fd(int fd, ev_callback cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500142#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800143 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
144 return -1;
145 }
Elliott Hughes07138192015-04-10 09:40:53 -0700146
Tao Bao0b1118d2017-01-14 07:46:10 -0800147 epoll_event ev;
148 ev.events = EPOLLIN | EPOLLWAKEUP;
149 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
150 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
151 if (!ret) {
152 ev_fdinfo[ev_count].fd = fd;
153 ev_fdinfo[ev_count].cb = std::move(cb);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500154#ifdef TW_USE_MINUI_WITH_DATA
155 ev_fdinfo[ev_count].data = data;
156#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800157 ev_count++;
158 ev_misc_count++;
159 }
Elliott Hughes07138192015-04-10 09:40:53 -0700160
Tao Bao0b1118d2017-01-14 07:46:10 -0800161 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700162}
163
164void ev_exit(void) {
165 while (ev_count > 0) {
166 close(ev_fdinfo[--ev_count].fd);
167 }
168 ev_misc_count = 0;
169 ev_dev_count = 0;
170 close(g_epoll_fd);
171}
172
173int ev_wait(int timeout) {
174 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
175 if (npolledevents <= 0) {
176 return -1;
177 }
178 return 0;
179}
180
181void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800182 for (int n = 0; n < npolledevents; n++) {
183 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
184 const ev_callback& cb = fdi->cb;
185 if (cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500186#ifdef TW_USE_MINUI_WITH_DATA
187 cb(fdi->fd, polledevents[n].events, fdi->data);
188#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800189 cb(fdi->fd, polledevents[n].events);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500190#endif
Elliott Hughes07138192015-04-10 09:40:53 -0700191 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800192 }
Elliott Hughes07138192015-04-10 09:40:53 -0700193}
194
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700195int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700196 if (epevents & EPOLLIN) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700197 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700198 if (r == sizeof(*ev)) {
199 return 0;
200 }
201 }
202 return -1;
203}
204
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500205#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700206int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500207#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800208int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500209#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800210 // Use unsigned long to match ioctl's parameter type.
211 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
212 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700213
Tao Bao0b1118d2017-01-14 07:46:10 -0800214 for (size_t i = 0; i < ev_dev_count; ++i) {
215 memset(ev_bits, 0, sizeof(ev_bits));
216 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700217
Tao Bao0b1118d2017-01-14 07:46:10 -0800218 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
219 continue;
220 }
221 if (!test_bit(EV_KEY, ev_bits)) {
222 continue;
223 }
224 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
225 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700226 }
227
Tao Bao0b1118d2017-01-14 07:46:10 -0800228 for (int code = 0; code <= KEY_MAX; code++) {
229 if (test_bit(code, key_bits)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500230#ifdef TW_USE_MINUI_WITH_DATA
231 set_key_cb(code, 1, data);
232#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800233 set_key_cb(code, 1);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500234#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800235 }
236 }
237 }
238
239 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700240}
241
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700242void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700243 // Use unsigned long to match ioctl's parameter type.
244 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
245 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700246
247 for (size_t i = 0; i < ev_dev_count; ++i) {
248 memset(ev_bits, 0, sizeof(ev_bits));
249 memset(key_bits, 0, sizeof(key_bits));
250
251 // Does this device even have keys?
252 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
253 continue;
254 }
255 if (!test_bit(EV_KEY, ev_bits)) {
256 continue;
257 }
258
259 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
260 if (rc == -1) {
261 continue;
262 }
263
264 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
265 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700266 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700267 }
268 }
269 }
270}
Tao Baoaf9f8b42017-07-28 00:05:40 -0700271
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600272#ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS
Tao Baoaf9f8b42017-07-28 00:05:40 -0700273void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
274 for (size_t i = 0; i < ev_dev_count; ++i) {
275 // Use unsigned long to match ioctl's parameter type.
276 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
277 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
278 continue;
279 }
280 if (!test_bit(EV_ABS, ev_bits)) {
281 continue;
282 }
283
284 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
285 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
286 continue;
287 }
288
289 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
290 if (test_bit(key_code, key_bits)) {
291 action(key_code);
292 }
293 }
294 }
295}
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600296#endif