blob: 296c95ec937e0335a08a1ff88877b43cb4f553a6 [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
Jeff Brown6b53e8d2010-11-10 16:03:06 -080039#include <ui/KeyLayoutMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080040#include <ui/KeyCharacterMap.h>
41#include <ui/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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52/* this macro is used to tell if "bit" is set in "array"
53 * it selects a byte from the array, and does a boolean AND
54 * operation with a byte that only has the relevant bit set.
55 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
56 */
57#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
58
Jeff Brownfd035822010-06-30 16:10:35 -070059/* this macro computes the number of bytes needed to represent a bit array of the specified size */
60#define sizeof_bit_array(bits) ((bits + 7) / 8)
61
Jeff Brownf2f48712010-10-01 17:46:21 -070062#define INDENT " "
63#define INDENT2 " "
64#define INDENT3 " "
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066namespace android {
67
68static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080069static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71/* return the larger integer */
72static inline int max(int v1, int v2)
73{
74 return (v1 > v2) ? v1 : v2;
75}
76
Jeff Brownf2f48712010-10-01 17:46:21 -070077static inline const char* toString(bool value) {
78 return value ? "true" : "false";
79}
80
Jeff Brown9ee285a2011-08-31 12:56:34 -070081// --- Global Functions ---
82
83uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
84 // Touch devices get dibs on touch-related axes.
85 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
86 switch (axis) {
87 case ABS_X:
88 case ABS_Y:
89 case ABS_PRESSURE:
90 case ABS_TOOL_WIDTH:
91 case ABS_DISTANCE:
92 case ABS_TILT_X:
93 case ABS_TILT_Y:
94 case ABS_MT_SLOT:
95 case ABS_MT_TOUCH_MAJOR:
96 case ABS_MT_TOUCH_MINOR:
97 case ABS_MT_WIDTH_MAJOR:
98 case ABS_MT_WIDTH_MINOR:
99 case ABS_MT_ORIENTATION:
100 case ABS_MT_POSITION_X:
101 case ABS_MT_POSITION_Y:
102 case ABS_MT_TOOL_TYPE:
103 case ABS_MT_BLOB_ID:
104 case ABS_MT_TRACKING_ID:
105 case ABS_MT_PRESSURE:
106 case ABS_MT_DISTANCE:
107 return INPUT_DEVICE_CLASS_TOUCH;
108 }
109 }
110
111 // Joystick devices get the rest.
112 return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK;
113}
114
Jeff Brown90655042010-12-02 13:50:46 -0800115// --- EventHub::Device ---
116
117EventHub::Device::Device(int fd, int32_t id, const String8& path,
118 const InputDeviceIdentifier& identifier) :
119 next(NULL),
120 fd(fd), id(id), path(path), identifier(identifier),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700121 classes(0), configuration(NULL), virtualKeyMap(NULL) {
122 memset(keyBitmask, 0, sizeof(keyBitmask));
123 memset(absBitmask, 0, sizeof(absBitmask));
124 memset(relBitmask, 0, sizeof(relBitmask));
125 memset(swBitmask, 0, sizeof(swBitmask));
126 memset(ledBitmask, 0, sizeof(ledBitmask));
127 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128}
129
Jeff Brown90655042010-12-02 13:50:46 -0800130EventHub::Device::~Device() {
131 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800132 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800133 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134}
135
Jeff Brown90655042010-12-02 13:50:46 -0800136void EventHub::Device::close() {
137 if (fd >= 0) {
138 ::close(fd);
139 fd = -1;
140 }
141}
142
143
144// --- EventHub ---
145
Jeff Brown93fa9b32011-06-14 17:09:25 -0700146const uint32_t EventHub::EPOLL_ID_INOTIFY;
147const uint32_t EventHub::EPOLL_ID_WAKE;
148const int EventHub::EPOLL_SIZE_HINT;
149const int EventHub::EPOLL_MAX_EVENTS;
150
Jeff Brown90655042010-12-02 13:50:46 -0800151EventHub::EventHub(void) :
Jeff Brown93fa9b32011-06-14 17:09:25 -0700152 mBuiltInKeyboardId(-1), mNextDeviceId(1),
Jeff Brown90655042010-12-02 13:50:46 -0800153 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700154 mNeedToSendFinishedDeviceScan(false),
155 mNeedToReopenDevices(false), mNeedToScanDevices(true),
156 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700158
Jeff Brown93fa9b32011-06-14 17:09:25 -0700159 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
160 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
161
162 mINotifyFd = inotify_init();
163 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
164 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
165 DEVICE_PATH, errno);
166
167 struct epoll_event eventItem;
168 memset(&eventItem, 0, sizeof(eventItem));
169 eventItem.events = EPOLLIN;
170 eventItem.data.u32 = EPOLL_ID_INOTIFY;
171 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
172 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
173
174 int wakeFds[2];
175 result = pipe(wakeFds);
176 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
177
178 mWakeReadPipeFd = wakeFds[0];
179 mWakeWritePipeFd = wakeFds[1];
180
181 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
182 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
183 errno);
184
185 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
186 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
187 errno);
188
189 eventItem.data.u32 = EPOLL_ID_WAKE;
190 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
191 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
192 errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193}
194
Jeff Brown90655042010-12-02 13:50:46 -0800195EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700196 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Jeff Brown93fa9b32011-06-14 17:09:25 -0700198 while (mClosingDevices) {
199 Device* device = mClosingDevices;
200 mClosingDevices = device->next;
201 delete device;
202 }
203
204 ::close(mEpollFd);
205 ::close(mINotifyFd);
206 ::close(mWakeReadPipeFd);
207 ::close(mWakeWritePipeFd);
208
209 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210}
211
Jeff Brown90655042010-12-02 13:50:46 -0800212String8 EventHub::getDeviceName(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800214 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 if (device == NULL) return String8();
Jeff Brown90655042010-12-02 13:50:46 -0800216 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217}
218
Jeff Brown90655042010-12-02 13:50:46 -0800219uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800221 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 if (device == NULL) return 0;
223 return device->classes;
224}
225
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800226void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800227 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800228 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800229 if (device && device->configuration) {
230 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800231 } else {
232 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800233 }
234}
235
Jeff Brown6d0fec22010-07-23 21:28:06 -0700236status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
237 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700238 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700239
Jeff Brownba421dd2011-08-10 15:07:05 -0700240 if (axis >= 0 && axis <= ABS_MAX) {
241 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
Jeff Brownba421dd2011-08-10 15:07:05 -0700243 Device* device = getDeviceLocked(deviceId);
244 if (device && test_bit(axis, device->absBitmask)) {
245 struct input_absinfo info;
246 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000247 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700248 axis, device->identifier.name.string(), device->fd, errno);
249 return -errno;
250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
Jeff Brownba421dd2011-08-10 15:07:05 -0700252 if (info.minimum != info.maximum) {
253 outAxisInfo->valid = true;
254 outAxisInfo->minValue = info.minimum;
255 outAxisInfo->maxValue = info.maximum;
256 outAxisInfo->flat = info.flat;
257 outAxisInfo->fuzz = info.fuzz;
258 outAxisInfo->resolution = info.resolution;
259 }
260 return OK;
261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700263 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264}
265
Jeff Browncc0c1592011-02-19 05:07:28 -0800266bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
267 if (axis >= 0 && axis <= REL_MAX) {
268 AutoMutex _l(mLock);
269
270 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700271 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800272 return test_bit(axis, device->relBitmask);
273 }
274 }
275 return false;
276}
277
Jeff Brown80fd47c2011-05-24 01:07:44 -0700278bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
279 if (property >= 0 && property <= INPUT_PROP_MAX) {
280 AutoMutex _l(mLock);
281
282 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700283 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700284 return test_bit(property, device->propBitmask);
285 }
286 }
287 return false;
288}
289
Jeff Brown6d0fec22010-07-23 21:28:06 -0700290int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700291 if (scanCode >= 0 && scanCode <= KEY_MAX) {
292 AutoMutex _l(mLock);
293
Jeff Brown90655042010-12-02 13:50:46 -0800294 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700295 if (device && test_bit(scanCode, device->keyBitmask)) {
296 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
297 memset(keyState, 0, sizeof(keyState));
298 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
299 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 }
302 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700303 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304}
305
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
307 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700308
Jeff Brown90655042010-12-02 13:50:46 -0800309 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700310 if (device && device->keyMap.haveKeyLayout()) {
311 Vector<int32_t> scanCodes;
312 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
313 if (scanCodes.size() != 0) {
314 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
315 memset(keyState, 0, sizeof(keyState));
316 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
317 for (size_t i = 0; i < scanCodes.size(); i++) {
318 int32_t sc = scanCodes.itemAt(i);
319 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
320 return AKEY_STATE_DOWN;
321 }
322 }
323 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
325 }
326 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700327 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700328}
329
Jeff Brown6d0fec22010-07-23 21:28:06 -0700330int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700331 if (sw >= 0 && sw <= SW_MAX) {
332 AutoMutex _l(mLock);
333
Jeff Brown90655042010-12-02 13:50:46 -0800334 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700335 if (device && test_bit(sw, device->swBitmask)) {
336 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
337 memset(swState, 0, sizeof(swState));
338 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
339 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
340 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700341 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700342 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700343 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700344}
345
Jeff Brown2717eff2011-06-30 23:53:07 -0700346status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700347 *outValue = 0;
348
Jeff Brown2717eff2011-06-30 23:53:07 -0700349 if (axis >= 0 && axis <= ABS_MAX) {
350 AutoMutex _l(mLock);
351
352 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700353 if (device && test_bit(axis, device->absBitmask)) {
354 struct input_absinfo info;
355 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000356 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700357 axis, device->identifier.name.string(), device->fd, errno);
358 return -errno;
359 }
360
361 *outValue = info.value;
362 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700363 }
364 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700365 return -1;
366}
367
Jeff Brown6d0fec22010-07-23 21:28:06 -0700368bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
369 const int32_t* keyCodes, uint8_t* outFlags) const {
370 AutoMutex _l(mLock);
371
Jeff Brown90655042010-12-02 13:50:46 -0800372 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700373 if (device && device->keyMap.haveKeyLayout()) {
374 Vector<int32_t> scanCodes;
375 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
376 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377
Jeff Brownba421dd2011-08-10 15:07:05 -0700378 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
379 keyCodes[codeIndex], &scanCodes);
380 if (! err) {
381 // check the possible scan codes identified by the layout map against the
382 // map of codes actually emitted by the driver
383 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
384 if (test_bit(scanCodes[sc], device->keyBitmask)) {
385 outFlags[codeIndex] = 1;
386 break;
387 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 }
389 }
390 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700391 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700393 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700394}
395
Jeff Brown6f2fba42011-02-19 01:08:02 -0800396status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700397 int32_t* outKeycode, uint32_t* outFlags) const
398{
399 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800400 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700401
Jeff Brown90655042010-12-02 13:50:46 -0800402 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800403 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700404 if (err == NO_ERROR) {
405 return NO_ERROR;
406 }
407 }
408
Jeff Brown90655042010-12-02 13:50:46 -0800409 if (mBuiltInKeyboardId != -1) {
410 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700411
Jeff Brown90655042010-12-02 13:50:46 -0800412 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800413 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700414 if (err == NO_ERROR) {
415 return NO_ERROR;
416 }
417 }
418 }
419
420 *outKeycode = 0;
421 *outFlags = 0;
422 return NAME_NOT_FOUND;
423}
424
Jeff Brown85297452011-03-04 13:07:49 -0800425status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800426{
427 AutoMutex _l(mLock);
428 Device* device = getDeviceLocked(deviceId);
429
430 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800431 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800432 if (err == NO_ERROR) {
433 return NO_ERROR;
434 }
435 }
436
437 if (mBuiltInKeyboardId != -1) {
438 device = getDeviceLocked(mBuiltInKeyboardId);
439
440 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800441 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800442 if (err == NO_ERROR) {
443 return NO_ERROR;
444 }
445 }
446 }
447
Jeff Brown6f2fba42011-02-19 01:08:02 -0800448 return NAME_NOT_FOUND;
449}
450
Jeff Brown1a84fd12011-06-02 01:26:32 -0700451void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700452 AutoMutex _l(mLock);
453
Jeff Brown1a84fd12011-06-02 01:26:32 -0700454 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400455}
456
Jeff Brown49754db2011-07-01 17:37:58 -0700457bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
458 AutoMutex _l(mLock);
459 Device* device = getDeviceLocked(deviceId);
460 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
461 if (test_bit(scanCode, device->keyBitmask)) {
462 return true;
463 }
464 }
465 return false;
466}
467
Jeff Brown497a92c2010-09-12 17:55:08 -0700468bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
469 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800470 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700471 if (device && led >= 0 && led <= LED_MAX) {
472 if (test_bit(led, device->ledBitmask)) {
473 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700474 }
475 }
476 return false;
477}
478
479void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
480 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800481 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700482 if (device && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700483 struct input_event ev;
484 ev.time.tv_sec = 0;
485 ev.time.tv_usec = 0;
486 ev.type = EV_LED;
487 ev.code = led;
488 ev.value = on ? 1 : 0;
489
490 ssize_t nWrite;
491 do {
492 nWrite = write(device->fd, &ev, sizeof(struct input_event));
493 } while (nWrite == -1 && errno == EINTR);
494 }
495}
496
Jeff Brown90655042010-12-02 13:50:46 -0800497void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
498 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
499 outVirtualKeys.clear();
500
501 AutoMutex _l(mLock);
502 Device* device = getDeviceLocked(deviceId);
503 if (device && device->virtualKeyMap) {
504 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
505 }
506}
507
Jeff Brown1e08fe92011-11-15 17:48:10 -0800508String8 EventHub::getKeyCharacterMapFile(int32_t deviceId) const {
509 AutoMutex _l(mLock);
510 Device* device = getDeviceLocked(deviceId);
511 if (device) {
512 return device->keyMap.keyCharacterMapFile;
513 }
514 return String8();
515}
516
Jeff Brown90655042010-12-02 13:50:46 -0800517EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
518 if (deviceId == 0) {
519 deviceId = mBuiltInKeyboardId;
520 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700521 ssize_t index = mDevices.indexOfKey(deviceId);
522 return index >= 0 ? mDevices.valueAt(index) : NULL;
523}
Jeff Brown90655042010-12-02 13:50:46 -0800524
Jeff Brown93fa9b32011-06-14 17:09:25 -0700525EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
526 for (size_t i = 0; i < mDevices.size(); i++) {
527 Device* device = mDevices.valueAt(i);
528 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800529 return device;
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
532 return NULL;
533}
534
Jeff Brownb7198742011-03-18 18:14:26 -0700535size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Steve Blockec193de2012-01-09 18:35:44 +0000536 ALOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400537
Jeff Brown93fa9b32011-06-14 17:09:25 -0700538 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400539
Jeff Brownb7198742011-03-18 18:14:26 -0700540 struct input_event readBuffer[bufferSize];
541
542 RawEvent* event = buffer;
543 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700544 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700545 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700546 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
547
Jeff Brown1a84fd12011-06-02 01:26:32 -0700548 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700549 if (mNeedToReopenDevices) {
550 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700551
Steve Block6215d3f2012-01-04 20:05:49 +0000552 ALOGI("Reopening all input devices due to a configuration change.");
Jeff Brown1a84fd12011-06-02 01:26:32 -0700553
Jeff Brown93fa9b32011-06-14 17:09:25 -0700554 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700555 mNeedToScanDevices = true;
556 break; // return to the caller before we actually rescan
557 }
558
Jeff Browncc2e7172010-08-17 16:48:25 -0700559 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700560 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800561 Device* device = mClosingDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100562 ALOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 device->id, device->path.string());
564 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700565 event->when = now;
566 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
567 event->type = DEVICE_REMOVED;
568 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700570 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700571 if (--capacity == 0) {
572 break;
573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575
Jeff Brown1a84fd12011-06-02 01:26:32 -0700576 if (mNeedToScanDevices) {
577 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700578 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700579 mNeedToSendFinishedDeviceScan = true;
580 }
581
Jeff Brownb7198742011-03-18 18:14:26 -0700582 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800583 Device* device = mOpeningDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100584 ALOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 device->id, device->path.string());
586 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700587 event->when = now;
588 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
589 event->type = DEVICE_ADDED;
590 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700591 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700592 if (--capacity == 0) {
593 break;
594 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700595 }
596
597 if (mNeedToSendFinishedDeviceScan) {
598 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700599 event->when = now;
600 event->type = FINISHED_DEVICE_SCAN;
601 event += 1;
602 if (--capacity == 0) {
603 break;
604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606
Jeff Browncc2e7172010-08-17 16:48:25 -0700607 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700608 bool deviceChanged = false;
609 while (mPendingEventIndex < mPendingEventCount) {
610 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
611 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
612 if (eventItem.events & EPOLLIN) {
613 mPendingINotify = true;
614 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000615 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700616 }
617 continue;
618 }
619
620 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
621 if (eventItem.events & EPOLLIN) {
Steve Block71f2cf12011-10-20 11:56:00 +0100622 ALOGV("awoken after wake()");
Jeff Brown93fa9b32011-06-14 17:09:25 -0700623 awoken = true;
624 char buffer[16];
625 ssize_t nRead;
626 do {
627 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
628 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
629 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000630 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700631 eventItem.events);
632 }
633 continue;
634 }
635
636 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
637 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000638 ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700639 eventItem.events, eventItem.data.u32);
640 continue;
641 }
642
643 Device* device = mDevices.valueAt(deviceIndex);
644 if (eventItem.events & EPOLLIN) {
645 int32_t readSize = read(device->fd, readBuffer,
646 sizeof(struct input_event) * capacity);
647 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
648 // Device was removed before INotify noticed.
Jeff Brown41305542011-10-05 11:14:13 -0700649 ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d "
650 "capacity: %d errno: %d)\n",
651 device->fd, readSize, bufferSize, capacity, errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700652 deviceChanged = true;
653 closeDeviceLocked(device);
654 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700655 if (errno != EAGAIN && errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000656 ALOGW("could not get event (errno=%d)", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700657 }
658 } else if ((readSize % sizeof(struct input_event)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000659 ALOGE("could not get event (wrong size: %d)", readSize);
Jeff Browncc2e7172010-08-17 16:48:25 -0700660 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700661 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
662
663 size_t count = size_t(readSize) / sizeof(struct input_event);
664 for (size_t i = 0; i < count; i++) {
665 const struct input_event& iev = readBuffer[i];
Steve Block71f2cf12011-10-20 11:56:00 +0100666 ALOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
Jeff Brownb7198742011-03-18 18:14:26 -0700667 device->path.string(),
668 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
669 iev.type, iev.code, iev.value);
670
Jeff Brown4e91a182011-04-07 11:38:09 -0700671#ifdef HAVE_POSIX_CLOCKS
672 // Use the time specified in the event instead of the current time
673 // so that downstream code can get more accurate estimates of
674 // event dispatch latency from the time the event is enqueued onto
675 // the evdev client buffer.
676 //
677 // The event's timestamp fortuitously uses the same monotonic clock
678 // time base as the rest of Android. The kernel event device driver
679 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
680 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
681 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
682 // system call that also queries ktime_get_ts().
683 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
684 + nsecs_t(iev.time.tv_usec) * 1000LL;
Steve Block71f2cf12011-10-20 11:56:00 +0100685 ALOGV("event time %lld, now %lld", event->when, now);
Jeff Brown4e91a182011-04-07 11:38:09 -0700686#else
Jeff Brownb7198742011-03-18 18:14:26 -0700687 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700688#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700689 event->deviceId = deviceId;
690 event->type = iev.type;
691 event->scanCode = iev.code;
692 event->value = iev.value;
693 event->keyCode = AKEYCODE_UNKNOWN;
694 event->flags = 0;
695 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
696 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
697 &event->keyCode, &event->flags);
Steve Block71f2cf12011-10-20 11:56:00 +0100698 ALOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
Jeff Brownb7198742011-03-18 18:14:26 -0700699 iev.code, event->keyCode, event->flags, err);
700 }
701 event += 1;
702 }
703 capacity -= count;
704 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700705 // The result buffer is full. Reset the pending event index
706 // so we will try to read the device again on the next iteration.
707 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700708 break;
709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700711 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000712 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700713 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700716
Jeff Brown93fa9b32011-06-14 17:09:25 -0700717 // readNotify() will modify the list of devices so this must be done after
718 // processing all other events to ensure that we read all remaining events
719 // before closing the devices.
720 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
721 mPendingINotify = false;
722 readNotifyLocked();
723 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800724 }
725
Jeff Brown93fa9b32011-06-14 17:09:25 -0700726 // Report added or removed devices immediately.
727 if (deviceChanged) {
728 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
Jeff Browna9b84222010-10-14 02:23:43 -0700730
Jeff Brown93fa9b32011-06-14 17:09:25 -0700731 // Return now if we have collected any events or if we were explicitly awoken.
732 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700733 break;
734 }
735
Jeff Browncc2e7172010-08-17 16:48:25 -0700736 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700737 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700738 // subtle choreography. When a device driver has pending (unread) events, it acquires
739 // a kernel wake lock. However, once the last pending event has been read, the device
740 // driver will release the kernel wake lock. To prevent the system from going to sleep
741 // when this happens, the EventHub holds onto its own user wake lock while the client
742 // is processing events. Thus the system can only sleep if there are no events
743 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700744 //
745 // The timeout is advisory only. If the device is asleep, it will not wake just to
746 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700747 mPendingEventIndex = 0;
748
749 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700750 release_wake_lock(WAKE_LOCK_ID);
751
Jeff Brown93fa9b32011-06-14 17:09:25 -0700752 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700753
754 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700755 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700756
Jeff Brownaa3855d2011-03-17 01:34:19 -0700757 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700758 // Timed out.
759 mPendingEventCount = 0;
760 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700761 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700762
Jeff Brownaa3855d2011-03-17 01:34:19 -0700763 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700764 // An error occurred.
765 mPendingEventCount = 0;
766
Jeff Brownb7198742011-03-18 18:14:26 -0700767 // Sleep after errors to avoid locking up the system.
768 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700769 if (errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000770 ALOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700771 usleep(100000);
772 }
Jeff Brownb7198742011-03-18 18:14:26 -0700773 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700774 // Some events occurred.
775 mPendingEventCount = size_t(pollResult);
Jeff Browncc2e7172010-08-17 16:48:25 -0700776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
Jeff Brownb7198742011-03-18 18:14:26 -0700778
779 // All done, return the number of events we read.
780 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781}
782
Jeff Brown93fa9b32011-06-14 17:09:25 -0700783void EventHub::wake() {
Steve Block71f2cf12011-10-20 11:56:00 +0100784 ALOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785
Jeff Brown93fa9b32011-06-14 17:09:25 -0700786 ssize_t nWrite;
787 do {
788 nWrite = write(mWakeWritePipeFd, "W", 1);
789 } while (nWrite == -1 && errno == EINTR);
790
791 if (nWrite != 1 && errno != EAGAIN) {
Steve Block8564c8d2012-01-05 23:22:43 +0000792 ALOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700794}
Jeff Brown90655042010-12-02 13:50:46 -0800795
Jeff Brown93fa9b32011-06-14 17:09:25 -0700796void EventHub::scanDevicesLocked() {
797 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 if(res < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000799 ALOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801}
802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803// ----------------------------------------------------------------------------
804
Jeff Brownfd035822010-06-30 16:10:35 -0700805static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
806 const uint8_t* end = array + endIndex;
807 array += startIndex;
808 while (array != end) {
809 if (*(array++) != 0) {
810 return true;
811 }
812 }
813 return false;
814}
815
816static const int32_t GAMEPAD_KEYCODES[] = {
817 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
818 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
819 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
820 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
821 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800822 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
823 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
824 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
825 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
826 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd035822010-06-30 16:10:35 -0700827};
828
Jeff Brown93fa9b32011-06-14 17:09:25 -0700829status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800830 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831
Steve Block71f2cf12011-10-20 11:56:00 +0100832 ALOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833
Jeff Brown874c1e92012-01-19 14:32:47 -0800834 int fd = open(devicePath, O_RDWR | O_CLOEXEC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 if(fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000836 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 return -1;
838 }
839
Jeff Brown90655042010-12-02 13:50:46 -0800840 InputDeviceIdentifier identifier;
841
842 // Get device name.
843 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
844 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
845 } else {
846 buffer[sizeof(buffer) - 1] = '\0';
847 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400849
Jeff Brown90655042010-12-02 13:50:46 -0800850 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700851 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
852 const String8& item = mExcludedDevices.itemAt(i);
853 if (identifier.name == item) {
Steve Block6215d3f2012-01-04 20:05:49 +0000854 ALOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400855 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400856 return -1;
857 }
858 }
859
Jeff Brown90655042010-12-02 13:50:46 -0800860 // Get device driver version.
861 int driverVersion;
862 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Steve Block3762c312012-01-06 19:20:56 +0000863 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -0800864 close(fd);
865 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
867
Jeff Brown90655042010-12-02 13:50:46 -0800868 // Get device identifier.
869 struct input_id inputId;
870 if(ioctl(fd, EVIOCGID, &inputId)) {
Steve Block3762c312012-01-06 19:20:56 +0000871 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -0800872 close(fd);
873 return -1;
874 }
875 identifier.bus = inputId.bustype;
876 identifier.product = inputId.product;
877 identifier.vendor = inputId.vendor;
878 identifier.version = inputId.version;
879
880 // Get device physical location.
881 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
882 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
883 } else {
884 buffer[sizeof(buffer) - 1] = '\0';
885 identifier.location.setTo(buffer);
886 }
887
888 // Get device unique id.
889 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
890 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
891 } else {
892 buffer[sizeof(buffer) - 1] = '\0';
893 identifier.uniqueId.setTo(buffer);
894 }
895
896 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700897 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
Steve Block3762c312012-01-06 19:20:56 +0000898 ALOGE("Error %d making device file descriptor non-blocking.", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700899 close(fd);
900 return -1;
901 }
902
Jeff Brown90655042010-12-02 13:50:46 -0800903 // Allocate device. (The device object takes ownership of the fd at this point.)
904 int32_t deviceId = mNextDeviceId++;
905 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906
907#if 0
Steve Block6215d3f2012-01-04 20:05:49 +0000908 ALOGI("add device %d: %s\n", deviceId, devicePath);
909 ALOGI(" bus: %04x\n"
Jeff Brown90655042010-12-02 13:50:46 -0800910 " vendor %04x\n"
911 " product %04x\n"
912 " version %04x\n",
913 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Steve Block6215d3f2012-01-04 20:05:49 +0000914 ALOGI(" name: \"%s\"\n", identifier.name.string());
915 ALOGI(" location: \"%s\"\n", identifier.location.string());
916 ALOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
917 ALOGI(" driver: v%d.%d.%d\n",
Jeff Brown90655042010-12-02 13:50:46 -0800918 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919#endif
920
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800921 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700922 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800923
Jeff Brownfd035822010-06-30 16:10:35 -0700924 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700925 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
926 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
927 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
928 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
929 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
930 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -0800931
Jeff Brown6f2fba42011-02-19 01:08:02 -0800932 // See if this is a keyboard. Ignore everything in the button range except for
933 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700934 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
935 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -0800936 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700937 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800938 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -0700939 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800940 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800941 if (haveKeyboardKeys || haveGamepadButtons) {
942 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800944
Jeff Brown83c09682010-12-23 17:50:18 -0800945 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700946 if (test_bit(BTN_MOUSE, device->keyBitmask)
947 && test_bit(REL_X, device->relBitmask)
948 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800949 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
Jeff Brownfd035822010-06-30 16:10:35 -0700951
952 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800953 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700954 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
955 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800956 // Some joysticks such as the PS3 controller report axes that conflict
957 // with the ABS_MT range. Try to confirm that the device really is
958 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700959 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800960 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd035822010-06-30 16:10:35 -0700961 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800962 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700963 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
964 && test_bit(ABS_X, device->absBitmask)
965 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800966 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800969 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800970 // Assumes that joysticks always have gamepad buttons in order to distinguish them
971 // from other devices such as accelerometers that also have absolute axes.
Jeff Brown9ee285a2011-08-31 12:56:34 -0700972 if (haveGamepadButtons) {
973 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
974 for (int i = 0; i <= ABS_MAX; i++) {
975 if (test_bit(i, device->absBitmask)
976 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
977 device->classes = assumedClasses;
978 break;
979 }
980 }
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800981 }
982
Jeff Brown93fa9b32011-06-14 17:09:25 -0700983 // Check whether this device has switches.
984 for (int i = 0; i <= SW_MAX; i++) {
985 if (test_bit(i, device->swBitmask)) {
986 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
987 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
Jeff Brown93fa9b32011-06-14 17:09:25 -0700991 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -0800992 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800993 // Load the virtual keys for the touch screen, if any.
994 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700995 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -0800996 if (!status) {
997 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 }
Jeff Brown90655042010-12-02 13:50:46 -0800999 }
1000
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001001 // Load the key map.
1002 // We need to do this for joysticks too because the key layout may specify axes.
1003 status_t keyMapStatus = NAME_NOT_FOUND;
1004 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -08001005 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001006 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001007 }
Jeff Brown90655042010-12-02 13:50:46 -08001008
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001009 // Configure the keyboard, gamepad or virtual keyboard.
1010 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001011 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001012 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -08001013 && mBuiltInKeyboardId == -1
1014 && isEligibleBuiltInKeyboard(device->identifier,
1015 device->configuration, &device->keyMap)) {
1016 mBuiltInKeyboardId = device->id;
Jeff Brown497a92c2010-09-12 17:55:08 -07001017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001019 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f48712010-10-01 17:46:21 -07001020 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001021 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001022 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001023
Jeff Brownfd035822010-06-30 16:10:35 -07001024 // See if this device has a DPAD.
Jeff Brownf2f48712010-10-01 17:46:21 -07001025 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1026 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1027 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1028 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1029 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001030 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001031 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001032
Jeff Brownfd035822010-06-30 16:10:35 -07001033 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001034 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f48712010-10-01 17:46:21 -07001035 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd035822010-06-30 16:10:35 -07001036 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1037 break;
1038 }
1039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
Sean McNeilaeb00c42010-06-23 16:00:37 +07001042 // If the device isn't recognized as something we handle, don't monitor it.
1043 if (device->classes == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001044 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Jeff Brown90655042010-12-02 13:50:46 -08001045 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001046 delete device;
1047 return -1;
1048 }
1049
Jeff Brown56194eb2011-03-02 19:23:13 -08001050 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001051 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001052 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1053 }
1054
Jeff Brown93fa9b32011-06-14 17:09:25 -07001055 // Register with epoll.
1056 struct epoll_event eventItem;
1057 memset(&eventItem, 0, sizeof(eventItem));
1058 eventItem.events = EPOLLIN;
1059 eventItem.data.u32 = deviceId;
1060 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
Steve Block3762c312012-01-06 19:20:56 +00001061 ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001062 delete device;
1063 return -1;
1064 }
1065
Jeff Browne22afbe2011-12-16 13:45:40 -08001066 // Enable wake-lock behavior on kernels that support it.
1067 // TODO: Only need this for devices that can really wake the system.
1068 bool usingSuspendBlock = ioctl(fd, EVIOCSSUSPENDBLOCK, 1) == 0;
1069
Steve Block6215d3f2012-01-04 20:05:49 +00001070 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
Jeff Browne22afbe2011-12-16 13:45:40 -08001071 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
1072 "usingSuspendBlock=%s",
Jeff Brown90655042010-12-02 13:50:46 -08001073 deviceId, fd, devicePath, device->identifier.name.string(),
1074 device->classes,
1075 device->configurationFile.string(),
1076 device->keyMap.keyLayoutFile.string(),
1077 device->keyMap.keyCharacterMapFile.string(),
Jeff Browne22afbe2011-12-16 13:45:40 -08001078 toString(mBuiltInKeyboardId == deviceId),
1079 toString(usingSuspendBlock));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001080
Jeff Brown93fa9b32011-06-14 17:09:25 -07001081 mDevices.add(deviceId, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 device->next = mOpeningDevices;
1084 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 return 0;
1086}
1087
Jeff Brown93fa9b32011-06-14 17:09:25 -07001088void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001089 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1090 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001091 if (device->configurationFile.isEmpty()) {
Steve Block5baa3a62011-12-20 16:23:08 +00001092 ALOGD("No input device configuration file found for device '%s'.",
Jeff Brown90655042010-12-02 13:50:46 -08001093 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001094 } else {
1095 status_t status = PropertyMap::load(device->configurationFile,
1096 &device->configuration);
1097 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001098 ALOGE("Error loading input device configuration file for device '%s'. "
Jeff Brown90655042010-12-02 13:50:46 -08001099 "Using default configuration.",
1100 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001101 }
1102 }
1103}
1104
Jeff Brown93fa9b32011-06-14 17:09:25 -07001105status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001106 // The virtual key map is supplied by the kernel as a system board property file.
1107 String8 path;
1108 path.append("/sys/board_properties/virtualkeys.");
1109 path.append(device->identifier.name);
1110 if (access(path.string(), R_OK)) {
1111 return NAME_NOT_FOUND;
1112 }
1113 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001114}
1115
Jeff Brown93fa9b32011-06-14 17:09:25 -07001116status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001117 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001118}
1119
Jeff Brown93fa9b32011-06-14 17:09:25 -07001120bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001121 if (device->configuration) {
1122 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001123 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1124 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001125 }
1126 }
1127 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1128}
1129
Jeff Brown90655042010-12-02 13:50:46 -08001130bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1131 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001132 return false;
1133 }
1134
1135 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001136 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001137 const size_t N = scanCodes.size();
1138 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1139 int32_t sc = scanCodes.itemAt(i);
1140 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1141 return true;
1142 }
1143 }
1144
1145 return false;
1146}
1147
Jeff Brown93fa9b32011-06-14 17:09:25 -07001148status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1149 Device* device = getDeviceByPathLocked(devicePath);
1150 if (device) {
1151 closeDeviceLocked(device);
1152 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
Steve Block71f2cf12011-10-20 11:56:00 +01001154 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 return -1;
1156}
1157
Jeff Brown93fa9b32011-06-14 17:09:25 -07001158void EventHub::closeAllDevicesLocked() {
1159 while (mDevices.size() > 0) {
1160 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1161 }
1162}
1163
1164void EventHub::closeDeviceLocked(Device* device) {
Steve Block6215d3f2012-01-04 20:05:49 +00001165 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001166 device->path.string(), device->identifier.name.string(), device->id,
1167 device->fd, device->classes);
1168
Jeff Brown33bbfd22011-02-24 20:55:35 -08001169 if (device->id == mBuiltInKeyboardId) {
Steve Block8564c8d2012-01-05 23:22:43 +00001170 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001171 device->path.string(), mBuiltInKeyboardId);
1172 mBuiltInKeyboardId = -1;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001173 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001174
Jeff Brown93fa9b32011-06-14 17:09:25 -07001175 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001176 ALOGW("Could not remove device fd from epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001177 }
1178
1179 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001180 device->close();
1181
Jeff Brown8e9d4432011-03-12 19:46:59 -08001182 // Unlink for opening devices list if it is present.
1183 Device* pred = NULL;
1184 bool found = false;
1185 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1186 if (entry == device) {
1187 found = true;
1188 break;
1189 }
1190 pred = entry;
1191 entry = entry->next;
1192 }
1193 if (found) {
1194 // Unlink the device from the opening devices list then delete it.
1195 // We don't need to tell the client that the device was closed because
1196 // it does not even know it was opened in the first place.
Steve Block6215d3f2012-01-04 20:05:49 +00001197 ALOGI("Device %s was immediately closed after opening.", device->path.string());
Jeff Brown8e9d4432011-03-12 19:46:59 -08001198 if (pred) {
1199 pred->next = device->next;
1200 } else {
1201 mOpeningDevices = device->next;
1202 }
1203 delete device;
1204 } else {
1205 // Link into closing devices list.
1206 // The device will be deleted later after we have informed the client.
1207 device->next = mClosingDevices;
1208 mClosingDevices = device;
1209 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001210}
1211
Jeff Brown93fa9b32011-06-14 17:09:25 -07001212status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 int res;
1214 char devname[PATH_MAX];
1215 char *filename;
1216 char event_buf[512];
1217 int event_size;
1218 int event_pos = 0;
1219 struct inotify_event *event;
1220
Steve Block71f2cf12011-10-20 11:56:00 +01001221 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001222 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 if(res < (int)sizeof(*event)) {
1224 if(errno == EINTR)
1225 return 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001226 ALOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001227 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 //printf("got %d bytes of event information\n", res);
1230
Jeff Brown90655042010-12-02 13:50:46 -08001231 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 filename = devname + strlen(devname);
1233 *filename++ = '/';
1234
1235 while(res >= (int)sizeof(*event)) {
1236 event = (struct inotify_event *)(event_buf + event_pos);
1237 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1238 if(event->len) {
1239 strcpy(filename, event->name);
1240 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001241 openDeviceLocked(devname);
1242 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00001243 ALOGI("Removing device '%s' due to inotify event\n", devname);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001244 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
1246 }
1247 event_size = sizeof(*event) + event->len;
1248 res -= event_size;
1249 event_pos += event_size;
1250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 return 0;
1252}
1253
Jeff Brown93fa9b32011-06-14 17:09:25 -07001254status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255{
1256 char devname[PATH_MAX];
1257 char *filename;
1258 DIR *dir;
1259 struct dirent *de;
1260 dir = opendir(dirname);
1261 if(dir == NULL)
1262 return -1;
1263 strcpy(devname, dirname);
1264 filename = devname + strlen(devname);
1265 *filename++ = '/';
1266 while((de = readdir(dir))) {
1267 if(de->d_name[0] == '.' &&
1268 (de->d_name[1] == '\0' ||
1269 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1270 continue;
1271 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001272 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274 closedir(dir);
1275 return 0;
1276}
1277
Jeff Brown93fa9b32011-06-14 17:09:25 -07001278void EventHub::requestReopenDevices() {
Steve Block71f2cf12011-10-20 11:56:00 +01001279 ALOGV("requestReopenDevices() called");
Jeff Brown93fa9b32011-06-14 17:09:25 -07001280
1281 AutoMutex _l(mLock);
1282 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001283}
1284
Jeff Brownf2f48712010-10-01 17:46:21 -07001285void EventHub::dump(String8& dump) {
1286 dump.append("Event Hub State:\n");
1287
1288 { // acquire lock
1289 AutoMutex _l(mLock);
1290
Jeff Brown90655042010-12-02 13:50:46 -08001291 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f48712010-10-01 17:46:21 -07001292
1293 dump.append(INDENT "Devices:\n");
1294
Jeff Brown93fa9b32011-06-14 17:09:25 -07001295 for (size_t i = 0; i < mDevices.size(); i++) {
1296 const Device* device = mDevices.valueAt(i);
1297 if (mBuiltInKeyboardId == device->id) {
1298 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1299 device->id, device->identifier.name.string());
1300 } else {
1301 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1302 device->identifier.name.string());
Jeff Brownf2f48712010-10-01 17:46:21 -07001303 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001304 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1305 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
1306 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1307 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1308 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1309 "product=0x%04x, version=0x%04x\n",
1310 device->identifier.bus, device->identifier.vendor,
1311 device->identifier.product, device->identifier.version);
1312 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1313 device->keyMap.keyLayoutFile.string());
1314 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1315 device->keyMap.keyCharacterMapFile.string());
1316 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1317 device->configurationFile.string());
Jeff Brownf2f48712010-10-01 17:46:21 -07001318 }
1319 } // release lock
1320}
1321
Jeff Brown89ef0722011-08-10 16:25:21 -07001322void EventHub::monitor() {
1323 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1324 mLock.lock();
1325 mLock.unlock();
1326}
1327
1328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329}; // namespace android