Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [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 <dirent.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/epoll.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <linux/input.h> |
| 26 | |
| 27 | #include "minui.h" |
| 28 | |
| 29 | #define MAX_DEVICES 16 |
| 30 | #define MAX_MISC_FDS 16 |
| 31 | |
| 32 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) |
| 33 | #define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) |
| 34 | |
| 35 | struct fd_info { |
| 36 | int fd; |
| 37 | ev_callback cb; |
| 38 | void* data; |
| 39 | }; |
| 40 | |
| 41 | static int g_epoll_fd; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 42 | static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 43 | static int npolledevents; |
| 44 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 45 | static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 46 | |
| 47 | static unsigned ev_count = 0; |
| 48 | static unsigned ev_dev_count = 0; |
| 49 | static unsigned ev_misc_count = 0; |
| 50 | |
| 51 | static bool test_bit(size_t bit, unsigned long* array) { |
| 52 | return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; |
| 53 | } |
| 54 | |
| 55 | int ev_init(ev_callback input_cb, void* data) { |
| 56 | bool epollctlfail = false; |
| 57 | |
| 58 | g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); |
| 59 | if (g_epoll_fd == -1) { |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | DIR* dir = opendir("/dev/input"); |
| 64 | if (dir != NULL) { |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 65 | dirent* de; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 66 | while ((de = readdir(dir))) { |
| 67 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; |
| 68 | |
| 69 | // fprintf(stderr,"/dev/input/%s\n", de->d_name); |
| 70 | if (strncmp(de->d_name, "event", 5)) continue; |
| 71 | int fd = openat(dirfd(dir), de->d_name, O_RDONLY); |
| 72 | if (fd == -1) continue; |
| 73 | |
| 74 | // Read the evbits of the input device. |
| 75 | if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 76 | close(fd); |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. |
| 81 | if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { |
| 82 | close(fd); |
| 83 | continue; |
| 84 | } |
| 85 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 86 | epoll_event ev; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 87 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 88 | ev.data.ptr = &ev_fdinfo[ev_count]; |
| 89 | if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 90 | close(fd); |
| 91 | epollctlfail = true; |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | ev_fdinfo[ev_count].fd = fd; |
| 96 | ev_fdinfo[ev_count].cb = input_cb; |
| 97 | ev_fdinfo[ev_count].data = data; |
| 98 | ev_count++; |
| 99 | ev_dev_count++; |
| 100 | if (ev_dev_count == MAX_DEVICES) break; |
| 101 | } |
| 102 | |
| 103 | closedir(dir); |
| 104 | } |
| 105 | |
| 106 | if (epollctlfail && !ev_count) { |
| 107 | close(g_epoll_fd); |
| 108 | g_epoll_fd = -1; |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int ev_get_epollfd(void) { |
| 116 | return g_epoll_fd; |
| 117 | } |
| 118 | |
| 119 | int ev_add_fd(int fd, ev_callback cb, void* data) { |
| 120 | if (ev_misc_count == MAX_MISC_FDS || cb == NULL) { |
| 121 | return -1; |
| 122 | } |
| 123 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 124 | epoll_event ev; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 125 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 126 | ev.data.ptr = (void *)&ev_fdinfo[ev_count]; |
| 127 | int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 128 | if (!ret) { |
| 129 | ev_fdinfo[ev_count].fd = fd; |
| 130 | ev_fdinfo[ev_count].cb = cb; |
| 131 | ev_fdinfo[ev_count].data = data; |
| 132 | ev_count++; |
| 133 | ev_misc_count++; |
| 134 | } |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | void ev_exit(void) { |
| 140 | while (ev_count > 0) { |
| 141 | close(ev_fdinfo[--ev_count].fd); |
| 142 | } |
| 143 | ev_misc_count = 0; |
| 144 | ev_dev_count = 0; |
| 145 | close(g_epoll_fd); |
| 146 | } |
| 147 | |
| 148 | int ev_wait(int timeout) { |
| 149 | npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout); |
| 150 | if (npolledevents <= 0) { |
| 151 | return -1; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | void ev_dispatch(void) { |
| 157 | for (int n = 0; n < npolledevents; n++) { |
| 158 | fd_info* fdi = reinterpret_cast<fd_info*>(polledevents[n].data.ptr); |
| 159 | ev_callback cb = fdi->cb; |
| 160 | if (cb) { |
| 161 | cb(fdi->fd, polledevents[n].events, fdi->data); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 166 | int ev_get_input(int fd, uint32_t epevents, input_event* ev) { |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 167 | if (epevents & EPOLLIN) { |
| 168 | ssize_t r = read(fd, ev, sizeof(*ev)); |
| 169 | if (r == sizeof(*ev)) { |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { |
| 177 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; |
| 178 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; |
| 179 | |
| 180 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 181 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 182 | memset(key_bits, 0, sizeof(key_bits)); |
| 183 | |
| 184 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 185 | continue; |
| 186 | } |
| 187 | if (!test_bit(EV_KEY, ev_bits)) { |
| 188 | continue; |
| 189 | } |
| 190 | if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) { |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | for (int code = 0; code <= KEY_MAX; code++) { |
| 195 | if (test_bit(code, key_bits)) { |
| 196 | set_key_cb(code, 1, data); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 204 | void ev_iterate_available_keys(std::function<void(int)> f) { |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 205 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; |
| 206 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; |
| 207 | |
| 208 | for (size_t i = 0; i < ev_dev_count; ++i) { |
| 209 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 210 | memset(key_bits, 0, sizeof(key_bits)); |
| 211 | |
| 212 | // Does this device even have keys? |
| 213 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 214 | continue; |
| 215 | } |
| 216 | if (!test_bit(EV_KEY, ev_bits)) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits); |
| 221 | if (rc == -1) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 226 | if (test_bit(key_code, key_bits)) { |
Elliott Hughes | 642aaa7 | 2015-04-10 12:47:46 -0700 | [diff] [blame] | 227 | f(key_code); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |