blob: a6b9671ed2a3349c746562ae31cf31a35839b72f [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 Hughes7bad7c42015-04-28 17:24:24 -070018#include <errno.h>
Elliott Hughes07138192015-04-10 09:40:53 -070019#include <fcntl.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/epoll.h>
24#include <unistd.h>
25
26#include <linux/input.h>
27
28#include "minui.h"
29
30#define MAX_DEVICES 16
31#define MAX_MISC_FDS 16
32
33#define BITS_PER_LONG (sizeof(unsigned long) * 8)
34#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
35
36struct fd_info {
37 int fd;
38 ev_callback cb;
39 void* data;
40};
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
56int ev_init(ev_callback input_cb, void* data) {
57 bool epollctlfail = false;
58
59 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) {
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070066 dirent* de;
Elliott Hughes07138192015-04-10 09:40:53 -070067 while ((de = readdir(dir))) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070068 // Use unsigned long to match ioctl's parameter type.
69 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -070070
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
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070088 epoll_event ev;
Elliott Hughes07138192015-04-10 09:40:53 -070089 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 = input_cb;
99 ev_fdinfo[ev_count].data = data;
100 ev_count++;
101 ev_dev_count++;
102 if (ev_dev_count == MAX_DEVICES) break;
103 }
104
105 closedir(dir);
106 }
107
108 if (epollctlfail && !ev_count) {
109 close(g_epoll_fd);
110 g_epoll_fd = -1;
111 return -1;
112 }
113
114 return 0;
115}
116
117int ev_get_epollfd(void) {
118 return g_epoll_fd;
119}
120
121int ev_add_fd(int fd, ev_callback cb, void* data) {
122 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
123 return -1;
124 }
125
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700126 epoll_event ev;
Elliott Hughes07138192015-04-10 09:40:53 -0700127 ev.events = EPOLLIN | EPOLLWAKEUP;
128 ev.data.ptr = (void *)&ev_fdinfo[ev_count];
129 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
130 if (!ret) {
131 ev_fdinfo[ev_count].fd = fd;
132 ev_fdinfo[ev_count].cb = cb;
133 ev_fdinfo[ev_count].data = data;
134 ev_count++;
135 ev_misc_count++;
136 }
137
138 return ret;
139}
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) {
159 for (int n = 0; n < npolledevents; n++) {
160 fd_info* fdi = reinterpret_cast<fd_info*>(polledevents[n].data.ptr);
161 ev_callback cb = fdi->cb;
162 if (cb) {
163 cb(fdi->fd, polledevents[n].events, fdi->data);
164 }
165 }
166}
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
178int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700179 // 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
183 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));
186
187 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;
195 }
196
197 for (int code = 0; code <= KEY_MAX; code++) {
198 if (test_bit(code, key_bits)) {
199 set_key_cb(code, 1, data);
200 }
201 }
202 }
203
204 return 0;
205}
206
Elliott Hughes642aaa72015-04-10 12:47:46 -0700207void ev_iterate_available_keys(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}