Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | |
Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <dirent.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <memory.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Siarhei Vishniakou | 7a522bf | 2019-09-24 12:46:29 +0100 | [diff] [blame] | 27 | #include <sys/capability.h> |
Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 28 | #include <sys/epoll.h> |
Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 29 | #include <sys/inotify.h> |
| 30 | #include <sys/ioctl.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 31 | #include <sys/limits.h> |
Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | #define LOG_TAG "EventHub" |
| 35 | |
| 36 | // #define LOG_NDEBUG 0 |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 39 | #include <input/KeyCharacterMap.h> |
| 40 | #include <input/KeyLayoutMap.h> |
| 41 | #include <input/VirtualKeyMap.h> |
Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 42 | #include <openssl/sha.h> |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 43 | #include <utils/Errors.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | #include <utils/Log.h> |
| 45 | #include <utils/Timers.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 46 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 47 | #include <filesystem> |
| 48 | |
| 49 | #include "EventHub.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 | #define INDENT " " |
| 52 | #define INDENT2 " " |
| 53 | #define INDENT3 " " |
| 54 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 55 | using android::base::StringPrintf; |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 56 | using namespace android::flag_operators; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 57 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | namespace android { |
| 59 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 60 | static const char* DEVICE_PATH = "/dev/input"; |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 61 | // v4l2 devices go directly into /dev |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 62 | static const char* VIDEO_DEVICE_PATH = "/dev"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 63 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 64 | static constexpr int32_t FF_STRONG_MAGNITUDE_CHANNEL_IDX = 0; |
| 65 | static constexpr int32_t FF_WEAK_MAGNITUDE_CHANNEL_IDX = 1; |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 66 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 67 | static inline const char* toString(bool value) { |
| 68 | return value ? "true" : "false"; |
| 69 | } |
| 70 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 71 | static std::string sha1(const std::string& in) { |
Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 72 | SHA_CTX ctx; |
| 73 | SHA1_Init(&ctx); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 74 | SHA1_Update(&ctx, reinterpret_cast<const u_char*>(in.c_str()), in.size()); |
Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 75 | u_char digest[SHA_DIGEST_LENGTH]; |
| 76 | SHA1_Final(digest, &ctx); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 78 | std::string out; |
Dan Albert | 677d87e | 2014-06-16 17:31:28 -0700 | [diff] [blame] | 79 | for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 80 | out += StringPrintf("%02x", digest[i]); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 81 | } |
| 82 | return out; |
| 83 | } |
| 84 | |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Return true if name matches "v4l-touch*" |
| 87 | */ |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 88 | static bool isV4lTouchNode(std::string name) { |
| 89 | return name.find("v4l-touch") != std::string::npos; |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Returns true if V4L devices should be scanned. |
| 94 | * |
| 95 | * The system property ro.input.video_enabled can be used to control whether |
| 96 | * EventHub scans and opens V4L devices. As V4L does not support multiple |
| 97 | * clients, EventHub effectively blocks access to these devices when it opens |
Siarhei Vishniakou | 29f8849 | 2019-04-05 14:11:43 -0700 | [diff] [blame] | 98 | * them. |
| 99 | * |
| 100 | * Setting this to "false" would prevent any video devices from being discovered and |
| 101 | * associated with input devices. |
| 102 | * |
| 103 | * This property can be used as follows: |
| 104 | * 1. To turn off features that are dependent on video device presence. |
| 105 | * 2. During testing and development, to allow other clients to read video devices |
| 106 | * directly from /dev. |
Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 107 | */ |
| 108 | static bool isV4lScanningEnabled() { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 109 | return property_get_bool("ro.input.video_enabled", true /* default_value */); |
Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Siarhei Vishniakou | 592bac2 | 2018-11-08 19:42:38 -0800 | [diff] [blame] | 112 | static nsecs_t processEventTimestamp(const struct input_event& event) { |
| 113 | // Use the time specified in the event instead of the current time |
| 114 | // so that downstream code can get more accurate estimates of |
| 115 | // event dispatch latency from the time the event is enqueued onto |
| 116 | // the evdev client buffer. |
| 117 | // |
| 118 | // The event's timestamp fortuitously uses the same monotonic clock |
| 119 | // time base as the rest of Android. The kernel event device driver |
| 120 | // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts(). |
| 121 | // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere |
| 122 | // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a |
| 123 | // system call that also queries ktime_get_ts(). |
| 124 | |
| 125 | const nsecs_t inputEventTime = seconds_to_nanoseconds(event.time.tv_sec) + |
| 126 | microseconds_to_nanoseconds(event.time.tv_usec); |
| 127 | return inputEventTime; |
| 128 | } |
| 129 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 130 | // --- Global Functions --- |
| 131 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 132 | Flags<InputDeviceClass> getAbsAxisUsage(int32_t axis, Flags<InputDeviceClass> deviceClasses) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 | // Touch devices get dibs on touch-related axes. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 134 | if (deviceClasses.test(InputDeviceClass::TOUCH)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 135 | switch (axis) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 136 | case ABS_X: |
| 137 | case ABS_Y: |
| 138 | case ABS_PRESSURE: |
| 139 | case ABS_TOOL_WIDTH: |
| 140 | case ABS_DISTANCE: |
| 141 | case ABS_TILT_X: |
| 142 | case ABS_TILT_Y: |
| 143 | case ABS_MT_SLOT: |
| 144 | case ABS_MT_TOUCH_MAJOR: |
| 145 | case ABS_MT_TOUCH_MINOR: |
| 146 | case ABS_MT_WIDTH_MAJOR: |
| 147 | case ABS_MT_WIDTH_MINOR: |
| 148 | case ABS_MT_ORIENTATION: |
| 149 | case ABS_MT_POSITION_X: |
| 150 | case ABS_MT_POSITION_Y: |
| 151 | case ABS_MT_TOOL_TYPE: |
| 152 | case ABS_MT_BLOB_ID: |
| 153 | case ABS_MT_TRACKING_ID: |
| 154 | case ABS_MT_PRESSURE: |
| 155 | case ABS_MT_DISTANCE: |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 156 | return InputDeviceClass::TOUCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 160 | if (deviceClasses.test(InputDeviceClass::SENSOR)) { |
| 161 | switch (axis) { |
| 162 | case ABS_X: |
| 163 | case ABS_Y: |
| 164 | case ABS_Z: |
| 165 | case ABS_RX: |
| 166 | case ABS_RY: |
| 167 | case ABS_RZ: |
| 168 | return InputDeviceClass::SENSOR; |
| 169 | } |
| 170 | } |
| 171 | |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 172 | // External stylus gets the pressure axis |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 173 | if (deviceClasses.test(InputDeviceClass::EXTERNAL_STYLUS)) { |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 174 | if (axis == ABS_PRESSURE) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 175 | return InputDeviceClass::EXTERNAL_STYLUS; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 179 | // Joystick devices get the rest. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 180 | return deviceClasses & InputDeviceClass::JOYSTICK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // --- EventHub::Device --- |
| 184 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 185 | EventHub::Device::Device(int fd, int32_t id, const std::string& path, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 186 | const InputDeviceIdentifier& identifier) |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 187 | : fd(fd), |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 188 | id(id), |
| 189 | path(path), |
| 190 | identifier(identifier), |
| 191 | classes(0), |
| 192 | configuration(nullptr), |
| 193 | virtualKeyMap(nullptr), |
| 194 | ffEffectPlaying(false), |
| 195 | ffEffectId(-1), |
| 196 | controllerNumber(0), |
| 197 | enabled(true), |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 198 | isVirtual(fd < 0) {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 199 | |
| 200 | EventHub::Device::~Device() { |
| 201 | close(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void EventHub::Device::close() { |
| 205 | if (fd >= 0) { |
| 206 | ::close(fd); |
| 207 | fd = -1; |
| 208 | } |
| 209 | } |
| 210 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 211 | status_t EventHub::Device::enable() { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 212 | fd = open(path.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 213 | if (fd < 0) { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 214 | ALOGE("could not open %s, %s\n", path.c_str(), strerror(errno)); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 215 | return -errno; |
| 216 | } |
| 217 | enabled = true; |
| 218 | return OK; |
| 219 | } |
| 220 | |
| 221 | status_t EventHub::Device::disable() { |
| 222 | close(); |
| 223 | enabled = false; |
| 224 | return OK; |
| 225 | } |
| 226 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 227 | bool EventHub::Device::hasValidFd() const { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 228 | return !isVirtual && enabled; |
| 229 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 230 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 231 | const std::shared_ptr<KeyCharacterMap> EventHub::Device::getKeyCharacterMap() const { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 232 | return keyMap.keyCharacterMap; |
| 233 | } |
| 234 | |
| 235 | template <std::size_t N> |
| 236 | status_t EventHub::Device::readDeviceBitMask(unsigned long ioctlCode, BitArray<N>& bitArray) { |
| 237 | if (!hasValidFd()) { |
| 238 | return BAD_VALUE; |
| 239 | } |
| 240 | if ((_IOC_SIZE(ioctlCode) == 0)) { |
| 241 | ioctlCode |= _IOC(0, 0, 0, bitArray.bytes()); |
| 242 | } |
| 243 | |
| 244 | typename BitArray<N>::Buffer buffer; |
| 245 | status_t ret = ioctl(fd, ioctlCode, buffer.data()); |
| 246 | bitArray.loadFromBuffer(buffer); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | void EventHub::Device::configureFd() { |
| 251 | // Set fd parameters with ioctl, such as key repeat, suspend block, and clock type |
| 252 | if (classes.test(InputDeviceClass::KEYBOARD)) { |
| 253 | // Disable kernel key repeat since we handle it ourselves |
| 254 | unsigned int repeatRate[] = {0, 0}; |
| 255 | if (ioctl(fd, EVIOCSREP, repeatRate)) { |
| 256 | ALOGW("Unable to disable kernel key repeat for %s: %s", path.c_str(), strerror(errno)); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Tell the kernel that we want to use the monotonic clock for reporting timestamps |
| 261 | // associated with input events. This is important because the input system |
| 262 | // uses the timestamps extensively and assumes they were recorded using the monotonic |
| 263 | // clock. |
| 264 | int clockId = CLOCK_MONOTONIC; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 265 | if (classes.test(InputDeviceClass::SENSOR)) { |
| 266 | // Each new sensor event should use the same time base as |
| 267 | // SystemClock.elapsedRealtimeNanos(). |
| 268 | clockId = CLOCK_BOOTTIME; |
| 269 | } |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 270 | bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId); |
| 271 | ALOGI("usingClockIoctl=%s", toString(usingClockIoctl)); |
| 272 | } |
| 273 | |
| 274 | bool EventHub::Device::hasKeycodeLocked(int keycode) const { |
| 275 | if (!keyMap.haveKeyLayout()) { |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | std::vector<int32_t> scanCodes; |
| 280 | keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes); |
| 281 | const size_t N = scanCodes.size(); |
| 282 | for (size_t i = 0; i < N && i <= KEY_MAX; i++) { |
| 283 | int32_t sc = scanCodes[i]; |
| 284 | if (sc >= 0 && sc <= KEY_MAX && keyBitmask.test(sc)) { |
| 285 | return true; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | void EventHub::Device::loadConfigurationLocked() { |
| 293 | configurationFile = |
| 294 | getInputDeviceConfigurationFilePathByDeviceIdentifier(identifier, |
| 295 | InputDeviceConfigurationFileType:: |
| 296 | CONFIGURATION); |
| 297 | if (configurationFile.empty()) { |
| 298 | ALOGD("No input device configuration file found for device '%s'.", identifier.name.c_str()); |
| 299 | } else { |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 300 | android::base::Result<std::unique_ptr<PropertyMap>> propertyMap = |
| 301 | PropertyMap::load(configurationFile.c_str()); |
| 302 | if (!propertyMap.ok()) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 303 | ALOGE("Error loading input device configuration file for device '%s'. " |
| 304 | "Using default configuration.", |
| 305 | identifier.name.c_str()); |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 306 | } else { |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 307 | configuration = std::move(*propertyMap); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | bool EventHub::Device::loadVirtualKeyMapLocked() { |
| 313 | // The virtual key map is supplied by the kernel as a system board property file. |
| 314 | std::string propPath = "/sys/board_properties/virtualkeys."; |
| 315 | propPath += identifier.getCanonicalName(); |
| 316 | if (access(propPath.c_str(), R_OK)) { |
| 317 | return false; |
| 318 | } |
| 319 | virtualKeyMap = VirtualKeyMap::load(propPath); |
| 320 | return virtualKeyMap != nullptr; |
| 321 | } |
| 322 | |
| 323 | status_t EventHub::Device::loadKeyMapLocked() { |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 324 | return keyMap.load(identifier, configuration.get()); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | bool EventHub::Device::isExternalDeviceLocked() { |
| 328 | if (configuration) { |
| 329 | bool value; |
| 330 | if (configuration->tryGetProperty(String8("device.internal"), value)) { |
| 331 | return !value; |
| 332 | } |
| 333 | } |
| 334 | return identifier.bus == BUS_USB || identifier.bus == BUS_BLUETOOTH; |
| 335 | } |
| 336 | |
| 337 | bool EventHub::Device::deviceHasMicLocked() { |
| 338 | if (configuration) { |
| 339 | bool value; |
| 340 | if (configuration->tryGetProperty(String8("audio.mic"), value)) { |
| 341 | return value; |
| 342 | } |
| 343 | } |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | void EventHub::Device::setLedStateLocked(int32_t led, bool on) { |
| 348 | int32_t sc; |
| 349 | if (hasValidFd() && mapLed(led, &sc) != NAME_NOT_FOUND) { |
| 350 | struct input_event ev; |
| 351 | ev.time.tv_sec = 0; |
| 352 | ev.time.tv_usec = 0; |
| 353 | ev.type = EV_LED; |
| 354 | ev.code = sc; |
| 355 | ev.value = on ? 1 : 0; |
| 356 | |
| 357 | ssize_t nWrite; |
| 358 | do { |
| 359 | nWrite = write(fd, &ev, sizeof(struct input_event)); |
| 360 | } while (nWrite == -1 && errno == EINTR); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void EventHub::Device::setLedForControllerLocked() { |
| 365 | for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) { |
| 366 | setLedStateLocked(ALED_CONTROLLER_1 + i, controllerNumber == i + 1); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | status_t EventHub::Device::mapLed(int32_t led, int32_t* outScanCode) const { |
| 371 | if (!keyMap.haveKeyLayout()) { |
| 372 | return NAME_NOT_FOUND; |
| 373 | } |
| 374 | |
| 375 | int32_t scanCode; |
| 376 | if (keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) { |
| 377 | if (scanCode >= 0 && scanCode <= LED_MAX && ledBitmask.test(scanCode)) { |
| 378 | *outScanCode = scanCode; |
| 379 | return NO_ERROR; |
| 380 | } |
| 381 | } |
| 382 | return NAME_NOT_FOUND; |
| 383 | } |
| 384 | |
Siarhei Vishniakou | 7a522bf | 2019-09-24 12:46:29 +0100 | [diff] [blame] | 385 | /** |
| 386 | * Get the capabilities for the current process. |
| 387 | * Crashes the system if unable to create / check / destroy the capabilities object. |
| 388 | */ |
| 389 | class Capabilities final { |
| 390 | public: |
| 391 | explicit Capabilities() { |
| 392 | mCaps = cap_get_proc(); |
| 393 | LOG_ALWAYS_FATAL_IF(mCaps == nullptr, "Could not get capabilities of the current process"); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Check whether the current process has a specific capability |
| 398 | * in the set of effective capabilities. |
| 399 | * Return CAP_SET if the process has the requested capability |
| 400 | * Return CAP_CLEAR otherwise. |
| 401 | */ |
| 402 | cap_flag_value_t checkEffectiveCapability(cap_value_t capability) { |
| 403 | cap_flag_value_t value; |
| 404 | const int result = cap_get_flag(mCaps, capability, CAP_EFFECTIVE, &value); |
| 405 | LOG_ALWAYS_FATAL_IF(result == -1, "Could not obtain the requested capability"); |
| 406 | return value; |
| 407 | } |
| 408 | |
| 409 | ~Capabilities() { |
| 410 | const int result = cap_free(mCaps); |
| 411 | LOG_ALWAYS_FATAL_IF(result == -1, "Could not release the capabilities structure"); |
| 412 | } |
| 413 | |
| 414 | private: |
| 415 | cap_t mCaps; |
| 416 | }; |
| 417 | |
| 418 | static void ensureProcessCanBlockSuspend() { |
| 419 | Capabilities capabilities; |
| 420 | const bool canBlockSuspend = |
| 421 | capabilities.checkEffectiveCapability(CAP_BLOCK_SUSPEND) == CAP_SET; |
| 422 | LOG_ALWAYS_FATAL_IF(!canBlockSuspend, |
| 423 | "Input must be able to block suspend to properly process events"); |
| 424 | } |
| 425 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 426 | // --- EventHub --- |
| 427 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 428 | const int EventHub::EPOLL_MAX_EVENTS; |
| 429 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 430 | EventHub::EventHub(void) |
| 431 | : mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), |
| 432 | mNextDeviceId(1), |
| 433 | mControllerNumbers(), |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 434 | mNeedToSendFinishedDeviceScan(false), |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 435 | mNeedToReopenDevices(false), |
| 436 | mNeedToScanDevices(true), |
| 437 | mPendingEventCount(0), |
| 438 | mPendingEventIndex(0), |
| 439 | mPendingINotify(false) { |
Siarhei Vishniakou | 7a522bf | 2019-09-24 12:46:29 +0100 | [diff] [blame] | 440 | ensureProcessCanBlockSuspend(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 441 | |
Nick Kralevich | fcf1b2b | 2018-12-15 11:59:30 -0800 | [diff] [blame] | 442 | mEpollFd = epoll_create1(EPOLL_CLOEXEC); |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 443 | LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance: %s", strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 444 | |
| 445 | mINotifyFd = inotify_init(); |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 446 | mInputWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 447 | LOG_ALWAYS_FATAL_IF(mInputWd < 0, "Could not register INotify for %s: %s", DEVICE_PATH, |
| 448 | strerror(errno)); |
Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 449 | if (isV4lScanningEnabled()) { |
| 450 | mVideoWd = inotify_add_watch(mINotifyFd, VIDEO_DEVICE_PATH, IN_DELETE | IN_CREATE); |
| 451 | LOG_ALWAYS_FATAL_IF(mVideoWd < 0, "Could not register INotify for %s: %s", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 452 | VIDEO_DEVICE_PATH, strerror(errno)); |
Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 453 | } else { |
| 454 | mVideoWd = -1; |
| 455 | ALOGI("Video device scanning disabled"); |
| 456 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 457 | |
Siarhei Vishniakou | 2d0e948 | 2019-09-24 12:52:47 +0100 | [diff] [blame] | 458 | struct epoll_event eventItem = {}; |
| 459 | eventItem.events = EPOLLIN | EPOLLWAKEUP; |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 460 | eventItem.data.fd = mINotifyFd; |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 461 | int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 462 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno); |
| 463 | |
| 464 | int wakeFds[2]; |
| 465 | result = pipe(wakeFds); |
| 466 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno); |
| 467 | |
| 468 | mWakeReadPipeFd = wakeFds[0]; |
| 469 | mWakeWritePipeFd = wakeFds[1]; |
| 470 | |
| 471 | result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK); |
| 472 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 473 | errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 474 | |
| 475 | result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK); |
| 476 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 477 | errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 478 | |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 479 | eventItem.data.fd = mWakeReadPipeFd; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 480 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem); |
| 481 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 482 | errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | EventHub::~EventHub(void) { |
| 486 | closeAllDevicesLocked(); |
| 487 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 488 | ::close(mEpollFd); |
| 489 | ::close(mINotifyFd); |
| 490 | ::close(mWakeReadPipeFd); |
| 491 | ::close(mWakeWritePipeFd); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 495 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 496 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 497 | return device != nullptr ? device->identifier : InputDeviceIdentifier(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 500 | Flags<InputDeviceClass> EventHub::getDeviceClasses(int32_t deviceId) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 501 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 502 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 503 | return device != nullptr ? device->classes : Flags<InputDeviceClass>(0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 507 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 508 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 509 | return device != nullptr ? device->controllerNumber : 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 513 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 514 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 515 | if (device != nullptr && device->configuration) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 516 | *outConfiguration = *device->configuration; |
| 517 | } else { |
| 518 | outConfiguration->clear(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 523 | RawAbsoluteAxisInfo* outAxisInfo) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 524 | outAxisInfo->clear(); |
| 525 | |
| 526 | if (axis >= 0 && axis <= ABS_MAX) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 527 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 528 | |
| 529 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 530 | if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | struct input_absinfo info; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 532 | if (ioctl(device->fd, EVIOCGABS(axis), &info)) { |
| 533 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis, |
| 534 | device->identifier.name.c_str(), device->fd, errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 535 | return -errno; |
| 536 | } |
| 537 | |
| 538 | if (info.minimum != info.maximum) { |
| 539 | outAxisInfo->valid = true; |
| 540 | outAxisInfo->minValue = info.minimum; |
| 541 | outAxisInfo->maxValue = info.maximum; |
| 542 | outAxisInfo->flat = info.flat; |
| 543 | outAxisInfo->fuzz = info.fuzz; |
| 544 | outAxisInfo->resolution = info.resolution; |
| 545 | } |
| 546 | return OK; |
| 547 | } |
| 548 | } |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const { |
| 553 | if (axis >= 0 && axis <= REL_MAX) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 554 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 555 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 556 | return device != nullptr ? device->relBitmask.test(axis) : false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | } |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | bool EventHub::hasInputProperty(int32_t deviceId, int property) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 562 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 564 | Device* device = getDeviceLocked(deviceId); |
| 565 | return property >= 0 && property <= INPUT_PROP_MAX && device != nullptr |
| 566 | ? device->propBitmask.test(property) |
| 567 | : false; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 570 | bool EventHub::hasMscEvent(int32_t deviceId, int mscEvent) const { |
| 571 | std::scoped_lock _l(mLock); |
| 572 | |
| 573 | Device* device = getDeviceLocked(deviceId); |
| 574 | return mscEvent >= 0 && mscEvent <= MSC_MAX && device != nullptr |
| 575 | ? device->mscBitmask.test(mscEvent) |
| 576 | : false; |
| 577 | } |
| 578 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 579 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
| 580 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 581 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | |
| 583 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 584 | if (device != nullptr && device->hasValidFd() && device->keyBitmask.test(scanCode)) { |
| 585 | if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) { |
| 586 | return device->keyState.test(scanCode) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | } |
| 590 | return AKEY_STATE_UNKNOWN; |
| 591 | } |
| 592 | |
| 593 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 594 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 595 | |
| 596 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 597 | if (device != nullptr && device->hasValidFd() && device->keyMap.haveKeyLayout()) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 598 | std::vector<int32_t> scanCodes; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 599 | device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes); |
| 600 | if (scanCodes.size() != 0) { |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 601 | if (device->readDeviceBitMask(EVIOCGKEY(0), device->keyState) >= 0) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | for (size_t i = 0; i < scanCodes.size(); i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 603 | int32_t sc = scanCodes[i]; |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 604 | if (sc >= 0 && sc <= KEY_MAX && device->keyState.test(sc)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 | return AKEY_STATE_DOWN; |
| 606 | } |
| 607 | } |
| 608 | return AKEY_STATE_UP; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | return AKEY_STATE_UNKNOWN; |
| 613 | } |
| 614 | |
| 615 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
| 616 | if (sw >= 0 && sw <= SW_MAX) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 617 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 618 | |
| 619 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 620 | if (device != nullptr && device->hasValidFd() && device->swBitmask.test(sw)) { |
| 621 | if (device->readDeviceBitMask(EVIOCGSW(0), device->swState) >= 0) { |
| 622 | return device->swState.test(sw) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | } |
| 626 | return AKEY_STATE_UNKNOWN; |
| 627 | } |
| 628 | |
| 629 | status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const { |
| 630 | *outValue = 0; |
| 631 | |
| 632 | if (axis >= 0 && axis <= ABS_MAX) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 633 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | |
| 635 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 636 | if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 637 | struct input_absinfo info; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 638 | if (ioctl(device->fd, EVIOCGABS(axis), &info)) { |
| 639 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", axis, |
| 640 | device->identifier.name.c_str(), device->fd, errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 641 | return -errno; |
| 642 | } |
| 643 | |
| 644 | *outValue = info.value; |
| 645 | return OK; |
| 646 | } |
| 647 | } |
| 648 | return -1; |
| 649 | } |
| 650 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 651 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, |
| 652 | uint8_t* outFlags) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 653 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 654 | |
| 655 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 656 | if (device != nullptr && device->keyMap.haveKeyLayout()) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 657 | std::vector<int32_t> scanCodes; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 658 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
| 659 | scanCodes.clear(); |
| 660 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 661 | status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(keyCodes[codeIndex], |
| 662 | &scanCodes); |
| 663 | if (!err) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 664 | // check the possible scan codes identified by the layout map against the |
| 665 | // map of codes actually emitted by the driver |
| 666 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 667 | if (device->keyBitmask.test(scanCodes[sc])) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 668 | outFlags[codeIndex] = 1; |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | return true; |
| 675 | } |
| 676 | return false; |
| 677 | } |
| 678 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 679 | status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 680 | int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 681 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 682 | Device* device = getDeviceLocked(deviceId); |
Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 683 | status_t status = NAME_NOT_FOUND; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 684 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 685 | if (device != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | // Check the key character map first. |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 687 | const std::shared_ptr<KeyCharacterMap> kcm = device->getKeyCharacterMap(); |
| 688 | if (kcm) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | if (!kcm->mapKey(scanCode, usageCode, outKeycode)) { |
| 690 | *outFlags = 0; |
Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 691 | status = NO_ERROR; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | // Check the key layout next. |
Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 696 | if (status != NO_ERROR && device->keyMap.haveKeyLayout()) { |
Siarhei Vishniakou | 592bac2 | 2018-11-08 19:42:38 -0800 | [diff] [blame] | 697 | if (!device->keyMap.keyLayoutMap->mapKey(scanCode, usageCode, outKeycode, outFlags)) { |
Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 698 | status = NO_ERROR; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (status == NO_ERROR) { |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 703 | if (kcm) { |
Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 704 | kcm->tryRemapKey(*outKeycode, metaState, outKeycode, outMetaState); |
| 705 | } else { |
| 706 | *outMetaState = metaState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
Dmitry Torokhov | 0faaa0b | 2015-09-24 13:13:55 -0700 | [diff] [blame] | 711 | if (status != NO_ERROR) { |
| 712 | *outKeycode = 0; |
| 713 | *outFlags = 0; |
| 714 | *outMetaState = metaState; |
| 715 | } |
| 716 | |
| 717 | return status; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 721 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 722 | Device* device = getDeviceLocked(deviceId); |
| 723 | |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 724 | if (device != nullptr && device->keyMap.haveKeyLayout()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 725 | status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo); |
| 726 | if (err == NO_ERROR) { |
| 727 | return NO_ERROR; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | return NAME_NOT_FOUND; |
| 732 | } |
| 733 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 734 | base::Result<std::pair<InputDeviceSensorType, int32_t>> EventHub::mapSensor(int32_t deviceId, |
| 735 | int32_t absCode) { |
| 736 | std::scoped_lock _l(mLock); |
| 737 | Device* device = getDeviceLocked(deviceId); |
| 738 | |
| 739 | if (device != nullptr && device->keyMap.haveKeyLayout()) { |
| 740 | return device->keyMap.keyLayoutMap->mapSensor(absCode); |
| 741 | } |
| 742 | return Errorf("Device not found or device has no key layout."); |
| 743 | } |
| 744 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 745 | void EventHub::setExcludedDevices(const std::vector<std::string>& devices) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 746 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | |
| 748 | mExcludedDevices = devices; |
| 749 | } |
| 750 | |
| 751 | bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 752 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 754 | if (device != nullptr && scanCode >= 0 && scanCode <= KEY_MAX) { |
| 755 | return device->keyBitmask.test(scanCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 756 | } |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 761 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 762 | Device* device = getDeviceLocked(deviceId); |
| 763 | int32_t sc; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 764 | if (device != nullptr && device->mapLed(led, &sc) == NO_ERROR) { |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 765 | return device->ledBitmask.test(sc); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 766 | } |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 771 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 772 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 773 | if (device != nullptr && device->hasValidFd()) { |
| 774 | device->setLedStateLocked(led, on); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 779 | std::vector<VirtualKeyDefinition>& outVirtualKeys) const { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 780 | outVirtualKeys.clear(); |
| 781 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 782 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 783 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 784 | if (device != nullptr && device->virtualKeyMap) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 785 | const std::vector<VirtualKeyDefinition> virtualKeys = |
| 786 | device->virtualKeyMap->getVirtualKeys(); |
| 787 | outVirtualKeys.insert(outVirtualKeys.end(), virtualKeys.begin(), virtualKeys.end()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 791 | const std::shared_ptr<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 792 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 793 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 794 | if (device != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | return device->getKeyCharacterMap(); |
| 796 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 797 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 800 | bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 801 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 803 | if (device != nullptr && map != nullptr && device->keyMap.keyCharacterMap != nullptr) { |
| 804 | device->keyMap.keyCharacterMap->combine(*map); |
| 805 | device->keyMap.keyCharacterMapFile = device->keyMap.keyCharacterMap->getLoadFileName(); |
| 806 | return true; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 807 | } |
| 808 | return false; |
| 809 | } |
| 810 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 811 | static std::string generateDescriptor(InputDeviceIdentifier& identifier) { |
| 812 | std::string rawDescriptor; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 813 | rawDescriptor += StringPrintf(":%04x:%04x:", identifier.vendor, identifier.product); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 814 | // TODO add handling for USB devices to not uniqueify kbs that show up twice |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 815 | if (!identifier.uniqueId.empty()) { |
| 816 | rawDescriptor += "uniqueId:"; |
| 817 | rawDescriptor += identifier.uniqueId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 818 | } else if (identifier.nonce != 0) { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 819 | rawDescriptor += StringPrintf("nonce:%04x", identifier.nonce); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | if (identifier.vendor == 0 && identifier.product == 0) { |
| 823 | // If we don't know the vendor and product id, then the device is probably |
| 824 | // built-in so we need to rely on other information to uniquely identify |
| 825 | // the input device. Usually we try to avoid relying on the device name or |
| 826 | // location but for built-in input device, they are unlikely to ever change. |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 827 | if (!identifier.name.empty()) { |
| 828 | rawDescriptor += "name:"; |
| 829 | rawDescriptor += identifier.name; |
| 830 | } else if (!identifier.location.empty()) { |
| 831 | rawDescriptor += "location:"; |
| 832 | rawDescriptor += identifier.location; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | identifier.descriptor = sha1(rawDescriptor); |
| 836 | return rawDescriptor; |
| 837 | } |
| 838 | |
| 839 | void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) { |
| 840 | // Compute a device descriptor that uniquely identifies the device. |
| 841 | // The descriptor is assumed to be a stable identifier. Its value should not |
| 842 | // change between reboots, reconnections, firmware updates or new releases |
| 843 | // of Android. In practice we sometimes get devices that cannot be uniquely |
| 844 | // identified. In this case we enforce uniqueness between connected devices. |
| 845 | // Ideally, we also want the descriptor to be short and relatively opaque. |
| 846 | |
| 847 | identifier.nonce = 0; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 848 | std::string rawDescriptor = generateDescriptor(identifier); |
| 849 | if (identifier.uniqueId.empty()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 850 | // If it didn't have a unique id check for conflicts and enforce |
| 851 | // uniqueness if necessary. |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 852 | while (getDeviceByDescriptorLocked(identifier.descriptor) != nullptr) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 853 | identifier.nonce++; |
| 854 | rawDescriptor = generateDescriptor(identifier); |
| 855 | } |
| 856 | } |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 857 | ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.c_str(), |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 858 | identifier.descriptor.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 859 | } |
| 860 | |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 861 | void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 862 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 863 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 864 | if (device != nullptr && device->hasValidFd()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 865 | ff_effect effect; |
| 866 | memset(&effect, 0, sizeof(effect)); |
| 867 | effect.type = FF_RUMBLE; |
| 868 | effect.id = device->ffEffectId; |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 869 | // evdev FF_RUMBLE effect only supports two channels of vibration. |
Chris Ye | 6393a26 | 2020-08-04 19:41:36 -0700 | [diff] [blame] | 870 | effect.u.rumble.strong_magnitude = element.getMagnitude(FF_STRONG_MAGNITUDE_CHANNEL_IDX); |
| 871 | effect.u.rumble.weak_magnitude = element.getMagnitude(FF_WEAK_MAGNITUDE_CHANNEL_IDX); |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 872 | effect.replay.length = element.duration.count(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 873 | effect.replay.delay = 0; |
| 874 | if (ioctl(device->fd, EVIOCSFF, &effect)) { |
| 875 | ALOGW("Could not upload force feedback effect to device %s due to error %d.", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 876 | device->identifier.name.c_str(), errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 877 | return; |
| 878 | } |
| 879 | device->ffEffectId = effect.id; |
| 880 | |
| 881 | struct input_event ev; |
| 882 | ev.time.tv_sec = 0; |
| 883 | ev.time.tv_usec = 0; |
| 884 | ev.type = EV_FF; |
| 885 | ev.code = device->ffEffectId; |
| 886 | ev.value = 1; |
| 887 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { |
| 888 | ALOGW("Could not start force feedback effect on device %s due to error %d.", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 889 | device->identifier.name.c_str(), errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 890 | return; |
| 891 | } |
| 892 | device->ffEffectPlaying = true; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | void EventHub::cancelVibrate(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 897 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 898 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 899 | if (device != nullptr && device->hasValidFd()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 900 | if (device->ffEffectPlaying) { |
| 901 | device->ffEffectPlaying = false; |
| 902 | |
| 903 | struct input_event ev; |
| 904 | ev.time.tv_sec = 0; |
| 905 | ev.time.tv_usec = 0; |
| 906 | ev.type = EV_FF; |
| 907 | ev.code = device->ffEffectId; |
| 908 | ev.value = 0; |
| 909 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { |
| 910 | ALOGW("Could not stop force feedback effect on device %s due to error %d.", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 911 | device->identifier.name.c_str(), errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 912 | return; |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 918 | std::vector<int32_t> EventHub::getVibratorIds(int32_t deviceId) { |
| 919 | std::scoped_lock _l(mLock); |
| 920 | std::vector<int32_t> vibrators; |
| 921 | Device* device = getDeviceLocked(deviceId); |
| 922 | if (device != nullptr && device->hasValidFd() && |
| 923 | device->classes.test(InputDeviceClass::VIBRATOR)) { |
| 924 | vibrators.push_back(FF_STRONG_MAGNITUDE_CHANNEL_IDX); |
| 925 | vibrators.push_back(FF_WEAK_MAGNITUDE_CHANNEL_IDX); |
| 926 | } |
| 927 | return vibrators; |
| 928 | } |
| 929 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 930 | EventHub::Device* EventHub::getDeviceByDescriptorLocked(const std::string& descriptor) const { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 931 | for (const auto& [id, device] : mDevices) { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 932 | if (descriptor == device->identifier.descriptor) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 933 | return device.get(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 934 | } |
| 935 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 936 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { |
Prabir Pradhan | cae4b3a | 2019-02-05 18:51:32 -0800 | [diff] [blame] | 940 | if (deviceId == ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 941 | deviceId = mBuiltInKeyboardId; |
| 942 | } |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 943 | const auto& it = mDevices.find(deviceId); |
| 944 | return it != mDevices.end() ? it->second.get() : nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 947 | EventHub::Device* EventHub::getDeviceByPathLocked(const std::string& devicePath) const { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 948 | for (const auto& [id, device] : mDevices) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 949 | if (device->path == devicePath) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 950 | return device.get(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 951 | } |
| 952 | } |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 953 | return nullptr; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 956 | /** |
| 957 | * The file descriptor could be either input device, or a video device (associated with a |
| 958 | * specific input device). Check both cases here, and return the device that this event |
| 959 | * belongs to. Caller can compare the fd's once more to determine event type. |
| 960 | * Looks through all input devices, and only attached video devices. Unattached video |
| 961 | * devices are ignored. |
| 962 | */ |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 963 | EventHub::Device* EventHub::getDeviceByFdLocked(int fd) const { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 964 | for (const auto& [id, device] : mDevices) { |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 965 | if (device->fd == fd) { |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 966 | // This is an input device event |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 967 | return device.get(); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 968 | } |
| 969 | if (device->videoDevice && device->videoDevice->getFd() == fd) { |
| 970 | // This is a video device event |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 971 | return device.get(); |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 974 | // We do not check mUnattachedVideoDevices here because they should not participate in epoll, |
| 975 | // and therefore should never be looked up by fd. |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 976 | return nullptr; |
| 977 | } |
| 978 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 979 | size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) { |
| 980 | ALOG_ASSERT(bufferSize >= 1); |
| 981 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 982 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 983 | |
| 984 | struct input_event readBuffer[bufferSize]; |
| 985 | |
| 986 | RawEvent* event = buffer; |
| 987 | size_t capacity = bufferSize; |
| 988 | bool awoken = false; |
| 989 | for (;;) { |
| 990 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 991 | |
| 992 | // Reopen input devices if needed. |
| 993 | if (mNeedToReopenDevices) { |
| 994 | mNeedToReopenDevices = false; |
| 995 | |
| 996 | ALOGI("Reopening all input devices due to a configuration change."); |
| 997 | |
| 998 | closeAllDevicesLocked(); |
| 999 | mNeedToScanDevices = true; |
| 1000 | break; // return to the caller before we actually rescan |
| 1001 | } |
| 1002 | |
| 1003 | // Report any devices that had last been added/removed. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1004 | for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();) { |
| 1005 | std::unique_ptr<Device> device = std::move(*it); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1006 | ALOGV("Reporting device closed: id=%d, name=%s\n", device->id, device->path.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1007 | event->when = now; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1008 | event->deviceId = (device->id == mBuiltInKeyboardId) |
| 1009 | ? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID |
| 1010 | : device->id; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | event->type = DEVICE_REMOVED; |
| 1012 | event += 1; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1013 | it = mClosingDevices.erase(it); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1014 | mNeedToSendFinishedDeviceScan = true; |
| 1015 | if (--capacity == 0) { |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | if (mNeedToScanDevices) { |
| 1021 | mNeedToScanDevices = false; |
| 1022 | scanDevicesLocked(); |
| 1023 | mNeedToSendFinishedDeviceScan = true; |
| 1024 | } |
| 1025 | |
Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 1026 | while (!mOpeningDevices.empty()) { |
| 1027 | std::unique_ptr<Device> device = std::move(*mOpeningDevices.rbegin()); |
| 1028 | mOpeningDevices.pop_back(); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1029 | ALOGV("Reporting device opened: id=%d, name=%s\n", device->id, device->path.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1030 | event->when = now; |
| 1031 | event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
| 1032 | event->type = DEVICE_ADDED; |
| 1033 | event += 1; |
Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 1034 | |
| 1035 | // Try to find a matching video device by comparing device names |
| 1036 | for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end(); |
| 1037 | it++) { |
| 1038 | std::unique_ptr<TouchVideoDevice>& videoDevice = *it; |
| 1039 | if (tryAddVideoDevice(*device, videoDevice)) { |
| 1040 | // videoDevice was transferred to 'device' |
| 1041 | it = mUnattachedVideoDevices.erase(it); |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device)); |
| 1047 | if (!inserted) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1048 | ALOGW("Device id %d exists, replaced.", device->id); |
| 1049 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1050 | mNeedToSendFinishedDeviceScan = true; |
| 1051 | if (--capacity == 0) { |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | if (mNeedToSendFinishedDeviceScan) { |
| 1057 | mNeedToSendFinishedDeviceScan = false; |
| 1058 | event->when = now; |
| 1059 | event->type = FINISHED_DEVICE_SCAN; |
| 1060 | event += 1; |
| 1061 | if (--capacity == 0) { |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // Grab the next input event. |
| 1067 | bool deviceChanged = false; |
| 1068 | while (mPendingEventIndex < mPendingEventCount) { |
| 1069 | const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++]; |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1070 | if (eventItem.data.fd == mINotifyFd) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1071 | if (eventItem.events & EPOLLIN) { |
| 1072 | mPendingINotify = true; |
| 1073 | } else { |
| 1074 | ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events); |
| 1075 | } |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1079 | if (eventItem.data.fd == mWakeReadPipeFd) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1080 | if (eventItem.events & EPOLLIN) { |
| 1081 | ALOGV("awoken after wake()"); |
| 1082 | awoken = true; |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 1083 | char wakeReadBuffer[16]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1084 | ssize_t nRead; |
| 1085 | do { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 1086 | nRead = read(mWakeReadPipeFd, wakeReadBuffer, sizeof(wakeReadBuffer)); |
| 1087 | } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(wakeReadBuffer)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1088 | } else { |
| 1089 | ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1090 | eventItem.events); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1091 | } |
| 1092 | continue; |
| 1093 | } |
| 1094 | |
Siarhei Vishniakou | 4bc561c | 2018-11-02 17:41:58 -0700 | [diff] [blame] | 1095 | Device* device = getDeviceByFdLocked(eventItem.data.fd); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1096 | if (device == nullptr) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1097 | ALOGE("Received unexpected epoll event 0x%08x for unknown fd %d.", eventItem.events, |
| 1098 | eventItem.data.fd); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1099 | ALOG_ASSERT(!DEBUG); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1100 | continue; |
| 1101 | } |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1102 | if (device->videoDevice && eventItem.data.fd == device->videoDevice->getFd()) { |
| 1103 | if (eventItem.events & EPOLLIN) { |
| 1104 | size_t numFrames = device->videoDevice->readAndQueueFrames(); |
| 1105 | if (numFrames == 0) { |
| 1106 | ALOGE("Received epoll event for video device %s, but could not read frame", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1107 | device->videoDevice->getName().c_str()); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1108 | } |
| 1109 | } else if (eventItem.events & EPOLLHUP) { |
| 1110 | // TODO(b/121395353) - consider adding EPOLLRDHUP |
| 1111 | ALOGI("Removing video device %s due to epoll hang-up event.", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1112 | device->videoDevice->getName().c_str()); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1113 | unregisterVideoDeviceFromEpollLocked(*device->videoDevice); |
| 1114 | device->videoDevice = nullptr; |
| 1115 | } else { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1116 | ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events, |
| 1117 | device->videoDevice->getName().c_str()); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1118 | ALOG_ASSERT(!DEBUG); |
| 1119 | } |
| 1120 | continue; |
| 1121 | } |
| 1122 | // This must be an input event |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1123 | if (eventItem.events & EPOLLIN) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1124 | int32_t readSize = |
| 1125 | read(device->fd, readBuffer, sizeof(struct input_event) * capacity); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1126 | if (readSize == 0 || (readSize < 0 && errno == ENODEV)) { |
| 1127 | // Device was removed before INotify noticed. |
Mark Salyzyn | 5aa26b2 | 2014-06-10 13:07:44 -0700 | [diff] [blame] | 1128 | ALOGW("could not get event, removed? (fd: %d size: %" PRId32 |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1129 | " bufferSize: %zu capacity: %zu errno: %d)\n", |
| 1130 | device->fd, readSize, bufferSize, capacity, errno); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1131 | deviceChanged = true; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1132 | closeDeviceLocked(*device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1133 | } else if (readSize < 0) { |
| 1134 | if (errno != EAGAIN && errno != EINTR) { |
| 1135 | ALOGW("could not get event (errno=%d)", errno); |
| 1136 | } |
| 1137 | } else if ((readSize % sizeof(struct input_event)) != 0) { |
| 1138 | ALOGE("could not get event (wrong size: %d)", readSize); |
| 1139 | } else { |
| 1140 | int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
| 1141 | |
| 1142 | size_t count = size_t(readSize) / sizeof(struct input_event); |
| 1143 | for (size_t i = 0; i < count; i++) { |
| 1144 | struct input_event& iev = readBuffer[i]; |
Siarhei Vishniakou | 592bac2 | 2018-11-08 19:42:38 -0800 | [diff] [blame] | 1145 | event->when = processEventTimestamp(iev); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1146 | event->deviceId = deviceId; |
| 1147 | event->type = iev.type; |
| 1148 | event->code = iev.code; |
| 1149 | event->value = iev.value; |
| 1150 | event += 1; |
| 1151 | capacity -= 1; |
| 1152 | } |
| 1153 | if (capacity == 0) { |
| 1154 | // The result buffer is full. Reset the pending event index |
| 1155 | // so we will try to read the device again on the next iteration. |
| 1156 | mPendingEventIndex -= 1; |
| 1157 | break; |
| 1158 | } |
| 1159 | } |
| 1160 | } else if (eventItem.events & EPOLLHUP) { |
| 1161 | ALOGI("Removing device %s due to epoll hang-up event.", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1162 | device->identifier.name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1163 | deviceChanged = true; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1164 | closeDeviceLocked(*device); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1165 | } else { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1166 | ALOGW("Received unexpected epoll event 0x%08x for device %s.", eventItem.events, |
| 1167 | device->identifier.name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | // readNotify() will modify the list of devices so this must be done after |
| 1172 | // processing all other events to ensure that we read all remaining events |
| 1173 | // before closing the devices. |
| 1174 | if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) { |
| 1175 | mPendingINotify = false; |
| 1176 | readNotifyLocked(); |
| 1177 | deviceChanged = true; |
| 1178 | } |
| 1179 | |
| 1180 | // Report added or removed devices immediately. |
| 1181 | if (deviceChanged) { |
| 1182 | continue; |
| 1183 | } |
| 1184 | |
| 1185 | // Return now if we have collected any events or if we were explicitly awoken. |
| 1186 | if (event != buffer || awoken) { |
| 1187 | break; |
| 1188 | } |
| 1189 | |
Siarhei Vishniakou | 4b5a87f | 2019-09-24 13:03:58 +0100 | [diff] [blame] | 1190 | // Poll for events. |
| 1191 | // When a device driver has pending (unread) events, it acquires |
| 1192 | // a kernel wake lock. Once the last pending event has been read, the device |
| 1193 | // driver will release the kernel wake lock, but the epoll will hold the wakelock, |
| 1194 | // since we are using EPOLLWAKEUP. The wakelock is released by the epoll when epoll_wait |
| 1195 | // is called again for the same fd that produced the event. |
| 1196 | // Thus the system can only sleep if there are no events pending or |
| 1197 | // currently being processed. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1198 | // |
| 1199 | // The timeout is advisory only. If the device is asleep, it will not wake just to |
| 1200 | // service the timeout. |
| 1201 | mPendingEventIndex = 0; |
| 1202 | |
Siarhei Vishniakou | 4b5a87f | 2019-09-24 13:03:58 +0100 | [diff] [blame] | 1203 | mLock.unlock(); // release lock before poll |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1204 | |
| 1205 | int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis); |
| 1206 | |
Siarhei Vishniakou | 4b5a87f | 2019-09-24 13:03:58 +0100 | [diff] [blame] | 1207 | mLock.lock(); // reacquire lock after poll |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1208 | |
| 1209 | if (pollResult == 0) { |
| 1210 | // Timed out. |
| 1211 | mPendingEventCount = 0; |
| 1212 | break; |
| 1213 | } |
| 1214 | |
| 1215 | if (pollResult < 0) { |
| 1216 | // An error occurred. |
| 1217 | mPendingEventCount = 0; |
| 1218 | |
| 1219 | // Sleep after errors to avoid locking up the system. |
| 1220 | // Hopefully the error is transient. |
| 1221 | if (errno != EINTR) { |
| 1222 | ALOGW("poll failed (errno=%d)\n", errno); |
| 1223 | usleep(100000); |
| 1224 | } |
| 1225 | } else { |
| 1226 | // Some events occurred. |
| 1227 | mPendingEventCount = size_t(pollResult); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | // All done, return the number of events we read. |
| 1232 | return event - buffer; |
| 1233 | } |
| 1234 | |
Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 1235 | std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1236 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 1237 | |
| 1238 | Device* device = getDeviceLocked(deviceId); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1239 | if (device == nullptr || !device->videoDevice) { |
Siarhei Vishniakou | add8929 | 2018-12-13 19:23:36 -0800 | [diff] [blame] | 1240 | return {}; |
| 1241 | } |
| 1242 | return device->videoDevice->consumeFrames(); |
| 1243 | } |
| 1244 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1245 | void EventHub::wake() { |
| 1246 | ALOGV("wake() called"); |
| 1247 | |
| 1248 | ssize_t nWrite; |
| 1249 | do { |
| 1250 | nWrite = write(mWakeWritePipeFd, "W", 1); |
| 1251 | } while (nWrite == -1 && errno == EINTR); |
| 1252 | |
| 1253 | if (nWrite != 1 && errno != EAGAIN) { |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1254 | ALOGW("Could not write wake signal: %s", strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | void EventHub::scanDevicesLocked() { |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1259 | status_t result = scanDirLocked(DEVICE_PATH); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1260 | if (result < 0) { |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1261 | ALOGE("scan dir failed for %s", DEVICE_PATH); |
| 1262 | } |
Philip Quinn | 39b8168 | 2019-01-09 22:20:39 -0800 | [diff] [blame] | 1263 | if (isV4lScanningEnabled()) { |
| 1264 | result = scanVideoDirLocked(VIDEO_DEVICE_PATH); |
| 1265 | if (result != OK) { |
| 1266 | ALOGE("scan video dir failed for %s", VIDEO_DEVICE_PATH); |
| 1267 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1268 | } |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1269 | if (mDevices.find(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) == mDevices.end()) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1270 | createVirtualKeyboardLocked(); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | // ---------------------------------------------------------------------------- |
| 1275 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1276 | static const int32_t GAMEPAD_KEYCODES[] = { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1277 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, // |
| 1278 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, // |
| 1279 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, // |
| 1280 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, // |
| 1281 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, // |
| 1282 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, // |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1283 | }; |
| 1284 | |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1285 | status_t EventHub::registerFdForEpoll(int fd) { |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1286 | // TODO(b/121395353) - consider adding EPOLLRDHUP |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1287 | struct epoll_event eventItem = {}; |
| 1288 | eventItem.events = EPOLLIN | EPOLLWAKEUP; |
| 1289 | eventItem.data.fd = fd; |
| 1290 | if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) { |
| 1291 | ALOGE("Could not add fd to epoll instance: %s", strerror(errno)); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1292 | return -errno; |
| 1293 | } |
| 1294 | return OK; |
| 1295 | } |
| 1296 | |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1297 | status_t EventHub::unregisterFdFromEpoll(int fd) { |
| 1298 | if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, fd, nullptr)) { |
| 1299 | ALOGW("Could not remove fd from epoll instance: %s", strerror(errno)); |
| 1300 | return -errno; |
| 1301 | } |
| 1302 | return OK; |
| 1303 | } |
| 1304 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1305 | status_t EventHub::registerDeviceForEpollLocked(Device& device) { |
| 1306 | status_t result = registerFdForEpoll(device.fd); |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1307 | if (result != OK) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1308 | ALOGE("Could not add input device fd to epoll for device %" PRId32, device.id); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1309 | return result; |
| 1310 | } |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1311 | if (device.videoDevice) { |
| 1312 | registerVideoDeviceForEpollLocked(*device.videoDevice); |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1313 | } |
| 1314 | return result; |
| 1315 | } |
| 1316 | |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1317 | void EventHub::registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) { |
| 1318 | status_t result = registerFdForEpoll(videoDevice.getFd()); |
| 1319 | if (result != OK) { |
| 1320 | ALOGE("Could not add video device %s to epoll", videoDevice.getName().c_str()); |
| 1321 | } |
| 1322 | } |
| 1323 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1324 | status_t EventHub::unregisterDeviceFromEpollLocked(Device& device) { |
| 1325 | if (device.hasValidFd()) { |
| 1326 | status_t result = unregisterFdFromEpoll(device.fd); |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1327 | if (result != OK) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1328 | ALOGW("Could not remove input device fd from epoll for device %" PRId32, device.id); |
Siarhei Vishniakou | 2592031 | 2018-12-12 15:24:44 -0800 | [diff] [blame] | 1329 | return result; |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1330 | } |
| 1331 | } |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1332 | if (device.videoDevice) { |
| 1333 | unregisterVideoDeviceFromEpollLocked(*device.videoDevice); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1334 | } |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1335 | return OK; |
| 1336 | } |
| 1337 | |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1338 | void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) { |
| 1339 | if (videoDevice.hasValidFd()) { |
| 1340 | status_t result = unregisterFdFromEpoll(videoDevice.getFd()); |
| 1341 | if (result != OK) { |
| 1342 | ALOGW("Could not remove video device fd from epoll for device: %s", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1343 | videoDevice.getName().c_str()); |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1348 | status_t EventHub::openDeviceLocked(const std::string& devicePath) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1349 | char buffer[80]; |
| 1350 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1351 | ALOGV("Opening device: %s", devicePath.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1352 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1353 | int fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1354 | if (fd < 0) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1355 | ALOGE("could not open %s, %s\n", devicePath.c_str(), strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1356 | return -1; |
| 1357 | } |
| 1358 | |
| 1359 | InputDeviceIdentifier identifier; |
| 1360 | |
| 1361 | // Get device name. |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1362 | if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1363 | ALOGE("Could not get device name for %s: %s", devicePath.c_str(), strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1364 | } else { |
| 1365 | buffer[sizeof(buffer) - 1] = '\0'; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1366 | identifier.name = buffer; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | // Check to see if the device is on our excluded list |
| 1370 | for (size_t i = 0; i < mExcludedDevices.size(); i++) { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1371 | const std::string& item = mExcludedDevices[i]; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1372 | if (identifier.name == item) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1373 | ALOGI("ignoring event id %s driver %s\n", devicePath.c_str(), item.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1374 | close(fd); |
| 1375 | return -1; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | // Get device driver version. |
| 1380 | int driverVersion; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1381 | if (ioctl(fd, EVIOCGVERSION, &driverVersion)) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1382 | ALOGE("could not get driver version for %s, %s\n", devicePath.c_str(), strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1383 | close(fd); |
| 1384 | return -1; |
| 1385 | } |
| 1386 | |
| 1387 | // Get device identifier. |
| 1388 | struct input_id inputId; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1389 | if (ioctl(fd, EVIOCGID, &inputId)) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1390 | ALOGE("could not get device input id for %s, %s\n", devicePath.c_str(), strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1391 | close(fd); |
| 1392 | return -1; |
| 1393 | } |
| 1394 | identifier.bus = inputId.bustype; |
| 1395 | identifier.product = inputId.product; |
| 1396 | identifier.vendor = inputId.vendor; |
| 1397 | identifier.version = inputId.version; |
| 1398 | |
| 1399 | // Get device physical location. |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1400 | if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { |
| 1401 | // fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1402 | } else { |
| 1403 | buffer[sizeof(buffer) - 1] = '\0'; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1404 | identifier.location = buffer; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | // Get device unique id. |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1408 | if (ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { |
| 1409 | // fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1410 | } else { |
| 1411 | buffer[sizeof(buffer) - 1] = '\0'; |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1412 | identifier.uniqueId = buffer; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | // Fill in the descriptor. |
| 1416 | assignDescriptorLocked(identifier); |
| 1417 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1418 | // Allocate device. (The device object takes ownership of the fd at this point.) |
| 1419 | int32_t deviceId = mNextDeviceId++; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1420 | std::unique_ptr<Device> device = std::make_unique<Device>(fd, deviceId, devicePath, identifier); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1421 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1422 | ALOGV("add device %d: %s\n", deviceId, devicePath.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1423 | ALOGV(" bus: %04x\n" |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1424 | " vendor %04x\n" |
| 1425 | " product %04x\n" |
| 1426 | " version %04x\n", |
| 1427 | identifier.bus, identifier.vendor, identifier.product, identifier.version); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1428 | ALOGV(" name: \"%s\"\n", identifier.name.c_str()); |
| 1429 | ALOGV(" location: \"%s\"\n", identifier.location.c_str()); |
| 1430 | ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.c_str()); |
| 1431 | ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.c_str()); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1432 | ALOGV(" driver: v%d.%d.%d\n", driverVersion >> 16, (driverVersion >> 8) & 0xff, |
| 1433 | driverVersion & 0xff); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1434 | |
| 1435 | // Load the configuration file for the device. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1436 | device->loadConfigurationLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1437 | |
| 1438 | // Figure out the kinds of events the device reports. |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1439 | device->readDeviceBitMask(EVIOCGBIT(EV_KEY, 0), device->keyBitmask); |
| 1440 | device->readDeviceBitMask(EVIOCGBIT(EV_ABS, 0), device->absBitmask); |
| 1441 | device->readDeviceBitMask(EVIOCGBIT(EV_REL, 0), device->relBitmask); |
| 1442 | device->readDeviceBitMask(EVIOCGBIT(EV_SW, 0), device->swBitmask); |
| 1443 | device->readDeviceBitMask(EVIOCGBIT(EV_LED, 0), device->ledBitmask); |
| 1444 | device->readDeviceBitMask(EVIOCGBIT(EV_FF, 0), device->ffBitmask); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1445 | device->readDeviceBitMask(EVIOCGBIT(EV_MSC, 0), device->mscBitmask); |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1446 | device->readDeviceBitMask(EVIOCGPROP(0), device->propBitmask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1447 | |
| 1448 | // See if this is a keyboard. Ignore everything in the button range except for |
| 1449 | // joystick and gamepad buttons which are handled like keyboards for the most part. |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1450 | bool haveKeyboardKeys = |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1451 | device->keyBitmask.any(0, BTN_MISC) || device->keyBitmask.any(BTN_WHEEL, KEY_MAX + 1); |
| 1452 | bool haveGamepadButtons = device->keyBitmask.any(BTN_MISC, BTN_MOUSE) || |
| 1453 | device->keyBitmask.any(BTN_JOYSTICK, BTN_DIGI); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1454 | if (haveKeyboardKeys || haveGamepadButtons) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1455 | device->classes |= InputDeviceClass::KEYBOARD; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | // See if this is a cursor device such as a trackball or mouse. |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1459 | if (device->keyBitmask.test(BTN_MOUSE) && device->relBitmask.test(REL_X) && |
| 1460 | device->relBitmask.test(REL_Y)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1461 | device->classes |= InputDeviceClass::CURSOR; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 1464 | // See if this is a rotary encoder type device. |
| 1465 | String8 deviceType = String8(); |
| 1466 | if (device->configuration && |
| 1467 | device->configuration->tryGetProperty(String8("device.type"), deviceType)) { |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1468 | if (!deviceType.compare(String8("rotaryEncoder"))) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1469 | device->classes |= InputDeviceClass::ROTARY_ENCODER; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1470 | } |
Prashant Malani | 1941ff5 | 2015-08-11 18:29:28 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1473 | // See if this is a touch pad. |
| 1474 | // Is this a new modern multi-touch driver? |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1475 | if (device->absBitmask.test(ABS_MT_POSITION_X) && device->absBitmask.test(ABS_MT_POSITION_Y)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1476 | // Some joysticks such as the PS3 controller report axes that conflict |
| 1477 | // with the ABS_MT range. Try to confirm that the device really is |
| 1478 | // a touch screen. |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1479 | if (device->keyBitmask.test(BTN_TOUCH) || !haveGamepadButtons) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1480 | device->classes |= (InputDeviceClass::TOUCH | InputDeviceClass::TOUCH_MT); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1481 | } |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1482 | // Is this an old style single-touch driver? |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1483 | } else if (device->keyBitmask.test(BTN_TOUCH) && device->absBitmask.test(ABS_X) && |
| 1484 | device->absBitmask.test(ABS_Y)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1485 | device->classes |= InputDeviceClass::TOUCH; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1486 | // Is this a BT stylus? |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1487 | } else if ((device->absBitmask.test(ABS_PRESSURE) || device->keyBitmask.test(BTN_TOUCH)) && |
| 1488 | !device->absBitmask.test(ABS_X) && !device->absBitmask.test(ABS_Y)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1489 | device->classes |= InputDeviceClass::EXTERNAL_STYLUS; |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 1490 | // Keyboard will try to claim some of the buttons but we really want to reserve those so we |
| 1491 | // can fuse it with the touch screen data, so just take them back. Note this means an |
| 1492 | // external stylus cannot also be a keyboard device. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1493 | device->classes &= ~InputDeviceClass::KEYBOARD; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | // See if this device is a joystick. |
| 1497 | // Assumes that joysticks always have gamepad buttons in order to distinguish them |
| 1498 | // from other devices such as accelerometers that also have absolute axes. |
| 1499 | if (haveGamepadButtons) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1500 | auto assumedClasses = device->classes | InputDeviceClass::JOYSTICK; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1501 | for (int i = 0; i <= ABS_MAX; i++) { |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1502 | if (device->absBitmask.test(i) && |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1503 | (getAbsAxisUsage(i, assumedClasses).test(InputDeviceClass::JOYSTICK))) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1504 | device->classes = assumedClasses; |
| 1505 | break; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1510 | // Check whether this device is an accelerometer. |
| 1511 | if (device->propBitmask.test(INPUT_PROP_ACCELEROMETER)) { |
| 1512 | device->classes |= InputDeviceClass::SENSOR; |
| 1513 | } |
| 1514 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1515 | // Check whether this device has switches. |
| 1516 | for (int i = 0; i <= SW_MAX; i++) { |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1517 | if (device->swBitmask.test(i)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1518 | device->classes |= InputDeviceClass::SWITCH; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | // Check whether this device supports the vibrator. |
Chris Ye | 66fbac3 | 2020-07-06 20:36:43 -0700 | [diff] [blame] | 1524 | if (device->ffBitmask.test(FF_RUMBLE)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1525 | device->classes |= InputDeviceClass::VIBRATOR; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | // Configure virtual keys. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1529 | if ((device->classes.test(InputDeviceClass::TOUCH))) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1530 | // Load the virtual keys for the touch screen, if any. |
| 1531 | // We do this now so that we can make sure to load the keymap if necessary. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1532 | bool success = device->loadVirtualKeyMapLocked(); |
Siarhei Vishniakou | 3e78dec | 2019-02-20 16:21:46 -0600 | [diff] [blame] | 1533 | if (success) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1534 | device->classes |= InputDeviceClass::KEYBOARD; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | // Load the key map. |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1539 | // We need to do this for joysticks too because the key layout may specify axes, and for |
| 1540 | // sensor as well because the key layout may specify the axes to sensor data mapping. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1541 | status_t keyMapStatus = NAME_NOT_FOUND; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 1542 | if (device->classes.any(InputDeviceClass::KEYBOARD | InputDeviceClass::JOYSTICK | |
| 1543 | InputDeviceClass::SENSOR)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1544 | // Load the keymap for the device. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1545 | keyMapStatus = device->loadKeyMapLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | // Configure the keyboard, gamepad or virtual keyboard. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1549 | if (device->classes.test(InputDeviceClass::KEYBOARD)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1550 | // Register the keyboard as a built-in keyboard if it is eligible. |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1551 | if (!keyMapStatus && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD && |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 1552 | isEligibleBuiltInKeyboard(device->identifier, device->configuration.get(), |
| 1553 | &device->keyMap)) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1554 | mBuiltInKeyboardId = device->id; |
| 1555 | } |
| 1556 | |
| 1557 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1558 | if (device->hasKeycodeLocked(AKEYCODE_Q)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1559 | device->classes |= InputDeviceClass::ALPHAKEY; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | // See if this device has a DPAD. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1563 | if (device->hasKeycodeLocked(AKEYCODE_DPAD_UP) && |
| 1564 | device->hasKeycodeLocked(AKEYCODE_DPAD_DOWN) && |
| 1565 | device->hasKeycodeLocked(AKEYCODE_DPAD_LEFT) && |
| 1566 | device->hasKeycodeLocked(AKEYCODE_DPAD_RIGHT) && |
| 1567 | device->hasKeycodeLocked(AKEYCODE_DPAD_CENTER)) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1568 | device->classes |= InputDeviceClass::DPAD; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | // See if this device has a gamepad. |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1572 | for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES) / sizeof(GAMEPAD_KEYCODES[0]); i++) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1573 | if (device->hasKeycodeLocked(GAMEPAD_KEYCODES[i])) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1574 | device->classes |= InputDeviceClass::GAMEPAD; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1575 | break; |
| 1576 | } |
| 1577 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | // If the device isn't recognized as something we handle, don't monitor it. |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1581 | if (device->classes == Flags<InputDeviceClass>(0)) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1582 | ALOGV("Dropping device: id=%d, path='%s', name='%s'", deviceId, devicePath.c_str(), |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1583 | device->identifier.name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1584 | return -1; |
| 1585 | } |
| 1586 | |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 1587 | // Determine whether the device has a mic. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1588 | if (device->deviceHasMicLocked()) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1589 | device->classes |= InputDeviceClass::MIC; |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1592 | // Determine whether the device is external or internal. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1593 | if (device->isExternalDeviceLocked()) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1594 | device->classes |= InputDeviceClass::EXTERNAL; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1597 | if (device->classes.any(InputDeviceClass::JOYSTICK | InputDeviceClass::DPAD) && |
| 1598 | device->classes.test(InputDeviceClass::GAMEPAD)) { |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1599 | device->controllerNumber = getNextControllerNumberLocked(device->identifier.name); |
| 1600 | device->setLedForControllerLocked(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1603 | if (registerDeviceForEpollLocked(*device) != OK) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1604 | return -1; |
| 1605 | } |
| 1606 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1607 | device->configureFd(); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1608 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1609 | ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=%s, " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1610 | "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, ", |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1611 | deviceId, fd, devicePath.c_str(), device->identifier.name.c_str(), |
| 1612 | device->classes.string().c_str(), device->configurationFile.c_str(), |
| 1613 | device->keyMap.keyLayoutFile.c_str(), device->keyMap.keyCharacterMapFile.c_str(), |
| 1614 | toString(mBuiltInKeyboardId == deviceId)); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1615 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1616 | addDeviceLocked(std::move(device)); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1617 | return OK; |
| 1618 | } |
| 1619 | |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1620 | void EventHub::openVideoDeviceLocked(const std::string& devicePath) { |
| 1621 | std::unique_ptr<TouchVideoDevice> videoDevice = TouchVideoDevice::create(devicePath); |
| 1622 | if (!videoDevice) { |
| 1623 | ALOGE("Could not create touch video device for %s. Ignoring", devicePath.c_str()); |
| 1624 | return; |
| 1625 | } |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1626 | // Transfer ownership of this video device to a matching input device |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1627 | for (const auto& [id, device] : mDevices) { |
Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 1628 | if (tryAddVideoDevice(*device, videoDevice)) { |
| 1629 | return; // 'device' now owns 'videoDevice' |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | // Couldn't find a matching input device, so just add it to a temporary holding queue. |
| 1634 | // A matching input device may appear later. |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1635 | ALOGI("Adding video device %s to list of unattached video devices", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1636 | videoDevice->getName().c_str()); |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1637 | mUnattachedVideoDevices.push_back(std::move(videoDevice)); |
| 1638 | } |
| 1639 | |
Siarhei Vishniakou | f49608d | 2020-08-20 19:18:21 -0500 | [diff] [blame] | 1640 | bool EventHub::tryAddVideoDevice(EventHub::Device& device, |
| 1641 | std::unique_ptr<TouchVideoDevice>& videoDevice) { |
| 1642 | if (videoDevice->getName() != device.identifier.name) { |
| 1643 | return false; |
| 1644 | } |
| 1645 | device.videoDevice = std::move(videoDevice); |
| 1646 | if (device.enabled) { |
| 1647 | registerVideoDeviceForEpollLocked(*device.videoDevice); |
| 1648 | } |
| 1649 | return true; |
| 1650 | } |
| 1651 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1652 | bool EventHub::isDeviceEnabled(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1653 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1654 | Device* device = getDeviceLocked(deviceId); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1655 | if (device == nullptr) { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1656 | ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__); |
| 1657 | return false; |
| 1658 | } |
| 1659 | return device->enabled; |
| 1660 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1661 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1662 | status_t EventHub::enableDevice(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1663 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1664 | Device* device = getDeviceLocked(deviceId); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1665 | if (device == nullptr) { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1666 | ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__); |
| 1667 | return BAD_VALUE; |
| 1668 | } |
| 1669 | if (device->enabled) { |
| 1670 | ALOGW("Duplicate call to %s, input device %" PRId32 " already enabled", __func__, deviceId); |
| 1671 | return OK; |
| 1672 | } |
| 1673 | status_t result = device->enable(); |
| 1674 | if (result != OK) { |
| 1675 | ALOGE("Failed to enable device %" PRId32, deviceId); |
| 1676 | return result; |
| 1677 | } |
| 1678 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1679 | device->configureFd(); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1680 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1681 | return registerDeviceForEpollLocked(*device); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | status_t EventHub::disableDevice(int32_t deviceId) { |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1685 | std::scoped_lock _l(mLock); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1686 | Device* device = getDeviceLocked(deviceId); |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 1687 | if (device == nullptr) { |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1688 | ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__); |
| 1689 | return BAD_VALUE; |
| 1690 | } |
| 1691 | if (!device->enabled) { |
| 1692 | ALOGW("Duplicate call to %s, input device already disabled", __func__); |
| 1693 | return OK; |
| 1694 | } |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1695 | unregisterDeviceFromEpollLocked(*device); |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1696 | return device->disable(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | void EventHub::createVirtualKeyboardLocked() { |
| 1700 | InputDeviceIdentifier identifier; |
| 1701 | identifier.name = "Virtual"; |
| 1702 | identifier.uniqueId = "<virtual>"; |
| 1703 | assignDescriptorLocked(identifier); |
| 1704 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1705 | std::unique_ptr<Device> device = |
| 1706 | std::make_unique<Device>(-1, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, "<virtual>", |
| 1707 | identifier); |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1708 | device->classes = InputDeviceClass::KEYBOARD | InputDeviceClass::ALPHAKEY | |
| 1709 | InputDeviceClass::DPAD | InputDeviceClass::VIRTUAL; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1710 | device->loadKeyMapLocked(); |
| 1711 | addDeviceLocked(std::move(device)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1712 | } |
| 1713 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1714 | void EventHub::addDeviceLocked(std::unique_ptr<Device> device) { |
| 1715 | mOpeningDevices.push_back(std::move(device)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1718 | int32_t EventHub::getNextControllerNumberLocked(const std::string& name) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1719 | if (mControllerNumbers.isFull()) { |
| 1720 | ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s", |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1721 | name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1722 | return 0; |
| 1723 | } |
| 1724 | // Since the controller number 0 is reserved for non-controllers, translate all numbers up by |
| 1725 | // one |
| 1726 | return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1); |
| 1727 | } |
| 1728 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1729 | void EventHub::releaseControllerNumberLocked(int32_t num) { |
| 1730 | if (num > 0) { |
| 1731 | mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1732 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1735 | void EventHub::closeDeviceByPathLocked(const std::string& devicePath) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1736 | Device* device = getDeviceByPathLocked(devicePath); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1737 | if (device != nullptr) { |
| 1738 | closeDeviceLocked(*device); |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1739 | return; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1740 | } |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1741 | ALOGV("Remove device: %s not found, device may already have been removed.", devicePath.c_str()); |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1742 | } |
| 1743 | |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1744 | /** |
| 1745 | * Find the video device by filename, and close it. |
| 1746 | * The video device is closed by path during an inotify event, where we don't have the |
| 1747 | * additional context about the video device fd, or the associated input device. |
| 1748 | */ |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1749 | void EventHub::closeVideoDeviceByPathLocked(const std::string& devicePath) { |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1750 | // A video device may be owned by an existing input device, or it may be stored in |
| 1751 | // the mUnattachedVideoDevices queue. Check both locations. |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1752 | for (const auto& [id, device] : mDevices) { |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1753 | if (device->videoDevice && device->videoDevice->getPath() == devicePath) { |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1754 | unregisterVideoDeviceFromEpollLocked(*device->videoDevice); |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1755 | device->videoDevice = nullptr; |
| 1756 | return; |
| 1757 | } |
| 1758 | } |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1759 | mUnattachedVideoDevices |
| 1760 | .erase(std::remove_if(mUnattachedVideoDevices.begin(), mUnattachedVideoDevices.end(), |
| 1761 | [&devicePath]( |
| 1762 | const std::unique_ptr<TouchVideoDevice>& videoDevice) { |
| 1763 | return videoDevice->getPath() == devicePath; |
| 1764 | }), |
| 1765 | mUnattachedVideoDevices.end()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | void EventHub::closeAllDevicesLocked() { |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1769 | mUnattachedVideoDevices.clear(); |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 1770 | while (!mDevices.empty()) { |
| 1771 | closeDeviceLocked(*(mDevices.begin()->second)); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1775 | void EventHub::closeDeviceLocked(Device& device) { |
| 1776 | ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=%s", device.path.c_str(), |
| 1777 | device.identifier.name.c_str(), device.id, device.fd, device.classes.string().c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1778 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1779 | if (device.id == mBuiltInKeyboardId) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1780 | ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1781 | device.path.c_str(), mBuiltInKeyboardId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1782 | mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD; |
| 1783 | } |
| 1784 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 1785 | unregisterDeviceFromEpollLocked(device); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1786 | if (device.videoDevice) { |
Siarhei Vishniakou | 1259868 | 2018-11-02 17:19:19 -0700 | [diff] [blame] | 1787 | // This must be done after the video device is removed from epoll |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1788 | mUnattachedVideoDevices.push_back(std::move(device.videoDevice)); |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1789 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1790 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1791 | releaseControllerNumberLocked(device.controllerNumber); |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 1792 | device.controllerNumber = 0; |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1793 | device.close(); |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1794 | mClosingDevices.push_back(std::move(mDevices[device.id])); |
Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 1795 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1796 | mDevices.erase(device.id); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | status_t EventHub::readNotifyLocked() { |
| 1800 | int res; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1801 | char event_buf[512]; |
| 1802 | int event_size; |
| 1803 | int event_pos = 0; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1804 | struct inotify_event* event; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1805 | |
| 1806 | ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd); |
| 1807 | res = read(mINotifyFd, event_buf, sizeof(event_buf)); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1808 | if (res < (int)sizeof(*event)) { |
| 1809 | if (errno == EINTR) return 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1810 | ALOGW("could not get event, %s\n", strerror(errno)); |
| 1811 | return -1; |
| 1812 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1813 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1814 | while (res >= (int)sizeof(*event)) { |
| 1815 | event = (struct inotify_event*)(event_buf + event_pos); |
| 1816 | if (event->len) { |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 1817 | if (event->wd == mInputWd) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1818 | std::string filename = std::string(DEVICE_PATH) + "/" + event->name; |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1819 | if (event->mask & IN_CREATE) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1820 | openDeviceLocked(filename); |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 1821 | } else { |
| 1822 | ALOGI("Removing device '%s' due to inotify event\n", filename.c_str()); |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1823 | closeDeviceByPathLocked(filename); |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 1824 | } |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1825 | } else if (event->wd == mVideoWd) { |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 1826 | if (isV4lTouchNode(event->name)) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1827 | std::string filename = std::string(VIDEO_DEVICE_PATH) + "/" + event->name; |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1828 | if (event->mask & IN_CREATE) { |
| 1829 | openVideoDeviceLocked(filename); |
| 1830 | } else { |
| 1831 | ALOGI("Removing video device '%s' due to inotify event", filename.c_str()); |
| 1832 | closeVideoDeviceByPathLocked(filename); |
| 1833 | } |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 1834 | } |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1835 | } else { |
Siarhei Vishniakou | 951f362 | 2018-12-12 19:45:42 -0800 | [diff] [blame] | 1836 | LOG_ALWAYS_FATAL("Unexpected inotify event, wd = %i", event->wd); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | event_size = sizeof(*event) + event->len; |
| 1840 | res -= event_size; |
| 1841 | event_pos += event_size; |
| 1842 | } |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1846 | status_t EventHub::scanDirLocked(const std::string& dirname) { |
| 1847 | for (const auto& entry : std::filesystem::directory_iterator(dirname)) { |
| 1848 | openDeviceLocked(entry.path()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1849 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1850 | return 0; |
| 1851 | } |
| 1852 | |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1853 | /** |
| 1854 | * Look for all dirname/v4l-touch* devices, and open them. |
| 1855 | */ |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1856 | status_t EventHub::scanVideoDirLocked(const std::string& dirname) { |
Chris Ye | 8594e19 | 2020-07-14 10:34:06 -0700 | [diff] [blame] | 1857 | for (const auto& entry : std::filesystem::directory_iterator(dirname)) { |
| 1858 | if (isV4lTouchNode(entry.path())) { |
| 1859 | ALOGI("Found touch video device %s", entry.path().c_str()); |
| 1860 | openVideoDeviceLocked(entry.path()); |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1861 | } |
| 1862 | } |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1863 | return OK; |
| 1864 | } |
| 1865 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1866 | void EventHub::requestReopenDevices() { |
| 1867 | ALOGV("requestReopenDevices() called"); |
| 1868 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1869 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1870 | mNeedToReopenDevices = true; |
| 1871 | } |
| 1872 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1873 | void EventHub::dump(std::string& dump) { |
| 1874 | dump += "Event Hub State:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1875 | |
| 1876 | { // acquire lock |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 1877 | std::scoped_lock _l(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1878 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1879 | dump += StringPrintf(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1880 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1881 | dump += INDENT "Devices:\n"; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1882 | |
Chris Ye | 989bb93 | 2020-07-04 16:18:59 -0700 | [diff] [blame] | 1883 | for (const auto& [id, device] : mDevices) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1884 | if (mBuiltInKeyboardId == device->id) { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1885 | dump += StringPrintf(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1886 | device->id, device->identifier.name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1887 | } else { |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1888 | dump += StringPrintf(INDENT2 "%d: %s\n", device->id, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1889 | device->identifier.name.c_str()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1890 | } |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 1891 | dump += StringPrintf(INDENT3 "Classes: %s\n", device->classes.string().c_str()); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1892 | dump += StringPrintf(INDENT3 "Path: %s\n", device->path.c_str()); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1893 | dump += StringPrintf(INDENT3 "Enabled: %s\n", toString(device->enabled)); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1894 | dump += StringPrintf(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.c_str()); |
| 1895 | dump += StringPrintf(INDENT3 "Location: %s\n", device->identifier.location.c_str()); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1896 | dump += StringPrintf(INDENT3 "ControllerNumber: %d\n", device->controllerNumber); |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 1897 | dump += StringPrintf(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.c_str()); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1898 | dump += StringPrintf(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1899 | "product=0x%04x, version=0x%04x\n", |
| 1900 | device->identifier.bus, device->identifier.vendor, |
| 1901 | device->identifier.product, device->identifier.version); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1902 | dump += StringPrintf(INDENT3 "KeyLayoutFile: %s\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1903 | device->keyMap.keyLayoutFile.c_str()); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1904 | dump += StringPrintf(INDENT3 "KeyCharacterMapFile: %s\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1905 | device->keyMap.keyCharacterMapFile.c_str()); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1906 | dump += StringPrintf(INDENT3 "ConfigurationFile: %s\n", |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 1907 | device->configurationFile.c_str()); |
Siarhei Vishniakou | ec7854a | 2018-12-14 16:52:34 -0800 | [diff] [blame] | 1908 | dump += INDENT3 "VideoDevice: "; |
| 1909 | if (device->videoDevice) { |
| 1910 | dump += device->videoDevice->dump() + "\n"; |
| 1911 | } else { |
| 1912 | dump += "<none>\n"; |
| 1913 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1914 | } |
Siarhei Vishniakou | 22c8846 | 2018-12-13 19:34:53 -0800 | [diff] [blame] | 1915 | |
| 1916 | dump += INDENT "Unattached video devices:\n"; |
| 1917 | for (const std::unique_ptr<TouchVideoDevice>& videoDevice : mUnattachedVideoDevices) { |
| 1918 | dump += INDENT2 + videoDevice->dump() + "\n"; |
| 1919 | } |
| 1920 | if (mUnattachedVideoDevices.empty()) { |
| 1921 | dump += INDENT2 "<none>\n"; |
| 1922 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1923 | } // release lock |
| 1924 | } |
| 1925 | |
| 1926 | void EventHub::monitor() { |
| 1927 | // Acquire and release the lock to ensure that the event hub has not deadlocked. |
Chris Ye | 1c2e089 | 2020-11-30 21:41:44 -0800 | [diff] [blame] | 1928 | std::unique_lock<std::mutex> lock(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1929 | } |
| 1930 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1931 | }; // namespace android |