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