blob: c533a482a3ee856f35b8b311cd712b80e601e160 [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>
21#include <sys/poll.h>
22
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 Zavin88e08992011-09-02 14:24:43 -070030#define test_bit(bit, array) ((array)[(bit)/8] & (1<<((bit)%8)))
31
Dima Zavinbc290632011-08-30 11:59:45 -070032struct fd_info {
33 ev_callback cb;
34 void *data;
35};
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Dima Zavin36583672011-09-02 11:51:31 -070037static struct pollfd ev_fds[MAX_DEVICES + MAX_MISC_FDS];
38static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Dima Zavinbc290632011-08-30 11:59:45 -070039
40static unsigned ev_count = 0;
41static unsigned ev_dev_count = 0;
Dima Zavin36583672011-09-02 11:51:31 -070042static unsigned ev_misc_count = 0;
Dima Zavinbc290632011-08-30 11:59:45 -070043
44int ev_init(ev_callback input_cb, void *data)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045{
46 DIR *dir;
47 struct dirent *de;
48 int fd;
49
50 dir = opendir("/dev/input");
51 if(dir != 0) {
52 while((de = readdir(dir))) {
Dima Zavin88e08992011-09-02 14:24:43 -070053 uint8_t ev_bits[(EV_MAX + 1) / 8];
54
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055// fprintf(stderr,"/dev/input/%s\n", de->d_name);
56 if(strncmp(de->d_name,"event",5)) continue;
57 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
58 if(fd < 0) continue;
59
Dima Zavin88e08992011-09-02 14:24:43 -070060 /* read the evbits of the input device */
61 if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) {
62 close(fd);
63 continue;
64 }
65
66 /* TODO: add ability to specify event masks. For now, just assume
67 * that only EV_KEY and EV_REL event types are ever needed. */
68 if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) {
69 close(fd);
70 continue;
71 }
72
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 ev_fds[ev_count].fd = fd;
74 ev_fds[ev_count].events = POLLIN;
Dima Zavinbc290632011-08-30 11:59:45 -070075 ev_fdinfo[ev_count].cb = input_cb;
76 ev_fdinfo[ev_count].data = data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 ev_count++;
Dima Zavin36583672011-09-02 11:51:31 -070078 ev_dev_count++;
79 if(ev_dev_count == MAX_DEVICES) break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080 }
81 }
82
83 return 0;
84}
85
Dima Zavin36583672011-09-02 11:51:31 -070086int ev_add_fd(int fd, ev_callback cb, void *data)
87{
88 if (ev_misc_count == MAX_MISC_FDS || cb == NULL)
89 return -1;
90
91 ev_fds[ev_count].fd = fd;
92 ev_fds[ev_count].events = POLLIN;
93 ev_fdinfo[ev_count].cb = cb;
94 ev_fdinfo[ev_count].data = data;
95 ev_count++;
96 ev_misc_count++;
97 return 0;
98}
99
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100void ev_exit(void)
101{
102 while (ev_count > 0) {
103 close(ev_fds[--ev_count].fd);
104 }
Dima Zavin36583672011-09-02 11:51:31 -0700105 ev_misc_count = 0;
106 ev_dev_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800107}
108
Dima Zavinbc290632011-08-30 11:59:45 -0700109int ev_wait(int timeout)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800110{
111 int r;
Dima Zavinbc290632011-08-30 11:59:45 -0700112
113 r = poll(ev_fds, ev_count, timeout);
114 if (r <= 0)
115 return -1;
116 return 0;
117}
118
119void ev_dispatch(void)
120{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121 unsigned n;
Dima Zavinbc290632011-08-30 11:59:45 -0700122 int ret;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123
Dima Zavinbc290632011-08-30 11:59:45 -0700124 for (n = 0; n < ev_count; n++) {
125 ev_callback cb = ev_fdinfo[n].cb;
126 if (cb && (ev_fds[n].revents & ev_fds[n].events))
127 cb(ev_fds[n].fd, ev_fds[n].revents, ev_fdinfo[n].data);
128 }
129}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130
Dima Zavinbc290632011-08-30 11:59:45 -0700131int ev_get_input(int fd, short revents, struct input_event *ev)
132{
133 int r;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134
Dima Zavinbc290632011-08-30 11:59:45 -0700135 if (revents & POLLIN) {
136 r = read(fd, ev, sizeof(*ev));
137 if (r == sizeof(*ev))
138 return 0;
139 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 return -1;
141}