release 0.1
[u-tools.git] / u-tools / apps / android_port / libminui / events.c
blob8fb00534524fd9619c70b9edc980366396c6f5f5
1 /*
2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <dirent.h>
21 #include <sys/poll.h>
23 #include <linux/input.h>
25 #include "include/minui.h"
27 #define MAX_DEVICES 16
29 static struct pollfd ev_fds[MAX_DEVICES];
30 static unsigned ev_count = 0;
32 int ev_init(void)
34 DIR *dir;
35 struct dirent *de;
36 int fd;
38 dir = opendir("/dev/input");
39 if(dir != 0) {
40 while((de = readdir(dir))) {
41 // fprintf(stderr,"/dev/input/%s\n", de->d_name);
42 if(strncmp(de->d_name,"event",5)) continue;
43 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
44 if(fd < 0) continue;
46 ev_fds[ev_count].fd = fd;
47 ev_fds[ev_count].events = POLLIN;
48 ev_count++;
49 if(ev_count == MAX_DEVICES) break;
53 return 0;
56 void ev_exit(void)
58 while (ev_count > 0) {
59 close(ev_fds[--ev_count].fd);
63 int ev_get(struct input_event *ev, unsigned dont_wait)
65 int r;
66 unsigned n;
68 do {
69 r = poll(ev_fds, ev_count, dont_wait ? 0 : -1);
71 if(r > 0) {
72 for(n = 0; n < ev_count; n++) {
73 if(ev_fds[n].revents & POLLIN) {
74 r = read(ev_fds[n].fd, ev, sizeof(*ev));
75 if(r == sizeof(*ev)) return 0;
79 } while(dont_wait == 0);
81 return -1;