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 | |
JP Abgrall | 25a465b | 2012-05-16 10:33:49 -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 | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 39 | #include <input/KeyLayoutMap.h> |
| 40 | #include <input/KeyCharacterMap.h> |
| 41 | #include <input/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> |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 51 | #include <sys/sha1.h> |
Michael Wright | 67774c7 | 2013-09-25 14:13:40 -0700 | [diff] [blame] | 52 | #include <sys/utsname.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 54 | /* this macro is used to tell if "bit" is set in "array" |
| 55 | * it selects a byte from the array, and does a boolean AND |
| 56 | * operation with a byte that only has the relevant bit set. |
| 57 | * eg. to check for the 12th bit, we do (array[1] & 1<<4) |
| 58 | */ |
| 59 | #define test_bit(bit, array) (array[bit/8] & (1<<(bit%8))) |
| 60 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 61 | /* this macro computes the number of bytes needed to represent a bit array of the specified size */ |
| 62 | #define sizeof_bit_array(bits) ((bits + 7) / 8) |
| 63 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 64 | #define INDENT " " |
| 65 | #define INDENT2 " " |
| 66 | #define INDENT3 " " |
| 67 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | namespace android { |
| 69 | |
| 70 | static const char *WAKE_LOCK_ID = "KeyEvents"; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 71 | static const char *DEVICE_PATH = "/dev/input"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
| 73 | /* return the larger integer */ |
| 74 | static inline int max(int v1, int v2) |
| 75 | { |
| 76 | return (v1 > v2) ? v1 : v2; |
| 77 | } |
| 78 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 79 | static inline const char* toString(bool value) { |
| 80 | return value ? "true" : "false"; |
| 81 | } |
| 82 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 83 | static String8 sha1(const String8& in) { |
| 84 | SHA1_CTX ctx; |
| 85 | SHA1Init(&ctx); |
| 86 | SHA1Update(&ctx, reinterpret_cast<const u_char*>(in.string()), in.size()); |
| 87 | u_char digest[SHA1_DIGEST_LENGTH]; |
| 88 | SHA1Final(digest, &ctx); |
| 89 | |
| 90 | String8 out; |
| 91 | for (size_t i = 0; i < SHA1_DIGEST_LENGTH; i++) { |
| 92 | out.appendFormat("%02x", digest[i]); |
| 93 | } |
| 94 | return out; |
| 95 | } |
| 96 | |
Michael Wright | 67774c7 | 2013-09-25 14:13:40 -0700 | [diff] [blame] | 97 | static void getLinuxRelease(int* major, int* minor) { |
| 98 | struct utsname info; |
| 99 | if (uname(&info) || sscanf(info.release, "%d.%d", major, minor) <= 0) { |
| 100 | *major = 0, *minor = 0; |
| 101 | ALOGE("Could not get linux version: %s", strerror(errno)); |
| 102 | } |
| 103 | } |
| 104 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 105 | static void setDescriptor(InputDeviceIdentifier& identifier) { |
| 106 | // Compute a device descriptor that uniquely identifies the device. |
| 107 | // The descriptor is assumed to be a stable identifier. Its value should not |
| 108 | // change between reboots, reconnections, firmware updates or new releases of Android. |
| 109 | // Ideally, we also want the descriptor to be short and relatively opaque. |
| 110 | String8 rawDescriptor; |
| 111 | rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor, identifier.product); |
| 112 | if (!identifier.uniqueId.isEmpty()) { |
| 113 | rawDescriptor.append("uniqueId:"); |
| 114 | rawDescriptor.append(identifier.uniqueId); |
| 115 | } if (identifier.vendor == 0 && identifier.product == 0) { |
| 116 | // If we don't know the vendor and product id, then the device is probably |
| 117 | // built-in so we need to rely on other information to uniquely identify |
| 118 | // the input device. Usually we try to avoid relying on the device name or |
| 119 | // location but for built-in input device, they are unlikely to ever change. |
| 120 | if (!identifier.name.isEmpty()) { |
| 121 | rawDescriptor.append("name:"); |
| 122 | rawDescriptor.append(identifier.name); |
| 123 | } else if (!identifier.location.isEmpty()) { |
| 124 | rawDescriptor.append("location:"); |
| 125 | rawDescriptor.append(identifier.location); |
| 126 | } |
| 127 | } |
| 128 | identifier.descriptor = sha1(rawDescriptor); |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 129 | ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(), |
| 130 | identifier.descriptor.string()); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 133 | // --- Global Functions --- |
| 134 | |
| 135 | uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) { |
| 136 | // Touch devices get dibs on touch-related axes. |
| 137 | if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) { |
| 138 | switch (axis) { |
| 139 | case ABS_X: |
| 140 | case ABS_Y: |
| 141 | case ABS_PRESSURE: |
| 142 | case ABS_TOOL_WIDTH: |
| 143 | case ABS_DISTANCE: |
| 144 | case ABS_TILT_X: |
| 145 | case ABS_TILT_Y: |
| 146 | case ABS_MT_SLOT: |
| 147 | case ABS_MT_TOUCH_MAJOR: |
| 148 | case ABS_MT_TOUCH_MINOR: |
| 149 | case ABS_MT_WIDTH_MAJOR: |
| 150 | case ABS_MT_WIDTH_MINOR: |
| 151 | case ABS_MT_ORIENTATION: |
| 152 | case ABS_MT_POSITION_X: |
| 153 | case ABS_MT_POSITION_Y: |
| 154 | case ABS_MT_TOOL_TYPE: |
| 155 | case ABS_MT_BLOB_ID: |
| 156 | case ABS_MT_TRACKING_ID: |
| 157 | case ABS_MT_PRESSURE: |
| 158 | case ABS_MT_DISTANCE: |
| 159 | return INPUT_DEVICE_CLASS_TOUCH; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Joystick devices get the rest. |
| 164 | return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK; |
| 165 | } |
| 166 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 167 | // --- EventHub::Device --- |
| 168 | |
| 169 | EventHub::Device::Device(int fd, int32_t id, const String8& path, |
| 170 | const InputDeviceIdentifier& identifier) : |
| 171 | next(NULL), |
| 172 | fd(fd), id(id), path(path), identifier(identifier), |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 173 | classes(0), configuration(NULL), virtualKeyMap(NULL), |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 174 | ffEffectPlaying(false), ffEffectId(-1), controllerNumber(0), |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 175 | timestampOverrideSec(0), timestampOverrideUsec(0) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 176 | memset(keyBitmask, 0, sizeof(keyBitmask)); |
| 177 | memset(absBitmask, 0, sizeof(absBitmask)); |
| 178 | memset(relBitmask, 0, sizeof(relBitmask)); |
| 179 | memset(swBitmask, 0, sizeof(swBitmask)); |
| 180 | memset(ledBitmask, 0, sizeof(ledBitmask)); |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 181 | memset(ffBitmask, 0, sizeof(ffBitmask)); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 182 | memset(propBitmask, 0, sizeof(propBitmask)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 185 | EventHub::Device::~Device() { |
| 186 | close(); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 187 | delete configuration; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 188 | delete virtualKeyMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 191 | void EventHub::Device::close() { |
| 192 | if (fd >= 0) { |
| 193 | ::close(fd); |
| 194 | fd = -1; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | // --- EventHub --- |
| 200 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 201 | const uint32_t EventHub::EPOLL_ID_INOTIFY; |
| 202 | const uint32_t EventHub::EPOLL_ID_WAKE; |
| 203 | const int EventHub::EPOLL_SIZE_HINT; |
| 204 | const int EventHub::EPOLL_MAX_EVENTS; |
| 205 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 206 | EventHub::EventHub(void) : |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 207 | mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(), |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 208 | mOpeningDevices(0), mClosingDevices(0), |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 209 | mNeedToSendFinishedDeviceScan(false), |
| 210 | mNeedToReopenDevices(false), mNeedToScanDevices(true), |
| 211 | mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 213 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 214 | mEpollFd = epoll_create(EPOLL_SIZE_HINT); |
| 215 | LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno); |
| 216 | |
| 217 | mINotifyFd = inotify_init(); |
| 218 | int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE); |
| 219 | LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d", |
| 220 | DEVICE_PATH, errno); |
| 221 | |
| 222 | struct epoll_event eventItem; |
| 223 | memset(&eventItem, 0, sizeof(eventItem)); |
| 224 | eventItem.events = EPOLLIN; |
| 225 | eventItem.data.u32 = EPOLL_ID_INOTIFY; |
| 226 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem); |
| 227 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno); |
| 228 | |
| 229 | int wakeFds[2]; |
| 230 | result = pipe(wakeFds); |
| 231 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno); |
| 232 | |
| 233 | mWakeReadPipeFd = wakeFds[0]; |
| 234 | mWakeWritePipeFd = wakeFds[1]; |
| 235 | |
| 236 | result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK); |
| 237 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d", |
| 238 | errno); |
| 239 | |
| 240 | result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK); |
| 241 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d", |
| 242 | errno); |
| 243 | |
| 244 | eventItem.data.u32 = EPOLL_ID_WAKE; |
| 245 | result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem); |
| 246 | LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d", |
| 247 | errno); |
Michael Wright | 67774c7 | 2013-09-25 14:13:40 -0700 | [diff] [blame] | 248 | |
| 249 | int major, minor; |
| 250 | getLinuxRelease(&major, &minor); |
| 251 | // EPOLLWAKEUP was introduced in kernel 3.5 |
| 252 | mUsingEpollWakeup = major > 3 || (major == 3 && minor >= 5); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 255 | EventHub::~EventHub(void) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 256 | closeAllDevicesLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 258 | while (mClosingDevices) { |
| 259 | Device* device = mClosingDevices; |
| 260 | mClosingDevices = device->next; |
| 261 | delete device; |
| 262 | } |
| 263 | |
| 264 | ::close(mEpollFd); |
| 265 | ::close(mINotifyFd); |
| 266 | ::close(mWakeReadPipeFd); |
| 267 | ::close(mWakeWritePipeFd); |
| 268 | |
| 269 | release_wake_lock(WAKE_LOCK_ID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 272 | InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 274 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 275 | if (device == NULL) return InputDeviceIdentifier(); |
| 276 | return device->identifier; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 279 | uint32_t EventHub::getDeviceClasses(int32_t deviceId) const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 281 | Device* device = getDeviceLocked(deviceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | if (device == NULL) return 0; |
| 283 | return device->classes; |
| 284 | } |
| 285 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 286 | int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const { |
| 287 | AutoMutex _l(mLock); |
| 288 | Device* device = getDeviceLocked(deviceId); |
| 289 | if (device == NULL) return 0; |
| 290 | return device->controllerNumber; |
| 291 | } |
| 292 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 293 | void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 294 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 295 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 296 | if (device && device->configuration) { |
| 297 | *outConfiguration = *device->configuration; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 298 | } else { |
| 299 | outConfiguration->clear(); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 303 | status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 304 | RawAbsoluteAxisInfo* outAxisInfo) const { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 305 | outAxisInfo->clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 306 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 307 | if (axis >= 0 && axis <= ABS_MAX) { |
| 308 | AutoMutex _l(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 310 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 311 | if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) { |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 312 | struct input_absinfo info; |
| 313 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 314 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 315 | axis, device->identifier.name.string(), device->fd, errno); |
| 316 | return -errno; |
| 317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 319 | if (info.minimum != info.maximum) { |
| 320 | outAxisInfo->valid = true; |
| 321 | outAxisInfo->minValue = info.minimum; |
| 322 | outAxisInfo->maxValue = info.maximum; |
| 323 | outAxisInfo->flat = info.flat; |
| 324 | outAxisInfo->fuzz = info.fuzz; |
| 325 | outAxisInfo->resolution = info.resolution; |
| 326 | } |
| 327 | return OK; |
| 328 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | } |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 330 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 333 | bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const { |
| 334 | if (axis >= 0 && axis <= REL_MAX) { |
| 335 | AutoMutex _l(mLock); |
| 336 | |
| 337 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 338 | if (device) { |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 339 | return test_bit(axis, device->relBitmask); |
| 340 | } |
| 341 | } |
| 342 | return false; |
| 343 | } |
| 344 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 345 | bool EventHub::hasInputProperty(int32_t deviceId, int property) const { |
| 346 | if (property >= 0 && property <= INPUT_PROP_MAX) { |
| 347 | AutoMutex _l(mLock); |
| 348 | |
| 349 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 350 | if (device) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 351 | return test_bit(property, device->propBitmask); |
| 352 | } |
| 353 | } |
| 354 | return false; |
| 355 | } |
| 356 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 357 | int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 358 | if (scanCode >= 0 && scanCode <= KEY_MAX) { |
| 359 | AutoMutex _l(mLock); |
| 360 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 361 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 362 | if (device && !device->isVirtual() && test_bit(scanCode, device->keyBitmask)) { |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 363 | uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; |
| 364 | memset(keyState, 0, sizeof(keyState)); |
| 365 | if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { |
| 366 | return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
| 367 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 370 | return AKEY_STATE_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 373 | int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const { |
| 374 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 375 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 376 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 377 | if (device && !device->isVirtual() && device->keyMap.haveKeyLayout()) { |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 378 | Vector<int32_t> scanCodes; |
| 379 | device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes); |
| 380 | if (scanCodes.size() != 0) { |
| 381 | uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)]; |
| 382 | memset(keyState, 0, sizeof(keyState)); |
| 383 | if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) { |
| 384 | for (size_t i = 0; i < scanCodes.size(); i++) { |
| 385 | int32_t sc = scanCodes.itemAt(i); |
| 386 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) { |
| 387 | return AKEY_STATE_DOWN; |
| 388 | } |
| 389 | } |
| 390 | return AKEY_STATE_UP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 394 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 397 | int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 398 | if (sw >= 0 && sw <= SW_MAX) { |
| 399 | AutoMutex _l(mLock); |
| 400 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 401 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 402 | if (device && !device->isVirtual() && test_bit(sw, device->swBitmask)) { |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 403 | uint8_t swState[sizeof_bit_array(SW_MAX + 1)]; |
| 404 | memset(swState, 0, sizeof(swState)); |
| 405 | if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) { |
| 406 | return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP; |
| 407 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 408 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 409 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 410 | return AKEY_STATE_UNKNOWN; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 413 | 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] | 414 | *outValue = 0; |
| 415 | |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 416 | if (axis >= 0 && axis <= ABS_MAX) { |
| 417 | AutoMutex _l(mLock); |
| 418 | |
| 419 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 420 | if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) { |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 421 | struct input_absinfo info; |
| 422 | if(ioctl(device->fd, EVIOCGABS(axis), &info)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 423 | ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d", |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 424 | axis, device->identifier.name.string(), device->fd, errno); |
| 425 | return -errno; |
| 426 | } |
| 427 | |
| 428 | *outValue = info.value; |
| 429 | return OK; |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 435 | bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, |
| 436 | const int32_t* keyCodes, uint8_t* outFlags) const { |
| 437 | AutoMutex _l(mLock); |
| 438 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 439 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 440 | if (device && device->keyMap.haveKeyLayout()) { |
| 441 | Vector<int32_t> scanCodes; |
| 442 | for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) { |
| 443 | scanCodes.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 444 | |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 445 | status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey( |
| 446 | keyCodes[codeIndex], &scanCodes); |
| 447 | if (! err) { |
| 448 | // check the possible scan codes identified by the layout map against the |
| 449 | // map of codes actually emitted by the driver |
| 450 | for (size_t sc = 0; sc < scanCodes.size(); sc++) { |
| 451 | if (test_bit(scanCodes[sc], device->keyBitmask)) { |
| 452 | outFlags[codeIndex] = 1; |
| 453 | break; |
| 454 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 458 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 459 | } |
Jeff Brown | ba421dd | 2011-08-10 15:07:05 -0700 | [diff] [blame] | 460 | return false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 463 | status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, |
| 464 | int32_t* outKeycode, uint32_t* outFlags) const { |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 465 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 466 | Device* device = getDeviceLocked(deviceId); |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 467 | |
Jeff Brown | 4a3862f | 2012-04-17 18:50:05 -0700 | [diff] [blame] | 468 | if (device) { |
| 469 | // Check the key character map first. |
| 470 | sp<KeyCharacterMap> kcm = device->getKeyCharacterMap(); |
| 471 | if (kcm != NULL) { |
| 472 | if (!kcm->mapKey(scanCode, usageCode, outKeycode)) { |
| 473 | *outFlags = 0; |
| 474 | return NO_ERROR; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // Check the key layout next. |
| 479 | if (device->keyMap.haveKeyLayout()) { |
| 480 | if (!device->keyMap.keyLayoutMap->mapKey( |
| 481 | scanCode, usageCode, outKeycode, outFlags)) { |
| 482 | return NO_ERROR; |
| 483 | } |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 486 | |
Dianne Hackborn | e3dd884 | 2009-07-14 12:06:54 -0700 | [diff] [blame] | 487 | *outKeycode = 0; |
| 488 | *outFlags = 0; |
| 489 | return NAME_NOT_FOUND; |
| 490 | } |
| 491 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 492 | status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 493 | AutoMutex _l(mLock); |
| 494 | Device* device = getDeviceLocked(deviceId); |
| 495 | |
| 496 | if (device && device->keyMap.haveKeyLayout()) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 497 | status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 498 | if (err == NO_ERROR) { |
| 499 | return NO_ERROR; |
| 500 | } |
| 501 | } |
| 502 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 503 | return NAME_NOT_FOUND; |
| 504 | } |
| 505 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 506 | void EventHub::setExcludedDevices(const Vector<String8>& devices) { |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 507 | AutoMutex _l(mLock); |
| 508 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 509 | mExcludedDevices = devices; |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 512 | bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const { |
| 513 | AutoMutex _l(mLock); |
| 514 | Device* device = getDeviceLocked(deviceId); |
| 515 | if (device && scanCode >= 0 && scanCode <= KEY_MAX) { |
| 516 | if (test_bit(scanCode, device->keyBitmask)) { |
| 517 | return true; |
| 518 | } |
| 519 | } |
| 520 | return false; |
| 521 | } |
| 522 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 523 | bool EventHub::hasLed(int32_t deviceId, int32_t led) const { |
| 524 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 525 | Device* device = getDeviceLocked(deviceId); |
Michael Wright | 58f5a61 | 2013-10-18 15:26:48 -0700 | [diff] [blame^] | 526 | int32_t sc; |
| 527 | if (device && mapLed(device, led, &sc) == NO_ERROR) { |
| 528 | if (test_bit(sc, device->ledBitmask)) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 529 | return true; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) { |
| 536 | AutoMutex _l(mLock); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 537 | Device* device = getDeviceLocked(deviceId); |
Michael Wright | 58f5a61 | 2013-10-18 15:26:48 -0700 | [diff] [blame^] | 538 | setLedStateLocked(device, led, on); |
| 539 | } |
| 540 | |
| 541 | void EventHub::setLedStateLocked(Device* device, int32_t led, bool on) { |
| 542 | int32_t sc; |
| 543 | if (device && !device->isVirtual() && mapLed(device, led, &sc) != NAME_NOT_FOUND) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 544 | struct input_event ev; |
| 545 | ev.time.tv_sec = 0; |
| 546 | ev.time.tv_usec = 0; |
| 547 | ev.type = EV_LED; |
Michael Wright | 58f5a61 | 2013-10-18 15:26:48 -0700 | [diff] [blame^] | 548 | ev.code = sc; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 549 | ev.value = on ? 1 : 0; |
| 550 | |
| 551 | ssize_t nWrite; |
| 552 | do { |
| 553 | nWrite = write(device->fd, &ev, sizeof(struct input_event)); |
| 554 | } while (nWrite == -1 && errno == EINTR); |
| 555 | } |
| 556 | } |
| 557 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 558 | void EventHub::getVirtualKeyDefinitions(int32_t deviceId, |
| 559 | Vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 560 | outVirtualKeys.clear(); |
| 561 | |
| 562 | AutoMutex _l(mLock); |
| 563 | Device* device = getDeviceLocked(deviceId); |
| 564 | if (device && device->virtualKeyMap) { |
| 565 | outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys()); |
| 566 | } |
| 567 | } |
| 568 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 569 | sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const { |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 570 | AutoMutex _l(mLock); |
| 571 | Device* device = getDeviceLocked(deviceId); |
| 572 | if (device) { |
Jeff Brown | 4a3862f | 2012-04-17 18:50:05 -0700 | [diff] [blame] | 573 | return device->getKeyCharacterMap(); |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 574 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 575 | return NULL; |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 578 | bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, |
| 579 | const sp<KeyCharacterMap>& map) { |
| 580 | AutoMutex _l(mLock); |
| 581 | Device* device = getDeviceLocked(deviceId); |
| 582 | if (device) { |
| 583 | if (map != device->overlayKeyMap) { |
| 584 | device->overlayKeyMap = map; |
| 585 | device->combinedKeyMap = KeyCharacterMap::combine( |
| 586 | device->keyMap.keyCharacterMap, map); |
| 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 593 | void EventHub::vibrate(int32_t deviceId, nsecs_t duration) { |
| 594 | AutoMutex _l(mLock); |
| 595 | Device* device = getDeviceLocked(deviceId); |
| 596 | if (device && !device->isVirtual()) { |
| 597 | ff_effect effect; |
| 598 | memset(&effect, 0, sizeof(effect)); |
| 599 | effect.type = FF_RUMBLE; |
| 600 | effect.id = device->ffEffectId; |
| 601 | effect.u.rumble.strong_magnitude = 0xc000; |
| 602 | effect.u.rumble.weak_magnitude = 0xc000; |
| 603 | effect.replay.length = (duration + 999999LL) / 1000000LL; |
| 604 | effect.replay.delay = 0; |
| 605 | if (ioctl(device->fd, EVIOCSFF, &effect)) { |
| 606 | ALOGW("Could not upload force feedback effect to device %s due to error %d.", |
| 607 | device->identifier.name.string(), errno); |
| 608 | return; |
| 609 | } |
| 610 | device->ffEffectId = effect.id; |
| 611 | |
| 612 | struct input_event ev; |
| 613 | ev.time.tv_sec = 0; |
| 614 | ev.time.tv_usec = 0; |
| 615 | ev.type = EV_FF; |
| 616 | ev.code = device->ffEffectId; |
| 617 | ev.value = 1; |
| 618 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { |
| 619 | ALOGW("Could not start force feedback effect on device %s due to error %d.", |
| 620 | device->identifier.name.string(), errno); |
| 621 | return; |
| 622 | } |
| 623 | device->ffEffectPlaying = true; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void EventHub::cancelVibrate(int32_t deviceId) { |
| 628 | AutoMutex _l(mLock); |
| 629 | Device* device = getDeviceLocked(deviceId); |
| 630 | if (device && !device->isVirtual()) { |
| 631 | if (device->ffEffectPlaying) { |
| 632 | device->ffEffectPlaying = false; |
| 633 | |
| 634 | struct input_event ev; |
| 635 | ev.time.tv_sec = 0; |
| 636 | ev.time.tv_usec = 0; |
| 637 | ev.type = EV_FF; |
| 638 | ev.code = device->ffEffectId; |
| 639 | ev.value = 0; |
| 640 | if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) { |
| 641 | ALOGW("Could not stop force feedback effect on device %s due to error %d.", |
| 642 | device->identifier.name.string(), errno); |
| 643 | return; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 649 | EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 650 | if (deviceId == BUILT_IN_KEYBOARD_ID) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 651 | deviceId = mBuiltInKeyboardId; |
| 652 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 653 | ssize_t index = mDevices.indexOfKey(deviceId); |
| 654 | return index >= 0 ? mDevices.valueAt(index) : NULL; |
| 655 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 656 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 657 | EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const { |
| 658 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 659 | Device* device = mDevices.valueAt(i); |
| 660 | if (device->path == devicePath) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 661 | return device; |
| 662 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | } |
| 664 | return NULL; |
| 665 | } |
| 666 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 667 | size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 668 | ALOG_ASSERT(bufferSize >= 1); |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 669 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 670 | AutoMutex _l(mLock); |
Mike Lockwood | 1d9dfc5 | 2009-07-16 11:11:18 -0400 | [diff] [blame] | 671 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 672 | struct input_event readBuffer[bufferSize]; |
| 673 | |
| 674 | RawEvent* event = buffer; |
| 675 | size_t capacity = bufferSize; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 676 | bool awoken = false; |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 677 | for (;;) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 678 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 679 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 680 | // Reopen input devices if needed. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 681 | if (mNeedToReopenDevices) { |
| 682 | mNeedToReopenDevices = false; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 683 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 684 | ALOGI("Reopening all input devices due to a configuration change."); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 685 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 686 | closeAllDevicesLocked(); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 687 | mNeedToScanDevices = true; |
| 688 | break; // return to the caller before we actually rescan |
| 689 | } |
| 690 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 691 | // Report any devices that had last been added/removed. |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 692 | while (mClosingDevices) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 693 | Device* device = mClosingDevices; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 694 | ALOGV("Reporting device closed: id=%d, name=%s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | device->id, device->path.string()); |
| 696 | mClosingDevices = device->next; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 697 | event->when = now; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 698 | event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 699 | event->type = DEVICE_REMOVED; |
| 700 | event += 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | delete device; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 702 | mNeedToSendFinishedDeviceScan = true; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 703 | if (--capacity == 0) { |
| 704 | break; |
| 705 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 707 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 708 | if (mNeedToScanDevices) { |
| 709 | mNeedToScanDevices = false; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 710 | scanDevicesLocked(); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 711 | mNeedToSendFinishedDeviceScan = true; |
| 712 | } |
| 713 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 714 | while (mOpeningDevices != NULL) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 715 | Device* device = mOpeningDevices; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 716 | ALOGV("Reporting device opened: id=%d, name=%s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | device->id, device->path.string()); |
| 718 | mOpeningDevices = device->next; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 719 | event->when = now; |
| 720 | event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
| 721 | event->type = DEVICE_ADDED; |
| 722 | event += 1; |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 723 | mNeedToSendFinishedDeviceScan = true; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 724 | if (--capacity == 0) { |
| 725 | break; |
| 726 | } |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | if (mNeedToSendFinishedDeviceScan) { |
| 730 | mNeedToSendFinishedDeviceScan = false; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 731 | event->when = now; |
| 732 | event->type = FINISHED_DEVICE_SCAN; |
| 733 | event += 1; |
| 734 | if (--capacity == 0) { |
| 735 | break; |
| 736 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 739 | // Grab the next input event. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 740 | bool deviceChanged = false; |
| 741 | while (mPendingEventIndex < mPendingEventCount) { |
| 742 | const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++]; |
| 743 | if (eventItem.data.u32 == EPOLL_ID_INOTIFY) { |
| 744 | if (eventItem.events & EPOLLIN) { |
| 745 | mPendingINotify = true; |
| 746 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 747 | ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 748 | } |
| 749 | continue; |
| 750 | } |
| 751 | |
| 752 | if (eventItem.data.u32 == EPOLL_ID_WAKE) { |
| 753 | if (eventItem.events & EPOLLIN) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 754 | ALOGV("awoken after wake()"); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 755 | awoken = true; |
| 756 | char buffer[16]; |
| 757 | ssize_t nRead; |
| 758 | do { |
| 759 | nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer)); |
| 760 | } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer)); |
| 761 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 762 | ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.", |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 763 | eventItem.events); |
| 764 | } |
| 765 | continue; |
| 766 | } |
| 767 | |
| 768 | ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32); |
| 769 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 770 | ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.", |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 771 | eventItem.events, eventItem.data.u32); |
| 772 | continue; |
| 773 | } |
| 774 | |
| 775 | Device* device = mDevices.valueAt(deviceIndex); |
| 776 | if (eventItem.events & EPOLLIN) { |
| 777 | int32_t readSize = read(device->fd, readBuffer, |
| 778 | sizeof(struct input_event) * capacity); |
| 779 | if (readSize == 0 || (readSize < 0 && errno == ENODEV)) { |
| 780 | // Device was removed before INotify noticed. |
Jeff Brown | 4130554 | 2011-10-05 11:14:13 -0700 | [diff] [blame] | 781 | ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d " |
| 782 | "capacity: %d errno: %d)\n", |
| 783 | device->fd, readSize, bufferSize, capacity, errno); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 784 | deviceChanged = true; |
| 785 | closeDeviceLocked(device); |
| 786 | } else if (readSize < 0) { |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 787 | if (errno != EAGAIN && errno != EINTR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 788 | ALOGW("could not get event (errno=%d)", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 789 | } |
| 790 | } else if ((readSize % sizeof(struct input_event)) != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 791 | ALOGE("could not get event (wrong size: %d)", readSize); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 792 | } else { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 793 | int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id; |
| 794 | |
| 795 | size_t count = size_t(readSize) / sizeof(struct input_event); |
| 796 | for (size_t i = 0; i < count; i++) { |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 797 | struct input_event& iev = readBuffer[i]; |
| 798 | ALOGV("%s got: time=%d.%06d, type=%d, code=%d, value=%d", |
JP Abgrall | 25a465b | 2012-05-16 10:33:49 -0700 | [diff] [blame] | 799 | device->path.string(), |
| 800 | (int) iev.time.tv_sec, (int) iev.time.tv_usec, |
| 801 | iev.type, iev.code, iev.value); |
| 802 | |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 803 | // Some input devices may have a better concept of the time |
| 804 | // when an input event was actually generated than the kernel |
| 805 | // which simply timestamps all events on entry to evdev. |
| 806 | // This is a custom Android extension of the input protocol |
| 807 | // mainly intended for use with uinput based device drivers. |
| 808 | if (iev.type == EV_MSC) { |
| 809 | if (iev.code == MSC_ANDROID_TIME_SEC) { |
| 810 | device->timestampOverrideSec = iev.value; |
| 811 | continue; |
| 812 | } else if (iev.code == MSC_ANDROID_TIME_USEC) { |
| 813 | device->timestampOverrideUsec = iev.value; |
| 814 | continue; |
| 815 | } |
| 816 | } |
| 817 | if (device->timestampOverrideSec || device->timestampOverrideUsec) { |
| 818 | iev.time.tv_sec = device->timestampOverrideSec; |
| 819 | iev.time.tv_usec = device->timestampOverrideUsec; |
| 820 | if (iev.type == EV_SYN && iev.code == SYN_REPORT) { |
| 821 | device->timestampOverrideSec = 0; |
| 822 | device->timestampOverrideUsec = 0; |
| 823 | } |
| 824 | ALOGV("applied override time %d.%06d", |
| 825 | int(iev.time.tv_sec), int(iev.time.tv_usec)); |
| 826 | } |
| 827 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 828 | #ifdef HAVE_POSIX_CLOCKS |
| 829 | // Use the time specified in the event instead of the current time |
| 830 | // so that downstream code can get more accurate estimates of |
| 831 | // event dispatch latency from the time the event is enqueued onto |
| 832 | // the evdev client buffer. |
| 833 | // |
| 834 | // The event's timestamp fortuitously uses the same monotonic clock |
| 835 | // time base as the rest of Android. The kernel event device driver |
| 836 | // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts(). |
| 837 | // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere |
| 838 | // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a |
| 839 | // system call that also queries ktime_get_ts(). |
| 840 | event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL |
| 841 | + nsecs_t(iev.time.tv_usec) * 1000LL; |
JP Abgrall | 25a465b | 2012-05-16 10:33:49 -0700 | [diff] [blame] | 842 | ALOGV("event time %lld, now %lld", event->when, now); |
Jeff Brown | f33b2b2 | 2012-10-05 17:59:56 -0700 | [diff] [blame] | 843 | |
| 844 | // Bug 7291243: Add a guard in case the kernel generates timestamps |
| 845 | // that appear to be far into the future because they were generated |
| 846 | // using the wrong clock source. |
| 847 | // |
| 848 | // This can happen because when the input device is initially opened |
| 849 | // it has a default clock source of CLOCK_REALTIME. Any input events |
| 850 | // enqueued right after the device is opened will have timestamps |
| 851 | // generated using CLOCK_REALTIME. We later set the clock source |
| 852 | // to CLOCK_MONOTONIC but it is already too late. |
| 853 | // |
| 854 | // Invalid input event timestamps can result in ANRs, crashes and |
| 855 | // and other issues that are hard to track down. We must not let them |
| 856 | // propagate through the system. |
| 857 | // |
| 858 | // Log a warning so that we notice the problem and recover gracefully. |
| 859 | if (event->when >= now + 10 * 1000000000LL) { |
| 860 | // Double-check. Time may have moved on. |
| 861 | nsecs_t time = systemTime(SYSTEM_TIME_MONOTONIC); |
| 862 | if (event->when > time) { |
| 863 | ALOGW("An input event from %s has a timestamp that appears to " |
| 864 | "have been generated using the wrong clock source " |
| 865 | "(expected CLOCK_MONOTONIC): " |
| 866 | "event time %lld, current time %lld, call time %lld. " |
| 867 | "Using current time instead.", |
| 868 | device->path.string(), event->when, time, now); |
| 869 | event->when = time; |
| 870 | } else { |
| 871 | ALOGV("Event time is ok but failed the fast path and required " |
| 872 | "an extra call to systemTime: " |
| 873 | "event time %lld, current time %lld, call time %lld.", |
| 874 | event->when, time, now); |
| 875 | } |
| 876 | } |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 877 | #else |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 878 | event->when = now; |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 879 | #endif |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 880 | event->deviceId = deviceId; |
| 881 | event->type = iev.type; |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 882 | event->code = iev.code; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 883 | event->value = iev.value; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 884 | event += 1; |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 885 | capacity -= 1; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 886 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 887 | if (capacity == 0) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 888 | // The result buffer is full. Reset the pending event index |
| 889 | // so we will try to read the device again on the next iteration. |
| 890 | mPendingEventIndex -= 1; |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 891 | break; |
| 892 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 894 | } else if (eventItem.events & EPOLLHUP) { |
| 895 | ALOGI("Removing device %s due to epoll hang-up event.", |
| 896 | device->identifier.name.string()); |
| 897 | deviceChanged = true; |
| 898 | closeDeviceLocked(device); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 899 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 900 | ALOGW("Received unexpected epoll event 0x%08x for device %s.", |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 901 | eventItem.events, device->identifier.name.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | } |
| 903 | } |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 904 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 905 | // readNotify() will modify the list of devices so this must be done after |
| 906 | // processing all other events to ensure that we read all remaining events |
| 907 | // before closing the devices. |
| 908 | if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) { |
| 909 | mPendingINotify = false; |
| 910 | readNotifyLocked(); |
| 911 | deviceChanged = true; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 912 | } |
| 913 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 914 | // Report added or removed devices immediately. |
| 915 | if (deviceChanged) { |
| 916 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | } |
Jeff Brown | a9b8422 | 2010-10-14 02:23:43 -0700 | [diff] [blame] | 918 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 919 | // Return now if we have collected any events or if we were explicitly awoken. |
| 920 | if (event != buffer || awoken) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 921 | break; |
| 922 | } |
| 923 | |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 924 | // Poll for events. Mind the wake lock dance! |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 925 | // 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] | 926 | // subtle choreography. When a device driver has pending (unread) events, it acquires |
| 927 | // a kernel wake lock. However, once the last pending event has been read, the device |
| 928 | // driver will release the kernel wake lock. To prevent the system from going to sleep |
| 929 | // when this happens, the EventHub holds onto its own user wake lock while the client |
| 930 | // is processing events. Thus the system can only sleep if there are no events |
| 931 | // pending or currently being processed. |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 932 | // |
| 933 | // The timeout is advisory only. If the device is asleep, it will not wake just to |
| 934 | // service the timeout. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 935 | mPendingEventIndex = 0; |
| 936 | |
| 937 | mLock.unlock(); // release lock before poll, must be before release_wake_lock |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 938 | release_wake_lock(WAKE_LOCK_ID); |
| 939 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 940 | int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 941 | |
| 942 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 943 | mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 944 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 945 | if (pollResult == 0) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 946 | // Timed out. |
| 947 | mPendingEventCount = 0; |
| 948 | break; |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 949 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 950 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 951 | if (pollResult < 0) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 952 | // An error occurred. |
| 953 | mPendingEventCount = 0; |
| 954 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 955 | // Sleep after errors to avoid locking up the system. |
| 956 | // Hopefully the error is transient. |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 957 | if (errno != EINTR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 958 | ALOGW("poll failed (errno=%d)\n", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 959 | usleep(100000); |
| 960 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 961 | } else { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 962 | // Some events occurred. |
| 963 | mPendingEventCount = size_t(pollResult); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 964 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 966 | |
| 967 | // All done, return the number of events we read. |
| 968 | return event - buffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | } |
| 970 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 971 | void EventHub::wake() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 972 | ALOGV("wake() called"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 974 | ssize_t nWrite; |
| 975 | do { |
| 976 | nWrite = write(mWakeWritePipeFd, "W", 1); |
| 977 | } while (nWrite == -1 && errno == EINTR); |
| 978 | |
| 979 | if (nWrite != 1 && errno != EAGAIN) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 980 | ALOGW("Could not write wake signal, errno=%d", errno); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 982 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 983 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 984 | void EventHub::scanDevicesLocked() { |
| 985 | status_t res = scanDirLocked(DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | if(res < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 987 | ALOGE("scan dir failed for %s\n", DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 989 | if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) { |
| 990 | createVirtualKeyboardLocked(); |
| 991 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | } |
| 993 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | // ---------------------------------------------------------------------------- |
| 995 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 996 | static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) { |
| 997 | const uint8_t* end = array + endIndex; |
| 998 | array += startIndex; |
| 999 | while (array != end) { |
| 1000 | if (*(array++) != 0) { |
| 1001 | return true; |
| 1002 | } |
| 1003 | } |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
| 1007 | static const int32_t GAMEPAD_KEYCODES[] = { |
| 1008 | AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C, |
| 1009 | AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z, |
| 1010 | AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1, |
| 1011 | AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2, |
| 1012 | AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR, |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1013 | AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE, |
| 1014 | AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4, |
| 1015 | AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8, |
| 1016 | AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12, |
| 1017 | AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16, |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1018 | }; |
| 1019 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1020 | status_t EventHub::openDeviceLocked(const char *devicePath) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1021 | char buffer[80]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1023 | ALOGV("Opening device: %s", devicePath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | |
Jeff Brown | 874c1e9 | 2012-01-19 14:32:47 -0800 | [diff] [blame] | 1025 | int fd = open(devicePath, O_RDWR | O_CLOEXEC); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | if(fd < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1027 | ALOGE("could not open %s, %s\n", devicePath, strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | return -1; |
| 1029 | } |
| 1030 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1031 | InputDeviceIdentifier identifier; |
| 1032 | |
| 1033 | // Get device name. |
| 1034 | if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) { |
| 1035 | //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno)); |
| 1036 | } else { |
| 1037 | buffer[sizeof(buffer) - 1] = '\0'; |
| 1038 | identifier.name.setTo(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | } |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 1040 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1041 | // Check to see if the device is on our excluded list |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1042 | for (size_t i = 0; i < mExcludedDevices.size(); i++) { |
| 1043 | const String8& item = mExcludedDevices.itemAt(i); |
| 1044 | if (identifier.name == item) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1045 | ALOGI("ignoring event id %s driver %s\n", devicePath, item.string()); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 1046 | close(fd); |
Mike Lockwood | 15431a9 | 2009-07-17 00:10:10 -0400 | [diff] [blame] | 1047 | return -1; |
| 1048 | } |
| 1049 | } |
| 1050 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1051 | // Get device driver version. |
| 1052 | int driverVersion; |
| 1053 | if(ioctl(fd, EVIOCGVERSION, &driverVersion)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1054 | ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno)); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1055 | close(fd); |
| 1056 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1059 | // Get device identifier. |
| 1060 | struct input_id inputId; |
| 1061 | if(ioctl(fd, EVIOCGID, &inputId)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1062 | ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno)); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1063 | close(fd); |
| 1064 | return -1; |
| 1065 | } |
| 1066 | identifier.bus = inputId.bustype; |
| 1067 | identifier.product = inputId.product; |
| 1068 | identifier.vendor = inputId.vendor; |
| 1069 | identifier.version = inputId.version; |
| 1070 | |
| 1071 | // Get device physical location. |
| 1072 | if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { |
| 1073 | //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno)); |
| 1074 | } else { |
| 1075 | buffer[sizeof(buffer) - 1] = '\0'; |
| 1076 | identifier.location.setTo(buffer); |
| 1077 | } |
| 1078 | |
| 1079 | // Get device unique id. |
| 1080 | if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) { |
| 1081 | //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno)); |
| 1082 | } else { |
| 1083 | buffer[sizeof(buffer) - 1] = '\0'; |
| 1084 | identifier.uniqueId.setTo(buffer); |
| 1085 | } |
| 1086 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1087 | // Fill in the descriptor. |
| 1088 | setDescriptor(identifier); |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 1089 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1090 | // Make file descriptor non-blocking for use with poll(). |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 1091 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1092 | ALOGE("Error %d making device file descriptor non-blocking.", errno); |
Jeff Brown | cc2e717 | 2010-08-17 16:48:25 -0700 | [diff] [blame] | 1093 | close(fd); |
| 1094 | return -1; |
| 1095 | } |
| 1096 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1097 | // Allocate device. (The device object takes ownership of the fd at this point.) |
| 1098 | int32_t deviceId = mNextDeviceId++; |
| 1099 | Device* device = new Device(fd, deviceId, String8(devicePath), identifier); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 1101 | ALOGV("add device %d: %s\n", deviceId, devicePath); |
| 1102 | ALOGV(" bus: %04x\n" |
| 1103 | " vendor %04x\n" |
| 1104 | " product %04x\n" |
| 1105 | " version %04x\n", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1106 | identifier.bus, identifier.vendor, identifier.product, identifier.version); |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 1107 | ALOGV(" name: \"%s\"\n", identifier.name.string()); |
| 1108 | ALOGV(" location: \"%s\"\n", identifier.location.string()); |
| 1109 | ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string()); |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1110 | ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string()); |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 1111 | ALOGV(" driver: v%d.%d.%d\n", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1112 | driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1114 | // Load the configuration file for the device. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1115 | loadConfigurationLocked(device); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1116 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1117 | // Figure out the kinds of events the device reports. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1118 | ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask); |
| 1119 | ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask); |
| 1120 | ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask); |
| 1121 | ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask); |
| 1122 | ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask); |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1123 | ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1124 | ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1125 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1126 | // See if this is a keyboard. Ignore everything in the button range except for |
| 1127 | // 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] | 1128 | bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC)) |
| 1129 | || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK), |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1130 | sizeof_bit_array(KEY_MAX + 1)); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1131 | bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC), |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1132 | sizeof_bit_array(BTN_MOUSE)) |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1133 | || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK), |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1134 | sizeof_bit_array(BTN_DIGI)); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1135 | if (haveKeyboardKeys || haveGamepadButtons) { |
| 1136 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1138 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1139 | // 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] | 1140 | if (test_bit(BTN_MOUSE, device->keyBitmask) |
| 1141 | && test_bit(REL_X, device->relBitmask) |
| 1142 | && test_bit(REL_Y, device->relBitmask)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1143 | device->classes |= INPUT_DEVICE_CLASS_CURSOR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1144 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1145 | |
| 1146 | // See if this is a touch pad. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1147 | // Is this a new modern multi-touch driver? |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1148 | if (test_bit(ABS_MT_POSITION_X, device->absBitmask) |
| 1149 | && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1150 | // Some joysticks such as the PS3 controller report axes that conflict |
| 1151 | // with the ABS_MT range. Try to confirm that the device really is |
| 1152 | // a touch screen. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1153 | if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 1154 | device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT; |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1155 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1156 | // Is this an old style single-touch driver? |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1157 | } else if (test_bit(BTN_TOUCH, device->keyBitmask) |
| 1158 | && test_bit(ABS_X, device->absBitmask) |
| 1159 | && test_bit(ABS_Y, device->absBitmask)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1160 | device->classes |= INPUT_DEVICE_CLASS_TOUCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1163 | // See if this device is a joystick. |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1164 | // Assumes that joysticks always have gamepad buttons in order to distinguish them |
| 1165 | // from other devices such as accelerometers that also have absolute axes. |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 1166 | if (haveGamepadButtons) { |
| 1167 | uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK; |
| 1168 | for (int i = 0; i <= ABS_MAX; i++) { |
| 1169 | if (test_bit(i, device->absBitmask) |
| 1170 | && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 1171 | device->classes = assumedClasses; |
| 1172 | break; |
| 1173 | } |
| 1174 | } |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1177 | // Check whether this device has switches. |
| 1178 | for (int i = 0; i <= SW_MAX; i++) { |
| 1179 | if (test_bit(i, device->swBitmask)) { |
| 1180 | device->classes |= INPUT_DEVICE_CLASS_SWITCH; |
| 1181 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } |
| 1183 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1184 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1185 | // Check whether this device supports the vibrator. |
| 1186 | if (test_bit(FF_RUMBLE, device->ffBitmask)) { |
| 1187 | device->classes |= INPUT_DEVICE_CLASS_VIBRATOR; |
| 1188 | } |
| 1189 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1190 | // Configure virtual keys. |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 1191 | if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1192 | // Load the virtual keys for the touch screen, if any. |
| 1193 | // 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] | 1194 | status_t status = loadVirtualKeyMapLocked(device); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1195 | if (!status) { |
| 1196 | device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1200 | // Load the key map. |
| 1201 | // We need to do this for joysticks too because the key layout may specify axes. |
| 1202 | status_t keyMapStatus = NAME_NOT_FOUND; |
| 1203 | if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1204 | // Load the keymap for the device. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1205 | keyMapStatus = loadKeyMapLocked(device); |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1206 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1207 | |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1208 | // Configure the keyboard, gamepad or virtual keyboard. |
| 1209 | if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1210 | // Register the keyboard as a built-in keyboard if it is eligible. |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1211 | if (!keyMapStatus |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1212 | && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1213 | && isEligibleBuiltInKeyboard(device->identifier, |
| 1214 | device->configuration, &device->keyMap)) { |
| 1215 | mBuiltInKeyboardId = device->id; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | |
Ken Wakasa | 02a44f7 | 2013-07-05 04:08:36 +0000 | [diff] [blame] | 1218 | // 'Q' key support = cheap test of whether this is an alpha-capable kbd |
| 1219 | if (hasKeycodeLocked(device, AKEYCODE_Q)) { |
| 1220 | device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY; |
| 1221 | } |
| 1222 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1223 | // See if this device has a DPAD. |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1224 | if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) && |
| 1225 | hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) && |
| 1226 | hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) && |
| 1227 | hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) && |
| 1228 | hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1229 | device->classes |= INPUT_DEVICE_CLASS_DPAD; |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1230 | } |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1231 | |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1232 | // See if this device has a gamepad. |
Kenny Root | 1d79a9d | 2010-10-21 15:46:03 -0700 | [diff] [blame] | 1233 | 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] | 1234 | if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) { |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1235 | device->classes |= INPUT_DEVICE_CLASS_GAMEPAD; |
| 1236 | break; |
| 1237 | } |
| 1238 | } |
Michael Wright | a0a7285 | 2013-02-21 23:51:45 -0800 | [diff] [blame] | 1239 | |
| 1240 | // Disable kernel key repeat since we handle it ourselves |
| 1241 | unsigned int repeatRate[] = {0,0}; |
| 1242 | if (ioctl(fd, EVIOCSREP, repeatRate)) { |
| 1243 | ALOGW("Unable to disable kernel key repeat for %s: %s", devicePath, strerror(errno)); |
| 1244 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 1247 | // If the device isn't recognized as something we handle, don't monitor it. |
| 1248 | if (device->classes == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1249 | ALOGV("Dropping device: id=%d, path='%s', name='%s'", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1250 | deviceId, devicePath, device->identifier.name.string()); |
Sean McNeil | aeb00c4 | 2010-06-23 16:00:37 +0700 | [diff] [blame] | 1251 | delete device; |
| 1252 | return -1; |
| 1253 | } |
| 1254 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1255 | // Determine whether the device is external or internal. |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1256 | if (isExternalDeviceLocked(device)) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1257 | device->classes |= INPUT_DEVICE_CLASS_EXTERNAL; |
| 1258 | } |
| 1259 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 1260 | if (device->classes & (INPUT_DEVICE_CLASS_JOYSTICK | INPUT_DEVICE_CLASS_GAMEPAD)) { |
| 1261 | device->controllerNumber = getNextControllerNumberLocked(device); |
Michael Wright | 58f5a61 | 2013-10-18 15:26:48 -0700 | [diff] [blame^] | 1262 | setLedForController(device); |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1265 | // Register with epoll. |
| 1266 | struct epoll_event eventItem; |
| 1267 | memset(&eventItem, 0, sizeof(eventItem)); |
Michael Wright | 67774c7 | 2013-09-25 14:13:40 -0700 | [diff] [blame] | 1268 | eventItem.events = mUsingEpollWakeup ? EPOLLIN : EPOLLIN | EPOLLWAKEUP; |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1269 | eventItem.data.u32 = deviceId; |
| 1270 | if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1271 | ALOGE("Could not add device fd to epoll instance. errno=%d", errno); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1272 | delete device; |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
Michael Wright | 67774c7 | 2013-09-25 14:13:40 -0700 | [diff] [blame] | 1276 | String8 wakeMechanism("EPOLLWAKEUP"); |
| 1277 | if (!mUsingEpollWakeup) { |
| 1278 | if (ioctl(fd, EVIOCSSUSPENDBLOCK, 1)) { |
| 1279 | wakeMechanism = "<none>"; |
| 1280 | } else { |
| 1281 | wakeMechanism = "EVIOCSSUSPENDBLOCK"; |
| 1282 | } |
| 1283 | } |
Jeff Brown | eca3cf5 | 2012-04-06 19:31:36 -0700 | [diff] [blame] | 1284 | |
| 1285 | // Tell the kernel that we want to use the monotonic clock for reporting timestamps |
| 1286 | // associated with input events. This is important because the input system |
| 1287 | // uses the timestamps extensively and assumes they were recorded using the monotonic |
| 1288 | // clock. |
| 1289 | // |
| 1290 | // In older kernel, before Linux 3.4, there was no way to tell the kernel which |
| 1291 | // clock to use to input event timestamps. The standard kernel behavior was to |
| 1292 | // record a real time timestamp, which isn't what we want. Android kernels therefore |
| 1293 | // contained a patch to the evdev_event() function in drivers/input/evdev.c to |
| 1294 | // replace the call to do_gettimeofday() with ktime_get_ts() to cause the monotonic |
| 1295 | // clock to be used instead of the real time clock. |
| 1296 | // |
| 1297 | // As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock. |
| 1298 | // Therefore, we no longer require the Android-specific kernel patch described above |
| 1299 | // as long as we make sure to set select the monotonic clock. We do that here. |
Jeff Brown | a75fe05 | 2012-05-01 18:41:26 -0700 | [diff] [blame] | 1300 | int clockId = CLOCK_MONOTONIC; |
| 1301 | bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId); |
Jeff Brown | e22afbe | 2011-12-16 13:45:40 -0800 | [diff] [blame] | 1302 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1303 | ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, " |
Jeff Brown | e22afbe | 2011-12-16 13:45:40 -0800 | [diff] [blame] | 1304 | "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, " |
Michael Wright | 67774c7 | 2013-09-25 14:13:40 -0700 | [diff] [blame] | 1305 | "wakeMechanism=%s, usingClockIoctl=%s", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1306 | deviceId, fd, devicePath, device->identifier.name.string(), |
| 1307 | device->classes, |
| 1308 | device->configurationFile.string(), |
| 1309 | device->keyMap.keyLayoutFile.string(), |
| 1310 | device->keyMap.keyCharacterMapFile.string(), |
Jeff Brown | e22afbe | 2011-12-16 13:45:40 -0800 | [diff] [blame] | 1311 | toString(mBuiltInKeyboardId == deviceId), |
Michael Wright | 67774c7 | 2013-09-25 14:13:40 -0700 | [diff] [blame] | 1312 | wakeMechanism.string(), toString(usingClockIoctl)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1313 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1314 | addDeviceLocked(device); |
| 1315 | return 0; |
| 1316 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1318 | void EventHub::createVirtualKeyboardLocked() { |
| 1319 | InputDeviceIdentifier identifier; |
| 1320 | identifier.name = "Virtual"; |
| 1321 | identifier.uniqueId = "<virtual>"; |
| 1322 | setDescriptor(identifier); |
| 1323 | |
| 1324 | Device* device = new Device(-1, VIRTUAL_KEYBOARD_ID, String8("<virtual>"), identifier); |
| 1325 | device->classes = INPUT_DEVICE_CLASS_KEYBOARD |
| 1326 | | INPUT_DEVICE_CLASS_ALPHAKEY |
| 1327 | | INPUT_DEVICE_CLASS_DPAD |
| 1328 | | INPUT_DEVICE_CLASS_VIRTUAL; |
| 1329 | loadKeyMapLocked(device); |
| 1330 | addDeviceLocked(device); |
| 1331 | } |
| 1332 | |
| 1333 | void EventHub::addDeviceLocked(Device* device) { |
| 1334 | mDevices.add(device->id, device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | device->next = mOpeningDevices; |
| 1336 | mOpeningDevices = device; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1339 | void EventHub::loadConfigurationLocked(Device* device) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1340 | device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier( |
| 1341 | device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1342 | if (device->configurationFile.isEmpty()) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1343 | ALOGD("No input device configuration file found for device '%s'.", |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1344 | device->identifier.name.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1345 | } else { |
| 1346 | status_t status = PropertyMap::load(device->configurationFile, |
| 1347 | &device->configuration); |
| 1348 | if (status) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1349 | ALOGE("Error loading input device configuration file for device '%s'. " |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1350 | "Using default configuration.", |
| 1351 | device->identifier.name.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1356 | status_t EventHub::loadVirtualKeyMapLocked(Device* device) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1357 | // The virtual key map is supplied by the kernel as a system board property file. |
| 1358 | String8 path; |
| 1359 | path.append("/sys/board_properties/virtualkeys."); |
| 1360 | path.append(device->identifier.name); |
| 1361 | if (access(path.string(), R_OK)) { |
| 1362 | return NAME_NOT_FOUND; |
| 1363 | } |
| 1364 | return VirtualKeyMap::load(path, &device->virtualKeyMap); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1367 | status_t EventHub::loadKeyMapLocked(Device* device) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1368 | return device->keyMap.load(device->identifier, device->configuration); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1371 | bool EventHub::isExternalDeviceLocked(Device* device) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1372 | if (device->configuration) { |
| 1373 | bool value; |
Max Braun | e81056f | 2011-08-30 14:35:45 -0700 | [diff] [blame] | 1374 | if (device->configuration->tryGetProperty(String8("device.internal"), value)) { |
| 1375 | return !value; |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH; |
| 1379 | } |
| 1380 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 1381 | int32_t EventHub::getNextControllerNumberLocked(Device* device) { |
| 1382 | if (mControllerNumbers.isFull()) { |
| 1383 | ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s", |
| 1384 | device->identifier.name.string()); |
| 1385 | return 0; |
| 1386 | } |
| 1387 | // Since the controller number 0 is reserved for non-controllers, translate all numbers up by |
| 1388 | // one |
| 1389 | return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1); |
| 1390 | } |
| 1391 | |
| 1392 | void EventHub::releaseControllerNumberLocked(Device* device) { |
| 1393 | int32_t num = device->controllerNumber; |
| 1394 | device->controllerNumber= 0; |
| 1395 | if (num == 0) { |
| 1396 | return; |
| 1397 | } |
| 1398 | mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1)); |
| 1399 | } |
| 1400 | |
Michael Wright | 58f5a61 | 2013-10-18 15:26:48 -0700 | [diff] [blame^] | 1401 | void EventHub::setLedForController(Device* device) { |
| 1402 | for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) { |
| 1403 | setLedStateLocked(device, ALED_CONTROLLER_1 + i, device->controllerNumber == i + 1); |
| 1404 | } |
| 1405 | } |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 1406 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1407 | bool EventHub::hasKeycodeLocked(Device* device, int keycode) const { |
| 1408 | if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) { |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1409 | return false; |
| 1410 | } |
| 1411 | |
| 1412 | Vector<int32_t> scanCodes; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1413 | device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes); |
Dianne Hackborn | 0dd7cb4 | 2009-08-04 05:49:43 -0700 | [diff] [blame] | 1414 | const size_t N = scanCodes.size(); |
| 1415 | for (size_t i=0; i<N && i<=KEY_MAX; i++) { |
| 1416 | int32_t sc = scanCodes.itemAt(i); |
| 1417 | if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) { |
| 1418 | return true; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
Michael Wright | 58f5a61 | 2013-10-18 15:26:48 -0700 | [diff] [blame^] | 1425 | status_t EventHub::mapLed(Device* device, int32_t led, int32_t* outScanCode) const { |
| 1426 | if (!device->keyMap.haveKeyLayout() || !device->ledBitmask) { |
| 1427 | return NAME_NOT_FOUND; |
| 1428 | } |
| 1429 | |
| 1430 | int32_t scanCode; |
| 1431 | if(device->keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) { |
| 1432 | if(scanCode >= 0 && scanCode <= LED_MAX && test_bit(scanCode, device->ledBitmask)) { |
| 1433 | *outScanCode = scanCode; |
| 1434 | return NO_ERROR; |
| 1435 | } |
| 1436 | } |
| 1437 | return NAME_NOT_FOUND; |
| 1438 | } |
| 1439 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1440 | status_t EventHub::closeDeviceByPathLocked(const char *devicePath) { |
| 1441 | Device* device = getDeviceByPathLocked(devicePath); |
| 1442 | if (device) { |
| 1443 | closeDeviceLocked(device); |
| 1444 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1446 | ALOGV("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] | 1447 | return -1; |
| 1448 | } |
| 1449 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1450 | void EventHub::closeAllDevicesLocked() { |
| 1451 | while (mDevices.size() > 0) { |
| 1452 | closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1)); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | void EventHub::closeDeviceLocked(Device* device) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1457 | ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n", |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1458 | device->path.string(), device->identifier.name.string(), device->id, |
| 1459 | device->fd, device->classes); |
| 1460 | |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1461 | if (device->id == mBuiltInKeyboardId) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1462 | ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this", |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1463 | device->path.string(), mBuiltInKeyboardId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1464 | mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1465 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1466 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1467 | if (!device->isVirtual()) { |
| 1468 | if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) { |
| 1469 | ALOGW("Could not remove device fd from epoll instance. errno=%d", errno); |
| 1470 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 1473 | releaseControllerNumberLocked(device); |
| 1474 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1475 | mDevices.removeItem(device->id); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1476 | device->close(); |
| 1477 | |
Jeff Brown | 8e9d443 | 2011-03-12 19:46:59 -0800 | [diff] [blame] | 1478 | // Unlink for opening devices list if it is present. |
| 1479 | Device* pred = NULL; |
| 1480 | bool found = false; |
| 1481 | for (Device* entry = mOpeningDevices; entry != NULL; ) { |
| 1482 | if (entry == device) { |
| 1483 | found = true; |
| 1484 | break; |
| 1485 | } |
| 1486 | pred = entry; |
| 1487 | entry = entry->next; |
| 1488 | } |
| 1489 | if (found) { |
| 1490 | // Unlink the device from the opening devices list then delete it. |
| 1491 | // We don't need to tell the client that the device was closed because |
| 1492 | // it does not even know it was opened in the first place. |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1493 | ALOGI("Device %s was immediately closed after opening.", device->path.string()); |
Jeff Brown | 8e9d443 | 2011-03-12 19:46:59 -0800 | [diff] [blame] | 1494 | if (pred) { |
| 1495 | pred->next = device->next; |
| 1496 | } else { |
| 1497 | mOpeningDevices = device->next; |
| 1498 | } |
| 1499 | delete device; |
| 1500 | } else { |
| 1501 | // Link into closing devices list. |
| 1502 | // The device will be deleted later after we have informed the client. |
| 1503 | device->next = mClosingDevices; |
| 1504 | mClosingDevices = device; |
| 1505 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1508 | status_t EventHub::readNotifyLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | int res; |
| 1510 | char devname[PATH_MAX]; |
| 1511 | char *filename; |
| 1512 | char event_buf[512]; |
| 1513 | int event_size; |
| 1514 | int event_pos = 0; |
| 1515 | struct inotify_event *event; |
| 1516 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1517 | ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1518 | res = read(mINotifyFd, event_buf, sizeof(event_buf)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | if(res < (int)sizeof(*event)) { |
| 1520 | if(errno == EINTR) |
| 1521 | return 0; |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1522 | ALOGW("could not get event, %s\n", strerror(errno)); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1523 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | } |
| 1525 | //printf("got %d bytes of event information\n", res); |
| 1526 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1527 | strcpy(devname, DEVICE_PATH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | filename = devname + strlen(devname); |
| 1529 | *filename++ = '/'; |
| 1530 | |
| 1531 | while(res >= (int)sizeof(*event)) { |
| 1532 | event = (struct inotify_event *)(event_buf + event_pos); |
| 1533 | //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : ""); |
| 1534 | if(event->len) { |
| 1535 | strcpy(filename, event->name); |
| 1536 | if(event->mask & IN_CREATE) { |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1537 | openDeviceLocked(devname); |
| 1538 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1539 | ALOGI("Removing device '%s' due to inotify event\n", devname); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1540 | closeDeviceByPathLocked(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | event_size = sizeof(*event) + event->len; |
| 1544 | res -= event_size; |
| 1545 | event_pos += event_size; |
| 1546 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | return 0; |
| 1548 | } |
| 1549 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1550 | status_t EventHub::scanDirLocked(const char *dirname) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | { |
| 1552 | char devname[PATH_MAX]; |
| 1553 | char *filename; |
| 1554 | DIR *dir; |
| 1555 | struct dirent *de; |
| 1556 | dir = opendir(dirname); |
| 1557 | if(dir == NULL) |
| 1558 | return -1; |
| 1559 | strcpy(devname, dirname); |
| 1560 | filename = devname + strlen(devname); |
| 1561 | *filename++ = '/'; |
| 1562 | while((de = readdir(dir))) { |
| 1563 | if(de->d_name[0] == '.' && |
| 1564 | (de->d_name[1] == '\0' || |
| 1565 | (de->d_name[1] == '.' && de->d_name[2] == '\0'))) |
| 1566 | continue; |
| 1567 | strcpy(filename, de->d_name); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1568 | openDeviceLocked(devname); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1569 | } |
| 1570 | closedir(dir); |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1574 | void EventHub::requestReopenDevices() { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1575 | ALOGV("requestReopenDevices() called"); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1576 | |
| 1577 | AutoMutex _l(mLock); |
| 1578 | mNeedToReopenDevices = true; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1581 | void EventHub::dump(String8& dump) { |
| 1582 | dump.append("Event Hub State:\n"); |
| 1583 | |
| 1584 | { // acquire lock |
| 1585 | AutoMutex _l(mLock); |
| 1586 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1587 | dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId); |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1588 | |
| 1589 | dump.append(INDENT "Devices:\n"); |
| 1590 | |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1591 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 1592 | const Device* device = mDevices.valueAt(i); |
| 1593 | if (mBuiltInKeyboardId == device->id) { |
| 1594 | dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n", |
| 1595 | device->id, device->identifier.name.string()); |
| 1596 | } else { |
| 1597 | dump.appendFormat(INDENT2 "%d: %s\n", device->id, |
| 1598 | device->identifier.name.string()); |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1599 | } |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1600 | dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes); |
| 1601 | dump.appendFormat(INDENT3 "Path: %s\n", device->path.string()); |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 1602 | dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string()); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1603 | dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string()); |
Michael Wright | ac6c78b | 2013-07-17 13:21:45 -0700 | [diff] [blame] | 1604 | dump.appendFormat(INDENT3 "ControllerNumber: %d\n", device->controllerNumber); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 1605 | dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string()); |
| 1606 | dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, " |
| 1607 | "product=0x%04x, version=0x%04x\n", |
| 1608 | device->identifier.bus, device->identifier.vendor, |
| 1609 | device->identifier.product, device->identifier.version); |
| 1610 | dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", |
| 1611 | device->keyMap.keyLayoutFile.string()); |
| 1612 | dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n", |
| 1613 | device->keyMap.keyCharacterMapFile.string()); |
| 1614 | dump.appendFormat(INDENT3 "ConfigurationFile: %s\n", |
| 1615 | device->configurationFile.string()); |
Jeff Brown | 61c0824 | 2012-04-19 11:14:33 -0700 | [diff] [blame] | 1616 | dump.appendFormat(INDENT3 "HaveKeyboardLayoutOverlay: %s\n", |
| 1617 | toString(device->overlayKeyMap != NULL)); |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 1618 | } |
| 1619 | } // release lock |
| 1620 | } |
| 1621 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 1622 | void EventHub::monitor() { |
| 1623 | // Acquire and release the lock to ensure that the event hub has not deadlocked. |
| 1624 | mLock.lock(); |
| 1625 | mLock.unlock(); |
| 1626 | } |
| 1627 | |
| 1628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | }; // namespace android |