blob: 3aed2a8600e9f7a245dd34bc400cba9996416289 [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001/*
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
28
29static struct pollfd ev_fds[MAX_DEVICES];
30static unsigned ev_count = 0;
31
32int ev_init(void)
33{
34 DIR *dir;
35 struct dirent *de;
36 int fd;
37
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;
45
46 ev_fds[ev_count].fd = fd;
47 ev_fds[ev_count].events = POLLIN;
48 ev_count++;
49 if(ev_count == MAX_DEVICES) break;
50 }
51 }
52
53 return 0;
54}
55
56void ev_exit(void)
57{
58 while (ev_count > 0) {
59 close(ev_fds[--ev_count].fd);
60 }
61}
62
63int ev_get(struct input_event *ev, unsigned dont_wait)
64{
65 int r;
66 unsigned n;
67
68 do {
69 r = poll(ev_fds, ev_count, dont_wait ? 0 : -1);
70
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;
76 }
77 }
78 }
79 } while(dont_wait == 0);
80
81 return -1;
82}