blob: e67c64c00cf39b591d3deba2c02fd85f6b2305ea [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>
Michael Wright67774c72013-09-25 14:13:40 -070052#include <sys/utsname.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
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 Brownfd0358292010-06-30 16:10:35 -070061/* 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 Brownf2f487182010-10-01 17:46:21 -070064#define INDENT " "
65#define INDENT2 " "
66#define INDENT3 " "
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068namespace android {
69
70static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080071static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73/* return the larger integer */
74static inline int max(int v1, int v2)
75{
76 return (v1 > v2) ? v1 : v2;
77}
78
Jeff Brownf2f487182010-10-01 17:46:21 -070079static inline const char* toString(bool value) {
80 return value ? "true" : "false";
81}
82
Jeff Browne38fdfa2012-04-06 14:51:01 -070083static 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 Wright67774c72013-09-25 14:13:40 -070097static 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 Brown9f25b7f2012-04-10 14:30:49 -0700105static 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 Brown49ccac52012-04-11 18:27:33 -0700129 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
130 identifier.descriptor.string());
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700131}
132
Jeff Brown9ee285af2011-08-31 12:56:34 -0700133// --- Global Functions ---
134
135uint32_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 Brown90655042010-12-02 13:50:46 -0800167// --- EventHub::Device ---
168
169EventHub::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 Browna47425a2012-04-13 04:09:27 -0700173 classes(0), configuration(NULL), virtualKeyMap(NULL),
Michael Wrightac6c78b2013-07-17 13:21:45 -0700174 ffEffectPlaying(false), ffEffectId(-1), controllerNumber(0),
Jeff Brown4dac9012013-04-10 01:03:19 -0700175 timestampOverrideSec(0), timestampOverrideUsec(0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700176 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 Browna47425a2012-04-13 04:09:27 -0700181 memset(ffBitmask, 0, sizeof(ffBitmask));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700182 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183}
184
Jeff Brown90655042010-12-02 13:50:46 -0800185EventHub::Device::~Device() {
186 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800187 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800188 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189}
190
Jeff Brown90655042010-12-02 13:50:46 -0800191void EventHub::Device::close() {
192 if (fd >= 0) {
193 ::close(fd);
194 fd = -1;
195 }
196}
197
198
199// --- EventHub ---
200
Jeff Brown93fa9b32011-06-14 17:09:25 -0700201const uint32_t EventHub::EPOLL_ID_INOTIFY;
202const uint32_t EventHub::EPOLL_ID_WAKE;
203const int EventHub::EPOLL_SIZE_HINT;
204const int EventHub::EPOLL_MAX_EVENTS;
205
Jeff Brown90655042010-12-02 13:50:46 -0800206EventHub::EventHub(void) :
Michael Wrightac6c78b2013-07-17 13:21:45 -0700207 mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
Jeff Brown90655042010-12-02 13:50:46 -0800208 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700209 mNeedToSendFinishedDeviceScan(false),
210 mNeedToReopenDevices(false), mNeedToScanDevices(true),
211 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700213
Jeff Brown93fa9b32011-06-14 17:09:25 -0700214 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 Wright67774c72013-09-25 14:13:40 -0700248
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 Project9066cfe2009-03-03 19:31:44 -0800253}
254
Jeff Brown90655042010-12-02 13:50:46 -0800255EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700256 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Jeff Brown93fa9b32011-06-14 17:09:25 -0700258 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 Project9066cfe2009-03-03 19:31:44 -0800270}
271
Jeff Browne38fdfa2012-04-06 14:51:01 -0700272InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800274 Device* device = getDeviceLocked(deviceId);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700275 if (device == NULL) return InputDeviceIdentifier();
276 return device->identifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277}
278
Jeff Brown90655042010-12-02 13:50:46 -0800279uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800281 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 if (device == NULL) return 0;
283 return device->classes;
284}
285
Michael Wrightac6c78b2013-07-17 13:21:45 -0700286int32_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 Brown47e6b1b2010-11-29 17:37:49 -0800293void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800294 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800295 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800296 if (device && device->configuration) {
297 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800298 } else {
299 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800300 }
301}
302
Jeff Brown6d0fec22010-07-23 21:28:06 -0700303status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
304 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700305 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306
Jeff Brownba421dd2011-08-10 15:07:05 -0700307 if (axis >= 0 && axis <= ABS_MAX) {
308 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
Jeff Brownba421dd2011-08-10 15:07:05 -0700310 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700311 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700312 struct input_absinfo info;
313 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000314 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700315 axis, device->identifier.name.string(), device->fd, errno);
316 return -errno;
317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
Jeff Brownba421dd2011-08-10 15:07:05 -0700319 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 Project9066cfe2009-03-03 19:31:44 -0800329 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700330 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331}
332
Jeff Browncc0c1592011-02-19 05:07:28 -0800333bool 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 Brownba421dd2011-08-10 15:07:05 -0700338 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800339 return test_bit(axis, device->relBitmask);
340 }
341 }
342 return false;
343}
344
Jeff Brown80fd47c2011-05-24 01:07:44 -0700345bool 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 Brownba421dd2011-08-10 15:07:05 -0700350 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700351 return test_bit(property, device->propBitmask);
352 }
353 }
354 return false;
355}
356
Jeff Brown6d0fec22010-07-23 21:28:06 -0700357int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700358 if (scanCode >= 0 && scanCode <= KEY_MAX) {
359 AutoMutex _l(mLock);
360
Jeff Brown90655042010-12-02 13:50:46 -0800361 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700362 if (device && !device->isVirtual() && test_bit(scanCode, device->keyBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700363 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 Project9066cfe2009-03-03 19:31:44 -0800368 }
369 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700370 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371}
372
Jeff Brown6d0fec22010-07-23 21:28:06 -0700373int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
374 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700375
Jeff Brown90655042010-12-02 13:50:46 -0800376 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700377 if (device && !device->isVirtual() && device->keyMap.haveKeyLayout()) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700378 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 Project9066cfe2009-03-03 19:31:44 -0800391 }
392 }
393 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700394 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700395}
396
Jeff Brown6d0fec22010-07-23 21:28:06 -0700397int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700398 if (sw >= 0 && sw <= SW_MAX) {
399 AutoMutex _l(mLock);
400
Jeff Brown90655042010-12-02 13:50:46 -0800401 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700402 if (device && !device->isVirtual() && test_bit(sw, device->swBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700403 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 Brown46b9ac0a2010-04-22 18:58:52 -0700408 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700409 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700410 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700411}
412
Jeff Brown2717eff2011-06-30 23:53:07 -0700413status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700414 *outValue = 0;
415
Jeff Brown2717eff2011-06-30 23:53:07 -0700416 if (axis >= 0 && axis <= ABS_MAX) {
417 AutoMutex _l(mLock);
418
419 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700420 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700421 struct input_absinfo info;
422 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000423 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700424 axis, device->identifier.name.string(), device->fd, errno);
425 return -errno;
426 }
427
428 *outValue = info.value;
429 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700430 }
431 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700432 return -1;
433}
434
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
436 const int32_t* keyCodes, uint8_t* outFlags) const {
437 AutoMutex _l(mLock);
438
Jeff Brown90655042010-12-02 13:50:46 -0800439 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700440 if (device && device->keyMap.haveKeyLayout()) {
441 Vector<int32_t> scanCodes;
442 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
443 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444
Jeff Brownba421dd2011-08-10 15:07:05 -0700445 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 Brown6d0fec22010-07-23 21:28:06 -0700455 }
456 }
457 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700458 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700459 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700460 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461}
462
Jeff Brown49ccac52012-04-11 18:27:33 -0700463status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
464 int32_t* outKeycode, uint32_t* outFlags) const {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700465 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800466 Device* device = getDeviceLocked(deviceId);
Jeff Brown49ccac52012-04-11 18:27:33 -0700467
Jeff Brown4a3862f2012-04-17 18:50:05 -0700468 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 Hackborne3dd8842009-07-14 12:06:54 -0700484 }
485 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700486
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700487 *outKeycode = 0;
488 *outFlags = 0;
489 return NAME_NOT_FOUND;
490}
491
Jeff Brown49ccac52012-04-11 18:27:33 -0700492status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800493 AutoMutex _l(mLock);
494 Device* device = getDeviceLocked(deviceId);
495
496 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700497 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800498 if (err == NO_ERROR) {
499 return NO_ERROR;
500 }
501 }
502
Jeff Brown6f2fba42011-02-19 01:08:02 -0800503 return NAME_NOT_FOUND;
504}
505
Jeff Brown1a84fd12011-06-02 01:26:32 -0700506void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700507 AutoMutex _l(mLock);
508
Jeff Brown1a84fd12011-06-02 01:26:32 -0700509 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400510}
511
Jeff Brown49754db2011-07-01 17:37:58 -0700512bool 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 Brown497a92c2010-09-12 17:55:08 -0700523bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
524 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800525 Device* device = getDeviceLocked(deviceId);
Michael Wright58f5a612013-10-18 15:26:48 -0700526 int32_t sc;
527 if (device && mapLed(device, led, &sc) == NO_ERROR) {
528 if (test_bit(sc, device->ledBitmask)) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700529 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700530 }
531 }
532 return false;
533}
534
535void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
536 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800537 Device* device = getDeviceLocked(deviceId);
Michael Wright58f5a612013-10-18 15:26:48 -0700538 setLedStateLocked(device, led, on);
539}
540
541void 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 Brown497a92c2010-09-12 17:55:08 -0700544 struct input_event ev;
545 ev.time.tv_sec = 0;
546 ev.time.tv_usec = 0;
547 ev.type = EV_LED;
Michael Wright58f5a612013-10-18 15:26:48 -0700548 ev.code = sc;
Jeff Brown497a92c2010-09-12 17:55:08 -0700549 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 Brown90655042010-12-02 13:50:46 -0800558void 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 Brown9f25b7f2012-04-10 14:30:49 -0700569sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Jeff Brown1e08fe92011-11-15 17:48:10 -0800570 AutoMutex _l(mLock);
571 Device* device = getDeviceLocked(deviceId);
572 if (device) {
Jeff Brown4a3862f2012-04-17 18:50:05 -0700573 return device->getKeyCharacterMap();
Jeff Brown1e08fe92011-11-15 17:48:10 -0800574 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700575 return NULL;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800576}
577
Jeff Brown6ec6f792012-04-17 16:52:41 -0700578bool 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 Browna47425a2012-04-13 04:09:27 -0700593void 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
627void 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 Brown90655042010-12-02 13:50:46 -0800649EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700650 if (deviceId == BUILT_IN_KEYBOARD_ID) {
Jeff Brown90655042010-12-02 13:50:46 -0800651 deviceId = mBuiltInKeyboardId;
652 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700653 ssize_t index = mDevices.indexOfKey(deviceId);
654 return index >= 0 ? mDevices.valueAt(index) : NULL;
655}
Jeff Brown90655042010-12-02 13:50:46 -0800656
Jeff Brown93fa9b32011-06-14 17:09:25 -0700657EventHub::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 Brown90655042010-12-02 13:50:46 -0800661 return device;
662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664 return NULL;
665}
666
Jeff Brownb7198742011-03-18 18:14:26 -0700667size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Steve Blockec193de2012-01-09 18:35:44 +0000668 ALOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400669
Jeff Brown93fa9b32011-06-14 17:09:25 -0700670 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400671
Jeff Brownb7198742011-03-18 18:14:26 -0700672 struct input_event readBuffer[bufferSize];
673
674 RawEvent* event = buffer;
675 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700676 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700677 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700678 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
679
Jeff Brown1a84fd12011-06-02 01:26:32 -0700680 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700681 if (mNeedToReopenDevices) {
682 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700683
Steve Block6215d3f2012-01-04 20:05:49 +0000684 ALOGI("Reopening all input devices due to a configuration change.");
Jeff Brown1a84fd12011-06-02 01:26:32 -0700685
Jeff Brown93fa9b32011-06-14 17:09:25 -0700686 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700687 mNeedToScanDevices = true;
688 break; // return to the caller before we actually rescan
689 }
690
Jeff Browncc2e7172010-08-17 16:48:25 -0700691 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700692 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800693 Device* device = mClosingDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100694 ALOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 device->id, device->path.string());
696 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700697 event->when = now;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700698 event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
Jeff Brownb7198742011-03-18 18:14:26 -0700699 event->type = DEVICE_REMOVED;
700 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700702 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700703 if (--capacity == 0) {
704 break;
705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700707
Jeff Brown1a84fd12011-06-02 01:26:32 -0700708 if (mNeedToScanDevices) {
709 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700710 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700711 mNeedToSendFinishedDeviceScan = true;
712 }
713
Jeff Brownb7198742011-03-18 18:14:26 -0700714 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800715 Device* device = mOpeningDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100716 ALOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 device->id, device->path.string());
718 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700719 event->when = now;
720 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
721 event->type = DEVICE_ADDED;
722 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700723 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700724 if (--capacity == 0) {
725 break;
726 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700727 }
728
729 if (mNeedToSendFinishedDeviceScan) {
730 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700731 event->when = now;
732 event->type = FINISHED_DEVICE_SCAN;
733 event += 1;
734 if (--capacity == 0) {
735 break;
736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738
Jeff Browncc2e7172010-08-17 16:48:25 -0700739 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700740 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 Block8564c8d2012-01-05 23:22:43 +0000747 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700748 }
749 continue;
750 }
751
752 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
753 if (eventItem.events & EPOLLIN) {
Steve Block71f2cf12011-10-20 11:56:00 +0100754 ALOGV("awoken after wake()");
Jeff Brown93fa9b32011-06-14 17:09:25 -0700755 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 Block8564c8d2012-01-05 23:22:43 +0000762 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700763 eventItem.events);
764 }
765 continue;
766 }
767
768 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
769 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000770 ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700771 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 Brown41305542011-10-05 11:14:13 -0700781 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 Brown93fa9b32011-06-14 17:09:25 -0700784 deviceChanged = true;
785 closeDeviceLocked(device);
786 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700787 if (errno != EAGAIN && errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000788 ALOGW("could not get event (errno=%d)", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700789 }
790 } else if ((readSize % sizeof(struct input_event)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000791 ALOGE("could not get event (wrong size: %d)", readSize);
Jeff Browncc2e7172010-08-17 16:48:25 -0700792 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700793 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 Brown4dac9012013-04-10 01:03:19 -0700797 struct input_event& iev = readBuffer[i];
798 ALOGV("%s got: time=%d.%06d, type=%d, code=%d, value=%d",
JP Abgrall25a465b2012-05-16 10:33:49 -0700799 device->path.string(),
800 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
801 iev.type, iev.code, iev.value);
802
Jeff Brown4dac9012013-04-10 01:03:19 -0700803 // 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 Brown4e91a182011-04-07 11:38:09 -0700828#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 Abgrall25a465b2012-05-16 10:33:49 -0700842 ALOGV("event time %lld, now %lld", event->when, now);
Jeff Brownf33b2b22012-10-05 17:59:56 -0700843
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 Brown4e91a182011-04-07 11:38:09 -0700877#else
Jeff Brownb7198742011-03-18 18:14:26 -0700878 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700879#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700880 event->deviceId = deviceId;
881 event->type = iev.type;
Jeff Brown49ccac52012-04-11 18:27:33 -0700882 event->code = iev.code;
Jeff Brownb7198742011-03-18 18:14:26 -0700883 event->value = iev.value;
Jeff Brownb7198742011-03-18 18:14:26 -0700884 event += 1;
Jeff Brown4dac9012013-04-10 01:03:19 -0700885 capacity -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700886 }
Jeff Brownb7198742011-03-18 18:14:26 -0700887 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700888 // 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 Brownb7198742011-03-18 18:14:26 -0700891 break;
892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700894 } 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 Brown93fa9b32011-06-14 17:09:25 -0700899 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000900 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700901 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700904
Jeff Brown93fa9b32011-06-14 17:09:25 -0700905 // 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 Brown33bbfd22011-02-24 20:55:35 -0800912 }
913
Jeff Brown93fa9b32011-06-14 17:09:25 -0700914 // Report added or removed devices immediately.
915 if (deviceChanged) {
916 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
Jeff Browna9b84222010-10-14 02:23:43 -0700918
Jeff Brown93fa9b32011-06-14 17:09:25 -0700919 // Return now if we have collected any events or if we were explicitly awoken.
920 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700921 break;
922 }
923
Jeff Browncc2e7172010-08-17 16:48:25 -0700924 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700925 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700926 // 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 Brownaa3855d2011-03-17 01:34:19 -0700932 //
933 // The timeout is advisory only. If the device is asleep, it will not wake just to
934 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700935 mPendingEventIndex = 0;
936
937 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700938 release_wake_lock(WAKE_LOCK_ID);
939
Jeff Brown93fa9b32011-06-14 17:09:25 -0700940 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700941
942 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700943 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700944
Jeff Brownaa3855d2011-03-17 01:34:19 -0700945 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700946 // Timed out.
947 mPendingEventCount = 0;
948 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700949 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700950
Jeff Brownaa3855d2011-03-17 01:34:19 -0700951 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700952 // An error occurred.
953 mPendingEventCount = 0;
954
Jeff Brownb7198742011-03-18 18:14:26 -0700955 // Sleep after errors to avoid locking up the system.
956 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700957 if (errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000958 ALOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700959 usleep(100000);
960 }
Jeff Brownb7198742011-03-18 18:14:26 -0700961 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700962 // Some events occurred.
963 mPendingEventCount = size_t(pollResult);
Jeff Browncc2e7172010-08-17 16:48:25 -0700964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
Jeff Brownb7198742011-03-18 18:14:26 -0700966
967 // All done, return the number of events we read.
968 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969}
970
Jeff Brown93fa9b32011-06-14 17:09:25 -0700971void EventHub::wake() {
Steve Block71f2cf12011-10-20 11:56:00 +0100972 ALOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
Jeff Brown93fa9b32011-06-14 17:09:25 -0700974 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 Block8564c8d2012-01-05 23:22:43 +0000980 ALOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700982}
Jeff Brown90655042010-12-02 13:50:46 -0800983
Jeff Brown93fa9b32011-06-14 17:09:25 -0700984void EventHub::scanDevicesLocked() {
985 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 if(res < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000987 ALOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700989 if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) {
990 createVirtualKeyboardLocked();
991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992}
993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994// ----------------------------------------------------------------------------
995
Jeff Brownfd0358292010-06-30 16:10:35 -0700996static 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
1007static 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 Browncb1404e2011-01-15 18:14:15 -08001013 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 Brownfd0358292010-06-30 16:10:35 -07001018};
1019
Jeff Brown93fa9b32011-06-14 17:09:25 -07001020status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -08001021 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022
Steve Block71f2cf12011-10-20 11:56:00 +01001023 ALOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024
Jeff Brown874c1e92012-01-19 14:32:47 -08001025 int fd = open(devicePath, O_RDWR | O_CLOEXEC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 if(fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001027 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 return -1;
1029 }
1030
Jeff Brown90655042010-12-02 13:50:46 -08001031 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 Project9066cfe2009-03-03 19:31:44 -08001039 }
Mike Lockwood15431a92009-07-17 00:10:10 -04001040
Jeff Brown90655042010-12-02 13:50:46 -08001041 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -07001042 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
1043 const String8& item = mExcludedDevices.itemAt(i);
1044 if (identifier.name == item) {
Steve Block6215d3f2012-01-04 20:05:49 +00001045 ALOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -04001046 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -04001047 return -1;
1048 }
1049 }
1050
Jeff Brown90655042010-12-02 13:50:46 -08001051 // Get device driver version.
1052 int driverVersion;
1053 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Steve Block3762c312012-01-06 19:20:56 +00001054 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001055 close(fd);
1056 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058
Jeff Brown90655042010-12-02 13:50:46 -08001059 // Get device identifier.
1060 struct input_id inputId;
1061 if(ioctl(fd, EVIOCGID, &inputId)) {
Steve Block3762c312012-01-06 19:20:56 +00001062 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001063 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 Brown9f25b7f2012-04-10 14:30:49 -07001087 // Fill in the descriptor.
1088 setDescriptor(identifier);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001089
Jeff Brown90655042010-12-02 13:50:46 -08001090 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -07001091 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
Steve Block3762c312012-01-06 19:20:56 +00001092 ALOGE("Error %d making device file descriptor non-blocking.", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -07001093 close(fd);
1094 return -1;
1095 }
1096
Jeff Brown90655042010-12-02 13:50:46 -08001097 // 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 Project9066cfe2009-03-03 19:31:44 -08001100
Jeff Browne38fdfa2012-04-06 14:51:01 -07001101 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 Brown90655042010-12-02 13:50:46 -08001106 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001107 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 Brown49ccac52012-04-11 18:27:33 -07001110 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001111 ALOGV(" driver: v%d.%d.%d\n",
Jeff Brown90655042010-12-02 13:50:46 -08001112 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001114 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001115 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001116
Jeff Brownfd0358292010-06-30 16:10:35 -07001117 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001118 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 Browna47425a2012-04-13 04:09:27 -07001123 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001124 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -08001125
Jeff Brown6f2fba42011-02-19 01:08:02 -08001126 // 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 Brown93fa9b32011-06-14 17:09:25 -07001128 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
1129 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -08001130 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001131 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001132 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -07001133 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001134 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -08001135 if (haveKeyboardKeys || haveGamepadButtons) {
1136 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001138
Jeff Brown83c09682010-12-23 17:50:18 -08001139 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001140 if (test_bit(BTN_MOUSE, device->keyBitmask)
1141 && test_bit(REL_X, device->relBitmask)
1142 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001143 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001145
1146 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -08001147 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001148 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
1149 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001150 // 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 Brown93fa9b32011-06-14 17:09:25 -07001153 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001154 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -07001155 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001156 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001157 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
1158 && test_bit(ABS_X, device->absBitmask)
1159 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001160 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001163 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001164 // 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 Brown9ee285af2011-08-31 12:56:34 -07001166 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 Brown9e8e40c2011-03-03 03:39:29 -08001175 }
1176
Jeff Brown93fa9b32011-06-14 17:09:25 -07001177 // 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 Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184
Jeff Browna47425a2012-04-13 04:09:27 -07001185 // 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 Brown93fa9b32011-06-14 17:09:25 -07001190 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -08001191 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -08001192 // 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 Brown93fa9b32011-06-14 17:09:25 -07001194 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -08001195 if (!status) {
1196 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
Jeff Brown90655042010-12-02 13:50:46 -08001198 }
1199
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001200 // 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 Brown90655042010-12-02 13:50:46 -08001204 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001205 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001206 }
Jeff Brown90655042010-12-02 13:50:46 -08001207
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001208 // Configure the keyboard, gamepad or virtual keyboard.
1209 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001210 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001211 if (!keyMapStatus
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001212 && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD
Jeff Brown90655042010-12-02 13:50:46 -08001213 && isEligibleBuiltInKeyboard(device->identifier,
1214 device->configuration, &device->keyMap)) {
1215 mBuiltInKeyboardId = device->id;
Jeff Brown497a92c2010-09-12 17:55:08 -07001216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217
Ken Wakasa02a44f72013-07-05 04:08:36 +00001218 // '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 Brownfd0358292010-06-30 16:10:35 -07001223 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -07001224 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 Brown46b9ac0a2010-04-22 18:58:52 -07001229 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001230 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001231
Jeff Brownfd0358292010-06-30 16:10:35 -07001232 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001233 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001234 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -07001235 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1236 break;
1237 }
1238 }
Michael Wrighta0a72852013-02-21 23:51:45 -08001239
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 Project9066cfe2009-03-03 19:31:44 -08001245 }
1246
Sean McNeilaeb00c42010-06-23 16:00:37 +07001247 // If the device isn't recognized as something we handle, don't monitor it.
1248 if (device->classes == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001249 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Jeff Brown90655042010-12-02 13:50:46 -08001250 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001251 delete device;
1252 return -1;
1253 }
1254
Jeff Brown56194eb2011-03-02 19:23:13 -08001255 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001256 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001257 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1258 }
1259
Michael Wrightac6c78b2013-07-17 13:21:45 -07001260 if (device->classes & (INPUT_DEVICE_CLASS_JOYSTICK | INPUT_DEVICE_CLASS_GAMEPAD)) {
1261 device->controllerNumber = getNextControllerNumberLocked(device);
Michael Wright58f5a612013-10-18 15:26:48 -07001262 setLedForController(device);
Michael Wrightac6c78b2013-07-17 13:21:45 -07001263 }
1264
Jeff Brown93fa9b32011-06-14 17:09:25 -07001265 // Register with epoll.
1266 struct epoll_event eventItem;
1267 memset(&eventItem, 0, sizeof(eventItem));
Michael Wright67774c72013-09-25 14:13:40 -07001268 eventItem.events = mUsingEpollWakeup ? EPOLLIN : EPOLLIN | EPOLLWAKEUP;
Jeff Brown93fa9b32011-06-14 17:09:25 -07001269 eventItem.data.u32 = deviceId;
1270 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
Steve Block3762c312012-01-06 19:20:56 +00001271 ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001272 delete device;
1273 return -1;
1274 }
1275
Michael Wright67774c72013-09-25 14:13:40 -07001276 String8 wakeMechanism("EPOLLWAKEUP");
1277 if (!mUsingEpollWakeup) {
1278 if (ioctl(fd, EVIOCSSUSPENDBLOCK, 1)) {
1279 wakeMechanism = "<none>";
1280 } else {
1281 wakeMechanism = "EVIOCSSUSPENDBLOCK";
1282 }
1283 }
Jeff Browneca3cf52012-04-06 19:31:36 -07001284
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 Browna75fe052012-05-01 18:41:26 -07001300 int clockId = CLOCK_MONOTONIC;
1301 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId);
Jeff Browne22afbe2011-12-16 13:45:40 -08001302
Steve Block6215d3f2012-01-04 20:05:49 +00001303 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
Jeff Browne22afbe2011-12-16 13:45:40 -08001304 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
Michael Wright67774c72013-09-25 14:13:40 -07001305 "wakeMechanism=%s, usingClockIoctl=%s",
Jeff Brown90655042010-12-02 13:50:46 -08001306 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 Browne22afbe2011-12-16 13:45:40 -08001311 toString(mBuiltInKeyboardId == deviceId),
Michael Wright67774c72013-09-25 14:13:40 -07001312 wakeMechanism.string(), toString(usingClockIoctl));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001313
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001314 addDeviceLocked(device);
1315 return 0;
1316}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001318void 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
1333void EventHub::addDeviceLocked(Device* device) {
1334 mDevices.add(device->id, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 device->next = mOpeningDevices;
1336 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337}
1338
Jeff Brown93fa9b32011-06-14 17:09:25 -07001339void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001340 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1341 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001342 if (device->configurationFile.isEmpty()) {
Steve Block5baa3a62011-12-20 16:23:08 +00001343 ALOGD("No input device configuration file found for device '%s'.",
Jeff Brown90655042010-12-02 13:50:46 -08001344 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001345 } else {
1346 status_t status = PropertyMap::load(device->configurationFile,
1347 &device->configuration);
1348 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001349 ALOGE("Error loading input device configuration file for device '%s'. "
Jeff Brown90655042010-12-02 13:50:46 -08001350 "Using default configuration.",
1351 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001352 }
1353 }
1354}
1355
Jeff Brown93fa9b32011-06-14 17:09:25 -07001356status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001357 // 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 Brown497a92c2010-09-12 17:55:08 -07001365}
1366
Jeff Brown93fa9b32011-06-14 17:09:25 -07001367status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001368 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001369}
1370
Jeff Brown93fa9b32011-06-14 17:09:25 -07001371bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001372 if (device->configuration) {
1373 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001374 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1375 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001376 }
1377 }
1378 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1379}
1380
Michael Wrightac6c78b2013-07-17 13:21:45 -07001381int32_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
1392void 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 Wright58f5a612013-10-18 15:26:48 -07001401void 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 Wrightac6c78b2013-07-17 13:21:45 -07001406
Jeff Brown90655042010-12-02 13:50:46 -08001407bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1408 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001409 return false;
1410 }
1411
1412 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001413 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001414 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 Wright58f5a612013-10-18 15:26:48 -07001425status_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 Brown93fa9b32011-06-14 17:09:25 -07001440status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1441 Device* device = getDeviceByPathLocked(devicePath);
1442 if (device) {
1443 closeDeviceLocked(device);
1444 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 }
Steve Block71f2cf12011-10-20 11:56:00 +01001446 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 return -1;
1448}
1449
Jeff Brown93fa9b32011-06-14 17:09:25 -07001450void EventHub::closeAllDevicesLocked() {
1451 while (mDevices.size() > 0) {
1452 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1453 }
1454}
1455
1456void EventHub::closeDeviceLocked(Device* device) {
Steve Block6215d3f2012-01-04 20:05:49 +00001457 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001458 device->path.string(), device->identifier.name.string(), device->id,
1459 device->fd, device->classes);
1460
Jeff Brown33bbfd22011-02-24 20:55:35 -08001461 if (device->id == mBuiltInKeyboardId) {
Steve Block8564c8d2012-01-05 23:22:43 +00001462 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001463 device->path.string(), mBuiltInKeyboardId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001464 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001465 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001466
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001467 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 Brown93fa9b32011-06-14 17:09:25 -07001471 }
1472
Michael Wrightac6c78b2013-07-17 13:21:45 -07001473 releaseControllerNumberLocked(device);
1474
Jeff Brown93fa9b32011-06-14 17:09:25 -07001475 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001476 device->close();
1477
Jeff Brown8e9d4432011-03-12 19:46:59 -08001478 // 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 Block6215d3f2012-01-04 20:05:49 +00001493 ALOGI("Device %s was immediately closed after opening.", device->path.string());
Jeff Brown8e9d4432011-03-12 19:46:59 -08001494 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 Brown33bbfd22011-02-24 20:55:35 -08001506}
1507
Jeff Brown93fa9b32011-06-14 17:09:25 -07001508status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 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 Block71f2cf12011-10-20 11:56:00 +01001517 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001518 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 if(res < (int)sizeof(*event)) {
1520 if(errno == EINTR)
1521 return 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001522 ALOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001523 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525 //printf("got %d bytes of event information\n", res);
1526
Jeff Brown90655042010-12-02 13:50:46 -08001527 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 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 Brown93fa9b32011-06-14 17:09:25 -07001537 openDeviceLocked(devname);
1538 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00001539 ALOGI("Removing device '%s' due to inotify event\n", devname);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001540 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
1542 }
1543 event_size = sizeof(*event) + event->len;
1544 res -= event_size;
1545 event_pos += event_size;
1546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 return 0;
1548}
1549
Jeff Brown93fa9b32011-06-14 17:09:25 -07001550status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551{
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 Brown93fa9b32011-06-14 17:09:25 -07001568 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
1570 closedir(dir);
1571 return 0;
1572}
1573
Jeff Brown93fa9b32011-06-14 17:09:25 -07001574void EventHub::requestReopenDevices() {
Steve Block71f2cf12011-10-20 11:56:00 +01001575 ALOGV("requestReopenDevices() called");
Jeff Brown93fa9b32011-06-14 17:09:25 -07001576
1577 AutoMutex _l(mLock);
1578 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001579}
1580
Jeff Brownf2f487182010-10-01 17:46:21 -07001581void EventHub::dump(String8& dump) {
1582 dump.append("Event Hub State:\n");
1583
1584 { // acquire lock
1585 AutoMutex _l(mLock);
1586
Jeff Brown90655042010-12-02 13:50:46 -08001587 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001588
1589 dump.append(INDENT "Devices:\n");
1590
Jeff Brown93fa9b32011-06-14 17:09:25 -07001591 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 Brownf2f487182010-10-01 17:46:21 -07001599 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001600 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1601 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001602 dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string());
Jeff Brown93fa9b32011-06-14 17:09:25 -07001603 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
Michael Wrightac6c78b2013-07-17 13:21:45 -07001604 dump.appendFormat(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001605 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 Brown61c08242012-04-19 11:14:33 -07001616 dump.appendFormat(INDENT3 "HaveKeyboardLayoutOverlay: %s\n",
1617 toString(device->overlayKeyMap != NULL));
Jeff Brownf2f487182010-10-01 17:46:21 -07001618 }
1619 } // release lock
1620}
1621
Jeff Brown89ef0722011-08-10 16:25:21 -07001622void 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 Project9066cfe2009-03-03 19:31:44 -08001629}; // namespace android