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