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 | 88e0899 | 2011-09-02 14:24:43 -0700 | [diff] [blame] | 30 | #define test_bit(bit, array) ((array)[(bit)/8] & (1<<((bit)%8))) |
| 31 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 32 | struct fd_info { |
| 33 | ev_callback cb; |
| 34 | void *data; |
| 35 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 36 | |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 37 | static struct pollfd ev_fds[MAX_DEVICES + MAX_MISC_FDS]; |
| 38 | static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 39 | |
| 40 | static unsigned ev_count = 0; |
| 41 | static unsigned ev_dev_count = 0; |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 42 | static unsigned ev_misc_count = 0; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 43 | |
| 44 | int ev_init(ev_callback input_cb, void *data) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | { |
| 46 | DIR *dir; |
| 47 | struct dirent *de; |
| 48 | int fd; |
| 49 | |
| 50 | dir = opendir("/dev/input"); |
| 51 | if(dir != 0) { |
| 52 | while((de = readdir(dir))) { |
Dima Zavin | 88e0899 | 2011-09-02 14:24:43 -0700 | [diff] [blame] | 53 | uint8_t ev_bits[(EV_MAX + 1) / 8]; |
| 54 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 55 | // fprintf(stderr,"/dev/input/%s\n", de->d_name); |
| 56 | if(strncmp(de->d_name,"event",5)) continue; |
| 57 | fd = openat(dirfd(dir), de->d_name, O_RDONLY); |
| 58 | if(fd < 0) continue; |
| 59 | |
Dima Zavin | 88e0899 | 2011-09-02 14:24:43 -0700 | [diff] [blame] | 60 | /* read the evbits of the input device */ |
| 61 | if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) { |
| 62 | close(fd); |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | /* TODO: add ability to specify event masks. For now, just assume |
| 67 | * that only EV_KEY and EV_REL event types are ever needed. */ |
| 68 | if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) { |
| 69 | close(fd); |
| 70 | continue; |
| 71 | } |
| 72 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 73 | ev_fds[ev_count].fd = fd; |
| 74 | ev_fds[ev_count].events = POLLIN; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 75 | ev_fdinfo[ev_count].cb = input_cb; |
| 76 | ev_fdinfo[ev_count].data = data; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 77 | ev_count++; |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 78 | ev_dev_count++; |
| 79 | if(ev_dev_count == MAX_DEVICES) break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 86 | int ev_add_fd(int fd, ev_callback cb, void *data) |
| 87 | { |
| 88 | if (ev_misc_count == MAX_MISC_FDS || cb == NULL) |
| 89 | return -1; |
| 90 | |
| 91 | ev_fds[ev_count].fd = fd; |
| 92 | ev_fds[ev_count].events = POLLIN; |
| 93 | ev_fdinfo[ev_count].cb = cb; |
| 94 | ev_fdinfo[ev_count].data = data; |
| 95 | ev_count++; |
| 96 | ev_misc_count++; |
| 97 | return 0; |
| 98 | } |
| 99 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 100 | void ev_exit(void) |
| 101 | { |
| 102 | while (ev_count > 0) { |
| 103 | close(ev_fds[--ev_count].fd); |
| 104 | } |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 105 | ev_misc_count = 0; |
| 106 | ev_dev_count = 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 109 | int ev_wait(int timeout) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 110 | { |
| 111 | int r; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 112 | |
| 113 | r = poll(ev_fds, ev_count, timeout); |
| 114 | if (r <= 0) |
| 115 | return -1; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | void ev_dispatch(void) |
| 120 | { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 121 | unsigned n; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 122 | int ret; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 124 | for (n = 0; n < ev_count; n++) { |
| 125 | ev_callback cb = ev_fdinfo[n].cb; |
| 126 | if (cb && (ev_fds[n].revents & ev_fds[n].events)) |
| 127 | cb(ev_fds[n].fd, ev_fds[n].revents, ev_fdinfo[n].data); |
| 128 | } |
| 129 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 131 | int ev_get_input(int fd, short revents, struct input_event *ev) |
| 132 | { |
| 133 | int r; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 134 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 135 | if (revents & POLLIN) { |
| 136 | r = read(fd, ev, sizeof(*ev)); |
| 137 | if (r == sizeof(*ev)) |
| 138 | return 0; |
| 139 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 140 | return -1; |
| 141 | } |