blob: e7a691db93db7e97985fa467b4abcf63a5d5e78f [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>
Dan Albert70c47d72014-06-20 22:40:25 +000051
52#include <openssl/sha.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 Brownfd035822010-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) {
Dan Albert70c47d72014-06-20 22:40:25 +000084 SHA_CTX ctx;
85 SHA1_Init(&ctx);
86 SHA1_Update(&ctx, reinterpret_cast<const u_char*>(in.string()), in.size());
87 u_char digest[SHA_DIGEST_LENGTH];
88 SHA1_Final(digest, &ctx);
Jeff Browne38fdfa2012-04-06 14:51:01 -070089
90 String8 out;
Dan Albert70c47d72014-06-20 22:40:25 +000091 for (size_t i = 0; i < SHA_DIGEST_LENGTH; i++) {
Jeff Browne38fdfa2012-04-06 14:51:01 -070092 out.appendFormat("%02x", digest[i]);
93 }
94 return out;
95}
96
Jeff Brown9f25b7f2012-04-10 14:30:49 -070097static void setDescriptor(InputDeviceIdentifier& identifier) {
98 // Compute a device descriptor that uniquely identifies the device.
99 // The descriptor is assumed to be a stable identifier. Its value should not
100 // change between reboots, reconnections, firmware updates or new releases of Android.
101 // Ideally, we also want the descriptor to be short and relatively opaque.
102 String8 rawDescriptor;
103 rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor, identifier.product);
104 if (!identifier.uniqueId.isEmpty()) {
105 rawDescriptor.append("uniqueId:");
106 rawDescriptor.append(identifier.uniqueId);
107 } if (identifier.vendor == 0 && identifier.product == 0) {
108 // If we don't know the vendor and product id, then the device is probably
109 // built-in so we need to rely on other information to uniquely identify
110 // the input device. Usually we try to avoid relying on the device name or
111 // location but for built-in input device, they are unlikely to ever change.
112 if (!identifier.name.isEmpty()) {
113 rawDescriptor.append("name:");
114 rawDescriptor.append(identifier.name);
115 } else if (!identifier.location.isEmpty()) {
116 rawDescriptor.append("location:");
117 rawDescriptor.append(identifier.location);
118 }
119 }
120 identifier.descriptor = sha1(rawDescriptor);
Jeff Brown49ccac52012-04-11 18:27:33 -0700121 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
122 identifier.descriptor.string());
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700123}
124
Jeff Brown9ee285a2011-08-31 12:56:34 -0700125// --- Global Functions ---
126
127uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
128 // Touch devices get dibs on touch-related axes.
129 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
130 switch (axis) {
131 case ABS_X:
132 case ABS_Y:
133 case ABS_PRESSURE:
134 case ABS_TOOL_WIDTH:
135 case ABS_DISTANCE:
136 case ABS_TILT_X:
137 case ABS_TILT_Y:
138 case ABS_MT_SLOT:
139 case ABS_MT_TOUCH_MAJOR:
140 case ABS_MT_TOUCH_MINOR:
141 case ABS_MT_WIDTH_MAJOR:
142 case ABS_MT_WIDTH_MINOR:
143 case ABS_MT_ORIENTATION:
144 case ABS_MT_POSITION_X:
145 case ABS_MT_POSITION_Y:
146 case ABS_MT_TOOL_TYPE:
147 case ABS_MT_BLOB_ID:
148 case ABS_MT_TRACKING_ID:
149 case ABS_MT_PRESSURE:
150 case ABS_MT_DISTANCE:
151 return INPUT_DEVICE_CLASS_TOUCH;
152 }
153 }
154
155 // Joystick devices get the rest.
156 return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK;
157}
158
Jeff Brown90655042010-12-02 13:50:46 -0800159// --- EventHub::Device ---
160
161EventHub::Device::Device(int fd, int32_t id, const String8& path,
162 const InputDeviceIdentifier& identifier) :
163 next(NULL),
164 fd(fd), id(id), path(path), identifier(identifier),
Jeff Browna47425a2012-04-13 04:09:27 -0700165 classes(0), configuration(NULL), virtualKeyMap(NULL),
Michael Wrightac6c78b2013-07-17 13:21:45 -0700166 ffEffectPlaying(false), ffEffectId(-1), controllerNumber(0),
Jeff Brown4dac9012013-04-10 01:03:19 -0700167 timestampOverrideSec(0), timestampOverrideUsec(0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700168 memset(keyBitmask, 0, sizeof(keyBitmask));
169 memset(absBitmask, 0, sizeof(absBitmask));
170 memset(relBitmask, 0, sizeof(relBitmask));
171 memset(swBitmask, 0, sizeof(swBitmask));
172 memset(ledBitmask, 0, sizeof(ledBitmask));
Jeff Browna47425a2012-04-13 04:09:27 -0700173 memset(ffBitmask, 0, sizeof(ffBitmask));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700174 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175}
176
Jeff Brown90655042010-12-02 13:50:46 -0800177EventHub::Device::~Device() {
178 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800179 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800180 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181}
182
Jeff Brown90655042010-12-02 13:50:46 -0800183void EventHub::Device::close() {
184 if (fd >= 0) {
185 ::close(fd);
186 fd = -1;
187 }
188}
189
190
191// --- EventHub ---
192
Jeff Brown93fa9b32011-06-14 17:09:25 -0700193const uint32_t EventHub::EPOLL_ID_INOTIFY;
194const uint32_t EventHub::EPOLL_ID_WAKE;
195const int EventHub::EPOLL_SIZE_HINT;
196const int EventHub::EPOLL_MAX_EVENTS;
197
Jeff Brown90655042010-12-02 13:50:46 -0800198EventHub::EventHub(void) :
Michael Wrightac6c78b2013-07-17 13:21:45 -0700199 mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
Jeff Brown90655042010-12-02 13:50:46 -0800200 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700201 mNeedToSendFinishedDeviceScan(false),
202 mNeedToReopenDevices(false), mNeedToScanDevices(true),
203 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700205
Jeff Brown93fa9b32011-06-14 17:09:25 -0700206 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
207 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
208
209 mINotifyFd = inotify_init();
210 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
211 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
212 DEVICE_PATH, errno);
213
214 struct epoll_event eventItem;
215 memset(&eventItem, 0, sizeof(eventItem));
216 eventItem.events = EPOLLIN;
217 eventItem.data.u32 = EPOLL_ID_INOTIFY;
218 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
219 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
220
221 int wakeFds[2];
222 result = pipe(wakeFds);
223 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
224
225 mWakeReadPipeFd = wakeFds[0];
226 mWakeWritePipeFd = wakeFds[1];
227
228 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
229 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
230 errno);
231
232 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
233 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
234 errno);
235
236 eventItem.data.u32 = EPOLL_ID_WAKE;
237 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
238 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
239 errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240}
241
Jeff Brown90655042010-12-02 13:50:46 -0800242EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700243 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
Jeff Brown93fa9b32011-06-14 17:09:25 -0700245 while (mClosingDevices) {
246 Device* device = mClosingDevices;
247 mClosingDevices = device->next;
248 delete device;
249 }
250
251 ::close(mEpollFd);
252 ::close(mINotifyFd);
253 ::close(mWakeReadPipeFd);
254 ::close(mWakeWritePipeFd);
255
256 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257}
258
Jeff Browne38fdfa2012-04-06 14:51:01 -0700259InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800261 Device* device = getDeviceLocked(deviceId);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700262 if (device == NULL) return InputDeviceIdentifier();
263 return device->identifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264}
265
Jeff Brown90655042010-12-02 13:50:46 -0800266uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800268 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 if (device == NULL) return 0;
270 return device->classes;
271}
272
Michael Wrightac6c78b2013-07-17 13:21:45 -0700273int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
274 AutoMutex _l(mLock);
275 Device* device = getDeviceLocked(deviceId);
276 if (device == NULL) return 0;
277 return device->controllerNumber;
278}
279
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800280void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800281 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800282 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800283 if (device && device->configuration) {
284 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800285 } else {
286 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800287 }
288}
289
Jeff Brown6d0fec22010-07-23 21:28:06 -0700290status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
291 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700292 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700293
Jeff Brownba421dd2011-08-10 15:07:05 -0700294 if (axis >= 0 && axis <= ABS_MAX) {
295 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296
Jeff Brownba421dd2011-08-10 15:07:05 -0700297 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700298 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700299 struct input_absinfo info;
300 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000301 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700302 axis, device->identifier.name.string(), device->fd, errno);
303 return -errno;
304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305
Jeff Brownba421dd2011-08-10 15:07:05 -0700306 if (info.minimum != info.maximum) {
307 outAxisInfo->valid = true;
308 outAxisInfo->minValue = info.minimum;
309 outAxisInfo->maxValue = info.maximum;
310 outAxisInfo->flat = info.flat;
311 outAxisInfo->fuzz = info.fuzz;
312 outAxisInfo->resolution = info.resolution;
313 }
314 return OK;
315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700317 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318}
319
Jeff Browncc0c1592011-02-19 05:07:28 -0800320bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
321 if (axis >= 0 && axis <= REL_MAX) {
322 AutoMutex _l(mLock);
323
324 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700325 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800326 return test_bit(axis, device->relBitmask);
327 }
328 }
329 return false;
330}
331
Jeff Brown80fd47c2011-05-24 01:07:44 -0700332bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
333 if (property >= 0 && property <= INPUT_PROP_MAX) {
334 AutoMutex _l(mLock);
335
336 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700337 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700338 return test_bit(property, device->propBitmask);
339 }
340 }
341 return false;
342}
343
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345 if (scanCode >= 0 && scanCode <= KEY_MAX) {
346 AutoMutex _l(mLock);
347
Jeff Brown90655042010-12-02 13:50:46 -0800348 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700349 if (device && !device->isVirtual() && test_bit(scanCode, device->keyBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700350 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
351 memset(keyState, 0, sizeof(keyState));
352 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
353 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
356 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700357 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358}
359
Jeff Brown6d0fec22010-07-23 21:28:06 -0700360int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
361 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700362
Jeff Brown90655042010-12-02 13:50:46 -0800363 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700364 if (device && !device->isVirtual() && device->keyMap.haveKeyLayout()) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700365 Vector<int32_t> scanCodes;
366 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
367 if (scanCodes.size() != 0) {
368 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
369 memset(keyState, 0, sizeof(keyState));
370 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
371 for (size_t i = 0; i < scanCodes.size(); i++) {
372 int32_t sc = scanCodes.itemAt(i);
373 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
374 return AKEY_STATE_DOWN;
375 }
376 }
377 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379 }
380 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700381 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700382}
383
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700385 if (sw >= 0 && sw <= SW_MAX) {
386 AutoMutex _l(mLock);
387
Jeff Brown90655042010-12-02 13:50:46 -0800388 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700389 if (device && !device->isVirtual() && test_bit(sw, device->swBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700390 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
391 memset(swState, 0, sizeof(swState));
392 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
393 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
394 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700395 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700397 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700398}
399
Jeff Brown2717eff2011-06-30 23:53:07 -0700400status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700401 *outValue = 0;
402
Jeff Brown2717eff2011-06-30 23:53:07 -0700403 if (axis >= 0 && axis <= ABS_MAX) {
404 AutoMutex _l(mLock);
405
406 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700407 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700408 struct input_absinfo info;
409 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000410 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700411 axis, device->identifier.name.string(), device->fd, errno);
412 return -errno;
413 }
414
415 *outValue = info.value;
416 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700417 }
418 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700419 return -1;
420}
421
Jeff Brown6d0fec22010-07-23 21:28:06 -0700422bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
423 const int32_t* keyCodes, uint8_t* outFlags) const {
424 AutoMutex _l(mLock);
425
Jeff Brown90655042010-12-02 13:50:46 -0800426 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700427 if (device && device->keyMap.haveKeyLayout()) {
428 Vector<int32_t> scanCodes;
429 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
430 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431
Jeff Brownba421dd2011-08-10 15:07:05 -0700432 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
433 keyCodes[codeIndex], &scanCodes);
434 if (! err) {
435 // check the possible scan codes identified by the layout map against the
436 // map of codes actually emitted by the driver
437 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
438 if (test_bit(scanCodes[sc], device->keyBitmask)) {
439 outFlags[codeIndex] = 1;
440 break;
441 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 }
443 }
444 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700445 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700447 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700448}
449
Jeff Brown49ccac52012-04-11 18:27:33 -0700450status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
451 int32_t* outKeycode, uint32_t* outFlags) const {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700452 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800453 Device* device = getDeviceLocked(deviceId);
Jeff Brown49ccac52012-04-11 18:27:33 -0700454
Jeff Brown4a3862f2012-04-17 18:50:05 -0700455 if (device) {
456 // Check the key character map first.
457 sp<KeyCharacterMap> kcm = device->getKeyCharacterMap();
458 if (kcm != NULL) {
459 if (!kcm->mapKey(scanCode, usageCode, outKeycode)) {
460 *outFlags = 0;
461 return NO_ERROR;
462 }
463 }
464
465 // Check the key layout next.
466 if (device->keyMap.haveKeyLayout()) {
467 if (!device->keyMap.keyLayoutMap->mapKey(
468 scanCode, usageCode, outKeycode, outFlags)) {
469 return NO_ERROR;
470 }
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700471 }
472 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700473
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700474 *outKeycode = 0;
475 *outFlags = 0;
476 return NAME_NOT_FOUND;
477}
478
Jeff Brown49ccac52012-04-11 18:27:33 -0700479status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800480 AutoMutex _l(mLock);
481 Device* device = getDeviceLocked(deviceId);
482
483 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700484 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800485 if (err == NO_ERROR) {
486 return NO_ERROR;
487 }
488 }
489
Jeff Brown6f2fba42011-02-19 01:08:02 -0800490 return NAME_NOT_FOUND;
491}
492
Jeff Brown1a84fd12011-06-02 01:26:32 -0700493void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700494 AutoMutex _l(mLock);
495
Jeff Brown1a84fd12011-06-02 01:26:32 -0700496 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400497}
498
Jeff Brown49754db2011-07-01 17:37:58 -0700499bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
500 AutoMutex _l(mLock);
501 Device* device = getDeviceLocked(deviceId);
502 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
503 if (test_bit(scanCode, device->keyBitmask)) {
504 return true;
505 }
506 }
507 return false;
508}
509
Jeff Brown497a92c2010-09-12 17:55:08 -0700510bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
511 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800512 Device* device = getDeviceLocked(deviceId);
Michael Wrighted28fc82013-10-18 15:26:48 -0700513 int32_t sc;
514 if (device && mapLed(device, led, &sc) == NO_ERROR) {
515 if (test_bit(sc, device->ledBitmask)) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700516 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700517 }
518 }
519 return false;
520}
521
522void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
523 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800524 Device* device = getDeviceLocked(deviceId);
Michael Wrighted28fc82013-10-18 15:26:48 -0700525 setLedStateLocked(device, led, on);
526}
527
528void EventHub::setLedStateLocked(Device* device, int32_t led, bool on) {
529 int32_t sc;
530 if (device && !device->isVirtual() && mapLed(device, led, &sc) != NAME_NOT_FOUND) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700531 struct input_event ev;
532 ev.time.tv_sec = 0;
533 ev.time.tv_usec = 0;
534 ev.type = EV_LED;
Michael Wrighted28fc82013-10-18 15:26:48 -0700535 ev.code = sc;
Jeff Brown497a92c2010-09-12 17:55:08 -0700536 ev.value = on ? 1 : 0;
537
538 ssize_t nWrite;
539 do {
540 nWrite = write(device->fd, &ev, sizeof(struct input_event));
541 } while (nWrite == -1 && errno == EINTR);
542 }
543}
544
Jeff Brown90655042010-12-02 13:50:46 -0800545void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
546 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
547 outVirtualKeys.clear();
548
549 AutoMutex _l(mLock);
550 Device* device = getDeviceLocked(deviceId);
551 if (device && device->virtualKeyMap) {
552 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
553 }
554}
555
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700556sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Jeff Brown1e08fe92011-11-15 17:48:10 -0800557 AutoMutex _l(mLock);
558 Device* device = getDeviceLocked(deviceId);
559 if (device) {
Jeff Brown4a3862f2012-04-17 18:50:05 -0700560 return device->getKeyCharacterMap();
Jeff Brown1e08fe92011-11-15 17:48:10 -0800561 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700562 return NULL;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800563}
564
Jeff Brown6ec6f792012-04-17 16:52:41 -0700565bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId,
566 const sp<KeyCharacterMap>& map) {
567 AutoMutex _l(mLock);
568 Device* device = getDeviceLocked(deviceId);
569 if (device) {
570 if (map != device->overlayKeyMap) {
571 device->overlayKeyMap = map;
572 device->combinedKeyMap = KeyCharacterMap::combine(
573 device->keyMap.keyCharacterMap, map);
574 return true;
575 }
576 }
577 return false;
578}
579
RoboErikc1e00152013-12-11 17:02:46 -0800580static String8 generateDescriptor(InputDeviceIdentifier& identifier) {
581 String8 rawDescriptor;
582 rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor,
583 identifier.product);
584 // TODO add handling for USB devices to not uniqueify kbs that show up twice
585 if (!identifier.uniqueId.isEmpty()) {
586 rawDescriptor.append("uniqueId:");
587 rawDescriptor.append(identifier.uniqueId);
588 } else if (identifier.nonce != 0) {
589 rawDescriptor.appendFormat("nonce:%04x", identifier.nonce);
590 }
591
592 if (identifier.vendor == 0 && identifier.product == 0) {
593 // If we don't know the vendor and product id, then the device is probably
594 // built-in so we need to rely on other information to uniquely identify
595 // the input device. Usually we try to avoid relying on the device name or
596 // location but for built-in input device, they are unlikely to ever change.
597 if (!identifier.name.isEmpty()) {
598 rawDescriptor.append("name:");
599 rawDescriptor.append(identifier.name);
600 } else if (!identifier.location.isEmpty()) {
601 rawDescriptor.append("location:");
602 rawDescriptor.append(identifier.location);
603 }
604 }
605 identifier.descriptor = sha1(rawDescriptor);
606 return rawDescriptor;
607}
608
609void EventHub::assignDescriptorLocked(InputDeviceIdentifier& identifier) {
610 // Compute a device descriptor that uniquely identifies the device.
611 // The descriptor is assumed to be a stable identifier. Its value should not
612 // change between reboots, reconnections, firmware updates or new releases
613 // of Android. In practice we sometimes get devices that cannot be uniquely
614 // identified. In this case we enforce uniqueness between connected devices.
615 // Ideally, we also want the descriptor to be short and relatively opaque.
616
617 identifier.nonce = 0;
618 String8 rawDescriptor = generateDescriptor(identifier);
619 if (identifier.uniqueId.isEmpty()) {
620 // If it didn't have a unique id check for conflicts and enforce
621 // uniqueness if necessary.
622 while(getDeviceByDescriptorLocked(identifier.descriptor) != NULL) {
623 identifier.nonce++;
624 rawDescriptor = generateDescriptor(identifier);
625 }
626 }
627 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
628 identifier.descriptor.string());
629}
630
Jeff Browna47425a2012-04-13 04:09:27 -0700631void EventHub::vibrate(int32_t deviceId, nsecs_t duration) {
632 AutoMutex _l(mLock);
633 Device* device = getDeviceLocked(deviceId);
634 if (device && !device->isVirtual()) {
635 ff_effect effect;
636 memset(&effect, 0, sizeof(effect));
637 effect.type = FF_RUMBLE;
638 effect.id = device->ffEffectId;
639 effect.u.rumble.strong_magnitude = 0xc000;
640 effect.u.rumble.weak_magnitude = 0xc000;
641 effect.replay.length = (duration + 999999LL) / 1000000LL;
642 effect.replay.delay = 0;
643 if (ioctl(device->fd, EVIOCSFF, &effect)) {
644 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
645 device->identifier.name.string(), errno);
646 return;
647 }
648 device->ffEffectId = effect.id;
649
650 struct input_event ev;
651 ev.time.tv_sec = 0;
652 ev.time.tv_usec = 0;
653 ev.type = EV_FF;
654 ev.code = device->ffEffectId;
655 ev.value = 1;
656 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
657 ALOGW("Could not start force feedback effect on device %s due to error %d.",
658 device->identifier.name.string(), errno);
659 return;
660 }
661 device->ffEffectPlaying = true;
662 }
663}
664
665void EventHub::cancelVibrate(int32_t deviceId) {
666 AutoMutex _l(mLock);
667 Device* device = getDeviceLocked(deviceId);
668 if (device && !device->isVirtual()) {
669 if (device->ffEffectPlaying) {
670 device->ffEffectPlaying = false;
671
672 struct input_event ev;
673 ev.time.tv_sec = 0;
674 ev.time.tv_usec = 0;
675 ev.type = EV_FF;
676 ev.code = device->ffEffectId;
677 ev.value = 0;
678 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
679 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
680 device->identifier.name.string(), errno);
681 return;
682 }
683 }
684 }
685}
686
RoboErikc1e00152013-12-11 17:02:46 -0800687EventHub::Device* EventHub::getDeviceByDescriptorLocked(String8& descriptor) const {
688 size_t size = mDevices.size();
689 for (size_t i = 0; i < size; i++) {
690 Device* device = mDevices.valueAt(i);
691 if (descriptor.compare(device->identifier.descriptor) == 0) {
692 return device;
693 }
694 }
695 return NULL;
696}
697
Jeff Brown90655042010-12-02 13:50:46 -0800698EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700699 if (deviceId == BUILT_IN_KEYBOARD_ID) {
Jeff Brown90655042010-12-02 13:50:46 -0800700 deviceId = mBuiltInKeyboardId;
701 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700702 ssize_t index = mDevices.indexOfKey(deviceId);
703 return index >= 0 ? mDevices.valueAt(index) : NULL;
704}
Jeff Brown90655042010-12-02 13:50:46 -0800705
Jeff Brown93fa9b32011-06-14 17:09:25 -0700706EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
707 for (size_t i = 0; i < mDevices.size(); i++) {
708 Device* device = mDevices.valueAt(i);
709 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800710 return device;
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713 return NULL;
714}
715
Jeff Brownb7198742011-03-18 18:14:26 -0700716size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Steve Blockec193de2012-01-09 18:35:44 +0000717 ALOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400718
Jeff Brown93fa9b32011-06-14 17:09:25 -0700719 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400720
Jeff Brownb7198742011-03-18 18:14:26 -0700721 struct input_event readBuffer[bufferSize];
722
723 RawEvent* event = buffer;
724 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700725 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700726 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700727 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
728
Jeff Brown1a84fd12011-06-02 01:26:32 -0700729 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700730 if (mNeedToReopenDevices) {
731 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700732
Steve Block6215d3f2012-01-04 20:05:49 +0000733 ALOGI("Reopening all input devices due to a configuration change.");
Jeff Brown1a84fd12011-06-02 01:26:32 -0700734
Jeff Brown93fa9b32011-06-14 17:09:25 -0700735 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700736 mNeedToScanDevices = true;
737 break; // return to the caller before we actually rescan
738 }
739
Jeff Browncc2e7172010-08-17 16:48:25 -0700740 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700741 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800742 Device* device = mClosingDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100743 ALOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 device->id, device->path.string());
745 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700746 event->when = now;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700747 event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
Jeff Brownb7198742011-03-18 18:14:26 -0700748 event->type = DEVICE_REMOVED;
749 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700751 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700752 if (--capacity == 0) {
753 break;
754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700756
Jeff Brown1a84fd12011-06-02 01:26:32 -0700757 if (mNeedToScanDevices) {
758 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700759 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700760 mNeedToSendFinishedDeviceScan = true;
761 }
762
Jeff Brownb7198742011-03-18 18:14:26 -0700763 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800764 Device* device = mOpeningDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100765 ALOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 device->id, device->path.string());
767 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700768 event->when = now;
769 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
770 event->type = DEVICE_ADDED;
771 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700772 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700773 if (--capacity == 0) {
774 break;
775 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700776 }
777
778 if (mNeedToSendFinishedDeviceScan) {
779 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700780 event->when = now;
781 event->type = FINISHED_DEVICE_SCAN;
782 event += 1;
783 if (--capacity == 0) {
784 break;
785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 }
787
Jeff Browncc2e7172010-08-17 16:48:25 -0700788 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700789 bool deviceChanged = false;
790 while (mPendingEventIndex < mPendingEventCount) {
791 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
792 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
793 if (eventItem.events & EPOLLIN) {
794 mPendingINotify = true;
795 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000796 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700797 }
798 continue;
799 }
800
801 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
802 if (eventItem.events & EPOLLIN) {
Steve Block71f2cf12011-10-20 11:56:00 +0100803 ALOGV("awoken after wake()");
Jeff Brown93fa9b32011-06-14 17:09:25 -0700804 awoken = true;
805 char buffer[16];
806 ssize_t nRead;
807 do {
808 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
809 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
810 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000811 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700812 eventItem.events);
813 }
814 continue;
815 }
816
817 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
818 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000819 ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700820 eventItem.events, eventItem.data.u32);
821 continue;
822 }
823
824 Device* device = mDevices.valueAt(deviceIndex);
825 if (eventItem.events & EPOLLIN) {
826 int32_t readSize = read(device->fd, readBuffer,
827 sizeof(struct input_event) * capacity);
828 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
829 // Device was removed before INotify noticed.
Jeff Brown41305542011-10-05 11:14:13 -0700830 ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d "
Narayan Kamath22d07462014-03-27 12:50:58 +0000831 "capacity: %zu errno: %d)\n",
Jeff Brown41305542011-10-05 11:14:13 -0700832 device->fd, readSize, bufferSize, capacity, errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700833 deviceChanged = true;
834 closeDeviceLocked(device);
835 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700836 if (errno != EAGAIN && errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000837 ALOGW("could not get event (errno=%d)", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700838 }
839 } else if ((readSize % sizeof(struct input_event)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000840 ALOGE("could not get event (wrong size: %d)", readSize);
Jeff Browncc2e7172010-08-17 16:48:25 -0700841 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700842 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
843
844 size_t count = size_t(readSize) / sizeof(struct input_event);
845 for (size_t i = 0; i < count; i++) {
Jeff Brown4dac9012013-04-10 01:03:19 -0700846 struct input_event& iev = readBuffer[i];
847 ALOGV("%s got: time=%d.%06d, type=%d, code=%d, value=%d",
JP Abgrall25a465b2012-05-16 10:33:49 -0700848 device->path.string(),
849 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
850 iev.type, iev.code, iev.value);
851
Jeff Brown4dac9012013-04-10 01:03:19 -0700852 // Some input devices may have a better concept of the time
853 // when an input event was actually generated than the kernel
854 // which simply timestamps all events on entry to evdev.
855 // This is a custom Android extension of the input protocol
856 // mainly intended for use with uinput based device drivers.
857 if (iev.type == EV_MSC) {
858 if (iev.code == MSC_ANDROID_TIME_SEC) {
859 device->timestampOverrideSec = iev.value;
860 continue;
861 } else if (iev.code == MSC_ANDROID_TIME_USEC) {
862 device->timestampOverrideUsec = iev.value;
863 continue;
864 }
865 }
866 if (device->timestampOverrideSec || device->timestampOverrideUsec) {
867 iev.time.tv_sec = device->timestampOverrideSec;
868 iev.time.tv_usec = device->timestampOverrideUsec;
869 if (iev.type == EV_SYN && iev.code == SYN_REPORT) {
870 device->timestampOverrideSec = 0;
871 device->timestampOverrideUsec = 0;
872 }
873 ALOGV("applied override time %d.%06d",
874 int(iev.time.tv_sec), int(iev.time.tv_usec));
875 }
876
Jeff Brown4e91a182011-04-07 11:38:09 -0700877#ifdef HAVE_POSIX_CLOCKS
878 // Use the time specified in the event instead of the current time
879 // so that downstream code can get more accurate estimates of
880 // event dispatch latency from the time the event is enqueued onto
881 // the evdev client buffer.
882 //
883 // The event's timestamp fortuitously uses the same monotonic clock
884 // time base as the rest of Android. The kernel event device driver
885 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
886 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
887 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
888 // system call that also queries ktime_get_ts().
889 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
890 + nsecs_t(iev.time.tv_usec) * 1000LL;
JP Abgrall25a465b2012-05-16 10:33:49 -0700891 ALOGV("event time %lld, now %lld", event->when, now);
Jeff Brownf33b2b22012-10-05 17:59:56 -0700892
893 // Bug 7291243: Add a guard in case the kernel generates timestamps
894 // that appear to be far into the future because they were generated
895 // using the wrong clock source.
896 //
897 // This can happen because when the input device is initially opened
898 // it has a default clock source of CLOCK_REALTIME. Any input events
899 // enqueued right after the device is opened will have timestamps
900 // generated using CLOCK_REALTIME. We later set the clock source
901 // to CLOCK_MONOTONIC but it is already too late.
902 //
903 // Invalid input event timestamps can result in ANRs, crashes and
904 // and other issues that are hard to track down. We must not let them
905 // propagate through the system.
906 //
907 // Log a warning so that we notice the problem and recover gracefully.
908 if (event->when >= now + 10 * 1000000000LL) {
909 // Double-check. Time may have moved on.
910 nsecs_t time = systemTime(SYSTEM_TIME_MONOTONIC);
911 if (event->when > time) {
912 ALOGW("An input event from %s has a timestamp that appears to "
913 "have been generated using the wrong clock source "
914 "(expected CLOCK_MONOTONIC): "
915 "event time %lld, current time %lld, call time %lld. "
916 "Using current time instead.",
917 device->path.string(), event->when, time, now);
918 event->when = time;
919 } else {
920 ALOGV("Event time is ok but failed the fast path and required "
921 "an extra call to systemTime: "
922 "event time %lld, current time %lld, call time %lld.",
923 event->when, time, now);
924 }
925 }
Jeff Brown4e91a182011-04-07 11:38:09 -0700926#else
Jeff Brownb7198742011-03-18 18:14:26 -0700927 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700928#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700929 event->deviceId = deviceId;
930 event->type = iev.type;
Jeff Brown49ccac52012-04-11 18:27:33 -0700931 event->code = iev.code;
Jeff Brownb7198742011-03-18 18:14:26 -0700932 event->value = iev.value;
Jeff Brownb7198742011-03-18 18:14:26 -0700933 event += 1;
Jeff Brown4dac9012013-04-10 01:03:19 -0700934 capacity -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700935 }
Jeff Brownb7198742011-03-18 18:14:26 -0700936 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700937 // The result buffer is full. Reset the pending event index
938 // so we will try to read the device again on the next iteration.
939 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700940 break;
941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700943 } else if (eventItem.events & EPOLLHUP) {
944 ALOGI("Removing device %s due to epoll hang-up event.",
945 device->identifier.name.string());
946 deviceChanged = true;
947 closeDeviceLocked(device);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700948 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000949 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700950 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700953
Jeff Brown93fa9b32011-06-14 17:09:25 -0700954 // readNotify() will modify the list of devices so this must be done after
955 // processing all other events to ensure that we read all remaining events
956 // before closing the devices.
957 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
958 mPendingINotify = false;
959 readNotifyLocked();
960 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800961 }
962
Jeff Brown93fa9b32011-06-14 17:09:25 -0700963 // Report added or removed devices immediately.
964 if (deviceChanged) {
965 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 }
Jeff Browna9b84222010-10-14 02:23:43 -0700967
Jeff Brown93fa9b32011-06-14 17:09:25 -0700968 // Return now if we have collected any events or if we were explicitly awoken.
969 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700970 break;
971 }
972
Jeff Browncc2e7172010-08-17 16:48:25 -0700973 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700974 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700975 // subtle choreography. When a device driver has pending (unread) events, it acquires
976 // a kernel wake lock. However, once the last pending event has been read, the device
977 // driver will release the kernel wake lock. To prevent the system from going to sleep
978 // when this happens, the EventHub holds onto its own user wake lock while the client
979 // is processing events. Thus the system can only sleep if there are no events
980 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700981 //
982 // The timeout is advisory only. If the device is asleep, it will not wake just to
983 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700984 mPendingEventIndex = 0;
985
986 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700987 release_wake_lock(WAKE_LOCK_ID);
988
Jeff Brown93fa9b32011-06-14 17:09:25 -0700989 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700990
991 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700992 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700993
Jeff Brownaa3855d2011-03-17 01:34:19 -0700994 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700995 // Timed out.
996 mPendingEventCount = 0;
997 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700998 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700999
Jeff Brownaa3855d2011-03-17 01:34:19 -07001000 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001001 // An error occurred.
1002 mPendingEventCount = 0;
1003
Jeff Brownb7198742011-03-18 18:14:26 -07001004 // Sleep after errors to avoid locking up the system.
1005 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -07001006 if (errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +00001007 ALOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -07001008 usleep(100000);
1009 }
Jeff Brownb7198742011-03-18 18:14:26 -07001010 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001011 // Some events occurred.
1012 mPendingEventCount = size_t(pollResult);
Jeff Browncc2e7172010-08-17 16:48:25 -07001013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
Jeff Brownb7198742011-03-18 18:14:26 -07001015
1016 // All done, return the number of events we read.
1017 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018}
1019
Jeff Brown93fa9b32011-06-14 17:09:25 -07001020void EventHub::wake() {
Steve Block71f2cf12011-10-20 11:56:00 +01001021 ALOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022
Jeff Brown93fa9b32011-06-14 17:09:25 -07001023 ssize_t nWrite;
1024 do {
1025 nWrite = write(mWakeWritePipeFd, "W", 1);
1026 } while (nWrite == -1 && errno == EINTR);
1027
1028 if (nWrite != 1 && errno != EAGAIN) {
Steve Block8564c8d2012-01-05 23:22:43 +00001029 ALOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
Jeff Brown1a84fd12011-06-02 01:26:32 -07001031}
Jeff Brown90655042010-12-02 13:50:46 -08001032
Jeff Brown93fa9b32011-06-14 17:09:25 -07001033void EventHub::scanDevicesLocked() {
1034 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 if(res < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001036 ALOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001038 if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) {
1039 createVirtualKeyboardLocked();
1040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041}
1042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043// ----------------------------------------------------------------------------
1044
Jeff Brownfd035822010-06-30 16:10:35 -07001045static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
1046 const uint8_t* end = array + endIndex;
1047 array += startIndex;
1048 while (array != end) {
1049 if (*(array++) != 0) {
1050 return true;
1051 }
1052 }
1053 return false;
1054}
1055
1056static const int32_t GAMEPAD_KEYCODES[] = {
1057 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
1058 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
1059 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
1060 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
1061 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -08001062 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
Jeff Brownfd035822010-06-30 16:10:35 -07001063};
1064
Jeff Brown93fa9b32011-06-14 17:09:25 -07001065status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -08001066 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067
Steve Block71f2cf12011-10-20 11:56:00 +01001068 ALOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
Jeff Brown874c1e92012-01-19 14:32:47 -08001070 int fd = open(devicePath, O_RDWR | O_CLOEXEC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 if(fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001072 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 return -1;
1074 }
1075
Jeff Brown90655042010-12-02 13:50:46 -08001076 InputDeviceIdentifier identifier;
1077
1078 // Get device name.
1079 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
1080 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
1081 } else {
1082 buffer[sizeof(buffer) - 1] = '\0';
1083 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
Mike Lockwood15431a92009-07-17 00:10:10 -04001085
Jeff Brown90655042010-12-02 13:50:46 -08001086 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -07001087 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
1088 const String8& item = mExcludedDevices.itemAt(i);
1089 if (identifier.name == item) {
Steve Block6215d3f2012-01-04 20:05:49 +00001090 ALOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -04001091 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -04001092 return -1;
1093 }
1094 }
1095
Jeff Brown90655042010-12-02 13:50:46 -08001096 // Get device driver version.
1097 int driverVersion;
1098 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Steve Block3762c312012-01-06 19:20:56 +00001099 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001100 close(fd);
1101 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103
Jeff Brown90655042010-12-02 13:50:46 -08001104 // Get device identifier.
1105 struct input_id inputId;
1106 if(ioctl(fd, EVIOCGID, &inputId)) {
Steve Block3762c312012-01-06 19:20:56 +00001107 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -08001108 close(fd);
1109 return -1;
1110 }
1111 identifier.bus = inputId.bustype;
1112 identifier.product = inputId.product;
1113 identifier.vendor = inputId.vendor;
1114 identifier.version = inputId.version;
1115
1116 // Get device physical location.
1117 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
1118 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
1119 } else {
1120 buffer[sizeof(buffer) - 1] = '\0';
1121 identifier.location.setTo(buffer);
1122 }
1123
1124 // Get device unique id.
1125 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
1126 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
1127 } else {
1128 buffer[sizeof(buffer) - 1] = '\0';
1129 identifier.uniqueId.setTo(buffer);
1130 }
1131
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001132 // Fill in the descriptor.
RoboErikc1e00152013-12-11 17:02:46 -08001133 assignDescriptorLocked(identifier);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001134
Jeff Brown90655042010-12-02 13:50:46 -08001135 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -07001136 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
Steve Block3762c312012-01-06 19:20:56 +00001137 ALOGE("Error %d making device file descriptor non-blocking.", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -07001138 close(fd);
1139 return -1;
1140 }
1141
Jeff Brown90655042010-12-02 13:50:46 -08001142 // Allocate device. (The device object takes ownership of the fd at this point.)
1143 int32_t deviceId = mNextDeviceId++;
1144 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145
Jeff Browne38fdfa2012-04-06 14:51:01 -07001146 ALOGV("add device %d: %s\n", deviceId, devicePath);
1147 ALOGV(" bus: %04x\n"
1148 " vendor %04x\n"
1149 " product %04x\n"
1150 " version %04x\n",
Jeff Brown90655042010-12-02 13:50:46 -08001151 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001152 ALOGV(" name: \"%s\"\n", identifier.name.string());
1153 ALOGV(" location: \"%s\"\n", identifier.location.string());
1154 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string());
Jeff Brown49ccac52012-04-11 18:27:33 -07001155 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001156 ALOGV(" driver: v%d.%d.%d\n",
Jeff Brown90655042010-12-02 13:50:46 -08001157 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001159 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001160 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001161
Jeff Brownfd035822010-06-30 16:10:35 -07001162 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001163 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
1164 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
1165 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
1166 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
1167 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
Jeff Browna47425a2012-04-13 04:09:27 -07001168 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001169 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -08001170
Jeff Brown6f2fba42011-02-19 01:08:02 -08001171 // See if this is a keyboard. Ignore everything in the button range except for
1172 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001173 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
1174 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -08001175 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001176 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001177 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -07001178 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001179 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -08001180 if (haveKeyboardKeys || haveGamepadButtons) {
1181 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001183
Jeff Brown83c09682010-12-23 17:50:18 -08001184 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001185 if (test_bit(BTN_MOUSE, device->keyBitmask)
1186 && test_bit(REL_X, device->relBitmask)
1187 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001188 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
Jeff Brownfd035822010-06-30 16:10:35 -07001190
1191 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -08001192 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001193 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
1194 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001195 // Some joysticks such as the PS3 controller report axes that conflict
1196 // with the ABS_MT range. Try to confirm that the device really is
1197 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001198 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001199 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd035822010-06-30 16:10:35 -07001200 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001201 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001202 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
1203 && test_bit(ABS_X, device->absBitmask)
1204 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001205 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001208 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001209 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1210 // from other devices such as accelerometers that also have absolute axes.
Jeff Brown9ee285a2011-08-31 12:56:34 -07001211 if (haveGamepadButtons) {
1212 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
1213 for (int i = 0; i <= ABS_MAX; i++) {
1214 if (test_bit(i, device->absBitmask)
1215 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
1216 device->classes = assumedClasses;
1217 break;
1218 }
1219 }
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001220 }
1221
Jeff Brown93fa9b32011-06-14 17:09:25 -07001222 // Check whether this device has switches.
1223 for (int i = 0; i <= SW_MAX; i++) {
1224 if (test_bit(i, device->swBitmask)) {
1225 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
1226 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229
Jeff Browna47425a2012-04-13 04:09:27 -07001230 // Check whether this device supports the vibrator.
1231 if (test_bit(FF_RUMBLE, device->ffBitmask)) {
1232 device->classes |= INPUT_DEVICE_CLASS_VIBRATOR;
1233 }
1234
Jeff Brown93fa9b32011-06-14 17:09:25 -07001235 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -08001236 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -08001237 // Load the virtual keys for the touch screen, if any.
1238 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001239 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -08001240 if (!status) {
1241 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
Jeff Brown90655042010-12-02 13:50:46 -08001243 }
1244
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001245 // Load the key map.
1246 // We need to do this for joysticks too because the key layout may specify axes.
1247 status_t keyMapStatus = NAME_NOT_FOUND;
1248 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -08001249 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001250 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001251 }
Jeff Brown90655042010-12-02 13:50:46 -08001252
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001253 // Configure the keyboard, gamepad or virtual keyboard.
1254 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001255 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001256 if (!keyMapStatus
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001257 && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD
Jeff Brown90655042010-12-02 13:50:46 -08001258 && isEligibleBuiltInKeyboard(device->identifier,
1259 device->configuration, &device->keyMap)) {
1260 mBuiltInKeyboardId = device->id;
Jeff Brown497a92c2010-09-12 17:55:08 -07001261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262
Ken Wakasa02a44f72013-07-05 04:08:36 +00001263 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
1264 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
1265 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
1266 }
1267
Jeff Brownfd035822010-06-30 16:10:35 -07001268 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -07001269 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1270 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1271 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1272 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1273 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001274 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001275 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001276
Jeff Brownfd035822010-06-30 16:10:35 -07001277 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001278 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001279 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd035822010-06-30 16:10:35 -07001280 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1281 break;
1282 }
1283 }
Michael Wrighta0a72852013-02-21 23:51:45 -08001284
1285 // Disable kernel key repeat since we handle it ourselves
1286 unsigned int repeatRate[] = {0,0};
1287 if (ioctl(fd, EVIOCSREP, repeatRate)) {
1288 ALOGW("Unable to disable kernel key repeat for %s: %s", devicePath, strerror(errno));
1289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291
Sean McNeilaeb00c42010-06-23 16:00:37 +07001292 // If the device isn't recognized as something we handle, don't monitor it.
1293 if (device->classes == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001294 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Jeff Brown90655042010-12-02 13:50:46 -08001295 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001296 delete device;
1297 return -1;
1298 }
1299
Jeff Brown56194eb2011-03-02 19:23:13 -08001300 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001301 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001302 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1303 }
1304
Michael Wrightb0aa4822014-03-12 12:56:51 -07001305 if (device->classes & (INPUT_DEVICE_CLASS_JOYSTICK | INPUT_DEVICE_CLASS_DPAD)
1306 && device->classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Michael Wrightac6c78b2013-07-17 13:21:45 -07001307 device->controllerNumber = getNextControllerNumberLocked(device);
Michael Wrighted28fc82013-10-18 15:26:48 -07001308 setLedForController(device);
Michael Wrightac6c78b2013-07-17 13:21:45 -07001309 }
1310
Jeff Brown93fa9b32011-06-14 17:09:25 -07001311 // Register with epoll.
1312 struct epoll_event eventItem;
1313 memset(&eventItem, 0, sizeof(eventItem));
1314 eventItem.events = EPOLLIN;
1315 eventItem.data.u32 = deviceId;
1316 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
Steve Block3762c312012-01-06 19:20:56 +00001317 ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001318 delete device;
1319 return -1;
1320 }
1321
Jeff Browne22afbe2011-12-16 13:45:40 -08001322 // Enable wake-lock behavior on kernels that support it.
1323 // TODO: Only need this for devices that can really wake the system.
Elliott Hughes6a2e9bc2013-11-12 13:16:37 -08001324#ifndef EVIOCSSUSPENDBLOCK
1325 // uapi headers don't include EVIOCSSUSPENDBLOCK, and future kernels
1326 // will use an epoll flag instead, so as long as we want to support
1327 // this feature, we need to be prepared to define the ioctl ourselves.
1328#define EVIOCSSUSPENDBLOCK _IOW('E', 0x91, int)
1329#endif
Jeff Browneca3cf52012-04-06 19:31:36 -07001330 bool usingSuspendBlockIoctl = !ioctl(fd, EVIOCSSUSPENDBLOCK, 1);
1331
1332 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
1333 // associated with input events. This is important because the input system
1334 // uses the timestamps extensively and assumes they were recorded using the monotonic
1335 // clock.
1336 //
1337 // In older kernel, before Linux 3.4, there was no way to tell the kernel which
1338 // clock to use to input event timestamps. The standard kernel behavior was to
1339 // record a real time timestamp, which isn't what we want. Android kernels therefore
1340 // contained a patch to the evdev_event() function in drivers/input/evdev.c to
1341 // replace the call to do_gettimeofday() with ktime_get_ts() to cause the monotonic
1342 // clock to be used instead of the real time clock.
1343 //
1344 // As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock.
1345 // Therefore, we no longer require the Android-specific kernel patch described above
1346 // as long as we make sure to set select the monotonic clock. We do that here.
Jeff Browna75fe052012-05-01 18:41:26 -07001347 int clockId = CLOCK_MONOTONIC;
1348 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &clockId);
Jeff Browne22afbe2011-12-16 13:45:40 -08001349
Steve Block6215d3f2012-01-04 20:05:49 +00001350 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
Jeff Browne22afbe2011-12-16 13:45:40 -08001351 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
Jeff Browneca3cf52012-04-06 19:31:36 -07001352 "usingSuspendBlockIoctl=%s, usingClockIoctl=%s",
Jeff Brown90655042010-12-02 13:50:46 -08001353 deviceId, fd, devicePath, device->identifier.name.string(),
1354 device->classes,
1355 device->configurationFile.string(),
1356 device->keyMap.keyLayoutFile.string(),
1357 device->keyMap.keyCharacterMapFile.string(),
Jeff Browne22afbe2011-12-16 13:45:40 -08001358 toString(mBuiltInKeyboardId == deviceId),
Jeff Browneca3cf52012-04-06 19:31:36 -07001359 toString(usingSuspendBlockIoctl), toString(usingClockIoctl));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001360
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001361 addDeviceLocked(device);
1362 return 0;
1363}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001365void EventHub::createVirtualKeyboardLocked() {
1366 InputDeviceIdentifier identifier;
1367 identifier.name = "Virtual";
1368 identifier.uniqueId = "<virtual>";
RoboErikc1e00152013-12-11 17:02:46 -08001369 assignDescriptorLocked(identifier);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001370
1371 Device* device = new Device(-1, VIRTUAL_KEYBOARD_ID, String8("<virtual>"), identifier);
1372 device->classes = INPUT_DEVICE_CLASS_KEYBOARD
1373 | INPUT_DEVICE_CLASS_ALPHAKEY
1374 | INPUT_DEVICE_CLASS_DPAD
1375 | INPUT_DEVICE_CLASS_VIRTUAL;
1376 loadKeyMapLocked(device);
1377 addDeviceLocked(device);
1378}
1379
1380void EventHub::addDeviceLocked(Device* device) {
1381 mDevices.add(device->id, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 device->next = mOpeningDevices;
1383 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384}
1385
Jeff Brown93fa9b32011-06-14 17:09:25 -07001386void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001387 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1388 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001389 if (device->configurationFile.isEmpty()) {
Steve Block5baa3a62011-12-20 16:23:08 +00001390 ALOGD("No input device configuration file found for device '%s'.",
Jeff Brown90655042010-12-02 13:50:46 -08001391 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001392 } else {
1393 status_t status = PropertyMap::load(device->configurationFile,
1394 &device->configuration);
1395 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001396 ALOGE("Error loading input device configuration file for device '%s'. "
Jeff Brown90655042010-12-02 13:50:46 -08001397 "Using default configuration.",
1398 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001399 }
1400 }
1401}
1402
Jeff Brown93fa9b32011-06-14 17:09:25 -07001403status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001404 // The virtual key map is supplied by the kernel as a system board property file.
1405 String8 path;
1406 path.append("/sys/board_properties/virtualkeys.");
1407 path.append(device->identifier.name);
1408 if (access(path.string(), R_OK)) {
1409 return NAME_NOT_FOUND;
1410 }
1411 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001412}
1413
Jeff Brown93fa9b32011-06-14 17:09:25 -07001414status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001415 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001416}
1417
Jeff Brown93fa9b32011-06-14 17:09:25 -07001418bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001419 if (device->configuration) {
1420 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001421 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1422 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001423 }
1424 }
1425 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1426}
1427
Michael Wrightac6c78b2013-07-17 13:21:45 -07001428int32_t EventHub::getNextControllerNumberLocked(Device* device) {
1429 if (mControllerNumbers.isFull()) {
1430 ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
1431 device->identifier.name.string());
1432 return 0;
1433 }
1434 // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
1435 // one
1436 return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
1437}
1438
1439void EventHub::releaseControllerNumberLocked(Device* device) {
1440 int32_t num = device->controllerNumber;
1441 device->controllerNumber= 0;
1442 if (num == 0) {
1443 return;
1444 }
1445 mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
1446}
1447
Michael Wrighted28fc82013-10-18 15:26:48 -07001448void EventHub::setLedForController(Device* device) {
1449 for (int i = 0; i < MAX_CONTROLLER_LEDS; i++) {
1450 setLedStateLocked(device, ALED_CONTROLLER_1 + i, device->controllerNumber == i + 1);
1451 }
1452}
Michael Wrightac6c78b2013-07-17 13:21:45 -07001453
Jeff Brown90655042010-12-02 13:50:46 -08001454bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1455 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001456 return false;
1457 }
1458
1459 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001460 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001461 const size_t N = scanCodes.size();
1462 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1463 int32_t sc = scanCodes.itemAt(i);
1464 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1465 return true;
1466 }
1467 }
1468
1469 return false;
1470}
1471
Michael Wrighted28fc82013-10-18 15:26:48 -07001472status_t EventHub::mapLed(Device* device, int32_t led, int32_t* outScanCode) const {
1473 if (!device->keyMap.haveKeyLayout() || !device->ledBitmask) {
1474 return NAME_NOT_FOUND;
1475 }
1476
1477 int32_t scanCode;
1478 if(device->keyMap.keyLayoutMap->findScanCodeForLed(led, &scanCode) != NAME_NOT_FOUND) {
1479 if(scanCode >= 0 && scanCode <= LED_MAX && test_bit(scanCode, device->ledBitmask)) {
1480 *outScanCode = scanCode;
1481 return NO_ERROR;
1482 }
1483 }
1484 return NAME_NOT_FOUND;
1485}
1486
Jeff Brown93fa9b32011-06-14 17:09:25 -07001487status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1488 Device* device = getDeviceByPathLocked(devicePath);
1489 if (device) {
1490 closeDeviceLocked(device);
1491 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
Steve Block71f2cf12011-10-20 11:56:00 +01001493 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 return -1;
1495}
1496
Jeff Brown93fa9b32011-06-14 17:09:25 -07001497void EventHub::closeAllDevicesLocked() {
1498 while (mDevices.size() > 0) {
1499 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1500 }
1501}
1502
1503void EventHub::closeDeviceLocked(Device* device) {
Steve Block6215d3f2012-01-04 20:05:49 +00001504 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001505 device->path.string(), device->identifier.name.string(), device->id,
1506 device->fd, device->classes);
1507
Jeff Brown33bbfd22011-02-24 20:55:35 -08001508 if (device->id == mBuiltInKeyboardId) {
Steve Block8564c8d2012-01-05 23:22:43 +00001509 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001510 device->path.string(), mBuiltInKeyboardId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001511 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001512 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001513
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001514 if (!device->isVirtual()) {
1515 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
1516 ALOGW("Could not remove device fd from epoll instance. errno=%d", errno);
1517 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001518 }
1519
Michael Wrightac6c78b2013-07-17 13:21:45 -07001520 releaseControllerNumberLocked(device);
1521
Jeff Brown93fa9b32011-06-14 17:09:25 -07001522 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001523 device->close();
1524
Jeff Brown8e9d4432011-03-12 19:46:59 -08001525 // Unlink for opening devices list if it is present.
1526 Device* pred = NULL;
1527 bool found = false;
1528 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1529 if (entry == device) {
1530 found = true;
1531 break;
1532 }
1533 pred = entry;
1534 entry = entry->next;
1535 }
1536 if (found) {
1537 // Unlink the device from the opening devices list then delete it.
1538 // We don't need to tell the client that the device was closed because
1539 // it does not even know it was opened in the first place.
Steve Block6215d3f2012-01-04 20:05:49 +00001540 ALOGI("Device %s was immediately closed after opening.", device->path.string());
Jeff Brown8e9d4432011-03-12 19:46:59 -08001541 if (pred) {
1542 pred->next = device->next;
1543 } else {
1544 mOpeningDevices = device->next;
1545 }
1546 delete device;
1547 } else {
1548 // Link into closing devices list.
1549 // The device will be deleted later after we have informed the client.
1550 device->next = mClosingDevices;
1551 mClosingDevices = device;
1552 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001553}
1554
Jeff Brown93fa9b32011-06-14 17:09:25 -07001555status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 int res;
1557 char devname[PATH_MAX];
1558 char *filename;
1559 char event_buf[512];
1560 int event_size;
1561 int event_pos = 0;
1562 struct inotify_event *event;
1563
Steve Block71f2cf12011-10-20 11:56:00 +01001564 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001565 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 if(res < (int)sizeof(*event)) {
1567 if(errno == EINTR)
1568 return 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001569 ALOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001570 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
1572 //printf("got %d bytes of event information\n", res);
1573
Jeff Brown90655042010-12-02 13:50:46 -08001574 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 filename = devname + strlen(devname);
1576 *filename++ = '/';
1577
1578 while(res >= (int)sizeof(*event)) {
1579 event = (struct inotify_event *)(event_buf + event_pos);
1580 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1581 if(event->len) {
1582 strcpy(filename, event->name);
1583 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001584 openDeviceLocked(devname);
1585 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00001586 ALOGI("Removing device '%s' due to inotify event\n", devname);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001587 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
1589 }
1590 event_size = sizeof(*event) + event->len;
1591 res -= event_size;
1592 event_pos += event_size;
1593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 return 0;
1595}
1596
Jeff Brown93fa9b32011-06-14 17:09:25 -07001597status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598{
1599 char devname[PATH_MAX];
1600 char *filename;
1601 DIR *dir;
1602 struct dirent *de;
1603 dir = opendir(dirname);
1604 if(dir == NULL)
1605 return -1;
1606 strcpy(devname, dirname);
1607 filename = devname + strlen(devname);
1608 *filename++ = '/';
1609 while((de = readdir(dir))) {
1610 if(de->d_name[0] == '.' &&
1611 (de->d_name[1] == '\0' ||
1612 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1613 continue;
1614 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001615 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
1617 closedir(dir);
1618 return 0;
1619}
1620
Jeff Brown93fa9b32011-06-14 17:09:25 -07001621void EventHub::requestReopenDevices() {
Steve Block71f2cf12011-10-20 11:56:00 +01001622 ALOGV("requestReopenDevices() called");
Jeff Brown93fa9b32011-06-14 17:09:25 -07001623
1624 AutoMutex _l(mLock);
1625 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001626}
1627
Jeff Brownf2f487182010-10-01 17:46:21 -07001628void EventHub::dump(String8& dump) {
1629 dump.append("Event Hub State:\n");
1630
1631 { // acquire lock
1632 AutoMutex _l(mLock);
1633
Jeff Brown90655042010-12-02 13:50:46 -08001634 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001635
1636 dump.append(INDENT "Devices:\n");
1637
Jeff Brown93fa9b32011-06-14 17:09:25 -07001638 for (size_t i = 0; i < mDevices.size(); i++) {
1639 const Device* device = mDevices.valueAt(i);
1640 if (mBuiltInKeyboardId == device->id) {
1641 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1642 device->id, device->identifier.name.string());
1643 } else {
1644 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1645 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001646 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001647 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1648 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001649 dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string());
Jeff Brown93fa9b32011-06-14 17:09:25 -07001650 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
Michael Wrightac6c78b2013-07-17 13:21:45 -07001651 dump.appendFormat(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001652 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1653 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1654 "product=0x%04x, version=0x%04x\n",
1655 device->identifier.bus, device->identifier.vendor,
1656 device->identifier.product, device->identifier.version);
1657 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1658 device->keyMap.keyLayoutFile.string());
1659 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1660 device->keyMap.keyCharacterMapFile.string());
1661 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1662 device->configurationFile.string());
Jeff Brown61c08242012-04-19 11:14:33 -07001663 dump.appendFormat(INDENT3 "HaveKeyboardLayoutOverlay: %s\n",
1664 toString(device->overlayKeyMap != NULL));
Jeff Brownf2f487182010-10-01 17:46:21 -07001665 }
1666 } // release lock
1667}
1668
Jeff Brown89ef0722011-08-10 16:25:21 -07001669void EventHub::monitor() {
1670 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1671 mLock.lock();
1672 mLock.unlock();
1673}
1674
1675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676}; // namespace android