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