The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 29 | struct fd_info { |
| 30 | ev_callback cb; |
| 31 | void *data; |
| 32 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 34 | static struct pollfd ev_fds[MAX_DEVICES]; |
| 35 | static struct fd_info ev_fdinfo[MAX_DEVICES]; |
| 36 | |
| 37 | static unsigned ev_count = 0; |
| 38 | static unsigned ev_dev_count = 0; |
| 39 | |
| 40 | int ev_init(ev_callback input_cb, void *data) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 41 | { |
| 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 Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 56 | ev_fdinfo[ev_count].cb = input_cb; |
| 57 | ev_fdinfo[ev_count].data = data; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 58 | ev_count++; |
| 59 | if(ev_count == MAX_DEVICES) break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | void ev_exit(void) |
| 67 | { |
| 68 | while (ev_count > 0) { |
| 69 | close(ev_fds[--ev_count].fd); |
| 70 | } |
| 71 | } |
| 72 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 73 | int ev_wait(int timeout) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 74 | { |
| 75 | int r; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 76 | |
| 77 | r = poll(ev_fds, ev_count, timeout); |
| 78 | if (r <= 0) |
| 79 | return -1; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | void ev_dispatch(void) |
| 84 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 85 | unsigned n; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 86 | int ret; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 87 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 88 | 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 94 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 95 | int ev_get_input(int fd, short revents, struct input_event *ev) |
| 96 | { |
| 97 | int r; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 98 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame^] | 99 | if (revents & POLLIN) { |
| 100 | r = read(fd, ev, sizeof(*ev)); |
| 101 | if (r == sizeof(*ev)) |
| 102 | return 0; |
| 103 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 104 | return -1; |
| 105 | } |