blob: 30f8d50a21c4626ec2f1dd6ffceeb6048a81aab8 [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>
Elliott Hughes07138192015-04-10 09:40:53 -070026#include <unistd.h>
27
Tao Bao9468fc02017-03-17 00:57:37 -070028#include <functional>
29
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;
Elliott Hughes07138192015-04-10 09:40:53 -070041};
42
43static int g_epoll_fd;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070044static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070045static int npolledevents;
46
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070047static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070048
49static unsigned ev_count = 0;
50static unsigned ev_dev_count = 0;
51static unsigned ev_misc_count = 0;
52
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070053static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -070054 return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
55}
56
Tao Bao5f8dd992017-07-28 00:05:40 -070057int ev_init(ev_callback input_cb, bool allow_touch_inputs) {
Nick Kralevich39c53c82018-12-17 08:51:38 -080058 g_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
Tao Bao0b1118d2017-01-14 07:46:10 -080059 if (g_epoll_fd == -1) {
60 return -1;
61 }
62
Tao Bao5f8dd992017-07-28 00:05:40 -070063 bool epollctlfail = false;
Tao Bao0b1118d2017-01-14 07:46:10 -080064 DIR* dir = opendir("/dev/input");
Tao Bao5f8dd992017-07-28 00:05:40 -070065 if (dir != nullptr) {
Tao Bao0b1118d2017-01-14 07:46:10 -080066 dirent* de;
67 while ((de = readdir(dir))) {
Tao Bao0b1118d2017-01-14 07:46:10 -080068 if (strncmp(de->d_name, "event", 5)) continue;
Tao Baob5492432019-01-16 09:29:17 -080069 int fd = openat(dirfd(dir), de->d_name, O_RDONLY | O_CLOEXEC);
Tao Bao0b1118d2017-01-14 07:46:10 -080070 if (fd == -1) continue;
71
Tao Bao5f8dd992017-07-28 00:05:40 -070072 // Use unsigned long to match ioctl's parameter type.
73 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
74
Tao Bao0b1118d2017-01-14 07:46:10 -080075 // Read the evbits of the input device.
76 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
77 close(fd);
78 continue;
79 }
80
Tao Bao5f8dd992017-07-28 00:05:40 -070081 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also
82 // allowed if allow_touch_inputs is set.
Tao Bao0b1118d2017-01-14 07:46:10 -080083 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
Tao Bao5f8dd992017-07-28 00:05:40 -070084 if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) {
85 close(fd);
86 continue;
87 }
Tao Bao0b1118d2017-01-14 07:46:10 -080088 }
89
90 epoll_event ev;
91 ev.events = EPOLLIN | EPOLLWAKEUP;
92 ev.data.ptr = &ev_fdinfo[ev_count];
93 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
94 close(fd);
95 epollctlfail = true;
96 continue;
97 }
98
99 ev_fdinfo[ev_count].fd = fd;
100 ev_fdinfo[ev_count].cb = std::move(input_cb);
101 ev_count++;
102 ev_dev_count++;
103 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -0700104 }
105
Tao Bao0b1118d2017-01-14 07:46:10 -0800106 closedir(dir);
107 }
Elliott Hughes07138192015-04-10 09:40:53 -0700108
Tao Bao0b1118d2017-01-14 07:46:10 -0800109 if (epollctlfail && !ev_count) {
110 close(g_epoll_fd);
111 g_epoll_fd = -1;
112 return -1;
113 }
Elliott Hughes07138192015-04-10 09:40:53 -0700114
Tao Bao0b1118d2017-01-14 07:46:10 -0800115 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700116}
117
118int ev_get_epollfd(void) {
119 return g_epoll_fd;
120}
121
Tao Bao0b1118d2017-01-14 07:46:10 -0800122int ev_add_fd(int fd, ev_callback cb) {
123 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
124 return -1;
125 }
Elliott Hughes07138192015-04-10 09:40:53 -0700126
Tao Bao0b1118d2017-01-14 07:46:10 -0800127 epoll_event ev;
128 ev.events = EPOLLIN | EPOLLWAKEUP;
129 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
130 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
131 if (!ret) {
132 ev_fdinfo[ev_count].fd = fd;
133 ev_fdinfo[ev_count].cb = std::move(cb);
134 ev_count++;
135 ev_misc_count++;
136 }
Elliott Hughes07138192015-04-10 09:40:53 -0700137
Tao Bao0b1118d2017-01-14 07:46:10 -0800138 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700139}
140
141void ev_exit(void) {
142 while (ev_count > 0) {
143 close(ev_fdinfo[--ev_count].fd);
144 }
145 ev_misc_count = 0;
146 ev_dev_count = 0;
147 close(g_epoll_fd);
148}
149
150int ev_wait(int timeout) {
151 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
152 if (npolledevents <= 0) {
153 return -1;
154 }
155 return 0;
156}
157
158void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800159 for (int n = 0; n < npolledevents; n++) {
160 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
161 const ev_callback& cb = fdi->cb;
162 if (cb) {
163 cb(fdi->fd, polledevents[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700164 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800165 }
Elliott Hughes07138192015-04-10 09:40:53 -0700166}
167
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700168int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700169 if (epevents & EPOLLIN) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700170 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700171 if (r == sizeof(*ev)) {
172 return 0;
173 }
174 }
175 return -1;
176}
177
Tao Bao0b1118d2017-01-14 07:46:10 -0800178int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
179 // Use unsigned long to match ioctl's parameter type.
180 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
181 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700182
Tao Bao0b1118d2017-01-14 07:46:10 -0800183 for (size_t i = 0; i < ev_dev_count; ++i) {
184 memset(ev_bits, 0, sizeof(ev_bits));
185 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700186
Tao Bao0b1118d2017-01-14 07:46:10 -0800187 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
188 continue;
189 }
190 if (!test_bit(EV_KEY, ev_bits)) {
191 continue;
192 }
193 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
194 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700195 }
196
Tao Bao0b1118d2017-01-14 07:46:10 -0800197 for (int code = 0; code <= KEY_MAX; code++) {
198 if (test_bit(code, key_bits)) {
199 set_key_cb(code, 1);
200 }
201 }
202 }
203
204 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700205}
206
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700207void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700208 // Use unsigned long to match ioctl's parameter type.
209 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
210 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700211
212 for (size_t i = 0; i < ev_dev_count; ++i) {
213 memset(ev_bits, 0, sizeof(ev_bits));
214 memset(key_bits, 0, sizeof(key_bits));
215
216 // Does this device even have keys?
217 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
218 continue;
219 }
220 if (!test_bit(EV_KEY, ev_bits)) {
221 continue;
222 }
223
224 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
225 if (rc == -1) {
226 continue;
227 }
228
229 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
230 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700231 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700232 }
233 }
234 }
235}
Tao Bao5f8dd992017-07-28 00:05:40 -0700236
237void ev_iterate_touch_inputs(const std::function<void(int)>& action) {
238 for (size_t i = 0; i < ev_dev_count; ++i) {
239 // Use unsigned long to match ioctl's parameter type.
240 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT
241 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
242 continue;
243 }
244 if (!test_bit(EV_ABS, ev_bits)) {
245 continue;
246 }
247
248 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT
249 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) {
250 continue;
251 }
252
253 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
254 if (test_bit(key_code, key_bits)) {
255 action(key_code);
256 }
257 }
258 }
259}