blob: 470a17a691c0d27495ca0b1feb2c667f50bae1c8 [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>
18#include <fcntl.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080019#include <linux/input.h>
Elliott Hughes07138192015-04-10 09:40:53 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/epoll.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080024#include <sys/ioctl.h>
Elliott Hughes07138192015-04-10 09:40:53 -070025#include <unistd.h>
Ethan Yonker8373cfe2017-09-08 06:50:54 -050026#include <errno.h>
Elliott Hughes07138192015-04-10 09:40:53 -070027
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 Yonker8373cfe2017-09-08 06:50:54 -050060#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -070061int ev_init(ev_callback input_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050062#else
Tao Bao0b1118d2017-01-14 07:46:10 -080063int ev_init(ev_callback input_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050064#endif
Tao Bao0b1118d2017-01-14 07:46:10 -080065 bool epollctlfail = false;
Elliott Hughes07138192015-04-10 09:40:53 -070066
Tao Bao0b1118d2017-01-14 07:46:10 -080067 g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
68 if (g_epoll_fd == -1) {
69 return -1;
70 }
71
72 DIR* dir = opendir("/dev/input");
73 if (dir != NULL) {
74 dirent* de;
75 while ((de = readdir(dir))) {
76 // Use unsigned long to match ioctl's parameter type.
77 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
78
79 // fprintf(stderr,"/dev/input/%s\n", de->d_name);
80 if (strncmp(de->d_name, "event", 5)) continue;
81 int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
82 if (fd == -1) continue;
83
84 // Read the evbits of the input device.
85 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
86 close(fd);
87 continue;
88 }
89
90 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed.
91 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
92 close(fd);
93 continue;
94 }
95
96 epoll_event ev;
97 ev.events = EPOLLIN | EPOLLWAKEUP;
98 ev.data.ptr = &ev_fdinfo[ev_count];
99 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
100 close(fd);
101 epollctlfail = true;
102 continue;
103 }
104
105 ev_fdinfo[ev_count].fd = fd;
106 ev_fdinfo[ev_count].cb = std::move(input_cb);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500107#ifdef TW_USE_MINUI_WITH_DATA
108 ev_fdinfo[ev_count].data = data;
109#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800110 ev_count++;
111 ev_dev_count++;
112 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -0700113 }
114
Tao Bao0b1118d2017-01-14 07:46:10 -0800115 closedir(dir);
116 }
Elliott Hughes07138192015-04-10 09:40:53 -0700117
Tao Bao0b1118d2017-01-14 07:46:10 -0800118 if (epollctlfail && !ev_count) {
119 close(g_epoll_fd);
120 g_epoll_fd = -1;
121 return -1;
122 }
Elliott Hughes07138192015-04-10 09:40:53 -0700123
Tao Bao0b1118d2017-01-14 07:46:10 -0800124 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700125}
126
127int ev_get_epollfd(void) {
128 return g_epoll_fd;
129}
130
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500131#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700132int ev_add_fd(int fd, ev_callback cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500133#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800134int ev_add_fd(int fd, ev_callback cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500135#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800136 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
137 return -1;
138 }
Elliott Hughes07138192015-04-10 09:40:53 -0700139
Tao Bao0b1118d2017-01-14 07:46:10 -0800140 epoll_event ev;
141 ev.events = EPOLLIN | EPOLLWAKEUP;
142 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
143 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
144 if (!ret) {
145 ev_fdinfo[ev_count].fd = fd;
146 ev_fdinfo[ev_count].cb = std::move(cb);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500147#ifdef TW_USE_MINUI_WITH_DATA
148 ev_fdinfo[ev_count].data = data;
149#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800150 ev_count++;
151 ev_misc_count++;
152 }
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) {
158 while (ev_count > 0) {
159 close(ev_fdinfo[--ev_count].fd);
160 }
161 ev_misc_count = 0;
162 ev_dev_count = 0;
163 close(g_epoll_fd);
164}
165
166int ev_wait(int timeout) {
167 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
168 if (npolledevents <= 0) {
169 return -1;
170 }
171 return 0;
172}
173
174void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800175 for (int n = 0; n < npolledevents; n++) {
176 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
177 const ev_callback& cb = fdi->cb;
178 if (cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500179#ifdef TW_USE_MINUI_WITH_DATA
180 cb(fdi->fd, polledevents[n].events, fdi->data);
181#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800182 cb(fdi->fd, polledevents[n].events);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500183#endif
Elliott Hughes07138192015-04-10 09:40:53 -0700184 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800185 }
Elliott Hughes07138192015-04-10 09:40:53 -0700186}
187
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700188int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700189 if (epevents & EPOLLIN) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700190 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700191 if (r == sizeof(*ev)) {
192 return 0;
193 }
194 }
195 return -1;
196}
197
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500198#ifdef TW_USE_MINUI_WITH_DATA
Elliott Hughes07138192015-04-10 09:40:53 -0700199int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500200#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800201int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500202#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800203 // Use unsigned long to match ioctl's parameter type.
204 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
205 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700206
Tao Bao0b1118d2017-01-14 07:46:10 -0800207 for (size_t i = 0; i < ev_dev_count; ++i) {
208 memset(ev_bits, 0, sizeof(ev_bits));
209 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700210
Tao Bao0b1118d2017-01-14 07:46:10 -0800211 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
212 continue;
213 }
214 if (!test_bit(EV_KEY, ev_bits)) {
215 continue;
216 }
217 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
218 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700219 }
220
Tao Bao0b1118d2017-01-14 07:46:10 -0800221 for (int code = 0; code <= KEY_MAX; code++) {
222 if (test_bit(code, key_bits)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500223#ifdef TW_USE_MINUI_WITH_DATA
224 set_key_cb(code, 1, data);
225#else
Tao Bao0b1118d2017-01-14 07:46:10 -0800226 set_key_cb(code, 1);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500227#endif
Tao Bao0b1118d2017-01-14 07:46:10 -0800228 }
229 }
230 }
231
232 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700233}
234
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700235void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700236 // Use unsigned long to match ioctl's parameter type.
237 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
238 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700239
240 for (size_t i = 0; i < ev_dev_count; ++i) {
241 memset(ev_bits, 0, sizeof(ev_bits));
242 memset(key_bits, 0, sizeof(key_bits));
243
244 // Does this device even have keys?
245 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
246 continue;
247 }
248 if (!test_bit(EV_KEY, ev_bits)) {
249 continue;
250 }
251
252 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
253 if (rc == -1) {
254 continue;
255 }
256
257 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
258 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700259 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700260 }
261 }
262 }
263}