blob: 8458b2d3915458fceb218d9a2e2807fb19f19a9a [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 Zavinbc290632011-08-30 11:59:45 -070030struct fd_info {
31 ev_callback cb;
32 void *data;
33};
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034
Dima Zavin36583672011-09-02 11:51:31 -070035static struct pollfd ev_fds[MAX_DEVICES + MAX_MISC_FDS];
36static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
Dima Zavinbc290632011-08-30 11:59:45 -070037
38static unsigned ev_count = 0;
39static unsigned ev_dev_count = 0;
Dima Zavin36583672011-09-02 11:51:31 -070040static unsigned ev_misc_count = 0;
Dima Zavinbc290632011-08-30 11:59:45 -070041
42int ev_init(ev_callback input_cb, void *data)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043{
44 DIR *dir;
45 struct dirent *de;
46 int fd;
47
48 dir = opendir("/dev/input");
49 if(dir != 0) {
50 while((de = readdir(dir))) {
51// fprintf(stderr,"/dev/input/%s\n", de->d_name);
52 if(strncmp(de->d_name,"event",5)) continue;
53 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
54 if(fd < 0) continue;
55
56 ev_fds[ev_count].fd = fd;
57 ev_fds[ev_count].events = POLLIN;
Dima Zavinbc290632011-08-30 11:59:45 -070058 ev_fdinfo[ev_count].cb = input_cb;
59 ev_fdinfo[ev_count].data = data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060 ev_count++;
Dima Zavin36583672011-09-02 11:51:31 -070061 ev_dev_count++;
62 if(ev_dev_count == MAX_DEVICES) break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063 }
64 }
65
66 return 0;
67}
68
Dima Zavin36583672011-09-02 11:51:31 -070069int ev_add_fd(int fd, ev_callback cb, void *data)
70{
71 if (ev_misc_count == MAX_MISC_FDS || cb == NULL)
72 return -1;
73
74 ev_fds[ev_count].fd = fd;
75 ev_fds[ev_count].events = POLLIN;
76 ev_fdinfo[ev_count].cb = cb;
77 ev_fdinfo[ev_count].data = data;
78 ev_count++;
79 ev_misc_count++;
80 return 0;
81}
82
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083void ev_exit(void)
84{
85 while (ev_count > 0) {
86 close(ev_fds[--ev_count].fd);
87 }
Dima Zavin36583672011-09-02 11:51:31 -070088 ev_misc_count = 0;
89 ev_dev_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090}
91
Dima Zavinbc290632011-08-30 11:59:45 -070092int ev_wait(int timeout)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093{
94 int r;
Dima Zavinbc290632011-08-30 11:59:45 -070095
96 r = poll(ev_fds, ev_count, timeout);
97 if (r <= 0)
98 return -1;
99 return 0;
100}
101
102void ev_dispatch(void)
103{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 unsigned n;
Dima Zavinbc290632011-08-30 11:59:45 -0700105 int ret;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800106
Dima Zavinbc290632011-08-30 11:59:45 -0700107 for (n = 0; n < ev_count; n++) {
108 ev_callback cb = ev_fdinfo[n].cb;
109 if (cb && (ev_fds[n].revents & ev_fds[n].events))
110 cb(ev_fds[n].fd, ev_fds[n].revents, ev_fdinfo[n].data);
111 }
112}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113
Dima Zavinbc290632011-08-30 11:59:45 -0700114int ev_get_input(int fd, short revents, struct input_event *ev)
115{
116 int r;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117
Dima Zavinbc290632011-08-30 11:59:45 -0700118 if (revents & POLLIN) {
119 r = read(fd, ev, sizeof(*ev));
120 if (r == sizeof(*ev))
121 return 0;
122 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123 return -1;
124}