blob: e30a772e2441a5e7ae613688fea41e8c427f382e [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
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 Project9066cfe2009-03-03 19:31:44 -080017#define LOG_TAG "EventHub"
18
JP Abgrall25a465b2012-05-16 10:33:49 -070019// #define LOG_NDEBUG 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Jeff Brownb4ff35d2011-01-02 16:37:43 -080021#include "EventHub.h"
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <hardware_legacy/power.h>
24
25#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <utils/Log.h>
27#include <utils/Timers.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070028#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070029#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
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 Brown9d3b1a42013-07-01 19:07:15 -070039#include <input/KeyLayoutMap.h>
40#include <input/KeyCharacterMap.h>
41#include <input/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43#include <string.h>
44#include <stdint.h>
45#include <dirent.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070046
47#include <sys/inotify.h>
48#include <sys/epoll.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include <sys/ioctl.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070050#include <sys/limits.h>
Jeff Brown4dac9012013-04-10 01:03:19 -070051#include <sys/sha1.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53/* this macro is used to tell if "bit" is set in "array"
54 * it selects a byte from the array, and does a boolean AND
55 * operation with a byte that only has the relevant bit set.
56 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
57 */
58#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
59
Jeff Brownfd0358292010-06-30 16:10:35 -070060/* this macro computes the number of bytes needed to represent a bit array of the specified size */
61#define sizeof_bit_array(bits) ((bits + 7) / 8)
62
Jeff Brownf2f487182010-10-01 17:46:21 -070063#define INDENT " "
64#define INDENT2 " "
65#define INDENT3 " "
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067namespace android {
68
69static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080070static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
72/* return the larger integer */
73static inline int max(int v1, int v2)
74{
75 return (v1 > v2) ? v1 : v2;
76}
77
Jeff Brownf2f487182010-10-01 17:46:21 -070078static inline const char* toString(bool value) {
79 return value ? "true" : "false";
80}
81
Jeff Browne38fdfa2012-04-06 14:51:01 -070082static String8 sha1(const String8& in) {
83 SHA1_CTX ctx;
84 SHA1Init(&ctx);
85 SHA1Update(&ctx, reinterpret_cast<const u_char*>(in.string()), in.size());
86 u_char digest[SHA1_DIGEST_LENGTH];
87 SHA1Final(digest, &ctx);
88
89 String8 out;
90 for (size_t i = 0; i < SHA1_DIGEST_LENGTH; i++) {
91 out.appendFormat("%02x", digest[i]);
92 }
93 return out;
94}
95
Jeff Brown9f25b7f2012-04-10 14:30:49 -070096static void setDescriptor(InputDeviceIdentifier& identifier) {
97 // Compute a device descriptor that uniquely identifies the device.
98 // The descriptor is assumed to be a stable identifier. Its value should not
99 // change between reboots, reconnections, firmware updates or new releases of Android.
100 // Ideally, we also want the descriptor to be short and relatively opaque.
101 String8 rawDescriptor;
102 rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor, identifier.product);
103 if (!identifier.uniqueId.isEmpty()) {
104 rawDescriptor.append("uniqueId:");
105 rawDescriptor.append(identifier.uniqueId);
106 } if (identifier.vendor == 0 && identifier.product == 0) {
107 // If we don't know the vendor and product id, then the device is probably
108 // built-in so we need to rely on other information to uniquely identify
109 // the input device. Usually we try to avoid relying on the device name or
110 // location but for built-in input device, they are unlikely to ever change.
111 if (!identifier.name.isEmpty()) {
112 rawDescriptor.append("name:");
113 rawDescriptor.append(identifier.name);
114 } else if (!identifier.location.isEmpty()) {
115 rawDescriptor.append("location:");
116 rawDescriptor.append(identifier.location);
117 }
118 }
119 identifier.descriptor = sha1(rawDescriptor);
Jeff Brown49ccac52012-04-11 18:27:33 -0700120 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
121 identifier.descriptor.string());
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700122}
123
Jeff Brown9ee285af2011-08-31 12:56:34 -0700124// --- Global Functions ---
125
126uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
127 // Touch devices get dibs on touch-related axes.
128 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
129 switch (axis) {
130 case ABS_X:
131 case ABS_Y:
132 case ABS_PRESSURE:
133 case ABS_TOOL_WIDTH:
134 case ABS_DISTANCE:
135 case ABS_TILT_X:
136 case ABS_TILT_Y:
137 case ABS_MT_SLOT:
138 case ABS_MT_TOUCH_MAJOR:
139 case ABS_MT_TOUCH_MINOR:
140 case ABS_MT_WIDTH_MAJOR:
141 case ABS_MT_WIDTH_MINOR:
142 case ABS_MT_ORIENTATION:
143 case ABS_MT_POSITION_X:
144 case ABS_MT_POSITION_Y:
145 case ABS_MT_TOOL_TYPE:
146 case ABS_MT_BLOB_ID:
147 case ABS_MT_TRACKING_ID:
148 case ABS_MT_PRESSURE:
149 case ABS_MT_DISTANCE:
150 return INPUT_DEVICE_CLASS_TOUCH;
151 }
152 }
153
154 // Joystick devices get the rest.
155 return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK;
156}
157
Jeff Brown90655042010-12-02 13:50:46 -0800158// --- EventHub::Device ---
159
160EventHub::Device::Device(int fd, int32_t id, const String8& path,
161 const InputDeviceIdentifier& identifier) :
162 next(NULL),
163 fd(fd), id(id), path(path), identifier(identifier),
Jeff Browna47425a2012-04-13 04:09:27 -0700164 classes(0), configuration(NULL), virtualKeyMap(NULL),
Michael Wrightac6c78b2013-07-17 13:21:45 -0700165 ffEffectPlaying(false), ffEffectId(-1), controllerNumber(0),
Jeff Brown4dac9012013-04-10 01:03:19 -0700166 timestampOverrideSec(0), timestampOverrideUsec(0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700167 memset(keyBitmask, 0, sizeof(keyBitmask));
168 memset(absBitmask, 0, sizeof(absBitmask));
169 memset(relBitmask, 0, sizeof(relBitmask));
170 memset(swBitmask, 0, sizeof(swBitmask));
171 memset(ledBitmask, 0, sizeof(ledBitmask));
Jeff Browna47425a2012-04-13 04:09:27 -0700172 memset(ffBitmask, 0, sizeof(ffBitmask));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700173 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174}
175
Jeff Brown90655042010-12-02 13:50:46 -0800176EventHub::Device::~Device() {
177 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800178 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800179 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180}
181
Jeff Brown90655042010-12-02 13:50:46 -0800182void EventHub::Device::close() {
183 if (fd >= 0) {
184 ::close(fd);
185 fd = -1;
186 }
187}
188
189
190// --- EventHub ---
191
Jeff Brown93fa9b32011-06-14 17:09:25 -0700192const uint32_t EventHub::EPOLL_ID_INOTIFY;
193const uint32_t EventHub::EPOLL_ID_WAKE;
194const int EventHub::EPOLL_SIZE_HINT;
195const int EventHub::EPOLL_MAX_EVENTS;
196
Jeff Brown90655042010-12-02 13:50:46 -0800197EventHub::EventHub(void) :
Michael Wrightac6c78b2013-07-17 13:21:45 -0700198 mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
Jeff Brown90655042010-12-02 13:50:46 -0800199 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700200 mNeedToSendFinishedDeviceScan(false),
201 mNeedToReopenDevices(false), mNeedToScanDevices(true),
202 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700204
Jeff Brown93fa9b32011-06-14 17:09:25 -0700205 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
206 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
207
208 mINotifyFd = inotify_init();
209 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
210 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
211 DEVICE_PATH, errno);
212
213 struct epoll_event eventItem;
214 memset(&eventItem, 0, sizeof(eventItem));
215 eventItem.events = EPOLLIN;
216 eventItem.data.u32 = EPOLL_ID_INOTIFY;
217 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
218 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
219
220 int wakeFds[2];
221 result = pipe(wakeFds);
222 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
223
224 mWakeReadPipeFd = wakeFds[0];
225 mWakeWritePipeFd = wakeFds[1];
226
227 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
228 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
229 errno);
230
231 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
232 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
233 errno);
234
235 eventItem.data.u32 = EPOLL_ID_WAKE;
236 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
237 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
238 errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239}
240
Jeff Brown90655042010-12-02 13:50:46 -0800241EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700242 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Jeff Brown93fa9b32011-06-14 17:09:25 -0700244 while (mClosingDevices) {
245 Device* device = mClosingDevices;
246 mClosingDevices = device->next;
247 delete device;
248 }
249
250 ::close(mEpollFd);
251 ::close(mINotifyFd);
252 ::close(mWakeReadPipeFd);
253 ::close(mWakeWritePipeFd);
254
255 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256}
257
Jeff Browne38fdfa2012-04-06 14:51:01 -0700258InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800260 Device* device = getDeviceLocked(deviceId);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700261 if (device == NULL) return InputDeviceIdentifier();
262 return device->identifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263}
264
Jeff Brown90655042010-12-02 13:50:46 -0800265uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800267 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 if (device == NULL) return 0;
269 return device->classes;
270}
271
Michael Wrightac6c78b2013-07-17 13:21:45 -0700272int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
273 AutoMutex _l(mLock);
274 Device* device = getDeviceLocked(deviceId);
275 if (device == NULL) return 0;
276 return device->controllerNumber;
277}
278
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800279void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800280 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800281 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800282 if (device && device->configuration) {
283 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800284 } else {
285 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800286 }
287}
288
Jeff Brown6d0fec22010-07-23 21:28:06 -0700289status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
290 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700291 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292
Jeff Brownba421dd2011-08-10 15:07:05 -0700293 if (axis >= 0 && axis <= ABS_MAX) {
294 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
Jeff Brownba421dd2011-08-10 15:07:05 -0700296 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700297 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700298 struct input_absinfo info;
299 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000300 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700301 axis, device->identifier.name.string(), device->fd, errno);
302 return -errno;
303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304
Jeff Brownba421dd2011-08-10 15:07:05 -0700305 if (info.minimum != info.maximum) {
306 outAxisInfo->valid = true;
307 outAxisInfo->minValue = info.minimum;
308 outAxisInfo->maxValue = info.maximum;
309 outAxisInfo->flat = info.flat;
310 outAxisInfo->fuzz = info.fuzz;
311 outAxisInfo->resolution = info.resolution;
312 }
313 return OK;
314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700316 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317}
318
Jeff Browncc0c1592011-02-19 05:07:28 -0800319bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
320 if (axis >= 0 && axis <= REL_MAX) {
321 AutoMutex _l(mLock);
322
323 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700324 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800325 return test_bit(axis, device->relBitmask);
326 }
327 }
328 return false;
329}
330
Jeff Brown80fd47c2011-05-24 01:07:44 -0700331bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
332 if (property >= 0 && property <= INPUT_PROP_MAX) {
333 AutoMutex _l(mLock);
334
335 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700336 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700337 return test_bit(property, device->propBitmask);
338 }
339 }
340 return false;
341}
342
Jeff Brown6d0fec22010-07-23 21:28:06 -0700343int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700344 if (scanCode >= 0 && scanCode <= KEY_MAX) {
345 AutoMutex _l(mLock);
346
Jeff Brown90655042010-12-02 13:50:46 -0800347 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700348 if (device && !device->isVirtual() && test_bit(scanCode, device->keyBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700349 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
350 memset(keyState, 0, sizeof(keyState));
351 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
352 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 }
355 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700356 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357}
358
Jeff Brown6d0fec22010-07-23 21:28:06 -0700359int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
360 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700361
Jeff Brown90655042010-12-02 13:50:46 -0800362 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700363 if (device && !device->isVirtual() && device->keyMap.haveKeyLayout()) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700364 Vector<int32_t> scanCodes;
365 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
366 if (scanCodes.size() != 0) {
367 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
368 memset(keyState, 0, sizeof(keyState));
369 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
370 for (size_t i = 0; i < scanCodes.size(); i++) {
371 int32_t sc = scanCodes.itemAt(i);
372 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
373 return AKEY_STATE_DOWN;
374 }
375 }
376 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 }
379 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700380 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700381}
382
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700384 if (sw >= 0 && sw <= SW_MAX) {
385 AutoMutex _l(mLock);
386
Jeff Brown90655042010-12-02 13:50:46 -0800387 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700388 if (device && !device->isVirtual() && test_bit(sw, device->swBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700389 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
390 memset(swState, 0, sizeof(swState));
391 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
392 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
393 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700394 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700395 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700396 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700397}
398
Jeff Brown2717eff2011-06-30 23:53:07 -0700399status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700400 *outValue = 0;
401
Jeff Brown2717eff2011-06-30 23:53:07 -0700402 if (axis >= 0 && axis <= ABS_MAX) {
403 AutoMutex _l(mLock);
404
405 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700406 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700407 struct input_absinfo info;
408 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000409 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700410 axis, device->identifier.name.string(), device->fd, errno);
411 return -errno;
412 }
413
414 *outValue = info.value;
415 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700416 }
417 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700418 return -1;
419}
420
Jeff Brown6d0fec22010-07-23 21:28:06 -0700421bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
422 const int32_t* keyCodes, uint8_t* outFlags) const {
423 AutoMutex _l(mLock);
424
Jeff Brown90655042010-12-02 13:50:46 -0800425 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700426 if (device && device->keyMap.haveKeyLayout()) {
427 Vector<int32_t> scanCodes;
428 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
429 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700430
Jeff Brownba421dd2011-08-10 15:07:05 -0700431 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
432 keyCodes[codeIndex], &scanCodes);
433 if (! err) {
434 // check the possible scan codes identified by the layout map against the
435 // map of codes actually emitted by the driver
436 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
437 if (test_bit(scanCodes[sc], device->keyBitmask)) {
438 outFlags[codeIndex] = 1;
439 break;
440 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441 }
442 }
443 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700444 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700445 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700446 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447}
448
Jeff Brown49ccac52012-04-11 18:27:33 -0700449status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
450 int32_t* outKeycode, uint32_t* outFlags) const {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700451 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800452 Device* device = getDeviceLocked(deviceId);
Jeff Brown49ccac52012-04-11 18:27:33 -0700453
Jeff Brown4a3862f2012-04-17 18:50:05 -0700454 if (device) {
455 // Check the key character map first.
456 sp<KeyCharacterMap> kcm = device->getKeyCharacterMap();
457 if (kcm != NULL) {
458 if (!kcm->mapKey(scanCode, usageCode, outKeycode)) {
459 *outFlags = 0;
460 return NO_ERROR;
461 }
462 }
463
464 // Check the key layout next.
465 if (device->keyMap.haveKeyLayout()) {
466 if (!device->keyMap.keyLayoutMap->mapKey(
467 scanCode, usageCode, outKeycode, outFlags)) {
468 return NO_ERROR;
469 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700470 }
471 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700472
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700473 *outKeycode = 0;
474 *outFlags = 0;
475 return NAME_NOT_FOUND;
476}
477
Jeff Brown49ccac52012-04-11 18:27:33 -0700478status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800479 AutoMutex _l(mLock);
480 Device* device = getDeviceLocked(deviceId);
481
482 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700483 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800484 if (err == NO_ERROR) {
485 return NO_ERROR;
486 }
487 }
488
Jeff Brown6f2fba42011-02-19 01:08:02 -0800489 return NAME_NOT_FOUND;
490}
491
Jeff Brown1a84fd12011-06-02 01:26:32 -0700492void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700493 AutoMutex _l(mLock);
494
Jeff Brown1a84fd12011-06-02 01:26:32 -0700495 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400496}
497
Jeff Brown49754db2011-07-01 17:37:58 -0700498bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
499 AutoMutex _l(mLock);
500 Device* device = getDeviceLocked(deviceId);
501 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
502 if (test_bit(scanCode, device->keyBitmask)) {
503 return true;
504 }
505 }
506 return false;
507}
508
Jeff Brown497a92c2010-09-12 17:55:08 -0700509bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
510 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800511 Device* device = getDeviceLocked(deviceId);
Michael Wrighted28fc82013-10-18 15:26:48 -0700512 int32_t sc;
513 if (device && mapLed(device, led, &sc) == NO_ERROR) {
514 if (test_bit(sc, device->ledBitmask)) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700515 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700516 }
517 }
518 return false;
519}
520
521void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
522 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800523 Device* device = getDeviceLocked(deviceId);
Michael Wrighted28fc82013-10-18 15:26:48 -0700524 setLedStateLocked(device, led, on);
525}
526
527void EventHub::setLedStateLocked(Device* device, int32_t led, bool on) {
528 int32_t sc;
529 if (device && !device->isVirtual() && mapLed(device, led, &sc) != NAME_NOT_FOUND) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700530 struct input_event ev;
531 ev.time.tv_sec = 0;
532 ev.time.tv_usec = 0;
533 ev.type = EV_LED;
Michael Wrighted28fc82013-10-18 15:26:48 -0700534 ev.code = sc;
Jeff Brown497a92c2010-09-12 17:55:08 -0700535 ev.value = on ? 1 : 0;
536
537 ssize_t nWrite;
538 do {
539 nWrite = write(device->fd, &ev, sizeof(struct input_event));
540 } while (nWrite == -1 && errno == EINTR);
541 }
542}
543
Jeff Brown90655042010-12-02 13:50:46 -0800544void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
545 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
546 outVirtualKeys.clear();
547
548 AutoMutex _l(mLock);
549 Device* device = getDeviceLocked(deviceId);
550 if (device && device->virtualKeyMap) {
551 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
552 }
553}
554
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700555sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Jeff Brown1e08fe92011-11-15 17:48:10 -0800556 AutoMutex _l(mLock);
557 Device* device = getDeviceLocked(deviceId);
558 if (device) {
Jeff Brown4a3862f2012-04-17 18:50:05 -0700559 return device->getKeyCharacterMap();
Jeff Brown1e08fe92011-11-15 17:48:10 -0800560 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700561 return NULL;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800562}
563
Jeff Brown6ec6f792012-04-17 16:52:41 -0700564bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId,
565 const sp<KeyCharacterMap>& map) {
566 AutoMutex _l(mLock);
567 Device* device = getDeviceLocked(deviceId);
568 if (device) {
569 if (map != device->overlayKeyMap) {
570 device->overlayKeyMap = map;
571 device->combinedKeyMap = KeyCharacterMap::combine(
572 device->keyMap.keyCharacterMap, map);
573 return true;
574 }
575 }
576 return false;
577}
578
RoboErikc1e00152013-12-11 17:02:46 -0800579static String8 generateDescriptor(InputDeviceIdentifier& identifier) {
580 String8 rawDescriptor;
581 rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor,
582 identifier.product);
583 // TODO add handling for USB devices to not uniqueify kbs that show up twice
584 if (!identifier.uniqueId.isEmpty()) {
585 rawDescriptor.append("uniqueId:");
586 rawDescriptor.append(identifier.uniqueId);
587 } else if (identifier.nonce != 0) {
588 rawDescriptor.appendFormat("nonce:%04x", identifier.nonce);
589 }
590
591 if (identifier.vendor == 0 && identifier.product == 0) {
592 // If we don't know the vendor and product id, then the device is probably
593 // built-in so we need to rely on other information to uniquely identify
594 // the input device. Usually we try to avoid relying on the device name or
595 // location but for built-in input device, they are unlikely to ever change.
596 if (!identifier.name.isEmpty()) {
597 rawDescriptor.append("name:");
598 rawDescriptor.append(identifier.name);
599 } else if (!identifier.location.isEmpty()) {
600 rawDescriptor.append("location:");
601 rawDescriptor.append(identifier.location);
602 }
603 }
604 identifier.descriptor = sha1(rawDescriptor);
605 return rawDescriptor;
606}
607
608void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
609 // Compute a device descriptor that uniquely identifies the device.
610 // The descriptor is assumed to be a stable identifier. Its value should not
611 // change between reboots, reconnections, firmware updates or new releases
612 // of Android. In practice we sometimes get devices that cannot be uniquely
613 // identified. In this case we enforce uniqueness between connected devices.
614 // Ideally, we also want the descriptor to be short and relatively opaque.
615
616 identifier.nonce = 0;
617 String8 rawDescriptor = generateDescriptor(identifier);
618 if (identifier.uniqueId.isEmpty()) {
619 // If it didn't have a unique id check for conflicts and enforce
620 // uniqueness if necessary.
621 while(getDeviceByDescriptorLocked(identifier.descriptor) != NULL) {
622 identifier.nonce++;
623 rawDescriptor = generateDescriptor(identifier);
624 }
625 }
626 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
627 identifier.descriptor.string());
628}
629
Jeff Browna47425a2012-04-13 04:09:27 -0700630void EventHub::vibrate(int32_t deviceId, nsecs_t duration) {
631 AutoMutex _l(mLock);
632 Device* device = getDeviceLocked(deviceId);
633 if (device && !device->isVirtual()) {
634 ff_effect effect;
635 memset(&effect, 0, sizeof(effect));
636 effect.type = FF_RUMBLE;
637 effect.id = device->ffEffectId;
638 effect.u.rumble.strong_magnitude = 0xc000;
639 effect.u.rumble.weak_magnitude = 0xc000;
640 effect.replay.length = (duration + 999999LL) / 1000000LL;
641 effect.replay.delay = 0;
642 if (ioctl(device->fd, EVIOCSFF, &effect)) {
643 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
644 device->identifier.name.string(), errno);
645 return;
646 }
647 device->ffEffectId = effect.id;
648
649 struct input_event ev;
650 ev.time.tv_sec = 0;
651 ev.time.tv_usec = 0;
652 ev.type = EV_FF;
653 ev.code = device->ffEffectId;
654 ev.value = 1;
655 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
656 ALOGW("Could not start force feedback effect on device %s due to error %d.",
657 device->identifier.name.string(), errno);
658 return;
659 }
660 device->ffEffectPlaying = true;
661 }
662}
663
664void EventHub::cancelVibrate(int32_t deviceId) {
665 AutoMutex _l(mLock);
666 Device* device = getDeviceLocked(deviceId);
667 if (device && !device->isVirtual()) {
668 if (device->ffEffectPlaying) {
669 device->ffEffectPlaying = false;
670
671 struct input_event ev;
672 ev.time.tv_sec = 0;
673 ev.time.tv_usec = 0;
674 ev.type = EV_FF;
675 ev.code = device->ffEffectId;
676 ev.value = 0;
677 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
678 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
679 device->identifier.name.string(), errno);
680 return;
681 }
682 }
683 }
684}
685
RoboErikc1e00152013-12-11 17:02:46 -0800686EventHub::Device* EventHub::getDeviceByDescriptorLocked(String8& descriptor) const {
687 size_t size = mDevices.size();
688 for (size_t i = 0; i < size; i++) {
689 Device* device = mDevices.valueAt(i);
690 if (descriptor.compare(device->identifier.descriptor) == 0) {
691 return device;
692 }
693 }
694 return NULL;
695}
696
Jeff Brown90655042010-12-02 13:50:46 -0800697EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700698 if (deviceId == BUILT_IN_KEYBOARD_ID) {
Jeff Brown90655042010-12-02 13:50:46 -0800699 deviceId = mBuiltInKeyboardId;
700 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700701 ssize_t index = mDevices.indexOfKey(deviceId);
702 return index >= 0 ? mDevices.valueAt(index) : NULL;
703}
Jeff Brown90655042010-12-02 13:50:46 -0800704
Jeff Brown93fa9b32011-06-14 17:09:25 -0700705EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
706 for (size_t i = 0; i < mDevices.size(); i++) {
707 Device* device = mDevices.valueAt(i);
708 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800709 return device;
710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712 return NULL;
713}
714
Jeff Brownb7198742011-03-18 18:14:26 -0700715size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Steve Blockec193de2012-01-09 18:35:44 +0000716 ALOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400717
Jeff Brown93fa9b32011-06-14 17:09:25 -0700718 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400719
Jeff Brownb7198742011-03-18 18:14:26 -0700720 struct input_event readBuffer[bufferSize];
721
722 RawEvent* event = buffer;
723 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700724 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700725 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700726 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
727
Jeff Brown1a84fd12011-06-02 01:26:32 -0700728 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700729 if (mNeedToReopenDevices) {
730 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700731
Steve Block6215d3f2012-01-04 20:05:49 +0000732 ALOGI("Reopening all input devices due to a configuration change.");
Jeff Brown1a84fd12011-06-02 01:26:32 -0700733
Jeff Brown93fa9b32011-06-14 17:09:25 -0700734 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700735 mNeedToScanDevices = true;
736 break; // return to the caller before we actually rescan
737 }
738
Jeff Browncc2e7172010-08-17 16:48:25 -0700739 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700740 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800741 Device* device = mClosingDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100742 ALOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 device->id, device->path.string());
744 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700745 event->when = now;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700746 event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
Jeff Brownb7198742011-03-18 18:14:26 -0700747 event->type = DEVICE_REMOVED;
748 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700750 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700751 if (--capacity == 0) {
752 break;
753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700755
Jeff Brown1a84fd12011-06-02 01:26:32 -0700756 if (mNeedToScanDevices) {
757 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700758 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700759 mNeedToSendFinishedDeviceScan = true;
760 }
761
Jeff Brownb7198742011-03-18 18:14:26 -0700762 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800763 Device* device = mOpeningDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100764 ALOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 device->id, device->path.string());
766 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700767 event->when = now;
768 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
769 event->type = DEVICE_ADDED;
770 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700771 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700772 if (--capacity == 0) {
773 break;
774 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700775 }
776
777 if (mNeedToSendFinishedDeviceScan) {
778 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700779 event->when = now;
780 event->type = FINISHED_DEVICE_SCAN;
781 event += 1;
782 if (--capacity == 0) {
783 break;
784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
Jeff Browncc2e7172010-08-17 16:48:25 -0700787 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700788 bool deviceChanged = false;
789 while (mPendingEventIndex < mPendingEventCount) {
790 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
791 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
792 if (eventItem.events & EPOLLIN) {
793 mPendingINotify = true;
794 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000795 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700796 }
797 continue;
798 }
799
800 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
801 if (eventItem.events & EPOLLIN) {
Steve Block71f2cf12011-10-20 11:56:00 +0100802 ALOGV("awoken after wake()");
Jeff Brown93fa9b32011-06-14 17:09:25 -0700803 awoken = true;
804 char buffer[16];
805 ssize_t nRead;
806 do {
807 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
808 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
809 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000810 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700811 eventItem.events);
812 }
813 continue;
814 }
815
816 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
817 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000818 ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700819 eventItem.events, eventItem.data.u32);
820 continue;
821 }
822
823 Device* device = mDevices.valueAt(deviceIndex);
824 if (eventItem.events & EPOLLIN) {
825 int32_t readSize = read(device->fd, readBuffer,
826 sizeof(struct input_event) * capacity);
827 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
828 // Device was removed before INotify noticed.
Jeff Brown41305542011-10-05 11:14:13 -0700829 ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d "
Narayan Kamath22d07462014-03-27 12:50:58 +0000830 "capacity: %zu errno: %d)\n",
Jeff Brown41305542011-10-05 11:14:13 -0700831 device->fd, readSize, bufferSize, capacity, errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700832 deviceChanged = true;
833 closeDeviceLocked(device);
834 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700835 if (errno != EAGAIN && errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000836 ALOGW("could not get event (errno=%d)", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700837 }
838 } else if ((readSize % sizeof(struct input_event)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000839 ALOGE("could not get event (wrong size: %d)", readSize);
Jeff Browncc2e7172010-08-17 16:48:25 -0700840 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700841 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
842
843 size_t count = size_t(readSize) / sizeof(struct input_event);
844 for (size_t i = 0; i < count; i++) {
Jeff Brown4dac9012013-04-10 01:03:19 -0700845 struct input_event& iev = readBuffer[i];
846 ALOGV("%s got: time=%d.%06d, type=%d, code=%d, value=%d",
JP Abgrall25a465b2012-05-16 10:33:49 -0700847 device->path.string(),
848 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
849 iev.type, iev.code, iev.value);
850
Jeff Brown4dac9012013-04-10 01:03:19 -0700851 // Some input devices may have a better concept of the time
852 // when an input event was actually generated than the kernel
853 // which simply timestamps all events on entry to evdev.
854 // This is a custom Android extension of the input protocol
855 // mainly intended for use with uinput based device drivers.
856 if (iev.type == EV_MSC) {
857 if (iev.code == MSC_ANDROID_TIME_SEC) {
858 device->timestampOverrideSec = iev.value;
859 continue;
860 } else if (iev.code == MSC_ANDROID_TIME_USEC) {
861 device->timestampOverrideUsec = iev.value;
862 continue;
863 }
864 }
865 if (device->timestampOverrideSec || device->timestampOverrideUsec) {
866 iev.time.tv_sec = device->timestampOverrideSec;
867 iev.time.tv_usec = device->timestampOverrideUsec;
868 if (iev.type == EV_SYN && iev.code == SYN_REPORT) {
869 device->timestampOverrideSec = 0;
870 device->timestampOverrideUsec = 0;
871 }
872 ALOGV("applied override time %d.%06d",
873 int(iev.time.tv_sec), int(iev.time.tv_usec));
874 }
875
Jeff Brown4e91a182011-04-07 11:38:09 -0700876#ifdef HAVE_POSIX_CLOCKS
877 // Use the time specified in the event instead of the current time
878 // so that downstream code can get more accurate estimates of
879 // event dispatch latency from the time the event is enqueued onto
880 // the evdev client buffer.
881 //
882 // The event's timestamp fortuitously uses the same monotonic clock
883 // time base as the rest of Android. The kernel event device driver
884 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
885 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
886 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
887 // system call that also queries ktime_get_ts().
888 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
889 + nsecs_t(iev.time.tv_usec) * 1000LL;
JP Abgrall25a465b2012-05-16 10:33:49 -0700890 ALOGV("event time %lld, now %lld", event->when, now);
Jeff Brownf33b2b22012-10-05 17:59:56 -0700891
892 // Bug 7291243: Add a guard in case the kernel generates timestamps
893 // that appear to be far into the future because they were generated
894 // using the wrong clock source.
895 //
896 // This can happen because when the input device is initially opened
897 // it has a default clock source of CLOCK_REALTIME. Any input events
898 // enqueued right after the device is opened will have timestamps
899 // generated using CLOCK_REALTIME. We later set the clock source
900 // to CLOCK_MONOTONIC but it is already too late.
901 //
902 // Invalid input event timestamps can result in ANRs, crashes and
903 // and other issues that are hard to track down. We must not let them
904 // propagate through the system.
905 //
906 // Log a warning so that we notice the problem and recover gracefully.
907 if (event->when >= now + 10 * 1000000000LL) {
908 // Double-check. Time may have moved on.
909 nsecs_t time = systemTime(SYSTEM_TIME_MONOTONIC);
910 if (event->when > time) {
911 ALOGW("An input event from %s has a timestamp that appears to "
912 "have been generated using the wrong clock source "
913 "(expected CLOCK_MONOTONIC): "
914 "event time %lld, current time %lld, call time %lld. "
915 "Using current time instead.",
916 device->path.string(), event->when, time, now);
917 event->when = time;
918 } else {
919 ALOGV("Event time is ok but failed the fast path and required "
920 "an extra call to systemTime: "
921 "event time %lld, current time %lld, call time %lld.",
922 event->when, time, now);
923 }
924 }
Jeff Brown4e91a182011-04-07 11:38:09 -0700925#else
Jeff Brownb7198742011-03-18 18:14:26 -0700926 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700927#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700928 event->deviceId = deviceId;
929 event->type = iev.type;
Jeff Brown49ccac52012-04-11 18:27:33 -0700930 event->code = iev.code;
Jeff Brownb7198742011-03-18 18:14:26 -0700931 event->value = iev.value;
Jeff Brownb7198742011-03-18 18:14:26 -0700932 event += 1;
Jeff Brown4dac9012013-04-10 01:03:19 -0700933 capacity -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700934 }
Jeff Brownb7198742011-03-18 18:14:26 -0700935 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700936 // The result buffer is full. Reset the pending event index
937 // so we will try to read the device again on the next iteration.
938 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700939 break;
940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700942 } else if (eventItem.events & EPOLLHUP) {
943 ALOGI("Removing device %s due to epoll hang-up event.",
944 device->identifier.name.string());
945 deviceChanged = true;
946 closeDeviceLocked(device);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700947 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000948 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700949 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700952
Jeff Brown93fa9b32011-06-14 17:09:25 -0700953 // readNotify() will modify the list of devices so this must be done after
954 // processing all other events to ensure that we read all remaining events
955 // before closing the devices.
956 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
957 mPendingINotify = false;
958 readNotifyLocked();
959 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800960 }
961
Jeff Brown93fa9b32011-06-14 17:09:25 -0700962 // Report added or removed devices immediately.
963 if (deviceChanged) {
964 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
Jeff Browna9b84222010-10-14 02:23:43 -0700966
Jeff Brown93fa9b32011-06-14 17:09:25 -0700967 // Return now if we have collected any events or if we were explicitly awoken.
968 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700969 break;
970 }
971
Jeff Browncc2e7172010-08-17 16:48:25 -0700972 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700973 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700974 // subtle choreography. When a device driver has pending (unread) events, it acquires
975 // a kernel wake lock. However, once the last pending event has been read, the device
976 // driver will release the kernel wake lock. To prevent the system from going to sleep
977 // when this happens, the EventHub holds onto its own user wake lock while the client
978 // is processing events. Thus the system can only sleep if there are no events
979 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700980 //
981 // The timeout is advisory only. If the device is asleep, it will not wake just to
982 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700983 mPendingEventIndex = 0;
984
985 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700986 release_wake_lock(WAKE_LOCK_ID);
987
Jeff Brown93fa9b32011-06-14 17:09:25 -0700988 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700989
990 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700991 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700992
Jeff Brownaa3855d2011-03-17 01:34:19 -0700993 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700994 // Timed out.
995 mPendingEventCount = 0;
996 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700997 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700998
Jeff Brownaa3855d2011-03-17 01:34:19 -0700999 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001000 // An error occurred.
1001 mPendingEventCount = 0;
1002
Jeff Brownb7198742011-03-18 18:14:26 -07001003 // Sleep after errors to avoid locking up the system.
1004 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -07001005 if (errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +00001006 ALOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -07001007 usleep(100000);
1008 }
Jeff Brownb7198742011-03-18 18:14:26 -07001009 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001010 // Some events occurred.
1011 mPendingEventCount = size_t(pollResult);
Jeff Browncc2e7172010-08-17 16:48:25 -07001012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
Jeff Brownb7198742011-03-18 18:14:26 -07001014
1015 // All done, return the number of events we read.
1016 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017}
1018
Jeff Brown93fa9b32011-06-14 17:09:25 -07001019void EventHub::wake() {
Steve Block71f2cf12011-10-20 11:56:00 +01001020 ALOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021
Jeff Brown93fa9b32011-06-14 17:09:25 -07001022 ssize_t nWrite;
1023 do {
1024 nWrite = write(mWakeWritePipeFd, "W", 1);
1025 } while (nWrite == -1 && errno == EINTR);
1026
1027 if (nWrite != 1 && errno != EAGAIN) {
Steve Block8564c8d2012-01-05 23:22:43 +00001028 ALOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
Jeff Brown1a84fd12011-06-02 01:26:32 -07001030}
Jeff Brown90655042010-12-02 13:50:46 -08001031
Jeff Brown93fa9b32011-06-14 17:09:25 -07001032void EventHub::scanDevicesLocked() {
1033 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 if(res < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001035 ALOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001037 if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) {
1038 createVirtualKeyboardLocked();
1039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040}
1041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042// ----------------------------------------------------------------------------
1043
Jeff Brownfd0358292010-06-30 16:10:35 -07001044static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
1045 const uint8_t* end = array + endIndex;
1046 array += startIndex;
1047 while (array != end) {
1048 if (*(array++) != 0) {
1049 return true;
1050 }
1051 }
1052 return false;
1053}
1054
1055static const int32_t GAMEPAD_KEYCODES[] = {
1056 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
1057 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
1058 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
1059 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
1060 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -08001061 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
Jeff Brownfd0358292010-06-30 16:10:35 -07001062};
1063
Jeff Brown93fa9b32011-06-14 17:09:25 -07001064status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -08001065 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066
Steve Block71f2cf12011-10-20 11:56:00 +01001067 ALOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068
Jeff Brown874c1e92012-01-19 14:32:47 -08001069 int fd = open(devicePath, O_RDWR | O_CLOEXEC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 if(fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001071 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 return -1;
1073 }
1074
Jeff Brown90655042010-12-02 13:50:46 -08001075 InputDeviceIdentifier identifier;
1076
1077 // Get device name.
1078 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
1079 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
1080 } else {
1081 buffer[sizeof(buffer) - 1] = '\0';
1082 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
Mike Lockwood15431a92009-07-17 00:10:10 -04001084
Jeff Brown90655042010-12-02 13:50:46 -08001085 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -07001086 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
1087 const String8& item = mExcludedDevices.itemAt(i);
1088 if (identifier.name == item) {
Steve Block6215d3f2012-01-04 20:05:49 +00001089 ALOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -04001090 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -04001091 return -1;
1092 }
1093 }
1094
Jeff Brown90655042010-12-02 13:50:46 -08001095 // Get device driver version.
1096 int driverVersion;
1097 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Steve Block3762c312012-01-06 19:20:56 +00001098 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001099 close(fd);
1100 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102
Jeff Brown90655042010-12-02 13:50:46 -08001103 // Get device identifier.
1104 struct input_id inputId;
1105 if(ioctl(fd, EVIOCGID, &inputId)) {
Steve Block3762c312012-01-06 19:20:56 +00001106 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001107 close(fd);
1108 return -1;
1109 }
1110 identifier.bus = inputId.bustype;
1111 identifier.product = inputId.product;
1112 identifier.vendor = inputId.vendor;
1113 identifier.version = inputId.version;
1114
1115 // Get device physical location.
1116 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
1117 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
1118 } else {
1119 buffer[sizeof(buffer) - 1] = '\0';
1120 identifier.location.setTo(buffer);
1121 }
1122
1123 // Get device unique id.
1124 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
1125 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
1126 } else {
1127 buffer[sizeof(buffer) - 1] = '\0';
1128 identifier.uniqueId.setTo(buffer);
1129 }
1130
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001131 // Fill in the descriptor.
RoboErikc1e00152013-12-11 17:02:46 -08001132 assignDescriptorLocked(identifier);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001133
Jeff Brown90655042010-12-02 13:50:46 -08001134 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -07001135 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
Steve Block3762c312012-01-06 19:20:56 +00001136 ALOGE("Error %d making device file descriptor non-blocking.", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -07001137 close(fd);
1138 return -1;
1139 }
1140
Jeff Brown90655042010-12-02 13:50:46 -08001141 // Allocate device. (The device object takes ownership of the fd at this point.)
1142 int32_t deviceId = mNextDeviceId++;
1143 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144
Jeff Browne38fdfa2012-04-06 14:51:01 -07001145 ALOGV("add device %d: %s\n", deviceId, devicePath);
1146 ALOGV(" bus: %04x\n"
1147 " vendor %04x\n"
1148 " product %04x\n"
1149 " version %04x\n",
Jeff Brown90655042010-12-02 13:50:46 -08001150 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001151 ALOGV(" name: \"%s\"\n", identifier.name.string());
1152 ALOGV(" location: \"%s\"\n", identifier.location.string());
1153 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string());
Jeff Brown49ccac52012-04-11 18:27:33 -07001154 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001155 ALOGV(" driver: v%d.%d.%d\n",
Jeff Brown90655042010-12-02 13:50:46 -08001156 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001158 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001159 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001160
Jeff Brownfd0358292010-06-30 16:10:35 -07001161 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001162 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
1163 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
1164 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
1165 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
1166 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
Jeff Browna47425a2012-04-13 04:09:27 -07001167 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001168 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -08001169
Jeff Brown6f2fba42011-02-19 01:08:02 -08001170 // See if this is a keyboard. Ignore everything in the button range except for
1171 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001172 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
1173 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -08001174 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001175 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001176 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -07001177 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001178 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -08001179 if (haveKeyboardKeys || haveGamepadButtons) {
1180 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001182
Jeff Brown83c09682010-12-23 17:50:18 -08001183 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001184 if (test_bit(BTN_MOUSE, device->keyBitmask)
1185 && test_bit(REL_X, device->relBitmask)
1186 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001187 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001189
1190 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -08001191 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001192 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
1193 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001194 // Some joysticks such as the PS3 controller report axes that conflict
1195 // with the ABS_MT range. Try to confirm that the device really is
1196 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001197 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001198 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -07001199 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001200 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001201 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
1202 && test_bit(ABS_X, device->absBitmask)
1203 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001204 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001207 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001208 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1209 // from other devices such as accelerometers that also have absolute axes.
Jeff Brown9ee285af2011-08-31 12:56:34 -07001210 if (haveGamepadButtons) {
1211 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
1212 for (int i = 0; i <= ABS_MAX; i++) {
1213 if (test_bit(i, device->absBitmask)
1214 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
1215 device->classes = assumedClasses;
1216 break;
1217 }
1218 }
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001219 }
1220
Jeff Brown93fa9b32011-06-14 17:09:25 -07001221 // Check whether this device has switches.
1222 for (int i = 0; i <= SW_MAX; i++) {
1223 if (test_bit(i, device->swBitmask)) {
1224 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
1225 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228
Jeff Browna47425a2012-04-13 04:09:27 -07001229 // Check whether this device supports the vibrator.
1230 if (test_bit(FF_RUMBLE, device->ffBitmask)) {
1231 device->classes |= INPUT_DEVICE_CLASS_VIBRATOR;
1232 }
1233
Jeff Brown93fa9b32011-06-14 17:09:25 -07001234 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -08001235 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -08001236 // Load the virtual keys for the touch screen, if any.
1237 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001238 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -08001239 if (!status) {
1240 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
Jeff Brown90655042010-12-02 13:50:46 -08001242 }
1243
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001244 // Load the key map.
1245 // We need to do this for joysticks too because the key layout may specify axes.
1246 status_t keyMapStatus = NAME_NOT_FOUND;
1247 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -08001248 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001249 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001250 }
Jeff Brown90655042010-12-02 13:50:46 -08001251
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001252 // Configure the keyboard, gamepad or virtual keyboard.
1253 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001254 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001255 if (!keyMapStatus
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001256 && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD
Jeff Brown90655042010-12-02 13:50:46 -08001257 && isEligibleBuiltInKeyboard(device->identifier,
1258 device->configuration, &device->keyMap)) {
1259 mBuiltInKeyboardId = device->id;
Jeff Brown497a92c2010-09-12 17:55:08 -07001260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261
Ken Wakasa02a44f72013-07-05 04:08:36 +00001262 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
1263 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
1264 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
1265 }
1266
Jeff Brownfd0358292010-06-30 16:10:35 -07001267 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -07001268 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1269 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1270 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1271 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1272 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001273 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001274 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001275
Jeff Brownfd0358292010-06-30 16:10:35 -07001276 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001277 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001278 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -07001279 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1280 break;
1281 }
1282 }
Michael Wrighta0a72852013-02-21 23:51:45 -08001283
1284 // Disable kernel key repeat since we handle it ourselves
1285 unsigned int repeatRate[] = {0,0};
1286 if (ioctl(fd, EVIOCSREP, repeatRate)) {
1287 ALOGW("Unable to disable kernel key repeat for %s: %s", devicePath, strerror(errno));
1288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290
Sean McNeilaeb00c42010-06-23 16:00:37 +07001291 // If the device isn't recognized as something we handle, don't monitor it.
1292 if (device->classes == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001293 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Jeff Brown90655042010-12-02 13:50:46 -08001294 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001295 delete device;
1296 return -1;
1297 }
1298
Jeff Brown56194eb2011-03-02 19:23:13 -08001299 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001300 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001301 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1302 }
1303
Michael Wrightb0aa4822014-03-12 12:56:51 -07001304 if (device->classes & (INPUT_DEVICE_CLASS_JOYSTICK | INPUT_DEVICE_CLASS_DPAD)
1305 && device->classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Michael Wrightac6c78b2013-07-17 13:21:45 -07001306 device->controllerNumber = getNextControllerNumberLocked(device);
Michael Wrighted28fc82013-10-18 15:26:48 -07001307 setLedForController(device);
Michael Wrightac6c78b2013-07-17 13:21:45 -07001308 }
1309
Jeff Brown93fa9b32011-06-14 17:09:25 -07001310 // Register with epoll.
1311 struct epoll_event eventItem;
1312 memset(&eventItem, 0, sizeof(eventItem));
1313 eventItem.events = EPOLLIN;
1314 eventItem.data.u32 = deviceId;
1315 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
Steve Block3762c312012-01-06 19:20:56 +00001316 ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001317 delete device;
1318 return -1;
1319 }
1320
Jeff Browne22afbe2011-12-16 13:45:40 -08001321 // Enable wake-lock behavior on kernels that support it.
1322 // TODO: Only need this for devices that can really wake the system.
Elliott Hughes6a2e9bc2013-11-12 13:16:37 -08001323#ifndef EVIOCSSUSPENDBLOCK
1324 // uapi headers don't include EVIOCSSUSPENDBLOCK, and future kernels
1325 // will use an epoll flag instead, so as long as we want to support
1326 // this feature, we need to be prepared to define the ioctl ourselves.
1327#define EVIOCSSUSPENDBLOCK _IOW('E', 0x91, int)
1328#endif
Jeff Browneca3cf52012-04-06 19:31:36 -07001329 bool usingSuspendBlockIoctl = !ioctl(fd, EVIOCSSUSPENDBLOCK, 1);
1330
1331 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
1332 // associated with input events. This is important because the input system
1333 // uses the timestamps extensively and assumes they were recorded using the monotonic
1334 // clock.
1335 //
1336 // In older kernel, before Linux 3.4, there was no way to tell the kernel which
1337 // clock to use to input event timestamps. The standard kernel behavior was to
1338 // record a real time timestamp, which isn't what we want. Android kernels therefore
1339 // contained a patch to the evdev_event() function in drivers/input/evdev.c to
1340 // replace the call to do_gettimeofday() with ktime_get_ts() to cause the monotonic
1341 // clock to be used instead of the real time clock.
1342 //
1343 // As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock.
1344 // Therefore, we no longer require the Android-specific kernel patch described above
1345 // as long as we make sure to set select the monotonic clock. We do that here.
Jeff Browna75fe052012-05-01 18:41:26 -07001346 int clockId = CLOCK_MONOTONIC;
1347 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId);
Jeff Browne22afbe2011-12-16 13:45:40 -08001348
Steve Block6215d3f2012-01-04 20:05:49 +00001349 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
Jeff Browne22afbe2011-12-16 13:45:40 -08001350 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
Jeff Browneca3cf52012-04-06 19:31:36 -07001351 "usingSuspendBlockIoctl=%s, usingClockIoctl=%s",
Jeff Brown90655042010-12-02 13:50:46 -08001352 deviceId, fd, devicePath, device->identifier.name.string(),
1353 device->classes,
1354 device->configurationFile.string(),
1355 device->keyMap.keyLayoutFile.string(),
1356 device->keyMap.keyCharacterMapFile.string(),
Jeff Browne22afbe2011-12-16 13:45:40 -08001357 toString(mBuiltInKeyboardId == deviceId),
Jeff Browneca3cf52012-04-06 19:31:36 -07001358 toString(usingSuspendBlockIoctl), toString(usingClockIoctl));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001359
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001360 addDeviceLocked(device);
1361 return 0;
1362}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001364void EventHub::createVirtualKeyboardLocked() {
1365 InputDeviceIdentifier identifier;
1366 identifier.name = "Virtual";
1367 identifier.uniqueId = "<virtual>";
RoboErikc1e00152013-12-11 17:02:46 -08001368 assignDescriptorLocked(identifier);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001369
1370 Device* device = new Device(-1, VIRTUAL_KEYBOARD_ID, String8("<virtual>"), identifier);
1371 device->classes = INPUT_DEVICE_CLASS_KEYBOARD
1372 | INPUT_DEVICE_CLASS_ALPHAKEY
1373 | INPUT_DEVICE_CLASS_DPAD
1374 | INPUT_DEVICE_CLASS_VIRTUAL;
1375 loadKeyMapLocked(device);
1376 addDeviceLocked(device);
1377}
1378
1379void EventHub::addDeviceLocked(Device* device) {
1380 mDevices.add(device->id, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 device->next = mOpeningDevices;
1382 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383}
1384
Jeff Brown93fa9b32011-06-14 17:09:25 -07001385void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001386 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1387 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001388 if (device->configurationFile.isEmpty()) {
Steve Block5baa3a62011-12-20 16:23:08 +00001389 ALOGD("No input device configuration file found for device '%s'.",
Jeff Brown90655042010-12-02 13:50:46 -08001390 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001391 } else {
1392 status_t status = PropertyMap::load(device->configurationFile,
1393 &device->configuration);
1394 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001395 ALOGE("Error loading input device configuration file for device '%s'. "
Jeff Brown90655042010-12-02 13:50:46 -08001396 "Using default configuration.",
1397 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001398 }
1399 }
1400}
1401
Jeff Brown93fa9b32011-06-14 17:09:25 -07001402status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001403 // The virtual key map is supplied by the kernel as a system board property file.
1404 String8 path;
1405 path.append("/sys/board_properties/virtualkeys.");
1406 path.append(device->identifier.name);
1407 if (access(path.string(), R_OK)) {
1408 return NAME_NOT_FOUND;
1409 }
1410 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001411}
1412
Jeff Brown93fa9b32011-06-14 17:09:25 -07001413status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001414 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001415}
1416
Jeff Brown93fa9b32011-06-14 17:09:25 -07001417bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001418 if (device->configuration) {
1419 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001420 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1421 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001422 }
1423 }
1424 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1425}
1426
Michael Wrightac6c78b2013-07-17 13:21:45 -07001427int32_t EventHub::getNextControllerNumberLocked(Device* device) {
1428 if (mControllerNumbers.isFull()) {
1429 ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
1430 device->identifier.name.string());
1431 return 0;
1432 }
1433 // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
1434 // one
1435 return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
1436}
1437
1438void EventHub::releaseControllerNumberLocked(Device* device) {
1439 int32_t num = device->controllerNumber;
1440 device->controllerNumber= 0;
1441 if (num == 0) {
1442 return;
1443 }
1444 mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
1445}
1446
Michael Wrighted28fc82013-10-18 15:26:48 -07001447void EventHub::setLedForController(Device* device) {
1448 for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) {
1449 setLedStateLocked(device, ALED_CONTROLLER_1 + i, device->controllerNumber == i + 1);
1450 }
1451}
Michael Wrightac6c78b2013-07-17 13:21:45 -07001452
Jeff Brown90655042010-12-02 13:50:46 -08001453bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1454 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001455 return false;
1456 }
1457
1458 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001459 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001460 const size_t N = scanCodes.size();
1461 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1462 int32_t sc = scanCodes.itemAt(i);
1463 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1464 return true;
1465 }
1466 }
1467
1468 return false;
1469}
1470
Michael Wrighted28fc82013-10-18 15:26:48 -07001471status_t EventHub::mapLed(Device* device, int32_t led, int32_t* outScanCode) const {
1472 if (!device->keyMap.haveKeyLayout() || !device->ledBitmask) {
1473 return NAME_NOT_FOUND;
1474 }
1475
1476 int32_t scanCode;
1477 if(device->keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) {
1478 if(scanCode >= 0 && scanCode <= LED_MAX && test_bit(scanCode, device->ledBitmask)) {
1479 *outScanCode = scanCode;
1480 return NO_ERROR;
1481 }
1482 }
1483 return NAME_NOT_FOUND;
1484}
1485
Jeff Brown93fa9b32011-06-14 17:09:25 -07001486status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1487 Device* device = getDeviceByPathLocked(devicePath);
1488 if (device) {
1489 closeDeviceLocked(device);
1490 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
Steve Block71f2cf12011-10-20 11:56:00 +01001492 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 return -1;
1494}
1495
Jeff Brown93fa9b32011-06-14 17:09:25 -07001496void EventHub::closeAllDevicesLocked() {
1497 while (mDevices.size() > 0) {
1498 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1499 }
1500}
1501
1502void EventHub::closeDeviceLocked(Device* device) {
Steve Block6215d3f2012-01-04 20:05:49 +00001503 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001504 device->path.string(), device->identifier.name.string(), device->id,
1505 device->fd, device->classes);
1506
Jeff Brown33bbfd22011-02-24 20:55:35 -08001507 if (device->id == mBuiltInKeyboardId) {
Steve Block8564c8d2012-01-05 23:22:43 +00001508 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001509 device->path.string(), mBuiltInKeyboardId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001510 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001511 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001512
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001513 if (!device->isVirtual()) {
1514 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
1515 ALOGW("Could not remove device fd from epoll instance. errno=%d", errno);
1516 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001517 }
1518
Michael Wrightac6c78b2013-07-17 13:21:45 -07001519 releaseControllerNumberLocked(device);
1520
Jeff Brown93fa9b32011-06-14 17:09:25 -07001521 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001522 device->close();
1523
Jeff Brown8e9d4432011-03-12 19:46:59 -08001524 // Unlink for opening devices list if it is present.
1525 Device* pred = NULL;
1526 bool found = false;
1527 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1528 if (entry == device) {
1529 found = true;
1530 break;
1531 }
1532 pred = entry;
1533 entry = entry->next;
1534 }
1535 if (found) {
1536 // Unlink the device from the opening devices list then delete it.
1537 // We don't need to tell the client that the device was closed because
1538 // it does not even know it was opened in the first place.
Steve Block6215d3f2012-01-04 20:05:49 +00001539 ALOGI("Device %s was immediately closed after opening.", device->path.string());
Jeff Brown8e9d4432011-03-12 19:46:59 -08001540 if (pred) {
1541 pred->next = device->next;
1542 } else {
1543 mOpeningDevices = device->next;
1544 }
1545 delete device;
1546 } else {
1547 // Link into closing devices list.
1548 // The device will be deleted later after we have informed the client.
1549 device->next = mClosingDevices;
1550 mClosingDevices = device;
1551 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001552}
1553
Jeff Brown93fa9b32011-06-14 17:09:25 -07001554status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 int res;
1556 char devname[PATH_MAX];
1557 char *filename;
1558 char event_buf[512];
1559 int event_size;
1560 int event_pos = 0;
1561 struct inotify_event *event;
1562
Steve Block71f2cf12011-10-20 11:56:00 +01001563 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001564 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 if(res < (int)sizeof(*event)) {
1566 if(errno == EINTR)
1567 return 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001568 ALOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001569 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571 //printf("got %d bytes of event information\n", res);
1572
Jeff Brown90655042010-12-02 13:50:46 -08001573 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 filename = devname + strlen(devname);
1575 *filename++ = '/';
1576
1577 while(res >= (int)sizeof(*event)) {
1578 event = (struct inotify_event *)(event_buf + event_pos);
1579 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1580 if(event->len) {
1581 strcpy(filename, event->name);
1582 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001583 openDeviceLocked(devname);
1584 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00001585 ALOGI("Removing device '%s' due to inotify event\n", devname);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001586 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
1588 }
1589 event_size = sizeof(*event) + event->len;
1590 res -= event_size;
1591 event_pos += event_size;
1592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 return 0;
1594}
1595
Jeff Brown93fa9b32011-06-14 17:09:25 -07001596status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597{
1598 char devname[PATH_MAX];
1599 char *filename;
1600 DIR *dir;
1601 struct dirent *de;
1602 dir = opendir(dirname);
1603 if(dir == NULL)
1604 return -1;
1605 strcpy(devname, dirname);
1606 filename = devname + strlen(devname);
1607 *filename++ = '/';
1608 while((de = readdir(dir))) {
1609 if(de->d_name[0] == '.' &&
1610 (de->d_name[1] == '\0' ||
1611 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1612 continue;
1613 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001614 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
1616 closedir(dir);
1617 return 0;
1618}
1619
Jeff Brown93fa9b32011-06-14 17:09:25 -07001620void EventHub::requestReopenDevices() {
Steve Block71f2cf12011-10-20 11:56:00 +01001621 ALOGV("requestReopenDevices() called");
Jeff Brown93fa9b32011-06-14 17:09:25 -07001622
1623 AutoMutex _l(mLock);
1624 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001625}
1626
Jeff Brownf2f487182010-10-01 17:46:21 -07001627void EventHub::dump(String8& dump) {
1628 dump.append("Event Hub State:\n");
1629
1630 { // acquire lock
1631 AutoMutex _l(mLock);
1632
Jeff Brown90655042010-12-02 13:50:46 -08001633 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001634
1635 dump.append(INDENT "Devices:\n");
1636
Jeff Brown93fa9b32011-06-14 17:09:25 -07001637 for (size_t i = 0; i < mDevices.size(); i++) {
1638 const Device* device = mDevices.valueAt(i);
1639 if (mBuiltInKeyboardId == device->id) {
1640 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1641 device->id, device->identifier.name.string());
1642 } else {
1643 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1644 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001645 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001646 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1647 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001648 dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string());
Jeff Brown93fa9b32011-06-14 17:09:25 -07001649 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
Michael Wrightac6c78b2013-07-17 13:21:45 -07001650 dump.appendFormat(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001651 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1652 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1653 "product=0x%04x, version=0x%04x\n",
1654 device->identifier.bus, device->identifier.vendor,
1655 device->identifier.product, device->identifier.version);
1656 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1657 device->keyMap.keyLayoutFile.string());
1658 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1659 device->keyMap.keyCharacterMapFile.string());
1660 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1661 device->configurationFile.string());
Jeff Brown61c08242012-04-19 11:14:33 -07001662 dump.appendFormat(INDENT3 "HaveKeyboardLayoutOverlay: %s\n",
1663 toString(device->overlayKeyMap != NULL));
Jeff Brownf2f487182010-10-01 17:46:21 -07001664 }
1665 } // release lock
1666}
1667
Jeff Brown89ef0722011-08-10 16:25:21 -07001668void EventHub::monitor() {
1669 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1670 mLock.lock();
1671 mLock.unlock();
1672}
1673
1674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675}; // namespace android