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