blob: fa44033d21a0d0e4afc827d773c4c62e8f8f56fd [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 Bao0ecbd762017-01-16 21:16:58 -080027#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070028
29#define MAX_DEVICES 16
30#define MAX_MISC_FDS 16
31
32#define BITS_PER_LONG (sizeof(unsigned long) * 8)
33#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
34
35struct fd_info {
Tao Bao0b1118d2017-01-14 07:46:10 -080036 int fd;
37 ev_callback cb;
Elliott Hughes07138192015-04-10 09:40:53 -070038};
39
40static int g_epoll_fd;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070041static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070042static int npolledevents;
43
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070044static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070045
46static unsigned ev_count = 0;
47static unsigned ev_dev_count = 0;
48static unsigned ev_misc_count = 0;
49
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070050static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -070051 return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
52}
53
Tao Bao0b1118d2017-01-14 07:46:10 -080054int ev_init(ev_callback input_cb) {
55 bool epollctlfail = false;
Elliott Hughes07138192015-04-10 09:40:53 -070056
Tao Bao0b1118d2017-01-14 07:46:10 -080057 g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
58 if (g_epoll_fd == -1) {
59 return -1;
60 }
61
62 DIR* dir = opendir("/dev/input");
63 if (dir != NULL) {
64 dirent* de;
65 while ((de = readdir(dir))) {
66 // Use unsigned long to match ioctl's parameter type.
67 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
68
69 // fprintf(stderr,"/dev/input/%s\n", de->d_name);
70 if (strncmp(de->d_name, "event", 5)) continue;
71 int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
72 if (fd == -1) continue;
73
74 // Read the evbits of the input device.
75 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
76 close(fd);
77 continue;
78 }
79
80 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed.
81 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
82 close(fd);
83 continue;
84 }
85
86 epoll_event ev;
87 ev.events = EPOLLIN | EPOLLWAKEUP;
88 ev.data.ptr = &ev_fdinfo[ev_count];
89 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
90 close(fd);
91 epollctlfail = true;
92 continue;
93 }
94
95 ev_fdinfo[ev_count].fd = fd;
96 ev_fdinfo[ev_count].cb = std::move(input_cb);
97 ev_count++;
98 ev_dev_count++;
99 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -0700100 }
101
Tao Bao0b1118d2017-01-14 07:46:10 -0800102 closedir(dir);
103 }
Elliott Hughes07138192015-04-10 09:40:53 -0700104
Tao Bao0b1118d2017-01-14 07:46:10 -0800105 if (epollctlfail && !ev_count) {
106 close(g_epoll_fd);
107 g_epoll_fd = -1;
108 return -1;
109 }
Elliott Hughes07138192015-04-10 09:40:53 -0700110
Tao Bao0b1118d2017-01-14 07:46:10 -0800111 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700112}
113
114int ev_get_epollfd(void) {
115 return g_epoll_fd;
116}
117
Tao Bao0b1118d2017-01-14 07:46:10 -0800118int ev_add_fd(int fd, ev_callback cb) {
119 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
120 return -1;
121 }
Elliott Hughes07138192015-04-10 09:40:53 -0700122
Tao Bao0b1118d2017-01-14 07:46:10 -0800123 epoll_event ev;
124 ev.events = EPOLLIN | EPOLLWAKEUP;
125 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
126 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
127 if (!ret) {
128 ev_fdinfo[ev_count].fd = fd;
129 ev_fdinfo[ev_count].cb = std::move(cb);
130 ev_count++;
131 ev_misc_count++;
132 }
Elliott Hughes07138192015-04-10 09:40:53 -0700133
Tao Bao0b1118d2017-01-14 07:46:10 -0800134 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700135}
136
137void ev_exit(void) {
138 while (ev_count > 0) {
139 close(ev_fdinfo[--ev_count].fd);
140 }
141 ev_misc_count = 0;
142 ev_dev_count = 0;
143 close(g_epoll_fd);
144}
145
146int ev_wait(int timeout) {
147 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
148 if (npolledevents <= 0) {
149 return -1;
150 }
151 return 0;
152}
153
154void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800155 for (int n = 0; n < npolledevents; n++) {
156 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
157 const ev_callback& cb = fdi->cb;
158 if (cb) {
159 cb(fdi->fd, polledevents[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700160 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800161 }
Elliott Hughes07138192015-04-10 09:40:53 -0700162}
163
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700164int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700165 if (epevents & EPOLLIN) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700166 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700167 if (r == sizeof(*ev)) {
168 return 0;
169 }
170 }
171 return -1;
172}
173
Tao Bao0b1118d2017-01-14 07:46:10 -0800174int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
175 // Use unsigned long to match ioctl's parameter type.
176 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
177 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700178
Tao Bao0b1118d2017-01-14 07:46:10 -0800179 for (size_t i = 0; i < ev_dev_count; ++i) {
180 memset(ev_bits, 0, sizeof(ev_bits));
181 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700182
Tao Bao0b1118d2017-01-14 07:46:10 -0800183 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
184 continue;
185 }
186 if (!test_bit(EV_KEY, ev_bits)) {
187 continue;
188 }
189 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
190 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700191 }
192
Tao Bao0b1118d2017-01-14 07:46:10 -0800193 for (int code = 0; code <= KEY_MAX; code++) {
194 if (test_bit(code, key_bits)) {
195 set_key_cb(code, 1);
196 }
197 }
198 }
199
200 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700201}
202
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700203void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700204 // Use unsigned long to match ioctl's parameter type.
205 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
206 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700207
208 for (size_t i = 0; i < ev_dev_count; ++i) {
209 memset(ev_bits, 0, sizeof(ev_bits));
210 memset(key_bits, 0, sizeof(key_bits));
211
212 // Does this device even have keys?
213 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
214 continue;
215 }
216 if (!test_bit(EV_KEY, ev_bits)) {
217 continue;
218 }
219
220 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
221 if (rc == -1) {
222 continue;
223 }
224
225 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
226 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700227 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700228 }
229 }
230 }
231}