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> |
Dan Albert | c8686b4 | 2017-10-11 11:47:54 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 20 | #include <linux/input.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/epoll.h> |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 25 | #include <sys/ioctl.h> |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Tao Bao | 9468fc0 | 2017-03-17 00:57:37 -0700 | [diff] [blame] | 29 | #include <functional> |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 30 | #include <memory> |
| 31 | |
| 32 | #include <android-base/unique_fd.h> |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 33 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 34 | #include "minui/minui.h" |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 35 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 36 | constexpr size_t MAX_DEVICES = 16; |
| 37 | constexpr size_t MAX_MISC_FDS = 16; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 38 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 39 | constexpr size_t BITS_PER_LONG = sizeof(unsigned long) * 8; |
| 40 | constexpr size_t BITS_TO_LONGS(size_t bits) { |
| 41 | return ((bits + BITS_PER_LONG - 1) / BITS_PER_LONG); |
| 42 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 43 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 44 | struct FdInfo { |
| 45 | android::base::unique_fd fd; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 46 | ev_callback cb; |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 47 | #ifdef TW_USE_MINUI_WITH_DATA |
| 48 | void* data; |
| 49 | #endif |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 52 | static android::base::unique_fd g_epoll_fd; |
| 53 | static epoll_event g_polled_events[MAX_DEVICES + MAX_MISC_FDS]; |
| 54 | static int g_polled_events_count; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 55 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 56 | static FdInfo ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 57 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 58 | static size_t g_ev_count = 0; |
| 59 | static size_t g_ev_dev_count = 0; |
| 60 | static size_t g_ev_misc_count = 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 61 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 62 | static bool test_bit(size_t bit, unsigned long* array) { // NOLINT |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 63 | return (array[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 66 | #ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS |
Tao Bao | af9f8b4 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 67 | int ev_init(ev_callback input_cb, bool allow_touch_inputs) { |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 68 | <<<<<<< HEAD |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 69 | #else |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 70 | #ifdef TW_USE_MINUI_WITH_DATA |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 71 | int ev_init(ev_callback input_cb, void* data) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 72 | #else |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 73 | int ev_init(ev_callback input_cb) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 74 | #endif |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 75 | bool allow_touch_inputs = false; |
| 76 | #endif |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 77 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 78 | g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); |
| 79 | if (g_epoll_fd == -1) { |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 80 | ======= |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 81 | g_epoll_fd.reset(); |
| 82 | |
| 83 | android::base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC)); |
| 84 | if (epoll_fd == -1) { |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 85 | >>>>>>> android-10.0.0_r25 |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 89 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir("/dev/input"), closedir); |
| 90 | if (!dir) { |
| 91 | return -1; |
| 92 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 93 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 94 | bool epoll_ctl_failed = false; |
| 95 | dirent* de; |
| 96 | while ((de = readdir(dir.get())) != nullptr) { |
| 97 | if (strncmp(de->d_name, "event", 5)) continue; |
| 98 | android::base::unique_fd fd(openat(dirfd(dir.get()), de->d_name, O_RDONLY | O_CLOEXEC)); |
| 99 | if (fd == -1) continue; |
Tao Bao | af9f8b4 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 100 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 101 | // Use unsigned long to match ioctl's parameter type. |
| 102 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 103 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 104 | // Read the evbits of the input device. |
| 105 | if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 106 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 109 | // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. EV_ABS is also |
| 110 | // allowed if allow_touch_inputs is set. |
| 111 | if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { |
| 112 | if (!allow_touch_inputs || !test_bit(EV_ABS, ev_bits)) { |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 113 | continue; |
| 114 | } |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 115 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 116 | |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 117 | <<<<<<< HEAD |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 118 | ev_fdinfo[ev_count].fd = fd; |
| 119 | ev_fdinfo[ev_count].cb = std::move(input_cb); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 120 | #ifdef TW_USE_MINUI_WITH_DATA |
| 121 | ev_fdinfo[ev_count].data = data; |
| 122 | #endif |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 123 | ev_count++; |
| 124 | ev_dev_count++; |
| 125 | if (ev_dev_count == MAX_DEVICES) break; |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 126 | ======= |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 127 | epoll_event ev; |
| 128 | ev.events = EPOLLIN | EPOLLWAKEUP; |
| 129 | ev.data.ptr = &ev_fdinfo[g_ev_count]; |
| 130 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 131 | epoll_ctl_failed = true; |
| 132 | continue; |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 133 | >>>>>>> android-10.0.0_r25 |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 136 | ev_fdinfo[g_ev_count].fd.reset(fd.release()); |
| 137 | ev_fdinfo[g_ev_count].cb = input_cb; |
| 138 | g_ev_count++; |
| 139 | g_ev_dev_count++; |
| 140 | if (g_ev_dev_count == MAX_DEVICES) break; |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 141 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 142 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 143 | if (epoll_ctl_failed && !g_ev_count) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 144 | return -1; |
| 145 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 146 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 147 | g_epoll_fd.reset(epoll_fd.release()); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 148 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int ev_get_epollfd(void) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 152 | return g_epoll_fd.get(); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 153 | } |
| 154 | |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 155 | <<<<<<< HEAD |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 156 | #ifdef TW_USE_MINUI_WITH_DATA |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 157 | int ev_add_fd(int fd, ev_callback cb, void* data) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 158 | #else |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 159 | int ev_add_fd(int fd, ev_callback cb) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 160 | #endif |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 161 | if (ev_misc_count == MAX_MISC_FDS || cb == NULL) { |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 162 | ======= |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 163 | int ev_add_fd(android::base::unique_fd&& fd, ev_callback cb) { |
| 164 | if (g_ev_misc_count == MAX_MISC_FDS || cb == nullptr) { |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 165 | >>>>>>> android-10.0.0_r25 |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 166 | return -1; |
| 167 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 168 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 169 | epoll_event ev; |
| 170 | ev.events = EPOLLIN | EPOLLWAKEUP; |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 171 | ev.data.ptr = static_cast<void*>(&ev_fdinfo[g_ev_count]); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 172 | int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 173 | if (!ret) { |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 174 | <<<<<<< HEAD |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 175 | ev_fdinfo[ev_count].fd = fd; |
| 176 | ev_fdinfo[ev_count].cb = std::move(cb); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 177 | #ifdef TW_USE_MINUI_WITH_DATA |
| 178 | ev_fdinfo[ev_count].data = data; |
| 179 | #endif |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 180 | ev_count++; |
| 181 | ev_misc_count++; |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 182 | ======= |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 183 | ev_fdinfo[g_ev_count].fd.reset(fd.release()); |
| 184 | ev_fdinfo[g_ev_count].cb = std::move(cb); |
| 185 | g_ev_count++; |
| 186 | g_ev_misc_count++; |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 187 | >>>>>>> android-10.0.0_r25 |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 188 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 189 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 190 | return ret; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void ev_exit(void) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 194 | while (g_ev_count > 0) { |
| 195 | ev_fdinfo[--g_ev_count].fd.reset(); |
| 196 | } |
| 197 | g_ev_misc_count = 0; |
| 198 | g_ev_dev_count = 0; |
| 199 | g_epoll_fd.reset(); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int ev_wait(int timeout) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 203 | g_polled_events_count = epoll_wait(g_epoll_fd, g_polled_events, g_ev_count, timeout); |
| 204 | if (g_polled_events_count <= 0) { |
| 205 | return -1; |
| 206 | } |
| 207 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void ev_dispatch(void) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 211 | for (int n = 0; n < g_polled_events_count; n++) { |
| 212 | FdInfo* fdi = static_cast<FdInfo*>(g_polled_events[n].data.ptr); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 213 | const ev_callback& cb = fdi->cb; |
| 214 | if (cb) { |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 215 | <<<<<<< HEAD |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 216 | #ifdef TW_USE_MINUI_WITH_DATA |
| 217 | cb(fdi->fd, polledevents[n].events, fdi->data); |
| 218 | #else |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 219 | cb(fdi->fd, polledevents[n].events); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 220 | #endif |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 221 | ======= |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 222 | cb(fdi->fd, g_polled_events[n].events); |
bigbiff | 26d5d5f | 2020-03-23 09:56:16 -0400 | [diff] [blame^] | 223 | >>>>>>> android-10.0.0_r25 |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 224 | } |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 225 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 228 | int ev_get_input(int fd, uint32_t epevents, input_event* ev) { |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 229 | if (epevents & EPOLLIN) { |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 230 | ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev))); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 231 | if (r == sizeof(*ev)) { |
| 232 | return 0; |
| 233 | } |
| 234 | } |
| 235 | return -1; |
| 236 | } |
| 237 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 238 | #ifdef TW_USE_MINUI_WITH_DATA |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 239 | int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 240 | #else |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 241 | int ev_sync_key_state(const ev_set_key_callback& set_key_cb) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 242 | #endif |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 243 | // Use unsigned long to match ioctl's parameter type. |
| 244 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 245 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 246 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 247 | for (size_t i = 0; i < g_ev_dev_count; ++i) { |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 248 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 249 | memset(key_bits, 0, sizeof(key_bits)); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 250 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 251 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 252 | continue; |
| 253 | } |
| 254 | if (!test_bit(EV_KEY, ev_bits)) { |
| 255 | continue; |
| 256 | } |
| 257 | if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) { |
| 258 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 261 | for (int code = 0; code <= KEY_MAX; code++) { |
| 262 | if (test_bit(code, key_bits)) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 263 | #ifdef TW_USE_MINUI_WITH_DATA |
| 264 | set_key_cb(code, 1, data); |
| 265 | #else |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 266 | set_key_cb(code, 1); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 267 | #endif |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return 0; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 275 | void ev_iterate_available_keys(const std::function<void(int)>& f) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 276 | // Use unsigned long to match ioctl's parameter type. |
| 277 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; // NOLINT |
| 278 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; // NOLINT |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 279 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 280 | for (size_t i = 0; i < g_ev_dev_count; ++i) { |
| 281 | memset(ev_bits, 0, sizeof(ev_bits)); |
| 282 | memset(key_bits, 0, sizeof(key_bits)); |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 283 | |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 284 | // Does this device even have keys? |
| 285 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 286 | continue; |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 287 | } |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 288 | if (!test_bit(EV_KEY, ev_bits)) { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits) == -1) { |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 297 | if (test_bit(key_code, key_bits)) { |
| 298 | f(key_code); |
| 299 | } |
| 300 | } |
| 301 | } |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 302 | } |
Tao Bao | af9f8b4 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 303 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 304 | #ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS |
Tao Bao | af9f8b4 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 305 | void ev_iterate_touch_inputs(const std::function<void(int)>& action) { |
Tao Bao | 835bf09 | 2019-03-11 14:13:21 -0700 | [diff] [blame] | 306 | for (size_t i = 0; i < g_ev_dev_count; ++i) { |
Tao Bao | af9f8b4 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 307 | // Use unsigned long to match ioctl's parameter type. |
| 308 | unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)] = {}; // NOLINT |
| 309 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { |
| 310 | continue; |
| 311 | } |
| 312 | if (!test_bit(EV_ABS, ev_bits)) { |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)] = {}; // NOLINT |
| 317 | if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_ABS, KEY_MAX), key_bits) == -1) { |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { |
| 322 | if (test_bit(key_code, key_bits)) { |
| 323 | action(key_code); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 328 | #endif |