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 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -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 | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 39 | #include <ui/KeyLayoutMap.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 40 | #include <ui/KeyCharacterMap.h> |
| 41 | #include <ui/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> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | /* this macro is used to tell if "bit" is set in "array" |
| 53 | * it selects a byte from the array, and does a boolean AND |
| 54 | * operation with a byte that only has the relevant bit set. |
| 55 | * eg. to check for the 12th bit, we do (array[1] & 1<<4) |
| 56 | */ |
| 57 | #define test_bit(bit, array) (array[bit/8] & (1<<(bit%8))) |
| 58 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 59 | /* this macro computes the number of bytes needed to represent a bit array of the specified size */ |
| 60 | #define sizeof_bit_array(bits) ((bits + 7) / 8) |
| 61 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 62 | #define INDENT " " |
| 63 | #define INDENT2 " " |
| 64 | #define INDENT3 " " |
| 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | namespace android { |
| 67 | |
| 68 | static const char *WAKE_LOCK_ID = "KeyEvents"; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 69 | static const char *DEVICE_PATH = "/dev/input"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
| 71 | /* return the larger integer */ |
| 72 | static inline int max(int v1, int v2) |
| 73 | { |
| 74 | return (v1 > v2) ? v1 : v2; |
| 75 | } |
| 76 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 77 | static inline const char* toString(bool value) { |
| 78 | return value ? "true" : "false"; |
| 79 | } |
| 80 | |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 81 | // --- Global Functions --- |
| 82 | |
| 83 | uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) { |
| 84 | // Touch devices get dibs on touch-related axes. |
| 85 | if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) { |
| 86 | switch (axis) { |
| 87 | case ABS_X: |
| 88 | case ABS_Y: |
| 89 | case ABS_PRESSURE: |
| 90 | case ABS_TOOL_WIDTH: |
| 91 | case ABS_DISTANCE: |
| 92 | case ABS_TILT_X: |
| 93 | case ABS_TILT_Y: |
| 94 | case ABS_MT_SLOT: |
| 95 | case ABS_MT_TOUCH_MAJOR: |
| 96 | case ABS_MT_TOUCH_MINOR: |
| 97 | case ABS_MT_WIDTH_MAJOR: |
| 98 | case ABS_MT_WIDTH_MINOR: |
| 99 | case ABS_MT_ORIENTATION: |
| 100 | case ABS_MT_POSITION_X: |
| 101 | case ABS_MT_POSITION_Y: |
| 102 | case ABS_MT_TOOL_TYPE: |
| 103 | case ABS_MT_BLOB_ID: |
| 104 | case ABS_MT_TRACKING_ID: |
| 105 | case ABS_MT_PRESSURE: |
| 106 | case ABS_MT_DISTANCE: |
| 107 | return INPUT_DEVICE_CLASS_TOUCH; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Joystick devices get the rest. |
| 112 | return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK; |
| 113 | } |
| 114 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 115 | // --- EventHub::Device --- |
| 116 | |
| 117 | EventHub::Device::Device(int fd, int32_t id, const String8& path, |
| 118 | const InputDeviceIdentifier& identifier) : |
| 119 | next(NULL), |
| 120 | fd(fd), id(id), path(path), identifier(identifier), |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 121 | classes(0), configuration(NULL), virtualKeyMap(NULL) { |
| 122 | memset(keyBitmask, 0, sizeof(keyBitmask)); |
| 123 | memset(absBitmask, 0, sizeof(absBitmask)); |
| 124 | memset(relBitmask, 0, sizeof(relBitmask)); |
| 125 | memset(swBitmask, 0, sizeof(swBitmask)); |
| 126 | memset(ledBitmask, 0, sizeof(ledBitmask)); |
| 127 | memset(propBitmask, 0, sizeof(propBitmask)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 130 | EventHub::Device::~Device() { |
| 131 | close(); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 132 | delete configuration; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 133 | delete virtualKeyMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 136 | void EventHub::Device::close() { |
| 137 | if (fd >= 0) { |
| 138 | ::close(fd); |
| 139 | fd = -1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | // --- EventHub --- |
| 145 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 146 | const uint32_t EventHub::EPOLL_ID_INOTIFY; |
| 147 | const uint32_t EventHub::EPOLL_ID_WAKE; |
| 148 | const int EventHub::EPOLL_SIZE_HINT; |
| 149 | const int EventHub::EPOLL_MAX_EVENTS; |
| 150 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 151 | EventHub::EventHub(void) : |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 152 | mBuiltInKeyboardId(-1), mNextDeviceId(1), |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 153 | mOpeningDevices(0), mClosingDevices(0), |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 154 | mNeedToSendFinishedDeviceScan(false), |
| 155 | mNeedToReopenDevices(false), mNeedToScanDevices(true), |
| 156 | mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 158 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 159 | mEpollFd = epoll_create(EPOLL_SIZE_HINT); |
| 160 | LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno); |
| 161 | |
| 162 | mINotifyFd = inotify_init(); |
| 163 | int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE); |
| 164 | LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d", |
| 165 | DEVICE_PATH, errno); |
| 166 | |
| 167 | struct epoll_event eventItem; |
| 168 | memset(&eventItem, 0, sizeof(eventItem)); |
| 169 | eventItem.events = EPOLLIN; |
| 170 | eventItem.data.u32 = EPOLL_ID_INOTIFY; |
| 171 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem); |
| 172 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno); |
| 173 | |
| 174 | int wakeFds[2]; |
| 175 | result = pipe(wakeFds); |
| 176 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno); |
| 177 | |
| 178 | mWakeReadPipeFd = wakeFds[0]; |
| 179 | mWakeWritePipeFd = wakeFds[1]; |
| 180 | |
| 181 | result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK); |
| 182 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d", |
| 183 | errno); |
| 184 | |
| 185 | result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK); |
| 186 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d", |
| 187 | errno); |
| 188 | |
| 189 | eventItem.data.u32 = EPOLL_ID_WAKE; |
| 190 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem); |
| 191 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d", |
| 192 | errno); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 195 | EventHub::~EventHub(void) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 196 | closeAllDevicesLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 198 | while (mClosingDevices) { |
| 199 | Device* device = mClosingDevices; |
| 200 | mClosingDevices = device->next; |
| 201 | delete device; |
| 202 | } |
| 203 | |
| 204 | ::close(mEpollFd); |
| 205 | ::close(mINotifyFd); |
| 206 | ::close(mWakeReadPipeFd); |
| 207 | ::close(mWakeWritePipeFd); |
| 208 | |
| 209 | release_wake_lock(WAKE_LOCK_ID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 212 | String8 EventHub::getDeviceName(int32_t deviceId) const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 214 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | if (device == NULL) return String8(); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 216 | return device->identifier.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 219 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 221 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | if (device == NULL) return 0; |
| 223 | return device->classes; |
| 224 | } |
| 225 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 226 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 227 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 228 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 229 | if (device && device->configuration) { |
| 230 | *outConfiguration = *device->configuration; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 231 | } else { |
| 232 | outConfiguration->clear(); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 236 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 237 | RawAbsoluteAxisInfo* outAxisInfo) const { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 238 | outAxisInfo->clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 239 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 240 | if (axis >= 0 && axis <= ABS_MAX) { |
| 241 | AutoMutex _l(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 243 | Device* device = getDeviceLocked(deviceId); |
| 244 | if (device && test_bit(axis, device->absBitmask)) { |
| 245 | struct input_absinfo info; |
| 246 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 247 | 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] | 248 | axis, device->identifier.name.string(), device->fd, errno); |
| 249 | return -errno; |
| 250 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 252 | if (info.minimum != info.maximum) { |
| 253 | outAxisInfo->valid = true; |
| 254 | outAxisInfo->minValue = info.minimum; |
| 255 | outAxisInfo->maxValue = info.maximum; |
| 256 | outAxisInfo->flat = info.flat; |
| 257 | outAxisInfo->fuzz = info.fuzz; |
| 258 | outAxisInfo->resolution = info.resolution; |
| 259 | } |
| 260 | return OK; |
| 261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | } |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 263 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 266 | bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const { |
| 267 | if (axis >= 0 && axis <= REL_MAX) { |
| 268 | AutoMutex _l(mLock); |
| 269 | |
| 270 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 271 | if (device) { |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 272 | return test_bit(axis, device->relBitmask); |
| 273 | } |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 278 | bool EventHub::hasInputProperty(int32_t deviceId, int property) const { |
| 279 | if (property >= 0 && property <= INPUT_PROP_MAX) { |
| 280 | AutoMutex _l(mLock); |
| 281 | |
| 282 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 283 | if (device) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 284 | return test_bit(property, device->propBitmask); |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 290 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 291 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
| 292 | AutoMutex _l(mLock); |
| 293 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 294 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 295 | if (device && test_bit(scanCode, device->keyBitmask)) { |
| 296 | uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; |
| 297 | memset(keyState, 0, sizeof(keyState)); |
| 298 | if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { |
| 299 | return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
| 300 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
| 302 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 303 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 306 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
| 307 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 308 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 309 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 310 | if (device && device->keyMap.haveKeyLayout()) { |
| 311 | Vector<int32_t> scanCodes; |
| 312 | device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes); |
| 313 | if (scanCodes.size() != 0) { |
| 314 | uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; |
| 315 | memset(keyState, 0, sizeof(keyState)); |
| 316 | if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { |
| 317 | for (size_t i = 0; i < scanCodes.size(); i++) { |
| 318 | int32_t sc = scanCodes.itemAt(i); |
| 319 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) { |
| 320 | return AKEY_STATE_DOWN; |
| 321 | } |
| 322 | } |
| 323 | return AKEY_STATE_UP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 327 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 330 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 331 | if (sw >= 0 && sw <= SW_MAX) { |
| 332 | AutoMutex _l(mLock); |
| 333 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 334 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 335 | if (device && test_bit(sw, device->swBitmask)) { |
| 336 | uint8_t swState[sizeof_bit_array(SW_MAX + 1)]; |
| 337 | memset(swState, 0, sizeof(swState)); |
| 338 | if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) { |
| 339 | return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
| 340 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 341 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 342 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 343 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 346 | 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] | 347 | *outValue = 0; |
| 348 | |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 349 | if (axis >= 0 && axis <= ABS_MAX) { |
| 350 | AutoMutex _l(mLock); |
| 351 | |
| 352 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 353 | if (device && test_bit(axis, device->absBitmask)) { |
| 354 | struct input_absinfo info; |
| 355 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 356 | 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] | 357 | axis, device->identifier.name.string(), device->fd, errno); |
| 358 | return -errno; |
| 359 | } |
| 360 | |
| 361 | *outValue = info.value; |
| 362 | return OK; |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 365 | return -1; |
| 366 | } |
| 367 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 368 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 369 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 370 | AutoMutex _l(mLock); |
| 371 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 372 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 373 | if (device && device->keyMap.haveKeyLayout()) { |
| 374 | Vector<int32_t> scanCodes; |
| 375 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
| 376 | scanCodes.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 377 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 378 | status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey( |
| 379 | keyCodes[codeIndex], &scanCodes); |
| 380 | if (! err) { |
| 381 | // check the possible scan codes identified by the layout map against the |
| 382 | // map of codes actually emitted by the driver |
| 383 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
| 384 | if (test_bit(scanCodes[sc], device->keyBitmask)) { |
| 385 | outFlags[codeIndex] = 1; |
| 386 | break; |
| 387 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 391 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 392 | } |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 393 | return false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 396 | status_t EventHub::mapKey(int32_t deviceId, int scancode, |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 397 | int32_t* outKeycode, uint32_t* outFlags) const |
| 398 | { |
| 399 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 400 | Device* device = getDeviceLocked(deviceId); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 401 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 402 | if (device && device->keyMap.haveKeyLayout()) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 403 | status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 404 | if (err == NO_ERROR) { |
| 405 | return NO_ERROR; |
| 406 | } |
| 407 | } |
| 408 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 409 | if (mBuiltInKeyboardId != -1) { |
| 410 | device = getDeviceLocked(mBuiltInKeyboardId); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 411 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 412 | if (device && device->keyMap.haveKeyLayout()) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 413 | status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 414 | if (err == NO_ERROR) { |
| 415 | return NO_ERROR; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | *outKeycode = 0; |
| 421 | *outFlags = 0; |
| 422 | return NAME_NOT_FOUND; |
| 423 | } |
| 424 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 425 | status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 426 | { |
| 427 | AutoMutex _l(mLock); |
| 428 | Device* device = getDeviceLocked(deviceId); |
| 429 | |
| 430 | if (device && device->keyMap.haveKeyLayout()) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 431 | status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 432 | if (err == NO_ERROR) { |
| 433 | return NO_ERROR; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (mBuiltInKeyboardId != -1) { |
| 438 | device = getDeviceLocked(mBuiltInKeyboardId); |
| 439 | |
| 440 | if (device && device->keyMap.haveKeyLayout()) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 441 | status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 442 | if (err == NO_ERROR) { |
| 443 | return NO_ERROR; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 448 | return NAME_NOT_FOUND; |
| 449 | } |
| 450 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 451 | void EventHub::setExcludedDevices(const Vector<String8>& devices) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 452 | AutoMutex _l(mLock); |
| 453 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 454 | mExcludedDevices = devices; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 457 | bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const { |
| 458 | AutoMutex _l(mLock); |
| 459 | Device* device = getDeviceLocked(deviceId); |
| 460 | if (device && scanCode >= 0 && scanCode <= KEY_MAX) { |
| 461 | if (test_bit(scanCode, device->keyBitmask)) { |
| 462 | return true; |
| 463 | } |
| 464 | } |
| 465 | return false; |
| 466 | } |
| 467 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 468 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { |
| 469 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 470 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 471 | if (device && led >= 0 && led <= LED_MAX) { |
| 472 | if (test_bit(led, device->ledBitmask)) { |
| 473 | return true; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
| 480 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 481 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 482 | if (device && led >= 0 && led <= LED_MAX) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 483 | struct input_event ev; |
| 484 | ev.time.tv_sec = 0; |
| 485 | ev.time.tv_usec = 0; |
| 486 | ev.type = EV_LED; |
| 487 | ev.code = led; |
| 488 | ev.value = on ? 1 : 0; |
| 489 | |
| 490 | ssize_t nWrite; |
| 491 | do { |
| 492 | nWrite = write(device->fd, &ev, sizeof(struct input_event)); |
| 493 | } while (nWrite == -1 && errno == EINTR); |
| 494 | } |
| 495 | } |
| 496 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 497 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, |
| 498 | Vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 499 | outVirtualKeys.clear(); |
| 500 | |
| 501 | AutoMutex _l(mLock); |
| 502 | Device* device = getDeviceLocked(deviceId); |
| 503 | if (device && device->virtualKeyMap) { |
| 504 | outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys()); |
| 505 | } |
| 506 | } |
| 507 | |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 508 | String8 EventHub::getKeyCharacterMapFile(int32_t deviceId) const { |
| 509 | AutoMutex _l(mLock); |
| 510 | Device* device = getDeviceLocked(deviceId); |
| 511 | if (device) { |
| 512 | return device->keyMap.keyCharacterMapFile; |
| 513 | } |
| 514 | return String8(); |
| 515 | } |
| 516 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 517 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { |
| 518 | if (deviceId == 0) { |
| 519 | deviceId = mBuiltInKeyboardId; |
| 520 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 521 | ssize_t index = mDevices.indexOfKey(deviceId); |
| 522 | return index >= 0 ? mDevices.valueAt(index) : NULL; |
| 523 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 524 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 525 | EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const { |
| 526 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 527 | Device* device = mDevices.valueAt(i); |
| 528 | if (device->path == devicePath) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 529 | return device; |
| 530 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } |
| 532 | return NULL; |
| 533 | } |
| 534 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 535 | size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 536 | ALOG_ASSERT(bufferSize >= 1); |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 537 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 538 | AutoMutex _l(mLock); |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 539 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 540 | struct input_event readBuffer[bufferSize]; |
| 541 | |
| 542 | RawEvent* event = buffer; |
| 543 | size_t capacity = bufferSize; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 544 | bool awoken = false; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 545 | for (;;) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 546 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 547 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 548 | // Reopen input devices if needed. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 549 | if (mNeedToReopenDevices) { |
| 550 | mNeedToReopenDevices = false; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 551 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 552 | ALOGI("Reopening all input devices due to a configuration change."); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 553 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 554 | closeAllDevicesLocked(); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 555 | mNeedToScanDevices = true; |
| 556 | break; // return to the caller before we actually rescan |
| 557 | } |
| 558 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 559 | // Report any devices that had last been added/removed. |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 560 | while (mClosingDevices) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 561 | Device* device = mClosingDevices; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 562 | ALOGV("Reporting device closed: id=%d, name=%s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | device->id, device->path.string()); |
| 564 | mClosingDevices = device->next; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 565 | event->when = now; |
| 566 | event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
| 567 | event->type = DEVICE_REMOVED; |
| 568 | event += 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | delete device; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 570 | mNeedToSendFinishedDeviceScan = true; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 571 | if (--capacity == 0) { |
| 572 | break; |
| 573 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 575 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 576 | if (mNeedToScanDevices) { |
| 577 | mNeedToScanDevices = false; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 578 | scanDevicesLocked(); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 579 | mNeedToSendFinishedDeviceScan = true; |
| 580 | } |
| 581 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 582 | while (mOpeningDevices != NULL) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 583 | Device* device = mOpeningDevices; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 584 | ALOGV("Reporting device opened: id=%d, name=%s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | device->id, device->path.string()); |
| 586 | mOpeningDevices = device->next; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 587 | event->when = now; |
| 588 | event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
| 589 | event->type = DEVICE_ADDED; |
| 590 | event += 1; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 591 | mNeedToSendFinishedDeviceScan = true; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 592 | if (--capacity == 0) { |
| 593 | break; |
| 594 | } |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | if (mNeedToSendFinishedDeviceScan) { |
| 598 | mNeedToSendFinishedDeviceScan = false; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 599 | event->when = now; |
| 600 | event->type = FINISHED_DEVICE_SCAN; |
| 601 | event += 1; |
| 602 | if (--capacity == 0) { |
| 603 | break; |
| 604 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 607 | // Grab the next input event. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 608 | bool deviceChanged = false; |
| 609 | while (mPendingEventIndex < mPendingEventCount) { |
| 610 | const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++]; |
| 611 | if (eventItem.data.u32 == EPOLL_ID_INOTIFY) { |
| 612 | if (eventItem.events & EPOLLIN) { |
| 613 | mPendingINotify = true; |
| 614 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 615 | ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 616 | } |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | if (eventItem.data.u32 == EPOLL_ID_WAKE) { |
| 621 | if (eventItem.events & EPOLLIN) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 622 | ALOGV("awoken after wake()"); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 623 | awoken = true; |
| 624 | char buffer[16]; |
| 625 | ssize_t nRead; |
| 626 | do { |
| 627 | nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); |
| 628 | } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); |
| 629 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 630 | ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.", |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 631 | eventItem.events); |
| 632 | } |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32); |
| 637 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 638 | ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.", |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 639 | eventItem.events, eventItem.data.u32); |
| 640 | continue; |
| 641 | } |
| 642 | |
| 643 | Device* device = mDevices.valueAt(deviceIndex); |
| 644 | if (eventItem.events & EPOLLIN) { |
| 645 | int32_t readSize = read(device->fd, readBuffer, |
| 646 | sizeof(struct input_event) * capacity); |
| 647 | if (readSize == 0 || (readSize < 0 && errno == ENODEV)) { |
| 648 | // Device was removed before INotify noticed. |
Jeff Brown | 4130554 | 2011-10-05 11:14:13 -0700 | [diff] [blame] | 649 | ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d " |
| 650 | "capacity: %d errno: %d)\n", |
| 651 | device->fd, readSize, bufferSize, capacity, errno); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 652 | deviceChanged = true; |
| 653 | closeDeviceLocked(device); |
| 654 | } else if (readSize < 0) { |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 655 | if (errno != EAGAIN && errno != EINTR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 656 | ALOGW("could not get event (errno=%d)", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 657 | } |
| 658 | } else if ((readSize % sizeof(struct input_event)) != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 659 | ALOGE("could not get event (wrong size: %d)", readSize); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 660 | } else { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 661 | int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
| 662 | |
| 663 | size_t count = size_t(readSize) / sizeof(struct input_event); |
| 664 | for (size_t i = 0; i < count; i++) { |
| 665 | const struct input_event& iev = readBuffer[i]; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 666 | ALOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d", |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 667 | device->path.string(), |
| 668 | (int) iev.time.tv_sec, (int) iev.time.tv_usec, |
| 669 | iev.type, iev.code, iev.value); |
| 670 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 671 | #ifdef HAVE_POSIX_CLOCKS |
| 672 | // Use the time specified in the event instead of the current time |
| 673 | // so that downstream code can get more accurate estimates of |
| 674 | // event dispatch latency from the time the event is enqueued onto |
| 675 | // the evdev client buffer. |
| 676 | // |
| 677 | // The event's timestamp fortuitously uses the same monotonic clock |
| 678 | // time base as the rest of Android. The kernel event device driver |
| 679 | // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts(). |
| 680 | // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere |
| 681 | // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a |
| 682 | // system call that also queries ktime_get_ts(). |
| 683 | event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL |
| 684 | + nsecs_t(iev.time.tv_usec) * 1000LL; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 685 | ALOGV("event time %lld, now %lld", event->when, now); |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 686 | #else |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 687 | event->when = now; |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 688 | #endif |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 689 | event->deviceId = deviceId; |
| 690 | event->type = iev.type; |
| 691 | event->scanCode = iev.code; |
| 692 | event->value = iev.value; |
| 693 | event->keyCode = AKEYCODE_UNKNOWN; |
| 694 | event->flags = 0; |
| 695 | if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) { |
| 696 | status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code, |
| 697 | &event->keyCode, &event->flags); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 698 | ALOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n", |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 699 | iev.code, event->keyCode, event->flags, err); |
| 700 | } |
| 701 | event += 1; |
| 702 | } |
| 703 | capacity -= count; |
| 704 | if (capacity == 0) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 705 | // The result buffer is full. Reset the pending event index |
| 706 | // so we will try to read the device again on the next iteration. |
| 707 | mPendingEventIndex -= 1; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 708 | break; |
| 709 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 711 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 712 | ALOGW("Received unexpected epoll event 0x%08x for device %s.", |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 713 | eventItem.events, device->identifier.name.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 716 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 717 | // readNotify() will modify the list of devices so this must be done after |
| 718 | // processing all other events to ensure that we read all remaining events |
| 719 | // before closing the devices. |
| 720 | if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) { |
| 721 | mPendingINotify = false; |
| 722 | readNotifyLocked(); |
| 723 | deviceChanged = true; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 724 | } |
| 725 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 726 | // Report added or removed devices immediately. |
| 727 | if (deviceChanged) { |
| 728 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | } |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 730 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 731 | // Return now if we have collected any events or if we were explicitly awoken. |
| 732 | if (event != buffer || awoken) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 733 | break; |
| 734 | } |
| 735 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 736 | // Poll for events. Mind the wake lock dance! |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 737 | // 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] | 738 | // subtle choreography. When a device driver has pending (unread) events, it acquires |
| 739 | // a kernel wake lock. However, once the last pending event has been read, the device |
| 740 | // driver will release the kernel wake lock. To prevent the system from going to sleep |
| 741 | // when this happens, the EventHub holds onto its own user wake lock while the client |
| 742 | // is processing events. Thus the system can only sleep if there are no events |
| 743 | // pending or currently being processed. |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 744 | // |
| 745 | // The timeout is advisory only. If the device is asleep, it will not wake just to |
| 746 | // service the timeout. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 747 | mPendingEventIndex = 0; |
| 748 | |
| 749 | mLock.unlock(); // release lock before poll, must be before release_wake_lock |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 750 | release_wake_lock(WAKE_LOCK_ID); |
| 751 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 752 | int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 753 | |
| 754 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 755 | mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 756 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 757 | if (pollResult == 0) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 758 | // Timed out. |
| 759 | mPendingEventCount = 0; |
| 760 | break; |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 761 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 762 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 763 | if (pollResult < 0) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 764 | // An error occurred. |
| 765 | mPendingEventCount = 0; |
| 766 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 767 | // Sleep after errors to avoid locking up the system. |
| 768 | // Hopefully the error is transient. |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 769 | if (errno != EINTR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 770 | ALOGW("poll failed (errno=%d)\n", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 771 | usleep(100000); |
| 772 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 773 | } else { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 774 | // Some events occurred. |
| 775 | mPendingEventCount = size_t(pollResult); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 776 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 778 | |
| 779 | // All done, return the number of events we read. |
| 780 | return event - buffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 783 | void EventHub::wake() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 784 | ALOGV("wake() called"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 786 | ssize_t nWrite; |
| 787 | do { |
| 788 | nWrite = write(mWakeWritePipeFd, "W", 1); |
| 789 | } while (nWrite == -1 && errno == EINTR); |
| 790 | |
| 791 | if (nWrite != 1 && errno != EAGAIN) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 792 | ALOGW("Could not write wake signal, errno=%d", errno); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 794 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 795 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 796 | void EventHub::scanDevicesLocked() { |
| 797 | status_t res = scanDirLocked(DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | if(res < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 799 | ALOGE("scan dir failed for %s\n", DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | } |
| 802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | // ---------------------------------------------------------------------------- |
| 804 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 805 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { |
| 806 | const uint8_t* end = array + endIndex; |
| 807 | array += startIndex; |
| 808 | while (array != end) { |
| 809 | if (*(array++) != 0) { |
| 810 | return true; |
| 811 | } |
| 812 | } |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | static const int32_t GAMEPAD_KEYCODES[] = { |
| 817 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, |
| 818 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, |
| 819 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, |
| 820 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, |
| 821 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 822 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, |
| 823 | AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4, |
| 824 | AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8, |
| 825 | AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12, |
| 826 | AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16, |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 827 | }; |
| 828 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 829 | status_t EventHub::openDeviceLocked(const char *devicePath) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 830 | char buffer[80]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 832 | ALOGV("Opening device: %s", devicePath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | |
Jeff Brown | 874c1e9 | 2012-01-19 14:32:47 -0800 | [diff] [blame^] | 834 | int fd = open(devicePath, O_RDWR | O_CLOEXEC); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | if(fd < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 836 | 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] | 837 | return -1; |
| 838 | } |
| 839 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 840 | InputDeviceIdentifier identifier; |
| 841 | |
| 842 | // Get device name. |
| 843 | if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { |
| 844 | //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno)); |
| 845 | } else { |
| 846 | buffer[sizeof(buffer) - 1] = '\0'; |
| 847 | identifier.name.setTo(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | } |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 849 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 850 | // Check to see if the device is on our excluded list |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 851 | for (size_t i = 0; i < mExcludedDevices.size(); i++) { |
| 852 | const String8& item = mExcludedDevices.itemAt(i); |
| 853 | if (identifier.name == item) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 854 | ALOGI("ignoring event id %s driver %s\n", devicePath, item.string()); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 855 | close(fd); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 856 | return -1; |
| 857 | } |
| 858 | } |
| 859 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 860 | // Get device driver version. |
| 861 | int driverVersion; |
| 862 | if(ioctl(fd, EVIOCGVERSION, &driverVersion)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 863 | 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] | 864 | close(fd); |
| 865 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 868 | // Get device identifier. |
| 869 | struct input_id inputId; |
| 870 | if(ioctl(fd, EVIOCGID, &inputId)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 871 | 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] | 872 | close(fd); |
| 873 | return -1; |
| 874 | } |
| 875 | identifier.bus = inputId.bustype; |
| 876 | identifier.product = inputId.product; |
| 877 | identifier.vendor = inputId.vendor; |
| 878 | identifier.version = inputId.version; |
| 879 | |
| 880 | // Get device physical location. |
| 881 | if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { |
| 882 | //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); |
| 883 | } else { |
| 884 | buffer[sizeof(buffer) - 1] = '\0'; |
| 885 | identifier.location.setTo(buffer); |
| 886 | } |
| 887 | |
| 888 | // Get device unique id. |
| 889 | if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { |
| 890 | //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); |
| 891 | } else { |
| 892 | buffer[sizeof(buffer) - 1] = '\0'; |
| 893 | identifier.uniqueId.setTo(buffer); |
| 894 | } |
| 895 | |
| 896 | // Make file descriptor non-blocking for use with poll(). |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 897 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 898 | ALOGE("Error %d making device file descriptor non-blocking.", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 899 | close(fd); |
| 900 | return -1; |
| 901 | } |
| 902 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 903 | // Allocate device. (The device object takes ownership of the fd at this point.) |
| 904 | int32_t deviceId = mNextDeviceId++; |
| 905 | Device* device = new Device(fd, deviceId, String8(devicePath), identifier); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | |
| 907 | #if 0 |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 908 | ALOGI("add device %d: %s\n", deviceId, devicePath); |
| 909 | ALOGI(" bus: %04x\n" |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 910 | " vendor %04x\n" |
| 911 | " product %04x\n" |
| 912 | " version %04x\n", |
| 913 | identifier.bus, identifier.vendor, identifier.product, identifier.version); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 914 | ALOGI(" name: \"%s\"\n", identifier.name.string()); |
| 915 | ALOGI(" location: \"%s\"\n", identifier.location.string()); |
| 916 | ALOGI(" unique id: \"%s\"\n", identifier.uniqueId.string()); |
| 917 | ALOGI(" driver: v%d.%d.%d\n", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 918 | driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | #endif |
| 920 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 921 | // Load the configuration file for the device. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 922 | loadConfigurationLocked(device); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 923 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 924 | // Figure out the kinds of events the device reports. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 925 | ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask); |
| 926 | ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask); |
| 927 | ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask); |
| 928 | ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask); |
| 929 | ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask); |
| 930 | ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 931 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 932 | // See if this is a keyboard. Ignore everything in the button range except for |
| 933 | // 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] | 934 | bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC)) |
| 935 | || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK), |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 936 | sizeof_bit_array(KEY_MAX + 1)); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 937 | bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC), |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 938 | sizeof_bit_array(BTN_MOUSE)) |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 939 | || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK), |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 940 | sizeof_bit_array(BTN_DIGI)); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 941 | if (haveKeyboardKeys || haveGamepadButtons) { |
| 942 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 944 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 945 | // 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] | 946 | if (test_bit(BTN_MOUSE, device->keyBitmask) |
| 947 | && test_bit(REL_X, device->relBitmask) |
| 948 | && test_bit(REL_Y, device->relBitmask)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 949 | device->classes |= INPUT_DEVICE_CLASS_CURSOR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 951 | |
| 952 | // See if this is a touch pad. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 953 | // Is this a new modern multi-touch driver? |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 954 | if (test_bit(ABS_MT_POSITION_X, device->absBitmask) |
| 955 | && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 956 | // Some joysticks such as the PS3 controller report axes that conflict |
| 957 | // with the ABS_MT range. Try to confirm that the device really is |
| 958 | // a touch screen. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 959 | if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 960 | device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT; |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 961 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 962 | // Is this an old style single-touch driver? |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 963 | } else if (test_bit(BTN_TOUCH, device->keyBitmask) |
| 964 | && test_bit(ABS_X, device->absBitmask) |
| 965 | && test_bit(ABS_Y, device->absBitmask)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 966 | device->classes |= INPUT_DEVICE_CLASS_TOUCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 969 | // See if this device is a joystick. |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 970 | // Assumes that joysticks always have gamepad buttons in order to distinguish them |
| 971 | // from other devices such as accelerometers that also have absolute axes. |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 972 | if (haveGamepadButtons) { |
| 973 | uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK; |
| 974 | for (int i = 0; i <= ABS_MAX; i++) { |
| 975 | if (test_bit(i, device->absBitmask) |
| 976 | && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 977 | device->classes = assumedClasses; |
| 978 | break; |
| 979 | } |
| 980 | } |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 983 | // Check whether this device has switches. |
| 984 | for (int i = 0; i <= SW_MAX; i++) { |
| 985 | if (test_bit(i, device->swBitmask)) { |
| 986 | device->classes |= INPUT_DEVICE_CLASS_SWITCH; |
| 987 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | } |
| 989 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 991 | // Configure virtual keys. |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 992 | if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 993 | // Load the virtual keys for the touch screen, if any. |
| 994 | // 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] | 995 | status_t status = loadVirtualKeyMapLocked(device); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 996 | if (!status) { |
| 997 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1001 | // Load the key map. |
| 1002 | // We need to do this for joysticks too because the key layout may specify axes. |
| 1003 | status_t keyMapStatus = NAME_NOT_FOUND; |
| 1004 | if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1005 | // Load the keymap for the device. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1006 | keyMapStatus = loadKeyMapLocked(device); |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1007 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1008 | |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1009 | // Configure the keyboard, gamepad or virtual keyboard. |
| 1010 | if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1011 | // Register the keyboard as a built-in keyboard if it is eligible. |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1012 | if (!keyMapStatus |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1013 | && mBuiltInKeyboardId == -1 |
| 1014 | && isEligibleBuiltInKeyboard(device->identifier, |
| 1015 | device->configuration, &device->keyMap)) { |
| 1016 | mBuiltInKeyboardId = device->id; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1017 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1019 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1020 | if (hasKeycodeLocked(device, AKEYCODE_Q)) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1021 | device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1022 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1023 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1024 | // See if this device has a DPAD. |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1025 | if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) && |
| 1026 | hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) && |
| 1027 | hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) && |
| 1028 | hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) && |
| 1029 | hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1030 | device->classes |= INPUT_DEVICE_CLASS_DPAD; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1031 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1032 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1033 | // See if this device has a gamepad. |
Kenny Root | 1d79a9d | 2010-10-21 15:46:03 -0700 | [diff] [blame] | 1034 | 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] | 1035 | if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1036 | device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; |
| 1037 | break; |
| 1038 | } |
| 1039 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 1042 | // If the device isn't recognized as something we handle, don't monitor it. |
| 1043 | if (device->classes == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1044 | ALOGV("Dropping device: id=%d, path='%s', name='%s'", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1045 | deviceId, devicePath, device->identifier.name.string()); |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 1046 | delete device; |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1050 | // Determine whether the device is external or internal. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1051 | if (isExternalDeviceLocked(device)) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1052 | device->classes |= INPUT_DEVICE_CLASS_EXTERNAL; |
| 1053 | } |
| 1054 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1055 | // Register with epoll. |
| 1056 | struct epoll_event eventItem; |
| 1057 | memset(&eventItem, 0, sizeof(eventItem)); |
| 1058 | eventItem.events = EPOLLIN; |
| 1059 | eventItem.data.u32 = deviceId; |
| 1060 | if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1061 | ALOGE("Could not add device fd to epoll instance. errno=%d", errno); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1062 | delete device; |
| 1063 | return -1; |
| 1064 | } |
| 1065 | |
Jeff Brown | e22afbe | 2011-12-16 13:45:40 -0800 | [diff] [blame] | 1066 | // Enable wake-lock behavior on kernels that support it. |
| 1067 | // TODO: Only need this for devices that can really wake the system. |
| 1068 | bool usingSuspendBlock = ioctl(fd, EVIOCSSUSPENDBLOCK, 1) == 0; |
| 1069 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1070 | 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] | 1071 | "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, " |
| 1072 | "usingSuspendBlock=%s", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1073 | deviceId, fd, devicePath, device->identifier.name.string(), |
| 1074 | device->classes, |
| 1075 | device->configurationFile.string(), |
| 1076 | device->keyMap.keyLayoutFile.string(), |
| 1077 | device->keyMap.keyCharacterMapFile.string(), |
Jeff Brown | e22afbe | 2011-12-16 13:45:40 -0800 | [diff] [blame] | 1078 | toString(mBuiltInKeyboardId == deviceId), |
| 1079 | toString(usingSuspendBlock)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1080 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1081 | mDevices.add(deviceId, device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | device->next = mOpeningDevices; |
| 1084 | mOpeningDevices = device; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1088 | void EventHub::loadConfigurationLocked(Device* device) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1089 | device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier( |
| 1090 | device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1091 | if (device->configurationFile.isEmpty()) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1092 | ALOGD("No input device configuration file found for device '%s'.", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1093 | device->identifier.name.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1094 | } else { |
| 1095 | status_t status = PropertyMap::load(device->configurationFile, |
| 1096 | &device->configuration); |
| 1097 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1098 | ALOGE("Error loading input device configuration file for device '%s'. " |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1099 | "Using default configuration.", |
| 1100 | device->identifier.name.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1105 | status_t EventHub::loadVirtualKeyMapLocked(Device* device) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1106 | // The virtual key map is supplied by the kernel as a system board property file. |
| 1107 | String8 path; |
| 1108 | path.append("/sys/board_properties/virtualkeys."); |
| 1109 | path.append(device->identifier.name); |
| 1110 | if (access(path.string(), R_OK)) { |
| 1111 | return NAME_NOT_FOUND; |
| 1112 | } |
| 1113 | return VirtualKeyMap::load(path, &device->virtualKeyMap); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1116 | status_t EventHub::loadKeyMapLocked(Device* device) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1117 | return device->keyMap.load(device->identifier, device->configuration); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1120 | bool EventHub::isExternalDeviceLocked(Device* device) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1121 | if (device->configuration) { |
| 1122 | bool value; |
Max Braun | e81056f | 2011-08-30 14:35:45 -0700 | [diff] [blame] | 1123 | if (device->configuration->tryGetProperty(String8("device.internal"), value)) { |
| 1124 | return !value; |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH; |
| 1128 | } |
| 1129 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1130 | bool EventHub::hasKeycodeLocked(Device* device, int keycode) const { |
| 1131 | if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | Vector<int32_t> scanCodes; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1136 | device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes); |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1137 | const size_t N = scanCodes.size(); |
| 1138 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 1139 | int32_t sc = scanCodes.itemAt(i); |
| 1140 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { |
| 1141 | return true; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1148 | status_t EventHub::closeDeviceByPathLocked(const char *devicePath) { |
| 1149 | Device* device = getDeviceByPathLocked(devicePath); |
| 1150 | if (device) { |
| 1151 | closeDeviceLocked(device); |
| 1152 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1154 | 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] | 1155 | return -1; |
| 1156 | } |
| 1157 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1158 | void EventHub::closeAllDevicesLocked() { |
| 1159 | while (mDevices.size() > 0) { |
| 1160 | closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1)); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | void EventHub::closeDeviceLocked(Device* device) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1165 | 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] | 1166 | device->path.string(), device->identifier.name.string(), device->id, |
| 1167 | device->fd, device->classes); |
| 1168 | |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1169 | if (device->id == mBuiltInKeyboardId) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1170 | 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] | 1171 | device->path.string(), mBuiltInKeyboardId); |
| 1172 | mBuiltInKeyboardId = -1; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1173 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1174 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1175 | if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1176 | ALOGW("Could not remove device fd from epoll instance. errno=%d", errno); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | mDevices.removeItem(device->id); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1180 | device->close(); |
| 1181 | |
Jeff Brown | 8e9d443 | 2011-03-12 19:46:59 -0800 | [diff] [blame] | 1182 | // Unlink for opening devices list if it is present. |
| 1183 | Device* pred = NULL; |
| 1184 | bool found = false; |
| 1185 | for (Device* entry = mOpeningDevices; entry != NULL; ) { |
| 1186 | if (entry == device) { |
| 1187 | found = true; |
| 1188 | break; |
| 1189 | } |
| 1190 | pred = entry; |
| 1191 | entry = entry->next; |
| 1192 | } |
| 1193 | if (found) { |
| 1194 | // Unlink the device from the opening devices list then delete it. |
| 1195 | // We don't need to tell the client that the device was closed because |
| 1196 | // it does not even know it was opened in the first place. |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1197 | ALOGI("Device %s was immediately closed after opening.", device->path.string()); |
Jeff Brown | 8e9d443 | 2011-03-12 19:46:59 -0800 | [diff] [blame] | 1198 | if (pred) { |
| 1199 | pred->next = device->next; |
| 1200 | } else { |
| 1201 | mOpeningDevices = device->next; |
| 1202 | } |
| 1203 | delete device; |
| 1204 | } else { |
| 1205 | // Link into closing devices list. |
| 1206 | // The device will be deleted later after we have informed the client. |
| 1207 | device->next = mClosingDevices; |
| 1208 | mClosingDevices = device; |
| 1209 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1212 | status_t EventHub::readNotifyLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | int res; |
| 1214 | char devname[PATH_MAX]; |
| 1215 | char *filename; |
| 1216 | char event_buf[512]; |
| 1217 | int event_size; |
| 1218 | int event_pos = 0; |
| 1219 | struct inotify_event *event; |
| 1220 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1221 | ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1222 | res = read(mINotifyFd, event_buf, sizeof(event_buf)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | if(res < (int)sizeof(*event)) { |
| 1224 | if(errno == EINTR) |
| 1225 | return 0; |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1226 | ALOGW("could not get event, %s\n", strerror(errno)); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1227 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | } |
| 1229 | //printf("got %d bytes of event information\n", res); |
| 1230 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1231 | strcpy(devname, DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | filename = devname + strlen(devname); |
| 1233 | *filename++ = '/'; |
| 1234 | |
| 1235 | while(res >= (int)sizeof(*event)) { |
| 1236 | event = (struct inotify_event *)(event_buf + event_pos); |
| 1237 | //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); |
| 1238 | if(event->len) { |
| 1239 | strcpy(filename, event->name); |
| 1240 | if(event->mask & IN_CREATE) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1241 | openDeviceLocked(devname); |
| 1242 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1243 | ALOGI("Removing device '%s' due to inotify event\n", devname); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1244 | closeDeviceByPathLocked(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | event_size = sizeof(*event) + event->len; |
| 1248 | res -= event_size; |
| 1249 | event_pos += event_size; |
| 1250 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | return 0; |
| 1252 | } |
| 1253 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1254 | status_t EventHub::scanDirLocked(const char *dirname) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | { |
| 1256 | char devname[PATH_MAX]; |
| 1257 | char *filename; |
| 1258 | DIR *dir; |
| 1259 | struct dirent *de; |
| 1260 | dir = opendir(dirname); |
| 1261 | if(dir == NULL) |
| 1262 | return -1; |
| 1263 | strcpy(devname, dirname); |
| 1264 | filename = devname + strlen(devname); |
| 1265 | *filename++ = '/'; |
| 1266 | while((de = readdir(dir))) { |
| 1267 | if(de->d_name[0] == '.' && |
| 1268 | (de->d_name[1] == '\0' || |
| 1269 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 1270 | continue; |
| 1271 | strcpy(filename, de->d_name); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1272 | openDeviceLocked(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | } |
| 1274 | closedir(dir); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1278 | void EventHub::requestReopenDevices() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1279 | ALOGV("requestReopenDevices() called"); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1280 | |
| 1281 | AutoMutex _l(mLock); |
| 1282 | mNeedToReopenDevices = true; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1285 | void EventHub::dump(String8& dump) { |
| 1286 | dump.append("Event Hub State:\n"); |
| 1287 | |
| 1288 | { // acquire lock |
| 1289 | AutoMutex _l(mLock); |
| 1290 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1291 | dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1292 | |
| 1293 | dump.append(INDENT "Devices:\n"); |
| 1294 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1295 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 1296 | const Device* device = mDevices.valueAt(i); |
| 1297 | if (mBuiltInKeyboardId == device->id) { |
| 1298 | dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", |
| 1299 | device->id, device->identifier.name.string()); |
| 1300 | } else { |
| 1301 | dump.appendFormat(INDENT2 "%d: %s\n", device->id, |
| 1302 | device->identifier.name.string()); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1303 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1304 | dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes); |
| 1305 | dump.appendFormat(INDENT3 "Path: %s\n", device->path.string()); |
| 1306 | dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string()); |
| 1307 | dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string()); |
| 1308 | dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " |
| 1309 | "product=0x%04x, version=0x%04x\n", |
| 1310 | device->identifier.bus, device->identifier.vendor, |
| 1311 | device->identifier.product, device->identifier.version); |
| 1312 | dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", |
| 1313 | device->keyMap.keyLayoutFile.string()); |
| 1314 | dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n", |
| 1315 | device->keyMap.keyCharacterMapFile.string()); |
| 1316 | dump.appendFormat(INDENT3 "ConfigurationFile: %s\n", |
| 1317 | device->configurationFile.string()); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1318 | } |
| 1319 | } // release lock |
| 1320 | } |
| 1321 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 1322 | void EventHub::monitor() { |
| 1323 | // Acquire and release the lock to ensure that the event hub has not deadlocked. |
| 1324 | mLock.lock(); |
| 1325 | mLock.unlock(); |
| 1326 | } |
| 1327 | |
| 1328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1329 | }; // namespace android |