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