Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2005 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #define LOG_TAG "EventHub" |
michael@0 | 18 | |
michael@0 | 19 | // #define LOG_NDEBUG 0 |
michael@0 | 20 | #include "cutils_log.h" |
michael@0 | 21 | |
michael@0 | 22 | #include "EventHub.h" |
michael@0 | 23 | |
michael@0 | 24 | #include <hardware_legacy/power.h> |
michael@0 | 25 | |
michael@0 | 26 | #include <cutils/properties.h> |
michael@0 | 27 | #include "cutils_log.h" |
michael@0 | 28 | #include <utils/Timers.h> |
michael@0 | 29 | #include <utils/threads.h> |
michael@0 | 30 | #include <utils/Errors.h> |
michael@0 | 31 | |
michael@0 | 32 | #include <stdlib.h> |
michael@0 | 33 | #include <stdio.h> |
michael@0 | 34 | #include <unistd.h> |
michael@0 | 35 | #include <fcntl.h> |
michael@0 | 36 | #include <memory.h> |
michael@0 | 37 | #include <errno.h> |
michael@0 | 38 | #include <assert.h> |
michael@0 | 39 | |
michael@0 | 40 | #include "KeyLayoutMap.h" |
michael@0 | 41 | #include "KeyCharacterMap.h" |
michael@0 | 42 | #include "VirtualKeyMap.h" |
michael@0 | 43 | |
michael@0 | 44 | #include <string.h> |
michael@0 | 45 | #include <stdint.h> |
michael@0 | 46 | #include <dirent.h> |
michael@0 | 47 | |
michael@0 | 48 | #include <sys/inotify.h> |
michael@0 | 49 | #include <sys/epoll.h> |
michael@0 | 50 | #include <sys/ioctl.h> |
michael@0 | 51 | #include <sys/limits.h> |
michael@0 | 52 | #include <sha1.h> |
michael@0 | 53 | |
michael@0 | 54 | /* this macro is used to tell if "bit" is set in "array" |
michael@0 | 55 | * it selects a byte from the array, and does a boolean AND |
michael@0 | 56 | * operation with a byte that only has the relevant bit set. |
michael@0 | 57 | * eg. to check for the 12th bit, we do (array[1] & 1<<4) |
michael@0 | 58 | */ |
michael@0 | 59 | #define test_bit(bit, array) (array[bit/8] & (1<<(bit%8))) |
michael@0 | 60 | |
michael@0 | 61 | /* this macro computes the number of bytes needed to represent a bit array of the specified size */ |
michael@0 | 62 | #define sizeof_bit_array(bits) ((bits + 7) / 8) |
michael@0 | 63 | |
michael@0 | 64 | #define INDENT " " |
michael@0 | 65 | #define INDENT2 " " |
michael@0 | 66 | #define INDENT3 " " |
michael@0 | 67 | |
michael@0 | 68 | namespace android { |
michael@0 | 69 | |
michael@0 | 70 | static const char *WAKE_LOCK_ID = "KeyEvents"; |
michael@0 | 71 | static const char *DEVICE_PATH = "/dev/input"; |
michael@0 | 72 | |
michael@0 | 73 | /* return the larger integer */ |
michael@0 | 74 | static inline int max(int v1, int v2) |
michael@0 | 75 | { |
michael@0 | 76 | return (v1 > v2) ? v1 : v2; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | static inline const char* toString(bool value) { |
michael@0 | 80 | return value ? "true" : "false"; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | static String8 sha1(const String8& in) { |
michael@0 | 84 | SHA1_CTX ctx; |
michael@0 | 85 | SHA1Init(&ctx); |
michael@0 | 86 | SHA1Update(&ctx, reinterpret_cast<const u_char*>(in.string()), in.size()); |
michael@0 | 87 | u_char digest[SHA1_DIGEST_LENGTH]; |
michael@0 | 88 | SHA1Final(digest, &ctx); |
michael@0 | 89 | |
michael@0 | 90 | String8 out; |
michael@0 | 91 | for (size_t i = 0; i < SHA1_DIGEST_LENGTH; i++) { |
michael@0 | 92 | out.appendFormat("%02x", digest[i]); |
michael@0 | 93 | } |
michael@0 | 94 | return out; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | static void setDescriptor(InputDeviceIdentifier& identifier) { |
michael@0 | 98 | // Compute a device descriptor that uniquely identifies the device. |
michael@0 | 99 | // The descriptor is assumed to be a stable identifier. Its value should not |
michael@0 | 100 | // change between reboots, reconnections, firmware updates or new releases of Android. |
michael@0 | 101 | // Ideally, we also want the descriptor to be short and relatively opaque. |
michael@0 | 102 | String8 rawDescriptor; |
michael@0 | 103 | rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor, identifier.product); |
michael@0 | 104 | if (!identifier.uniqueId.isEmpty()) { |
michael@0 | 105 | rawDescriptor.append("uniqueId:"); |
michael@0 | 106 | rawDescriptor.append(identifier.uniqueId); |
michael@0 | 107 | } if (identifier.vendor == 0 && identifier.product == 0) { |
michael@0 | 108 | // If we don't know the vendor and product id, then the device is probably |
michael@0 | 109 | // built-in so we need to rely on other information to uniquely identify |
michael@0 | 110 | // the input device. Usually we try to avoid relying on the device name or |
michael@0 | 111 | // location but for built-in input device, they are unlikely to ever change. |
michael@0 | 112 | if (!identifier.name.isEmpty()) { |
michael@0 | 113 | rawDescriptor.append("name:"); |
michael@0 | 114 | rawDescriptor.append(identifier.name); |
michael@0 | 115 | } else if (!identifier.location.isEmpty()) { |
michael@0 | 116 | rawDescriptor.append("location:"); |
michael@0 | 117 | rawDescriptor.append(identifier.location); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | identifier.descriptor = sha1(rawDescriptor); |
michael@0 | 121 | ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(), |
michael@0 | 122 | identifier.descriptor.string()); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // --- Global Functions --- |
michael@0 | 126 | |
michael@0 | 127 | uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) { |
michael@0 | 128 | // Touch devices get dibs on touch-related axes. |
michael@0 | 129 | if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) { |
michael@0 | 130 | switch (axis) { |
michael@0 | 131 | case ABS_X: |
michael@0 | 132 | case ABS_Y: |
michael@0 | 133 | case ABS_PRESSURE: |
michael@0 | 134 | case ABS_TOOL_WIDTH: |
michael@0 | 135 | case ABS_DISTANCE: |
michael@0 | 136 | case ABS_TILT_X: |
michael@0 | 137 | case ABS_TILT_Y: |
michael@0 | 138 | case ABS_MT_SLOT: |
michael@0 | 139 | case ABS_MT_TOUCH_MAJOR: |
michael@0 | 140 | case ABS_MT_TOUCH_MINOR: |
michael@0 | 141 | case ABS_MT_WIDTH_MAJOR: |
michael@0 | 142 | case ABS_MT_WIDTH_MINOR: |
michael@0 | 143 | case ABS_MT_ORIENTATION: |
michael@0 | 144 | case ABS_MT_POSITION_X: |
michael@0 | 145 | case ABS_MT_POSITION_Y: |
michael@0 | 146 | case ABS_MT_TOOL_TYPE: |
michael@0 | 147 | case ABS_MT_BLOB_ID: |
michael@0 | 148 | case ABS_MT_TRACKING_ID: |
michael@0 | 149 | case ABS_MT_PRESSURE: |
michael@0 | 150 | case ABS_MT_DISTANCE: |
michael@0 | 151 | return INPUT_DEVICE_CLASS_TOUCH; |
michael@0 | 152 | } |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | // Joystick devices get the rest. |
michael@0 | 156 | return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | // --- EventHub::Device --- |
michael@0 | 160 | |
michael@0 | 161 | EventHub::Device::Device(int fd, int32_t id, const String8& path, |
michael@0 | 162 | const InputDeviceIdentifier& identifier) : |
michael@0 | 163 | next(NULL), |
michael@0 | 164 | fd(fd), id(id), path(path), identifier(identifier), |
michael@0 | 165 | classes(0), configuration(NULL), virtualKeyMap(NULL), |
michael@0 | 166 | ffEffectPlaying(false), ffEffectId(-1), |
michael@0 | 167 | timestampOverrideSec(0), timestampOverrideUsec(0) { |
michael@0 | 168 | memset(keyBitmask, 0, sizeof(keyBitmask)); |
michael@0 | 169 | memset(absBitmask, 0, sizeof(absBitmask)); |
michael@0 | 170 | memset(relBitmask, 0, sizeof(relBitmask)); |
michael@0 | 171 | memset(swBitmask, 0, sizeof(swBitmask)); |
michael@0 | 172 | memset(ledBitmask, 0, sizeof(ledBitmask)); |
michael@0 | 173 | memset(ffBitmask, 0, sizeof(ffBitmask)); |
michael@0 | 174 | memset(propBitmask, 0, sizeof(propBitmask)); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | EventHub::Device::~Device() { |
michael@0 | 178 | close(); |
michael@0 | 179 | delete configuration; |
michael@0 | 180 | delete virtualKeyMap; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | void EventHub::Device::close() { |
michael@0 | 184 | if (fd >= 0) { |
michael@0 | 185 | ::close(fd); |
michael@0 | 186 | fd = -1; |
michael@0 | 187 | } |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | |
michael@0 | 191 | // --- EventHub --- |
michael@0 | 192 | |
michael@0 | 193 | const uint32_t EventHub::EPOLL_ID_INOTIFY; |
michael@0 | 194 | const uint32_t EventHub::EPOLL_ID_WAKE; |
michael@0 | 195 | const int EventHub::EPOLL_SIZE_HINT; |
michael@0 | 196 | const int EventHub::EPOLL_MAX_EVENTS; |
michael@0 | 197 | |
michael@0 | 198 | EventHub::EventHub(void) : |
michael@0 | 199 | mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), |
michael@0 | 200 | mOpeningDevices(0), mClosingDevices(0), |
michael@0 | 201 | mNeedToSendFinishedDeviceScan(false), |
michael@0 | 202 | mNeedToReopenDevices(false), mNeedToScanDevices(true), |
michael@0 | 203 | mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) { |
michael@0 | 204 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
michael@0 | 205 | |
michael@0 | 206 | mEpollFd = epoll_create(EPOLL_SIZE_HINT); |
michael@0 | 207 | LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno); |
michael@0 | 208 | |
michael@0 | 209 | mINotifyFd = inotify_init(); |
michael@0 | 210 | int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE); |
michael@0 | 211 | LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d", |
michael@0 | 212 | DEVICE_PATH, errno); |
michael@0 | 213 | |
michael@0 | 214 | struct epoll_event eventItem; |
michael@0 | 215 | memset(&eventItem, 0, sizeof(eventItem)); |
michael@0 | 216 | eventItem.events = EPOLLIN; |
michael@0 | 217 | eventItem.data.u32 = EPOLL_ID_INOTIFY; |
michael@0 | 218 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem); |
michael@0 | 219 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno); |
michael@0 | 220 | |
michael@0 | 221 | int wakeFds[2]; |
michael@0 | 222 | result = pipe(wakeFds); |
michael@0 | 223 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno); |
michael@0 | 224 | |
michael@0 | 225 | mWakeReadPipeFd = wakeFds[0]; |
michael@0 | 226 | mWakeWritePipeFd = wakeFds[1]; |
michael@0 | 227 | |
michael@0 | 228 | result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK); |
michael@0 | 229 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d", |
michael@0 | 230 | errno); |
michael@0 | 231 | |
michael@0 | 232 | result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK); |
michael@0 | 233 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d", |
michael@0 | 234 | errno); |
michael@0 | 235 | |
michael@0 | 236 | eventItem.data.u32 = EPOLL_ID_WAKE; |
michael@0 | 237 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem); |
michael@0 | 238 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d", |
michael@0 | 239 | errno); |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | EventHub::~EventHub(void) { |
michael@0 | 243 | closeAllDevicesLocked(); |
michael@0 | 244 | |
michael@0 | 245 | while (mClosingDevices) { |
michael@0 | 246 | Device* device = mClosingDevices; |
michael@0 | 247 | mClosingDevices = device->next; |
michael@0 | 248 | delete device; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | ::close(mEpollFd); |
michael@0 | 252 | ::close(mINotifyFd); |
michael@0 | 253 | ::close(mWakeReadPipeFd); |
michael@0 | 254 | ::close(mWakeWritePipeFd); |
michael@0 | 255 | |
michael@0 | 256 | release_wake_lock(WAKE_LOCK_ID); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const { |
michael@0 | 260 | AutoMutex _l(mLock); |
michael@0 | 261 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 262 | if (device == NULL) return InputDeviceIdentifier(); |
michael@0 | 263 | return device->identifier; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const { |
michael@0 | 267 | AutoMutex _l(mLock); |
michael@0 | 268 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 269 | if (device == NULL) return 0; |
michael@0 | 270 | return device->classes; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { |
michael@0 | 274 | AutoMutex _l(mLock); |
michael@0 | 275 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 276 | if (device && device->configuration) { |
michael@0 | 277 | *outConfiguration = *device->configuration; |
michael@0 | 278 | } else { |
michael@0 | 279 | outConfiguration->clear(); |
michael@0 | 280 | } |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
michael@0 | 284 | RawAbsoluteAxisInfo* outAxisInfo) const { |
michael@0 | 285 | outAxisInfo->clear(); |
michael@0 | 286 | |
michael@0 | 287 | if (axis >= 0 && axis <= ABS_MAX) { |
michael@0 | 288 | AutoMutex _l(mLock); |
michael@0 | 289 | |
michael@0 | 290 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 291 | if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) { |
michael@0 | 292 | struct input_absinfo info; |
michael@0 | 293 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
michael@0 | 294 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", |
michael@0 | 295 | axis, device->identifier.name.string(), device->fd, errno); |
michael@0 | 296 | return -errno; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | if (info.minimum != info.maximum) { |
michael@0 | 300 | outAxisInfo->valid = true; |
michael@0 | 301 | outAxisInfo->minValue = info.minimum; |
michael@0 | 302 | outAxisInfo->maxValue = info.maximum; |
michael@0 | 303 | outAxisInfo->flat = info.flat; |
michael@0 | 304 | outAxisInfo->fuzz = info.fuzz; |
michael@0 | 305 | outAxisInfo->resolution = info.resolution; |
michael@0 | 306 | } |
michael@0 | 307 | return OK; |
michael@0 | 308 | } |
michael@0 | 309 | } |
michael@0 | 310 | return -1; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const { |
michael@0 | 314 | if (axis >= 0 && axis <= REL_MAX) { |
michael@0 | 315 | AutoMutex _l(mLock); |
michael@0 | 316 | |
michael@0 | 317 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 318 | if (device) { |
michael@0 | 319 | return test_bit(axis, device->relBitmask); |
michael@0 | 320 | } |
michael@0 | 321 | } |
michael@0 | 322 | return false; |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | bool EventHub::hasInputProperty(int32_t deviceId, int property) const { |
michael@0 | 326 | if (property >= 0 && property <= INPUT_PROP_MAX) { |
michael@0 | 327 | AutoMutex _l(mLock); |
michael@0 | 328 | |
michael@0 | 329 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 330 | if (device) { |
michael@0 | 331 | return test_bit(property, device->propBitmask); |
michael@0 | 332 | } |
michael@0 | 333 | } |
michael@0 | 334 | return false; |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
michael@0 | 338 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
michael@0 | 339 | AutoMutex _l(mLock); |
michael@0 | 340 | |
michael@0 | 341 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 342 | if (device && !device->isVirtual() && test_bit(scanCode, device->keyBitmask)) { |
michael@0 | 343 | uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; |
michael@0 | 344 | memset(keyState, 0, sizeof(keyState)); |
michael@0 | 345 | if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { |
michael@0 | 346 | return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
michael@0 | 347 | } |
michael@0 | 348 | } |
michael@0 | 349 | } |
michael@0 | 350 | return AKEY_STATE_UNKNOWN; |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
michael@0 | 354 | AutoMutex _l(mLock); |
michael@0 | 355 | |
michael@0 | 356 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 357 | if (device && !device->isVirtual() && device->keyMap.haveKeyLayout()) { |
michael@0 | 358 | Vector<int32_t> scanCodes; |
michael@0 | 359 | device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes); |
michael@0 | 360 | if (scanCodes.size() != 0) { |
michael@0 | 361 | uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; |
michael@0 | 362 | memset(keyState, 0, sizeof(keyState)); |
michael@0 | 363 | if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { |
michael@0 | 364 | for (size_t i = 0; i < scanCodes.size(); i++) { |
michael@0 | 365 | int32_t sc = scanCodes.itemAt(i); |
michael@0 | 366 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) { |
michael@0 | 367 | return AKEY_STATE_DOWN; |
michael@0 | 368 | } |
michael@0 | 369 | } |
michael@0 | 370 | return AKEY_STATE_UP; |
michael@0 | 371 | } |
michael@0 | 372 | } |
michael@0 | 373 | } |
michael@0 | 374 | return AKEY_STATE_UNKNOWN; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
michael@0 | 378 | if (sw >= 0 && sw <= SW_MAX) { |
michael@0 | 379 | AutoMutex _l(mLock); |
michael@0 | 380 | |
michael@0 | 381 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 382 | if (device && !device->isVirtual() && test_bit(sw, device->swBitmask)) { |
michael@0 | 383 | uint8_t swState[sizeof_bit_array(SW_MAX + 1)]; |
michael@0 | 384 | memset(swState, 0, sizeof(swState)); |
michael@0 | 385 | if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) { |
michael@0 | 386 | return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
michael@0 | 387 | } |
michael@0 | 388 | } |
michael@0 | 389 | } |
michael@0 | 390 | return AKEY_STATE_UNKNOWN; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const { |
michael@0 | 394 | *outValue = 0; |
michael@0 | 395 | |
michael@0 | 396 | if (axis >= 0 && axis <= ABS_MAX) { |
michael@0 | 397 | AutoMutex _l(mLock); |
michael@0 | 398 | |
michael@0 | 399 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 400 | if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) { |
michael@0 | 401 | struct input_absinfo info; |
michael@0 | 402 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
michael@0 | 403 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", |
michael@0 | 404 | axis, device->identifier.name.string(), device->fd, errno); |
michael@0 | 405 | return -errno; |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | *outValue = info.value; |
michael@0 | 409 | return OK; |
michael@0 | 410 | } |
michael@0 | 411 | } |
michael@0 | 412 | return -1; |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
michael@0 | 416 | const int32_t* keyCodes, uint8_t* outFlags) const { |
michael@0 | 417 | AutoMutex _l(mLock); |
michael@0 | 418 | |
michael@0 | 419 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 420 | if (device && device->keyMap.haveKeyLayout()) { |
michael@0 | 421 | Vector<int32_t> scanCodes; |
michael@0 | 422 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
michael@0 | 423 | scanCodes.clear(); |
michael@0 | 424 | |
michael@0 | 425 | status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey( |
michael@0 | 426 | keyCodes[codeIndex], &scanCodes); |
michael@0 | 427 | if (! err) { |
michael@0 | 428 | // check the possible scan codes identified by the layout map against the |
michael@0 | 429 | // map of codes actually emitted by the driver |
michael@0 | 430 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
michael@0 | 431 | if (test_bit(scanCodes[sc], device->keyBitmask)) { |
michael@0 | 432 | outFlags[codeIndex] = 1; |
michael@0 | 433 | break; |
michael@0 | 434 | } |
michael@0 | 435 | } |
michael@0 | 436 | } |
michael@0 | 437 | } |
michael@0 | 438 | return true; |
michael@0 | 439 | } |
michael@0 | 440 | return false; |
michael@0 | 441 | } |
michael@0 | 442 | |
michael@0 | 443 | status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
michael@0 | 444 | int32_t* outKeycode, uint32_t* outFlags) const { |
michael@0 | 445 | AutoMutex _l(mLock); |
michael@0 | 446 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 447 | |
michael@0 | 448 | if (device) { |
michael@0 | 449 | // Check the key character map first. |
michael@0 | 450 | sp<KeyCharacterMap> kcm = device->getKeyCharacterMap(); |
michael@0 | 451 | if (kcm != NULL) { |
michael@0 | 452 | if (!kcm->mapKey(scanCode, usageCode, outKeycode)) { |
michael@0 | 453 | *outFlags = 0; |
michael@0 | 454 | return NO_ERROR; |
michael@0 | 455 | } |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | // Check the key layout next. |
michael@0 | 459 | if (device->keyMap.haveKeyLayout()) { |
michael@0 | 460 | if (!device->keyMap.keyLayoutMap->mapKey( |
michael@0 | 461 | scanCode, usageCode, outKeycode, outFlags)) { |
michael@0 | 462 | return NO_ERROR; |
michael@0 | 463 | } |
michael@0 | 464 | } |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | *outKeycode = 0; |
michael@0 | 468 | *outFlags = 0; |
michael@0 | 469 | return NAME_NOT_FOUND; |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const { |
michael@0 | 473 | AutoMutex _l(mLock); |
michael@0 | 474 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 475 | |
michael@0 | 476 | if (device && device->keyMap.haveKeyLayout()) { |
michael@0 | 477 | status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo); |
michael@0 | 478 | if (err == NO_ERROR) { |
michael@0 | 479 | return NO_ERROR; |
michael@0 | 480 | } |
michael@0 | 481 | } |
michael@0 | 482 | |
michael@0 | 483 | return NAME_NOT_FOUND; |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | void EventHub::setExcludedDevices(const Vector<String8>& devices) { |
michael@0 | 487 | AutoMutex _l(mLock); |
michael@0 | 488 | |
michael@0 | 489 | mExcludedDevices = devices; |
michael@0 | 490 | } |
michael@0 | 491 | |
michael@0 | 492 | bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const { |
michael@0 | 493 | AutoMutex _l(mLock); |
michael@0 | 494 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 495 | if (device && scanCode >= 0 && scanCode <= KEY_MAX) { |
michael@0 | 496 | if (test_bit(scanCode, device->keyBitmask)) { |
michael@0 | 497 | return true; |
michael@0 | 498 | } |
michael@0 | 499 | } |
michael@0 | 500 | return false; |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { |
michael@0 | 504 | AutoMutex _l(mLock); |
michael@0 | 505 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 506 | if (device && led >= 0 && led <= LED_MAX) { |
michael@0 | 507 | if (test_bit(led, device->ledBitmask)) { |
michael@0 | 508 | return true; |
michael@0 | 509 | } |
michael@0 | 510 | } |
michael@0 | 511 | return false; |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
michael@0 | 515 | AutoMutex _l(mLock); |
michael@0 | 516 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 517 | if (device && !device->isVirtual() && led >= 0 && led <= LED_MAX) { |
michael@0 | 518 | struct input_event ev; |
michael@0 | 519 | ev.time.tv_sec = 0; |
michael@0 | 520 | ev.time.tv_usec = 0; |
michael@0 | 521 | ev.type = EV_LED; |
michael@0 | 522 | ev.code = led; |
michael@0 | 523 | ev.value = on ? 1 : 0; |
michael@0 | 524 | |
michael@0 | 525 | ssize_t nWrite; |
michael@0 | 526 | do { |
michael@0 | 527 | nWrite = write(device->fd, &ev, sizeof(struct input_event)); |
michael@0 | 528 | } while (nWrite == -1 && errno == EINTR); |
michael@0 | 529 | } |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, |
michael@0 | 533 | Vector<VirtualKeyDefinition>& outVirtualKeys) const { |
michael@0 | 534 | outVirtualKeys.clear(); |
michael@0 | 535 | |
michael@0 | 536 | AutoMutex _l(mLock); |
michael@0 | 537 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 538 | if (device && device->virtualKeyMap) { |
michael@0 | 539 | outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys()); |
michael@0 | 540 | } |
michael@0 | 541 | } |
michael@0 | 542 | |
michael@0 | 543 | sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const { |
michael@0 | 544 | AutoMutex _l(mLock); |
michael@0 | 545 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 546 | if (device) { |
michael@0 | 547 | return device->getKeyCharacterMap(); |
michael@0 | 548 | } |
michael@0 | 549 | return NULL; |
michael@0 | 550 | } |
michael@0 | 551 | |
michael@0 | 552 | bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, |
michael@0 | 553 | const sp<KeyCharacterMap>& map) { |
michael@0 | 554 | AutoMutex _l(mLock); |
michael@0 | 555 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 556 | if (device) { |
michael@0 | 557 | if (map != device->overlayKeyMap) { |
michael@0 | 558 | device->overlayKeyMap = map; |
michael@0 | 559 | device->combinedKeyMap = KeyCharacterMap::combine( |
michael@0 | 560 | device->keyMap.keyCharacterMap, map); |
michael@0 | 561 | return true; |
michael@0 | 562 | } |
michael@0 | 563 | } |
michael@0 | 564 | return false; |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | void EventHub::vibrate(int32_t deviceId, nsecs_t duration) { |
michael@0 | 568 | AutoMutex _l(mLock); |
michael@0 | 569 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 570 | if (device && !device->isVirtual()) { |
michael@0 | 571 | ff_effect effect; |
michael@0 | 572 | memset(&effect, 0, sizeof(effect)); |
michael@0 | 573 | effect.type = FF_RUMBLE; |
michael@0 | 574 | effect.id = device->ffEffectId; |
michael@0 | 575 | effect.u.rumble.strong_magnitude = 0xc000; |
michael@0 | 576 | effect.u.rumble.weak_magnitude = 0xc000; |
michael@0 | 577 | effect.replay.length = (duration + 999999LL) / 1000000LL; |
michael@0 | 578 | effect.replay.delay = 0; |
michael@0 | 579 | if (ioctl(device->fd, EVIOCSFF, &effect)) { |
michael@0 | 580 | ALOGW("Could not upload force feedback effect to device %s due to error %d.", |
michael@0 | 581 | device->identifier.name.string(), errno); |
michael@0 | 582 | return; |
michael@0 | 583 | } |
michael@0 | 584 | device->ffEffectId = effect.id; |
michael@0 | 585 | |
michael@0 | 586 | struct input_event ev; |
michael@0 | 587 | ev.time.tv_sec = 0; |
michael@0 | 588 | ev.time.tv_usec = 0; |
michael@0 | 589 | ev.type = EV_FF; |
michael@0 | 590 | ev.code = device->ffEffectId; |
michael@0 | 591 | ev.value = 1; |
michael@0 | 592 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { |
michael@0 | 593 | ALOGW("Could not start force feedback effect on device %s due to error %d.", |
michael@0 | 594 | device->identifier.name.string(), errno); |
michael@0 | 595 | return; |
michael@0 | 596 | } |
michael@0 | 597 | device->ffEffectPlaying = true; |
michael@0 | 598 | } |
michael@0 | 599 | } |
michael@0 | 600 | |
michael@0 | 601 | void EventHub::cancelVibrate(int32_t deviceId) { |
michael@0 | 602 | AutoMutex _l(mLock); |
michael@0 | 603 | Device* device = getDeviceLocked(deviceId); |
michael@0 | 604 | if (device && !device->isVirtual()) { |
michael@0 | 605 | if (device->ffEffectPlaying) { |
michael@0 | 606 | device->ffEffectPlaying = false; |
michael@0 | 607 | |
michael@0 | 608 | struct input_event ev; |
michael@0 | 609 | ev.time.tv_sec = 0; |
michael@0 | 610 | ev.time.tv_usec = 0; |
michael@0 | 611 | ev.type = EV_FF; |
michael@0 | 612 | ev.code = device->ffEffectId; |
michael@0 | 613 | ev.value = 0; |
michael@0 | 614 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { |
michael@0 | 615 | ALOGW("Could not stop force feedback effect on device %s due to error %d.", |
michael@0 | 616 | device->identifier.name.string(), errno); |
michael@0 | 617 | return; |
michael@0 | 618 | } |
michael@0 | 619 | } |
michael@0 | 620 | } |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { |
michael@0 | 624 | if (deviceId == BUILT_IN_KEYBOARD_ID) { |
michael@0 | 625 | deviceId = mBuiltInKeyboardId; |
michael@0 | 626 | } |
michael@0 | 627 | ssize_t index = mDevices.indexOfKey(deviceId); |
michael@0 | 628 | return index >= 0 ? mDevices.valueAt(index) : NULL; |
michael@0 | 629 | } |
michael@0 | 630 | |
michael@0 | 631 | EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const { |
michael@0 | 632 | for (size_t i = 0; i < mDevices.size(); i++) { |
michael@0 | 633 | Device* device = mDevices.valueAt(i); |
michael@0 | 634 | if (device->path == devicePath) { |
michael@0 | 635 | return device; |
michael@0 | 636 | } |
michael@0 | 637 | } |
michael@0 | 638 | return NULL; |
michael@0 | 639 | } |
michael@0 | 640 | |
michael@0 | 641 | size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) { |
michael@0 | 642 | ALOG_ASSERT(bufferSize >= 1); |
michael@0 | 643 | |
michael@0 | 644 | AutoMutex _l(mLock); |
michael@0 | 645 | |
michael@0 | 646 | struct input_event readBuffer[bufferSize]; |
michael@0 | 647 | |
michael@0 | 648 | RawEvent* event = buffer; |
michael@0 | 649 | size_t capacity = bufferSize; |
michael@0 | 650 | bool awoken = false; |
michael@0 | 651 | for (;;) { |
michael@0 | 652 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
michael@0 | 653 | |
michael@0 | 654 | // Reopen input devices if needed. |
michael@0 | 655 | if (mNeedToReopenDevices) { |
michael@0 | 656 | mNeedToReopenDevices = false; |
michael@0 | 657 | |
michael@0 | 658 | ALOGI("Reopening all input devices due to a configuration change."); |
michael@0 | 659 | |
michael@0 | 660 | closeAllDevicesLocked(); |
michael@0 | 661 | mNeedToScanDevices = true; |
michael@0 | 662 | break; // return to the caller before we actually rescan |
michael@0 | 663 | } |
michael@0 | 664 | |
michael@0 | 665 | // Report any devices that had last been added/removed. |
michael@0 | 666 | while (mClosingDevices) { |
michael@0 | 667 | Device* device = mClosingDevices; |
michael@0 | 668 | ALOGV("Reporting device closed: id=%d, name=%s\n", |
michael@0 | 669 | device->id, device->path.string()); |
michael@0 | 670 | mClosingDevices = device->next; |
michael@0 | 671 | event->when = now; |
michael@0 | 672 | event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id; |
michael@0 | 673 | event->type = DEVICE_REMOVED; |
michael@0 | 674 | event += 1; |
michael@0 | 675 | delete device; |
michael@0 | 676 | mNeedToSendFinishedDeviceScan = true; |
michael@0 | 677 | if (--capacity == 0) { |
michael@0 | 678 | break; |
michael@0 | 679 | } |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | if (mNeedToScanDevices) { |
michael@0 | 683 | mNeedToScanDevices = false; |
michael@0 | 684 | scanDevicesLocked(); |
michael@0 | 685 | mNeedToSendFinishedDeviceScan = true; |
michael@0 | 686 | } |
michael@0 | 687 | |
michael@0 | 688 | while (mOpeningDevices != NULL) { |
michael@0 | 689 | Device* device = mOpeningDevices; |
michael@0 | 690 | ALOGV("Reporting device opened: id=%d, name=%s\n", |
michael@0 | 691 | device->id, device->path.string()); |
michael@0 | 692 | mOpeningDevices = device->next; |
michael@0 | 693 | event->when = now; |
michael@0 | 694 | event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
michael@0 | 695 | event->type = DEVICE_ADDED; |
michael@0 | 696 | event += 1; |
michael@0 | 697 | mNeedToSendFinishedDeviceScan = true; |
michael@0 | 698 | if (--capacity == 0) { |
michael@0 | 699 | break; |
michael@0 | 700 | } |
michael@0 | 701 | } |
michael@0 | 702 | |
michael@0 | 703 | if (mNeedToSendFinishedDeviceScan) { |
michael@0 | 704 | mNeedToSendFinishedDeviceScan = false; |
michael@0 | 705 | event->when = now; |
michael@0 | 706 | event->type = FINISHED_DEVICE_SCAN; |
michael@0 | 707 | event += 1; |
michael@0 | 708 | if (--capacity == 0) { |
michael@0 | 709 | break; |
michael@0 | 710 | } |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | // Grab the next input event. |
michael@0 | 714 | bool deviceChanged = false; |
michael@0 | 715 | while (mPendingEventIndex < mPendingEventCount) { |
michael@0 | 716 | const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++]; |
michael@0 | 717 | if (eventItem.data.u32 == EPOLL_ID_INOTIFY) { |
michael@0 | 718 | if (eventItem.events & EPOLLIN) { |
michael@0 | 719 | mPendingINotify = true; |
michael@0 | 720 | } else { |
michael@0 | 721 | ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events); |
michael@0 | 722 | } |
michael@0 | 723 | continue; |
michael@0 | 724 | } |
michael@0 | 725 | |
michael@0 | 726 | if (eventItem.data.u32 == EPOLL_ID_WAKE) { |
michael@0 | 727 | if (eventItem.events & EPOLLIN) { |
michael@0 | 728 | ALOGV("awoken after wake()"); |
michael@0 | 729 | awoken = true; |
michael@0 | 730 | char buffer[16]; |
michael@0 | 731 | ssize_t nRead; |
michael@0 | 732 | do { |
michael@0 | 733 | nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); |
michael@0 | 734 | } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); |
michael@0 | 735 | } else { |
michael@0 | 736 | ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.", |
michael@0 | 737 | eventItem.events); |
michael@0 | 738 | } |
michael@0 | 739 | continue; |
michael@0 | 740 | } |
michael@0 | 741 | |
michael@0 | 742 | ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32); |
michael@0 | 743 | if (deviceIndex < 0) { |
michael@0 | 744 | ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.", |
michael@0 | 745 | eventItem.events, eventItem.data.u32); |
michael@0 | 746 | continue; |
michael@0 | 747 | } |
michael@0 | 748 | |
michael@0 | 749 | Device* device = mDevices.valueAt(deviceIndex); |
michael@0 | 750 | if (eventItem.events & EPOLLIN) { |
michael@0 | 751 | int32_t readSize = read(device->fd, readBuffer, |
michael@0 | 752 | sizeof(struct input_event) * capacity); |
michael@0 | 753 | if (readSize == 0 || (readSize < 0 && errno == ENODEV)) { |
michael@0 | 754 | // Device was removed before INotify noticed. |
michael@0 | 755 | ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d " |
michael@0 | 756 | "capacity: %d errno: %d)\n", |
michael@0 | 757 | device->fd, readSize, bufferSize, capacity, errno); |
michael@0 | 758 | deviceChanged = true; |
michael@0 | 759 | closeDeviceLocked(device); |
michael@0 | 760 | } else if (readSize < 0) { |
michael@0 | 761 | if (errno != EAGAIN && errno != EINTR) { |
michael@0 | 762 | ALOGW("could not get event (errno=%d)", errno); |
michael@0 | 763 | } |
michael@0 | 764 | } else if ((readSize % sizeof(struct input_event)) != 0) { |
michael@0 | 765 | ALOGE("could not get event (wrong size: %d)", readSize); |
michael@0 | 766 | } else { |
michael@0 | 767 | int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
michael@0 | 768 | |
michael@0 | 769 | size_t count = size_t(readSize) / sizeof(struct input_event); |
michael@0 | 770 | for (size_t i = 0; i < count; i++) { |
michael@0 | 771 | struct input_event& iev = readBuffer[i]; |
michael@0 | 772 | ALOGV("%s got: time=%d.%06d, type=%d, code=%d, value=%d", |
michael@0 | 773 | device->path.string(), |
michael@0 | 774 | (int) iev.time.tv_sec, (int) iev.time.tv_usec, |
michael@0 | 775 | iev.type, iev.code, iev.value); |
michael@0 | 776 | |
michael@0 | 777 | // Some input devices may have a better concept of the time |
michael@0 | 778 | // when an input event was actually generated than the kernel |
michael@0 | 779 | // which simply timestamps all events on entry to evdev. |
michael@0 | 780 | // This is a custom Android extension of the input protocol |
michael@0 | 781 | // mainly intended for use with uinput based device drivers. |
michael@0 | 782 | if (iev.type == EV_MSC) { |
michael@0 | 783 | if (iev.code == MSC_ANDROID_TIME_SEC) { |
michael@0 | 784 | device->timestampOverrideSec = iev.value; |
michael@0 | 785 | continue; |
michael@0 | 786 | } else if (iev.code == MSC_ANDROID_TIME_USEC) { |
michael@0 | 787 | device->timestampOverrideUsec = iev.value; |
michael@0 | 788 | continue; |
michael@0 | 789 | } |
michael@0 | 790 | } |
michael@0 | 791 | if (device->timestampOverrideSec || device->timestampOverrideUsec) { |
michael@0 | 792 | iev.time.tv_sec = device->timestampOverrideSec; |
michael@0 | 793 | iev.time.tv_usec = device->timestampOverrideUsec; |
michael@0 | 794 | if (iev.type == EV_SYN && iev.code == SYN_REPORT) { |
michael@0 | 795 | device->timestampOverrideSec = 0; |
michael@0 | 796 | device->timestampOverrideUsec = 0; |
michael@0 | 797 | } |
michael@0 | 798 | ALOGV("applied override time %d.%06d", |
michael@0 | 799 | int(iev.time.tv_sec), int(iev.time.tv_usec)); |
michael@0 | 800 | } |
michael@0 | 801 | |
michael@0 | 802 | #ifdef HAVE_POSIX_CLOCKS |
michael@0 | 803 | // Use the time specified in the event instead of the current time |
michael@0 | 804 | // so that downstream code can get more accurate estimates of |
michael@0 | 805 | // event dispatch latency from the time the event is enqueued onto |
michael@0 | 806 | // the evdev client buffer. |
michael@0 | 807 | // |
michael@0 | 808 | // The event's timestamp fortuitously uses the same monotonic clock |
michael@0 | 809 | // time base as the rest of Android. The kernel event device driver |
michael@0 | 810 | // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts(). |
michael@0 | 811 | // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere |
michael@0 | 812 | // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a |
michael@0 | 813 | // system call that also queries ktime_get_ts(). |
michael@0 | 814 | event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL |
michael@0 | 815 | + nsecs_t(iev.time.tv_usec) * 1000LL; |
michael@0 | 816 | ALOGV("event time %lld, now %lld", event->when, now); |
michael@0 | 817 | |
michael@0 | 818 | // Bug 7291243: Add a guard in case the kernel generates timestamps |
michael@0 | 819 | // that appear to be far into the future because they were generated |
michael@0 | 820 | // using the wrong clock source. |
michael@0 | 821 | // |
michael@0 | 822 | // This can happen because when the input device is initially opened |
michael@0 | 823 | // it has a default clock source of CLOCK_REALTIME. Any input events |
michael@0 | 824 | // enqueued right after the device is opened will have timestamps |
michael@0 | 825 | // generated using CLOCK_REALTIME. We later set the clock source |
michael@0 | 826 | // to CLOCK_MONOTONIC but it is already too late. |
michael@0 | 827 | // |
michael@0 | 828 | // Invalid input event timestamps can result in ANRs, crashes and |
michael@0 | 829 | // and other issues that are hard to track down. We must not let them |
michael@0 | 830 | // propagate through the system. |
michael@0 | 831 | // |
michael@0 | 832 | // Log a warning so that we notice the problem and recover gracefully. |
michael@0 | 833 | if (event->when >= now + 10 * 1000000000LL) { |
michael@0 | 834 | // Double-check. Time may have moved on. |
michael@0 | 835 | nsecs_t time = systemTime(SYSTEM_TIME_MONOTONIC); |
michael@0 | 836 | if (event->when > time) { |
michael@0 | 837 | ALOGW("An input event from %s has a timestamp that appears to " |
michael@0 | 838 | "have been generated using the wrong clock source " |
michael@0 | 839 | "(expected CLOCK_MONOTONIC): " |
michael@0 | 840 | "event time %lld, current time %lld, call time %lld. " |
michael@0 | 841 | "Using current time instead.", |
michael@0 | 842 | device->path.string(), event->when, time, now); |
michael@0 | 843 | event->when = time; |
michael@0 | 844 | } else { |
michael@0 | 845 | ALOGV("Event time is ok but failed the fast path and required " |
michael@0 | 846 | "an extra call to systemTime: " |
michael@0 | 847 | "event time %lld, current time %lld, call time %lld.", |
michael@0 | 848 | event->when, time, now); |
michael@0 | 849 | } |
michael@0 | 850 | } |
michael@0 | 851 | #else |
michael@0 | 852 | event->when = now; |
michael@0 | 853 | #endif |
michael@0 | 854 | event->deviceId = deviceId; |
michael@0 | 855 | event->type = iev.type; |
michael@0 | 856 | event->code = iev.code; |
michael@0 | 857 | event->value = iev.value; |
michael@0 | 858 | event += 1; |
michael@0 | 859 | capacity -= 1; |
michael@0 | 860 | } |
michael@0 | 861 | if (capacity == 0) { |
michael@0 | 862 | // The result buffer is full. Reset the pending event index |
michael@0 | 863 | // so we will try to read the device again on the next iteration. |
michael@0 | 864 | mPendingEventIndex -= 1; |
michael@0 | 865 | break; |
michael@0 | 866 | } |
michael@0 | 867 | } |
michael@0 | 868 | } else if (eventItem.events & EPOLLHUP) { |
michael@0 | 869 | ALOGI("Removing device %s due to epoll hang-up event.", |
michael@0 | 870 | device->identifier.name.string()); |
michael@0 | 871 | deviceChanged = true; |
michael@0 | 872 | closeDeviceLocked(device); |
michael@0 | 873 | } else { |
michael@0 | 874 | ALOGW("Received unexpected epoll event 0x%08x for device %s.", |
michael@0 | 875 | eventItem.events, device->identifier.name.string()); |
michael@0 | 876 | } |
michael@0 | 877 | } |
michael@0 | 878 | |
michael@0 | 879 | // readNotify() will modify the list of devices so this must be done after |
michael@0 | 880 | // processing all other events to ensure that we read all remaining events |
michael@0 | 881 | // before closing the devices. |
michael@0 | 882 | if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) { |
michael@0 | 883 | mPendingINotify = false; |
michael@0 | 884 | readNotifyLocked(); |
michael@0 | 885 | deviceChanged = true; |
michael@0 | 886 | } |
michael@0 | 887 | |
michael@0 | 888 | // Report added or removed devices immediately. |
michael@0 | 889 | if (deviceChanged) { |
michael@0 | 890 | continue; |
michael@0 | 891 | } |
michael@0 | 892 | |
michael@0 | 893 | // Return now if we have collected any events or if we were explicitly awoken. |
michael@0 | 894 | if (event != buffer || awoken) { |
michael@0 | 895 | break; |
michael@0 | 896 | } |
michael@0 | 897 | |
michael@0 | 898 | // Poll for events. Mind the wake lock dance! |
michael@0 | 899 | // We hold a wake lock at all times except during epoll_wait(). This works due to some |
michael@0 | 900 | // subtle choreography. When a device driver has pending (unread) events, it acquires |
michael@0 | 901 | // a kernel wake lock. However, once the last pending event has been read, the device |
michael@0 | 902 | // driver will release the kernel wake lock. To prevent the system from going to sleep |
michael@0 | 903 | // when this happens, the EventHub holds onto its own user wake lock while the client |
michael@0 | 904 | // is processing events. Thus the system can only sleep if there are no events |
michael@0 | 905 | // pending or currently being processed. |
michael@0 | 906 | // |
michael@0 | 907 | // The timeout is advisory only. If the device is asleep, it will not wake just to |
michael@0 | 908 | // service the timeout. |
michael@0 | 909 | mPendingEventIndex = 0; |
michael@0 | 910 | |
michael@0 | 911 | mLock.unlock(); // release lock before poll, must be before release_wake_lock |
michael@0 | 912 | release_wake_lock(WAKE_LOCK_ID); |
michael@0 | 913 | |
michael@0 | 914 | int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis); |
michael@0 | 915 | |
michael@0 | 916 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
michael@0 | 917 | mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock |
michael@0 | 918 | |
michael@0 | 919 | if (pollResult == 0) { |
michael@0 | 920 | // Timed out. |
michael@0 | 921 | mPendingEventCount = 0; |
michael@0 | 922 | break; |
michael@0 | 923 | } |
michael@0 | 924 | |
michael@0 | 925 | if (pollResult < 0) { |
michael@0 | 926 | // An error occurred. |
michael@0 | 927 | mPendingEventCount = 0; |
michael@0 | 928 | |
michael@0 | 929 | // Sleep after errors to avoid locking up the system. |
michael@0 | 930 | // Hopefully the error is transient. |
michael@0 | 931 | if (errno != EINTR) { |
michael@0 | 932 | ALOGW("poll failed (errno=%d)\n", errno); |
michael@0 | 933 | usleep(100000); |
michael@0 | 934 | } |
michael@0 | 935 | } else { |
michael@0 | 936 | // Some events occurred. |
michael@0 | 937 | mPendingEventCount = size_t(pollResult); |
michael@0 | 938 | } |
michael@0 | 939 | } |
michael@0 | 940 | |
michael@0 | 941 | // All done, return the number of events we read. |
michael@0 | 942 | return event - buffer; |
michael@0 | 943 | } |
michael@0 | 944 | |
michael@0 | 945 | void EventHub::wake() { |
michael@0 | 946 | ALOGV("wake() called"); |
michael@0 | 947 | |
michael@0 | 948 | ssize_t nWrite; |
michael@0 | 949 | do { |
michael@0 | 950 | nWrite = write(mWakeWritePipeFd, "W", 1); |
michael@0 | 951 | } while (nWrite == -1 && errno == EINTR); |
michael@0 | 952 | |
michael@0 | 953 | if (nWrite != 1 && errno != EAGAIN) { |
michael@0 | 954 | ALOGW("Could not write wake signal, errno=%d", errno); |
michael@0 | 955 | } |
michael@0 | 956 | } |
michael@0 | 957 | |
michael@0 | 958 | void EventHub::scanDevicesLocked() { |
michael@0 | 959 | status_t res = scanDirLocked(DEVICE_PATH); |
michael@0 | 960 | if(res < 0) { |
michael@0 | 961 | ALOGE("scan dir failed for %s\n", DEVICE_PATH); |
michael@0 | 962 | } |
michael@0 | 963 | if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) { |
michael@0 | 964 | createVirtualKeyboardLocked(); |
michael@0 | 965 | } |
michael@0 | 966 | } |
michael@0 | 967 | |
michael@0 | 968 | // ---------------------------------------------------------------------------- |
michael@0 | 969 | |
michael@0 | 970 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { |
michael@0 | 971 | const uint8_t* end = array + endIndex; |
michael@0 | 972 | array += startIndex; |
michael@0 | 973 | while (array != end) { |
michael@0 | 974 | if (*(array++) != 0) { |
michael@0 | 975 | return true; |
michael@0 | 976 | } |
michael@0 | 977 | } |
michael@0 | 978 | return false; |
michael@0 | 979 | } |
michael@0 | 980 | |
michael@0 | 981 | static const int32_t GAMEPAD_KEYCODES[] = { |
michael@0 | 982 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, |
michael@0 | 983 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, |
michael@0 | 984 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, |
michael@0 | 985 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, |
michael@0 | 986 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, |
michael@0 | 987 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, |
michael@0 | 988 | AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4, |
michael@0 | 989 | AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8, |
michael@0 | 990 | AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12, |
michael@0 | 991 | AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16, |
michael@0 | 992 | }; |
michael@0 | 993 | |
michael@0 | 994 | status_t EventHub::openDeviceLocked(const char *devicePath) { |
michael@0 | 995 | char buffer[80]; |
michael@0 | 996 | |
michael@0 | 997 | ALOGV("Opening device: %s", devicePath); |
michael@0 | 998 | |
michael@0 | 999 | int fd = open(devicePath, O_RDWR | O_CLOEXEC); |
michael@0 | 1000 | if(fd < 0) { |
michael@0 | 1001 | ALOGE("could not open %s, %s\n", devicePath, strerror(errno)); |
michael@0 | 1002 | return -1; |
michael@0 | 1003 | } |
michael@0 | 1004 | |
michael@0 | 1005 | InputDeviceIdentifier identifier; |
michael@0 | 1006 | |
michael@0 | 1007 | // Get device name. |
michael@0 | 1008 | if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { |
michael@0 | 1009 | //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno)); |
michael@0 | 1010 | } else { |
michael@0 | 1011 | buffer[sizeof(buffer) - 1] = '\0'; |
michael@0 | 1012 | identifier.name.setTo(buffer); |
michael@0 | 1013 | } |
michael@0 | 1014 | |
michael@0 | 1015 | // Check to see if the device is on our excluded list |
michael@0 | 1016 | for (size_t i = 0; i < mExcludedDevices.size(); i++) { |
michael@0 | 1017 | const String8& item = mExcludedDevices.itemAt(i); |
michael@0 | 1018 | if (identifier.name == item) { |
michael@0 | 1019 | ALOGI("ignoring event id %s driver %s\n", devicePath, item.string()); |
michael@0 | 1020 | close(fd); |
michael@0 | 1021 | return -1; |
michael@0 | 1022 | } |
michael@0 | 1023 | } |
michael@0 | 1024 | |
michael@0 | 1025 | // Get device driver version. |
michael@0 | 1026 | int driverVersion; |
michael@0 | 1027 | if(ioctl(fd, EVIOCGVERSION, &driverVersion)) { |
michael@0 | 1028 | ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno)); |
michael@0 | 1029 | close(fd); |
michael@0 | 1030 | return -1; |
michael@0 | 1031 | } |
michael@0 | 1032 | |
michael@0 | 1033 | // Get device identifier. |
michael@0 | 1034 | struct input_id inputId; |
michael@0 | 1035 | if(ioctl(fd, EVIOCGID, &inputId)) { |
michael@0 | 1036 | ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno)); |
michael@0 | 1037 | close(fd); |
michael@0 | 1038 | return -1; |
michael@0 | 1039 | } |
michael@0 | 1040 | identifier.bus = inputId.bustype; |
michael@0 | 1041 | identifier.product = inputId.product; |
michael@0 | 1042 | identifier.vendor = inputId.vendor; |
michael@0 | 1043 | identifier.version = inputId.version; |
michael@0 | 1044 | |
michael@0 | 1045 | // Get device physical location. |
michael@0 | 1046 | if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { |
michael@0 | 1047 | //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); |
michael@0 | 1048 | } else { |
michael@0 | 1049 | buffer[sizeof(buffer) - 1] = '\0'; |
michael@0 | 1050 | identifier.location.setTo(buffer); |
michael@0 | 1051 | } |
michael@0 | 1052 | |
michael@0 | 1053 | // Get device unique id. |
michael@0 | 1054 | if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { |
michael@0 | 1055 | //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); |
michael@0 | 1056 | } else { |
michael@0 | 1057 | buffer[sizeof(buffer) - 1] = '\0'; |
michael@0 | 1058 | identifier.uniqueId.setTo(buffer); |
michael@0 | 1059 | } |
michael@0 | 1060 | |
michael@0 | 1061 | // Fill in the descriptor. |
michael@0 | 1062 | setDescriptor(identifier); |
michael@0 | 1063 | |
michael@0 | 1064 | // Make file descriptor non-blocking for use with poll(). |
michael@0 | 1065 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
michael@0 | 1066 | ALOGE("Error %d making device file descriptor non-blocking.", errno); |
michael@0 | 1067 | close(fd); |
michael@0 | 1068 | return -1; |
michael@0 | 1069 | } |
michael@0 | 1070 | |
michael@0 | 1071 | // Allocate device. (The device object takes ownership of the fd at this point.) |
michael@0 | 1072 | int32_t deviceId = mNextDeviceId++; |
michael@0 | 1073 | Device* device = new Device(fd, deviceId, String8(devicePath), identifier); |
michael@0 | 1074 | |
michael@0 | 1075 | ALOGV("add device %d: %s\n", deviceId, devicePath); |
michael@0 | 1076 | ALOGV(" bus: %04x\n" |
michael@0 | 1077 | " vendor %04x\n" |
michael@0 | 1078 | " product %04x\n" |
michael@0 | 1079 | " version %04x\n", |
michael@0 | 1080 | identifier.bus, identifier.vendor, identifier.product, identifier.version); |
michael@0 | 1081 | ALOGV(" name: \"%s\"\n", identifier.name.string()); |
michael@0 | 1082 | ALOGV(" location: \"%s\"\n", identifier.location.string()); |
michael@0 | 1083 | ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string()); |
michael@0 | 1084 | ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string()); |
michael@0 | 1085 | ALOGV(" driver: v%d.%d.%d\n", |
michael@0 | 1086 | driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff); |
michael@0 | 1087 | |
michael@0 | 1088 | // Load the configuration file for the device. |
michael@0 | 1089 | loadConfigurationLocked(device); |
michael@0 | 1090 | |
michael@0 | 1091 | // Figure out the kinds of events the device reports. |
michael@0 | 1092 | ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask); |
michael@0 | 1093 | ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask); |
michael@0 | 1094 | ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask); |
michael@0 | 1095 | ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask); |
michael@0 | 1096 | ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask); |
michael@0 | 1097 | ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask); |
michael@0 | 1098 | ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask); |
michael@0 | 1099 | |
michael@0 | 1100 | // See if this is a keyboard. Ignore everything in the button range except for |
michael@0 | 1101 | // joystick and gamepad buttons which are handled like keyboards for the most part. |
michael@0 | 1102 | bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC)) |
michael@0 | 1103 | || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK), |
michael@0 | 1104 | sizeof_bit_array(KEY_MAX + 1)); |
michael@0 | 1105 | bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC), |
michael@0 | 1106 | sizeof_bit_array(BTN_MOUSE)) |
michael@0 | 1107 | || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK), |
michael@0 | 1108 | sizeof_bit_array(BTN_DIGI)); |
michael@0 | 1109 | if (haveKeyboardKeys || haveGamepadButtons) { |
michael@0 | 1110 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
michael@0 | 1111 | } |
michael@0 | 1112 | |
michael@0 | 1113 | // See if this is a cursor device such as a trackball or mouse. |
michael@0 | 1114 | if (test_bit(BTN_MOUSE, device->keyBitmask) |
michael@0 | 1115 | && test_bit(REL_X, device->relBitmask) |
michael@0 | 1116 | && test_bit(REL_Y, device->relBitmask)) { |
michael@0 | 1117 | device->classes |= INPUT_DEVICE_CLASS_CURSOR; |
michael@0 | 1118 | } |
michael@0 | 1119 | |
michael@0 | 1120 | // See if this is a touch pad. |
michael@0 | 1121 | // Is this a new modern multi-touch driver? |
michael@0 | 1122 | if (test_bit(ABS_MT_POSITION_X, device->absBitmask) |
michael@0 | 1123 | && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) { |
michael@0 | 1124 | // Some joysticks such as the PS3 controller report axes that conflict |
michael@0 | 1125 | // with the ABS_MT range. Try to confirm that the device really is |
michael@0 | 1126 | // a touch screen. |
michael@0 | 1127 | if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) { |
michael@0 | 1128 | device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT; |
michael@0 | 1129 | } |
michael@0 | 1130 | // Is this an old style single-touch driver? |
michael@0 | 1131 | } else if (test_bit(BTN_TOUCH, device->keyBitmask) |
michael@0 | 1132 | && test_bit(ABS_X, device->absBitmask) |
michael@0 | 1133 | && test_bit(ABS_Y, device->absBitmask)) { |
michael@0 | 1134 | device->classes |= INPUT_DEVICE_CLASS_TOUCH; |
michael@0 | 1135 | } |
michael@0 | 1136 | |
michael@0 | 1137 | // See if this device is a joystick. |
michael@0 | 1138 | // Assumes that joysticks always have gamepad buttons in order to distinguish them |
michael@0 | 1139 | // from other devices such as accelerometers that also have absolute axes. |
michael@0 | 1140 | if (haveGamepadButtons) { |
michael@0 | 1141 | uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK; |
michael@0 | 1142 | for (int i = 0; i <= ABS_MAX; i++) { |
michael@0 | 1143 | if (test_bit(i, device->absBitmask) |
michael@0 | 1144 | && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) { |
michael@0 | 1145 | device->classes = assumedClasses; |
michael@0 | 1146 | break; |
michael@0 | 1147 | } |
michael@0 | 1148 | } |
michael@0 | 1149 | } |
michael@0 | 1150 | |
michael@0 | 1151 | // Check whether this device has switches. |
michael@0 | 1152 | for (int i = 0; i <= SW_MAX; i++) { |
michael@0 | 1153 | if (test_bit(i, device->swBitmask)) { |
michael@0 | 1154 | device->classes |= INPUT_DEVICE_CLASS_SWITCH; |
michael@0 | 1155 | break; |
michael@0 | 1156 | } |
michael@0 | 1157 | } |
michael@0 | 1158 | |
michael@0 | 1159 | // Check whether this device supports the vibrator. |
michael@0 | 1160 | if (test_bit(FF_RUMBLE, device->ffBitmask)) { |
michael@0 | 1161 | device->classes |= INPUT_DEVICE_CLASS_VIBRATOR; |
michael@0 | 1162 | } |
michael@0 | 1163 | |
michael@0 | 1164 | // Configure virtual keys. |
michael@0 | 1165 | if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) { |
michael@0 | 1166 | // Load the virtual keys for the touch screen, if any. |
michael@0 | 1167 | // We do this now so that we can make sure to load the keymap if necessary. |
michael@0 | 1168 | status_t status = loadVirtualKeyMapLocked(device); |
michael@0 | 1169 | if (!status) { |
michael@0 | 1170 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
michael@0 | 1171 | } |
michael@0 | 1172 | } |
michael@0 | 1173 | |
michael@0 | 1174 | // Load the key map. |
michael@0 | 1175 | // We need to do this for joysticks too because the key layout may specify axes. |
michael@0 | 1176 | status_t keyMapStatus = NAME_NOT_FOUND; |
michael@0 | 1177 | if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) { |
michael@0 | 1178 | // Load the keymap for the device. |
michael@0 | 1179 | keyMapStatus = loadKeyMapLocked(device); |
michael@0 | 1180 | } |
michael@0 | 1181 | |
michael@0 | 1182 | // Configure the keyboard, gamepad or virtual keyboard. |
michael@0 | 1183 | if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
michael@0 | 1184 | // Register the keyboard as a built-in keyboard if it is eligible. |
michael@0 | 1185 | if (!keyMapStatus |
michael@0 | 1186 | && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD |
michael@0 | 1187 | && isEligibleBuiltInKeyboard(device->identifier, |
michael@0 | 1188 | device->configuration, &device->keyMap)) { |
michael@0 | 1189 | mBuiltInKeyboardId = device->id; |
michael@0 | 1190 | } |
michael@0 | 1191 | |
michael@0 | 1192 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd |
michael@0 | 1193 | if (hasKeycodeLocked(device, AKEYCODE_Q)) { |
michael@0 | 1194 | device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; |
michael@0 | 1195 | } |
michael@0 | 1196 | |
michael@0 | 1197 | // See if this device has a DPAD. |
michael@0 | 1198 | if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) && |
michael@0 | 1199 | hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) && |
michael@0 | 1200 | hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) && |
michael@0 | 1201 | hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) && |
michael@0 | 1202 | hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) { |
michael@0 | 1203 | device->classes |= INPUT_DEVICE_CLASS_DPAD; |
michael@0 | 1204 | } |
michael@0 | 1205 | |
michael@0 | 1206 | // See if this device has a gamepad. |
michael@0 | 1207 | for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) { |
michael@0 | 1208 | if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) { |
michael@0 | 1209 | device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; |
michael@0 | 1210 | break; |
michael@0 | 1211 | } |
michael@0 | 1212 | } |
michael@0 | 1213 | |
michael@0 | 1214 | // Disable kernel key repeat since we handle it ourselves |
michael@0 | 1215 | unsigned int repeatRate[] = {0,0}; |
michael@0 | 1216 | if (ioctl(fd, EVIOCSREP, repeatRate)) { |
michael@0 | 1217 | ALOGW("Unable to disable kernel key repeat for %s: %s", devicePath, strerror(errno)); |
michael@0 | 1218 | } |
michael@0 | 1219 | } |
michael@0 | 1220 | |
michael@0 | 1221 | // If the device isn't recognized as something we handle, don't monitor it. |
michael@0 | 1222 | if (device->classes == 0) { |
michael@0 | 1223 | ALOGV("Dropping device: id=%d, path='%s', name='%s'", |
michael@0 | 1224 | deviceId, devicePath, device->identifier.name.string()); |
michael@0 | 1225 | delete device; |
michael@0 | 1226 | return -1; |
michael@0 | 1227 | } |
michael@0 | 1228 | |
michael@0 | 1229 | // Determine whether the device is external or internal. |
michael@0 | 1230 | if (isExternalDeviceLocked(device)) { |
michael@0 | 1231 | device->classes |= INPUT_DEVICE_CLASS_EXTERNAL; |
michael@0 | 1232 | } |
michael@0 | 1233 | |
michael@0 | 1234 | // Register with epoll. |
michael@0 | 1235 | struct epoll_event eventItem; |
michael@0 | 1236 | memset(&eventItem, 0, sizeof(eventItem)); |
michael@0 | 1237 | eventItem.events = EPOLLIN; |
michael@0 | 1238 | eventItem.data.u32 = deviceId; |
michael@0 | 1239 | if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) { |
michael@0 | 1240 | ALOGE("Could not add device fd to epoll instance. errno=%d", errno); |
michael@0 | 1241 | delete device; |
michael@0 | 1242 | return -1; |
michael@0 | 1243 | } |
michael@0 | 1244 | |
michael@0 | 1245 | // Enable wake-lock behavior on kernels that support it. |
michael@0 | 1246 | // TODO: Only need this for devices that can really wake the system. |
michael@0 | 1247 | bool usingSuspendBlockIoctl = !ioctl(fd, EVIOCSSUSPENDBLOCK, 1); |
michael@0 | 1248 | |
michael@0 | 1249 | // Tell the kernel that we want to use the monotonic clock for reporting timestamps |
michael@0 | 1250 | // associated with input events. This is important because the input system |
michael@0 | 1251 | // uses the timestamps extensively and assumes they were recorded using the monotonic |
michael@0 | 1252 | // clock. |
michael@0 | 1253 | // |
michael@0 | 1254 | // In older kernel, before Linux 3.4, there was no way to tell the kernel which |
michael@0 | 1255 | // clock to use to input event timestamps. The standard kernel behavior was to |
michael@0 | 1256 | // record a real time timestamp, which isn't what we want. Android kernels therefore |
michael@0 | 1257 | // contained a patch to the evdev_event() function in drivers/input/evdev.c to |
michael@0 | 1258 | // replace the call to do_gettimeofday() with ktime_get_ts() to cause the monotonic |
michael@0 | 1259 | // clock to be used instead of the real time clock. |
michael@0 | 1260 | // |
michael@0 | 1261 | // As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock. |
michael@0 | 1262 | // Therefore, we no longer require the Android-specific kernel patch described above |
michael@0 | 1263 | // as long as we make sure to set select the monotonic clock. We do that here. |
michael@0 | 1264 | int clockId = CLOCK_MONOTONIC; |
michael@0 | 1265 | bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId); |
michael@0 | 1266 | |
michael@0 | 1267 | ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, " |
michael@0 | 1268 | "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, " |
michael@0 | 1269 | "usingSuspendBlockIoctl=%s, usingClockIoctl=%s", |
michael@0 | 1270 | deviceId, fd, devicePath, device->identifier.name.string(), |
michael@0 | 1271 | device->classes, |
michael@0 | 1272 | device->configurationFile.string(), |
michael@0 | 1273 | device->keyMap.keyLayoutFile.string(), |
michael@0 | 1274 | device->keyMap.keyCharacterMapFile.string(), |
michael@0 | 1275 | toString(mBuiltInKeyboardId == deviceId), |
michael@0 | 1276 | toString(usingSuspendBlockIoctl), toString(usingClockIoctl)); |
michael@0 | 1277 | |
michael@0 | 1278 | addDeviceLocked(device); |
michael@0 | 1279 | return 0; |
michael@0 | 1280 | } |
michael@0 | 1281 | |
michael@0 | 1282 | void EventHub::createVirtualKeyboardLocked() { |
michael@0 | 1283 | InputDeviceIdentifier identifier; |
michael@0 | 1284 | identifier.name = "Virtual"; |
michael@0 | 1285 | identifier.uniqueId = "<virtual>"; |
michael@0 | 1286 | setDescriptor(identifier); |
michael@0 | 1287 | |
michael@0 | 1288 | Device* device = new Device(-1, VIRTUAL_KEYBOARD_ID, String8("<virtual>"), identifier); |
michael@0 | 1289 | device->classes = INPUT_DEVICE_CLASS_KEYBOARD |
michael@0 | 1290 | | INPUT_DEVICE_CLASS_ALPHAKEY |
michael@0 | 1291 | | INPUT_DEVICE_CLASS_DPAD |
michael@0 | 1292 | | INPUT_DEVICE_CLASS_VIRTUAL; |
michael@0 | 1293 | loadKeyMapLocked(device); |
michael@0 | 1294 | addDeviceLocked(device); |
michael@0 | 1295 | } |
michael@0 | 1296 | |
michael@0 | 1297 | void EventHub::addDeviceLocked(Device* device) { |
michael@0 | 1298 | mDevices.add(device->id, device); |
michael@0 | 1299 | device->next = mOpeningDevices; |
michael@0 | 1300 | mOpeningDevices = device; |
michael@0 | 1301 | } |
michael@0 | 1302 | |
michael@0 | 1303 | void EventHub::loadConfigurationLocked(Device* device) { |
michael@0 | 1304 | device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier( |
michael@0 | 1305 | device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION); |
michael@0 | 1306 | if (device->configurationFile.isEmpty()) { |
michael@0 | 1307 | ALOGD("No input device configuration file found for device '%s'.", |
michael@0 | 1308 | device->identifier.name.string()); |
michael@0 | 1309 | } else { |
michael@0 | 1310 | status_t status = PropertyMap::load(device->configurationFile, |
michael@0 | 1311 | &device->configuration); |
michael@0 | 1312 | if (status) { |
michael@0 | 1313 | ALOGE("Error loading input device configuration file for device '%s'. " |
michael@0 | 1314 | "Using default configuration.", |
michael@0 | 1315 | device->identifier.name.string()); |
michael@0 | 1316 | } |
michael@0 | 1317 | } |
michael@0 | 1318 | } |
michael@0 | 1319 | |
michael@0 | 1320 | status_t EventHub::loadVirtualKeyMapLocked(Device* device) { |
michael@0 | 1321 | // The virtual key map is supplied by the kernel as a system board property file. |
michael@0 | 1322 | String8 path; |
michael@0 | 1323 | path.append("/sys/board_properties/virtualkeys."); |
michael@0 | 1324 | path.append(device->identifier.name); |
michael@0 | 1325 | if (access(path.string(), R_OK)) { |
michael@0 | 1326 | return NAME_NOT_FOUND; |
michael@0 | 1327 | } |
michael@0 | 1328 | return VirtualKeyMap::load(path, &device->virtualKeyMap); |
michael@0 | 1329 | } |
michael@0 | 1330 | |
michael@0 | 1331 | status_t EventHub::loadKeyMapLocked(Device* device) { |
michael@0 | 1332 | return device->keyMap.load(device->identifier, device->configuration); |
michael@0 | 1333 | } |
michael@0 | 1334 | |
michael@0 | 1335 | bool EventHub::isExternalDeviceLocked(Device* device) { |
michael@0 | 1336 | if (device->configuration) { |
michael@0 | 1337 | bool value; |
michael@0 | 1338 | if (device->configuration->tryGetProperty(String8("device.internal"), value)) { |
michael@0 | 1339 | return !value; |
michael@0 | 1340 | } |
michael@0 | 1341 | } |
michael@0 | 1342 | return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH; |
michael@0 | 1343 | } |
michael@0 | 1344 | |
michael@0 | 1345 | bool EventHub::hasKeycodeLocked(Device* device, int keycode) const { |
michael@0 | 1346 | if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { |
michael@0 | 1347 | return false; |
michael@0 | 1348 | } |
michael@0 | 1349 | |
michael@0 | 1350 | Vector<int32_t> scanCodes; |
michael@0 | 1351 | device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes); |
michael@0 | 1352 | const size_t N = scanCodes.size(); |
michael@0 | 1353 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
michael@0 | 1354 | int32_t sc = scanCodes.itemAt(i); |
michael@0 | 1355 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { |
michael@0 | 1356 | return true; |
michael@0 | 1357 | } |
michael@0 | 1358 | } |
michael@0 | 1359 | |
michael@0 | 1360 | return false; |
michael@0 | 1361 | } |
michael@0 | 1362 | |
michael@0 | 1363 | status_t EventHub::closeDeviceByPathLocked(const char *devicePath) { |
michael@0 | 1364 | Device* device = getDeviceByPathLocked(devicePath); |
michael@0 | 1365 | if (device) { |
michael@0 | 1366 | closeDeviceLocked(device); |
michael@0 | 1367 | return 0; |
michael@0 | 1368 | } |
michael@0 | 1369 | ALOGV("Remove device: %s not found, device may already have been removed.", devicePath); |
michael@0 | 1370 | return -1; |
michael@0 | 1371 | } |
michael@0 | 1372 | |
michael@0 | 1373 | void EventHub::closeAllDevicesLocked() { |
michael@0 | 1374 | while (mDevices.size() > 0) { |
michael@0 | 1375 | closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1)); |
michael@0 | 1376 | } |
michael@0 | 1377 | } |
michael@0 | 1378 | |
michael@0 | 1379 | void EventHub::closeDeviceLocked(Device* device) { |
michael@0 | 1380 | ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n", |
michael@0 | 1381 | device->path.string(), device->identifier.name.string(), device->id, |
michael@0 | 1382 | device->fd, device->classes); |
michael@0 | 1383 | |
michael@0 | 1384 | if (device->id == mBuiltInKeyboardId) { |
michael@0 | 1385 | ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", |
michael@0 | 1386 | device->path.string(), mBuiltInKeyboardId); |
michael@0 | 1387 | mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD; |
michael@0 | 1388 | } |
michael@0 | 1389 | |
michael@0 | 1390 | if (!device->isVirtual()) { |
michael@0 | 1391 | if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) { |
michael@0 | 1392 | ALOGW("Could not remove device fd from epoll instance. errno=%d", errno); |
michael@0 | 1393 | } |
michael@0 | 1394 | } |
michael@0 | 1395 | |
michael@0 | 1396 | mDevices.removeItem(device->id); |
michael@0 | 1397 | device->close(); |
michael@0 | 1398 | |
michael@0 | 1399 | // Unlink for opening devices list if it is present. |
michael@0 | 1400 | Device* pred = NULL; |
michael@0 | 1401 | bool found = false; |
michael@0 | 1402 | for (Device* entry = mOpeningDevices; entry != NULL; ) { |
michael@0 | 1403 | if (entry == device) { |
michael@0 | 1404 | found = true; |
michael@0 | 1405 | break; |
michael@0 | 1406 | } |
michael@0 | 1407 | pred = entry; |
michael@0 | 1408 | entry = entry->next; |
michael@0 | 1409 | } |
michael@0 | 1410 | if (found) { |
michael@0 | 1411 | // Unlink the device from the opening devices list then delete it. |
michael@0 | 1412 | // We don't need to tell the client that the device was closed because |
michael@0 | 1413 | // it does not even know it was opened in the first place. |
michael@0 | 1414 | ALOGI("Device %s was immediately closed after opening.", device->path.string()); |
michael@0 | 1415 | if (pred) { |
michael@0 | 1416 | pred->next = device->next; |
michael@0 | 1417 | } else { |
michael@0 | 1418 | mOpeningDevices = device->next; |
michael@0 | 1419 | } |
michael@0 | 1420 | delete device; |
michael@0 | 1421 | } else { |
michael@0 | 1422 | // Link into closing devices list. |
michael@0 | 1423 | // The device will be deleted later after we have informed the client. |
michael@0 | 1424 | device->next = mClosingDevices; |
michael@0 | 1425 | mClosingDevices = device; |
michael@0 | 1426 | } |
michael@0 | 1427 | } |
michael@0 | 1428 | |
michael@0 | 1429 | status_t EventHub::readNotifyLocked() { |
michael@0 | 1430 | int res; |
michael@0 | 1431 | char devname[PATH_MAX]; |
michael@0 | 1432 | char *filename; |
michael@0 | 1433 | char event_buf[512]; |
michael@0 | 1434 | int event_size; |
michael@0 | 1435 | int event_pos = 0; |
michael@0 | 1436 | struct inotify_event *event; |
michael@0 | 1437 | |
michael@0 | 1438 | ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd); |
michael@0 | 1439 | res = read(mINotifyFd, event_buf, sizeof(event_buf)); |
michael@0 | 1440 | if(res < (int)sizeof(*event)) { |
michael@0 | 1441 | if(errno == EINTR) |
michael@0 | 1442 | return 0; |
michael@0 | 1443 | ALOGW("could not get event, %s\n", strerror(errno)); |
michael@0 | 1444 | return -1; |
michael@0 | 1445 | } |
michael@0 | 1446 | //printf("got %d bytes of event information\n", res); |
michael@0 | 1447 | |
michael@0 | 1448 | strcpy(devname, DEVICE_PATH); |
michael@0 | 1449 | filename = devname + strlen(devname); |
michael@0 | 1450 | *filename++ = '/'; |
michael@0 | 1451 | |
michael@0 | 1452 | while(res >= (int)sizeof(*event)) { |
michael@0 | 1453 | event = (struct inotify_event *)(event_buf + event_pos); |
michael@0 | 1454 | //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); |
michael@0 | 1455 | if(event->len) { |
michael@0 | 1456 | strcpy(filename, event->name); |
michael@0 | 1457 | if(event->mask & IN_CREATE) { |
michael@0 | 1458 | openDeviceLocked(devname); |
michael@0 | 1459 | } else { |
michael@0 | 1460 | ALOGI("Removing device '%s' due to inotify event\n", devname); |
michael@0 | 1461 | closeDeviceByPathLocked(devname); |
michael@0 | 1462 | } |
michael@0 | 1463 | } |
michael@0 | 1464 | event_size = sizeof(*event) + event->len; |
michael@0 | 1465 | res -= event_size; |
michael@0 | 1466 | event_pos += event_size; |
michael@0 | 1467 | } |
michael@0 | 1468 | return 0; |
michael@0 | 1469 | } |
michael@0 | 1470 | |
michael@0 | 1471 | status_t EventHub::scanDirLocked(const char *dirname) |
michael@0 | 1472 | { |
michael@0 | 1473 | char devname[PATH_MAX]; |
michael@0 | 1474 | char *filename; |
michael@0 | 1475 | DIR *dir; |
michael@0 | 1476 | struct dirent *de; |
michael@0 | 1477 | dir = opendir(dirname); |
michael@0 | 1478 | if(dir == NULL) |
michael@0 | 1479 | return -1; |
michael@0 | 1480 | strcpy(devname, dirname); |
michael@0 | 1481 | filename = devname + strlen(devname); |
michael@0 | 1482 | *filename++ = '/'; |
michael@0 | 1483 | while((de = readdir(dir))) { |
michael@0 | 1484 | if(de->d_name[0] == '.' && |
michael@0 | 1485 | (de->d_name[1] == '\0' || |
michael@0 | 1486 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
michael@0 | 1487 | continue; |
michael@0 | 1488 | strcpy(filename, de->d_name); |
michael@0 | 1489 | openDeviceLocked(devname); |
michael@0 | 1490 | } |
michael@0 | 1491 | closedir(dir); |
michael@0 | 1492 | return 0; |
michael@0 | 1493 | } |
michael@0 | 1494 | |
michael@0 | 1495 | void EventHub::requestReopenDevices() { |
michael@0 | 1496 | ALOGV("requestReopenDevices() called"); |
michael@0 | 1497 | |
michael@0 | 1498 | AutoMutex _l(mLock); |
michael@0 | 1499 | mNeedToReopenDevices = true; |
michael@0 | 1500 | } |
michael@0 | 1501 | |
michael@0 | 1502 | void EventHub::dump(String8& dump) { |
michael@0 | 1503 | dump.append("Event Hub State:\n"); |
michael@0 | 1504 | |
michael@0 | 1505 | { // acquire lock |
michael@0 | 1506 | AutoMutex _l(mLock); |
michael@0 | 1507 | |
michael@0 | 1508 | dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); |
michael@0 | 1509 | |
michael@0 | 1510 | dump.append(INDENT "Devices:\n"); |
michael@0 | 1511 | |
michael@0 | 1512 | for (size_t i = 0; i < mDevices.size(); i++) { |
michael@0 | 1513 | const Device* device = mDevices.valueAt(i); |
michael@0 | 1514 | if (mBuiltInKeyboardId == device->id) { |
michael@0 | 1515 | dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", |
michael@0 | 1516 | device->id, device->identifier.name.string()); |
michael@0 | 1517 | } else { |
michael@0 | 1518 | dump.appendFormat(INDENT2 "%d: %s\n", device->id, |
michael@0 | 1519 | device->identifier.name.string()); |
michael@0 | 1520 | } |
michael@0 | 1521 | dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes); |
michael@0 | 1522 | dump.appendFormat(INDENT3 "Path: %s\n", device->path.string()); |
michael@0 | 1523 | dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string()); |
michael@0 | 1524 | dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string()); |
michael@0 | 1525 | dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string()); |
michael@0 | 1526 | dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " |
michael@0 | 1527 | "product=0x%04x, version=0x%04x\n", |
michael@0 | 1528 | device->identifier.bus, device->identifier.vendor, |
michael@0 | 1529 | device->identifier.product, device->identifier.version); |
michael@0 | 1530 | dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", |
michael@0 | 1531 | device->keyMap.keyLayoutFile.string()); |
michael@0 | 1532 | dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n", |
michael@0 | 1533 | device->keyMap.keyCharacterMapFile.string()); |
michael@0 | 1534 | dump.appendFormat(INDENT3 "ConfigurationFile: %s\n", |
michael@0 | 1535 | device->configurationFile.string()); |
michael@0 | 1536 | dump.appendFormat(INDENT3 "HaveKeyboardLayoutOverlay: %s\n", |
michael@0 | 1537 | toString(device->overlayKeyMap != NULL)); |
michael@0 | 1538 | } |
michael@0 | 1539 | } // release lock |
michael@0 | 1540 | } |
michael@0 | 1541 | |
michael@0 | 1542 | void EventHub::monitor() { |
michael@0 | 1543 | // Acquire and release the lock to ensure that the event hub has not deadlocked. |
michael@0 | 1544 | mLock.lock(); |
michael@0 | 1545 | mLock.unlock(); |
michael@0 | 1546 | } |
michael@0 | 1547 | |
michael@0 | 1548 | |
michael@0 | 1549 | }; // namespace android |