blob: eca3bc73b09b4339aa5df0f1f0f881acae7b0329 [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
28
Dima Zavinbc290632011-08-30 11:59:45 -070029struct fd_info {
30 ev_callback cb;
31 void *data;
32};
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Dima Zavinbc290632011-08-30 11:59:45 -070034static struct pollfd ev_fds[MAX_DEVICES];
35static struct fd_info ev_fdinfo[MAX_DEVICES];
36
37static unsigned ev_count = 0;
38static unsigned ev_dev_count = 0;
39
40int ev_init(ev_callback input_cb, void *data)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041{
42 DIR *dir;
43 struct dirent *de;
44 int fd;
45
46 dir = opendir("/dev/input");
47 if(dir != 0) {
48 while((de = readdir(dir))) {
49// fprintf(stderr,"/dev/input/%s\n", de->d_name);
50 if(strncmp(de->d_name,"event",5)) continue;
51 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
52 if(fd < 0) continue;
53
54 ev_fds[ev_count].fd = fd;
55 ev_fds[ev_count].events = POLLIN;
Dima Zavinbc290632011-08-30 11:59:45 -070056 ev_fdinfo[ev_count].cb = input_cb;
57 ev_fdinfo[ev_count].data = data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058 ev_count++;
59 if(ev_count == MAX_DEVICES) break;
60 }
61 }
62
63 return 0;
64}
65
66void ev_exit(void)
67{
68 while (ev_count > 0) {
69 close(ev_fds[--ev_count].fd);
70 }
71}
72
Dima Zavinbc290632011-08-30 11:59:45 -070073int ev_wait(int timeout)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074{
75 int r;
Dima Zavinbc290632011-08-30 11:59:45 -070076
77 r = poll(ev_fds, ev_count, timeout);
78 if (r <= 0)
79 return -1;
80 return 0;
81}
82
83void ev_dispatch(void)
84{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085 unsigned n;
Dima Zavinbc290632011-08-30 11:59:45 -070086 int ret;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080087
Dima Zavinbc290632011-08-30 11:59:45 -070088 for (n = 0; n < ev_count; n++) {
89 ev_callback cb = ev_fdinfo[n].cb;
90 if (cb && (ev_fds[n].revents & ev_fds[n].events))
91 cb(ev_fds[n].fd, ev_fds[n].revents, ev_fdinfo[n].data);
92 }
93}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094
Dima Zavinbc290632011-08-30 11:59:45 -070095int ev_get_input(int fd, short revents, struct input_event *ev)
96{
97 int r;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098
Dima Zavinbc290632011-08-30 11:59:45 -070099 if (revents & POLLIN) {
100 r = read(fd, ev, sizeof(*ev));
101 if (r == sizeof(*ev))
102 return 0;
103 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 return -1;
105}