| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2005 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #define LOG_TAG "EventHub" | 
 | 18 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 19 | // #define LOG_NDEBUG 0 | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 |  | 
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 21 | #include "EventHub.h" | 
 | 22 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <hardware_legacy/power.h> | 
 | 24 |  | 
 | 25 | #include <cutils/properties.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> | 
 | 27 | #include <utils/Timers.h> | 
| Mathias Agopian | 3b4062e | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 28 | #include <utils/threads.h> | 
| Mathias Agopian | 3b4062e | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 29 | #include <utils/Errors.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 |  | 
 | 31 | #include <stdlib.h> | 
 | 32 | #include <stdio.h> | 
 | 33 | #include <unistd.h> | 
 | 34 | #include <fcntl.h> | 
 | 35 | #include <memory.h> | 
 | 36 | #include <errno.h> | 
 | 37 | #include <assert.h> | 
 | 38 |  | 
| Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 39 | #include <ui/KeyLayoutMap.h> | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 40 | #include <ui/KeyCharacterMap.h> | 
 | 41 | #include <ui/VirtualKeyMap.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 |  | 
 | 43 | #include <string.h> | 
 | 44 | #include <stdint.h> | 
 | 45 | #include <dirent.h> | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 46 |  | 
 | 47 | #include <sys/inotify.h> | 
 | 48 | #include <sys/epoll.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | #include <sys/ioctl.h> | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 50 | #include <sys/limits.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 |  | 
 | 52 | /* this macro is used to tell if "bit" is set in "array" | 
 | 53 |  * it selects a byte from the array, and does a boolean AND | 
 | 54 |  * operation with a byte that only has the relevant bit set. | 
 | 55 |  * eg. to check for the 12th bit, we do (array[1] & 1<<4) | 
 | 56 |  */ | 
 | 57 | #define test_bit(bit, array)    (array[bit/8] & (1<<(bit%8))) | 
 | 58 |  | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 59 | /* this macro computes the number of bytes needed to represent a bit array of the specified size */ | 
 | 60 | #define sizeof_bit_array(bits)  ((bits + 7) / 8) | 
 | 61 |  | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 62 | #define INDENT "  " | 
 | 63 | #define INDENT2 "    " | 
 | 64 | #define INDENT3 "      " | 
 | 65 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | namespace android { | 
 | 67 |  | 
 | 68 | static const char *WAKE_LOCK_ID = "KeyEvents"; | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 69 | static const char *DEVICE_PATH = "/dev/input"; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 |  | 
 | 71 | /* return the larger integer */ | 
 | 72 | static inline int max(int v1, int v2) | 
 | 73 | { | 
 | 74 |     return (v1 > v2) ? v1 : v2; | 
 | 75 | } | 
 | 76 |  | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 77 | static inline const char* toString(bool value) { | 
 | 78 |     return value ? "true" : "false"; | 
 | 79 | } | 
 | 80 |  | 
| Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 81 | // --- Global Functions --- | 
 | 82 |  | 
 | 83 | uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) { | 
 | 84 |     // Touch devices get dibs on touch-related axes. | 
 | 85 |     if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) { | 
 | 86 |         switch (axis) { | 
 | 87 |         case ABS_X: | 
 | 88 |         case ABS_Y: | 
 | 89 |         case ABS_PRESSURE: | 
 | 90 |         case ABS_TOOL_WIDTH: | 
 | 91 |         case ABS_DISTANCE: | 
 | 92 |         case ABS_TILT_X: | 
 | 93 |         case ABS_TILT_Y: | 
 | 94 |         case ABS_MT_SLOT: | 
 | 95 |         case ABS_MT_TOUCH_MAJOR: | 
 | 96 |         case ABS_MT_TOUCH_MINOR: | 
 | 97 |         case ABS_MT_WIDTH_MAJOR: | 
 | 98 |         case ABS_MT_WIDTH_MINOR: | 
 | 99 |         case ABS_MT_ORIENTATION: | 
 | 100 |         case ABS_MT_POSITION_X: | 
 | 101 |         case ABS_MT_POSITION_Y: | 
 | 102 |         case ABS_MT_TOOL_TYPE: | 
 | 103 |         case ABS_MT_BLOB_ID: | 
 | 104 |         case ABS_MT_TRACKING_ID: | 
 | 105 |         case ABS_MT_PRESSURE: | 
 | 106 |         case ABS_MT_DISTANCE: | 
 | 107 |             return INPUT_DEVICE_CLASS_TOUCH; | 
 | 108 |         } | 
 | 109 |     } | 
 | 110 |  | 
 | 111 |     // Joystick devices get the rest. | 
 | 112 |     return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK; | 
 | 113 | } | 
 | 114 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 115 | // --- EventHub::Device --- | 
 | 116 |  | 
 | 117 | EventHub::Device::Device(int fd, int32_t id, const String8& path, | 
 | 118 |         const InputDeviceIdentifier& identifier) : | 
 | 119 |         next(NULL), | 
 | 120 |         fd(fd), id(id), path(path), identifier(identifier), | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 121 |         classes(0), configuration(NULL), virtualKeyMap(NULL) { | 
 | 122 |     memset(keyBitmask, 0, sizeof(keyBitmask)); | 
 | 123 |     memset(absBitmask, 0, sizeof(absBitmask)); | 
 | 124 |     memset(relBitmask, 0, sizeof(relBitmask)); | 
 | 125 |     memset(swBitmask, 0, sizeof(swBitmask)); | 
 | 126 |     memset(ledBitmask, 0, sizeof(ledBitmask)); | 
 | 127 |     memset(propBitmask, 0, sizeof(propBitmask)); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } | 
 | 129 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 130 | EventHub::Device::~Device() { | 
 | 131 |     close(); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 132 |     delete configuration; | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 133 |     delete virtualKeyMap; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } | 
 | 135 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 136 | void EventHub::Device::close() { | 
 | 137 |     if (fd >= 0) { | 
 | 138 |         ::close(fd); | 
 | 139 |         fd = -1; | 
 | 140 |     } | 
 | 141 | } | 
 | 142 |  | 
 | 143 |  | 
 | 144 | // --- EventHub --- | 
 | 145 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 146 | const uint32_t EventHub::EPOLL_ID_INOTIFY; | 
 | 147 | const uint32_t EventHub::EPOLL_ID_WAKE; | 
 | 148 | const int EventHub::EPOLL_SIZE_HINT; | 
 | 149 | const int EventHub::EPOLL_MAX_EVENTS; | 
 | 150 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 151 | EventHub::EventHub(void) : | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 152 |         mBuiltInKeyboardId(-1), mNextDeviceId(1), | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 153 |         mOpeningDevices(0), mClosingDevices(0), | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 154 |         mNeedToSendFinishedDeviceScan(false), | 
 | 155 |         mNeedToReopenDevices(false), mNeedToScanDevices(true), | 
 | 156 |         mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 |     acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 158 |  | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 159 |     mNumCpus = sysconf(_SC_NPROCESSORS_ONLN); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 160 |  | 
 | 161 |     mEpollFd = epoll_create(EPOLL_SIZE_HINT); | 
 | 162 |     LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance.  errno=%d", errno); | 
 | 163 |  | 
 | 164 |     mINotifyFd = inotify_init(); | 
 | 165 |     int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE); | 
 | 166 |     LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s.  errno=%d", | 
 | 167 |             DEVICE_PATH, errno); | 
 | 168 |  | 
 | 169 |     struct epoll_event eventItem; | 
 | 170 |     memset(&eventItem, 0, sizeof(eventItem)); | 
 | 171 |     eventItem.events = EPOLLIN; | 
 | 172 |     eventItem.data.u32 = EPOLL_ID_INOTIFY; | 
 | 173 |     result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem); | 
 | 174 |     LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance.  errno=%d", errno); | 
 | 175 |  | 
 | 176 |     int wakeFds[2]; | 
 | 177 |     result = pipe(wakeFds); | 
 | 178 |     LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe.  errno=%d", errno); | 
 | 179 |  | 
 | 180 |     mWakeReadPipeFd = wakeFds[0]; | 
 | 181 |     mWakeWritePipeFd = wakeFds[1]; | 
 | 182 |  | 
 | 183 |     result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK); | 
 | 184 |     LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking.  errno=%d", | 
 | 185 |             errno); | 
 | 186 |  | 
 | 187 |     result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK); | 
 | 188 |     LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking.  errno=%d", | 
 | 189 |             errno); | 
 | 190 |  | 
 | 191 |     eventItem.data.u32 = EPOLL_ID_WAKE; | 
 | 192 |     result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem); | 
 | 193 |     LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance.  errno=%d", | 
 | 194 |             errno); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | } | 
 | 196 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 197 | EventHub::~EventHub(void) { | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 198 |     closeAllDevicesLocked(); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 200 |     while (mClosingDevices) { | 
 | 201 |         Device* device = mClosingDevices; | 
 | 202 |         mClosingDevices = device->next; | 
 | 203 |         delete device; | 
 | 204 |     } | 
 | 205 |  | 
 | 206 |     ::close(mEpollFd); | 
 | 207 |     ::close(mINotifyFd); | 
 | 208 |     ::close(mWakeReadPipeFd); | 
 | 209 |     ::close(mWakeWritePipeFd); | 
 | 210 |  | 
 | 211 |     release_wake_lock(WAKE_LOCK_ID); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } | 
 | 213 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 214 | String8 EventHub::getDeviceName(int32_t deviceId) const { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 |     AutoMutex _l(mLock); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 216 |     Device* device = getDeviceLocked(deviceId); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 |     if (device == NULL) return String8(); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 218 |     return device->identifier.name; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } | 
 | 220 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 221 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 |     AutoMutex _l(mLock); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 223 |     Device* device = getDeviceLocked(deviceId); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 |     if (device == NULL) return 0; | 
 | 225 |     return device->classes; | 
 | 226 | } | 
 | 227 |  | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 228 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 229 |     AutoMutex _l(mLock); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 230 |     Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 231 |     if (device && device->configuration) { | 
 | 232 |         *outConfiguration = *device->configuration; | 
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 233 |     } else { | 
 | 234 |         outConfiguration->clear(); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 235 |     } | 
 | 236 | } | 
 | 237 |  | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 238 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, | 
 | 239 |         RawAbsoluteAxisInfo* outAxisInfo) const { | 
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 240 |     outAxisInfo->clear(); | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 241 |  | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 242 |     if (axis >= 0 && axis <= ABS_MAX) { | 
 | 243 |         AutoMutex _l(mLock); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 |  | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 245 |         Device* device = getDeviceLocked(deviceId); | 
 | 246 |         if (device && test_bit(axis, device->absBitmask)) { | 
 | 247 |             struct input_absinfo info; | 
 | 248 |             if(ioctl(device->fd, EVIOCGABS(axis), &info)) { | 
 | 249 |                 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", | 
 | 250 |                      axis, device->identifier.name.string(), device->fd, errno); | 
 | 251 |                 return -errno; | 
 | 252 |             } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 |  | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 254 |             if (info.minimum != info.maximum) { | 
 | 255 |                 outAxisInfo->valid = true; | 
 | 256 |                 outAxisInfo->minValue = info.minimum; | 
 | 257 |                 outAxisInfo->maxValue = info.maximum; | 
 | 258 |                 outAxisInfo->flat = info.flat; | 
 | 259 |                 outAxisInfo->fuzz = info.fuzz; | 
 | 260 |                 outAxisInfo->resolution = info.resolution; | 
 | 261 |             } | 
 | 262 |             return OK; | 
 | 263 |         } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 |     } | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 265 |     return -1; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } | 
 | 267 |  | 
| Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 268 | bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const { | 
 | 269 |     if (axis >= 0 && axis <= REL_MAX) { | 
 | 270 |         AutoMutex _l(mLock); | 
 | 271 |  | 
 | 272 |         Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 273 |         if (device) { | 
| Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 274 |             return test_bit(axis, device->relBitmask); | 
 | 275 |         } | 
 | 276 |     } | 
 | 277 |     return false; | 
 | 278 | } | 
 | 279 |  | 
| Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 280 | bool EventHub::hasInputProperty(int32_t deviceId, int property) const { | 
 | 281 |     if (property >= 0 && property <= INPUT_PROP_MAX) { | 
 | 282 |         AutoMutex _l(mLock); | 
 | 283 |  | 
 | 284 |         Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 285 |         if (device) { | 
| Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 286 |             return test_bit(property, device->propBitmask); | 
 | 287 |         } | 
 | 288 |     } | 
 | 289 |     return false; | 
 | 290 | } | 
 | 291 |  | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 292 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 293 |     if (scanCode >= 0 && scanCode <= KEY_MAX) { | 
 | 294 |         AutoMutex _l(mLock); | 
 | 295 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 296 |         Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 297 |         if (device && test_bit(scanCode, device->keyBitmask)) { | 
 | 298 |             uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; | 
 | 299 |             memset(keyState, 0, sizeof(keyState)); | 
 | 300 |             if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { | 
 | 301 |                 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; | 
 | 302 |             } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 |         } | 
 | 304 |     } | 
| Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 305 |     return AKEY_STATE_UNKNOWN; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | } | 
 | 307 |  | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 308 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { | 
 | 309 |     AutoMutex _l(mLock); | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 310 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 311 |     Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 312 |     if (device && device->keyMap.haveKeyLayout()) { | 
 | 313 |         Vector<int32_t> scanCodes; | 
 | 314 |         device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes); | 
 | 315 |         if (scanCodes.size() != 0) { | 
 | 316 |             uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; | 
 | 317 |             memset(keyState, 0, sizeof(keyState)); | 
 | 318 |             if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { | 
 | 319 |                 for (size_t i = 0; i < scanCodes.size(); i++) { | 
 | 320 |                     int32_t sc = scanCodes.itemAt(i); | 
 | 321 |                     if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) { | 
 | 322 |                         return AKEY_STATE_DOWN; | 
 | 323 |                     } | 
 | 324 |                 } | 
 | 325 |                 return AKEY_STATE_UP; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 |             } | 
 | 327 |         } | 
 | 328 |     } | 
| Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 329 |     return AKEY_STATE_UNKNOWN; | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 330 | } | 
 | 331 |  | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 332 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 333 |     if (sw >= 0 && sw <= SW_MAX) { | 
 | 334 |         AutoMutex _l(mLock); | 
 | 335 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 336 |         Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 337 |         if (device && test_bit(sw, device->swBitmask)) { | 
 | 338 |             uint8_t swState[sizeof_bit_array(SW_MAX + 1)]; | 
 | 339 |             memset(swState, 0, sizeof(swState)); | 
 | 340 |             if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) { | 
 | 341 |                 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; | 
 | 342 |             } | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 343 |         } | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 344 |     } | 
| Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 345 |     return AKEY_STATE_UNKNOWN; | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 346 | } | 
 | 347 |  | 
| Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 348 | status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const { | 
| Jeff Brown | 0630975 | 2011-08-11 17:10:06 -0700 | [diff] [blame] | 349 |     *outValue = 0; | 
 | 350 |  | 
| Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 351 |     if (axis >= 0 && axis <= ABS_MAX) { | 
 | 352 |         AutoMutex _l(mLock); | 
 | 353 |  | 
 | 354 |         Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 355 |         if (device && test_bit(axis, device->absBitmask)) { | 
 | 356 |             struct input_absinfo info; | 
 | 357 |             if(ioctl(device->fd, EVIOCGABS(axis), &info)) { | 
 | 358 |                 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", | 
 | 359 |                      axis, device->identifier.name.string(), device->fd, errno); | 
 | 360 |                 return -errno; | 
 | 361 |             } | 
 | 362 |  | 
 | 363 |             *outValue = info.value; | 
 | 364 |             return OK; | 
| Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 365 |         } | 
 | 366 |     } | 
| Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 367 |     return -1; | 
 | 368 | } | 
 | 369 |  | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 370 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, | 
 | 371 |         const int32_t* keyCodes, uint8_t* outFlags) const { | 
 | 372 |     AutoMutex _l(mLock); | 
 | 373 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 374 |     Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 375 |     if (device && device->keyMap.haveKeyLayout()) { | 
 | 376 |         Vector<int32_t> scanCodes; | 
 | 377 |         for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { | 
 | 378 |             scanCodes.clear(); | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 379 |  | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 380 |             status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey( | 
 | 381 |                     keyCodes[codeIndex], &scanCodes); | 
 | 382 |             if (! err) { | 
 | 383 |                 // check the possible scan codes identified by the layout map against the | 
 | 384 |                 // map of codes actually emitted by the driver | 
 | 385 |                 for (size_t sc = 0; sc < scanCodes.size(); sc++) { | 
 | 386 |                     if (test_bit(scanCodes[sc], device->keyBitmask)) { | 
 | 387 |                         outFlags[codeIndex] = 1; | 
 | 388 |                         break; | 
 | 389 |                     } | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 390 |                 } | 
 | 391 |             } | 
 | 392 |         } | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 393 |         return true; | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 394 |     } | 
| Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 395 |     return false; | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 396 | } | 
 | 397 |  | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 398 | status_t EventHub::mapKey(int32_t deviceId, int scancode, | 
| Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 399 |         int32_t* outKeycode, uint32_t* outFlags) const | 
 | 400 | { | 
 | 401 |     AutoMutex _l(mLock); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 402 |     Device* device = getDeviceLocked(deviceId); | 
| Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 403 |      | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 404 |     if (device && device->keyMap.haveKeyLayout()) { | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 405 |         status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags); | 
| Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 406 |         if (err == NO_ERROR) { | 
 | 407 |             return NO_ERROR; | 
 | 408 |         } | 
 | 409 |     } | 
 | 410 |      | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 411 |     if (mBuiltInKeyboardId != -1) { | 
 | 412 |         device = getDeviceLocked(mBuiltInKeyboardId); | 
| Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 413 |          | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 414 |         if (device && device->keyMap.haveKeyLayout()) { | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 415 |             status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags); | 
| Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 416 |             if (err == NO_ERROR) { | 
 | 417 |                 return NO_ERROR; | 
 | 418 |             } | 
 | 419 |         } | 
 | 420 |     } | 
 | 421 |      | 
 | 422 |     *outKeycode = 0; | 
 | 423 |     *outFlags = 0; | 
 | 424 |     return NAME_NOT_FOUND; | 
 | 425 | } | 
 | 426 |  | 
| Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 427 | status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 428 | { | 
 | 429 |     AutoMutex _l(mLock); | 
 | 430 |     Device* device = getDeviceLocked(deviceId); | 
 | 431 |  | 
 | 432 |     if (device && device->keyMap.haveKeyLayout()) { | 
| Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 433 |         status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo); | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 434 |         if (err == NO_ERROR) { | 
 | 435 |             return NO_ERROR; | 
 | 436 |         } | 
 | 437 |     } | 
 | 438 |  | 
 | 439 |     if (mBuiltInKeyboardId != -1) { | 
 | 440 |         device = getDeviceLocked(mBuiltInKeyboardId); | 
 | 441 |  | 
 | 442 |         if (device && device->keyMap.haveKeyLayout()) { | 
| Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 443 |             status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo); | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 444 |             if (err == NO_ERROR) { | 
 | 445 |                 return NO_ERROR; | 
 | 446 |             } | 
 | 447 |         } | 
 | 448 |     } | 
 | 449 |  | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 450 |     return NAME_NOT_FOUND; | 
 | 451 | } | 
 | 452 |  | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 453 | void EventHub::setExcludedDevices(const Vector<String8>& devices) { | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 454 |     AutoMutex _l(mLock); | 
 | 455 |  | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 456 |     mExcludedDevices = devices; | 
| Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 457 | } | 
 | 458 |  | 
| Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 459 | bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const { | 
 | 460 |     AutoMutex _l(mLock); | 
 | 461 |     Device* device = getDeviceLocked(deviceId); | 
 | 462 |     if (device && scanCode >= 0 && scanCode <= KEY_MAX) { | 
 | 463 |         if (test_bit(scanCode, device->keyBitmask)) { | 
 | 464 |             return true; | 
 | 465 |         } | 
 | 466 |     } | 
 | 467 |     return false; | 
 | 468 | } | 
 | 469 |  | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 470 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { | 
 | 471 |     AutoMutex _l(mLock); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 472 |     Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 473 |     if (device && led >= 0 && led <= LED_MAX) { | 
 | 474 |         if (test_bit(led, device->ledBitmask)) { | 
 | 475 |             return true; | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 476 |         } | 
 | 477 |     } | 
 | 478 |     return false; | 
 | 479 | } | 
 | 480 |  | 
 | 481 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { | 
 | 482 |     AutoMutex _l(mLock); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 483 |     Device* device = getDeviceLocked(deviceId); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 484 |     if (device && led >= 0 && led <= LED_MAX) { | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 485 |         struct input_event ev; | 
 | 486 |         ev.time.tv_sec = 0; | 
 | 487 |         ev.time.tv_usec = 0; | 
 | 488 |         ev.type = EV_LED; | 
 | 489 |         ev.code = led; | 
 | 490 |         ev.value = on ? 1 : 0; | 
 | 491 |  | 
 | 492 |         ssize_t nWrite; | 
 | 493 |         do { | 
 | 494 |             nWrite = write(device->fd, &ev, sizeof(struct input_event)); | 
 | 495 |         } while (nWrite == -1 && errno == EINTR); | 
 | 496 |     } | 
 | 497 | } | 
 | 498 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 499 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, | 
 | 500 |         Vector<VirtualKeyDefinition>& outVirtualKeys) const { | 
 | 501 |     outVirtualKeys.clear(); | 
 | 502 |  | 
 | 503 |     AutoMutex _l(mLock); | 
 | 504 |     Device* device = getDeviceLocked(deviceId); | 
 | 505 |     if (device && device->virtualKeyMap) { | 
 | 506 |         outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys()); | 
 | 507 |     } | 
 | 508 | } | 
 | 509 |  | 
| Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 510 | String8 EventHub::getKeyCharacterMapFile(int32_t deviceId) const { | 
 | 511 |     AutoMutex _l(mLock); | 
 | 512 |     Device* device = getDeviceLocked(deviceId); | 
 | 513 |     if (device) { | 
 | 514 |         return device->keyMap.keyCharacterMapFile; | 
 | 515 |     } | 
 | 516 |     return String8(); | 
 | 517 | } | 
 | 518 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 519 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { | 
 | 520 |     if (deviceId == 0) { | 
 | 521 |         deviceId = mBuiltInKeyboardId; | 
 | 522 |     } | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 523 |     ssize_t index = mDevices.indexOfKey(deviceId); | 
 | 524 |     return index >= 0 ? mDevices.valueAt(index) : NULL; | 
 | 525 | } | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 526 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 527 | EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const { | 
 | 528 |     for (size_t i = 0; i < mDevices.size(); i++) { | 
 | 529 |         Device* device = mDevices.valueAt(i); | 
 | 530 |         if (device->path == devicePath) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 531 |             return device; | 
 | 532 |         } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 |     } | 
 | 534 |     return NULL; | 
 | 535 | } | 
 | 536 |  | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 537 | size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) { | 
| Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 538 |     LOG_ASSERT(bufferSize >= 1); | 
| Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 539 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 540 |     AutoMutex _l(mLock); | 
| Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 541 |  | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 542 |     struct input_event readBuffer[bufferSize]; | 
 | 543 |  | 
 | 544 |     RawEvent* event = buffer; | 
 | 545 |     size_t capacity = bufferSize; | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 546 |     bool awoken = false; | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 547 |     for (;;) { | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 548 |         nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 549 |  | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 550 |         // Reopen input devices if needed. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 551 |         if (mNeedToReopenDevices) { | 
 | 552 |             mNeedToReopenDevices = false; | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 553 |  | 
 | 554 |             LOGI("Reopening all input devices due to a configuration change."); | 
 | 555 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 556 |             closeAllDevicesLocked(); | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 557 |             mNeedToScanDevices = true; | 
 | 558 |             break; // return to the caller before we actually rescan | 
 | 559 |         } | 
 | 560 |  | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 561 |         // Report any devices that had last been added/removed. | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 562 |         while (mClosingDevices) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 563 |             Device* device = mClosingDevices; | 
 | 564 |             LOGV("Reporting device closed: id=%d, name=%s\n", | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 |                  device->id, device->path.string()); | 
 | 566 |             mClosingDevices = device->next; | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 567 |             event->when = now; | 
 | 568 |             event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; | 
 | 569 |             event->type = DEVICE_REMOVED; | 
 | 570 |             event += 1; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 |             delete device; | 
| Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 572 |             mNeedToSendFinishedDeviceScan = true; | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 573 |             if (--capacity == 0) { | 
 | 574 |                 break; | 
 | 575 |             } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 |         } | 
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 577 |  | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 578 |         if (mNeedToScanDevices) { | 
 | 579 |             mNeedToScanDevices = false; | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 580 |             scanDevicesLocked(); | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 581 |             mNeedToSendFinishedDeviceScan = true; | 
 | 582 |         } | 
 | 583 |  | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 584 |         while (mOpeningDevices != NULL) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 585 |             Device* device = mOpeningDevices; | 
 | 586 |             LOGV("Reporting device opened: id=%d, name=%s\n", | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 |                  device->id, device->path.string()); | 
 | 588 |             mOpeningDevices = device->next; | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 589 |             event->when = now; | 
 | 590 |             event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; | 
 | 591 |             event->type = DEVICE_ADDED; | 
 | 592 |             event += 1; | 
| Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 593 |             mNeedToSendFinishedDeviceScan = true; | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 594 |             if (--capacity == 0) { | 
 | 595 |                 break; | 
 | 596 |             } | 
| Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 597 |         } | 
 | 598 |  | 
 | 599 |         if (mNeedToSendFinishedDeviceScan) { | 
 | 600 |             mNeedToSendFinishedDeviceScan = false; | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 601 |             event->when = now; | 
 | 602 |             event->type = FINISHED_DEVICE_SCAN; | 
 | 603 |             event += 1; | 
 | 604 |             if (--capacity == 0) { | 
 | 605 |                 break; | 
 | 606 |             } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 |         } | 
 | 608 |  | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 609 |         // Grab the next input event. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 610 |         bool deviceChanged = false; | 
 | 611 |         while (mPendingEventIndex < mPendingEventCount) { | 
 | 612 |             const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++]; | 
 | 613 |             if (eventItem.data.u32 == EPOLL_ID_INOTIFY) { | 
 | 614 |                 if (eventItem.events & EPOLLIN) { | 
 | 615 |                     mPendingINotify = true; | 
 | 616 |                 } else { | 
 | 617 |                     LOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events); | 
 | 618 |                 } | 
 | 619 |                 continue; | 
 | 620 |             } | 
 | 621 |  | 
 | 622 |             if (eventItem.data.u32 == EPOLL_ID_WAKE) { | 
 | 623 |                 if (eventItem.events & EPOLLIN) { | 
 | 624 |                     LOGV("awoken after wake()"); | 
 | 625 |                     awoken = true; | 
 | 626 |                     char buffer[16]; | 
 | 627 |                     ssize_t nRead; | 
 | 628 |                     do { | 
 | 629 |                         nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); | 
 | 630 |                     } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); | 
 | 631 |                 } else { | 
 | 632 |                     LOGW("Received unexpected epoll event 0x%08x for wake read pipe.", | 
 | 633 |                             eventItem.events); | 
 | 634 |                 } | 
 | 635 |                 continue; | 
 | 636 |             } | 
 | 637 |  | 
 | 638 |             ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32); | 
 | 639 |             if (deviceIndex < 0) { | 
 | 640 |                 LOGW("Received unexpected epoll event 0x%08x for unknown device id %d.", | 
 | 641 |                         eventItem.events, eventItem.data.u32); | 
 | 642 |                 continue; | 
 | 643 |             } | 
 | 644 |  | 
 | 645 |             Device* device = mDevices.valueAt(deviceIndex); | 
 | 646 |             if (eventItem.events & EPOLLIN) { | 
 | 647 |                 int32_t readSize = read(device->fd, readBuffer, | 
 | 648 |                         sizeof(struct input_event) * capacity); | 
 | 649 |                 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) { | 
 | 650 |                     // Device was removed before INotify noticed. | 
| Dima Zavin | e8b7f4a | 2011-09-26 23:31:27 -0700 | [diff] [blame] | 651 |                     LOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d capacity: %d errno: %d)\n", | 
 | 652 |                          device->fd, readSize, bufferSize, capacity, errno); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 653 |                     deviceChanged = true; | 
 | 654 |                     closeDeviceLocked(device); | 
 | 655 |                 } else if (readSize < 0) { | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 656 |                     if (errno != EAGAIN && errno != EINTR) { | 
 | 657 |                         LOGW("could not get event (errno=%d)", errno); | 
 | 658 |                     } | 
 | 659 |                 } else if ((readSize % sizeof(struct input_event)) != 0) { | 
 | 660 |                     LOGE("could not get event (wrong size: %d)", readSize); | 
 | 661 |                 } else { | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 662 |                     int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; | 
 | 663 |  | 
 | 664 |                     size_t count = size_t(readSize) / sizeof(struct input_event); | 
 | 665 |                     for (size_t i = 0; i < count; i++) { | 
 | 666 |                         const struct input_event& iev = readBuffer[i]; | 
 | 667 |                         LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d", | 
 | 668 |                                 device->path.string(), | 
 | 669 |                                 (int) iev.time.tv_sec, (int) iev.time.tv_usec, | 
 | 670 |                                 iev.type, iev.code, iev.value); | 
 | 671 |  | 
| Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 672 | #ifdef HAVE_POSIX_CLOCKS | 
 | 673 |                         // Use the time specified in the event instead of the current time | 
 | 674 |                         // so that downstream code can get more accurate estimates of | 
 | 675 |                         // event dispatch latency from the time the event is enqueued onto | 
 | 676 |                         // the evdev client buffer. | 
 | 677 |                         // | 
 | 678 |                         // The event's timestamp fortuitously uses the same monotonic clock | 
 | 679 |                         // time base as the rest of Android.  The kernel event device driver | 
 | 680 |                         // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts(). | 
 | 681 |                         // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere | 
 | 682 |                         // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a | 
 | 683 |                         // system call that also queries ktime_get_ts(). | 
 | 684 |                         event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL | 
 | 685 |                                 + nsecs_t(iev.time.tv_usec) * 1000LL; | 
 | 686 |                         LOGV("event time %lld, now %lld", event->when, now); | 
 | 687 | #else | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 688 |                         event->when = now; | 
| Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 689 | #endif | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 690 |                         event->deviceId = deviceId; | 
 | 691 |                         event->type = iev.type; | 
 | 692 |                         event->scanCode = iev.code; | 
 | 693 |                         event->value = iev.value; | 
 | 694 |                         event->keyCode = AKEYCODE_UNKNOWN; | 
 | 695 |                         event->flags = 0; | 
 | 696 |                         if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) { | 
 | 697 |                             status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code, | 
 | 698 |                                         &event->keyCode, &event->flags); | 
 | 699 |                             LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n", | 
 | 700 |                                     iev.code, event->keyCode, event->flags, err); | 
 | 701 |                         } | 
 | 702 |                         event += 1; | 
 | 703 |                     } | 
 | 704 |                     capacity -= count; | 
 | 705 |                     if (capacity == 0) { | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 706 |                         // The result buffer is full.  Reset the pending event index | 
 | 707 |                         // so we will try to read the device again on the next iteration. | 
 | 708 |                         mPendingEventIndex -= 1; | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 709 |                         break; | 
 | 710 |                     } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 |                 } | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 712 |             } else { | 
 | 713 |                 LOGW("Received unexpected epoll event 0x%08x for device %s.", | 
 | 714 |                         eventItem.events, device->identifier.name.string()); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 |             } | 
 | 716 |         } | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 717 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 718 |         // readNotify() will modify the list of devices so this must be done after | 
 | 719 |         // processing all other events to ensure that we read all remaining events | 
 | 720 |         // before closing the devices. | 
 | 721 |         if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) { | 
 | 722 |             mPendingINotify = false; | 
 | 723 |             readNotifyLocked(); | 
 | 724 |             deviceChanged = true; | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 725 |         } | 
 | 726 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 727 |         // Report added or removed devices immediately. | 
 | 728 |         if (deviceChanged) { | 
 | 729 |             continue; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 |         } | 
| Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 731 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 732 |         // Return now if we have collected any events or if we were explicitly awoken. | 
 | 733 |         if (event != buffer || awoken) { | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 734 |             break; | 
 | 735 |         } | 
 | 736 |  | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 737 |         // Poll for events.  Mind the wake lock dance! | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 738 |         // We hold a wake lock at all times except during epoll_wait().  This works due to some | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 739 |         // subtle choreography.  When a device driver has pending (unread) events, it acquires | 
 | 740 |         // a kernel wake lock.  However, once the last pending event has been read, the device | 
 | 741 |         // driver will release the kernel wake lock.  To prevent the system from going to sleep | 
 | 742 |         // when this happens, the EventHub holds onto its own user wake lock while the client | 
 | 743 |         // is processing events.  Thus the system can only sleep if there are no events | 
 | 744 |         // pending or currently being processed. | 
| Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 745 |         // | 
 | 746 |         // The timeout is advisory only.  If the device is asleep, it will not wake just to | 
 | 747 |         // service the timeout. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 748 |         mPendingEventIndex = 0; | 
 | 749 |  | 
 | 750 |         mLock.unlock(); // release lock before poll, must be before release_wake_lock | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 751 |         release_wake_lock(WAKE_LOCK_ID); | 
 | 752 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 753 |         int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis); | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 754 |  | 
 | 755 |         acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 756 |         mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 757 |  | 
| Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 758 |         if (pollResult == 0) { | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 759 |             // Timed out. | 
 | 760 |             mPendingEventCount = 0; | 
 | 761 |             break; | 
| Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 762 |         } | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 763 |  | 
| Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 764 |         if (pollResult < 0) { | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 765 |             // An error occurred. | 
 | 766 |             mPendingEventCount = 0; | 
 | 767 |  | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 768 |             // Sleep after errors to avoid locking up the system. | 
 | 769 |             // Hopefully the error is transient. | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 770 |             if (errno != EINTR) { | 
| Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 771 |                 LOGW("poll failed (errno=%d)\n", errno); | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 772 |                 usleep(100000); | 
 | 773 |             } | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 774 |         } else { | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 775 |             // Some events occurred. | 
 | 776 |             mPendingEventCount = size_t(pollResult); | 
 | 777 |  | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 778 |             // On an SMP system, it is possible for the framework to read input events | 
 | 779 |             // faster than the kernel input device driver can produce a complete packet. | 
 | 780 |             // Because poll() wakes up as soon as the first input event becomes available, | 
 | 781 |             // the framework will often end up reading one event at a time until the | 
 | 782 |             // packet is complete.  Instead of one call to read() returning 71 events, | 
 | 783 |             // it could take 71 calls to read() each returning 1 event. | 
 | 784 |             // | 
 | 785 |             // Sleep for a short period of time after waking up from the poll() to give | 
 | 786 |             // the kernel time to finish writing the entire packet of input events. | 
 | 787 |             if (mNumCpus > 1) { | 
 | 788 |                 usleep(250); | 
 | 789 |             } | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 790 |         } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 |     } | 
| Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 792 |  | 
 | 793 |     // All done, return the number of events we read. | 
 | 794 |     return event - buffer; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | } | 
 | 796 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 797 | void EventHub::wake() { | 
 | 798 |     LOGV("wake() called"); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 800 |     ssize_t nWrite; | 
 | 801 |     do { | 
 | 802 |         nWrite = write(mWakeWritePipeFd, "W", 1); | 
 | 803 |     } while (nWrite == -1 && errno == EINTR); | 
 | 804 |  | 
 | 805 |     if (nWrite != 1 && errno != EAGAIN) { | 
 | 806 |         LOGW("Could not write wake signal, errno=%d", errno); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 |     } | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 808 | } | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 809 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 810 | void EventHub::scanDevicesLocked() { | 
 | 811 |     status_t res = scanDirLocked(DEVICE_PATH); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 |     if(res < 0) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 813 |         LOGE("scan dir failed for %s\n", DEVICE_PATH); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 |     } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | } | 
 | 816 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | // ---------------------------------------------------------------------------- | 
 | 818 |  | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 819 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { | 
 | 820 |     const uint8_t* end = array + endIndex; | 
 | 821 |     array += startIndex; | 
 | 822 |     while (array != end) { | 
 | 823 |         if (*(array++) != 0) { | 
 | 824 |             return true; | 
 | 825 |         } | 
 | 826 |     } | 
 | 827 |     return false; | 
 | 828 | } | 
 | 829 |  | 
 | 830 | static const int32_t GAMEPAD_KEYCODES[] = { | 
 | 831 |         AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, | 
 | 832 |         AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, | 
 | 833 |         AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, | 
 | 834 |         AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, | 
 | 835 |         AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, | 
| Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 836 |         AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, | 
 | 837 |         AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4, | 
 | 838 |         AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8, | 
 | 839 |         AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12, | 
 | 840 |         AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16, | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 841 | }; | 
 | 842 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 843 | status_t EventHub::openDeviceLocked(const char *devicePath) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 844 |     char buffer[80]; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 846 |     LOGV("Opening device: %s", devicePath); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 848 |     int fd = open(devicePath, O_RDWR); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 |     if(fd < 0) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 850 |         LOGE("could not open %s, %s\n", devicePath, strerror(errno)); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 |         return -1; | 
 | 852 |     } | 
 | 853 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 854 |     InputDeviceIdentifier identifier; | 
 | 855 |  | 
 | 856 |     // Get device name. | 
 | 857 |     if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { | 
 | 858 |         //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno)); | 
 | 859 |     } else { | 
 | 860 |         buffer[sizeof(buffer) - 1] = '\0'; | 
 | 861 |         identifier.name.setTo(buffer); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 |     } | 
| Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 863 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 864 |     // Check to see if the device is on our excluded list | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 865 |     for (size_t i = 0; i < mExcludedDevices.size(); i++) { | 
 | 866 |         const String8& item = mExcludedDevices.itemAt(i); | 
 | 867 |         if (identifier.name == item) { | 
 | 868 |             LOGI("ignoring event id %s driver %s\n", devicePath, item.string()); | 
| Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 869 |             close(fd); | 
| Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 870 |             return -1; | 
 | 871 |         } | 
 | 872 |     } | 
 | 873 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 874 |     // Get device driver version. | 
 | 875 |     int driverVersion; | 
 | 876 |     if(ioctl(fd, EVIOCGVERSION, &driverVersion)) { | 
 | 877 |         LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno)); | 
 | 878 |         close(fd); | 
 | 879 |         return -1; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 |     } | 
 | 881 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 882 |     // Get device identifier. | 
 | 883 |     struct input_id inputId; | 
 | 884 |     if(ioctl(fd, EVIOCGID, &inputId)) { | 
 | 885 |         LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno)); | 
 | 886 |         close(fd); | 
 | 887 |         return -1; | 
 | 888 |     } | 
 | 889 |     identifier.bus = inputId.bustype; | 
 | 890 |     identifier.product = inputId.product; | 
 | 891 |     identifier.vendor = inputId.vendor; | 
 | 892 |     identifier.version = inputId.version; | 
 | 893 |  | 
 | 894 |     // Get device physical location. | 
 | 895 |     if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { | 
 | 896 |         //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); | 
 | 897 |     } else { | 
 | 898 |         buffer[sizeof(buffer) - 1] = '\0'; | 
 | 899 |         identifier.location.setTo(buffer); | 
 | 900 |     } | 
 | 901 |  | 
 | 902 |     // Get device unique id. | 
 | 903 |     if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { | 
 | 904 |         //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); | 
 | 905 |     } else { | 
 | 906 |         buffer[sizeof(buffer) - 1] = '\0'; | 
 | 907 |         identifier.uniqueId.setTo(buffer); | 
 | 908 |     } | 
 | 909 |  | 
 | 910 |     // Make file descriptor non-blocking for use with poll(). | 
| Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 911 |     if (fcntl(fd, F_SETFL, O_NONBLOCK)) { | 
 | 912 |         LOGE("Error %d making device file descriptor non-blocking.", errno); | 
 | 913 |         close(fd); | 
 | 914 |         return -1; | 
 | 915 |     } | 
 | 916 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 917 |     // Allocate device.  (The device object takes ownership of the fd at this point.) | 
 | 918 |     int32_t deviceId = mNextDeviceId++; | 
 | 919 |     Device* device = new Device(fd, deviceId, String8(devicePath), identifier); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 |  | 
 | 921 | #if 0 | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 922 |     LOGI("add device %d: %s\n", deviceId, devicePath); | 
 | 923 |     LOGI("  bus:       %04x\n" | 
 | 924 |          "  vendor     %04x\n" | 
 | 925 |          "  product    %04x\n" | 
 | 926 |          "  version    %04x\n", | 
 | 927 |         identifier.bus, identifier.vendor, identifier.product, identifier.version); | 
 | 928 |     LOGI("  name:      \"%s\"\n", identifier.name.string()); | 
 | 929 |     LOGI("  location:  \"%s\"\n", identifier.location.string()); | 
 | 930 |     LOGI("  unique id: \"%s\"\n", identifier.uniqueId.string()); | 
 | 931 |     LOGI("  driver:    v%d.%d.%d\n", | 
 | 932 |         driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | #endif | 
 | 934 |  | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 935 |     // Load the configuration file for the device. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 936 |     loadConfigurationLocked(device); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 937 |  | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 938 |     // Figure out the kinds of events the device reports. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 939 |     ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask); | 
 | 940 |     ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask); | 
 | 941 |     ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask); | 
 | 942 |     ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask); | 
 | 943 |     ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask); | 
 | 944 |     ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask); | 
| Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 945 |  | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 946 |     // See if this is a keyboard.  Ignore everything in the button range except for | 
 | 947 |     // joystick and gamepad buttons which are handled like keyboards for the most part. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 948 |     bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC)) | 
 | 949 |             || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK), | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 950 |                     sizeof_bit_array(KEY_MAX + 1)); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 951 |     bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC), | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 952 |                     sizeof_bit_array(BTN_MOUSE)) | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 953 |             || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK), | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 954 |                     sizeof_bit_array(BTN_DIGI)); | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 955 |     if (haveKeyboardKeys || haveGamepadButtons) { | 
 | 956 |         device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 |     } | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 958 |  | 
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 959 |     // See if this is a cursor device such as a trackball or mouse. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 960 |     if (test_bit(BTN_MOUSE, device->keyBitmask) | 
 | 961 |             && test_bit(REL_X, device->relBitmask) | 
 | 962 |             && test_bit(REL_Y, device->relBitmask)) { | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 963 |         device->classes |= INPUT_DEVICE_CLASS_CURSOR; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 |     } | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 965 |  | 
 | 966 |     // See if this is a touch pad. | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 967 |     // Is this a new modern multi-touch driver? | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 968 |     if (test_bit(ABS_MT_POSITION_X, device->absBitmask) | 
 | 969 |             && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) { | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 970 |         // Some joysticks such as the PS3 controller report axes that conflict | 
 | 971 |         // with the ABS_MT range.  Try to confirm that the device really is | 
 | 972 |         // a touch screen. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 973 |         if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) { | 
| Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 974 |             device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT; | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 975 |         } | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 976 |     // Is this an old style single-touch driver? | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 977 |     } else if (test_bit(BTN_TOUCH, device->keyBitmask) | 
 | 978 |             && test_bit(ABS_X, device->absBitmask) | 
 | 979 |             && test_bit(ABS_Y, device->absBitmask)) { | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 980 |         device->classes |= INPUT_DEVICE_CLASS_TOUCH; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 |     } | 
 | 982 |  | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 983 |     // See if this device is a joystick. | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 984 |     // Assumes that joysticks always have gamepad buttons in order to distinguish them | 
 | 985 |     // from other devices such as accelerometers that also have absolute axes. | 
| Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 986 |     if (haveGamepadButtons) { | 
 | 987 |         uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK; | 
 | 988 |         for (int i = 0; i <= ABS_MAX; i++) { | 
 | 989 |             if (test_bit(i, device->absBitmask) | 
 | 990 |                     && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) { | 
 | 991 |                 device->classes = assumedClasses; | 
 | 992 |                 break; | 
 | 993 |             } | 
 | 994 |         } | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 995 |     } | 
 | 996 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 997 |     // Check whether this device has switches. | 
 | 998 |     for (int i = 0; i <= SW_MAX; i++) { | 
 | 999 |         if (test_bit(i, device->swBitmask)) { | 
 | 1000 |             device->classes |= INPUT_DEVICE_CLASS_SWITCH; | 
 | 1001 |             break; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 |         } | 
 | 1003 |     } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1005 |     // Configure virtual keys. | 
| Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 1006 |     if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1007 |         // Load the virtual keys for the touch screen, if any. | 
 | 1008 |         // We do this now so that we can make sure to load the keymap if necessary. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1009 |         status_t status = loadVirtualKeyMapLocked(device); | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1010 |         if (!status) { | 
 | 1011 |             device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 |         } | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1013 |     } | 
 | 1014 |  | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1015 |     // Load the key map. | 
 | 1016 |     // We need to do this for joysticks too because the key layout may specify axes. | 
 | 1017 |     status_t keyMapStatus = NAME_NOT_FOUND; | 
 | 1018 |     if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1019 |         // Load the keymap for the device. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1020 |         keyMapStatus = loadKeyMapLocked(device); | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1021 |     } | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1022 |  | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1023 |     // Configure the keyboard, gamepad or virtual keyboard. | 
 | 1024 |     if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1025 |         // Register the keyboard as a built-in keyboard if it is eligible. | 
| Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1026 |         if (!keyMapStatus | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1027 |                 && mBuiltInKeyboardId == -1 | 
 | 1028 |                 && isEligibleBuiltInKeyboard(device->identifier, | 
 | 1029 |                         device->configuration, &device->keyMap)) { | 
 | 1030 |             mBuiltInKeyboardId = device->id; | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1031 |         } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 |  | 
| Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1033 |         // 'Q' key support = cheap test of whether this is an alpha-capable kbd | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1034 |         if (hasKeycodeLocked(device, AKEYCODE_Q)) { | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1035 |             device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; | 
| Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1036 |         } | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1037 |  | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1038 |         // See if this device has a DPAD. | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1039 |         if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) && | 
 | 1040 |                 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) && | 
 | 1041 |                 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) && | 
 | 1042 |                 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) && | 
 | 1043 |                 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) { | 
| Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1044 |             device->classes |= INPUT_DEVICE_CLASS_DPAD; | 
| Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1045 |         } | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1046 |  | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1047 |         // See if this device has a gamepad. | 
| Kenny Root | 1d79a9d | 2010-10-21 15:46:03 -0700 | [diff] [blame] | 1048 |         for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) { | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1049 |             if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) { | 
| Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1050 |                 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; | 
 | 1051 |                 break; | 
 | 1052 |             } | 
 | 1053 |         } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 |     } | 
 | 1055 |  | 
| Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 1056 |     // If the device isn't recognized as something we handle, don't monitor it. | 
 | 1057 |     if (device->classes == 0) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1058 |         LOGV("Dropping device: id=%d, path='%s', name='%s'", | 
 | 1059 |                 deviceId, devicePath, device->identifier.name.string()); | 
| Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 1060 |         delete device; | 
 | 1061 |         return -1; | 
 | 1062 |     } | 
 | 1063 |  | 
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1064 |     // Determine whether the device is external or internal. | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1065 |     if (isExternalDeviceLocked(device)) { | 
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1066 |         device->classes |= INPUT_DEVICE_CLASS_EXTERNAL; | 
 | 1067 |     } | 
 | 1068 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1069 |     // Register with epoll. | 
 | 1070 |     struct epoll_event eventItem; | 
 | 1071 |     memset(&eventItem, 0, sizeof(eventItem)); | 
 | 1072 |     eventItem.events = EPOLLIN; | 
 | 1073 |     eventItem.data.u32 = deviceId; | 
 | 1074 |     if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) { | 
 | 1075 |         LOGE("Could not add device fd to epoll instance.  errno=%d", errno); | 
 | 1076 |         delete device; | 
 | 1077 |         return -1; | 
 | 1078 |     } | 
 | 1079 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1080 |     LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, " | 
 | 1081 |             "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s", | 
 | 1082 |          deviceId, fd, devicePath, device->identifier.name.string(), | 
 | 1083 |          device->classes, | 
 | 1084 |          device->configurationFile.string(), | 
 | 1085 |          device->keyMap.keyLayoutFile.string(), | 
 | 1086 |          device->keyMap.keyCharacterMapFile.string(), | 
 | 1087 |          toString(mBuiltInKeyboardId == deviceId)); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1088 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1089 |     mDevices.add(deviceId, device); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 |     device->next = mOpeningDevices; | 
 | 1092 |     mOpeningDevices = device; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 |     return 0; | 
 | 1094 | } | 
 | 1095 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1096 | void EventHub::loadConfigurationLocked(Device* device) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1097 |     device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier( | 
 | 1098 |             device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1099 |     if (device->configurationFile.isEmpty()) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1100 |         LOGD("No input device configuration file found for device '%s'.", | 
 | 1101 |                 device->identifier.name.string()); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1102 |     } else { | 
 | 1103 |         status_t status = PropertyMap::load(device->configurationFile, | 
 | 1104 |                 &device->configuration); | 
 | 1105 |         if (status) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1106 |             LOGE("Error loading input device configuration file for device '%s'.  " | 
 | 1107 |                     "Using default configuration.", | 
 | 1108 |                     device->identifier.name.string()); | 
| Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1109 |         } | 
 | 1110 |     } | 
 | 1111 | } | 
 | 1112 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1113 | status_t EventHub::loadVirtualKeyMapLocked(Device* device) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1114 |     // The virtual key map is supplied by the kernel as a system board property file. | 
 | 1115 |     String8 path; | 
 | 1116 |     path.append("/sys/board_properties/virtualkeys."); | 
 | 1117 |     path.append(device->identifier.name); | 
 | 1118 |     if (access(path.string(), R_OK)) { | 
 | 1119 |         return NAME_NOT_FOUND; | 
 | 1120 |     } | 
 | 1121 |     return VirtualKeyMap::load(path, &device->virtualKeyMap); | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1122 | } | 
 | 1123 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1124 | status_t EventHub::loadKeyMapLocked(Device* device) { | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1125 |     return device->keyMap.load(device->identifier, device->configuration); | 
| Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1126 | } | 
 | 1127 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1128 | bool EventHub::isExternalDeviceLocked(Device* device) { | 
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1129 |     if (device->configuration) { | 
 | 1130 |         bool value; | 
| Max Braun | e81056f | 2011-08-30 14:35:45 -0700 | [diff] [blame] | 1131 |         if (device->configuration->tryGetProperty(String8("device.internal"), value)) { | 
 | 1132 |             return !value; | 
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1133 |         } | 
 | 1134 |     } | 
 | 1135 |     return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH; | 
 | 1136 | } | 
 | 1137 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1138 | bool EventHub::hasKeycodeLocked(Device* device, int keycode) const { | 
 | 1139 |     if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { | 
| Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1140 |         return false; | 
 | 1141 |     } | 
 | 1142 |      | 
 | 1143 |     Vector<int32_t> scanCodes; | 
| Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1144 |     device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes); | 
| Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1145 |     const size_t N = scanCodes.size(); | 
 | 1146 |     for (size_t i=0; i<N && i<=KEY_MAX; i++) { | 
 | 1147 |         int32_t sc = scanCodes.itemAt(i); | 
 | 1148 |         if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { | 
 | 1149 |             return true; | 
 | 1150 |         } | 
 | 1151 |     } | 
 | 1152 |      | 
 | 1153 |     return false; | 
 | 1154 | } | 
 | 1155 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1156 | status_t EventHub::closeDeviceByPathLocked(const char *devicePath) { | 
 | 1157 |     Device* device = getDeviceByPathLocked(devicePath); | 
 | 1158 |     if (device) { | 
 | 1159 |         closeDeviceLocked(device); | 
 | 1160 |         return 0; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 |     } | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1162 |     LOGV("Remove device: %s not found, device may already have been removed.", devicePath); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 |     return -1; | 
 | 1164 | } | 
 | 1165 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1166 | void EventHub::closeAllDevicesLocked() { | 
 | 1167 |     while (mDevices.size() > 0) { | 
 | 1168 |         closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1)); | 
 | 1169 |     } | 
 | 1170 | } | 
 | 1171 |  | 
 | 1172 | void EventHub::closeDeviceLocked(Device* device) { | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1173 |     LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n", | 
 | 1174 |          device->path.string(), device->identifier.name.string(), device->id, | 
 | 1175 |          device->fd, device->classes); | 
 | 1176 |  | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1177 |     if (device->id == mBuiltInKeyboardId) { | 
 | 1178 |         LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", | 
 | 1179 |                 device->path.string(), mBuiltInKeyboardId); | 
 | 1180 |         mBuiltInKeyboardId = -1; | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1181 |     } | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1182 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1183 |     if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) { | 
 | 1184 |         LOGW("Could not remove device fd from epoll instance.  errno=%d", errno); | 
 | 1185 |     } | 
 | 1186 |  | 
 | 1187 |     mDevices.removeItem(device->id); | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1188 |     device->close(); | 
 | 1189 |  | 
| Jeff Brown | 8e9d443 | 2011-03-12 19:46:59 -0800 | [diff] [blame] | 1190 |     // Unlink for opening devices list if it is present. | 
 | 1191 |     Device* pred = NULL; | 
 | 1192 |     bool found = false; | 
 | 1193 |     for (Device* entry = mOpeningDevices; entry != NULL; ) { | 
 | 1194 |         if (entry == device) { | 
 | 1195 |             found = true; | 
 | 1196 |             break; | 
 | 1197 |         } | 
 | 1198 |         pred = entry; | 
 | 1199 |         entry = entry->next; | 
 | 1200 |     } | 
 | 1201 |     if (found) { | 
 | 1202 |         // Unlink the device from the opening devices list then delete it. | 
 | 1203 |         // We don't need to tell the client that the device was closed because | 
 | 1204 |         // it does not even know it was opened in the first place. | 
 | 1205 |         LOGI("Device %s was immediately closed after opening.", device->path.string()); | 
 | 1206 |         if (pred) { | 
 | 1207 |             pred->next = device->next; | 
 | 1208 |         } else { | 
 | 1209 |             mOpeningDevices = device->next; | 
 | 1210 |         } | 
 | 1211 |         delete device; | 
 | 1212 |     } else { | 
 | 1213 |         // Link into closing devices list. | 
 | 1214 |         // The device will be deleted later after we have informed the client. | 
 | 1215 |         device->next = mClosingDevices; | 
 | 1216 |         mClosingDevices = device; | 
 | 1217 |     } | 
| Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1218 | } | 
 | 1219 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1220 | status_t EventHub::readNotifyLocked() { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 |     int res; | 
 | 1222 |     char devname[PATH_MAX]; | 
 | 1223 |     char *filename; | 
 | 1224 |     char event_buf[512]; | 
 | 1225 |     int event_size; | 
 | 1226 |     int event_pos = 0; | 
 | 1227 |     struct inotify_event *event; | 
 | 1228 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1229 |     LOGV("EventHub::readNotify nfd: %d\n", mINotifyFd); | 
 | 1230 |     res = read(mINotifyFd, event_buf, sizeof(event_buf)); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1231 |     if(res < (int)sizeof(*event)) { | 
 | 1232 |         if(errno == EINTR) | 
 | 1233 |             return 0; | 
 | 1234 |         LOGW("could not get event, %s\n", strerror(errno)); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1235 |         return -1; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 |     } | 
 | 1237 |     //printf("got %d bytes of event information\n", res); | 
 | 1238 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1239 |     strcpy(devname, DEVICE_PATH); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 |     filename = devname + strlen(devname); | 
 | 1241 |     *filename++ = '/'; | 
 | 1242 |  | 
 | 1243 |     while(res >= (int)sizeof(*event)) { | 
 | 1244 |         event = (struct inotify_event *)(event_buf + event_pos); | 
 | 1245 |         //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); | 
 | 1246 |         if(event->len) { | 
 | 1247 |             strcpy(filename, event->name); | 
 | 1248 |             if(event->mask & IN_CREATE) { | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1249 |                 openDeviceLocked(devname); | 
 | 1250 |             } else { | 
| Dima Zavin | e8b7f4a | 2011-09-26 23:31:27 -0700 | [diff] [blame] | 1251 |                 LOGI("Removing device '%s' due to inotify event\n", devname); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1252 |                 closeDeviceByPathLocked(devname); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 |             } | 
 | 1254 |         } | 
 | 1255 |         event_size = sizeof(*event) + event->len; | 
 | 1256 |         res -= event_size; | 
 | 1257 |         event_pos += event_size; | 
 | 1258 |     } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 |     return 0; | 
 | 1260 | } | 
 | 1261 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1262 | status_t EventHub::scanDirLocked(const char *dirname) | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | { | 
 | 1264 |     char devname[PATH_MAX]; | 
 | 1265 |     char *filename; | 
 | 1266 |     DIR *dir; | 
 | 1267 |     struct dirent *de; | 
 | 1268 |     dir = opendir(dirname); | 
 | 1269 |     if(dir == NULL) | 
 | 1270 |         return -1; | 
 | 1271 |     strcpy(devname, dirname); | 
 | 1272 |     filename = devname + strlen(devname); | 
 | 1273 |     *filename++ = '/'; | 
 | 1274 |     while((de = readdir(dir))) { | 
 | 1275 |         if(de->d_name[0] == '.' && | 
 | 1276 |            (de->d_name[1] == '\0' || | 
 | 1277 |             (de->d_name[1] == '.' && de->d_name[2] == '\0'))) | 
 | 1278 |             continue; | 
 | 1279 |         strcpy(filename, de->d_name); | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1280 |         openDeviceLocked(devname); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 |     } | 
 | 1282 |     closedir(dir); | 
 | 1283 |     return 0; | 
 | 1284 | } | 
 | 1285 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1286 | void EventHub::requestReopenDevices() { | 
 | 1287 |     LOGV("requestReopenDevices() called"); | 
 | 1288 |  | 
 | 1289 |     AutoMutex _l(mLock); | 
 | 1290 |     mNeedToReopenDevices = true; | 
| Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1291 | } | 
 | 1292 |  | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1293 | void EventHub::dump(String8& dump) { | 
 | 1294 |     dump.append("Event Hub State:\n"); | 
 | 1295 |  | 
 | 1296 |     { // acquire lock | 
 | 1297 |         AutoMutex _l(mLock); | 
 | 1298 |  | 
| Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1299 |         dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1300 |  | 
 | 1301 |         dump.append(INDENT "Devices:\n"); | 
 | 1302 |  | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1303 |         for (size_t i = 0; i < mDevices.size(); i++) { | 
 | 1304 |             const Device* device = mDevices.valueAt(i); | 
 | 1305 |             if (mBuiltInKeyboardId == device->id) { | 
 | 1306 |                 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", | 
 | 1307 |                         device->id, device->identifier.name.string()); | 
 | 1308 |             } else { | 
 | 1309 |                 dump.appendFormat(INDENT2 "%d: %s\n", device->id, | 
 | 1310 |                         device->identifier.name.string()); | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1311 |             } | 
| Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1312 |             dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes); | 
 | 1313 |             dump.appendFormat(INDENT3 "Path: %s\n", device->path.string()); | 
 | 1314 |             dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string()); | 
 | 1315 |             dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string()); | 
 | 1316 |             dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " | 
 | 1317 |                     "product=0x%04x, version=0x%04x\n", | 
 | 1318 |                     device->identifier.bus, device->identifier.vendor, | 
 | 1319 |                     device->identifier.product, device->identifier.version); | 
 | 1320 |             dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", | 
 | 1321 |                     device->keyMap.keyLayoutFile.string()); | 
 | 1322 |             dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n", | 
 | 1323 |                     device->keyMap.keyCharacterMapFile.string()); | 
 | 1324 |             dump.appendFormat(INDENT3 "ConfigurationFile: %s\n", | 
 | 1325 |                     device->configurationFile.string()); | 
| Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1326 |         } | 
 | 1327 |     } // release lock | 
 | 1328 | } | 
 | 1329 |  | 
| Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 1330 | void EventHub::monitor() { | 
 | 1331 |     // Acquire and release the lock to ensure that the event hub has not deadlocked. | 
 | 1332 |     mLock.lock(); | 
 | 1333 |     mLock.unlock(); | 
 | 1334 | } | 
 | 1335 |  | 
 | 1336 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | }; // namespace android |