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