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