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