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> |
| 19 | #include <hardware_legacy/power.h> |
| 20 | |
| 21 | #include <cutils/properties.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <utils/Timers.h> |
Mathias Agopian | e0c3220 | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 24 | #include <utils/threads.h> |
Mathias Agopian | e0c3220 | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <memory.h> |
| 32 | #include <errno.h> |
| 33 | #include <assert.h> |
| 34 | |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 35 | #include <ui/KeyLayoutMap.h> |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 36 | #include <ui/KeyCharacterMap.h> |
| 37 | #include <ui/VirtualKeyMap.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | #include <string.h> |
| 40 | #include <stdint.h> |
| 41 | #include <dirent.h> |
| 42 | #ifdef HAVE_INOTIFY |
| 43 | # include <sys/inotify.h> |
| 44 | #endif |
| 45 | #ifdef HAVE_ANDROID_OS |
| 46 | # include <sys/limits.h> /* not part of Linux */ |
| 47 | #endif |
| 48 | #include <sys/poll.h> |
| 49 | #include <sys/ioctl.h> |
| 50 | |
| 51 | /* this macro is used to tell if "bit" is set in "array" |
| 52 | * it selects a byte from the array, and does a boolean AND |
| 53 | * operation with a byte that only has the relevant bit set. |
| 54 | * eg. to check for the 12th bit, we do (array[1] & 1<<4) |
| 55 | */ |
| 56 | #define test_bit(bit, array) (array[bit/8] & (1<<(bit%8))) |
| 57 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 58 | /* this macro computes the number of bytes needed to represent a bit array of the specified size */ |
| 59 | #define sizeof_bit_array(bits) ((bits + 7) / 8) |
| 60 | |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 61 | #ifndef ABS_MT_TOUCH_MAJOR |
| 62 | #define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */ |
| 63 | #endif |
| 64 | |
| 65 | #ifndef ABS_MT_POSITION_X |
| 66 | #define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */ |
| 67 | #endif |
| 68 | |
| 69 | #ifndef ABS_MT_POSITION_Y |
| 70 | #define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */ |
| 71 | #endif |
| 72 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 73 | // Fd at index 0 is always reserved for inotify |
| 74 | #define FIRST_ACTUAL_DEVICE_INDEX 1 |
| 75 | |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 76 | #define INDENT " " |
| 77 | #define INDENT2 " " |
| 78 | #define INDENT3 " " |
| 79 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | namespace android { |
| 81 | |
| 82 | static const char *WAKE_LOCK_ID = "KeyEvents"; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 83 | static const char *DEVICE_PATH = "/dev/input"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | /* return the larger integer */ |
| 86 | static inline int max(int v1, int v2) |
| 87 | { |
| 88 | return (v1 > v2) ? v1 : v2; |
| 89 | } |
| 90 | |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 91 | static inline const char* toString(bool value) { |
| 92 | return value ? "true" : "false"; |
| 93 | } |
| 94 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 95 | // --- EventHub::Device --- |
| 96 | |
| 97 | EventHub::Device::Device(int fd, int32_t id, const String8& path, |
| 98 | const InputDeviceIdentifier& identifier) : |
| 99 | next(NULL), |
| 100 | fd(fd), id(id), path(path), identifier(identifier), |
| 101 | classes(0), keyBitmask(NULL), configuration(NULL), virtualKeyMap(NULL) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 104 | EventHub::Device::~Device() { |
| 105 | close(); |
| 106 | delete[] keyBitmask; |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 107 | delete configuration; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 108 | delete virtualKeyMap; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 111 | void EventHub::Device::close() { |
| 112 | if (fd >= 0) { |
| 113 | ::close(fd); |
| 114 | fd = -1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | // --- EventHub --- |
| 120 | |
| 121 | EventHub::EventHub(void) : |
| 122 | mError(NO_INIT), mBuiltInKeyboardId(-1), mNextDeviceId(1), |
| 123 | mOpeningDevices(0), mClosingDevices(0), |
| 124 | mOpened(false), mNeedToSendFinishedDeviceScan(false), |
| 125 | mInputBufferIndex(0), mInputBufferCount(0), mInputFdIndex(0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 127 | #ifdef EV_SW |
| 128 | memset(mSwitches, 0, sizeof(mSwitches)); |
| 129 | #endif |
| 130 | } |
| 131 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 132 | EventHub::~EventHub(void) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | release_wake_lock(WAKE_LOCK_ID); |
| 134 | // we should free stuff here... |
| 135 | } |
| 136 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 137 | status_t EventHub::errorCheck() const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | return mError; |
| 139 | } |
| 140 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 141 | String8 EventHub::getDeviceName(int32_t deviceId) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | AutoMutex _l(mLock); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 143 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | if (device == NULL) return String8(); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 145 | return device->identifier.name; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 148 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | AutoMutex _l(mLock); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 150 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | if (device == NULL) return 0; |
| 152 | return device->classes; |
| 153 | } |
| 154 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 155 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 156 | AutoMutex _l(mLock); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 157 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 158 | if (device && device->configuration) { |
| 159 | *outConfiguration = *device->configuration; |
Jeff Brown | 53c1664 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 160 | } else { |
| 161 | outConfiguration->clear(); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 165 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 166 | RawAbsoluteAxisInfo* outAxisInfo) const { |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 167 | outAxisInfo->clear(); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 168 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | AutoMutex _l(mLock); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 170 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | if (device == NULL) return -1; |
| 172 | |
| 173 | struct input_absinfo info; |
| 174 | |
Jens Gulin | 7dcaa58 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 175 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 176 | LOGW("Error reading absolute controller %d for device %s fd %d\n", |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 177 | axis, device->identifier.name.string(), device->fd); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 178 | return -errno; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 180 | |
| 181 | if (info.minimum != info.maximum) { |
| 182 | outAxisInfo->valid = true; |
| 183 | outAxisInfo->minValue = info.minimum; |
| 184 | outAxisInfo->maxValue = info.maximum; |
| 185 | outAxisInfo->flat = info.flat; |
| 186 | outAxisInfo->fuzz = info.fuzz; |
| 187 | } |
| 188 | return OK; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 191 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 192 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
| 193 | AutoMutex _l(mLock); |
| 194 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 195 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 196 | if (device != NULL) { |
| 197 | return getScanCodeStateLocked(device, scanCode); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 200 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 203 | int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const { |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 204 | uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)]; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 205 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | 7dcaa58 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 206 | if (ioctl(device->fd, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 207 | EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) { |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 208 | return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 209 | } |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 210 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 213 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
| 214 | AutoMutex _l(mLock); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 215 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 216 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 217 | if (device != NULL) { |
| 218 | return getKeyCodeStateLocked(device, keyCode); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 220 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 223 | int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const { |
| 224 | if (!device->keyMap.haveKeyLayout()) { |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 225 | return AKEY_STATE_UNKNOWN; |
| 226 | } |
| 227 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | Vector<int32_t> scanCodes; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 229 | device->keyMap.keyLayoutMap->findScanCodes(keyCode, &scanCodes); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 230 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 231 | 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] | 232 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jens Gulin | 7dcaa58 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 233 | 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] | 234 | #if 0 |
| 235 | for (size_t i=0; i<=KEY_MAX; i++) { |
| 236 | LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask)); |
| 237 | } |
| 238 | #endif |
| 239 | const size_t N = scanCodes.size(); |
| 240 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 241 | int32_t sc = scanCodes.itemAt(i); |
| 242 | //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask)); |
| 243 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) { |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 244 | return AKEY_STATE_DOWN; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 247 | return AKEY_STATE_UP; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | } |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 249 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 252 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 253 | #ifdef EV_SW |
| 254 | if (sw >= 0 && sw <= SW_MAX) { |
| 255 | AutoMutex _l(mLock); |
| 256 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 257 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 258 | if (device != NULL) { |
| 259 | return getSwitchStateLocked(device, sw); |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 260 | } |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 261 | } |
| 262 | #endif |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 263 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 266 | int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const { |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 267 | uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)]; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 268 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jens Gulin | 7dcaa58 | 2010-06-22 22:21:57 +0200 | [diff] [blame] | 269 | if (ioctl(device->fd, |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 270 | EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 271 | return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 272 | } |
Jeff Brown | 5c1ed84 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 273 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 276 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 277 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 278 | AutoMutex _l(mLock); |
| 279 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 280 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 281 | if (device != NULL) { |
| 282 | return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags); |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 287 | bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes, |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 288 | const int32_t* keyCodes, uint8_t* outFlags) const { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 289 | if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | |
| 293 | Vector<int32_t> scanCodes; |
| 294 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
| 295 | scanCodes.clear(); |
| 296 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 297 | status_t err = device->keyMap.keyLayoutMap->findScanCodes(keyCodes[codeIndex], &scanCodes); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 298 | if (! err) { |
| 299 | // check the possible scan codes identified by the layout map against the |
| 300 | // map of codes actually emitted by the driver |
| 301 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
| 302 | if (test_bit(scanCodes[sc], device->keyBitmask)) { |
| 303 | outFlags[codeIndex] = 1; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | |
Dianne Hackborn | c968c3a | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 312 | status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode, |
| 313 | int32_t* outKeycode, uint32_t* outFlags) const |
| 314 | { |
| 315 | AutoMutex _l(mLock); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 316 | Device* device = getDeviceLocked(deviceId); |
Dianne Hackborn | c968c3a | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 317 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 318 | if (device && device->keyMap.haveKeyLayout()) { |
| 319 | status_t err = device->keyMap.keyLayoutMap->map(scancode, outKeycode, outFlags); |
Dianne Hackborn | c968c3a | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 320 | if (err == NO_ERROR) { |
| 321 | return NO_ERROR; |
| 322 | } |
| 323 | } |
| 324 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 325 | if (mBuiltInKeyboardId != -1) { |
| 326 | device = getDeviceLocked(mBuiltInKeyboardId); |
Dianne Hackborn | c968c3a | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 327 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 328 | if (device && device->keyMap.haveKeyLayout()) { |
| 329 | status_t err = device->keyMap.keyLayoutMap->map(scancode, outKeycode, outFlags); |
Dianne Hackborn | c968c3a | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 330 | if (err == NO_ERROR) { |
| 331 | return NO_ERROR; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | *outKeycode = 0; |
| 337 | *outFlags = 0; |
| 338 | return NAME_NOT_FOUND; |
| 339 | } |
| 340 | |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 341 | void EventHub::addExcludedDevice(const char* deviceName) |
| 342 | { |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 343 | AutoMutex _l(mLock); |
| 344 | |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 345 | String8 name(deviceName); |
| 346 | mExcludedDevices.push_back(name); |
| 347 | } |
| 348 | |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 349 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { |
| 350 | AutoMutex _l(mLock); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 351 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 352 | if (device) { |
| 353 | uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)]; |
| 354 | memset(bitmask, 0, sizeof(bitmask)); |
| 355 | if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) { |
| 356 | if (test_bit(led, bitmask)) { |
| 357 | return true; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
| 365 | AutoMutex _l(mLock); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 366 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 367 | if (device) { |
| 368 | struct input_event ev; |
| 369 | ev.time.tv_sec = 0; |
| 370 | ev.time.tv_usec = 0; |
| 371 | ev.type = EV_LED; |
| 372 | ev.code = led; |
| 373 | ev.value = on ? 1 : 0; |
| 374 | |
| 375 | ssize_t nWrite; |
| 376 | do { |
| 377 | nWrite = write(device->fd, &ev, sizeof(struct input_event)); |
| 378 | } while (nWrite == -1 && errno == EINTR); |
| 379 | } |
| 380 | } |
| 381 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 382 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, |
| 383 | Vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 384 | outVirtualKeys.clear(); |
| 385 | |
| 386 | AutoMutex _l(mLock); |
| 387 | Device* device = getDeviceLocked(deviceId); |
| 388 | if (device && device->virtualKeyMap) { |
| 389 | outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys()); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { |
| 394 | if (deviceId == 0) { |
| 395 | deviceId = mBuiltInKeyboardId; |
| 396 | } |
| 397 | |
| 398 | size_t numDevices = mDevices.size(); |
| 399 | for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < numDevices; i++) { |
| 400 | Device* device = mDevices[i]; |
| 401 | if (device->id == deviceId) { |
| 402 | return device; |
| 403 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | return NULL; |
| 406 | } |
| 407 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 408 | bool EventHub::getEvent(RawEvent* outEvent) { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 409 | outEvent->deviceId = 0; |
| 410 | outEvent->type = 0; |
| 411 | outEvent->scanCode = 0; |
| 412 | outEvent->keyCode = 0; |
| 413 | outEvent->flags = 0; |
| 414 | outEvent->value = 0; |
| 415 | outEvent->when = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | // Note that we only allow one caller to getEvent(), so don't need |
| 418 | // to do locking here... only when adding/removing devices. |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 419 | |
| 420 | if (!mOpened) { |
| 421 | mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR; |
| 422 | mOpened = true; |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 423 | mNeedToSendFinishedDeviceScan = true; |
Mike Lockwood | b441106 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 424 | } |
| 425 | |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 426 | for (;;) { |
| 427 | // Report any devices that had last been added/removed. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | if (mClosingDevices != NULL) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 429 | Device* device = mClosingDevices; |
| 430 | LOGV("Reporting device closed: id=%d, name=%s\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | device->id, device->path.string()); |
| 432 | mClosingDevices = device->next; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 433 | if (device->id == mBuiltInKeyboardId) { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 434 | outEvent->deviceId = 0; |
| 435 | } else { |
| 436 | outEvent->deviceId = device->id; |
| 437 | } |
| 438 | outEvent->type = DEVICE_REMOVED; |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 439 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | delete device; |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 441 | mNeedToSendFinishedDeviceScan = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | return true; |
| 443 | } |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 444 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | if (mOpeningDevices != NULL) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 446 | Device* device = mOpeningDevices; |
| 447 | LOGV("Reporting device opened: id=%d, name=%s\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | device->id, device->path.string()); |
| 449 | mOpeningDevices = device->next; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 450 | if (device->id == mBuiltInKeyboardId) { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 451 | outEvent->deviceId = 0; |
| 452 | } else { |
| 453 | outEvent->deviceId = device->id; |
| 454 | } |
| 455 | outEvent->type = DEVICE_ADDED; |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 456 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 457 | mNeedToSendFinishedDeviceScan = true; |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | if (mNeedToSendFinishedDeviceScan) { |
| 462 | mNeedToSendFinishedDeviceScan = false; |
| 463 | outEvent->type = FINISHED_DEVICE_SCAN; |
Jeff Brown | 3c3cc62 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 464 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | return true; |
| 466 | } |
| 467 | |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 468 | // Grab the next input event. |
| 469 | for (;;) { |
| 470 | // Consume buffered input events, if any. |
| 471 | if (mInputBufferIndex < mInputBufferCount) { |
| 472 | const struct input_event& iev = mInputBufferData[mInputBufferIndex++]; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 473 | const Device* device = mDevices[mInputFdIndex]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 475 | LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(), |
| 476 | (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 477 | if (device->id == mBuiltInKeyboardId) { |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 478 | outEvent->deviceId = 0; |
| 479 | } else { |
| 480 | outEvent->deviceId = device->id; |
| 481 | } |
| 482 | outEvent->type = iev.type; |
| 483 | outEvent->scanCode = iev.code; |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 484 | outEvent->flags = 0; |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 485 | if (iev.type == EV_KEY) { |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 486 | outEvent->keyCode = AKEYCODE_UNKNOWN; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 487 | if (device->keyMap.haveKeyLayout()) { |
| 488 | status_t err = device->keyMap.keyLayoutMap->map(iev.code, |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 489 | &outEvent->keyCode, &outEvent->flags); |
| 490 | LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n", |
| 491 | iev.code, outEvent->keyCode, outEvent->flags, err); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | } |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 493 | } else { |
| 494 | outEvent->keyCode = iev.code; |
| 495 | } |
| 496 | outEvent->value = iev.value; |
| 497 | |
| 498 | // Use an event timestamp in the same timebase as |
| 499 | // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis() |
| 500 | // as expected by the rest of the system. |
| 501 | outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 502 | return true; |
| 503 | } |
| 504 | |
| 505 | // Finish reading all events from devices identified in previous poll(). |
| 506 | // This code assumes that mInputDeviceIndex is initially 0 and that the |
| 507 | // revents member of pollfd is initialized to 0 when the device is first added. |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 508 | // Since mFds[0] is used for inotify, we process regular events starting at index 1. |
| 509 | mInputFdIndex += 1; |
| 510 | if (mInputFdIndex >= mFds.size()) { |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 511 | break; |
| 512 | } |
| 513 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 514 | const struct pollfd& pfd = mFds[mInputFdIndex]; |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 515 | if (pfd.revents & POLLIN) { |
| 516 | int32_t readSize = read(pfd.fd, mInputBufferData, |
| 517 | sizeof(struct input_event) * INPUT_BUFFER_SIZE); |
| 518 | if (readSize < 0) { |
| 519 | if (errno != EAGAIN && errno != EINTR) { |
| 520 | LOGW("could not get event (errno=%d)", errno); |
| 521 | } |
| 522 | } else if ((readSize % sizeof(struct input_event)) != 0) { |
| 523 | LOGE("could not get event (wrong size: %d)", readSize); |
| 524 | } else { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 525 | mInputBufferCount = size_t(readSize) / sizeof(struct input_event); |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 526 | mInputBufferIndex = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | } |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 530 | |
Jeff Brown | 7e40f36 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 531 | #if HAVE_INOTIFY |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 532 | // readNotify() will modify mFDs and mFDCount, so this must be done after |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | // processing all other events. |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 534 | if(mFds[0].revents & POLLIN) { |
| 535 | readNotify(mFds[0].fd); |
| 536 | mFds.editItemAt(0).revents = 0; |
Jeff Brown | 7e40f36 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 537 | continue; // report added or removed devices immediately |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | } |
Jeff Brown | 7e40f36 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 539 | #endif |
| 540 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 541 | mInputFdIndex = 0; |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 542 | |
| 543 | // Poll for events. Mind the wake lock dance! |
| 544 | // We hold a wake lock at all times except during poll(). This works due to some |
| 545 | // subtle choreography. When a device driver has pending (unread) events, it acquires |
| 546 | // a kernel wake lock. However, once the last pending event has been read, the device |
| 547 | // driver will release the kernel wake lock. To prevent the system from going to sleep |
| 548 | // when this happens, the EventHub holds onto its own user wake lock while the client |
| 549 | // is processing events. Thus the system can only sleep if there are no events |
| 550 | // pending or currently being processed. |
| 551 | release_wake_lock(WAKE_LOCK_ID); |
| 552 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 553 | int pollResult = poll(mFds.editArray(), mFds.size(), -1); |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 554 | |
| 555 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
| 556 | |
| 557 | if (pollResult <= 0) { |
| 558 | if (errno != EINTR) { |
Jeff Brown | 7e40f36 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 559 | LOGW("poll failed (errno=%d)\n", errno); |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 560 | usleep(100000); |
| 561 | } |
| 562 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Open the platform-specific input device. |
| 568 | */ |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 569 | bool EventHub::openPlatformInput(void) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | /* |
| 571 | * Open platform-specific input device(s). |
| 572 | */ |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 573 | int res, fd; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | #ifdef HAVE_INOTIFY |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 576 | fd = inotify_init(); |
| 577 | res = inotify_add_watch(fd, DEVICE_PATH, IN_DELETE | IN_CREATE); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | if(res < 0) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 579 | LOGE("could not add watch for %s, %s\n", DEVICE_PATH, strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | } |
| 581 | #else |
| 582 | /* |
| 583 | * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd. |
| 584 | * We allocate space for it and set it to something invalid. |
| 585 | */ |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 586 | fd = -1; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | #endif |
| 588 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 589 | // Reserve fd index 0 for inotify. |
| 590 | struct pollfd pollfd; |
| 591 | pollfd.fd = fd; |
| 592 | pollfd.events = POLLIN; |
| 593 | pollfd.revents = 0; |
| 594 | mFds.push(pollfd); |
| 595 | mDevices.push(NULL); |
| 596 | |
| 597 | res = scanDir(DEVICE_PATH); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | if(res < 0) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 599 | LOGE("scan dir failed for %s\n", DEVICE_PATH); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | return true; |
| 603 | } |
| 604 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | // ---------------------------------------------------------------------------- |
| 606 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 607 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { |
| 608 | const uint8_t* end = array + endIndex; |
| 609 | array += startIndex; |
| 610 | while (array != end) { |
| 611 | if (*(array++) != 0) { |
| 612 | return true; |
| 613 | } |
| 614 | } |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | static const int32_t GAMEPAD_KEYCODES[] = { |
| 619 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, |
| 620 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, |
| 621 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, |
| 622 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, |
| 623 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, |
| 624 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE |
| 625 | }; |
| 626 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 627 | int EventHub::openDevice(const char *devicePath) { |
| 628 | char buffer[80]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 630 | LOGV("Opening device: %s", devicePath); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | |
| 632 | AutoMutex _l(mLock); |
Nick Pelly | c81bb20 | 2010-01-20 19:36:49 -0800 | [diff] [blame] | 633 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 634 | int fd = open(devicePath, O_RDWR); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | if(fd < 0) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 636 | LOGE("could not open %s, %s\n", devicePath, strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | return -1; |
| 638 | } |
| 639 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 640 | InputDeviceIdentifier identifier; |
| 641 | |
| 642 | // Get device name. |
| 643 | if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { |
| 644 | //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno)); |
| 645 | } else { |
| 646 | buffer[sizeof(buffer) - 1] = '\0'; |
| 647 | identifier.name.setTo(buffer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | } |
Mike Lockwood | 24a7e04 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 649 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 650 | // Check to see if the device is on our excluded list |
Mike Lockwood | 24a7e04 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 651 | List<String8>::iterator iter = mExcludedDevices.begin(); |
| 652 | List<String8>::iterator end = mExcludedDevices.end(); |
| 653 | for ( ; iter != end; iter++) { |
| 654 | const char* test = *iter; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 655 | if (identifier.name == test) { |
| 656 | LOGI("ignoring event id %s driver %s\n", devicePath, test); |
Mike Lockwood | 24a7e04 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 657 | close(fd); |
Mike Lockwood | 24a7e04 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 658 | return -1; |
| 659 | } |
| 660 | } |
| 661 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 662 | // Get device driver version. |
| 663 | int driverVersion; |
| 664 | if(ioctl(fd, EVIOCGVERSION, &driverVersion)) { |
| 665 | LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno)); |
| 666 | close(fd); |
| 667 | return -1; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 670 | // Get device identifier. |
| 671 | struct input_id inputId; |
| 672 | if(ioctl(fd, EVIOCGID, &inputId)) { |
| 673 | LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno)); |
| 674 | close(fd); |
| 675 | return -1; |
| 676 | } |
| 677 | identifier.bus = inputId.bustype; |
| 678 | identifier.product = inputId.product; |
| 679 | identifier.vendor = inputId.vendor; |
| 680 | identifier.version = inputId.version; |
| 681 | |
| 682 | // Get device physical location. |
| 683 | if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { |
| 684 | //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); |
| 685 | } else { |
| 686 | buffer[sizeof(buffer) - 1] = '\0'; |
| 687 | identifier.location.setTo(buffer); |
| 688 | } |
| 689 | |
| 690 | // Get device unique id. |
| 691 | if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { |
| 692 | //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); |
| 693 | } else { |
| 694 | buffer[sizeof(buffer) - 1] = '\0'; |
| 695 | identifier.uniqueId.setTo(buffer); |
| 696 | } |
| 697 | |
| 698 | // Make file descriptor non-blocking for use with poll(). |
Jeff Brown | 8276307 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 699 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
| 700 | LOGE("Error %d making device file descriptor non-blocking.", errno); |
| 701 | close(fd); |
| 702 | return -1; |
| 703 | } |
| 704 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 705 | // Allocate device. (The device object takes ownership of the fd at this point.) |
| 706 | int32_t deviceId = mNextDeviceId++; |
| 707 | Device* device = new Device(fd, deviceId, String8(devicePath), identifier); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | |
| 709 | #if 0 |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 710 | LOGI("add device %d: %s\n", deviceId, devicePath); |
| 711 | LOGI(" bus: %04x\n" |
| 712 | " vendor %04x\n" |
| 713 | " product %04x\n" |
| 714 | " version %04x\n", |
| 715 | identifier.bus, identifier.vendor, identifier.product, identifier.version); |
| 716 | LOGI(" name: \"%s\"\n", identifier.name.string()); |
| 717 | LOGI(" location: \"%s\"\n", identifier.location.string()); |
| 718 | LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string()); |
| 719 | LOGI(" driver: v%d.%d.%d\n", |
| 720 | driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | #endif |
| 722 | |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 723 | // Load the configuration file for the device. |
| 724 | loadConfiguration(device); |
| 725 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 726 | // Figure out the kinds of events the device reports. |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 727 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 728 | 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] | 729 | memset(key_bitmask, 0, sizeof(key_bitmask)); |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 730 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | LOGV("Getting keys..."); |
| 732 | if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) { |
| 733 | //LOGI("MAP\n"); |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 734 | //for (int i = 0; i < sizeof(key_bitmask); i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | // LOGI("%d: 0x%02x\n", i, key_bitmask[i]); |
| 736 | //} |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 737 | |
| 738 | // See if this is a keyboard. Ignore everything in the button range except for |
| 739 | // gamepads which are also considered keyboards. |
| 740 | if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC)) |
| 741 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD), |
| 742 | sizeof_bit_array(BTN_DIGI)) |
| 743 | || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK), |
| 744 | sizeof_bit_array(KEY_MAX + 1))) { |
| 745 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
| 746 | |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 747 | device->keyBitmask = new uint8_t[sizeof(key_bitmask)]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | if (device->keyBitmask != NULL) { |
| 749 | memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask)); |
| 750 | } else { |
| 751 | delete device; |
| 752 | LOGE("out of memory allocating key bitmask"); |
| 753 | return -1; |
| 754 | } |
| 755 | } |
| 756 | } |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 757 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 758 | // See if this is a trackball (or mouse). |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | if (test_bit(BTN_MOUSE, key_bitmask)) { |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 760 | 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] | 761 | memset(rel_bitmask, 0, sizeof(rel_bitmask)); |
| 762 | LOGV("Getting relative controllers..."); |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 763 | 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] | 764 | 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] | 765 | device->classes |= INPUT_DEVICE_CLASS_TRACKBALL; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | } |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 769 | |
| 770 | // See if this is a touch pad. |
| 771 | uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)]; |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 772 | memset(abs_bitmask, 0, sizeof(abs_bitmask)); |
| 773 | LOGV("Getting absolute controllers..."); |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 774 | if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) { |
| 775 | // Is this a new modern multi-touch driver? |
Jeff Brown | 38a7fab | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 776 | if (test_bit(ABS_MT_POSITION_X, abs_bitmask) |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 777 | && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) { |
| 778 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT; |
| 779 | |
| 780 | // Is this an old style single-touch driver? |
| 781 | } else if (test_bit(BTN_TOUCH, key_bitmask) |
| 782 | && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) { |
| 783 | device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN; |
| 784 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | #ifdef EV_SW |
| 788 | // figure out the switches this device reports |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 789 | 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] | 790 | memset(sw_bitmask, 0, sizeof(sw_bitmask)); |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 791 | bool hasSwitches = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) { |
| 793 | for (int i=0; i<EV_SW; i++) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 794 | //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | if (test_bit(i, sw_bitmask)) { |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 796 | hasSwitches = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | if (mSwitches[i] == 0) { |
| 798 | mSwitches[i] = device->id; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
Jeff Brown | e57e895 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 803 | if (hasSwitches) { |
| 804 | device->classes |= INPUT_DEVICE_CLASS_SWITCH; |
| 805 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | #endif |
| 807 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 808 | if ((device->classes & INPUT_DEVICE_CLASS_TOUCHSCREEN)) { |
| 809 | // Load the virtual keys for the touch screen, if any. |
| 810 | // We do this now so that we can make sure to load the keymap if necessary. |
| 811 | status_t status = loadVirtualKeyMap(device); |
| 812 | if (!status) { |
| 813 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | } |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 815 | } |
| 816 | |
| 817 | if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) { |
| 818 | // Load the keymap for the device. |
| 819 | status_t status = loadKeyMap(device); |
| 820 | |
| 821 | // Set system properties for the keyboard. |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 822 | setKeyboardProperties(device, false); |
| 823 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 824 | // Register the keyboard as a built-in keyboard if it is eligible. |
| 825 | if (!status |
| 826 | && mBuiltInKeyboardId == -1 |
| 827 | && isEligibleBuiltInKeyboard(device->identifier, |
| 828 | device->configuration, &device->keyMap)) { |
| 829 | mBuiltInKeyboardId = device->id; |
| 830 | setKeyboardProperties(device, true); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 831 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 833 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 834 | if (hasKeycodeLocked(device, AKEYCODE_Q)) { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 835 | device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 836 | } |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 837 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 838 | // See if this device has a DPAD. |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 839 | if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) && |
| 840 | hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) && |
| 841 | hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) && |
| 842 | hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) && |
| 843 | hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) { |
Jeff Brown | e839a58 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 844 | device->classes |= INPUT_DEVICE_CLASS_DPAD; |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 845 | } |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 846 | |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 847 | // See if this device has a gamepad. |
Kenny Root | bc9c82f | 2010-10-21 15:46:03 -0700 | [diff] [blame] | 848 | for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) { |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 849 | if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) { |
Jeff Brown | 8575a87 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 850 | device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; |
| 851 | break; |
| 852 | } |
| 853 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Sean McNeil | 2e7a530 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 856 | // If the device isn't recognized as something we handle, don't monitor it. |
| 857 | if (device->classes == 0) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 858 | LOGV("Dropping device: id=%d, path='%s', name='%s'", |
| 859 | deviceId, devicePath, device->identifier.name.string()); |
Sean McNeil | 2e7a530 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 860 | delete device; |
| 861 | return -1; |
| 862 | } |
| 863 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 864 | LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, " |
| 865 | "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s", |
| 866 | deviceId, fd, devicePath, device->identifier.name.string(), |
| 867 | device->classes, |
| 868 | device->configurationFile.string(), |
| 869 | device->keyMap.keyLayoutFile.string(), |
| 870 | device->keyMap.keyCharacterMapFile.string(), |
| 871 | toString(mBuiltInKeyboardId == deviceId)); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 872 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 873 | struct pollfd pollfd; |
| 874 | pollfd.fd = fd; |
| 875 | pollfd.events = POLLIN; |
| 876 | pollfd.revents = 0; |
| 877 | mFds.push(pollfd); |
| 878 | mDevices.push(device); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | device->next = mOpeningDevices; |
| 881 | mOpeningDevices = device; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | return 0; |
| 883 | } |
| 884 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 885 | void EventHub::loadConfiguration(Device* device) { |
| 886 | device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier( |
| 887 | device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 888 | if (device->configurationFile.isEmpty()) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 889 | LOGD("No input device configuration file found for device '%s'.", |
| 890 | device->identifier.name.string()); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 891 | } else { |
| 892 | status_t status = PropertyMap::load(device->configurationFile, |
| 893 | &device->configuration); |
| 894 | if (status) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 895 | LOGE("Error loading input device configuration file for device '%s'. " |
| 896 | "Using default configuration.", |
| 897 | device->identifier.name.string()); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 902 | status_t EventHub::loadVirtualKeyMap(Device* device) { |
| 903 | // The virtual key map is supplied by the kernel as a system board property file. |
| 904 | String8 path; |
| 905 | path.append("/sys/board_properties/virtualkeys."); |
| 906 | path.append(device->identifier.name); |
| 907 | if (access(path.string(), R_OK)) { |
| 908 | return NAME_NOT_FOUND; |
| 909 | } |
| 910 | return VirtualKeyMap::load(path, &device->virtualKeyMap); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 913 | status_t EventHub::loadKeyMap(Device* device) { |
| 914 | return device->keyMap.load(device->identifier, device->configuration); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 917 | void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) { |
| 918 | int32_t id = builtInKeyboard ? 0 : device->id; |
| 919 | android::setKeyboardProperties(id, device->identifier, |
| 920 | device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile); |
| 921 | } |
| 922 | |
| 923 | void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) { |
| 924 | int32_t id = builtInKeyboard ? 0 : device->id; |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 925 | android::clearKeyboardProperties(id); |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 928 | bool EventHub::hasKeycodeLocked(Device* device, int keycode) const { |
| 929 | if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 930 | return false; |
| 931 | } |
| 932 | |
| 933 | Vector<int32_t> scanCodes; |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 934 | device->keyMap.keyLayoutMap->findScanCodes(keycode, &scanCodes); |
Dianne Hackborn | c591736 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 935 | const size_t N = scanCodes.size(); |
| 936 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 937 | int32_t sc = scanCodes.itemAt(i); |
| 938 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { |
| 939 | return true; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | return false; |
| 944 | } |
| 945 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 946 | int EventHub::closeDevice(const char *devicePath) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | AutoMutex _l(mLock); |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 948 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 949 | for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) { |
| 950 | Device* device = mDevices[i]; |
| 951 | if (device->path == devicePath) { |
| 952 | LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n", |
| 953 | device->path.string(), device->identifier.name.string(), device->id, |
| 954 | device->fd, device->classes); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | |
| 956 | #ifdef EV_SW |
| 957 | for (int j=0; j<EV_SW; j++) { |
| 958 | if (mSwitches[j] == device->id) { |
| 959 | mSwitches[j] = 0; |
| 960 | } |
| 961 | } |
| 962 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 964 | if (device->id == mBuiltInKeyboardId) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 966 | device->path.string(), mBuiltInKeyboardId); |
| 967 | mBuiltInKeyboardId = -1; |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 968 | clearKeyboardProperties(device, true); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | } |
Jeff Brown | 6a817e2 | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 970 | clearKeyboardProperties(device, false); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 971 | |
| 972 | mFds.removeAt(i); |
| 973 | mDevices.removeAt(i); |
| 974 | device->close(); |
| 975 | |
| 976 | device->next = mClosingDevices; |
| 977 | mClosingDevices = device; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | } |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 981 | LOGE("remove device: %s not found\n", devicePath); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | return -1; |
| 983 | } |
| 984 | |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 985 | int EventHub::readNotify(int nfd) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | #ifdef HAVE_INOTIFY |
| 987 | int res; |
| 988 | char devname[PATH_MAX]; |
| 989 | char *filename; |
| 990 | char event_buf[512]; |
| 991 | int event_size; |
| 992 | int event_pos = 0; |
| 993 | struct inotify_event *event; |
| 994 | |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 995 | LOGV("EventHub::readNotify nfd: %d\n", nfd); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | res = read(nfd, event_buf, sizeof(event_buf)); |
| 997 | if(res < (int)sizeof(*event)) { |
| 998 | if(errno == EINTR) |
| 999 | return 0; |
| 1000 | LOGW("could not get event, %s\n", strerror(errno)); |
| 1001 | return 1; |
| 1002 | } |
| 1003 | //printf("got %d bytes of event information\n", res); |
| 1004 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1005 | strcpy(devname, DEVICE_PATH); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | filename = devname + strlen(devname); |
| 1007 | *filename++ = '/'; |
| 1008 | |
| 1009 | while(res >= (int)sizeof(*event)) { |
| 1010 | event = (struct inotify_event *)(event_buf + event_pos); |
| 1011 | //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); |
| 1012 | if(event->len) { |
| 1013 | strcpy(filename, event->name); |
| 1014 | if(event->mask & IN_CREATE) { |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1015 | openDevice(devname); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | } |
| 1017 | else { |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1018 | closeDevice(devname); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | event_size = sizeof(*event) + event->len; |
| 1022 | res -= event_size; |
| 1023 | event_pos += event_size; |
| 1024 | } |
| 1025 | #endif |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1029 | int EventHub::scanDir(const char *dirname) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | { |
| 1031 | char devname[PATH_MAX]; |
| 1032 | char *filename; |
| 1033 | DIR *dir; |
| 1034 | struct dirent *de; |
| 1035 | dir = opendir(dirname); |
| 1036 | if(dir == NULL) |
| 1037 | return -1; |
| 1038 | strcpy(devname, dirname); |
| 1039 | filename = devname + strlen(devname); |
| 1040 | *filename++ = '/'; |
| 1041 | while((de = readdir(dir))) { |
| 1042 | if(de->d_name[0] == '.' && |
| 1043 | (de->d_name[1] == '\0' || |
| 1044 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 1045 | continue; |
| 1046 | strcpy(filename, de->d_name); |
Jeff Brown | 1ad00e9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 1047 | openDevice(devname); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | } |
| 1049 | closedir(dir); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1053 | void EventHub::dump(String8& dump) { |
| 1054 | dump.append("Event Hub State:\n"); |
| 1055 | |
| 1056 | { // acquire lock |
| 1057 | AutoMutex _l(mLock); |
| 1058 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1059 | dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1060 | |
| 1061 | dump.append(INDENT "Devices:\n"); |
| 1062 | |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1063 | for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) { |
| 1064 | const Device* device = mDevices[i]; |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1065 | if (device) { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1066 | if (mBuiltInKeyboardId == device->id) { |
| 1067 | dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", |
| 1068 | device->id, device->identifier.name.string()); |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1069 | } else { |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1070 | dump.appendFormat(INDENT2 "%d: %s\n", device->id, |
| 1071 | device->identifier.name.string()); |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1072 | } |
| 1073 | dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes); |
| 1074 | dump.appendFormat(INDENT3 "Path: %s\n", device->path.string()); |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1075 | dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string()); |
| 1076 | dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string()); |
| 1077 | dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " |
| 1078 | "product=0x%04x, version=0x%04x\n", |
| 1079 | device->identifier.bus, device->identifier.vendor, |
| 1080 | device->identifier.product, device->identifier.version); |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1081 | dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1082 | device->keyMap.keyLayoutFile.string()); |
Jeff Brown | a3477c8 | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1083 | dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n", |
Jeff Brown | db36064 | 2010-12-02 13:50:46 -0800 | [diff] [blame^] | 1084 | device->keyMap.keyCharacterMapFile.string()); |
Jeff Brown | 6688837 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1085 | dump.appendFormat(INDENT3 "ConfigurationFile: %s\n", |
| 1086 | device->configurationFile.string()); |
Jeff Brown | 2806e38 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | } // release lock |
| 1090 | } |
| 1091 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | }; // namespace android |