blob: 0e1fd44a0798b222671f482814477d52a491e2d3 [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>
Elliott Hughes25a29d42017-02-23 10:45:42 -080018#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>
26
Tao Bao9468fc02017-03-17 00:57:37 -070027#include <functional>
28
Tao Bao0ecbd762017-01-16 21:16:58 -080029#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070030
31#define MAX_DEVICES 16
32#define MAX_MISC_FDS 16
33
34#define BITS_PER_LONG (sizeof(unsigned long) * 8)
35#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
36
37struct fd_info {
Tao Bao0b1118d2017-01-14 07:46:10 -080038 int fd;
39 ev_callback cb;
Elliott Hughes07138192015-04-10 09:40:53 -070040};
41
42static int g_epoll_fd;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070043static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070044static int npolledevents;
45
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070046static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070047
48static unsigned ev_count = 0;
49static unsigned ev_dev_count = 0;
50static unsigned ev_misc_count = 0;
51
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070052static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -070053 return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
54}
55
Tao Bao0b1118d2017-01-14 07:46:10 -080056int ev_init(ev_callback input_cb) {
57 bool epollctlfail = false;
Elliott Hughes07138192015-04-10 09:40:53 -070058
Tao Bao0b1118d2017-01-14 07:46:10 -080059 g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
60 if (g_epoll_fd == -1) {
61 return -1;
62 }
63
64 DIR* dir = opendir("/dev/input");
65 if (dir != NULL) {
66 dirent* de;
67 while ((de = readdir(dir))) {
68 // Use unsigned long to match ioctl's parameter type.
69 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
70
71 // fprintf(stderr,"/dev/input/%s\n", de->d_name);
72 if (strncmp(de->d_name, "event", 5)) continue;
73 int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
74 if (fd == -1) continue;
75
76 // Read the evbits of the input device.
77 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
78 close(fd);
79 continue;
80 }
81
82 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed.
83 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
84 close(fd);
85 continue;
86 }
87
88 epoll_event ev;
89 ev.events = EPOLLIN | EPOLLWAKEUP;
90 ev.data.ptr = &ev_fdinfo[ev_count];
91 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
92 close(fd);
93 epollctlfail = true;
94 continue;
95 }
96
97 ev_fdinfo[ev_count].fd = fd;
98 ev_fdinfo[ev_count].cb = std::move(input_cb);
99 ev_count++;
100 ev_dev_count++;
101 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -0700102 }
103
Tao Bao0b1118d2017-01-14 07:46:10 -0800104 closedir(dir);
105 }
Elliott Hughes07138192015-04-10 09:40:53 -0700106
Tao Bao0b1118d2017-01-14 07:46:10 -0800107 if (epollctlfail && !ev_count) {
108 close(g_epoll_fd);
109 g_epoll_fd = -1;
110 return -1;
111 }
Elliott Hughes07138192015-04-10 09:40:53 -0700112
Tao Bao0b1118d2017-01-14 07:46:10 -0800113 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700114}
115
116int ev_get_epollfd(void) {
117 return g_epoll_fd;
118}
119
Tao Bao0b1118d2017-01-14 07:46:10 -0800120int ev_add_fd(int fd, ev_callback cb) {
121 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
122 return -1;
123 }
Elliott Hughes07138192015-04-10 09:40:53 -0700124
Tao Bao0b1118d2017-01-14 07:46:10 -0800125 epoll_event ev;
126 ev.events = EPOLLIN | EPOLLWAKEUP;
127 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
128 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
129 if (!ret) {
130 ev_fdinfo[ev_count].fd = fd;
131 ev_fdinfo[ev_count].cb = std::move(cb);
132 ev_count++;
133 ev_misc_count++;
134 }
Elliott Hughes07138192015-04-10 09:40:53 -0700135
Tao Bao0b1118d2017-01-14 07:46:10 -0800136 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700137}
138
139void ev_exit(void) {
140 while (ev_count > 0) {
141 close(ev_fdinfo[--ev_count].fd);
142 }
143 ev_misc_count = 0;
144 ev_dev_count = 0;
145 close(g_epoll_fd);
146}
147
148int ev_wait(int timeout) {
149 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
150 if (npolledevents <= 0) {
151 return -1;
152 }
153 return 0;
154}
155
156void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800157 for (int n = 0; n < npolledevents; n++) {
158 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
159 const ev_callback& cb = fdi->cb;
160 if (cb) {
161 cb(fdi->fd, polledevents[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700162 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800163 }
Elliott Hughes07138192015-04-10 09:40:53 -0700164}
165
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700166int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700167 if (epevents & EPOLLIN) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700168 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700169 if (r == sizeof(*ev)) {
170 return 0;
171 }
172 }
173 return -1;
174}
175
Tao Bao0b1118d2017-01-14 07:46:10 -0800176int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
177 // Use unsigned long to match ioctl's parameter type.
178 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
179 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700180
Tao Bao0b1118d2017-01-14 07:46:10 -0800181 for (size_t i = 0; i < ev_dev_count; ++i) {
182 memset(ev_bits, 0, sizeof(ev_bits));
183 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700184
Tao Bao0b1118d2017-01-14 07:46:10 -0800185 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
186 continue;
187 }
188 if (!test_bit(EV_KEY, ev_bits)) {
189 continue;
190 }
191 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
192 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700193 }
194
Tao Bao0b1118d2017-01-14 07:46:10 -0800195 for (int code = 0; code <= KEY_MAX; code++) {
196 if (test_bit(code, key_bits)) {
197 set_key_cb(code, 1);
198 }
199 }
200 }
201
202 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700203}
204
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700205void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700206 // Use unsigned long to match ioctl's parameter type.
207 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
208 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700209
210 for (size_t i = 0; i < ev_dev_count; ++i) {
211 memset(ev_bits, 0, sizeof(ev_bits));
212 memset(key_bits, 0, sizeof(key_bits));
213
214 // Does this device even have keys?
215 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
216 continue;
217 }
218 if (!test_bit(EV_KEY, ev_bits)) {
219 continue;
220 }
221
222 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
223 if (rc == -1) {
224 continue;
225 }
226
227 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
228 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700229 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700230 }
231 }
232 }
233}