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