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