The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2005 The Android Open Source Project |
| 3 | // |
| 4 | // Handle events, like key input and vsync. |
| 5 | // |
| 6 | // The goal is to provide an optimized solution for Linux, not an |
| 7 | // implementation that works well across all platforms. We expect |
| 8 | // events to arrive on file descriptors, so that we can use a select() |
| 9 | // select() call to sleep. |
| 10 | // |
| 11 | // We can't select() on anything but network sockets in Windows, so we |
| 12 | // provide an alternative implementation of waitEvent for that platform. |
| 13 | // |
| 14 | #define LOG_TAG "EventHub" |
| 15 | |
| 16 | //#define LOG_NDEBUG 0 |
| 17 | |
| 18 | #include <ui/EventHub.h> |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 19 | #include <ui/KeycodeLabels.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <hardware_legacy/power.h> |
| 21 | |
| 22 | #include <cutils/properties.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | #include <utils/Timers.h> |
Mathias Agopian | 3b4062e | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 25 | #include <utils/threads.h> |
Mathias Agopian | 3b4062e | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 26 | #include <utils/Errors.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <unistd.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <memory.h> |
| 33 | #include <errno.h> |
| 34 | #include <assert.h> |
| 35 | |
| 36 | #include "KeyLayoutMap.h" |
| 37 | |
| 38 | #include <string.h> |
| 39 | #include <stdint.h> |
| 40 | #include <dirent.h> |
| 41 | #ifdef HAVE_INOTIFY |
| 42 | # include <sys/inotify.h> |
| 43 | #endif |
| 44 | #ifdef HAVE_ANDROID_OS |
| 45 | # include <sys/limits.h> /* not part of Linux */ |
| 46 | #endif |
| 47 | #include <sys/poll.h> |
| 48 | #include <sys/ioctl.h> |
| 49 | |
| 50 | /* this macro is used to tell if "bit" is set in "array" |
| 51 | * it selects a byte from the array, and does a boolean AND |
| 52 | * operation with a byte that only has the relevant bit set. |
| 53 | * eg. to check for the 12th bit, we do (array[1] & 1<<4) |
| 54 | */ |
| 55 | #define test_bit(bit, array) (array[bit/8] & (1<<(bit%8))) |
| 56 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 57 | /* this macro computes the number of bytes needed to represent a bit array of the specified size */ |
| 58 | #define sizeof_bit_array(bits) ((bits + 7) / 8) |
| 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | #define ID_MASK 0x0000ffff |
| 61 | #define SEQ_MASK 0x7fff0000 |
| 62 | #define SEQ_SHIFT 16 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 64 | #ifndef ABS_MT_TOUCH_MAJOR |
| 65 | #define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */ |
| 66 | #endif |
| 67 | |
| 68 | #ifndef ABS_MT_POSITION_X |
| 69 | #define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */ |
| 70 | #endif |
| 71 | |
| 72 | #ifndef ABS_MT_POSITION_Y |
| 73 | #define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */ |
| 74 | #endif |
| 75 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 76 | #define INDENT " " |
| 77 | #define INDENT2 " " |
| 78 | #define INDENT3 " " |
| 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | namespace android { |
| 81 | |
| 82 | static const char *WAKE_LOCK_ID = "KeyEvents"; |
| 83 | static const char *device_path = "/dev/input"; |
| 84 | |
| 85 | /* return the larger integer */ |
| 86 | static inline int max(int v1, int v2) |
| 87 | { |
| 88 | return (v1 > v2) ? v1 : v2; |
| 89 | } |
| 90 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 91 | static inline const char* toString(bool value) { |
| 92 | return value ? "true" : "false"; |
| 93 | } |
| 94 | |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 95 | EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name) |
| 96 | : id(_id), path(_path), name(name), classes(0) |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 97 | , keyBitmask(NULL), layoutMap(new KeyLayoutMap()), defaultKeyMap(false), fd(-1), next(NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | EventHub::device_t::~device_t() { |
| 101 | delete [] keyBitmask; |
| 102 | delete layoutMap; |
| 103 | } |
| 104 | |
| 105 | EventHub::EventHub(void) |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 106 | : mError(NO_INIT), mHaveFirstKeyboard(false), mFirstKeyboardId(-1) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | , mDevicesById(0), mNumDevicesById(0) |
| 108 | , mOpeningDevices(0), mClosingDevices(0) |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 109 | , mDevices(0), mFDs(0), mFDCount(0), mOpened(false), mNeedToSendFinishedDeviceScan(false) |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 110 | , mInputBufferIndex(0), mInputBufferCount(0), mInputDeviceIndex(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | { |
| 112 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 113 | #ifdef EV_SW |
| 114 | memset(mSwitches, 0, sizeof(mSwitches)); |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Clean up. |
| 120 | */ |
| 121 | EventHub::~EventHub(void) |
| 122 | { |
| 123 | release_wake_lock(WAKE_LOCK_ID); |
| 124 | // we should free stuff here... |
| 125 | } |
| 126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | status_t EventHub::errorCheck() const |
| 128 | { |
| 129 | return mError; |
| 130 | } |
| 131 | |
| 132 | String8 EventHub::getDeviceName(int32_t deviceId) const |
| 133 | { |
| 134 | AutoMutex _l(mLock); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 135 | device_t* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | if (device == NULL) return String8(); |
| 137 | return device->name; |
| 138 | } |
| 139 | |
| 140 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const |
| 141 | { |
| 142 | AutoMutex _l(mLock); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 143 | device_t* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | if (device == NULL) return 0; |
| 145 | return device->classes; |
| 146 | } |
| 147 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 148 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 149 | RawAbsoluteAxisInfo* outAxisInfo) const { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 150 | outAxisInfo->clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | AutoMutex _l(mLock); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 153 | device_t* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | if (device == NULL) return -1; |
| 155 | |
| 156 | struct input_absinfo info; |
| 157 | |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 158 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 159 | LOGW("Error reading absolute controller %d for device %s fd %d\n", |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 160 | axis, device->name.string(), device->fd); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 161 | return -errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 163 | |
| 164 | if (info.minimum != info.maximum) { |
| 165 | outAxisInfo->valid = true; |
| 166 | outAxisInfo->minValue = info.minimum; |
| 167 | outAxisInfo->maxValue = info.maximum; |
| 168 | outAxisInfo->flat = info.flat; |
| 169 | outAxisInfo->fuzz = info.fuzz; |
| 170 | } |
| 171 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 174 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 175 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
| 176 | AutoMutex _l(mLock); |
| 177 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 178 | device_t* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 179 | if (device != NULL) { |
| 180 | return getScanCodeStateLocked(device, scanCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 183 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 186 | int32_t EventHub::getScanCodeStateLocked(device_t* device, int32_t scanCode) const { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 187 | uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)]; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 188 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 189 | if (ioctl(device->fd, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 190 | EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 191 | return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 192 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 193 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 196 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
| 197 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 198 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 199 | device_t* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 200 | if (device != NULL) { |
| 201 | return getKeyCodeStateLocked(device, keyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 206 | int32_t EventHub::getKeyCodeStateLocked(device_t* device, int32_t keyCode) const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | Vector<int32_t> scanCodes; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 208 | device->layoutMap->findScancodes(keyCode, &scanCodes); |
| 209 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 210 | 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] | 211 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 212 | 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] | 213 | #if 0 |
| 214 | for (size_t i=0; i<=KEY_MAX; i++) { |
| 215 | LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask)); |
| 216 | } |
| 217 | #endif |
| 218 | const size_t N = scanCodes.size(); |
| 219 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 220 | int32_t sc = scanCodes.itemAt(i); |
| 221 | //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask)); |
| 222 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 223 | return AKEY_STATE_DOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 226 | return AKEY_STATE_UP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 228 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 231 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 232 | #ifdef EV_SW |
| 233 | if (sw >= 0 && sw <= SW_MAX) { |
| 234 | AutoMutex _l(mLock); |
| 235 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 236 | device_t* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 237 | if (device != NULL) { |
| 238 | return getSwitchStateLocked(device, sw); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 239 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 240 | } |
| 241 | #endif |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 242 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | int32_t EventHub::getSwitchStateLocked(device_t* device, int32_t sw) const { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 246 | uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)]; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 247 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 248 | if (ioctl(device->fd, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 249 | EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 250 | return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 251 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 252 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 255 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 256 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 257 | AutoMutex _l(mLock); |
| 258 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 259 | device_t* device = getDeviceLocked(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 260 | if (device != NULL) { |
| 261 | return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags); |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | bool EventHub::markSupportedKeyCodesLocked(device_t* device, size_t numCodes, |
| 267 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 268 | if (device->layoutMap == NULL || device->keyBitmask == NULL) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | Vector<int32_t> scanCodes; |
| 273 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
| 274 | scanCodes.clear(); |
| 275 | |
| 276 | status_t err = device->layoutMap->findScancodes(keyCodes[codeIndex], &scanCodes); |
| 277 | if (! err) { |
| 278 | // check the possible scan codes identified by the layout map against the |
| 279 | // map of codes actually emitted by the driver |
| 280 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
| 281 | if (test_bit(scanCodes[sc], device->keyBitmask)) { |
| 282 | outFlags[codeIndex] = 1; |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 291 | status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode, |
| 292 | int32_t* outKeycode, uint32_t* outFlags) const |
| 293 | { |
| 294 | AutoMutex _l(mLock); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 295 | device_t* device = getDeviceLocked(deviceId); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 296 | |
| 297 | if (device != NULL && device->layoutMap != NULL) { |
| 298 | status_t err = device->layoutMap->map(scancode, outKeycode, outFlags); |
| 299 | if (err == NO_ERROR) { |
| 300 | return NO_ERROR; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (mHaveFirstKeyboard) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 305 | device = getDeviceLocked(mFirstKeyboardId); |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 306 | |
| 307 | if (device != NULL && device->layoutMap != NULL) { |
| 308 | status_t err = device->layoutMap->map(scancode, outKeycode, outFlags); |
| 309 | if (err == NO_ERROR) { |
| 310 | return NO_ERROR; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | *outKeycode = 0; |
| 316 | *outFlags = 0; |
| 317 | return NAME_NOT_FOUND; |
| 318 | } |
| 319 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 320 | void EventHub::addExcludedDevice(const char* deviceName) |
| 321 | { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 322 | AutoMutex _l(mLock); |
| 323 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 324 | String8 name(deviceName); |
| 325 | mExcludedDevices.push_back(name); |
| 326 | } |
| 327 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 328 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { |
| 329 | AutoMutex _l(mLock); |
| 330 | device_t* device = getDeviceLocked(deviceId); |
| 331 | if (device) { |
| 332 | uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)]; |
| 333 | memset(bitmask, 0, sizeof(bitmask)); |
| 334 | if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) { |
| 335 | if (test_bit(led, bitmask)) { |
| 336 | return true; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
| 344 | AutoMutex _l(mLock); |
| 345 | device_t* device = getDeviceLocked(deviceId); |
| 346 | if (device) { |
| 347 | struct input_event ev; |
| 348 | ev.time.tv_sec = 0; |
| 349 | ev.time.tv_usec = 0; |
| 350 | ev.type = EV_LED; |
| 351 | ev.code = led; |
| 352 | ev.value = on ? 1 : 0; |
| 353 | |
| 354 | ssize_t nWrite; |
| 355 | do { |
| 356 | nWrite = write(device->fd, &ev, sizeof(struct input_event)); |
| 357 | } while (nWrite == -1 && errno == EINTR); |
| 358 | } |
| 359 | } |
| 360 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 361 | EventHub::device_t* EventHub::getDeviceLocked(int32_t deviceId) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | { |
| 363 | if (deviceId == 0) deviceId = mFirstKeyboardId; |
| 364 | int32_t id = deviceId & ID_MASK; |
| 365 | if (id >= mNumDevicesById || id < 0) return NULL; |
| 366 | device_t* dev = mDevicesById[id].device; |
Dianne Hackborn | 4e829f0 | 2009-03-25 16:21:55 -0700 | [diff] [blame] | 367 | if (dev == NULL) return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | if (dev->id == deviceId) { |
| 369 | return dev; |
| 370 | } |
| 371 | return NULL; |
| 372 | } |
| 373 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 374 | bool EventHub::getEvent(RawEvent* outEvent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 376 | outEvent->deviceId = 0; |
| 377 | outEvent->type = 0; |
| 378 | outEvent->scanCode = 0; |
| 379 | outEvent->keyCode = 0; |
| 380 | outEvent->flags = 0; |
| 381 | outEvent->value = 0; |
| 382 | outEvent->when = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | // Note that we only allow one caller to getEvent(), so don't need |
| 385 | // to do locking here... only when adding/removing devices. |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 386 | |
| 387 | if (!mOpened) { |
| 388 | mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR; |
| 389 | mOpened = true; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 390 | mNeedToSendFinishedDeviceScan = true; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 393 | for (;;) { |
| 394 | // Report any devices that had last been added/removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | if (mClosingDevices != NULL) { |
| 396 | device_t* device = mClosingDevices; |
| 397 | LOGV("Reporting device closed: id=0x%x, name=%s\n", |
| 398 | device->id, device->path.string()); |
| 399 | mClosingDevices = device->next; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 400 | if (device->id == mFirstKeyboardId) { |
| 401 | outEvent->deviceId = 0; |
| 402 | } else { |
| 403 | outEvent->deviceId = device->id; |
| 404 | } |
| 405 | outEvent->type = DEVICE_REMOVED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | delete device; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 407 | mNeedToSendFinishedDeviceScan = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | return true; |
| 409 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | if (mOpeningDevices != NULL) { |
| 412 | device_t* device = mOpeningDevices; |
| 413 | LOGV("Reporting device opened: id=0x%x, name=%s\n", |
| 414 | device->id, device->path.string()); |
| 415 | mOpeningDevices = device->next; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 416 | if (device->id == mFirstKeyboardId) { |
| 417 | outEvent->deviceId = 0; |
| 418 | } else { |
| 419 | outEvent->deviceId = device->id; |
| 420 | } |
| 421 | outEvent->type = DEVICE_ADDED; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 422 | mNeedToSendFinishedDeviceScan = true; |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | if (mNeedToSendFinishedDeviceScan) { |
| 427 | mNeedToSendFinishedDeviceScan = false; |
| 428 | outEvent->type = FINISHED_DEVICE_SCAN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | return true; |
| 430 | } |
| 431 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 432 | // Grab the next input event. |
| 433 | for (;;) { |
| 434 | // Consume buffered input events, if any. |
| 435 | if (mInputBufferIndex < mInputBufferCount) { |
| 436 | const struct input_event& iev = mInputBufferData[mInputBufferIndex++]; |
| 437 | const device_t* device = mDevices[mInputDeviceIndex]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 439 | LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(), |
| 440 | (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value); |
| 441 | if (device->id == mFirstKeyboardId) { |
| 442 | outEvent->deviceId = 0; |
| 443 | } else { |
| 444 | outEvent->deviceId = device->id; |
| 445 | } |
| 446 | outEvent->type = iev.type; |
| 447 | outEvent->scanCode = iev.code; |
| 448 | if (iev.type == EV_KEY) { |
| 449 | status_t err = device->layoutMap->map(iev.code, |
| 450 | & outEvent->keyCode, & outEvent->flags); |
| 451 | LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n", |
| 452 | iev.code, outEvent->keyCode, outEvent->flags, err); |
| 453 | if (err != 0) { |
| 454 | outEvent->keyCode = AKEYCODE_UNKNOWN; |
| 455 | outEvent->flags = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 457 | } else { |
| 458 | outEvent->keyCode = iev.code; |
| 459 | } |
| 460 | outEvent->value = iev.value; |
| 461 | |
| 462 | // Use an event timestamp in the same timebase as |
| 463 | // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis() |
| 464 | // as expected by the rest of the system. |
| 465 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | // Finish reading all events from devices identified in previous poll(). |
| 470 | // This code assumes that mInputDeviceIndex is initially 0 and that the |
| 471 | // revents member of pollfd is initialized to 0 when the device is first added. |
| 472 | // Since mFDs[0] is used for inotify, we process regular events starting at index 1. |
| 473 | mInputDeviceIndex += 1; |
| 474 | if (mInputDeviceIndex >= mFDCount) { |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 478 | const struct pollfd& pfd = mFDs[mInputDeviceIndex]; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 479 | if (pfd.revents & POLLIN) { |
| 480 | int32_t readSize = read(pfd.fd, mInputBufferData, |
| 481 | sizeof(struct input_event) * INPUT_BUFFER_SIZE); |
| 482 | if (readSize < 0) { |
| 483 | if (errno != EAGAIN && errno != EINTR) { |
| 484 | LOGW("could not get event (errno=%d)", errno); |
| 485 | } |
| 486 | } else if ((readSize % sizeof(struct input_event)) != 0) { |
| 487 | LOGE("could not get event (wrong size: %d)", readSize); |
| 488 | } else { |
| 489 | mInputBufferCount = readSize / sizeof(struct input_event); |
| 490 | mInputBufferIndex = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 494 | |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 495 | #if HAVE_INOTIFY |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 496 | // 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] | 497 | // processing all other events. |
| 498 | if(mFDs[0].revents & POLLIN) { |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 499 | readNotify(mFDs[0].fd); |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 500 | mFDs[0].revents = 0; |
| 501 | continue; // report added or removed devices immediately |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | } |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 503 | #endif |
| 504 | |
| 505 | mInputDeviceIndex = 0; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 506 | |
| 507 | // Poll for events. Mind the wake lock dance! |
| 508 | // We hold a wake lock at all times except during poll(). This works due to some |
| 509 | // subtle choreography. When a device driver has pending (unread) events, it acquires |
| 510 | // a kernel wake lock. However, once the last pending event has been read, the device |
| 511 | // driver will release the kernel wake lock. To prevent the system from going to sleep |
| 512 | // when this happens, the EventHub holds onto its own user wake lock while the client |
| 513 | // is processing events. Thus the system can only sleep if there are no events |
| 514 | // pending or currently being processed. |
| 515 | release_wake_lock(WAKE_LOCK_ID); |
| 516 | |
| 517 | int pollResult = poll(mFDs, mFDCount, -1); |
| 518 | |
| 519 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 520 | |
| 521 | if (pollResult <= 0) { |
| 522 | if (errno != EINTR) { |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 523 | LOGW("poll failed (errno=%d)\n", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 524 | usleep(100000); |
| 525 | } |
| 526 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Open the platform-specific input device. |
| 532 | */ |
| 533 | bool EventHub::openPlatformInput(void) |
| 534 | { |
| 535 | /* |
| 536 | * Open platform-specific input device(s). |
| 537 | */ |
| 538 | int res; |
| 539 | |
| 540 | mFDCount = 1; |
| 541 | mFDs = (pollfd *)calloc(1, sizeof(mFDs[0])); |
| 542 | mDevices = (device_t **)calloc(1, sizeof(mDevices[0])); |
| 543 | mFDs[0].events = POLLIN; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 544 | mFDs[0].revents = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | mDevices[0] = NULL; |
| 546 | #ifdef HAVE_INOTIFY |
| 547 | mFDs[0].fd = inotify_init(); |
| 548 | res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE); |
| 549 | if(res < 0) { |
| 550 | LOGE("could not add watch for %s, %s\n", device_path, strerror(errno)); |
| 551 | } |
| 552 | #else |
| 553 | /* |
| 554 | * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd. |
| 555 | * We allocate space for it and set it to something invalid. |
| 556 | */ |
| 557 | mFDs[0].fd = -1; |
| 558 | #endif |
| 559 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 560 | res = scanDir(device_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | if(res < 0) { |
| 562 | LOGE("scan dir failed for %s\n", device_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return true; |
| 566 | } |
| 567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | // ---------------------------------------------------------------------------- |
| 569 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 570 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { |
| 571 | const uint8_t* end = array + endIndex; |
| 572 | array += startIndex; |
| 573 | while (array != end) { |
| 574 | if (*(array++) != 0) { |
| 575 | return true; |
| 576 | } |
| 577 | } |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | static const int32_t GAMEPAD_KEYCODES[] = { |
| 582 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, |
| 583 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, |
| 584 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, |
| 585 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, |
| 586 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, |
| 587 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE |
| 588 | }; |
| 589 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 590 | int EventHub::openDevice(const char *deviceName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | int version; |
| 592 | int fd; |
| 593 | struct pollfd *new_mFDs; |
| 594 | device_t **new_devices; |
| 595 | char **new_device_names; |
| 596 | char name[80]; |
| 597 | char location[80]; |
| 598 | char idstr[80]; |
| 599 | struct input_id id; |
| 600 | |
| 601 | LOGV("Opening device: %s", deviceName); |
| 602 | |
| 603 | AutoMutex _l(mLock); |
Nick Pelly | e6b1bbd | 2010-01-20 19:36:49 -0800 | [diff] [blame] | 604 | |
Nick Pelly | c8b60d1 | 2010-01-26 10:27:15 -0800 | [diff] [blame] | 605 | fd = open(deviceName, O_RDWR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | if(fd < 0) { |
| 607 | LOGE("could not open %s, %s\n", deviceName, strerror(errno)); |
| 608 | return -1; |
| 609 | } |
| 610 | |
| 611 | if(ioctl(fd, EVIOCGVERSION, &version)) { |
| 612 | LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno)); |
| 613 | return -1; |
| 614 | } |
| 615 | if(ioctl(fd, EVIOCGID, &id)) { |
| 616 | LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno)); |
| 617 | return -1; |
| 618 | } |
| 619 | name[sizeof(name) - 1] = '\0'; |
| 620 | location[sizeof(location) - 1] = '\0'; |
| 621 | idstr[sizeof(idstr) - 1] = '\0'; |
| 622 | if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) { |
| 623 | //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno)); |
| 624 | name[0] = '\0'; |
| 625 | } |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 626 | |
| 627 | // check to see if the device is on our excluded list |
| 628 | List<String8>::iterator iter = mExcludedDevices.begin(); |
| 629 | List<String8>::iterator end = mExcludedDevices.end(); |
| 630 | for ( ; iter != end; iter++) { |
| 631 | const char* test = *iter; |
| 632 | if (strcmp(name, test) == 0) { |
| 633 | LOGI("ignoring event id %s driver %s\n", deviceName, test); |
| 634 | close(fd); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | } |
| 638 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) { |
| 640 | //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno)); |
| 641 | location[0] = '\0'; |
| 642 | } |
| 643 | if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) { |
| 644 | //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno)); |
| 645 | idstr[0] = '\0'; |
| 646 | } |
| 647 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 648 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
| 649 | LOGE("Error %d making device file descriptor non-blocking.", errno); |
| 650 | close(fd); |
| 651 | return -1; |
| 652 | } |
| 653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | int devid = 0; |
| 655 | while (devid < mNumDevicesById) { |
| 656 | if (mDevicesById[devid].device == NULL) { |
| 657 | break; |
| 658 | } |
| 659 | devid++; |
| 660 | } |
| 661 | if (devid >= mNumDevicesById) { |
| 662 | device_ent* new_devids = (device_ent*)realloc(mDevicesById, |
| 663 | sizeof(mDevicesById[0]) * (devid + 1)); |
| 664 | if (new_devids == NULL) { |
| 665 | LOGE("out of memory"); |
| 666 | return -1; |
| 667 | } |
| 668 | mDevicesById = new_devids; |
| 669 | mNumDevicesById = devid+1; |
| 670 | mDevicesById[devid].device = NULL; |
| 671 | mDevicesById[devid].seq = 0; |
| 672 | } |
| 673 | |
| 674 | mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK; |
| 675 | if (mDevicesById[devid].seq == 0) { |
| 676 | mDevicesById[devid].seq = 1<<SEQ_SHIFT; |
| 677 | } |
| 678 | |
| 679 | new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1)); |
| 680 | new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1)); |
| 681 | if (new_mFDs == NULL || new_devices == NULL) { |
| 682 | LOGE("out of memory"); |
| 683 | return -1; |
| 684 | } |
| 685 | mFDs = new_mFDs; |
| 686 | mDevices = new_devices; |
| 687 | |
| 688 | #if 0 |
| 689 | LOGI("add device %d: %s\n", mFDCount, deviceName); |
| 690 | LOGI(" bus: %04x\n" |
| 691 | " vendor %04x\n" |
| 692 | " product %04x\n" |
| 693 | " version %04x\n", |
| 694 | id.bustype, id.vendor, id.product, id.version); |
| 695 | LOGI(" name: \"%s\"\n", name); |
| 696 | LOGI(" location: \"%s\"\n" |
| 697 | " id: \"%s\"\n", location, idstr); |
| 698 | LOGI(" version: %d.%d.%d\n", |
| 699 | version >> 16, (version >> 8) & 0xff, version & 0xff); |
| 700 | #endif |
| 701 | |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 702 | device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | if (device == NULL) { |
| 704 | LOGE("out of memory"); |
| 705 | return -1; |
| 706 | } |
| 707 | |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 708 | device->fd = fd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | mFDs[mFDCount].fd = fd; |
| 710 | mFDs[mFDCount].events = POLLIN; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 711 | mFDs[mFDCount].revents = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 713 | // Figure out the kinds of events the device reports. |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 714 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 715 | 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] | 716 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | LOGV("Getting keys..."); |
| 719 | if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) { |
| 720 | //LOGI("MAP\n"); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 721 | //for (int i = 0; i < sizeof(key_bitmask); i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | // LOGI("%d: 0x%02x\n", i, key_bitmask[i]); |
| 723 | //} |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 724 | |
| 725 | // See if this is a keyboard. Ignore everything in the button range except for |
| 726 | // gamepads which are also considered keyboards. |
| 727 | if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC)) |
| 728 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD), |
| 729 | sizeof_bit_array(BTN_DIGI)) |
| 730 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK), |
| 731 | sizeof_bit_array(KEY_MAX + 1))) { |
| 732 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
| 733 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 734 | device->keyBitmask = new uint8_t[sizeof(key_bitmask)]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | if (device->keyBitmask != NULL) { |
| 736 | memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask)); |
| 737 | } else { |
| 738 | delete device; |
| 739 | LOGE("out of memory allocating key bitmask"); |
| 740 | return -1; |
| 741 | } |
| 742 | } |
| 743 | } |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 744 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 745 | // See if this is a trackball (or mouse). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | if (test_bit(BTN_MOUSE, key_bitmask)) { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 747 | 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] | 748 | memset(rel_bitmask, 0, sizeof(rel_bitmask)); |
| 749 | LOGV("Getting relative controllers..."); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 750 | 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] | 751 | if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 752 | device->classes |= INPUT_DEVICE_CLASS_TRACKBALL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 756 | |
| 757 | // See if this is a touch pad. |
| 758 | uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)]; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 759 | memset(abs_bitmask, 0, sizeof(abs_bitmask)); |
| 760 | LOGV("Getting absolute controllers..."); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 761 | if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) { |
| 762 | // Is this a new modern multi-touch driver? |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 763 | if (test_bit(ABS_MT_POSITION_X, abs_bitmask) |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 764 | && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) { |
| 765 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT; |
| 766 | |
| 767 | // Is this an old style single-touch driver? |
| 768 | } else if (test_bit(BTN_TOUCH, key_bitmask) |
| 769 | && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) { |
| 770 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN; |
| 771 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | #ifdef EV_SW |
| 775 | // figure out the switches this device reports |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 776 | 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] | 777 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 778 | bool hasSwitches = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
| 780 | for (int i=0; i<EV_SW; i++) { |
| 781 | //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask)); |
| 782 | if (test_bit(i, sw_bitmask)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 783 | hasSwitches = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | if (mSwitches[i] == 0) { |
| 785 | mSwitches[i] = device->id; |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 790 | if (hasSwitches) { |
| 791 | device->classes |= INPUT_DEVICE_CLASS_SWITCH; |
| 792 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | #endif |
| 794 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 795 | if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | // a more descriptive name |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 797 | device->name = name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 799 | // Configure the keymap for the device. |
| 800 | configureKeyMap(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 802 | // Tell the world about the devname (the descriptive name) |
| 803 | if (!mHaveFirstKeyboard && !device->defaultKeyMap && strstr(name, "-keypad")) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | // the built-in keyboard has a well-known device ID of 0, |
| 805 | // this device better not go away. |
| 806 | mHaveFirstKeyboard = true; |
| 807 | mFirstKeyboardId = device->id; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 808 | setKeyboardProperties(device, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | // ensure mFirstKeyboardId is set to -something-. |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 811 | if (mFirstKeyboardId == -1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | mFirstKeyboardId = device->id; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 813 | setKeyboardProperties(device, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | } |
| 815 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 816 | setKeyboardProperties(device, false); |
| 817 | |
| 818 | // Load the keylayout. |
| 819 | if (!device->keyLayoutFilename.isEmpty()) { |
| 820 | status_t status = device->layoutMap->load(device->keyLayoutFilename); |
| 821 | if (status) { |
| 822 | LOGE("Error %d loading key layout file '%s'.", status, |
| 823 | device->keyLayoutFilename.string()); |
| 824 | } |
| 825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 827 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 828 | if (hasKeycodeLocked(device, AKEYCODE_Q)) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 829 | device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 830 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 831 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 832 | // See if this device has a DPAD. |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 833 | if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) && |
| 834 | hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) && |
| 835 | hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) && |
| 836 | hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) && |
| 837 | hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 838 | device->classes |= INPUT_DEVICE_CLASS_DPAD; |
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 | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 841 | // See if this device has a gamepad. |
| 842 | for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES); i++) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 843 | if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 844 | device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; |
| 845 | break; |
| 846 | } |
| 847 | } |
| 848 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 849 | LOGI("New keyboard: device->id=0x%x devname='%s' keylayout='%s' keycharactermap='%s'\n", |
| 850 | device->id, name, |
| 851 | device->keyLayoutFilename.string(), device->keyCharacterMapFilename.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 854 | // If the device isn't recognized as something we handle, don't monitor it. |
| 855 | if (device->classes == 0) { |
| 856 | LOGV("Dropping device %s %p, id = %d\n", deviceName, device, devid); |
| 857 | close(fd); |
| 858 | delete device; |
| 859 | return -1; |
| 860 | } |
| 861 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 862 | LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n", |
| 863 | deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes); |
| 864 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n", |
| 866 | deviceName, device, mFDCount, devid, device->classes); |
| 867 | |
| 868 | mDevicesById[devid].device = device; |
| 869 | device->next = mOpeningDevices; |
| 870 | mOpeningDevices = device; |
| 871 | mDevices[mFDCount] = device; |
| 872 | |
| 873 | mFDCount++; |
| 874 | return 0; |
| 875 | } |
| 876 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 877 | void EventHub::configureKeyMap(device_t* device) { |
| 878 | // As an initial key map name, try using the device name. |
| 879 | String8 keyMapName(device->name); |
| 880 | char* p = keyMapName.lockBuffer(keyMapName.size()); |
| 881 | while (*p) { |
| 882 | if (*p == ' ') *p = '_'; |
| 883 | p++; |
| 884 | } |
| 885 | keyMapName.unlockBuffer(); |
| 886 | |
| 887 | if (probeKeyMap(device, keyMapName, false)) return; |
| 888 | |
| 889 | // TODO Consider allowing the user to configure a specific key map somehow. |
| 890 | |
| 891 | // Try the Generic key map. |
| 892 | // TODO Apply some additional heuristics here to figure out what kind of |
| 893 | // generic key map to use (US English, etc.). |
| 894 | keyMapName.setTo("Generic"); |
| 895 | if (probeKeyMap(device, keyMapName, true)) return; |
| 896 | |
| 897 | // Fall back on the old style catchall qwerty key map. |
| 898 | keyMapName.setTo("qwerty"); |
| 899 | if (probeKeyMap(device, keyMapName, true)) return; |
| 900 | |
| 901 | // Give up! |
| 902 | keyMapName.setTo("unknown"); |
| 903 | selectKeyMap(device, keyMapName, true); |
| 904 | LOGE("Could not determine key map for device '%s'.", device->name.string()); |
| 905 | } |
| 906 | |
| 907 | bool EventHub::probeKeyMap(device_t* device, const String8& keyMapName, bool defaultKeyMap) { |
| 908 | const char* root = getenv("ANDROID_ROOT"); |
| 909 | |
| 910 | // TODO Consider also looking somewhere in a writeable partition like /data for a |
| 911 | // custom keymap supplied by the user for this device. |
| 912 | bool haveKeyLayout = !device->keyLayoutFilename.isEmpty(); |
| 913 | if (!haveKeyLayout) { |
| 914 | device->keyLayoutFilename.setTo(root); |
| 915 | device->keyLayoutFilename.append("/usr/keylayout/"); |
| 916 | device->keyLayoutFilename.append(keyMapName); |
| 917 | device->keyLayoutFilename.append(".kl"); |
| 918 | if (access(device->keyLayoutFilename.string(), R_OK)) { |
| 919 | device->keyLayoutFilename.clear(); |
| 920 | } else { |
| 921 | haveKeyLayout = true; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | bool haveKeyCharacterMap = !device->keyCharacterMapFilename.isEmpty(); |
| 926 | if (!haveKeyCharacterMap) { |
| 927 | device->keyCharacterMapFilename.setTo(root); |
| 928 | device->keyCharacterMapFilename.append("/usr/keychars/"); |
| 929 | device->keyCharacterMapFilename.append(keyMapName); |
| 930 | device->keyCharacterMapFilename.append(".kcm.bin"); |
| 931 | if (access(device->keyCharacterMapFilename.string(), R_OK)) { |
| 932 | device->keyCharacterMapFilename.clear(); |
| 933 | } else { |
| 934 | haveKeyCharacterMap = true; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | if (haveKeyLayout || haveKeyCharacterMap) { |
| 939 | selectKeyMap(device, keyMapName, defaultKeyMap); |
| 940 | } |
| 941 | return haveKeyLayout && haveKeyCharacterMap; |
| 942 | } |
| 943 | |
| 944 | void EventHub::selectKeyMap(device_t* device, |
| 945 | const String8& keyMapName, bool defaultKeyMap) { |
| 946 | if (device->keyMapName.isEmpty()) { |
| 947 | device->keyMapName.setTo(keyMapName); |
| 948 | device->defaultKeyMap = defaultKeyMap; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | void EventHub::setKeyboardProperties(device_t* device, bool firstKeyboard) { |
| 953 | int32_t id = firstKeyboard ? 0 : device->id; |
| 954 | |
| 955 | char propName[100]; |
| 956 | sprintf(propName, "hw.keyboards.%u.devname", id); |
| 957 | property_set(propName, device->name.string()); |
| 958 | sprintf(propName, "hw.keyboards.%u.keymap", id); |
| 959 | property_set(propName, device->keyMapName.string()); |
| 960 | sprintf(propName, "hw.keyboards.%u.klfile", id); |
| 961 | property_set(propName, device->keyLayoutFilename.string()); |
| 962 | sprintf(propName, "hw.keyboards.%u.kcmfile", id); |
| 963 | property_set(propName, device->keyCharacterMapFilename.string()); |
| 964 | } |
| 965 | |
| 966 | void EventHub::clearKeyboardProperties(device_t* device, bool firstKeyboard) { |
| 967 | int32_t id = firstKeyboard ? 0 : device->id; |
| 968 | |
| 969 | char propName[100]; |
| 970 | sprintf(propName, "hw.keyboards.%u.devname", id); |
| 971 | property_set(propName, ""); |
| 972 | sprintf(propName, "hw.keyboards.%u.keymap", id); |
| 973 | property_set(propName, ""); |
| 974 | sprintf(propName, "hw.keyboards.%u.klfile", id); |
| 975 | property_set(propName, ""); |
| 976 | sprintf(propName, "hw.keyboards.%u.kcmfile", id); |
| 977 | property_set(propName, ""); |
| 978 | } |
| 979 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 980 | bool EventHub::hasKeycodeLocked(device_t* device, int keycode) const |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 981 | { |
| 982 | if (device->keyBitmask == NULL || device->layoutMap == NULL) { |
| 983 | return false; |
| 984 | } |
| 985 | |
| 986 | Vector<int32_t> scanCodes; |
| 987 | device->layoutMap->findScancodes(keycode, &scanCodes); |
| 988 | const size_t N = scanCodes.size(); |
| 989 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 990 | int32_t sc = scanCodes.itemAt(i); |
| 991 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { |
| 992 | return true; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | return false; |
| 997 | } |
| 998 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 999 | int EventHub::closeDevice(const char *deviceName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | AutoMutex _l(mLock); |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1001 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | int i; |
| 1003 | for(i = 1; i < mFDCount; i++) { |
| 1004 | if(strcmp(mDevices[i]->path.string(), deviceName) == 0) { |
| 1005 | //LOGD("remove device %d: %s\n", i, deviceName); |
| 1006 | device_t* device = mDevices[i]; |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 1007 | |
| 1008 | LOGI("Removed device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n", |
| 1009 | device->path.string(), device->name.string(), device->id, |
| 1010 | mNumDevicesById, mFDCount, mFDs[i].fd, device->classes); |
| 1011 | |
| 1012 | // Clear this device's entry. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | int index = (device->id&ID_MASK); |
| 1014 | mDevicesById[index].device = NULL; |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 1015 | |
| 1016 | // Close the file descriptor and compact the fd array. |
Mike Lockwood | 36dad72 | 2009-08-28 13:29:06 -0700 | [diff] [blame] | 1017 | close(mFDs[i].fd); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 1018 | int count = mFDCount - i - 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count); |
| 1020 | memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 1021 | mFDCount--; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | |
| 1023 | #ifdef EV_SW |
| 1024 | for (int j=0; j<EV_SW; j++) { |
| 1025 | if (mSwitches[j] == device->id) { |
| 1026 | mSwitches[j] = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | #endif |
| 1030 | |
| 1031 | device->next = mClosingDevices; |
| 1032 | mClosingDevices = device; |
| 1033 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | if (device->id == mFirstKeyboardId) { |
| 1035 | LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", |
| 1036 | device->path.string(), mFirstKeyboardId); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1037 | mFirstKeyboardId = -1; |
| 1038 | clearKeyboardProperties(device, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1040 | clearKeyboardProperties(device, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | return 0; |
| 1042 | } |
| 1043 | } |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 1044 | LOGE("remove device: %s not found\n", deviceName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | return -1; |
| 1046 | } |
| 1047 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1048 | int EventHub::readNotify(int nfd) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | #ifdef HAVE_INOTIFY |
| 1050 | int res; |
| 1051 | char devname[PATH_MAX]; |
| 1052 | char *filename; |
| 1053 | char event_buf[512]; |
| 1054 | int event_size; |
| 1055 | int event_pos = 0; |
| 1056 | struct inotify_event *event; |
| 1057 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1058 | LOGV("EventHub::readNotify nfd: %d\n", nfd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | res = read(nfd, event_buf, sizeof(event_buf)); |
| 1060 | if(res < (int)sizeof(*event)) { |
| 1061 | if(errno == EINTR) |
| 1062 | return 0; |
| 1063 | LOGW("could not get event, %s\n", strerror(errno)); |
| 1064 | return 1; |
| 1065 | } |
| 1066 | //printf("got %d bytes of event information\n", res); |
| 1067 | |
| 1068 | strcpy(devname, device_path); |
| 1069 | filename = devname + strlen(devname); |
| 1070 | *filename++ = '/'; |
| 1071 | |
| 1072 | while(res >= (int)sizeof(*event)) { |
| 1073 | event = (struct inotify_event *)(event_buf + event_pos); |
| 1074 | //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); |
| 1075 | if(event->len) { |
| 1076 | strcpy(filename, event->name); |
| 1077 | if(event->mask & IN_CREATE) { |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1078 | openDevice(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | } |
| 1080 | else { |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1081 | closeDevice(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | event_size = sizeof(*event) + event->len; |
| 1085 | res -= event_size; |
| 1086 | event_pos += event_size; |
| 1087 | } |
| 1088 | #endif |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1093 | int EventHub::scanDir(const char *dirname) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | { |
| 1095 | char devname[PATH_MAX]; |
| 1096 | char *filename; |
| 1097 | DIR *dir; |
| 1098 | struct dirent *de; |
| 1099 | dir = opendir(dirname); |
| 1100 | if(dir == NULL) |
| 1101 | return -1; |
| 1102 | strcpy(devname, dirname); |
| 1103 | filename = devname + strlen(devname); |
| 1104 | *filename++ = '/'; |
| 1105 | while((de = readdir(dir))) { |
| 1106 | if(de->d_name[0] == '.' && |
| 1107 | (de->d_name[1] == '\0' || |
| 1108 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 1109 | continue; |
| 1110 | strcpy(filename, de->d_name); |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1111 | openDevice(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | } |
| 1113 | closedir(dir); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1117 | void EventHub::dump(String8& dump) { |
| 1118 | dump.append("Event Hub State:\n"); |
| 1119 | |
| 1120 | { // acquire lock |
| 1121 | AutoMutex _l(mLock); |
| 1122 | |
| 1123 | dump.appendFormat(INDENT "HaveFirstKeyboard: %s\n", toString(mHaveFirstKeyboard)); |
| 1124 | dump.appendFormat(INDENT "FirstKeyboardId: 0x%x\n", mFirstKeyboardId); |
| 1125 | |
| 1126 | dump.append(INDENT "Devices:\n"); |
| 1127 | |
| 1128 | for (int i = 0; i < mNumDevicesById; i++) { |
| 1129 | const device_t* device = mDevicesById[i].device; |
| 1130 | if (device) { |
| 1131 | if (mFirstKeyboardId == device->id) { |
| 1132 | dump.appendFormat(INDENT2 "0x%x: %s (aka device 0 - first keyboard)\n", |
| 1133 | device->id, device->name.string()); |
| 1134 | } else { |
| 1135 | dump.appendFormat(INDENT2 "0x%x: %s\n", device->id, device->name.string()); |
| 1136 | } |
| 1137 | dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes); |
| 1138 | dump.appendFormat(INDENT3 "Path: %s\n", device->path.string()); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1139 | dump.appendFormat(INDENT3 "KeyMapName: %s\n", device->keyMapName.string()); |
| 1140 | dump.appendFormat(INDENT3 "KeyLayoutFilename: %s\n", |
| 1141 | device->keyLayoutFilename.string()); |
| 1142 | dump.appendFormat(INDENT3 "KeyCharacterMapFilename: %s\n", |
| 1143 | device->keyCharacterMapFilename.string()); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | } // release lock |
| 1147 | } |
| 1148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | }; // namespace android |