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), |
| 104 | classes(0), keyBitmask(NULL), configuration(NULL), virtualKeyMap(NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 107 | EventHub::Device::~Device() { |
| 108 | close(); |
| 109 | delete[] keyBitmask; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 110 | delete configuration; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 111 | delete virtualKeyMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 114 | void EventHub::Device::close() { |
| 115 | if (fd >= 0) { |
| 116 | ::close(fd); |
| 117 | fd = -1; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | // --- EventHub --- |
| 123 | |
| 124 | EventHub::EventHub(void) : |
| 125 | mError(NO_INIT), mBuiltInKeyboardId(-1), mNextDeviceId(1), |
| 126 | mOpeningDevices(0), mClosingDevices(0), |
| 127 | mOpened(false), mNeedToSendFinishedDeviceScan(false), |
| 128 | mInputBufferIndex(0), mInputBufferCount(0), mInputFdIndex(0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 130 | #ifdef EV_SW |
| 131 | memset(mSwitches, 0, sizeof(mSwitches)); |
| 132 | #endif |
| 133 | } |
| 134 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 135 | EventHub::~EventHub(void) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | release_wake_lock(WAKE_LOCK_ID); |
| 137 | // we should free stuff here... |
| 138 | } |
| 139 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 140 | status_t EventHub::errorCheck() const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | return mError; |
| 142 | } |
| 143 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 144 | String8 EventHub::getDeviceName(int32_t deviceId) const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 146 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | if (device == NULL) return String8(); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 148 | return device->identifier.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 151 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 153 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | if (device == NULL) return 0; |
| 155 | return device->classes; |
| 156 | } |
| 157 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 158 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 159 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 160 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 161 | if (device && device->configuration) { |
| 162 | *outConfiguration = *device->configuration; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 163 | } else { |
| 164 | outConfiguration->clear(); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 168 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 169 | RawAbsoluteAxisInfo* outAxisInfo) const { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 170 | outAxisInfo->clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 173 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | if (device == NULL) return -1; |
| 175 | |
| 176 | struct input_absinfo info; |
| 177 | |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 178 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 179 | LOGW("Error reading absolute controller %d for device %s fd %d\n", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 180 | axis, device->identifier.name.string(), device->fd); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 181 | return -errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 183 | |
| 184 | if (info.minimum != info.maximum) { |
| 185 | outAxisInfo->valid = true; |
| 186 | outAxisInfo->minValue = info.minimum; |
| 187 | outAxisInfo->maxValue = info.maximum; |
| 188 | outAxisInfo->flat = info.flat; |
| 189 | outAxisInfo->fuzz = info.fuzz; |
| 190 | } |
| 191 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 194 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 195 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
| 196 | AutoMutex _l(mLock); |
| 197 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 198 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 199 | if (device != NULL) { |
| 200 | return getScanCodeStateLocked(device, scanCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 203 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 206 | int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const { |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 207 | uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)]; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 208 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 209 | if (ioctl(device->fd, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 210 | EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 211 | return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 212 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 213 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 216 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
| 217 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 218 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 219 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 220 | if (device != NULL) { |
| 221 | return getKeyCodeStateLocked(device, keyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 223 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 226 | int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const { |
| 227 | if (!device->keyMap.haveKeyLayout()) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 228 | return AKEY_STATE_UNKNOWN; |
| 229 | } |
| 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | Vector<int32_t> scanCodes; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 232 | device->keyMap.keyLayoutMap->findScanCodes(keyCode, &scanCodes); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 233 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 234 | 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] | 235 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 236 | 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] | 237 | #if 0 |
| 238 | for (size_t i=0; i<=KEY_MAX; i++) { |
| 239 | LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask)); |
| 240 | } |
| 241 | #endif |
| 242 | const size_t N = scanCodes.size(); |
| 243 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 244 | int32_t sc = scanCodes.itemAt(i); |
| 245 | //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask)); |
| 246 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 247 | return AKEY_STATE_DOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 250 | return AKEY_STATE_UP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 252 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 255 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 256 | #ifdef EV_SW |
| 257 | if (sw >= 0 && sw <= SW_MAX) { |
| 258 | AutoMutex _l(mLock); |
| 259 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 260 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 261 | if (device != NULL) { |
| 262 | return getSwitchStateLocked(device, sw); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 263 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 264 | } |
| 265 | #endif |
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 | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 269 | int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const { |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 270 | uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)]; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 271 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 272 | if (ioctl(device->fd, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 273 | EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 274 | return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 275 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 276 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 279 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 280 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 281 | AutoMutex _l(mLock); |
| 282 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 283 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 284 | if (device != NULL) { |
| 285 | return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags); |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 290 | bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 291 | const int32_t* keyCodes, uint8_t* outFlags) const { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 292 | if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | |
| 296 | Vector<int32_t> scanCodes; |
| 297 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
| 298 | scanCodes.clear(); |
| 299 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 300 | status_t err = device->keyMap.keyLayoutMap->findScanCodes(keyCodes[codeIndex], &scanCodes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 301 | if (! err) { |
| 302 | // check the possible scan codes identified by the layout map against the |
| 303 | // map of codes actually emitted by the driver |
| 304 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
| 305 | if (test_bit(scanCodes[sc], device->keyBitmask)) { |
| 306 | outFlags[codeIndex] = 1; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | return true; |
| 313 | } |
| 314 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 315 | status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode, |
| 316 | int32_t* outKeycode, uint32_t* outFlags) const |
| 317 | { |
| 318 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 319 | Device* device = getDeviceLocked(deviceId); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 320 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 321 | if (device && device->keyMap.haveKeyLayout()) { |
| 322 | status_t err = device->keyMap.keyLayoutMap->map(scancode, outKeycode, outFlags); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 323 | if (err == NO_ERROR) { |
| 324 | return NO_ERROR; |
| 325 | } |
| 326 | } |
| 327 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 328 | if (mBuiltInKeyboardId != -1) { |
| 329 | device = getDeviceLocked(mBuiltInKeyboardId); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 330 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 331 | if (device && device->keyMap.haveKeyLayout()) { |
| 332 | status_t err = device->keyMap.keyLayoutMap->map(scancode, outKeycode, outFlags); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 333 | if (err == NO_ERROR) { |
| 334 | return NO_ERROR; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | *outKeycode = 0; |
| 340 | *outFlags = 0; |
| 341 | return NAME_NOT_FOUND; |
| 342 | } |
| 343 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 344 | void EventHub::addExcludedDevice(const char* deviceName) |
| 345 | { |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 346 | AutoMutex _l(mLock); |
| 347 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 348 | String8 name(deviceName); |
| 349 | mExcludedDevices.push_back(name); |
| 350 | } |
| 351 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 352 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { |
| 353 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 354 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 355 | if (device) { |
| 356 | uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)]; |
| 357 | memset(bitmask, 0, sizeof(bitmask)); |
| 358 | if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) { |
| 359 | if (test_bit(led, bitmask)) { |
| 360 | return true; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
| 368 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 369 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 370 | if (device) { |
| 371 | struct input_event ev; |
| 372 | ev.time.tv_sec = 0; |
| 373 | ev.time.tv_usec = 0; |
| 374 | ev.type = EV_LED; |
| 375 | ev.code = led; |
| 376 | ev.value = on ? 1 : 0; |
| 377 | |
| 378 | ssize_t nWrite; |
| 379 | do { |
| 380 | nWrite = write(device->fd, &ev, sizeof(struct input_event)); |
| 381 | } while (nWrite == -1 && errno == EINTR); |
| 382 | } |
| 383 | } |
| 384 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 385 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, |
| 386 | Vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 387 | outVirtualKeys.clear(); |
| 388 | |
| 389 | AutoMutex _l(mLock); |
| 390 | Device* device = getDeviceLocked(deviceId); |
| 391 | if (device && device->virtualKeyMap) { |
| 392 | outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys()); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { |
| 397 | if (deviceId == 0) { |
| 398 | deviceId = mBuiltInKeyboardId; |
| 399 | } |
| 400 | |
| 401 | size_t numDevices = mDevices.size(); |
| 402 | for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < numDevices; i++) { |
| 403 | Device* device = mDevices[i]; |
| 404 | if (device->id == deviceId) { |
| 405 | return device; |
| 406 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | } |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 411 | bool EventHub::getEvent(RawEvent* outEvent) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 412 | outEvent->deviceId = 0; |
| 413 | outEvent->type = 0; |
| 414 | outEvent->scanCode = 0; |
| 415 | outEvent->keyCode = 0; |
| 416 | outEvent->flags = 0; |
| 417 | outEvent->value = 0; |
| 418 | outEvent->when = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | // Note that we only allow one caller to getEvent(), so don't need |
| 421 | // to do locking here... only when adding/removing devices. |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 422 | |
| 423 | if (!mOpened) { |
| 424 | mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR; |
| 425 | mOpened = true; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 426 | mNeedToSendFinishedDeviceScan = true; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 427 | } |
| 428 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 429 | for (;;) { |
| 430 | // Report any devices that had last been added/removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | if (mClosingDevices != NULL) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 432 | Device* device = mClosingDevices; |
| 433 | LOGV("Reporting device closed: id=%d, name=%s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | device->id, device->path.string()); |
| 435 | mClosingDevices = device->next; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 436 | if (device->id == mBuiltInKeyboardId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 437 | outEvent->deviceId = 0; |
| 438 | } else { |
| 439 | outEvent->deviceId = device->id; |
| 440 | } |
| 441 | outEvent->type = DEVICE_REMOVED; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 442 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | delete device; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 444 | mNeedToSendFinishedDeviceScan = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | return true; |
| 446 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 447 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | if (mOpeningDevices != NULL) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 449 | Device* device = mOpeningDevices; |
| 450 | LOGV("Reporting device opened: id=%d, name=%s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | device->id, device->path.string()); |
| 452 | mOpeningDevices = device->next; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 453 | if (device->id == mBuiltInKeyboardId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 454 | outEvent->deviceId = 0; |
| 455 | } else { |
| 456 | outEvent->deviceId = device->id; |
| 457 | } |
| 458 | outEvent->type = DEVICE_ADDED; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 459 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 460 | mNeedToSendFinishedDeviceScan = true; |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | if (mNeedToSendFinishedDeviceScan) { |
| 465 | mNeedToSendFinishedDeviceScan = false; |
| 466 | outEvent->type = FINISHED_DEVICE_SCAN; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 467 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 471 | // Grab the next input event. |
| 472 | for (;;) { |
| 473 | // Consume buffered input events, if any. |
| 474 | if (mInputBufferIndex < mInputBufferCount) { |
| 475 | const struct input_event& iev = mInputBufferData[mInputBufferIndex++]; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 476 | const Device* device = mDevices[mInputFdIndex]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 478 | LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(), |
| 479 | (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 480 | if (device->id == mBuiltInKeyboardId) { |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 481 | outEvent->deviceId = 0; |
| 482 | } else { |
| 483 | outEvent->deviceId = device->id; |
| 484 | } |
| 485 | outEvent->type = iev.type; |
| 486 | outEvent->scanCode = iev.code; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 487 | outEvent->flags = 0; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 488 | if (iev.type == EV_KEY) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 489 | outEvent->keyCode = AKEYCODE_UNKNOWN; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 490 | if (device->keyMap.haveKeyLayout()) { |
| 491 | status_t err = device->keyMap.keyLayoutMap->map(iev.code, |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 492 | &outEvent->keyCode, &outEvent->flags); |
| 493 | LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n", |
| 494 | iev.code, outEvent->keyCode, outEvent->flags, err); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 496 | } else { |
| 497 | outEvent->keyCode = iev.code; |
| 498 | } |
| 499 | outEvent->value = iev.value; |
| 500 | |
| 501 | // Use an event timestamp in the same timebase as |
| 502 | // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis() |
| 503 | // as expected by the rest of the system. |
| 504 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | // Finish reading all events from devices identified in previous poll(). |
| 509 | // This code assumes that mInputDeviceIndex is initially 0 and that the |
| 510 | // revents member of pollfd is initialized to 0 when the device is first added. |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 511 | // Since mFds[0] is used for inotify, we process regular events starting at index 1. |
| 512 | mInputFdIndex += 1; |
| 513 | if (mInputFdIndex >= mFds.size()) { |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 517 | const struct pollfd& pfd = mFds[mInputFdIndex]; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 518 | if (pfd.revents & POLLIN) { |
| 519 | int32_t readSize = read(pfd.fd, mInputBufferData, |
| 520 | sizeof(struct input_event) * INPUT_BUFFER_SIZE); |
| 521 | if (readSize < 0) { |
| 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); |
| 527 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 528 | mInputBufferCount = size_t(readSize) / sizeof(struct input_event); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 529 | mInputBufferIndex = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 533 | |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 534 | #if HAVE_INOTIFY |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 535 | // 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] | 536 | // processing all other events. |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 537 | if(mFds[0].revents & POLLIN) { |
| 538 | readNotify(mFds[0].fd); |
| 539 | mFds.editItemAt(0).revents = 0; |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 540 | continue; // report added or removed devices immediately |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 542 | #endif |
| 543 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 544 | mInputFdIndex = 0; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 545 | |
| 546 | // Poll for events. Mind the wake lock dance! |
| 547 | // We hold a wake lock at all times except during poll(). This works due to some |
| 548 | // subtle choreography. When a device driver has pending (unread) events, it acquires |
| 549 | // a kernel wake lock. However, once the last pending event has been read, the device |
| 550 | // driver will release the kernel wake lock. To prevent the system from going to sleep |
| 551 | // when this happens, the EventHub holds onto its own user wake lock while the client |
| 552 | // is processing events. Thus the system can only sleep if there are no events |
| 553 | // pending or currently being processed. |
| 554 | release_wake_lock(WAKE_LOCK_ID); |
| 555 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 556 | int pollResult = poll(mFds.editArray(), mFds.size(), -1); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 557 | |
| 558 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 559 | |
| 560 | if (pollResult <= 0) { |
| 561 | if (errno != EINTR) { |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 562 | LOGW("poll failed (errno=%d)\n", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 563 | usleep(100000); |
| 564 | } |
| 565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Open the platform-specific input device. |
| 571 | */ |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 572 | bool EventHub::openPlatformInput(void) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | /* |
| 574 | * Open platform-specific input device(s). |
| 575 | */ |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 576 | int res, fd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | #ifdef HAVE_INOTIFY |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 579 | fd = inotify_init(); |
| 580 | 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] | 581 | if(res < 0) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 582 | 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] | 583 | } |
| 584 | #else |
| 585 | /* |
| 586 | * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd. |
| 587 | * We allocate space for it and set it to something invalid. |
| 588 | */ |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 589 | fd = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | #endif |
| 591 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 592 | // Reserve fd index 0 for inotify. |
| 593 | struct pollfd pollfd; |
| 594 | pollfd.fd = fd; |
| 595 | pollfd.events = POLLIN; |
| 596 | pollfd.revents = 0; |
| 597 | mFds.push(pollfd); |
| 598 | mDevices.push(NULL); |
| 599 | |
| 600 | res = scanDir(DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | if(res < 0) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 602 | LOGE("scan dir failed for %s\n", DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | return true; |
| 606 | } |
| 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | // ---------------------------------------------------------------------------- |
| 609 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 610 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { |
| 611 | const uint8_t* end = array + endIndex; |
| 612 | array += startIndex; |
| 613 | while (array != end) { |
| 614 | if (*(array++) != 0) { |
| 615 | return true; |
| 616 | } |
| 617 | } |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | static const int32_t GAMEPAD_KEYCODES[] = { |
| 622 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, |
| 623 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, |
| 624 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, |
| 625 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, |
| 626 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, |
| 627 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE |
| 628 | }; |
| 629 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 630 | int EventHub::openDevice(const char *devicePath) { |
| 631 | char buffer[80]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 633 | LOGV("Opening device: %s", devicePath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | |
| 635 | AutoMutex _l(mLock); |
Nick Pelly | e6b1bbd | 2010-01-20 19:36:49 -0800 | [diff] [blame] | 636 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 637 | int fd = open(devicePath, O_RDWR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | if(fd < 0) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 639 | 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] | 640 | return -1; |
| 641 | } |
| 642 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 643 | InputDeviceIdentifier identifier; |
| 644 | |
| 645 | // Get device name. |
| 646 | if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { |
| 647 | //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno)); |
| 648 | } else { |
| 649 | buffer[sizeof(buffer) - 1] = '\0'; |
| 650 | identifier.name.setTo(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | } |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 652 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 653 | // Check to see if the device is on our excluded list |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 654 | List<String8>::iterator iter = mExcludedDevices.begin(); |
| 655 | List<String8>::iterator end = mExcludedDevices.end(); |
| 656 | for ( ; iter != end; iter++) { |
| 657 | const char* test = *iter; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 658 | if (identifier.name == test) { |
| 659 | LOGI("ignoring event id %s driver %s\n", devicePath, test); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 660 | close(fd); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 661 | return -1; |
| 662 | } |
| 663 | } |
| 664 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 665 | // Get device driver version. |
| 666 | int driverVersion; |
| 667 | if(ioctl(fd, EVIOCGVERSION, &driverVersion)) { |
| 668 | LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno)); |
| 669 | close(fd); |
| 670 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 673 | // Get device identifier. |
| 674 | struct input_id inputId; |
| 675 | if(ioctl(fd, EVIOCGID, &inputId)) { |
| 676 | LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno)); |
| 677 | close(fd); |
| 678 | return -1; |
| 679 | } |
| 680 | identifier.bus = inputId.bustype; |
| 681 | identifier.product = inputId.product; |
| 682 | identifier.vendor = inputId.vendor; |
| 683 | identifier.version = inputId.version; |
| 684 | |
| 685 | // Get device physical location. |
| 686 | if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { |
| 687 | //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); |
| 688 | } else { |
| 689 | buffer[sizeof(buffer) - 1] = '\0'; |
| 690 | identifier.location.setTo(buffer); |
| 691 | } |
| 692 | |
| 693 | // Get device unique id. |
| 694 | if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { |
| 695 | //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); |
| 696 | } else { |
| 697 | buffer[sizeof(buffer) - 1] = '\0'; |
| 698 | identifier.uniqueId.setTo(buffer); |
| 699 | } |
| 700 | |
| 701 | // Make file descriptor non-blocking for use with poll(). |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 702 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
| 703 | LOGE("Error %d making device file descriptor non-blocking.", errno); |
| 704 | close(fd); |
| 705 | return -1; |
| 706 | } |
| 707 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 708 | // Allocate device. (The device object takes ownership of the fd at this point.) |
| 709 | int32_t deviceId = mNextDeviceId++; |
| 710 | Device* device = new Device(fd, deviceId, String8(devicePath), identifier); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | |
| 712 | #if 0 |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 713 | LOGI("add device %d: %s\n", deviceId, devicePath); |
| 714 | LOGI(" bus: %04x\n" |
| 715 | " vendor %04x\n" |
| 716 | " product %04x\n" |
| 717 | " version %04x\n", |
| 718 | identifier.bus, identifier.vendor, identifier.product, identifier.version); |
| 719 | LOGI(" name: \"%s\"\n", identifier.name.string()); |
| 720 | LOGI(" location: \"%s\"\n", identifier.location.string()); |
| 721 | LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string()); |
| 722 | LOGI(" driver: v%d.%d.%d\n", |
| 723 | driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | #endif |
| 725 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 726 | // Load the configuration file for the device. |
| 727 | loadConfiguration(device); |
| 728 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 729 | // Figure out the kinds of events the device reports. |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 730 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 731 | 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] | 732 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 733 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | LOGV("Getting keys..."); |
| 735 | if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) { |
| 736 | //LOGI("MAP\n"); |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 737 | //for (int i = 0; i < sizeof(key_bitmask); i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | // LOGI("%d: 0x%02x\n", i, key_bitmask[i]); |
| 739 | //} |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 740 | |
| 741 | // See if this is a keyboard. Ignore everything in the button range except for |
| 742 | // gamepads which are also considered keyboards. |
| 743 | if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC)) |
| 744 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD), |
| 745 | sizeof_bit_array(BTN_DIGI)) |
| 746 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK), |
| 747 | sizeof_bit_array(KEY_MAX + 1))) { |
| 748 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
| 749 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 750 | device->keyBitmask = new uint8_t[sizeof(key_bitmask)]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | if (device->keyBitmask != NULL) { |
| 752 | memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask)); |
| 753 | } else { |
| 754 | delete device; |
| 755 | LOGE("out of memory allocating key bitmask"); |
| 756 | return -1; |
| 757 | } |
| 758 | } |
| 759 | } |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 760 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 761 | // See if this is a cursor device such as a trackball or mouse. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | if (test_bit(BTN_MOUSE, key_bitmask)) { |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 763 | uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | memset(rel_bitmask, 0, sizeof(rel_bitmask)); |
| 765 | LOGV("Getting relative controllers..."); |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 766 | if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) { |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 768 | device->classes |= INPUT_DEVICE_CLASS_CURSOR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 772 | |
| 773 | // See if this is a touch pad. |
| 774 | uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)]; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 775 | memset(abs_bitmask, 0, sizeof(abs_bitmask)); |
| 776 | LOGV("Getting absolute controllers..."); |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 777 | if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) { |
| 778 | // Is this a new modern multi-touch driver? |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 779 | if (test_bit(ABS_MT_POSITION_X, abs_bitmask) |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 780 | && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) { |
| 781 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT; |
| 782 | |
| 783 | // Is this an old style single-touch driver? |
| 784 | } else if (test_bit(BTN_TOUCH, key_bitmask) |
| 785 | && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) { |
| 786 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN; |
| 787 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | #ifdef EV_SW |
| 791 | // figure out the switches this device reports |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 792 | uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 794 | bool hasSwitches = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
| 796 | for (int i=0; i<EV_SW; i++) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 797 | //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | if (test_bit(i, sw_bitmask)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 799 | hasSwitches = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | if (mSwitches[i] == 0) { |
| 801 | mSwitches[i] = device->id; |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 806 | if (hasSwitches) { |
| 807 | device->classes |= INPUT_DEVICE_CLASS_SWITCH; |
| 808 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | #endif |
| 810 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 811 | if ((device->classes & INPUT_DEVICE_CLASS_TOUCHSCREEN)) { |
| 812 | // Load the virtual keys for the touch screen, if any. |
| 813 | // We do this now so that we can make sure to load the keymap if necessary. |
| 814 | status_t status = loadVirtualKeyMap(device); |
| 815 | if (!status) { |
| 816 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) { |
| 821 | // Load the keymap for the device. |
| 822 | status_t status = loadKeyMap(device); |
| 823 | |
| 824 | // Set system properties for the keyboard. |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 825 | setKeyboardProperties(device, false); |
| 826 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 827 | // Register the keyboard as a built-in keyboard if it is eligible. |
| 828 | if (!status |
| 829 | && mBuiltInKeyboardId == -1 |
| 830 | && isEligibleBuiltInKeyboard(device->identifier, |
| 831 | device->configuration, &device->keyMap)) { |
| 832 | mBuiltInKeyboardId = device->id; |
| 833 | setKeyboardProperties(device, true); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 834 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 836 | // '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] | 837 | if (hasKeycodeLocked(device, AKEYCODE_Q)) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 838 | device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 839 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 840 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 841 | // See if this device has a DPAD. |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 842 | if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) && |
| 843 | hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) && |
| 844 | hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) && |
| 845 | hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) && |
| 846 | hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 847 | device->classes |= INPUT_DEVICE_CLASS_DPAD; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 848 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 849 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 850 | // See if this device has a gamepad. |
Kenny Root | 1d79a9d | 2010-10-21 15:46:03 -0700 | [diff] [blame] | 851 | 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] | 852 | if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) { |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 853 | device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; |
| 854 | break; |
| 855 | } |
| 856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | } |
| 858 | |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 859 | // If the device isn't recognized as something we handle, don't monitor it. |
| 860 | if (device->classes == 0) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 861 | LOGV("Dropping device: id=%d, path='%s', name='%s'", |
| 862 | deviceId, devicePath, device->identifier.name.string()); |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 863 | delete device; |
| 864 | return -1; |
| 865 | } |
| 866 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 867 | LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, " |
| 868 | "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s", |
| 869 | deviceId, fd, devicePath, device->identifier.name.string(), |
| 870 | device->classes, |
| 871 | device->configurationFile.string(), |
| 872 | device->keyMap.keyLayoutFile.string(), |
| 873 | device->keyMap.keyCharacterMapFile.string(), |
| 874 | toString(mBuiltInKeyboardId == deviceId)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 875 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 876 | struct pollfd pollfd; |
| 877 | pollfd.fd = fd; |
| 878 | pollfd.events = POLLIN; |
| 879 | pollfd.revents = 0; |
| 880 | mFds.push(pollfd); |
| 881 | mDevices.push(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | device->next = mOpeningDevices; |
| 884 | mOpeningDevices = device; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | return 0; |
| 886 | } |
| 887 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 888 | void EventHub::loadConfiguration(Device* device) { |
| 889 | device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier( |
| 890 | device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 891 | if (device->configurationFile.isEmpty()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 892 | LOGD("No input device configuration file found for device '%s'.", |
| 893 | device->identifier.name.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 894 | } else { |
| 895 | status_t status = PropertyMap::load(device->configurationFile, |
| 896 | &device->configuration); |
| 897 | if (status) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 898 | LOGE("Error loading input device configuration file for device '%s'. " |
| 899 | "Using default configuration.", |
| 900 | device->identifier.name.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 905 | status_t EventHub::loadVirtualKeyMap(Device* device) { |
| 906 | // The virtual key map is supplied by the kernel as a system board property file. |
| 907 | String8 path; |
| 908 | path.append("/sys/board_properties/virtualkeys."); |
| 909 | path.append(device->identifier.name); |
| 910 | if (access(path.string(), R_OK)) { |
| 911 | return NAME_NOT_FOUND; |
| 912 | } |
| 913 | return VirtualKeyMap::load(path, &device->virtualKeyMap); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 916 | status_t EventHub::loadKeyMap(Device* device) { |
| 917 | return device->keyMap.load(device->identifier, device->configuration); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 920 | void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) { |
| 921 | int32_t id = builtInKeyboard ? 0 : device->id; |
| 922 | android::setKeyboardProperties(id, device->identifier, |
| 923 | device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile); |
| 924 | } |
| 925 | |
| 926 | void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) { |
| 927 | int32_t id = builtInKeyboard ? 0 : device->id; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 928 | android::clearKeyboardProperties(id); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 931 | bool EventHub::hasKeycodeLocked(Device* device, int keycode) const { |
| 932 | if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 933 | return false; |
| 934 | } |
| 935 | |
| 936 | Vector<int32_t> scanCodes; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 937 | device->keyMap.keyLayoutMap->findScanCodes(keycode, &scanCodes); |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 938 | const size_t N = scanCodes.size(); |
| 939 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 940 | int32_t sc = scanCodes.itemAt(i); |
| 941 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { |
| 942 | return true; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | return false; |
| 947 | } |
| 948 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 949 | int EventHub::closeDevice(const char *devicePath) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | AutoMutex _l(mLock); |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 951 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 952 | for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) { |
| 953 | Device* device = mDevices[i]; |
| 954 | if (device->path == devicePath) { |
| 955 | LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n", |
| 956 | device->path.string(), device->identifier.name.string(), device->id, |
| 957 | device->fd, device->classes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | |
| 959 | #ifdef EV_SW |
| 960 | for (int j=0; j<EV_SW; j++) { |
| 961 | if (mSwitches[j] == device->id) { |
| 962 | mSwitches[j] = 0; |
| 963 | } |
| 964 | } |
| 965 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 967 | if (device->id == mBuiltInKeyboardId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 969 | device->path.string(), mBuiltInKeyboardId); |
| 970 | mBuiltInKeyboardId = -1; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 971 | clearKeyboardProperties(device, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 973 | clearKeyboardProperties(device, false); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 974 | |
| 975 | mFds.removeAt(i); |
| 976 | mDevices.removeAt(i); |
| 977 | device->close(); |
| 978 | |
| 979 | device->next = mClosingDevices; |
| 980 | mClosingDevices = device; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | return 0; |
| 982 | } |
| 983 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 984 | LOGE("remove device: %s not found\n", devicePath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | return -1; |
| 986 | } |
| 987 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 988 | int EventHub::readNotify(int nfd) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | #ifdef HAVE_INOTIFY |
| 990 | int res; |
| 991 | char devname[PATH_MAX]; |
| 992 | char *filename; |
| 993 | char event_buf[512]; |
| 994 | int event_size; |
| 995 | int event_pos = 0; |
| 996 | struct inotify_event *event; |
| 997 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 998 | LOGV("EventHub::readNotify nfd: %d\n", nfd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | res = read(nfd, event_buf, sizeof(event_buf)); |
| 1000 | if(res < (int)sizeof(*event)) { |
| 1001 | if(errno == EINTR) |
| 1002 | return 0; |
| 1003 | LOGW("could not get event, %s\n", strerror(errno)); |
| 1004 | return 1; |
| 1005 | } |
| 1006 | //printf("got %d bytes of event information\n", res); |
| 1007 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1008 | strcpy(devname, DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | filename = devname + strlen(devname); |
| 1010 | *filename++ = '/'; |
| 1011 | |
| 1012 | while(res >= (int)sizeof(*event)) { |
| 1013 | event = (struct inotify_event *)(event_buf + event_pos); |
| 1014 | //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); |
| 1015 | if(event->len) { |
| 1016 | strcpy(filename, event->name); |
| 1017 | if(event->mask & IN_CREATE) { |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1018 | openDevice(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | } |
| 1020 | else { |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1021 | closeDevice(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | event_size = sizeof(*event) + event->len; |
| 1025 | res -= event_size; |
| 1026 | event_pos += event_size; |
| 1027 | } |
| 1028 | #endif |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1032 | int EventHub::scanDir(const char *dirname) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | { |
| 1034 | char devname[PATH_MAX]; |
| 1035 | char *filename; |
| 1036 | DIR *dir; |
| 1037 | struct dirent *de; |
| 1038 | dir = opendir(dirname); |
| 1039 | if(dir == NULL) |
| 1040 | return -1; |
| 1041 | strcpy(devname, dirname); |
| 1042 | filename = devname + strlen(devname); |
| 1043 | *filename++ = '/'; |
| 1044 | while((de = readdir(dir))) { |
| 1045 | if(de->d_name[0] == '.' && |
| 1046 | (de->d_name[1] == '\0' || |
| 1047 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 1048 | continue; |
| 1049 | strcpy(filename, de->d_name); |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1050 | openDevice(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | } |
| 1052 | closedir(dir); |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1056 | void EventHub::dump(String8& dump) { |
| 1057 | dump.append("Event Hub State:\n"); |
| 1058 | |
| 1059 | { // acquire lock |
| 1060 | AutoMutex _l(mLock); |
| 1061 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1062 | dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1063 | |
| 1064 | dump.append(INDENT "Devices:\n"); |
| 1065 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1066 | for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) { |
| 1067 | const Device* device = mDevices[i]; |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1068 | if (device) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1069 | if (mBuiltInKeyboardId == device->id) { |
| 1070 | dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", |
| 1071 | device->id, device->identifier.name.string()); |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1072 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1073 | dump.appendFormat(INDENT2 "%d: %s\n", device->id, |
| 1074 | device->identifier.name.string()); |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1075 | } |
| 1076 | dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes); |
| 1077 | dump.appendFormat(INDENT3 "Path: %s\n", device->path.string()); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1078 | dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string()); |
| 1079 | dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string()); |
| 1080 | dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " |
| 1081 | "product=0x%04x, version=0x%04x\n", |
| 1082 | device->identifier.bus, device->identifier.vendor, |
| 1083 | device->identifier.product, device->identifier.version); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1084 | dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1085 | device->keyMap.keyLayoutFile.string()); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1086 | dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1087 | device->keyMap.keyCharacterMapFile.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1088 | dump.appendFormat(INDENT3 "ConfigurationFile: %s\n", |
| 1089 | device->configurationFile.string()); |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | } // release lock |
| 1093 | } |
| 1094 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | }; // namespace android |