blob: 665d711f8a9b3657f3151c37506cf8ef087c4e15 [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 Brownfd035822010-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 Brownf2f48712010-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 Brownf2f48712010-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 Brown9ee285a2011-08-31 12:56:34 -070096// --- Global Functions ---
97
98uint32_t getAbsAxisUsage(int32_t axis, uint32_t deviceClasses) {
99 // Touch devices get dibs on touch-related axes.
100 if (deviceClasses & INPUT_DEVICE_CLASS_TOUCH) {
101 switch (axis) {
102 case ABS_X:
103 case ABS_Y:
104 case ABS_PRESSURE:
105 case ABS_TOOL_WIDTH:
106 case ABS_DISTANCE:
107 case ABS_TILT_X:
108 case ABS_TILT_Y:
109 case ABS_MT_SLOT:
110 case ABS_MT_TOUCH_MAJOR:
111 case ABS_MT_TOUCH_MINOR:
112 case ABS_MT_WIDTH_MAJOR:
113 case ABS_MT_WIDTH_MINOR:
114 case ABS_MT_ORIENTATION:
115 case ABS_MT_POSITION_X:
116 case ABS_MT_POSITION_Y:
117 case ABS_MT_TOOL_TYPE:
118 case ABS_MT_BLOB_ID:
119 case ABS_MT_TRACKING_ID:
120 case ABS_MT_PRESSURE:
121 case ABS_MT_DISTANCE:
122 return INPUT_DEVICE_CLASS_TOUCH;
123 }
124 }
125
126 // Joystick devices get the rest.
127 return deviceClasses & INPUT_DEVICE_CLASS_JOYSTICK;
128}
129
Jeff Brown90655042010-12-02 13:50:46 -0800130// --- EventHub::Device ---
131
132EventHub::Device::Device(int fd, int32_t id, const String8& path,
133 const InputDeviceIdentifier& identifier) :
134 next(NULL),
135 fd(fd), id(id), path(path), identifier(identifier),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700136 classes(0), configuration(NULL), virtualKeyMap(NULL) {
137 memset(keyBitmask, 0, sizeof(keyBitmask));
138 memset(absBitmask, 0, sizeof(absBitmask));
139 memset(relBitmask, 0, sizeof(relBitmask));
140 memset(swBitmask, 0, sizeof(swBitmask));
141 memset(ledBitmask, 0, sizeof(ledBitmask));
142 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143}
144
Jeff Brown90655042010-12-02 13:50:46 -0800145EventHub::Device::~Device() {
146 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800147 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800148 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149}
150
Jeff Brown90655042010-12-02 13:50:46 -0800151void EventHub::Device::close() {
152 if (fd >= 0) {
153 ::close(fd);
154 fd = -1;
155 }
156}
157
158
159// --- EventHub ---
160
Jeff Brown93fa9b32011-06-14 17:09:25 -0700161const uint32_t EventHub::EPOLL_ID_INOTIFY;
162const uint32_t EventHub::EPOLL_ID_WAKE;
163const int EventHub::EPOLL_SIZE_HINT;
164const int EventHub::EPOLL_MAX_EVENTS;
165
Jeff Brown90655042010-12-02 13:50:46 -0800166EventHub::EventHub(void) :
Jeff Brown93fa9b32011-06-14 17:09:25 -0700167 mBuiltInKeyboardId(-1), mNextDeviceId(1),
Jeff Brown90655042010-12-02 13:50:46 -0800168 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700169 mNeedToSendFinishedDeviceScan(false),
170 mNeedToReopenDevices(false), mNeedToScanDevices(true),
171 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700173
Jeff Brown93fa9b32011-06-14 17:09:25 -0700174 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
175 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
176
177 mINotifyFd = inotify_init();
178 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
179 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
180 DEVICE_PATH, errno);
181
182 struct epoll_event eventItem;
183 memset(&eventItem, 0, sizeof(eventItem));
184 eventItem.events = EPOLLIN;
185 eventItem.data.u32 = EPOLL_ID_INOTIFY;
186 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
187 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
188
189 int wakeFds[2];
190 result = pipe(wakeFds);
191 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
192
193 mWakeReadPipeFd = wakeFds[0];
194 mWakeWritePipeFd = wakeFds[1];
195
196 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
197 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
198 errno);
199
200 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
201 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
202 errno);
203
204 eventItem.data.u32 = EPOLL_ID_WAKE;
205 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
206 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
207 errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208}
209
Jeff Brown90655042010-12-02 13:50:46 -0800210EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700211 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Jeff Brown93fa9b32011-06-14 17:09:25 -0700213 while (mClosingDevices) {
214 Device* device = mClosingDevices;
215 mClosingDevices = device->next;
216 delete device;
217 }
218
219 ::close(mEpollFd);
220 ::close(mINotifyFd);
221 ::close(mWakeReadPipeFd);
222 ::close(mWakeWritePipeFd);
223
224 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225}
226
Jeff Browne38fdfa2012-04-06 14:51:01 -0700227InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800229 Device* device = getDeviceLocked(deviceId);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700230 if (device == NULL) return InputDeviceIdentifier();
231 return device->identifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232}
233
Jeff Brown90655042010-12-02 13:50:46 -0800234uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800236 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 if (device == NULL) return 0;
238 return device->classes;
239}
240
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800241void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800242 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800243 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800244 if (device && device->configuration) {
245 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800246 } else {
247 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800248 }
249}
250
Jeff Brown6d0fec22010-07-23 21:28:06 -0700251status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
252 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700253 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700254
Jeff Brownba421dd2011-08-10 15:07:05 -0700255 if (axis >= 0 && axis <= ABS_MAX) {
256 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Jeff Brownba421dd2011-08-10 15:07:05 -0700258 Device* device = getDeviceLocked(deviceId);
259 if (device && test_bit(axis, device->absBitmask)) {
260 struct input_absinfo info;
261 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000262 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700263 axis, device->identifier.name.string(), device->fd, errno);
264 return -errno;
265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Jeff Brownba421dd2011-08-10 15:07:05 -0700267 if (info.minimum != info.maximum) {
268 outAxisInfo->valid = true;
269 outAxisInfo->minValue = info.minimum;
270 outAxisInfo->maxValue = info.maximum;
271 outAxisInfo->flat = info.flat;
272 outAxisInfo->fuzz = info.fuzz;
273 outAxisInfo->resolution = info.resolution;
274 }
275 return OK;
276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700278 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279}
280
Jeff Browncc0c1592011-02-19 05:07:28 -0800281bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
282 if (axis >= 0 && axis <= REL_MAX) {
283 AutoMutex _l(mLock);
284
285 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700286 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800287 return test_bit(axis, device->relBitmask);
288 }
289 }
290 return false;
291}
292
Jeff Brown80fd47c2011-05-24 01:07:44 -0700293bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
294 if (property >= 0 && property <= INPUT_PROP_MAX) {
295 AutoMutex _l(mLock);
296
297 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700298 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700299 return test_bit(property, device->propBitmask);
300 }
301 }
302 return false;
303}
304
Jeff Brown6d0fec22010-07-23 21:28:06 -0700305int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700306 if (scanCode >= 0 && scanCode <= KEY_MAX) {
307 AutoMutex _l(mLock);
308
Jeff Brown90655042010-12-02 13:50:46 -0800309 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700310 if (device && test_bit(scanCode, device->keyBitmask)) {
311 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
312 memset(keyState, 0, sizeof(keyState));
313 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
314 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
317 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700318 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319}
320
Jeff Brown6d0fec22010-07-23 21:28:06 -0700321int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
322 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700323
Jeff Brown90655042010-12-02 13:50:46 -0800324 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700325 if (device && device->keyMap.haveKeyLayout()) {
326 Vector<int32_t> scanCodes;
327 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
328 if (scanCodes.size() != 0) {
329 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
330 memset(keyState, 0, sizeof(keyState));
331 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
332 for (size_t i = 0; i < scanCodes.size(); i++) {
333 int32_t sc = scanCodes.itemAt(i);
334 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
335 return AKEY_STATE_DOWN;
336 }
337 }
338 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340 }
341 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700342 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700343}
344
Jeff Brown6d0fec22010-07-23 21:28:06 -0700345int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700346 if (sw >= 0 && sw <= SW_MAX) {
347 AutoMutex _l(mLock);
348
Jeff Brown90655042010-12-02 13:50:46 -0800349 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700350 if (device && test_bit(sw, device->swBitmask)) {
351 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
352 memset(swState, 0, sizeof(swState));
353 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
354 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
355 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700356 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700357 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700358 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700359}
360
Jeff Brown2717eff2011-06-30 23:53:07 -0700361status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700362 *outValue = 0;
363
Jeff Brown2717eff2011-06-30 23:53:07 -0700364 if (axis >= 0 && axis <= ABS_MAX) {
365 AutoMutex _l(mLock);
366
367 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700368 if (device && test_bit(axis, device->absBitmask)) {
369 struct input_absinfo info;
370 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000371 ALOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
Jeff Brownba421dd2011-08-10 15:07:05 -0700372 axis, device->identifier.name.string(), device->fd, errno);
373 return -errno;
374 }
375
376 *outValue = info.value;
377 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700378 }
379 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700380 return -1;
381}
382
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
384 const int32_t* keyCodes, uint8_t* outFlags) const {
385 AutoMutex _l(mLock);
386
Jeff Brown90655042010-12-02 13:50:46 -0800387 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700388 if (device && device->keyMap.haveKeyLayout()) {
389 Vector<int32_t> scanCodes;
390 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
391 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392
Jeff Brownba421dd2011-08-10 15:07:05 -0700393 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
394 keyCodes[codeIndex], &scanCodes);
395 if (! err) {
396 // check the possible scan codes identified by the layout map against the
397 // map of codes actually emitted by the driver
398 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
399 if (test_bit(scanCodes[sc], device->keyBitmask)) {
400 outFlags[codeIndex] = 1;
401 break;
402 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700403 }
404 }
405 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700406 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700408 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409}
410
Jeff Brown6f2fba42011-02-19 01:08:02 -0800411status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700412 int32_t* outKeycode, uint32_t* outFlags) const
413{
414 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800415 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700416
Jeff Brown90655042010-12-02 13:50:46 -0800417 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800418 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700419 if (err == NO_ERROR) {
420 return NO_ERROR;
421 }
422 }
423
Jeff Brown90655042010-12-02 13:50:46 -0800424 if (mBuiltInKeyboardId != -1) {
425 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700426
Jeff Brown90655042010-12-02 13:50:46 -0800427 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800428 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700429 if (err == NO_ERROR) {
430 return NO_ERROR;
431 }
432 }
433 }
434
435 *outKeycode = 0;
436 *outFlags = 0;
437 return NAME_NOT_FOUND;
438}
439
Jeff Brown85297452011-03-04 13:07:49 -0800440status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800441{
442 AutoMutex _l(mLock);
443 Device* device = getDeviceLocked(deviceId);
444
445 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800446 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800447 if (err == NO_ERROR) {
448 return NO_ERROR;
449 }
450 }
451
452 if (mBuiltInKeyboardId != -1) {
453 device = getDeviceLocked(mBuiltInKeyboardId);
454
455 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800456 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800457 if (err == NO_ERROR) {
458 return NO_ERROR;
459 }
460 }
461 }
462
Jeff Brown6f2fba42011-02-19 01:08:02 -0800463 return NAME_NOT_FOUND;
464}
465
Jeff Brown1a84fd12011-06-02 01:26:32 -0700466void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700467 AutoMutex _l(mLock);
468
Jeff Brown1a84fd12011-06-02 01:26:32 -0700469 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400470}
471
Jeff Brown49754db2011-07-01 17:37:58 -0700472bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
473 AutoMutex _l(mLock);
474 Device* device = getDeviceLocked(deviceId);
475 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
476 if (test_bit(scanCode, device->keyBitmask)) {
477 return true;
478 }
479 }
480 return false;
481}
482
Jeff Brown497a92c2010-09-12 17:55:08 -0700483bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
484 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800485 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700486 if (device && led >= 0 && led <= LED_MAX) {
487 if (test_bit(led, device->ledBitmask)) {
488 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700489 }
490 }
491 return false;
492}
493
494void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
495 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800496 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700497 if (device && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700498 struct input_event ev;
499 ev.time.tv_sec = 0;
500 ev.time.tv_usec = 0;
501 ev.type = EV_LED;
502 ev.code = led;
503 ev.value = on ? 1 : 0;
504
505 ssize_t nWrite;
506 do {
507 nWrite = write(device->fd, &ev, sizeof(struct input_event));
508 } while (nWrite == -1 && errno == EINTR);
509 }
510}
511
Jeff Brown90655042010-12-02 13:50:46 -0800512void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
513 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
514 outVirtualKeys.clear();
515
516 AutoMutex _l(mLock);
517 Device* device = getDeviceLocked(deviceId);
518 if (device && device->virtualKeyMap) {
519 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
520 }
521}
522
Jeff Brown1e08fe92011-11-15 17:48:10 -0800523String8 EventHub::getKeyCharacterMapFile(int32_t deviceId) const {
524 AutoMutex _l(mLock);
525 Device* device = getDeviceLocked(deviceId);
526 if (device) {
527 return device->keyMap.keyCharacterMapFile;
528 }
529 return String8();
530}
531
Jeff Brown90655042010-12-02 13:50:46 -0800532EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
533 if (deviceId == 0) {
534 deviceId = mBuiltInKeyboardId;
535 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700536 ssize_t index = mDevices.indexOfKey(deviceId);
537 return index >= 0 ? mDevices.valueAt(index) : NULL;
538}
Jeff Brown90655042010-12-02 13:50:46 -0800539
Jeff Brown93fa9b32011-06-14 17:09:25 -0700540EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
541 for (size_t i = 0; i < mDevices.size(); i++) {
542 Device* device = mDevices.valueAt(i);
543 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800544 return device;
545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
547 return NULL;
548}
549
Jeff Brownb7198742011-03-18 18:14:26 -0700550size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Steve Blockec193de2012-01-09 18:35:44 +0000551 ALOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400552
Jeff Brown93fa9b32011-06-14 17:09:25 -0700553 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400554
Jeff Brownb7198742011-03-18 18:14:26 -0700555 struct input_event readBuffer[bufferSize];
556
557 RawEvent* event = buffer;
558 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700559 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700560 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700561 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
562
Jeff Brown1a84fd12011-06-02 01:26:32 -0700563 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700564 if (mNeedToReopenDevices) {
565 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700566
Steve Block6215d3f2012-01-04 20:05:49 +0000567 ALOGI("Reopening all input devices due to a configuration change.");
Jeff Brown1a84fd12011-06-02 01:26:32 -0700568
Jeff Brown93fa9b32011-06-14 17:09:25 -0700569 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700570 mNeedToScanDevices = true;
571 break; // return to the caller before we actually rescan
572 }
573
Jeff Browncc2e7172010-08-17 16:48:25 -0700574 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700575 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800576 Device* device = mClosingDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100577 ALOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 device->id, device->path.string());
579 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700580 event->when = now;
581 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
582 event->type = DEVICE_REMOVED;
583 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700585 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700586 if (--capacity == 0) {
587 break;
588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700590
Jeff Brown1a84fd12011-06-02 01:26:32 -0700591 if (mNeedToScanDevices) {
592 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700593 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700594 mNeedToSendFinishedDeviceScan = true;
595 }
596
Jeff Brownb7198742011-03-18 18:14:26 -0700597 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800598 Device* device = mOpeningDevices;
Steve Block71f2cf12011-10-20 11:56:00 +0100599 ALOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 device->id, device->path.string());
601 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700602 event->when = now;
603 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
604 event->type = DEVICE_ADDED;
605 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700606 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700607 if (--capacity == 0) {
608 break;
609 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700610 }
611
612 if (mNeedToSendFinishedDeviceScan) {
613 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700614 event->when = now;
615 event->type = FINISHED_DEVICE_SCAN;
616 event += 1;
617 if (--capacity == 0) {
618 break;
619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
621
Jeff Browncc2e7172010-08-17 16:48:25 -0700622 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700623 bool deviceChanged = false;
624 while (mPendingEventIndex < mPendingEventCount) {
625 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
626 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
627 if (eventItem.events & EPOLLIN) {
628 mPendingINotify = true;
629 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000630 ALOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700631 }
632 continue;
633 }
634
635 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
636 if (eventItem.events & EPOLLIN) {
Steve Block71f2cf12011-10-20 11:56:00 +0100637 ALOGV("awoken after wake()");
Jeff Brown93fa9b32011-06-14 17:09:25 -0700638 awoken = true;
639 char buffer[16];
640 ssize_t nRead;
641 do {
642 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
643 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
644 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000645 ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700646 eventItem.events);
647 }
648 continue;
649 }
650
651 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
652 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000653 ALOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700654 eventItem.events, eventItem.data.u32);
655 continue;
656 }
657
658 Device* device = mDevices.valueAt(deviceIndex);
659 if (eventItem.events & EPOLLIN) {
660 int32_t readSize = read(device->fd, readBuffer,
661 sizeof(struct input_event) * capacity);
662 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
663 // Device was removed before INotify noticed.
Jeff Brown41305542011-10-05 11:14:13 -0700664 ALOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d "
665 "capacity: %d errno: %d)\n",
666 device->fd, readSize, bufferSize, capacity, errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700667 deviceChanged = true;
668 closeDeviceLocked(device);
669 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700670 if (errno != EAGAIN && errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000671 ALOGW("could not get event (errno=%d)", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700672 }
673 } else if ((readSize % sizeof(struct input_event)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000674 ALOGE("could not get event (wrong size: %d)", readSize);
Jeff Browncc2e7172010-08-17 16:48:25 -0700675 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700676 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
677
678 size_t count = size_t(readSize) / sizeof(struct input_event);
679 for (size_t i = 0; i < count; i++) {
680 const struct input_event& iev = readBuffer[i];
Steve Block71f2cf12011-10-20 11:56:00 +0100681 ALOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
Jeff Brownb7198742011-03-18 18:14:26 -0700682 device->path.string(),
683 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
684 iev.type, iev.code, iev.value);
685
Jeff Brown4e91a182011-04-07 11:38:09 -0700686#ifdef HAVE_POSIX_CLOCKS
687 // Use the time specified in the event instead of the current time
688 // so that downstream code can get more accurate estimates of
689 // event dispatch latency from the time the event is enqueued onto
690 // the evdev client buffer.
691 //
692 // The event's timestamp fortuitously uses the same monotonic clock
693 // time base as the rest of Android. The kernel event device driver
694 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
695 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
696 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
697 // system call that also queries ktime_get_ts().
698 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
699 + nsecs_t(iev.time.tv_usec) * 1000LL;
Steve Block71f2cf12011-10-20 11:56:00 +0100700 ALOGV("event time %lld, now %lld", event->when, now);
Jeff Brown4e91a182011-04-07 11:38:09 -0700701#else
Jeff Brownb7198742011-03-18 18:14:26 -0700702 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700703#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700704 event->deviceId = deviceId;
705 event->type = iev.type;
706 event->scanCode = iev.code;
707 event->value = iev.value;
708 event->keyCode = AKEYCODE_UNKNOWN;
709 event->flags = 0;
710 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
711 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
712 &event->keyCode, &event->flags);
Steve Block71f2cf12011-10-20 11:56:00 +0100713 ALOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
Jeff Brownb7198742011-03-18 18:14:26 -0700714 iev.code, event->keyCode, event->flags, err);
715 }
716 event += 1;
717 }
718 capacity -= count;
719 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700720 // The result buffer is full. Reset the pending event index
721 // so we will try to read the device again on the next iteration.
722 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700723 break;
724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700726 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000727 ALOGW("Received unexpected epoll event 0x%08x for device %s.",
Jeff Brown93fa9b32011-06-14 17:09:25 -0700728 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700731
Jeff Brown93fa9b32011-06-14 17:09:25 -0700732 // readNotify() will modify the list of devices so this must be done after
733 // processing all other events to ensure that we read all remaining events
734 // before closing the devices.
735 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
736 mPendingINotify = false;
737 readNotifyLocked();
738 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800739 }
740
Jeff Brown93fa9b32011-06-14 17:09:25 -0700741 // Report added or removed devices immediately.
742 if (deviceChanged) {
743 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
Jeff Browna9b84222010-10-14 02:23:43 -0700745
Jeff Brown93fa9b32011-06-14 17:09:25 -0700746 // Return now if we have collected any events or if we were explicitly awoken.
747 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700748 break;
749 }
750
Jeff Browncc2e7172010-08-17 16:48:25 -0700751 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700752 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700753 // subtle choreography. When a device driver has pending (unread) events, it acquires
754 // a kernel wake lock. However, once the last pending event has been read, the device
755 // driver will release the kernel wake lock. To prevent the system from going to sleep
756 // when this happens, the EventHub holds onto its own user wake lock while the client
757 // is processing events. Thus the system can only sleep if there are no events
758 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700759 //
760 // The timeout is advisory only. If the device is asleep, it will not wake just to
761 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700762 mPendingEventIndex = 0;
763
764 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700765 release_wake_lock(WAKE_LOCK_ID);
766
Jeff Brown93fa9b32011-06-14 17:09:25 -0700767 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700768
769 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700770 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700771
Jeff Brownaa3855d2011-03-17 01:34:19 -0700772 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700773 // Timed out.
774 mPendingEventCount = 0;
775 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700776 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700777
Jeff Brownaa3855d2011-03-17 01:34:19 -0700778 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700779 // An error occurred.
780 mPendingEventCount = 0;
781
Jeff Brownb7198742011-03-18 18:14:26 -0700782 // Sleep after errors to avoid locking up the system.
783 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700784 if (errno != EINTR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000785 ALOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700786 usleep(100000);
787 }
Jeff Brownb7198742011-03-18 18:14:26 -0700788 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700789 // Some events occurred.
790 mPendingEventCount = size_t(pollResult);
Jeff Browncc2e7172010-08-17 16:48:25 -0700791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
Jeff Brownb7198742011-03-18 18:14:26 -0700793
794 // All done, return the number of events we read.
795 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796}
797
Jeff Brown93fa9b32011-06-14 17:09:25 -0700798void EventHub::wake() {
Steve Block71f2cf12011-10-20 11:56:00 +0100799 ALOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800
Jeff Brown93fa9b32011-06-14 17:09:25 -0700801 ssize_t nWrite;
802 do {
803 nWrite = write(mWakeWritePipeFd, "W", 1);
804 } while (nWrite == -1 && errno == EINTR);
805
806 if (nWrite != 1 && errno != EAGAIN) {
Steve Block8564c8d2012-01-05 23:22:43 +0000807 ALOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700809}
Jeff Brown90655042010-12-02 13:50:46 -0800810
Jeff Brown93fa9b32011-06-14 17:09:25 -0700811void EventHub::scanDevicesLocked() {
812 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 if(res < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000814 ALOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816}
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818// ----------------------------------------------------------------------------
819
Jeff Brownfd035822010-06-30 16:10:35 -0700820static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
821 const uint8_t* end = array + endIndex;
822 array += startIndex;
823 while (array != end) {
824 if (*(array++) != 0) {
825 return true;
826 }
827 }
828 return false;
829}
830
831static const int32_t GAMEPAD_KEYCODES[] = {
832 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
833 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
834 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
835 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
836 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800837 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
838 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
839 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
840 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
841 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd035822010-06-30 16:10:35 -0700842};
843
Jeff Brown93fa9b32011-06-14 17:09:25 -0700844status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800845 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846
Steve Block71f2cf12011-10-20 11:56:00 +0100847 ALOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848
Jeff Brown874c1e92012-01-19 14:32:47 -0800849 int fd = open(devicePath, O_RDWR | O_CLOEXEC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 if(fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000851 ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 return -1;
853 }
854
Jeff Brown90655042010-12-02 13:50:46 -0800855 InputDeviceIdentifier identifier;
856
857 // Get device name.
858 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
859 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
860 } else {
861 buffer[sizeof(buffer) - 1] = '\0';
862 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400864
Jeff Brown90655042010-12-02 13:50:46 -0800865 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700866 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
867 const String8& item = mExcludedDevices.itemAt(i);
868 if (identifier.name == item) {
Steve Block6215d3f2012-01-04 20:05:49 +0000869 ALOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400870 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400871 return -1;
872 }
873 }
874
Jeff Brown90655042010-12-02 13:50:46 -0800875 // Get device driver version.
876 int driverVersion;
877 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
Steve Block3762c312012-01-06 19:20:56 +0000878 ALOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -0800879 close(fd);
880 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882
Jeff Brown90655042010-12-02 13:50:46 -0800883 // Get device identifier.
884 struct input_id inputId;
885 if(ioctl(fd, EVIOCGID, &inputId)) {
Steve Block3762c312012-01-06 19:20:56 +0000886 ALOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
Jeff Brown90655042010-12-02 13:50:46 -0800887 close(fd);
888 return -1;
889 }
890 identifier.bus = inputId.bustype;
891 identifier.product = inputId.product;
892 identifier.vendor = inputId.vendor;
893 identifier.version = inputId.version;
894
895 // Get device physical location.
896 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
897 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
898 } else {
899 buffer[sizeof(buffer) - 1] = '\0';
900 identifier.location.setTo(buffer);
901 }
902
903 // Get device unique id.
904 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
905 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
906 } else {
907 buffer[sizeof(buffer) - 1] = '\0';
908 identifier.uniqueId.setTo(buffer);
909 }
910
Jeff Browne38fdfa2012-04-06 14:51:01 -0700911 // Compute a device descriptor that uniquely identifies the device.
912 // The descriptor is assumed to be a stable identifier. Its value should not
913 // change between reboots, reconnections, firmware updates or new releases of Android.
914 // Ideally, we also want the descriptor to be short and relatively opaque.
915 String8 rawDescriptor;
916 rawDescriptor.appendFormat(":%04x:%04x:", identifier.vendor, identifier.product);
917 if (!identifier.uniqueId.isEmpty()) {
918 rawDescriptor.append("uniqueId:");
919 rawDescriptor.append(identifier.uniqueId);
920 } if (identifier.vendor == 0 && identifier.product == 0) {
921 // If we don't know the vendor and product id, then the device is probably
922 // built-in so we need to rely on other information to uniquely identify
923 // the input device. Usually we try to avoid relying on the device name or
924 // location but for built-in input device, they are unlikely to ever change.
925 if (!identifier.name.isEmpty()) {
926 rawDescriptor.append("name:");
927 rawDescriptor.append(identifier.name);
928 } else if (!identifier.location.isEmpty()) {
929 rawDescriptor.append("location:");
930 rawDescriptor.append(identifier.location);
931 }
932 }
933 identifier.descriptor = sha1(rawDescriptor);
934
Jeff Brown90655042010-12-02 13:50:46 -0800935 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700936 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
Steve Block3762c312012-01-06 19:20:56 +0000937 ALOGE("Error %d making device file descriptor non-blocking.", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700938 close(fd);
939 return -1;
940 }
941
Jeff Brown90655042010-12-02 13:50:46 -0800942 // Allocate device. (The device object takes ownership of the fd at this point.)
943 int32_t deviceId = mNextDeviceId++;
944 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945
Jeff Browne38fdfa2012-04-06 14:51:01 -0700946 ALOGV("add device %d: %s\n", deviceId, devicePath);
947 ALOGV(" bus: %04x\n"
948 " vendor %04x\n"
949 " product %04x\n"
950 " version %04x\n",
Jeff Brown90655042010-12-02 13:50:46 -0800951 identifier.bus, identifier.vendor, identifier.product, identifier.version);
Jeff Browne38fdfa2012-04-06 14:51:01 -0700952 ALOGV(" name: \"%s\"\n", identifier.name.string());
953 ALOGV(" location: \"%s\"\n", identifier.location.string());
954 ALOGV(" unique id: \"%s\"\n", identifier.uniqueId.string());
955 ALOGV(" descriptor: \"%s\" (%s)\n", identifier.descriptor.string(), rawDescriptor.string());
956 ALOGV(" driver: v%d.%d.%d\n",
Jeff Brown90655042010-12-02 13:50:46 -0800957 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800959 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700960 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800961
Jeff Brownfd035822010-06-30 16:10:35 -0700962 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700963 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
964 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
965 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
966 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
967 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
968 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -0800969
Jeff Brown6f2fba42011-02-19 01:08:02 -0800970 // See if this is a keyboard. Ignore everything in the button range except for
971 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700972 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
973 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -0800974 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700975 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800976 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -0700977 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800978 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800979 if (haveKeyboardKeys || haveGamepadButtons) {
980 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800982
Jeff Brown83c09682010-12-23 17:50:18 -0800983 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700984 if (test_bit(BTN_MOUSE, device->keyBitmask)
985 && test_bit(REL_X, device->relBitmask)
986 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800987 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
Jeff Brownfd035822010-06-30 16:10:35 -0700989
990 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800991 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700992 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
993 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800994 // Some joysticks such as the PS3 controller report axes that conflict
995 // with the ABS_MT range. Try to confirm that the device really is
996 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700997 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800998 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd035822010-06-30 16:10:35 -0700999 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001000 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -07001001 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
1002 && test_bit(ABS_X, device->absBitmask)
1003 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001004 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
1006
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001007 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001008 // Assumes that joysticks always have gamepad buttons in order to distinguish them
1009 // from other devices such as accelerometers that also have absolute axes.
Jeff Brown9ee285a2011-08-31 12:56:34 -07001010 if (haveGamepadButtons) {
1011 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
1012 for (int i = 0; i <= ABS_MAX; i++) {
1013 if (test_bit(i, device->absBitmask)
1014 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
1015 device->classes = assumedClasses;
1016 break;
1017 }
1018 }
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001019 }
1020
Jeff Brown93fa9b32011-06-14 17:09:25 -07001021 // Check whether this device has switches.
1022 for (int i = 0; i <= SW_MAX; i++) {
1023 if (test_bit(i, device->swBitmask)) {
1024 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
1025 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028
Jeff Brown93fa9b32011-06-14 17:09:25 -07001029 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -08001030 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -08001031 // Load the virtual keys for the touch screen, if any.
1032 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001033 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -08001034 if (!status) {
1035 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
Jeff Brown90655042010-12-02 13:50:46 -08001037 }
1038
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001039 // Load the key map.
1040 // We need to do this for joysticks too because the key layout may specify axes.
1041 status_t keyMapStatus = NAME_NOT_FOUND;
1042 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -08001043 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001044 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001045 }
Jeff Brown90655042010-12-02 13:50:46 -08001046
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001047 // Configure the keyboard, gamepad or virtual keyboard.
1048 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001049 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001050 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -08001051 && mBuiltInKeyboardId == -1
1052 && isEligibleBuiltInKeyboard(device->identifier,
1053 device->configuration, &device->keyMap)) {
1054 mBuiltInKeyboardId = device->id;
Jeff Brown497a92c2010-09-12 17:55:08 -07001055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001057 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f48712010-10-01 17:46:21 -07001058 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001059 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001060 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001061
Jeff Brownfd035822010-06-30 16:10:35 -07001062 // See if this device has a DPAD.
Jeff Brownf2f48712010-10-01 17:46:21 -07001063 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1064 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1065 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1066 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1067 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001068 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001069 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001070
Jeff Brownfd035822010-06-30 16:10:35 -07001071 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001072 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f48712010-10-01 17:46:21 -07001073 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd035822010-06-30 16:10:35 -07001074 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1075 break;
1076 }
1077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079
Sean McNeilaeb00c42010-06-23 16:00:37 +07001080 // If the device isn't recognized as something we handle, don't monitor it.
1081 if (device->classes == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001082 ALOGV("Dropping device: id=%d, path='%s', name='%s'",
Jeff Brown90655042010-12-02 13:50:46 -08001083 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001084 delete device;
1085 return -1;
1086 }
1087
Jeff Brown56194eb2011-03-02 19:23:13 -08001088 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001089 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001090 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1091 }
1092
Jeff Brown93fa9b32011-06-14 17:09:25 -07001093 // Register with epoll.
1094 struct epoll_event eventItem;
1095 memset(&eventItem, 0, sizeof(eventItem));
1096 eventItem.events = EPOLLIN;
1097 eventItem.data.u32 = deviceId;
1098 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
Steve Block3762c312012-01-06 19:20:56 +00001099 ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001100 delete device;
1101 return -1;
1102 }
1103
Jeff Browne22afbe2011-12-16 13:45:40 -08001104 // Enable wake-lock behavior on kernels that support it.
1105 // TODO: Only need this for devices that can really wake the system.
1106 bool usingSuspendBlock = ioctl(fd, EVIOCSSUSPENDBLOCK, 1) == 0;
1107
Steve Block6215d3f2012-01-04 20:05:49 +00001108 ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
Jeff Browne22afbe2011-12-16 13:45:40 -08001109 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
1110 "usingSuspendBlock=%s",
Jeff Brown90655042010-12-02 13:50:46 -08001111 deviceId, fd, devicePath, device->identifier.name.string(),
1112 device->classes,
1113 device->configurationFile.string(),
1114 device->keyMap.keyLayoutFile.string(),
1115 device->keyMap.keyCharacterMapFile.string(),
Jeff Browne22afbe2011-12-16 13:45:40 -08001116 toString(mBuiltInKeyboardId == deviceId),
1117 toString(usingSuspendBlock));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001118
Jeff Brown93fa9b32011-06-14 17:09:25 -07001119 mDevices.add(deviceId, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 device->next = mOpeningDevices;
1122 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 return 0;
1124}
1125
Jeff Brown93fa9b32011-06-14 17:09:25 -07001126void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001127 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1128 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001129 if (device->configurationFile.isEmpty()) {
Steve Block5baa3a62011-12-20 16:23:08 +00001130 ALOGD("No input device configuration file found for device '%s'.",
Jeff Brown90655042010-12-02 13:50:46 -08001131 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001132 } else {
1133 status_t status = PropertyMap::load(device->configurationFile,
1134 &device->configuration);
1135 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001136 ALOGE("Error loading input device configuration file for device '%s'. "
Jeff Brown90655042010-12-02 13:50:46 -08001137 "Using default configuration.",
1138 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001139 }
1140 }
1141}
1142
Jeff Brown93fa9b32011-06-14 17:09:25 -07001143status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001144 // The virtual key map is supplied by the kernel as a system board property file.
1145 String8 path;
1146 path.append("/sys/board_properties/virtualkeys.");
1147 path.append(device->identifier.name);
1148 if (access(path.string(), R_OK)) {
1149 return NAME_NOT_FOUND;
1150 }
1151 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001152}
1153
Jeff Brown93fa9b32011-06-14 17:09:25 -07001154status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001155 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001156}
1157
Jeff Brown93fa9b32011-06-14 17:09:25 -07001158bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001159 if (device->configuration) {
1160 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001161 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1162 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001163 }
1164 }
1165 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1166}
1167
Jeff Brown90655042010-12-02 13:50:46 -08001168bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1169 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001170 return false;
1171 }
1172
1173 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001174 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001175 const size_t N = scanCodes.size();
1176 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1177 int32_t sc = scanCodes.itemAt(i);
1178 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1179 return true;
1180 }
1181 }
1182
1183 return false;
1184}
1185
Jeff Brown93fa9b32011-06-14 17:09:25 -07001186status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1187 Device* device = getDeviceByPathLocked(devicePath);
1188 if (device) {
1189 closeDeviceLocked(device);
1190 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
Steve Block71f2cf12011-10-20 11:56:00 +01001192 ALOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 return -1;
1194}
1195
Jeff Brown93fa9b32011-06-14 17:09:25 -07001196void EventHub::closeAllDevicesLocked() {
1197 while (mDevices.size() > 0) {
1198 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1199 }
1200}
1201
1202void EventHub::closeDeviceLocked(Device* device) {
Steve Block6215d3f2012-01-04 20:05:49 +00001203 ALOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001204 device->path.string(), device->identifier.name.string(), device->id,
1205 device->fd, device->classes);
1206
Jeff Brown33bbfd22011-02-24 20:55:35 -08001207 if (device->id == mBuiltInKeyboardId) {
Steve Block8564c8d2012-01-05 23:22:43 +00001208 ALOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown33bbfd22011-02-24 20:55:35 -08001209 device->path.string(), mBuiltInKeyboardId);
1210 mBuiltInKeyboardId = -1;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001211 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001212
Jeff Brown93fa9b32011-06-14 17:09:25 -07001213 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001214 ALOGW("Could not remove device fd from epoll instance. errno=%d", errno);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001215 }
1216
1217 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001218 device->close();
1219
Jeff Brown8e9d4432011-03-12 19:46:59 -08001220 // Unlink for opening devices list if it is present.
1221 Device* pred = NULL;
1222 bool found = false;
1223 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1224 if (entry == device) {
1225 found = true;
1226 break;
1227 }
1228 pred = entry;
1229 entry = entry->next;
1230 }
1231 if (found) {
1232 // Unlink the device from the opening devices list then delete it.
1233 // We don't need to tell the client that the device was closed because
1234 // it does not even know it was opened in the first place.
Steve Block6215d3f2012-01-04 20:05:49 +00001235 ALOGI("Device %s was immediately closed after opening.", device->path.string());
Jeff Brown8e9d4432011-03-12 19:46:59 -08001236 if (pred) {
1237 pred->next = device->next;
1238 } else {
1239 mOpeningDevices = device->next;
1240 }
1241 delete device;
1242 } else {
1243 // Link into closing devices list.
1244 // The device will be deleted later after we have informed the client.
1245 device->next = mClosingDevices;
1246 mClosingDevices = device;
1247 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001248}
1249
Jeff Brown93fa9b32011-06-14 17:09:25 -07001250status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 int res;
1252 char devname[PATH_MAX];
1253 char *filename;
1254 char event_buf[512];
1255 int event_size;
1256 int event_pos = 0;
1257 struct inotify_event *event;
1258
Steve Block71f2cf12011-10-20 11:56:00 +01001259 ALOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001260 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 if(res < (int)sizeof(*event)) {
1262 if(errno == EINTR)
1263 return 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001264 ALOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001265 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267 //printf("got %d bytes of event information\n", res);
1268
Jeff Brown90655042010-12-02 13:50:46 -08001269 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 filename = devname + strlen(devname);
1271 *filename++ = '/';
1272
1273 while(res >= (int)sizeof(*event)) {
1274 event = (struct inotify_event *)(event_buf + event_pos);
1275 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1276 if(event->len) {
1277 strcpy(filename, event->name);
1278 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001279 openDeviceLocked(devname);
1280 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00001281 ALOGI("Removing device '%s' due to inotify event\n", devname);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001282 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 }
1284 }
1285 event_size = sizeof(*event) + event->len;
1286 res -= event_size;
1287 event_pos += event_size;
1288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 return 0;
1290}
1291
Jeff Brown93fa9b32011-06-14 17:09:25 -07001292status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293{
1294 char devname[PATH_MAX];
1295 char *filename;
1296 DIR *dir;
1297 struct dirent *de;
1298 dir = opendir(dirname);
1299 if(dir == NULL)
1300 return -1;
1301 strcpy(devname, dirname);
1302 filename = devname + strlen(devname);
1303 *filename++ = '/';
1304 while((de = readdir(dir))) {
1305 if(de->d_name[0] == '.' &&
1306 (de->d_name[1] == '\0' ||
1307 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1308 continue;
1309 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001310 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312 closedir(dir);
1313 return 0;
1314}
1315
Jeff Brown93fa9b32011-06-14 17:09:25 -07001316void EventHub::requestReopenDevices() {
Steve Block71f2cf12011-10-20 11:56:00 +01001317 ALOGV("requestReopenDevices() called");
Jeff Brown93fa9b32011-06-14 17:09:25 -07001318
1319 AutoMutex _l(mLock);
1320 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001321}
1322
Jeff Brownf2f48712010-10-01 17:46:21 -07001323void EventHub::dump(String8& dump) {
1324 dump.append("Event Hub State:\n");
1325
1326 { // acquire lock
1327 AutoMutex _l(mLock);
1328
Jeff Brown90655042010-12-02 13:50:46 -08001329 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f48712010-10-01 17:46:21 -07001330
1331 dump.append(INDENT "Devices:\n");
1332
Jeff Brown93fa9b32011-06-14 17:09:25 -07001333 for (size_t i = 0; i < mDevices.size(); i++) {
1334 const Device* device = mDevices.valueAt(i);
1335 if (mBuiltInKeyboardId == device->id) {
1336 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1337 device->id, device->identifier.name.string());
1338 } else {
1339 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1340 device->identifier.name.string());
Jeff Brownf2f48712010-10-01 17:46:21 -07001341 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001342 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1343 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Browne38fdfa2012-04-06 14:51:01 -07001344 dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string());
Jeff Brown93fa9b32011-06-14 17:09:25 -07001345 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1346 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1347 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1348 "product=0x%04x, version=0x%04x\n",
1349 device->identifier.bus, device->identifier.vendor,
1350 device->identifier.product, device->identifier.version);
1351 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1352 device->keyMap.keyLayoutFile.string());
1353 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1354 device->keyMap.keyCharacterMapFile.string());
1355 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1356 device->configurationFile.string());
Jeff Brownf2f48712010-10-01 17:46:21 -07001357 }
1358 } // release lock
1359}
1360
Jeff Brown89ef0722011-08-10 16:25:21 -07001361void EventHub::monitor() {
1362 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1363 mLock.lock();
1364 mLock.unlock();
1365}
1366
1367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368}; // namespace android