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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | namespace android { |
| 77 | |
| 78 | static const char *WAKE_LOCK_ID = "KeyEvents"; |
| 79 | static const char *device_path = "/dev/input"; |
| 80 | |
| 81 | /* return the larger integer */ |
| 82 | static inline int max(int v1, int v2) |
| 83 | { |
| 84 | return (v1 > v2) ? v1 : v2; |
| 85 | } |
| 86 | |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 87 | EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name) |
| 88 | : id(_id), path(_path), name(name), classes(0) |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 89 | , keyBitmask(NULL), layoutMap(new KeyLayoutMap()), fd(-1), next(NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | EventHub::device_t::~device_t() { |
| 93 | delete [] keyBitmask; |
| 94 | delete layoutMap; |
| 95 | } |
| 96 | |
| 97 | EventHub::EventHub(void) |
| 98 | : mError(NO_INIT), mHaveFirstKeyboard(false), mFirstKeyboardId(0) |
| 99 | , mDevicesById(0), mNumDevicesById(0) |
| 100 | , mOpeningDevices(0), mClosingDevices(0) |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 101 | , mDevices(0), mFDs(0), mFDCount(0), mOpened(false) |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 102 | , mInputBufferIndex(0), mInputBufferCount(0), mInputDeviceIndex(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | { |
| 104 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 105 | #ifdef EV_SW |
| 106 | memset(mSwitches, 0, sizeof(mSwitches)); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Clean up. |
| 112 | */ |
| 113 | EventHub::~EventHub(void) |
| 114 | { |
| 115 | release_wake_lock(WAKE_LOCK_ID); |
| 116 | // we should free stuff here... |
| 117 | } |
| 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | status_t EventHub::errorCheck() const |
| 120 | { |
| 121 | return mError; |
| 122 | } |
| 123 | |
| 124 | String8 EventHub::getDeviceName(int32_t deviceId) const |
| 125 | { |
| 126 | AutoMutex _l(mLock); |
| 127 | device_t* device = getDevice(deviceId); |
| 128 | if (device == NULL) return String8(); |
| 129 | return device->name; |
| 130 | } |
| 131 | |
| 132 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const |
| 133 | { |
| 134 | AutoMutex _l(mLock); |
| 135 | device_t* device = getDevice(deviceId); |
| 136 | if (device == NULL) return 0; |
| 137 | return device->classes; |
| 138 | } |
| 139 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 140 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 141 | RawAbsoluteAxisInfo* outAxisInfo) const { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 142 | outAxisInfo->clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 143 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | AutoMutex _l(mLock); |
| 145 | device_t* device = getDevice(deviceId); |
| 146 | if (device == NULL) return -1; |
| 147 | |
| 148 | struct input_absinfo info; |
| 149 | |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 150 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 151 | LOGW("Error reading absolute controller %d for device %s fd %d\n", |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 152 | axis, device->name.string(), device->fd); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 153 | return -errno; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 155 | |
| 156 | if (info.minimum != info.maximum) { |
| 157 | outAxisInfo->valid = true; |
| 158 | outAxisInfo->minValue = info.minimum; |
| 159 | outAxisInfo->maxValue = info.maximum; |
| 160 | outAxisInfo->flat = info.flat; |
| 161 | outAxisInfo->fuzz = info.fuzz; |
| 162 | } |
| 163 | return OK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 166 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 167 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
| 168 | AutoMutex _l(mLock); |
| 169 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 170 | device_t* device = getDevice(deviceId); |
| 171 | if (device != NULL) { |
| 172 | return getScanCodeStateLocked(device, scanCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 175 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 178 | int32_t EventHub::getScanCodeStateLocked(device_t* device, int32_t scanCode) const { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 179 | uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)]; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 180 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 181 | if (ioctl(device->fd, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 183 | return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 184 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 185 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 188 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
| 189 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 190 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 191 | device_t* device = getDevice(deviceId); |
| 192 | if (device != NULL) { |
| 193 | return getKeyCodeStateLocked(device, keyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 195 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 198 | 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] | 199 | Vector<int32_t> scanCodes; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 200 | device->layoutMap->findScancodes(keyCode, &scanCodes); |
| 201 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 202 | 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] | 203 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 204 | 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] | 205 | #if 0 |
| 206 | for (size_t i=0; i<=KEY_MAX; i++) { |
| 207 | LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask)); |
| 208 | } |
| 209 | #endif |
| 210 | const size_t N = scanCodes.size(); |
| 211 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 212 | int32_t sc = scanCodes.itemAt(i); |
| 213 | //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask)); |
| 214 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 215 | return AKEY_STATE_DOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 218 | return AKEY_STATE_UP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 220 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 223 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 224 | #ifdef EV_SW |
| 225 | if (sw >= 0 && sw <= SW_MAX) { |
| 226 | AutoMutex _l(mLock); |
| 227 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 228 | device_t* device = getDevice(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 229 | if (device != NULL) { |
| 230 | return getSwitchStateLocked(device, sw); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 231 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 232 | } |
| 233 | #endif |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 234 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | int32_t EventHub::getSwitchStateLocked(device_t* device, int32_t sw) const { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 238 | uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)]; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 239 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 240 | if (ioctl(device->fd, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 241 | EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 242 | return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 243 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 244 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 247 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 248 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 249 | AutoMutex _l(mLock); |
| 250 | |
| 251 | device_t* device = getDevice(deviceId); |
| 252 | if (device != NULL) { |
| 253 | return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags); |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | bool EventHub::markSupportedKeyCodesLocked(device_t* device, size_t numCodes, |
| 259 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 260 | if (device->layoutMap == NULL || device->keyBitmask == NULL) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | Vector<int32_t> scanCodes; |
| 265 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
| 266 | scanCodes.clear(); |
| 267 | |
| 268 | status_t err = device->layoutMap->findScancodes(keyCodes[codeIndex], &scanCodes); |
| 269 | if (! err) { |
| 270 | // check the possible scan codes identified by the layout map against the |
| 271 | // map of codes actually emitted by the driver |
| 272 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
| 273 | if (test_bit(scanCodes[sc], device->keyBitmask)) { |
| 274 | outFlags[codeIndex] = 1; |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | return true; |
| 281 | } |
| 282 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 283 | status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode, |
| 284 | int32_t* outKeycode, uint32_t* outFlags) const |
| 285 | { |
| 286 | AutoMutex _l(mLock); |
| 287 | device_t* device = getDevice(deviceId); |
| 288 | |
| 289 | if (device != NULL && device->layoutMap != NULL) { |
| 290 | status_t err = device->layoutMap->map(scancode, outKeycode, outFlags); |
| 291 | if (err == NO_ERROR) { |
| 292 | return NO_ERROR; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (mHaveFirstKeyboard) { |
| 297 | device = getDevice(mFirstKeyboardId); |
| 298 | |
| 299 | if (device != NULL && device->layoutMap != NULL) { |
| 300 | status_t err = device->layoutMap->map(scancode, outKeycode, outFlags); |
| 301 | if (err == NO_ERROR) { |
| 302 | return NO_ERROR; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | *outKeycode = 0; |
| 308 | *outFlags = 0; |
| 309 | return NAME_NOT_FOUND; |
| 310 | } |
| 311 | |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 312 | void EventHub::addExcludedDevice(const char* deviceName) |
| 313 | { |
| 314 | String8 name(deviceName); |
| 315 | mExcludedDevices.push_back(name); |
| 316 | } |
| 317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | EventHub::device_t* EventHub::getDevice(int32_t deviceId) const |
| 319 | { |
| 320 | if (deviceId == 0) deviceId = mFirstKeyboardId; |
| 321 | int32_t id = deviceId & ID_MASK; |
| 322 | if (id >= mNumDevicesById || id < 0) return NULL; |
| 323 | device_t* dev = mDevicesById[id].device; |
Dianne Hackborn | 4e829f0 | 2009-03-25 16:21:55 -0700 | [diff] [blame] | 324 | if (dev == NULL) return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | if (dev->id == deviceId) { |
| 326 | return dev; |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 331 | bool EventHub::getEvent(RawEvent* outEvent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 333 | outEvent->deviceId = 0; |
| 334 | outEvent->type = 0; |
| 335 | outEvent->scanCode = 0; |
| 336 | outEvent->keyCode = 0; |
| 337 | outEvent->flags = 0; |
| 338 | outEvent->value = 0; |
| 339 | outEvent->when = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | // Note that we only allow one caller to getEvent(), so don't need |
| 342 | // to do locking here... only when adding/removing devices. |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 343 | |
| 344 | if (!mOpened) { |
| 345 | mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR; |
| 346 | mOpened = true; |
| 347 | } |
| 348 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 349 | for (;;) { |
| 350 | // Report any devices that had last been added/removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | if (mClosingDevices != NULL) { |
| 352 | device_t* device = mClosingDevices; |
| 353 | LOGV("Reporting device closed: id=0x%x, name=%s\n", |
| 354 | device->id, device->path.string()); |
| 355 | mClosingDevices = device->next; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 356 | if (device->id == mFirstKeyboardId) { |
| 357 | outEvent->deviceId = 0; |
| 358 | } else { |
| 359 | outEvent->deviceId = device->id; |
| 360 | } |
| 361 | outEvent->type = DEVICE_REMOVED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | delete device; |
| 363 | return true; |
| 364 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 365 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | if (mOpeningDevices != NULL) { |
| 367 | device_t* device = mOpeningDevices; |
| 368 | LOGV("Reporting device opened: id=0x%x, name=%s\n", |
| 369 | device->id, device->path.string()); |
| 370 | mOpeningDevices = device->next; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 371 | if (device->id == mFirstKeyboardId) { |
| 372 | outEvent->deviceId = 0; |
| 373 | } else { |
| 374 | outEvent->deviceId = device->id; |
| 375 | } |
| 376 | outEvent->type = DEVICE_ADDED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 380 | // Grab the next input event. |
| 381 | for (;;) { |
| 382 | // Consume buffered input events, if any. |
| 383 | if (mInputBufferIndex < mInputBufferCount) { |
| 384 | const struct input_event& iev = mInputBufferData[mInputBufferIndex++]; |
| 385 | const device_t* device = mDevices[mInputDeviceIndex]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 387 | LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(), |
| 388 | (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value); |
| 389 | if (device->id == mFirstKeyboardId) { |
| 390 | outEvent->deviceId = 0; |
| 391 | } else { |
| 392 | outEvent->deviceId = device->id; |
| 393 | } |
| 394 | outEvent->type = iev.type; |
| 395 | outEvent->scanCode = iev.code; |
| 396 | if (iev.type == EV_KEY) { |
| 397 | status_t err = device->layoutMap->map(iev.code, |
| 398 | & outEvent->keyCode, & outEvent->flags); |
| 399 | LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n", |
| 400 | iev.code, outEvent->keyCode, outEvent->flags, err); |
| 401 | if (err != 0) { |
| 402 | outEvent->keyCode = AKEYCODE_UNKNOWN; |
| 403 | outEvent->flags = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 405 | } else { |
| 406 | outEvent->keyCode = iev.code; |
| 407 | } |
| 408 | outEvent->value = iev.value; |
| 409 | |
| 410 | // Use an event timestamp in the same timebase as |
| 411 | // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis() |
| 412 | // as expected by the rest of the system. |
| 413 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | // Finish reading all events from devices identified in previous poll(). |
| 418 | // This code assumes that mInputDeviceIndex is initially 0 and that the |
| 419 | // revents member of pollfd is initialized to 0 when the device is first added. |
| 420 | // Since mFDs[0] is used for inotify, we process regular events starting at index 1. |
| 421 | mInputDeviceIndex += 1; |
| 422 | if (mInputDeviceIndex >= mFDCount) { |
| 423 | mInputDeviceIndex = 0; |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | const struct pollfd &pfd = mFDs[mInputDeviceIndex]; |
| 428 | if (pfd.revents & POLLIN) { |
| 429 | int32_t readSize = read(pfd.fd, mInputBufferData, |
| 430 | sizeof(struct input_event) * INPUT_BUFFER_SIZE); |
| 431 | if (readSize < 0) { |
| 432 | if (errno != EAGAIN && errno != EINTR) { |
| 433 | LOGW("could not get event (errno=%d)", errno); |
| 434 | } |
| 435 | } else if ((readSize % sizeof(struct input_event)) != 0) { |
| 436 | LOGE("could not get event (wrong size: %d)", readSize); |
| 437 | } else { |
| 438 | mInputBufferCount = readSize / sizeof(struct input_event); |
| 439 | mInputBufferIndex = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | // read_notify() will modify mFDs and mFDCount, so this must be done after |
| 445 | // processing all other events. |
| 446 | if(mFDs[0].revents & POLLIN) { |
| 447 | read_notify(mFDs[0].fd); |
| 448 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 449 | |
| 450 | // Poll for events. Mind the wake lock dance! |
| 451 | // We hold a wake lock at all times except during poll(). This works due to some |
| 452 | // subtle choreography. When a device driver has pending (unread) events, it acquires |
| 453 | // a kernel wake lock. However, once the last pending event has been read, the device |
| 454 | // driver will release the kernel wake lock. To prevent the system from going to sleep |
| 455 | // when this happens, the EventHub holds onto its own user wake lock while the client |
| 456 | // is processing events. Thus the system can only sleep if there are no events |
| 457 | // pending or currently being processed. |
| 458 | release_wake_lock(WAKE_LOCK_ID); |
| 459 | |
| 460 | int pollResult = poll(mFDs, mFDCount, -1); |
| 461 | |
| 462 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 463 | |
| 464 | if (pollResult <= 0) { |
| 465 | if (errno != EINTR) { |
| 466 | LOGW("select failed (errno=%d)\n", errno); |
| 467 | usleep(100000); |
| 468 | } |
| 469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Open the platform-specific input device. |
| 475 | */ |
| 476 | bool EventHub::openPlatformInput(void) |
| 477 | { |
| 478 | /* |
| 479 | * Open platform-specific input device(s). |
| 480 | */ |
| 481 | int res; |
| 482 | |
| 483 | mFDCount = 1; |
| 484 | mFDs = (pollfd *)calloc(1, sizeof(mFDs[0])); |
| 485 | mDevices = (device_t **)calloc(1, sizeof(mDevices[0])); |
| 486 | mFDs[0].events = POLLIN; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 487 | mFDs[0].revents = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | mDevices[0] = NULL; |
| 489 | #ifdef HAVE_INOTIFY |
| 490 | mFDs[0].fd = inotify_init(); |
| 491 | res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE); |
| 492 | if(res < 0) { |
| 493 | LOGE("could not add watch for %s, %s\n", device_path, strerror(errno)); |
| 494 | } |
| 495 | #else |
| 496 | /* |
| 497 | * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd. |
| 498 | * We allocate space for it and set it to something invalid. |
| 499 | */ |
| 500 | mFDs[0].fd = -1; |
| 501 | #endif |
| 502 | |
| 503 | res = scan_dir(device_path); |
| 504 | if(res < 0) { |
| 505 | LOGE("scan dir failed for %s\n", device_path); |
| 506 | //open_device("/dev/input/event0"); |
| 507 | } |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | // ---------------------------------------------------------------------------- |
| 513 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 514 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { |
| 515 | const uint8_t* end = array + endIndex; |
| 516 | array += startIndex; |
| 517 | while (array != end) { |
| 518 | if (*(array++) != 0) { |
| 519 | return true; |
| 520 | } |
| 521 | } |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | static const int32_t GAMEPAD_KEYCODES[] = { |
| 526 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, |
| 527 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, |
| 528 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, |
| 529 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, |
| 530 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, |
| 531 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE |
| 532 | }; |
| 533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | int EventHub::open_device(const char *deviceName) |
| 535 | { |
| 536 | int version; |
| 537 | int fd; |
| 538 | struct pollfd *new_mFDs; |
| 539 | device_t **new_devices; |
| 540 | char **new_device_names; |
| 541 | char name[80]; |
| 542 | char location[80]; |
| 543 | char idstr[80]; |
| 544 | struct input_id id; |
| 545 | |
| 546 | LOGV("Opening device: %s", deviceName); |
| 547 | |
| 548 | AutoMutex _l(mLock); |
Nick Pelly | e6b1bbd | 2010-01-20 19:36:49 -0800 | [diff] [blame] | 549 | |
Nick Pelly | c8b60d1 | 2010-01-26 10:27:15 -0800 | [diff] [blame] | 550 | fd = open(deviceName, O_RDWR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | if(fd < 0) { |
| 552 | LOGE("could not open %s, %s\n", deviceName, strerror(errno)); |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | if(ioctl(fd, EVIOCGVERSION, &version)) { |
| 557 | LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno)); |
| 558 | return -1; |
| 559 | } |
| 560 | if(ioctl(fd, EVIOCGID, &id)) { |
| 561 | LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno)); |
| 562 | return -1; |
| 563 | } |
| 564 | name[sizeof(name) - 1] = '\0'; |
| 565 | location[sizeof(location) - 1] = '\0'; |
| 566 | idstr[sizeof(idstr) - 1] = '\0'; |
| 567 | if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) { |
| 568 | //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno)); |
| 569 | name[0] = '\0'; |
| 570 | } |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 571 | |
| 572 | // check to see if the device is on our excluded list |
| 573 | List<String8>::iterator iter = mExcludedDevices.begin(); |
| 574 | List<String8>::iterator end = mExcludedDevices.end(); |
| 575 | for ( ; iter != end; iter++) { |
| 576 | const char* test = *iter; |
| 577 | if (strcmp(name, test) == 0) { |
| 578 | LOGI("ignoring event id %s driver %s\n", deviceName, test); |
| 579 | close(fd); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 580 | return -1; |
| 581 | } |
| 582 | } |
| 583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) { |
| 585 | //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno)); |
| 586 | location[0] = '\0'; |
| 587 | } |
| 588 | if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) { |
| 589 | //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno)); |
| 590 | idstr[0] = '\0'; |
| 591 | } |
| 592 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 593 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
| 594 | LOGE("Error %d making device file descriptor non-blocking.", errno); |
| 595 | close(fd); |
| 596 | return -1; |
| 597 | } |
| 598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | int devid = 0; |
| 600 | while (devid < mNumDevicesById) { |
| 601 | if (mDevicesById[devid].device == NULL) { |
| 602 | break; |
| 603 | } |
| 604 | devid++; |
| 605 | } |
| 606 | if (devid >= mNumDevicesById) { |
| 607 | device_ent* new_devids = (device_ent*)realloc(mDevicesById, |
| 608 | sizeof(mDevicesById[0]) * (devid + 1)); |
| 609 | if (new_devids == NULL) { |
| 610 | LOGE("out of memory"); |
| 611 | return -1; |
| 612 | } |
| 613 | mDevicesById = new_devids; |
| 614 | mNumDevicesById = devid+1; |
| 615 | mDevicesById[devid].device = NULL; |
| 616 | mDevicesById[devid].seq = 0; |
| 617 | } |
| 618 | |
| 619 | mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK; |
| 620 | if (mDevicesById[devid].seq == 0) { |
| 621 | mDevicesById[devid].seq = 1<<SEQ_SHIFT; |
| 622 | } |
| 623 | |
| 624 | new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1)); |
| 625 | new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1)); |
| 626 | if (new_mFDs == NULL || new_devices == NULL) { |
| 627 | LOGE("out of memory"); |
| 628 | return -1; |
| 629 | } |
| 630 | mFDs = new_mFDs; |
| 631 | mDevices = new_devices; |
| 632 | |
| 633 | #if 0 |
| 634 | LOGI("add device %d: %s\n", mFDCount, deviceName); |
| 635 | LOGI(" bus: %04x\n" |
| 636 | " vendor %04x\n" |
| 637 | " product %04x\n" |
| 638 | " version %04x\n", |
| 639 | id.bustype, id.vendor, id.product, id.version); |
| 640 | LOGI(" name: \"%s\"\n", name); |
| 641 | LOGI(" location: \"%s\"\n" |
| 642 | " id: \"%s\"\n", location, idstr); |
| 643 | LOGI(" version: %d.%d.%d\n", |
| 644 | version >> 16, (version >> 8) & 0xff, version & 0xff); |
| 645 | #endif |
| 646 | |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 647 | 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] | 648 | if (device == NULL) { |
| 649 | LOGE("out of memory"); |
| 650 | return -1; |
| 651 | } |
| 652 | |
Jens Gulin | c4554b9 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 653 | device->fd = fd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | mFDs[mFDCount].fd = fd; |
| 655 | mFDs[mFDCount].events = POLLIN; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 656 | mFDs[mFDCount].revents = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 658 | // Figure out the kinds of events the device reports. |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 659 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 660 | 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] | 661 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 662 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | LOGV("Getting keys..."); |
| 664 | if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) { |
| 665 | //LOGI("MAP\n"); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 666 | //for (int i = 0; i < sizeof(key_bitmask); i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | // LOGI("%d: 0x%02x\n", i, key_bitmask[i]); |
| 668 | //} |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 669 | |
| 670 | // See if this is a keyboard. Ignore everything in the button range except for |
| 671 | // gamepads which are also considered keyboards. |
| 672 | if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC)) |
| 673 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD), |
| 674 | sizeof_bit_array(BTN_DIGI)) |
| 675 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK), |
| 676 | sizeof_bit_array(KEY_MAX + 1))) { |
| 677 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
| 678 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 679 | device->keyBitmask = new uint8_t[sizeof(key_bitmask)]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | if (device->keyBitmask != NULL) { |
| 681 | memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask)); |
| 682 | } else { |
| 683 | delete device; |
| 684 | LOGE("out of memory allocating key bitmask"); |
| 685 | return -1; |
| 686 | } |
| 687 | } |
| 688 | } |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 689 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 690 | // See if this is a trackball (or mouse). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | if (test_bit(BTN_MOUSE, key_bitmask)) { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 692 | 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] | 693 | memset(rel_bitmask, 0, sizeof(rel_bitmask)); |
| 694 | LOGV("Getting relative controllers..."); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 695 | 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] | 696 | 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] | 697 | device->classes |= INPUT_DEVICE_CLASS_TRACKBALL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 701 | |
| 702 | // See if this is a touch pad. |
| 703 | uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)]; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 704 | memset(abs_bitmask, 0, sizeof(abs_bitmask)); |
| 705 | LOGV("Getting absolute controllers..."); |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 706 | if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) { |
| 707 | // Is this a new modern multi-touch driver? |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 708 | if (test_bit(ABS_MT_POSITION_X, abs_bitmask) |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 709 | && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) { |
| 710 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT; |
| 711 | |
| 712 | // Is this an old style single-touch driver? |
| 713 | } else if (test_bit(BTN_TOUCH, key_bitmask) |
| 714 | && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) { |
| 715 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN; |
| 716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | #ifdef EV_SW |
| 720 | // figure out the switches this device reports |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 721 | 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] | 722 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 723 | bool hasSwitches = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
| 725 | for (int i=0; i<EV_SW; i++) { |
| 726 | //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask)); |
| 727 | if (test_bit(i, sw_bitmask)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 728 | hasSwitches = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | if (mSwitches[i] == 0) { |
| 730 | mSwitches[i] = device->id; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 735 | if (hasSwitches) { |
| 736 | device->classes |= INPUT_DEVICE_CLASS_SWITCH; |
| 737 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | #endif |
| 739 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 740 | if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) { |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 741 | char tmpfn[sizeof(name)]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | char keylayoutFilename[300]; |
| 743 | |
| 744 | // a more descriptive name |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 745 | device->name = name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | |
| 747 | // replace all the spaces with underscores |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 748 | strcpy(tmpfn, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' ')) |
| 750 | *p = '_'; |
| 751 | |
| 752 | // find the .kl file we need for this device |
| 753 | const char* root = getenv("ANDROID_ROOT"); |
| 754 | snprintf(keylayoutFilename, sizeof(keylayoutFilename), |
| 755 | "%s/usr/keylayout/%s.kl", root, tmpfn); |
| 756 | bool defaultKeymap = false; |
| 757 | if (access(keylayoutFilename, R_OK)) { |
| 758 | snprintf(keylayoutFilename, sizeof(keylayoutFilename), |
| 759 | "%s/usr/keylayout/%s", root, "qwerty.kl"); |
| 760 | defaultKeymap = true; |
| 761 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 762 | status_t status = device->layoutMap->load(keylayoutFilename); |
| 763 | if (status) { |
| 764 | LOGE("Error %d loading key layout.", status); |
| 765 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | |
| 767 | // tell the world about the devname (the descriptive name) |
Dianne Hackborn | a2e9226 | 2010-03-02 17:19:29 -0800 | [diff] [blame] | 768 | if (!mHaveFirstKeyboard && !defaultKeymap && strstr(name, "-keypad")) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | // the built-in keyboard has a well-known device ID of 0, |
| 770 | // this device better not go away. |
| 771 | mHaveFirstKeyboard = true; |
| 772 | mFirstKeyboardId = device->id; |
Dianne Hackborn | a2e9226 | 2010-03-02 17:19:29 -0800 | [diff] [blame] | 773 | property_set("hw.keyboards.0.devname", name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | // ensure mFirstKeyboardId is set to -something-. |
| 776 | if (mFirstKeyboardId == 0) { |
| 777 | mFirstKeyboardId = device->id; |
| 778 | } |
| 779 | } |
| 780 | char propName[100]; |
Dianne Hackborn | a2e9226 | 2010-03-02 17:19:29 -0800 | [diff] [blame] | 781 | sprintf(propName, "hw.keyboards.%u.devname", device->id); |
Iliyan Malchev | fc2ebc4 | 2009-08-06 14:50:08 -0700 | [diff] [blame] | 782 | property_set(propName, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 784 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 785 | if (hasKeycode(device, AKEYCODE_Q)) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 786 | device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 789 | // See if this device has a DPAD. |
| 790 | if (hasKeycode(device, AKEYCODE_DPAD_UP) && |
| 791 | hasKeycode(device, AKEYCODE_DPAD_DOWN) && |
| 792 | hasKeycode(device, AKEYCODE_DPAD_LEFT) && |
| 793 | hasKeycode(device, AKEYCODE_DPAD_RIGHT) && |
| 794 | hasKeycode(device, AKEYCODE_DPAD_CENTER)) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 795 | device->classes |= INPUT_DEVICE_CLASS_DPAD; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 798 | // See if this device has a gamepad. |
| 799 | for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES); i++) { |
| 800 | if (hasKeycode(device, GAMEPAD_KEYCODES[i])) { |
| 801 | device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
Dianne Hackborn | a2e9226 | 2010-03-02 17:19:29 -0800 | [diff] [blame] | 806 | LOGI("New keyboard: device->id=0x%x devname='%s' propName='%s' keylayout='%s'\n", |
| 807 | device->id, name, propName, keylayoutFilename); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 810 | // If the device isn't recognized as something we handle, don't monitor it. |
| 811 | if (device->classes == 0) { |
| 812 | LOGV("Dropping device %s %p, id = %d\n", deviceName, device, devid); |
| 813 | close(fd); |
| 814 | delete device; |
| 815 | return -1; |
| 816 | } |
| 817 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 818 | LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n", |
| 819 | deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes); |
| 820 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n", |
| 822 | deviceName, device, mFDCount, devid, device->classes); |
| 823 | |
| 824 | mDevicesById[devid].device = device; |
| 825 | device->next = mOpeningDevices; |
| 826 | mOpeningDevices = device; |
| 827 | mDevices[mFDCount] = device; |
| 828 | |
| 829 | mFDCount++; |
| 830 | return 0; |
| 831 | } |
| 832 | |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 833 | bool EventHub::hasKeycode(device_t* device, int keycode) const |
| 834 | { |
| 835 | if (device->keyBitmask == NULL || device->layoutMap == NULL) { |
| 836 | return false; |
| 837 | } |
| 838 | |
| 839 | Vector<int32_t> scanCodes; |
| 840 | device->layoutMap->findScancodes(keycode, &scanCodes); |
| 841 | const size_t N = scanCodes.size(); |
| 842 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 843 | int32_t sc = scanCodes.itemAt(i); |
| 844 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { |
| 845 | return true; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | return false; |
| 850 | } |
| 851 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | int EventHub::close_device(const char *deviceName) |
| 853 | { |
| 854 | AutoMutex _l(mLock); |
| 855 | |
| 856 | int i; |
| 857 | for(i = 1; i < mFDCount; i++) { |
| 858 | if(strcmp(mDevices[i]->path.string(), deviceName) == 0) { |
| 859 | //LOGD("remove device %d: %s\n", i, deviceName); |
| 860 | device_t* device = mDevices[i]; |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 861 | |
| 862 | LOGI("Removed device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n", |
| 863 | device->path.string(), device->name.string(), device->id, |
| 864 | mNumDevicesById, mFDCount, mFDs[i].fd, device->classes); |
| 865 | |
| 866 | // Clear this device's entry. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | int index = (device->id&ID_MASK); |
| 868 | mDevicesById[index].device = NULL; |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 869 | |
| 870 | // Close the file descriptor and compact the fd array. |
Mike Lockwood | 36dad72 | 2009-08-28 13:29:06 -0700 | [diff] [blame] | 871 | close(mFDs[i].fd); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 872 | int count = mFDCount - i - 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count); |
| 874 | memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count); |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 875 | mFDCount--; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | |
| 877 | #ifdef EV_SW |
| 878 | for (int j=0; j<EV_SW; j++) { |
| 879 | if (mSwitches[j] == device->id) { |
| 880 | mSwitches[j] = 0; |
| 881 | } |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | device->next = mClosingDevices; |
| 886 | mClosingDevices = device; |
| 887 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | if (device->id == mFirstKeyboardId) { |
| 889 | LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", |
| 890 | device->path.string(), mFirstKeyboardId); |
| 891 | mFirstKeyboardId = 0; |
Dianne Hackborn | a2e9226 | 2010-03-02 17:19:29 -0800 | [diff] [blame] | 892 | property_set("hw.keyboards.0.devname", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
| 894 | // clear the property |
| 895 | char propName[100]; |
Dianne Hackborn | a2e9226 | 2010-03-02 17:19:29 -0800 | [diff] [blame] | 896 | sprintf(propName, "hw.keyboards.%u.devname", device->id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | property_set(propName, NULL); |
| 898 | return 0; |
| 899 | } |
| 900 | } |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 901 | LOGE("remove device: %s not found\n", deviceName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | return -1; |
| 903 | } |
| 904 | |
| 905 | int EventHub::read_notify(int nfd) |
| 906 | { |
| 907 | #ifdef HAVE_INOTIFY |
| 908 | int res; |
| 909 | char devname[PATH_MAX]; |
| 910 | char *filename; |
| 911 | char event_buf[512]; |
| 912 | int event_size; |
| 913 | int event_pos = 0; |
| 914 | struct inotify_event *event; |
| 915 | |
Dianne Hackborn | a8f6018 | 2009-09-01 19:01:50 -0700 | [diff] [blame] | 916 | LOGV("EventHub::read_notify nfd: %d\n", nfd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | res = read(nfd, event_buf, sizeof(event_buf)); |
| 918 | if(res < (int)sizeof(*event)) { |
| 919 | if(errno == EINTR) |
| 920 | return 0; |
| 921 | LOGW("could not get event, %s\n", strerror(errno)); |
| 922 | return 1; |
| 923 | } |
| 924 | //printf("got %d bytes of event information\n", res); |
| 925 | |
| 926 | strcpy(devname, device_path); |
| 927 | filename = devname + strlen(devname); |
| 928 | *filename++ = '/'; |
| 929 | |
| 930 | while(res >= (int)sizeof(*event)) { |
| 931 | event = (struct inotify_event *)(event_buf + event_pos); |
| 932 | //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); |
| 933 | if(event->len) { |
| 934 | strcpy(filename, event->name); |
| 935 | if(event->mask & IN_CREATE) { |
| 936 | open_device(devname); |
| 937 | } |
| 938 | else { |
| 939 | close_device(devname); |
| 940 | } |
| 941 | } |
| 942 | event_size = sizeof(*event) + event->len; |
| 943 | res -= event_size; |
| 944 | event_pos += event_size; |
| 945 | } |
| 946 | #endif |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | |
| 951 | int EventHub::scan_dir(const char *dirname) |
| 952 | { |
| 953 | char devname[PATH_MAX]; |
| 954 | char *filename; |
| 955 | DIR *dir; |
| 956 | struct dirent *de; |
| 957 | dir = opendir(dirname); |
| 958 | if(dir == NULL) |
| 959 | return -1; |
| 960 | strcpy(devname, dirname); |
| 961 | filename = devname + strlen(devname); |
| 962 | *filename++ = '/'; |
| 963 | while((de = readdir(dir))) { |
| 964 | if(de->d_name[0] == '.' && |
| 965 | (de->d_name[1] == '\0' || |
| 966 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 967 | continue; |
| 968 | strcpy(filename, de->d_name); |
| 969 | open_device(devname); |
| 970 | } |
| 971 | closedir(dir); |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | }; // namespace android |