blob: 6dd60fe687ce48c90d241614167f1cae20f9f8ab [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>
Tao Bao0b1118d2017-01-14 07:46:10 -080018#include <linux/input.h>
Elliott Hughes07138192015-04-10 09:40:53 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/epoll.h>
Tao Bao0b1118d2017-01-14 07:46:10 -080023#include <sys/ioctl.h>
Elliott Hughes07138192015-04-10 09:40:53 -070024#include <unistd.h>
25
Tao Bao0ecbd762017-01-16 21:16:58 -080026#include "minui/minui.h"
Elliott Hughes07138192015-04-10 09:40:53 -070027
28#define MAX_DEVICES 16
29#define MAX_MISC_FDS 16
30
31#define BITS_PER_LONG (sizeof(unsigned long) * 8)
32#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
33
34struct fd_info {
Tao Bao0b1118d2017-01-14 07:46:10 -080035 int fd;
36 ev_callback cb;
Elliott Hughes07138192015-04-10 09:40:53 -070037};
38
39static int g_epoll_fd;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070040static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070041static int npolledevents;
42
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070043static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Elliott Hughes07138192015-04-10 09:40:53 -070044
45static unsigned ev_count = 0;
46static unsigned ev_dev_count = 0;
47static unsigned ev_misc_count = 0;
48
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070049static bool test_bit(size_t bit, unsigned long* array) { // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -070050 return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0;
51}
52
Tao Bao0b1118d2017-01-14 07:46:10 -080053int ev_init(ev_callback input_cb) {
54 bool epollctlfail = false;
Elliott Hughes07138192015-04-10 09:40:53 -070055
Tao Bao0b1118d2017-01-14 07:46:10 -080056 g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
57 if (g_epoll_fd == -1) {
58 return -1;
59 }
60
61 DIR* dir = opendir("/dev/input");
62 if (dir != NULL) {
63 dirent* de;
64 while ((de = readdir(dir))) {
65 // Use unsigned long to match ioctl's parameter type.
66 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
67
68 // fprintf(stderr,"/dev/input/%s\n", de->d_name);
69 if (strncmp(de->d_name, "event", 5)) continue;
70 int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
71 if (fd == -1) continue;
72
73 // Read the evbits of the input device.
74 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
75 close(fd);
76 continue;
77 }
78
79 // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed.
80 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
81 close(fd);
82 continue;
83 }
84
85 epoll_event ev;
86 ev.events = EPOLLIN | EPOLLWAKEUP;
87 ev.data.ptr = &ev_fdinfo[ev_count];
88 if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
89 close(fd);
90 epollctlfail = true;
91 continue;
92 }
93
94 ev_fdinfo[ev_count].fd = fd;
95 ev_fdinfo[ev_count].cb = std::move(input_cb);
96 ev_count++;
97 ev_dev_count++;
98 if (ev_dev_count == MAX_DEVICES) break;
Elliott Hughes07138192015-04-10 09:40:53 -070099 }
100
Tao Bao0b1118d2017-01-14 07:46:10 -0800101 closedir(dir);
102 }
Elliott Hughes07138192015-04-10 09:40:53 -0700103
Tao Bao0b1118d2017-01-14 07:46:10 -0800104 if (epollctlfail && !ev_count) {
105 close(g_epoll_fd);
106 g_epoll_fd = -1;
107 return -1;
108 }
Elliott Hughes07138192015-04-10 09:40:53 -0700109
Tao Bao0b1118d2017-01-14 07:46:10 -0800110 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700111}
112
113int ev_get_epollfd(void) {
114 return g_epoll_fd;
115}
116
Tao Bao0b1118d2017-01-14 07:46:10 -0800117int ev_add_fd(int fd, ev_callback cb) {
118 if (ev_misc_count == MAX_MISC_FDS || cb == NULL) {
119 return -1;
120 }
Elliott Hughes07138192015-04-10 09:40:53 -0700121
Tao Bao0b1118d2017-01-14 07:46:10 -0800122 epoll_event ev;
123 ev.events = EPOLLIN | EPOLLWAKEUP;
124 ev.data.ptr = static_cast<void*>(&ev_fdinfo[ev_count]);
125 int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
126 if (!ret) {
127 ev_fdinfo[ev_count].fd = fd;
128 ev_fdinfo[ev_count].cb = std::move(cb);
129 ev_count++;
130 ev_misc_count++;
131 }
Elliott Hughes07138192015-04-10 09:40:53 -0700132
Tao Bao0b1118d2017-01-14 07:46:10 -0800133 return ret;
Elliott Hughes07138192015-04-10 09:40:53 -0700134}
135
136void ev_exit(void) {
137 while (ev_count > 0) {
138 close(ev_fdinfo[--ev_count].fd);
139 }
140 ev_misc_count = 0;
141 ev_dev_count = 0;
142 close(g_epoll_fd);
143}
144
145int ev_wait(int timeout) {
146 npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout);
147 if (npolledevents <= 0) {
148 return -1;
149 }
150 return 0;
151}
152
153void ev_dispatch(void) {
Tao Bao0b1118d2017-01-14 07:46:10 -0800154 for (int n = 0; n < npolledevents; n++) {
155 fd_info* fdi = static_cast<fd_info*>(polledevents[n].data.ptr);
156 const ev_callback& cb = fdi->cb;
157 if (cb) {
158 cb(fdi->fd, polledevents[n].events);
Elliott Hughes07138192015-04-10 09:40:53 -0700159 }
Tao Bao0b1118d2017-01-14 07:46:10 -0800160 }
Elliott Hughes07138192015-04-10 09:40:53 -0700161}
162
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700163int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
Elliott Hughes07138192015-04-10 09:40:53 -0700164 if (epevents & EPOLLIN) {
Elliott Hughes7bad7c42015-04-28 17:24:24 -0700165 ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
Elliott Hughes07138192015-04-10 09:40:53 -0700166 if (r == sizeof(*ev)) {
167 return 0;
168 }
169 }
170 return -1;
171}
172
Tao Bao0b1118d2017-01-14 07:46:10 -0800173int ev_sync_key_state(const ev_set_key_callback& set_key_cb) {
174 // Use unsigned long to match ioctl's parameter type.
175 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT
176 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT
Elliott Hughes07138192015-04-10 09:40:53 -0700177
Tao Bao0b1118d2017-01-14 07:46:10 -0800178 for (size_t i = 0; i < ev_dev_count; ++i) {
179 memset(ev_bits, 0, sizeof(ev_bits));
180 memset(key_bits, 0, sizeof(key_bits));
Elliott Hughes07138192015-04-10 09:40:53 -0700181
Tao Bao0b1118d2017-01-14 07:46:10 -0800182 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
183 continue;
184 }
185 if (!test_bit(EV_KEY, ev_bits)) {
186 continue;
187 }
188 if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) {
189 continue;
Elliott Hughes07138192015-04-10 09:40:53 -0700190 }
191
Tao Bao0b1118d2017-01-14 07:46:10 -0800192 for (int code = 0; code <= KEY_MAX; code++) {
193 if (test_bit(code, key_bits)) {
194 set_key_cb(code, 1);
195 }
196 }
197 }
198
199 return 0;
Elliott Hughes07138192015-04-10 09:40:53 -0700200}
201
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700202void ev_iterate_available_keys(const std::function<void(int)>& f) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700203 // 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
207 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));
210
211 // Does this device even have keys?
212 if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) {
213 continue;
214 }
215 if (!test_bit(EV_KEY, ev_bits)) {
216 continue;
217 }
218
219 int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits);
220 if (rc == -1) {
221 continue;
222 }
223
224 for (int key_code = 0; key_code <= KEY_MAX; ++key_code) {
225 if (test_bit(key_code, key_bits)) {
Elliott Hughes642aaa72015-04-10 12:47:46 -0700226 f(key_code);
Elliott Hughes07138192015-04-10 09:40:53 -0700227 }
228 }
229 }
230}