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