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 |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame^] | 28 | #define MAX_MISC_FDS 16 |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 29 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 30 | struct fd_info { |
| 31 | ev_callback cb; |
| 32 | void *data; |
| 33 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame^] | 35 | static struct pollfd ev_fds[MAX_DEVICES + MAX_MISC_FDS]; |
| 36 | static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 37 | |
| 38 | static unsigned ev_count = 0; |
| 39 | static unsigned ev_dev_count = 0; |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame^] | 40 | static unsigned ev_misc_count = 0; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 41 | |
| 42 | int ev_init(ev_callback input_cb, void *data) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 43 | { |
| 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 Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 58 | ev_fdinfo[ev_count].cb = input_cb; |
| 59 | ev_fdinfo[ev_count].data = data; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 60 | ev_count++; |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame^] | 61 | ev_dev_count++; |
| 62 | if(ev_dev_count == MAX_DEVICES) break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame^] | 69 | int 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 83 | void ev_exit(void) |
| 84 | { |
| 85 | while (ev_count > 0) { |
| 86 | close(ev_fds[--ev_count].fd); |
| 87 | } |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame^] | 88 | ev_misc_count = 0; |
| 89 | ev_dev_count = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 92 | int ev_wait(int timeout) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 93 | { |
| 94 | int r; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 95 | |
| 96 | r = poll(ev_fds, ev_count, timeout); |
| 97 | if (r <= 0) |
| 98 | return -1; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | void ev_dispatch(void) |
| 103 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 104 | unsigned n; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 105 | int ret; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 106 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 107 | 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 113 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 114 | int ev_get_input(int fd, short revents, struct input_event *ev) |
| 115 | { |
| 116 | int r; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 117 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 118 | if (revents & POLLIN) { |
| 119 | r = read(fd, ev, sizeof(*ev)); |
| 120 | if (r == sizeof(*ev)) |
| 121 | return 0; |
| 122 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | return -1; |
| 124 | } |