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