blob: fe364bf01f8c61f2ff1a16b3dc556c0d305eb81c [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 <stdio.h>
18#include <stdlib.h>
19#include <fcntl.h>
20#include <dirent.h>
Todd Poynor1fc89d42013-09-10 16:52:54 -070021#include <sys/epoll.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022
23#include <linux/input.h>
24
25#include "minui.h"
26
27#define MAX_DEVICES 16
Dima Zavin36583672011-09-02 11:51:31 -070028#define MAX_MISC_FDS 16
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029
Dima Zavin441031d2011-10-12 15:53:29 -070030#define BITS_PER_LONG (sizeof(unsigned long) * 8)
31#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
32
33#define test_bit(bit, array) \
34 ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG)))
Dima Zavin88e08992011-09-02 14:24:43 -070035
Dima Zavinbc290632011-08-30 11:59:45 -070036struct fd_info {
Todd Poynor1fc89d42013-09-10 16:52:54 -070037 int fd;
Dima Zavinbc290632011-08-30 11:59:45 -070038 ev_callback cb;
39 void *data;
40};
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
Todd Poynor1fc89d42013-09-10 16:52:54 -070042static int epollfd;
43static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
44static int npolledevents;
45
Dima Zavin36583672011-09-02 11:51:31 -070046static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Dima Zavinbc290632011-08-30 11:59:45 -070047
48static unsigned ev_count = 0;
49static unsigned ev_dev_count = 0;
Dima Zavin36583672011-09-02 11:51:31 -070050static unsigned ev_misc_count = 0;
Dima Zavinbc290632011-08-30 11:59:45 -070051
52int ev_init(ev_callback input_cb, void *data)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053{
54 DIR *dir;
55 struct dirent *de;
56 int fd;
Todd Poynor1fc89d42013-09-10 16:52:54 -070057 struct epoll_event ev;
58 bool epollctlfail = false;
59
60 epollfd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
61 if (epollfd == -1)
62 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063
64 dir = opendir("/dev/input");
65 if(dir != 0) {
66 while((de = readdir(dir))) {
Dima Zavin441031d2011-10-12 15:53:29 -070067 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
Dima Zavin88e08992011-09-02 14:24:43 -070068
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069// fprintf(stderr,"/dev/input/%s\n", de->d_name);
70 if(strncmp(de->d_name,"event",5)) continue;
71 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
72 if(fd < 0) continue;
73
Dima Zavin88e08992011-09-02 14:24:43 -070074 /* read the evbits of the input device */
75 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) {
76 close(fd);
77 continue;
78 }
79
80 /* TODO: add ability to specify event masks. For now, just assume
81 * that only EV_KEY and EV_REL event types are ever needed. */
82 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) {
83 close(fd);
84 continue;
85 }
86
Todd Poynor1fc89d42013-09-10 16:52:54 -070087 ev.events = EPOLLIN | EPOLLWAKEUP;
88 ev.data.ptr = (void *)&ev_fdinfo[ev_count];
89 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
90 close(fd);
91 epollctlfail = true;
92 continue;
93 }
94
95 ev_fdinfo[ev_count].fd = fd;
Dima Zavinbc290632011-08-30 11:59:45 -070096 ev_fdinfo[ev_count].cb = input_cb;
97 ev_fdinfo[ev_count].data = data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098 ev_count++;
Dima Zavin36583672011-09-02 11:51:31 -070099 ev_dev_count++;
100 if(ev_dev_count == MAX_DEVICES) break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 }
102 }
103
Todd Poynor1fc89d42013-09-10 16:52:54 -0700104 if (epollctlfail && !ev_count) {
105 close(epollfd);
106 epollfd = -1;
107 return -1;
108 }
109
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800110 return 0;
111}
112
Dima Zavin36583672011-09-02 11:51:31 -0700113int ev_add_fd(int fd, ev_callback cb, void *data)
114{
Todd Poynor1fc89d42013-09-10 16:52:54 -0700115 struct epoll_event ev;
116 int ret;
117
Dima Zavin36583672011-09-02 11:51:31 -0700118 if (ev_misc_count == MAX_MISC_FDS || cb == NULL)
119 return -1;
120
Todd Poynor1fc89d42013-09-10 16:52:54 -0700121 ev.events = EPOLLIN | EPOLLWAKEUP;
122 ev.data.ptr = (void *)&ev_fdinfo[ev_count];
123 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
124 if (!ret) {
125 ev_fdinfo[ev_count].fd = fd;
126 ev_fdinfo[ev_count].cb = cb;
127 ev_fdinfo[ev_count].data = data;
128 ev_count++;
129 ev_misc_count++;
130 }
131
132 return ret;
Dima Zavin36583672011-09-02 11:51:31 -0700133}
134
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135void ev_exit(void)
136{
137 while (ev_count > 0) {
Todd Poynor1fc89d42013-09-10 16:52:54 -0700138 close(ev_fdinfo[--ev_count].fd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 }
Dima Zavin36583672011-09-02 11:51:31 -0700140 ev_misc_count = 0;
141 ev_dev_count = 0;
Todd Poynor1fc89d42013-09-10 16:52:54 -0700142 close(epollfd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143}
144
Dima Zavinbc290632011-08-30 11:59:45 -0700145int ev_wait(int timeout)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146{
Todd Poynor1fc89d42013-09-10 16:52:54 -0700147 npolledevents = epoll_wait(epollfd, polledevents, ev_count, timeout);
148 if (npolledevents <= 0)
Dima Zavinbc290632011-08-30 11:59:45 -0700149 return -1;
150 return 0;
151}
152
153void ev_dispatch(void)
154{
Todd Poynor1fc89d42013-09-10 16:52:54 -0700155 int n;
Dima Zavinbc290632011-08-30 11:59:45 -0700156 int ret;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157
Todd Poynor1fc89d42013-09-10 16:52:54 -0700158 for (n = 0; n < npolledevents; n++) {
159 struct fd_info *fdi = polledevents[n].data.ptr;
160 ev_callback cb = fdi->cb;
161 if (cb)
162 cb(fdi->fd, polledevents[n].events, fdi->data);
Dima Zavinbc290632011-08-30 11:59:45 -0700163 }
164}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165
Todd Poynor1fc89d42013-09-10 16:52:54 -0700166int ev_get_input(int fd, unsigned int epevents, struct input_event *ev)
Dima Zavinbc290632011-08-30 11:59:45 -0700167{
168 int r;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800169
Todd Poynor1fc89d42013-09-10 16:52:54 -0700170 if (epevents & EPOLLIN) {
Dima Zavinbc290632011-08-30 11:59:45 -0700171 r = read(fd, ev, sizeof(*ev));
172 if (r == sizeof(*ev))
173 return 0;
174 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175 return -1;
176}
Dima Zavin441031d2011-10-12 15:53:29 -0700177
178int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data)
179{
180 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)];
181 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
182 unsigned i;
183 int ret;
184
185 for (i = 0; i < ev_dev_count; i++) {
186 int code;
187
188 memset(key_bits, 0, sizeof(key_bits));
189 memset(ev_bits, 0, sizeof(ev_bits));
190
Todd Poynor1fc89d42013-09-10 16:52:54 -0700191 ret = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
Dima Zavin441031d2011-10-12 15:53:29 -0700192 if (ret < 0 || !test_bit(EV_KEY, ev_bits))
193 continue;
194
Todd Poynor1fc89d42013-09-10 16:52:54 -0700195 ret = ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits);
Dima Zavin441031d2011-10-12 15:53:29 -0700196 if (ret < 0)
197 continue;
198
199 for (code = 0; code <= KEY_MAX; code++) {
200 if (test_bit(code, key_bits))
201 set_key_cb(code, 1, data);
202 }
203 }
204
205 return 0;
206}