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