blob: 9e4255dd7541854b4ca8a870ed462ad7e66719d5 [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>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
20#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <fcntl.h>
22#include <dirent.h>
Todd Poynor1fc89d42013-09-10 16:52:54 -070023#include <sys/epoll.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024
25#include <linux/input.h>
26
27#include "minui.h"
28
29#define MAX_DEVICES 16
Dima Zavin36583672011-09-02 11:51:31 -070030#define MAX_MISC_FDS 16
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Dima Zavin441031d2011-10-12 15:53:29 -070032#define BITS_PER_LONG (sizeof(unsigned long) * 8)
33#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
34
35#define test_bit(bit, array) \
36 ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG)))
Dima Zavin88e08992011-09-02 14:24:43 -070037
Dima Zavinbc290632011-08-30 11:59:45 -070038struct fd_info {
Todd Poynor1fc89d42013-09-10 16:52:54 -070039 int fd;
Dima Zavinbc290632011-08-30 11:59:45 -070040 ev_callback cb;
41 void *data;
42};
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Todd Poynor1fc89d42013-09-10 16:52:54 -070044static int epollfd;
45static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
46static int npolledevents;
47
Dima Zavin36583672011-09-02 11:51:31 -070048static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Dima Zavinbc290632011-08-30 11:59:45 -070049
50static unsigned ev_count = 0;
51static unsigned ev_dev_count = 0;
Dima Zavin36583672011-09-02 11:51:31 -070052static unsigned ev_misc_count = 0;
Dima Zavinbc290632011-08-30 11:59:45 -070053
54int ev_init(ev_callback input_cb, void *data)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055{
56 DIR *dir;
57 struct dirent *de;
58 int fd;
Todd Poynor1fc89d42013-09-10 16:52:54 -070059 struct epoll_event ev;
60 bool epollctlfail = false;
61
62 epollfd = epoll_create(MAX_DEVICES + MAX_MISC_FDS);
63 if (epollfd == -1)
64 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065
66 dir = opendir("/dev/input");
67 if(dir != 0) {
68 while((de = readdir(dir))) {
Dima Zavin441031d2011-10-12 15:53:29 -070069 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
Dima Zavin88e08992011-09-02 14:24:43 -070070
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071// fprintf(stderr,"/dev/input/%s\n", de->d_name);
72 if(strncmp(de->d_name,"event",5)) continue;
73 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
74 if(fd < 0) continue;
75
Dima Zavin88e08992011-09-02 14:24:43 -070076 /* read the evbits of the input device */
77 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) {
78 close(fd);
79 continue;
80 }
81
82 /* TODO: add ability to specify event masks. For now, just assume
Ajay Dudani07d96272015-02-04 16:51:34 -080083 * that only EV_KEY, EV_REL & EV_SW event types are ever needed. */
84 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) {
Dima Zavin88e08992011-09-02 14:24:43 -070085 close(fd);
86 continue;
87 }
88
Todd Poynor1fc89d42013-09-10 16:52:54 -070089 ev.events = EPOLLIN | EPOLLWAKEUP;
90 ev.data.ptr = (void *)&ev_fdinfo[ev_count];
91 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
92 close(fd);
93 epollctlfail = true;
94 continue;
95 }
96
97 ev_fdinfo[ev_count].fd = fd;
Dima Zavinbc290632011-08-30 11:59:45 -070098 ev_fdinfo[ev_count].cb = input_cb;
99 ev_fdinfo[ev_count].data = data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 ev_count++;
Dima Zavin36583672011-09-02 11:51:31 -0700101 ev_dev_count++;
102 if(ev_dev_count == MAX_DEVICES) break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 }
104 }
105
Todd Poynor1fc89d42013-09-10 16:52:54 -0700106 if (epollctlfail && !ev_count) {
107 close(epollfd);
108 epollfd = -1;
109 return -1;
110 }
111
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 return 0;
113}
114
Dima Zavin36583672011-09-02 11:51:31 -0700115int ev_add_fd(int fd, ev_callback cb, void *data)
116{
Todd Poynor1fc89d42013-09-10 16:52:54 -0700117 struct epoll_event ev;
118 int ret;
119
Dima Zavin36583672011-09-02 11:51:31 -0700120 if (ev_misc_count == MAX_MISC_FDS || cb == NULL)
121 return -1;
122
Todd Poynor1fc89d42013-09-10 16:52:54 -0700123 ev.events = EPOLLIN | EPOLLWAKEUP;
124 ev.data.ptr = (void *)&ev_fdinfo[ev_count];
125 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
126 if (!ret) {
127 ev_fdinfo[ev_count].fd = fd;
128 ev_fdinfo[ev_count].cb = cb;
129 ev_fdinfo[ev_count].data = data;
130 ev_count++;
131 ev_misc_count++;
132 }
133
134 return ret;
Dima Zavin36583672011-09-02 11:51:31 -0700135}
136
Todd Poynor4665ede2013-09-10 17:03:45 -0700137int ev_get_epollfd(void)
138{
139 return epollfd;
140}
141
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142void ev_exit(void)
143{
144 while (ev_count > 0) {
Todd Poynor1fc89d42013-09-10 16:52:54 -0700145 close(ev_fdinfo[--ev_count].fd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146 }
Dima Zavin36583672011-09-02 11:51:31 -0700147 ev_misc_count = 0;
148 ev_dev_count = 0;
Todd Poynor1fc89d42013-09-10 16:52:54 -0700149 close(epollfd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150}
151
Dima Zavinbc290632011-08-30 11:59:45 -0700152int ev_wait(int timeout)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153{
Todd Poynor1fc89d42013-09-10 16:52:54 -0700154 npolledevents = epoll_wait(epollfd, polledevents, ev_count, timeout);
155 if (npolledevents <= 0)
Dima Zavinbc290632011-08-30 11:59:45 -0700156 return -1;
157 return 0;
158}
159
160void ev_dispatch(void)
161{
Todd Poynor1fc89d42013-09-10 16:52:54 -0700162 int n;
Dima Zavinbc290632011-08-30 11:59:45 -0700163 int ret;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164
Todd Poynor1fc89d42013-09-10 16:52:54 -0700165 for (n = 0; n < npolledevents; n++) {
166 struct fd_info *fdi = polledevents[n].data.ptr;
167 ev_callback cb = fdi->cb;
168 if (cb)
169 cb(fdi->fd, polledevents[n].events, fdi->data);
Dima Zavinbc290632011-08-30 11:59:45 -0700170 }
171}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172
Todd Poynora5ef19f2013-09-17 13:39:10 -0700173int ev_get_input(int fd, uint32_t epevents, struct input_event *ev)
Dima Zavinbc290632011-08-30 11:59:45 -0700174{
175 int r;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176
Todd Poynor1fc89d42013-09-10 16:52:54 -0700177 if (epevents & EPOLLIN) {
Dima Zavinbc290632011-08-30 11:59:45 -0700178 r = read(fd, ev, sizeof(*ev));
179 if (r == sizeof(*ev))
180 return 0;
181 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 return -1;
183}
Dima Zavin441031d2011-10-12 15:53:29 -0700184
185int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data)
186{
187 unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)];
188 unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
189 unsigned i;
190 int ret;
191
192 for (i = 0; i < ev_dev_count; i++) {
193 int code;
194
195 memset(key_bits, 0, sizeof(key_bits));
196 memset(ev_bits, 0, sizeof(ev_bits));
197
Todd Poynor1fc89d42013-09-10 16:52:54 -0700198 ret = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
Dima Zavin441031d2011-10-12 15:53:29 -0700199 if (ret < 0 || !test_bit(EV_KEY, ev_bits))
200 continue;
201
Todd Poynor1fc89d42013-09-10 16:52:54 -0700202 ret = ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits);
Dima Zavin441031d2011-10-12 15:53:29 -0700203 if (ret < 0)
204 continue;
205
206 for (code = 0; code <= KEY_MAX; code++) {
207 if (test_bit(code, key_bits))
208 set_key_cb(code, 1, data);
209 }
210 }
211
212 return 0;
213}