blob: b788e16f1f5e4bf07cee46889ac67fddfca88c11 [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
Jeff Brown93fa9b32011-06-14 17:09:25 -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
Mathias Agopianb93a03f82012-02-17 15:34:57 -080039#include <androidfw/KeyLayoutMap.h>
40#include <androidfw/KeyCharacterMap.h>
41#include <androidfw/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Jeff Browne38fdfa2012-04-06 14:51:01 -070043#include <sha1.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044#include <string.h>
45#include <stdint.h>
46#include <dirent.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070047
48#include <sys/inotify.h>
49#include <sys/epoll.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include <sys/ioctl.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070051#include <sys/limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53/* this macro is used to tell if "bit" is set in "array"
54 * it selects a byte from the array, and does a boolean AND
55 * operation with a byte that only has the relevant bit set.
56 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
57 */
58#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
59
Jeff Brownfd0358292010-06-30 16:10:35 -070060/* this macro computes the number of bytes needed to represent a bit array of the specified size */
61#define sizeof_bit_array(bits) ((bits + 7) / 8)
62
Jeff Brownf2f487182010-10-01 17:46:21 -070063#define INDENT " "
64#define INDENT2 " "
65#define INDENT3 " "
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067namespace android {
68
69static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080070static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
72/* return the larger integer */
73static inline int max(int v1, int v2)
74{
75 return (v1 > v2) ? v1 : v2;
76}
77
Jeff Brownf2f487182010-10-01 17:46:21 -070078static inline const char* toString(bool value) {
79 return value ? "true" : "false";
80}
81
Jeff Browne38fdfa2012-04-06 14:51:01 -070082static String8 sha1(const String8& in) {
83 SHA1_CTX ctx;
84 SHA1Init(&ctx);
85 SHA1Update(&ctx, reinterpret_cast<const u_char*>(in.string()), in.size());
86 u_char digest[SHA1_DIGEST_LENGTH];
87 SHA1Final(digest, &ctx);
88
89 String8 out;
90 for (size_t i = 0; i < SHA1_DIGEST_LENGTH; i++) {
91 out.appendFormat("%02x", digest[i]);
92 }
93 return out;
94}
95
Jeff Brown9f25b7f2012-04-10 14:30:49 -070096static void setDescriptor(InputDeviceIdentifier& identifier) {
97 // Compute a device descriptor that uniquely identifies the device.
98 // The descriptor is assumed to be a stable identifier. Its value should not
99 // change between reboots, reconnections, firmware updates or new releases of Android.
100 // Ideally, we also want the descriptor to be short and relatively opaque.
101 String8 rawDescriptor;
102 rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor, identifier.product);
103 if (!identifier.uniqueId.isEmpty()) {
104 rawDescriptor.append("uniqueId:");
105 rawDescriptor.append(identifier.uniqueId);
106 } if (identifier.vendor == 0 && identifier.product == 0) {
107 // If we don't know the vendor and product id, then the device is probably
108 // built-in so we need to rely on other information to uniquely identify
109 // the input device. Usually we try to avoid relying on the device name or
110 // location but for built-in input device, they are unlikely to ever change.
111 if (!identifier.name.isEmpty()) {
112 rawDescriptor.append("name:");
113 rawDescriptor.append(identifier.name);
114 } else if (!identifier.location.isEmpty()) {
115 rawDescriptor.append("location:");
116 rawDescriptor.append(identifier.location);
117 }
118 }
119 identifier.descriptor = sha1(rawDescriptor);
Jeff Brown49ccac52012-04-11 18:27:33 -0700120 ALOGV("Created descriptor: raw=%s, cooked=%s", rawDescriptor.string(),
121 identifier.descriptor.string());
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700122}
123
Jeff Brown9ee285af2011-08-31 12:56:34 -0700124// --- Global Functions ---
125
126uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
127 // Touch devices get dibs on touch-related axes.
128 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
129 switch (axis) {
130 case ABS_X:
131 case ABS_Y:
132 case ABS_PRESSURE:
133 case ABS_TOOL_WIDTH:
134 case ABS_DISTANCE:
135 case ABS_TILT_X:
136 case ABS_TILT_Y:
137 case ABS_MT_SLOT:
138 case ABS_MT_TOUCH_MAJOR:
139 case ABS_MT_TOUCH_MINOR:
140 case ABS_MT_WIDTH_MAJOR:
141 case ABS_MT_WIDTH_MINOR:
142 case ABS_MT_ORIENTATION:
143 case ABS_MT_POSITION_X:
144 case ABS_MT_POSITION_Y:
145 case ABS_MT_TOOL_TYPE:
146 case ABS_MT_BLOB_ID:
147 case ABS_MT_TRACKING_ID:
148 case ABS_MT_PRESSURE:
149 case ABS_MT_DISTANCE:
150 return INPUT_DEVICE_CLASS_TOUCH;
151 }
152 }
153
154 // Joystick devices get the rest.
155 return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK;
156}
157
Jeff Brown90655042010-12-02 13:50:46 -0800158// --- EventHub::Device ---
159
160EventHub::Device::Device(int fd, int32_t id, const String8& path,
161 const InputDeviceIdentifier& identifier) :
162 next(NULL),
163 fd(fd), id(id), path(path), identifier(identifier),
Jeff Browna47425a2012-04-13 04:09:27 -0700164 classes(0), configuration(NULL), virtualKeyMap(NULL),
165 ffEffectPlaying(false), ffEffectId(-1) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700166 memset(keyBitmask, 0, sizeof(keyBitmask));
167 memset(absBitmask, 0, sizeof(absBitmask));
168 memset(relBitmask, 0, sizeof(relBitmask));
169 memset(swBitmask, 0, sizeof(swBitmask));
170 memset(ledBitmask, 0, sizeof(ledBitmask));
Jeff Browna47425a2012-04-13 04:09:27 -0700171 memset(ffBitmask, 0, sizeof(ffBitmask));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700172 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173}
174
Jeff Brown90655042010-12-02 13:50:46 -0800175EventHub::Device::~Device() {
176 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800177 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800178 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179}
180
Jeff Brown90655042010-12-02 13:50:46 -0800181void EventHub::Device::close() {
182 if (fd >= 0) {
183 ::close(fd);
184 fd = -1;
185 }
186}
187
188
189// --- EventHub ---
190
Jeff Brown93fa9b32011-06-14 17:09:25 -0700191const uint32_t EventHub::EPOLL_ID_INOTIFY;
192const uint32_t EventHub::EPOLL_ID_WAKE;
193const int EventHub::EPOLL_SIZE_HINT;
194const int EventHub::EPOLL_MAX_EVENTS;
195
Jeff Brown90655042010-12-02 13:50:46 -0800196EventHub::EventHub(void) :
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700197 mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1),
Jeff Brown90655042010-12-02 13:50:46 -0800198 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700199 mNeedToSendFinishedDeviceScan(false),
200 mNeedToReopenDevices(false), mNeedToScanDevices(true),
201 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700203
Jeff Brown93fa9b32011-06-14 17:09:25 -0700204 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
205 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
206
207 mINotifyFd = inotify_init();
208 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
209 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
210 DEVICE_PATH, errno);
211
212 struct epoll_event eventItem;
213 memset(&eventItem, 0, sizeof(eventItem));
214 eventItem.events = EPOLLIN;
215 eventItem.data.u32 = EPOLL_ID_INOTIFY;
216 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
217 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
218
219 int wakeFds[2];
220 result = pipe(wakeFds);
221 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
222
223 mWakeReadPipeFd = wakeFds[0];
224 mWakeWritePipeFd = wakeFds[1];
225
226 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
227 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
228 errno);
229
230 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
231 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
232 errno);
233
234 eventItem.data.u32 = EPOLL_ID_WAKE;
235 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
236 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
237 errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238}
239
Jeff Brown90655042010-12-02 13:50:46 -0800240EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700241 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
Jeff Brown93fa9b32011-06-14 17:09:25 -0700243 while (mClosingDevices) {
244 Device* device = mClosingDevices;
245 mClosingDevices = device->next;
246 delete device;
247 }
248
249 ::close(mEpollFd);
250 ::close(mINotifyFd);
251 ::close(mWakeReadPipeFd);
252 ::close(mWakeWritePipeFd);
253
254 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255}
256
Jeff Browne38fdfa2012-04-06 14:51:01 -0700257InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800259 Device* device = getDeviceLocked(deviceId);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700260 if (device == NULL) return InputDeviceIdentifier();
261 return device->identifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262}
263
Jeff Brown90655042010-12-02 13:50:46 -0800264uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800266 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 if (device == NULL) return 0;
268 return device->classes;
269}
270
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800271void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800272 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800273 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800274 if (device && device->configuration) {
275 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800276 } else {
277 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800278 }
279}
280
Jeff Brown6d0fec22010-07-23 21:28:06 -0700281status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
282 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700283 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700284
Jeff Brownba421dd2011-08-10 15:07:05 -0700285 if (axis >= 0 && axis <= ABS_MAX) {
286 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
Jeff Brownba421dd2011-08-10 15:07:05 -0700288 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700289 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700290 struct input_absinfo info;
291 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000292 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700293 axis, device->identifier.name.string(), device->fd, errno);
294 return -errno;
295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296
Jeff Brownba421dd2011-08-10 15:07:05 -0700297 if (info.minimum != info.maximum) {
298 outAxisInfo->valid = true;
299 outAxisInfo->minValue = info.minimum;
300 outAxisInfo->maxValue = info.maximum;
301 outAxisInfo->flat = info.flat;
302 outAxisInfo->fuzz = info.fuzz;
303 outAxisInfo->resolution = info.resolution;
304 }
305 return OK;
306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700308 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309}
310
Jeff Browncc0c1592011-02-19 05:07:28 -0800311bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
312 if (axis >= 0 && axis <= REL_MAX) {
313 AutoMutex _l(mLock);
314
315 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700316 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800317 return test_bit(axis, device->relBitmask);
318 }
319 }
320 return false;
321}
322
Jeff Brown80fd47c2011-05-24 01:07:44 -0700323bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
324 if (property >= 0 && property <= INPUT_PROP_MAX) {
325 AutoMutex _l(mLock);
326
327 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700328 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700329 return test_bit(property, device->propBitmask);
330 }
331 }
332 return false;
333}
334
Jeff Brown6d0fec22010-07-23 21:28:06 -0700335int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700336 if (scanCode >= 0 && scanCode <= KEY_MAX) {
337 AutoMutex _l(mLock);
338
Jeff Brown90655042010-12-02 13:50:46 -0800339 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700340 if (device && !device->isVirtual() && test_bit(scanCode, device->keyBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700341 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
342 memset(keyState, 0, sizeof(keyState));
343 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
344 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700348 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349}
350
Jeff Brown6d0fec22010-07-23 21:28:06 -0700351int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
352 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700353
Jeff Brown90655042010-12-02 13:50:46 -0800354 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700355 if (device && !device->isVirtual() && device->keyMap.haveKeyLayout()) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700356 Vector<int32_t> scanCodes;
357 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
358 if (scanCodes.size() != 0) {
359 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
360 memset(keyState, 0, sizeof(keyState));
361 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
362 for (size_t i = 0; i < scanCodes.size(); i++) {
363 int32_t sc = scanCodes.itemAt(i);
364 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
365 return AKEY_STATE_DOWN;
366 }
367 }
368 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 }
370 }
371 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700372 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700373}
374
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700376 if (sw >= 0 && sw <= SW_MAX) {
377 AutoMutex _l(mLock);
378
Jeff Brown90655042010-12-02 13:50:46 -0800379 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700380 if (device && !device->isVirtual() && test_bit(sw, device->swBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700381 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
382 memset(swState, 0, sizeof(swState));
383 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
384 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
385 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700386 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700387 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700388 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700389}
390
Jeff Brown2717eff2011-06-30 23:53:07 -0700391status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700392 *outValue = 0;
393
Jeff Brown2717eff2011-06-30 23:53:07 -0700394 if (axis >= 0 && axis <= ABS_MAX) {
395 AutoMutex _l(mLock);
396
397 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700398 if (device && !device->isVirtual() && test_bit(axis, device->absBitmask)) {
Jeff Brownba421dd2011-08-10 15:07:05 -0700399 struct input_absinfo info;
400 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000401 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700402 axis, device->identifier.name.string(), device->fd, errno);
403 return -errno;
404 }
405
406 *outValue = info.value;
407 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700408 }
409 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700410 return -1;
411}
412
Jeff Brown6d0fec22010-07-23 21:28:06 -0700413bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
414 const int32_t* keyCodes, uint8_t* outFlags) const {
415 AutoMutex _l(mLock);
416
Jeff Brown90655042010-12-02 13:50:46 -0800417 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700418 if (device && device->keyMap.haveKeyLayout()) {
419 Vector<int32_t> scanCodes;
420 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
421 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700422
Jeff Brownba421dd2011-08-10 15:07:05 -0700423 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
424 keyCodes[codeIndex], &scanCodes);
425 if (! err) {
426 // check the possible scan codes identified by the layout map against the
427 // map of codes actually emitted by the driver
428 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
429 if (test_bit(scanCodes[sc], device->keyBitmask)) {
430 outFlags[codeIndex] = 1;
431 break;
432 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433 }
434 }
435 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700436 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700437 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700438 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439}
440
Jeff Brown49ccac52012-04-11 18:27:33 -0700441status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
442 int32_t* outKeycode, uint32_t* outFlags) const {
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700443 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800444 Device* device = getDeviceLocked(deviceId);
Jeff Brown49ccac52012-04-11 18:27:33 -0700445
Jeff Brown90655042010-12-02 13:50:46 -0800446 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700447 status_t err = device->keyMap.keyLayoutMap->mapKey(
448 scanCode, usageCode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700449 if (err == NO_ERROR) {
450 return NO_ERROR;
451 }
452 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700453
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700454 *outKeycode = 0;
455 *outFlags = 0;
456 return NAME_NOT_FOUND;
457}
458
Jeff Brown49ccac52012-04-11 18:27:33 -0700459status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800460 AutoMutex _l(mLock);
461 Device* device = getDeviceLocked(deviceId);
462
463 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700464 status_t err = device->keyMap.keyLayoutMap->mapAxis(scanCode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800465 if (err == NO_ERROR) {
466 return NO_ERROR;
467 }
468 }
469
Jeff Brown6f2fba42011-02-19 01:08:02 -0800470 return NAME_NOT_FOUND;
471}
472
Jeff Brown1a84fd12011-06-02 01:26:32 -0700473void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700474 AutoMutex _l(mLock);
475
Jeff Brown1a84fd12011-06-02 01:26:32 -0700476 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400477}
478
Jeff Brown49754db2011-07-01 17:37:58 -0700479bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
480 AutoMutex _l(mLock);
481 Device* device = getDeviceLocked(deviceId);
482 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
483 if (test_bit(scanCode, device->keyBitmask)) {
484 return true;
485 }
486 }
487 return false;
488}
489
Jeff Brown497a92c2010-09-12 17:55:08 -0700490bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
491 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800492 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700493 if (device && led >= 0 && led <= LED_MAX) {
494 if (test_bit(led, device->ledBitmask)) {
495 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700496 }
497 }
498 return false;
499}
500
501void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
502 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800503 Device* device = getDeviceLocked(deviceId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700504 if (device && !device->isVirtual() && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700505 struct input_event ev;
506 ev.time.tv_sec = 0;
507 ev.time.tv_usec = 0;
508 ev.type = EV_LED;
509 ev.code = led;
510 ev.value = on ? 1 : 0;
511
512 ssize_t nWrite;
513 do {
514 nWrite = write(device->fd, &ev, sizeof(struct input_event));
515 } while (nWrite == -1 && errno == EINTR);
516 }
517}
518
Jeff Brown90655042010-12-02 13:50:46 -0800519void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
520 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
521 outVirtualKeys.clear();
522
523 AutoMutex _l(mLock);
524 Device* device = getDeviceLocked(deviceId);
525 if (device && device->virtualKeyMap) {
526 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
527 }
528}
529
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700530sp<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
Jeff Brown1e08fe92011-11-15 17:48:10 -0800531 AutoMutex _l(mLock);
532 Device* device = getDeviceLocked(deviceId);
533 if (device) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700534 if (device->combinedKeyMap != NULL) {
535 return device->combinedKeyMap;
536 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700537 return device->keyMap.keyCharacterMap;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800538 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700539 return NULL;
Jeff Brown1e08fe92011-11-15 17:48:10 -0800540}
541
Jeff Brown6ec6f792012-04-17 16:52:41 -0700542bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId,
543 const sp<KeyCharacterMap>& map) {
544 AutoMutex _l(mLock);
545 Device* device = getDeviceLocked(deviceId);
546 if (device) {
547 if (map != device->overlayKeyMap) {
548 device->overlayKeyMap = map;
549 device->combinedKeyMap = KeyCharacterMap::combine(
550 device->keyMap.keyCharacterMap, map);
551 return true;
552 }
553 }
554 return false;
555}
556
Jeff Browna47425a2012-04-13 04:09:27 -0700557void EventHub::vibrate(int32_t deviceId, nsecs_t duration) {
558 AutoMutex _l(mLock);
559 Device* device = getDeviceLocked(deviceId);
560 if (device && !device->isVirtual()) {
561 ff_effect effect;
562 memset(&effect, 0, sizeof(effect));
563 effect.type = FF_RUMBLE;
564 effect.id = device->ffEffectId;
565 effect.u.rumble.strong_magnitude = 0xc000;
566 effect.u.rumble.weak_magnitude = 0xc000;
567 effect.replay.length = (duration + 999999LL) / 1000000LL;
568 effect.replay.delay = 0;
569 if (ioctl(device->fd, EVIOCSFF, &effect)) {
570 ALOGW("Could not upload force feedback effect to device %s due to error %d.",
571 device->identifier.name.string(), errno);
572 return;
573 }
574 device->ffEffectId = effect.id;
575
576 struct input_event ev;
577 ev.time.tv_sec = 0;
578 ev.time.tv_usec = 0;
579 ev.type = EV_FF;
580 ev.code = device->ffEffectId;
581 ev.value = 1;
582 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
583 ALOGW("Could not start force feedback effect on device %s due to error %d.",
584 device->identifier.name.string(), errno);
585 return;
586 }
587 device->ffEffectPlaying = true;
588 }
589}
590
591void EventHub::cancelVibrate(int32_t deviceId) {
592 AutoMutex _l(mLock);
593 Device* device = getDeviceLocked(deviceId);
594 if (device && !device->isVirtual()) {
595 if (device->ffEffectPlaying) {
596 device->ffEffectPlaying = false;
597
598 struct input_event ev;
599 ev.time.tv_sec = 0;
600 ev.time.tv_usec = 0;
601 ev.type = EV_FF;
602 ev.code = device->ffEffectId;
603 ev.value = 0;
604 if (write(device->fd, &ev, sizeof(ev)) != sizeof(ev)) {
605 ALOGW("Could not stop force feedback effect on device %s due to error %d.",
606 device->identifier.name.string(), errno);
607 return;
608 }
609 }
610 }
611}
612
Jeff Brown90655042010-12-02 13:50:46 -0800613EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700614 if (deviceId == BUILT_IN_KEYBOARD_ID) {
Jeff Brown90655042010-12-02 13:50:46 -0800615 deviceId = mBuiltInKeyboardId;
616 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700617 ssize_t index = mDevices.indexOfKey(deviceId);
618 return index >= 0 ? mDevices.valueAt(index) : NULL;
619}
Jeff Brown90655042010-12-02 13:50:46 -0800620
Jeff Brown93fa9b32011-06-14 17:09:25 -0700621EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
622 for (size_t i = 0; i < mDevices.size(); i++) {
623 Device* device = mDevices.valueAt(i);
624 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800625 return device;
626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628 return NULL;
629}
630
Jeff Brownb7198742011-03-18 18:14:26 -0700631size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Steve Blockec193de2012-01-09 18:35:44 +0000632 ALOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400633
Jeff Brown93fa9b32011-06-14 17:09:25 -0700634 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400635
Jeff Brownb7198742011-03-18 18:14:26 -0700636 struct input_event readBuffer[bufferSize];
637
638 RawEvent* event = buffer;
639 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700640 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700641 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700642 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
643
Jeff Brown1a84fd12011-06-02 01:26:32 -0700644 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700645 if (mNeedToReopenDevices) {
646 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700647
Steve Block6215d3f2012-01-04 20:05:49 +0000648 ALOGI("Reopening all input devices due to a configuration change.");
Jeff Brown1a84fd12011-06-02 01:26:32 -0700649
Jeff Brown93fa9b32011-06-14 17:09:25 -0700650 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700651 mNeedToScanDevices = true;
652 break; // return to the caller before we actually rescan
653 }
654
Jeff Browncc2e7172010-08-17 16:48:25 -0700655 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700656 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800657 Device* device = mClosingDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100658 ALOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 device->id, device->path.string());
660 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700661 event->when = now;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700662 event->deviceId = device->id == mBuiltInKeyboardId ? BUILT_IN_KEYBOARD_ID : device->id;
Jeff Brownb7198742011-03-18 18:14:26 -0700663 event->type = DEVICE_REMOVED;
664 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700666 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700667 if (--capacity == 0) {
668 break;
669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700671
Jeff Brown1a84fd12011-06-02 01:26:32 -0700672 if (mNeedToScanDevices) {
673 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700674 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700675 mNeedToSendFinishedDeviceScan = true;
676 }
677
Jeff Brownb7198742011-03-18 18:14:26 -0700678 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800679 Device* device = mOpeningDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100680 ALOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 device->id, device->path.string());
682 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700683 event->when = now;
684 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
685 event->type = DEVICE_ADDED;
686 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700687 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700688 if (--capacity == 0) {
689 break;
690 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700691 }
692
693 if (mNeedToSendFinishedDeviceScan) {
694 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700695 event->when = now;
696 event->type = FINISHED_DEVICE_SCAN;
697 event += 1;
698 if (--capacity == 0) {
699 break;
700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702
Jeff Browncc2e7172010-08-17 16:48:25 -0700703 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700704 bool deviceChanged = false;
705 while (mPendingEventIndex < mPendingEventCount) {
706 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
707 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
708 if (eventItem.events & EPOLLIN) {
709 mPendingINotify = true;
710 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000711 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700712 }
713 continue;
714 }
715
716 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
717 if (eventItem.events & EPOLLIN) {
Steve Block71f2cf12011-10-20 11:56:00 +0100718 ALOGV("awoken after wake()");
Jeff Brown93fa9b32011-06-14 17:09:25 -0700719 awoken = true;
720 char buffer[16];
721 ssize_t nRead;
722 do {
723 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
724 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
725 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000726 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700727 eventItem.events);
728 }
729 continue;
730 }
731
732 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
733 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000734 ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700735 eventItem.events, eventItem.data.u32);
736 continue;
737 }
738
739 Device* device = mDevices.valueAt(deviceIndex);
740 if (eventItem.events & EPOLLIN) {
741 int32_t readSize = read(device->fd, readBuffer,
742 sizeof(struct input_event) * capacity);
743 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
744 // Device was removed before INotify noticed.
Jeff Brown41305542011-10-05 11:14:13 -0700745 ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d "
746 "capacity: %d errno: %d)\n",
747 device->fd, readSize, bufferSize, capacity, errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700748 deviceChanged = true;
749 closeDeviceLocked(device);
750 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700751 if (errno != EAGAIN && errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000752 ALOGW("could not get event (errno=%d)", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700753 }
754 } else if ((readSize % sizeof(struct input_event)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000755 ALOGE("could not get event (wrong size: %d)", readSize);
Jeff Browncc2e7172010-08-17 16:48:25 -0700756 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700757 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
758
759 size_t count = size_t(readSize) / sizeof(struct input_event);
760 for (size_t i = 0; i < count; i++) {
761 const struct input_event& iev = readBuffer[i];
Steve Block71f2cf12011-10-20 11:56:00 +0100762 ALOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
Jeff Brownb7198742011-03-18 18:14:26 -0700763 device->path.string(),
764 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
765 iev.type, iev.code, iev.value);
766
Jeff Brown4e91a182011-04-07 11:38:09 -0700767#ifdef HAVE_POSIX_CLOCKS
768 // Use the time specified in the event instead of the current time
769 // so that downstream code can get more accurate estimates of
770 // event dispatch latency from the time the event is enqueued onto
771 // the evdev client buffer.
772 //
773 // The event's timestamp fortuitously uses the same monotonic clock
774 // time base as the rest of Android. The kernel event device driver
775 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
776 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
777 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
778 // system call that also queries ktime_get_ts().
779 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
780 + nsecs_t(iev.time.tv_usec) * 1000LL;
Steve Block71f2cf12011-10-20 11:56:00 +0100781 ALOGV("event time %lld, now %lld", event->when, now);
Jeff Brown4e91a182011-04-07 11:38:09 -0700782#else
Jeff Brownb7198742011-03-18 18:14:26 -0700783 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700784#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700785 event->deviceId = deviceId;
786 event->type = iev.type;
Jeff Brown49ccac52012-04-11 18:27:33 -0700787 event->code = iev.code;
Jeff Brownb7198742011-03-18 18:14:26 -0700788 event->value = iev.value;
Jeff Brownb7198742011-03-18 18:14:26 -0700789 event += 1;
790 }
791 capacity -= count;
792 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700793 // The result buffer is full. Reset the pending event index
794 // so we will try to read the device again on the next iteration.
795 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700796 break;
797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700799 } else if (eventItem.events & EPOLLHUP) {
800 ALOGI("Removing device %s due to epoll hang-up event.",
801 device->identifier.name.string());
802 deviceChanged = true;
803 closeDeviceLocked(device);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700804 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000805 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700806 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 }
808 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700809
Jeff Brown93fa9b32011-06-14 17:09:25 -0700810 // readNotify() will modify the list of devices so this must be done after
811 // processing all other events to ensure that we read all remaining events
812 // before closing the devices.
813 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
814 mPendingINotify = false;
815 readNotifyLocked();
816 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800817 }
818
Jeff Brown93fa9b32011-06-14 17:09:25 -0700819 // Report added or removed devices immediately.
820 if (deviceChanged) {
821 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
Jeff Browna9b84222010-10-14 02:23:43 -0700823
Jeff Brown93fa9b32011-06-14 17:09:25 -0700824 // Return now if we have collected any events or if we were explicitly awoken.
825 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700826 break;
827 }
828
Jeff Browncc2e7172010-08-17 16:48:25 -0700829 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700830 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700831 // subtle choreography. When a device driver has pending (unread) events, it acquires
832 // a kernel wake lock. However, once the last pending event has been read, the device
833 // driver will release the kernel wake lock. To prevent the system from going to sleep
834 // when this happens, the EventHub holds onto its own user wake lock while the client
835 // is processing events. Thus the system can only sleep if there are no events
836 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700837 //
838 // The timeout is advisory only. If the device is asleep, it will not wake just to
839 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700840 mPendingEventIndex = 0;
841
842 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700843 release_wake_lock(WAKE_LOCK_ID);
844
Jeff Brown93fa9b32011-06-14 17:09:25 -0700845 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700846
847 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700848 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700849
Jeff Brownaa3855d2011-03-17 01:34:19 -0700850 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700851 // Timed out.
852 mPendingEventCount = 0;
853 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700854 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700855
Jeff Brownaa3855d2011-03-17 01:34:19 -0700856 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700857 // An error occurred.
858 mPendingEventCount = 0;
859
Jeff Brownb7198742011-03-18 18:14:26 -0700860 // Sleep after errors to avoid locking up the system.
861 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700862 if (errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000863 ALOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700864 usleep(100000);
865 }
Jeff Brownb7198742011-03-18 18:14:26 -0700866 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700867 // Some events occurred.
868 mPendingEventCount = size_t(pollResult);
Jeff Browncc2e7172010-08-17 16:48:25 -0700869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
Jeff Brownb7198742011-03-18 18:14:26 -0700871
872 // All done, return the number of events we read.
873 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874}
875
Jeff Brown93fa9b32011-06-14 17:09:25 -0700876void EventHub::wake() {
Steve Block71f2cf12011-10-20 11:56:00 +0100877 ALOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878
Jeff Brown93fa9b32011-06-14 17:09:25 -0700879 ssize_t nWrite;
880 do {
881 nWrite = write(mWakeWritePipeFd, "W", 1);
882 } while (nWrite == -1 && errno == EINTR);
883
884 if (nWrite != 1 && errno != EAGAIN) {
Steve Block8564c8d2012-01-05 23:22:43 +0000885 ALOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700887}
Jeff Brown90655042010-12-02 13:50:46 -0800888
Jeff Brown93fa9b32011-06-14 17:09:25 -0700889void EventHub::scanDevicesLocked() {
890 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 if(res < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000892 ALOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700894 if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) {
895 createVirtualKeyboardLocked();
896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897}
898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899// ----------------------------------------------------------------------------
900
Jeff Brownfd0358292010-06-30 16:10:35 -0700901static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
902 const uint8_t* end = array + endIndex;
903 array += startIndex;
904 while (array != end) {
905 if (*(array++) != 0) {
906 return true;
907 }
908 }
909 return false;
910}
911
912static const int32_t GAMEPAD_KEYCODES[] = {
913 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
914 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
915 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
916 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
917 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800918 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
919 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
920 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
921 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
922 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -0700923};
924
Jeff Brown93fa9b32011-06-14 17:09:25 -0700925status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800926 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927
Steve Block71f2cf12011-10-20 11:56:00 +0100928 ALOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929
Jeff Brown874c1e92012-01-19 14:32:47 -0800930 int fd = open(devicePath, O_RDWR | O_CLOEXEC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 if(fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000932 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 return -1;
934 }
935
Jeff Brown90655042010-12-02 13:50:46 -0800936 InputDeviceIdentifier identifier;
937
938 // Get device name.
939 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
940 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
941 } else {
942 buffer[sizeof(buffer) - 1] = '\0';
943 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400945
Jeff Brown90655042010-12-02 13:50:46 -0800946 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700947 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
948 const String8& item = mExcludedDevices.itemAt(i);
949 if (identifier.name == item) {
Steve Block6215d3f2012-01-04 20:05:49 +0000950 ALOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400951 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400952 return -1;
953 }
954 }
955
Jeff Brown90655042010-12-02 13:50:46 -0800956 // Get device driver version.
957 int driverVersion;
958 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Steve Block3762c312012-01-06 19:20:56 +0000959 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -0800960 close(fd);
961 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963
Jeff Brown90655042010-12-02 13:50:46 -0800964 // Get device identifier.
965 struct input_id inputId;
966 if(ioctl(fd, EVIOCGID, &inputId)) {
Steve Block3762c312012-01-06 19:20:56 +0000967 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -0800968 close(fd);
969 return -1;
970 }
971 identifier.bus = inputId.bustype;
972 identifier.product = inputId.product;
973 identifier.vendor = inputId.vendor;
974 identifier.version = inputId.version;
975
976 // Get device physical location.
977 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
978 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
979 } else {
980 buffer[sizeof(buffer) - 1] = '\0';
981 identifier.location.setTo(buffer);
982 }
983
984 // Get device unique id.
985 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
986 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
987 } else {
988 buffer[sizeof(buffer) - 1] = '\0';
989 identifier.uniqueId.setTo(buffer);
990 }
991
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700992 // Fill in the descriptor.
993 setDescriptor(identifier);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700994
Jeff Brown90655042010-12-02 13:50:46 -0800995 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700996 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
Steve Block3762c312012-01-06 19:20:56 +0000997 ALOGE("Error %d making device file descriptor non-blocking.", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700998 close(fd);
999 return -1;
1000 }
1001
Jeff Brown90655042010-12-02 13:50:46 -08001002 // Allocate device. (The device object takes ownership of the fd at this point.)
1003 int32_t deviceId = mNextDeviceId++;
1004 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005
Jeff Browne38fdfa2012-04-06 14:51:01 -07001006 ALOGV("add device %d: %s\n", deviceId, devicePath);
1007 ALOGV(" bus: %04x\n"
1008 " vendor %04x\n"
1009 " product %04x\n"
1010 " version %04x\n",
Jeff Brown90655042010-12-02 13:50:46 -08001011 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Jeff Browne38fdfa2012-04-06 14:51:01 -07001012 ALOGV(" name: \"%s\"\n", identifier.name.string());
1013 ALOGV(" location: \"%s\"\n", identifier.location.string());
1014 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string());
Jeff Brown49ccac52012-04-11 18:27:33 -07001015 ALOGV(" descriptor: \"%s\"\n", identifier.descriptor.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001016 ALOGV(" driver: v%d.%d.%d\n",
Jeff Brown90655042010-12-02 13:50:46 -08001017 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001019 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001020 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001021
Jeff Brownfd0358292010-06-30 16:10:35 -07001022 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001023 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
1024 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
1025 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
1026 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
1027 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
Jeff Browna47425a2012-04-13 04:09:27 -07001028 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(device->ffBitmask)), device->ffBitmask);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001029 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -08001030
Jeff Brown6f2fba42011-02-19 01:08:02 -08001031 // See if this is a keyboard. Ignore everything in the button range except for
1032 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001033 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
1034 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -08001035 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001036 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001037 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -07001038 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001039 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -08001040 if (haveKeyboardKeys || haveGamepadButtons) {
1041 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001043
Jeff Brown83c09682010-12-23 17:50:18 -08001044 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001045 if (test_bit(BTN_MOUSE, device->keyBitmask)
1046 && test_bit(REL_X, device->relBitmask)
1047 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001048 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001050
1051 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -08001052 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001053 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
1054 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001055 // Some joysticks such as the PS3 controller report axes that conflict
1056 // with the ABS_MT range. Try to confirm that the device really is
1057 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001058 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001059 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -07001060 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001061 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001062 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
1063 && test_bit(ABS_X, device->absBitmask)
1064 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001065 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001068 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001069 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1070 // from other devices such as accelerometers that also have absolute axes.
Jeff Brown9ee285af2011-08-31 12:56:34 -07001071 if (haveGamepadButtons) {
1072 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
1073 for (int i = 0; i <= ABS_MAX; i++) {
1074 if (test_bit(i, device->absBitmask)
1075 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
1076 device->classes = assumedClasses;
1077 break;
1078 }
1079 }
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001080 }
1081
Jeff Brown93fa9b32011-06-14 17:09:25 -07001082 // Check whether this device has switches.
1083 for (int i = 0; i <= SW_MAX; i++) {
1084 if (test_bit(i, device->swBitmask)) {
1085 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
1086 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089
Jeff Browna47425a2012-04-13 04:09:27 -07001090 // Check whether this device supports the vibrator.
1091 if (test_bit(FF_RUMBLE, device->ffBitmask)) {
1092 device->classes |= INPUT_DEVICE_CLASS_VIBRATOR;
1093 }
1094
Jeff Brown93fa9b32011-06-14 17:09:25 -07001095 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -08001096 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -08001097 // Load the virtual keys for the touch screen, if any.
1098 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001099 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -08001100 if (!status) {
1101 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
Jeff Brown90655042010-12-02 13:50:46 -08001103 }
1104
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001105 // Load the key map.
1106 // We need to do this for joysticks too because the key layout may specify axes.
1107 status_t keyMapStatus = NAME_NOT_FOUND;
1108 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -08001109 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001110 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001111 }
Jeff Brown90655042010-12-02 13:50:46 -08001112
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001113 // Configure the keyboard, gamepad or virtual keyboard.
1114 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001115 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001116 if (!keyMapStatus
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001117 && mBuiltInKeyboardId == NO_BUILT_IN_KEYBOARD
Jeff Brown90655042010-12-02 13:50:46 -08001118 && isEligibleBuiltInKeyboard(device->identifier,
1119 device->configuration, &device->keyMap)) {
1120 mBuiltInKeyboardId = device->id;
Jeff Brown497a92c2010-09-12 17:55:08 -07001121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001123 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -07001124 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001125 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001126 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001127
Jeff Brownfd0358292010-06-30 16:10:35 -07001128 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -07001129 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1130 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1131 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1132 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1133 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001134 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001135 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001136
Jeff Brownfd0358292010-06-30 16:10:35 -07001137 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001138 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001139 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -07001140 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1141 break;
1142 }
1143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145
Sean McNeilaeb00c42010-06-23 16:00:37 +07001146 // If the device isn't recognized as something we handle, don't monitor it.
1147 if (device->classes == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001148 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Jeff Brown90655042010-12-02 13:50:46 -08001149 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001150 delete device;
1151 return -1;
1152 }
1153
Jeff Brown56194eb2011-03-02 19:23:13 -08001154 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001155 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001156 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1157 }
1158
Jeff Brown93fa9b32011-06-14 17:09:25 -07001159 // Register with epoll.
1160 struct epoll_event eventItem;
1161 memset(&eventItem, 0, sizeof(eventItem));
1162 eventItem.events = EPOLLIN;
1163 eventItem.data.u32 = deviceId;
1164 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
Steve Block3762c312012-01-06 19:20:56 +00001165 ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001166 delete device;
1167 return -1;
1168 }
1169
Jeff Browne22afbe2011-12-16 13:45:40 -08001170 // Enable wake-lock behavior on kernels that support it.
1171 // TODO: Only need this for devices that can really wake the system.
Jeff Browneca3cf52012-04-06 19:31:36 -07001172 bool usingSuspendBlockIoctl = !ioctl(fd, EVIOCSSUSPENDBLOCK, 1);
1173
1174 // Tell the kernel that we want to use the monotonic clock for reporting timestamps
1175 // associated with input events. This is important because the input system
1176 // uses the timestamps extensively and assumes they were recorded using the monotonic
1177 // clock.
1178 //
1179 // In older kernel, before Linux 3.4, there was no way to tell the kernel which
1180 // clock to use to input event timestamps. The standard kernel behavior was to
1181 // record a real time timestamp, which isn't what we want. Android kernels therefore
1182 // contained a patch to the evdev_event() function in drivers/input/evdev.c to
1183 // replace the call to do_gettimeofday() with ktime_get_ts() to cause the monotonic
1184 // clock to be used instead of the real time clock.
1185 //
1186 // As of Linux 3.4, there is a new EVIOCSCLOCKID ioctl to set the desired clock.
1187 // Therefore, we no longer require the Android-specific kernel patch described above
1188 // as long as we make sure to set select the monotonic clock. We do that here.
1189 bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, CLOCK_MONOTONIC);
Jeff Browne22afbe2011-12-16 13:45:40 -08001190
Steve Block6215d3f2012-01-04 20:05:49 +00001191 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
Jeff Browne22afbe2011-12-16 13:45:40 -08001192 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
Jeff Browneca3cf52012-04-06 19:31:36 -07001193 "usingSuspendBlockIoctl=%s, usingClockIoctl=%s",
Jeff Brown90655042010-12-02 13:50:46 -08001194 deviceId, fd, devicePath, device->identifier.name.string(),
1195 device->classes,
1196 device->configurationFile.string(),
1197 device->keyMap.keyLayoutFile.string(),
1198 device->keyMap.keyCharacterMapFile.string(),
Jeff Browne22afbe2011-12-16 13:45:40 -08001199 toString(mBuiltInKeyboardId == deviceId),
Jeff Browneca3cf52012-04-06 19:31:36 -07001200 toString(usingSuspendBlockIoctl), toString(usingClockIoctl));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001201
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001202 addDeviceLocked(device);
1203 return 0;
1204}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001206void EventHub::createVirtualKeyboardLocked() {
1207 InputDeviceIdentifier identifier;
1208 identifier.name = "Virtual";
1209 identifier.uniqueId = "<virtual>";
1210 setDescriptor(identifier);
1211
1212 Device* device = new Device(-1, VIRTUAL_KEYBOARD_ID, String8("<virtual>"), identifier);
1213 device->classes = INPUT_DEVICE_CLASS_KEYBOARD
1214 | INPUT_DEVICE_CLASS_ALPHAKEY
1215 | INPUT_DEVICE_CLASS_DPAD
1216 | INPUT_DEVICE_CLASS_VIRTUAL;
1217 loadKeyMapLocked(device);
1218 addDeviceLocked(device);
1219}
1220
1221void EventHub::addDeviceLocked(Device* device) {
1222 mDevices.add(device->id, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 device->next = mOpeningDevices;
1224 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225}
1226
Jeff Brown93fa9b32011-06-14 17:09:25 -07001227void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001228 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1229 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001230 if (device->configurationFile.isEmpty()) {
Steve Block5baa3a62011-12-20 16:23:08 +00001231 ALOGD("No input device configuration file found for device '%s'.",
Jeff Brown90655042010-12-02 13:50:46 -08001232 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001233 } else {
1234 status_t status = PropertyMap::load(device->configurationFile,
1235 &device->configuration);
1236 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001237 ALOGE("Error loading input device configuration file for device '%s'. "
Jeff Brown90655042010-12-02 13:50:46 -08001238 "Using default configuration.",
1239 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001240 }
1241 }
1242}
1243
Jeff Brown93fa9b32011-06-14 17:09:25 -07001244status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001245 // The virtual key map is supplied by the kernel as a system board property file.
1246 String8 path;
1247 path.append("/sys/board_properties/virtualkeys.");
1248 path.append(device->identifier.name);
1249 if (access(path.string(), R_OK)) {
1250 return NAME_NOT_FOUND;
1251 }
1252 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001253}
1254
Jeff Brown93fa9b32011-06-14 17:09:25 -07001255status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001256 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001257}
1258
Jeff Brown93fa9b32011-06-14 17:09:25 -07001259bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001260 if (device->configuration) {
1261 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001262 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1263 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001264 }
1265 }
1266 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1267}
1268
Jeff Brown90655042010-12-02 13:50:46 -08001269bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1270 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001271 return false;
1272 }
1273
1274 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001275 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001276 const size_t N = scanCodes.size();
1277 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1278 int32_t sc = scanCodes.itemAt(i);
1279 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1280 return true;
1281 }
1282 }
1283
1284 return false;
1285}
1286
Jeff Brown93fa9b32011-06-14 17:09:25 -07001287status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1288 Device* device = getDeviceByPathLocked(devicePath);
1289 if (device) {
1290 closeDeviceLocked(device);
1291 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
Steve Block71f2cf12011-10-20 11:56:00 +01001293 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 return -1;
1295}
1296
Jeff Brown93fa9b32011-06-14 17:09:25 -07001297void EventHub::closeAllDevicesLocked() {
1298 while (mDevices.size() > 0) {
1299 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1300 }
1301}
1302
1303void EventHub::closeDeviceLocked(Device* device) {
Steve Block6215d3f2012-01-04 20:05:49 +00001304 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001305 device->path.string(), device->identifier.name.string(), device->id,
1306 device->fd, device->classes);
1307
Jeff Brown33bbfd22011-02-24 20:55:35 -08001308 if (device->id == mBuiltInKeyboardId) {
Steve Block8564c8d2012-01-05 23:22:43 +00001309 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001310 device->path.string(), mBuiltInKeyboardId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001311 mBuiltInKeyboardId = NO_BUILT_IN_KEYBOARD;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001312 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001313
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001314 if (!device->isVirtual()) {
1315 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
1316 ALOGW("Could not remove device fd from epoll instance. errno=%d", errno);
1317 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001318 }
1319
1320 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001321 device->close();
1322
Jeff Brown8e9d4432011-03-12 19:46:59 -08001323 // Unlink for opening devices list if it is present.
1324 Device* pred = NULL;
1325 bool found = false;
1326 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1327 if (entry == device) {
1328 found = true;
1329 break;
1330 }
1331 pred = entry;
1332 entry = entry->next;
1333 }
1334 if (found) {
1335 // Unlink the device from the opening devices list then delete it.
1336 // We don't need to tell the client that the device was closed because
1337 // it does not even know it was opened in the first place.
Steve Block6215d3f2012-01-04 20:05:49 +00001338 ALOGI("Device %s was immediately closed after opening.", device->path.string());
Jeff Brown8e9d4432011-03-12 19:46:59 -08001339 if (pred) {
1340 pred->next = device->next;
1341 } else {
1342 mOpeningDevices = device->next;
1343 }
1344 delete device;
1345 } else {
1346 // Link into closing devices list.
1347 // The device will be deleted later after we have informed the client.
1348 device->next = mClosingDevices;
1349 mClosingDevices = device;
1350 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001351}
1352
Jeff Brown93fa9b32011-06-14 17:09:25 -07001353status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 int res;
1355 char devname[PATH_MAX];
1356 char *filename;
1357 char event_buf[512];
1358 int event_size;
1359 int event_pos = 0;
1360 struct inotify_event *event;
1361
Steve Block71f2cf12011-10-20 11:56:00 +01001362 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001363 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 if(res < (int)sizeof(*event)) {
1365 if(errno == EINTR)
1366 return 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001367 ALOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001368 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370 //printf("got %d bytes of event information\n", res);
1371
Jeff Brown90655042010-12-02 13:50:46 -08001372 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 filename = devname + strlen(devname);
1374 *filename++ = '/';
1375
1376 while(res >= (int)sizeof(*event)) {
1377 event = (struct inotify_event *)(event_buf + event_pos);
1378 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1379 if(event->len) {
1380 strcpy(filename, event->name);
1381 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001382 openDeviceLocked(devname);
1383 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00001384 ALOGI("Removing device '%s' due to inotify event\n", devname);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001385 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
1387 }
1388 event_size = sizeof(*event) + event->len;
1389 res -= event_size;
1390 event_pos += event_size;
1391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 return 0;
1393}
1394
Jeff Brown93fa9b32011-06-14 17:09:25 -07001395status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396{
1397 char devname[PATH_MAX];
1398 char *filename;
1399 DIR *dir;
1400 struct dirent *de;
1401 dir = opendir(dirname);
1402 if(dir == NULL)
1403 return -1;
1404 strcpy(devname, dirname);
1405 filename = devname + strlen(devname);
1406 *filename++ = '/';
1407 while((de = readdir(dir))) {
1408 if(de->d_name[0] == '.' &&
1409 (de->d_name[1] == '\0' ||
1410 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1411 continue;
1412 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001413 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 }
1415 closedir(dir);
1416 return 0;
1417}
1418
Jeff Brown93fa9b32011-06-14 17:09:25 -07001419void EventHub::requestReopenDevices() {
Steve Block71f2cf12011-10-20 11:56:00 +01001420 ALOGV("requestReopenDevices() called");
Jeff Brown93fa9b32011-06-14 17:09:25 -07001421
1422 AutoMutex _l(mLock);
1423 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001424}
1425
Jeff Brownf2f487182010-10-01 17:46:21 -07001426void EventHub::dump(String8& dump) {
1427 dump.append("Event Hub State:\n");
1428
1429 { // acquire lock
1430 AutoMutex _l(mLock);
1431
Jeff Brown90655042010-12-02 13:50:46 -08001432 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001433
1434 dump.append(INDENT "Devices:\n");
1435
Jeff Brown93fa9b32011-06-14 17:09:25 -07001436 for (size_t i = 0; i < mDevices.size(); i++) {
1437 const Device* device = mDevices.valueAt(i);
1438 if (mBuiltInKeyboardId == device->id) {
1439 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1440 device->id, device->identifier.name.string());
1441 } else {
1442 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1443 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001444 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001445 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1446 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001447 dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string());
Jeff Brown93fa9b32011-06-14 17:09:25 -07001448 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1449 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1450 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1451 "product=0x%04x, version=0x%04x\n",
1452 device->identifier.bus, device->identifier.vendor,
1453 device->identifier.product, device->identifier.version);
1454 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1455 device->keyMap.keyLayoutFile.string());
1456 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1457 device->keyMap.keyCharacterMapFile.string());
1458 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1459 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001460 }
1461 } // release lock
1462}
1463
Jeff Brown89ef0722011-08-10 16:25:21 -07001464void EventHub::monitor() {
1465 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1466 mLock.lock();
1467 mLock.unlock();
1468}
1469
1470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471}; // namespace android