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