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> |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 21 | #include <sys/epoll.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | 441031d | 2011-10-12 15:53:29 -0700 | [diff] [blame] | 30 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) |
| 31 | #define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) |
| 32 | |
| 33 | #define test_bit(bit, array) \ |
| 34 | ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG))) |
Dima Zavin | 88e0899 | 2011-09-02 14:24:43 -0700 | [diff] [blame] | 35 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 36 | struct fd_info { |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 37 | int fd; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 38 | ev_callback cb; |
| 39 | void *data; |
| 40 | }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 41 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 42 | static int epollfd; |
| 43 | static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; |
| 44 | static int npolledevents; |
| 45 | |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 46 | static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 47 | |
| 48 | static unsigned ev_count = 0; |
| 49 | static unsigned ev_dev_count = 0; |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 50 | static unsigned ev_misc_count = 0; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 51 | |
| 52 | int ev_init(ev_callback input_cb, void *data) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 | { |
| 54 | DIR *dir; |
| 55 | struct dirent *de; |
| 56 | int fd; |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 57 | struct epoll_event ev; |
| 58 | bool epollctlfail = false; |
| 59 | |
| 60 | epollfd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); |
| 61 | if (epollfd == -1) |
| 62 | return -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | |
| 64 | dir = opendir("/dev/input"); |
| 65 | if(dir != 0) { |
| 66 | while((de = readdir(dir))) { |
Dima Zavin | 441031d | 2011-10-12 15:53:29 -0700 | [diff] [blame] | 67 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; |
Dima Zavin | 88e0899 | 2011-09-02 14:24:43 -0700 | [diff] [blame] | 68 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | // fprintf(stderr,"/dev/input/%s\n", de->d_name); |
| 70 | if(strncmp(de->d_name,"event",5)) continue; |
| 71 | fd = openat(dirfd(dir), de->d_name, O_RDONLY); |
| 72 | if(fd < 0) continue; |
| 73 | |
Dima Zavin | 88e0899 | 2011-09-02 14:24:43 -0700 | [diff] [blame] | 74 | /* read the evbits of the input device */ |
| 75 | if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) { |
| 76 | close(fd); |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | /* TODO: add ability to specify event masks. For now, just assume |
| 81 | * that only EV_KEY and EV_REL event types are ever needed. */ |
| 82 | if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) { |
| 83 | close(fd); |
| 84 | continue; |
| 85 | } |
| 86 | |
Ethan Yonker | 452e420 | 2014-12-11 10:35:52 -0600 | [diff] [blame] | 87 | #ifdef EPOLLWAKEUP |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 88 | ev.events = EPOLLIN | EPOLLWAKEUP; |
Ethan Yonker | 452e420 | 2014-12-11 10:35:52 -0600 | [diff] [blame] | 89 | #else |
| 90 | ev.events = EPOLLIN; |
| 91 | #endif |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 92 | ev.data.ptr = (void *)&ev_fdinfo[ev_count]; |
| 93 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) { |
| 94 | close(fd); |
| 95 | epollctlfail = true; |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | ev_fdinfo[ev_count].fd = fd; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 100 | ev_fdinfo[ev_count].cb = input_cb; |
| 101 | ev_fdinfo[ev_count].data = data; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 102 | ev_count++; |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 103 | ev_dev_count++; |
| 104 | if(ev_dev_count == MAX_DEVICES) break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 108 | if (epollctlfail && !ev_count) { |
| 109 | close(epollfd); |
| 110 | epollfd = -1; |
| 111 | return -1; |
| 112 | } |
| 113 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 117 | int ev_add_fd(int fd, ev_callback cb, void *data) |
| 118 | { |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 119 | struct epoll_event ev; |
| 120 | int ret; |
| 121 | |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 122 | if (ev_misc_count == MAX_MISC_FDS || cb == NULL) |
| 123 | return -1; |
| 124 | |
Ethan Yonker | 452e420 | 2014-12-11 10:35:52 -0600 | [diff] [blame] | 125 | #ifdef EPOLLWAKEUP |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 126 | ev.events = EPOLLIN | EPOLLWAKEUP; |
Ethan Yonker | 452e420 | 2014-12-11 10:35:52 -0600 | [diff] [blame] | 127 | #else |
| 128 | ev.events = EPOLLIN; |
| 129 | #endif |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 130 | ev.data.ptr = (void *)&ev_fdinfo[ev_count]; |
| 131 | ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); |
| 132 | if (!ret) { |
| 133 | ev_fdinfo[ev_count].fd = fd; |
| 134 | ev_fdinfo[ev_count].cb = cb; |
| 135 | ev_fdinfo[ev_count].data = data; |
| 136 | ev_count++; |
| 137 | ev_misc_count++; |
| 138 | } |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | int ev_get_epollfd(void) |
| 144 | { |
| 145 | return epollfd; |
Todd Poynor | 4665ede | 2013-09-10 17:03:45 -0700 | [diff] [blame] | 146 | } |
| 147 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 148 | void ev_exit(void) |
| 149 | { |
| 150 | while (ev_count > 0) { |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 151 | close(ev_fdinfo[--ev_count].fd); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | } |
Dima Zavin | 3658367 | 2011-09-02 11:51:31 -0700 | [diff] [blame] | 153 | ev_misc_count = 0; |
| 154 | ev_dev_count = 0; |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 155 | close(epollfd); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 158 | int ev_wait(int timeout) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 159 | { |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 160 | npolledevents = epoll_wait(epollfd, polledevents, ev_count, timeout); |
| 161 | if (npolledevents <= 0) |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 162 | return -1; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | void ev_dispatch(void) |
| 167 | { |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 168 | int n; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 169 | int ret; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 170 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 171 | for (n = 0; n < npolledevents; n++) { |
| 172 | struct fd_info *fdi = polledevents[n].data.ptr; |
| 173 | ev_callback cb = fdi->cb; |
| 174 | if (cb) |
| 175 | cb(fdi->fd, polledevents[n].events, fdi->data); |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 178 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 179 | int ev_get_input(int fd, uint32_t epevents, struct input_event *ev) |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 180 | { |
| 181 | int r; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 182 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 183 | if (epevents & EPOLLIN) { |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 184 | r = read(fd, ev, sizeof(*ev)); |
| 185 | if (r == sizeof(*ev)) |
| 186 | return 0; |
| 187 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 188 | return -1; |
| 189 | } |
Dima Zavin | 441031d | 2011-10-12 15:53:29 -0700 | [diff] [blame] | 190 | |
| 191 | int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data) |
| 192 | { |
| 193 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; |
| 194 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; |
| 195 | unsigned i; |
| 196 | int ret; |
| 197 | |
| 198 | for (i = 0; i < ev_dev_count; i++) { |
| 199 | int code; |
| 200 | |
| 201 | memset(key_bits, 0, sizeof(key_bits)); |
| 202 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 203 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 204 | ret = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits); |
Dima Zavin | 441031d | 2011-10-12 15:53:29 -0700 | [diff] [blame] | 205 | if (ret < 0 || !test_bit(EV_KEY, ev_bits)) |
| 206 | continue; |
| 207 | |
Ethan Yonker | 304f32f | 2014-11-07 10:14:05 -0600 | [diff] [blame] | 208 | ret = ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits); |
Dima Zavin | 441031d | 2011-10-12 15:53:29 -0700 | [diff] [blame] | 209 | if (ret < 0) |
| 210 | continue; |
| 211 | |
| 212 | for (code = 0; code <= KEY_MAX; code++) { |
| 213 | if (test_bit(code, key_bits)) |
| 214 | set_key_cb(code, 1, data); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |