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