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