blob: 157a9975fdc5088002247ac9aeb435a339459300 [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 Brownb7198742011-03-18 18:14:26 -0700159 mNumCpus = sysconf(_SC_NPROCESSORS_ONLN);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700160
161 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
162 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
163
164 mINotifyFd = inotify_init();
165 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
166 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
167 DEVICE_PATH, errno);
168
169 struct epoll_event eventItem;
170 memset(&eventItem, 0, sizeof(eventItem));
171 eventItem.events = EPOLLIN;
172 eventItem.data.u32 = EPOLL_ID_INOTIFY;
173 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
174 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
175
176 int wakeFds[2];
177 result = pipe(wakeFds);
178 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
179
180 mWakeReadPipeFd = wakeFds[0];
181 mWakeWritePipeFd = wakeFds[1];
182
183 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
184 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
185 errno);
186
187 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
188 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
189 errno);
190
191 eventItem.data.u32 = EPOLL_ID_WAKE;
192 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
193 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
194 errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195}
196
Jeff Brown90655042010-12-02 13:50:46 -0800197EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700198 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
Jeff Brown93fa9b32011-06-14 17:09:25 -0700200 while (mClosingDevices) {
201 Device* device = mClosingDevices;
202 mClosingDevices = device->next;
203 delete device;
204 }
205
206 ::close(mEpollFd);
207 ::close(mINotifyFd);
208 ::close(mWakeReadPipeFd);
209 ::close(mWakeWritePipeFd);
210
211 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212}
213
Jeff Brown90655042010-12-02 13:50:46 -0800214String8 EventHub::getDeviceName(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800216 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 if (device == NULL) return String8();
Jeff Brown90655042010-12-02 13:50:46 -0800218 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219}
220
Jeff Brown90655042010-12-02 13:50:46 -0800221uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800223 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 if (device == NULL) return 0;
225 return device->classes;
226}
227
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800228void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800229 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800230 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800231 if (device && device->configuration) {
232 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800233 } else {
234 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800235 }
236}
237
Jeff Brown6d0fec22010-07-23 21:28:06 -0700238status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
239 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700240 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700241
Jeff Brownba421dd2011-08-10 15:07:05 -0700242 if (axis >= 0 && axis <= ABS_MAX) {
243 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
Jeff Brownba421dd2011-08-10 15:07:05 -0700245 Device* device = getDeviceLocked(deviceId);
246 if (device && test_bit(axis, device->absBitmask)) {
247 struct input_absinfo info;
248 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
249 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
250 axis, device->identifier.name.string(), device->fd, errno);
251 return -errno;
252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Jeff Brownba421dd2011-08-10 15:07:05 -0700254 if (info.minimum != info.maximum) {
255 outAxisInfo->valid = true;
256 outAxisInfo->minValue = info.minimum;
257 outAxisInfo->maxValue = info.maximum;
258 outAxisInfo->flat = info.flat;
259 outAxisInfo->fuzz = info.fuzz;
260 outAxisInfo->resolution = info.resolution;
261 }
262 return OK;
263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700265 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266}
267
Jeff Browncc0c1592011-02-19 05:07:28 -0800268bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
269 if (axis >= 0 && axis <= REL_MAX) {
270 AutoMutex _l(mLock);
271
272 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700273 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800274 return test_bit(axis, device->relBitmask);
275 }
276 }
277 return false;
278}
279
Jeff Brown80fd47c2011-05-24 01:07:44 -0700280bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
281 if (property >= 0 && property <= INPUT_PROP_MAX) {
282 AutoMutex _l(mLock);
283
284 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700285 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700286 return test_bit(property, device->propBitmask);
287 }
288 }
289 return false;
290}
291
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700293 if (scanCode >= 0 && scanCode <= KEY_MAX) {
294 AutoMutex _l(mLock);
295
Jeff Brown90655042010-12-02 13:50:46 -0800296 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700297 if (device && test_bit(scanCode, device->keyBitmask)) {
298 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
299 memset(keyState, 0, sizeof(keyState));
300 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
301 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700305 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306}
307
Jeff Brown6d0fec22010-07-23 21:28:06 -0700308int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
309 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700310
Jeff Brown90655042010-12-02 13:50:46 -0800311 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700312 if (device && device->keyMap.haveKeyLayout()) {
313 Vector<int32_t> scanCodes;
314 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
315 if (scanCodes.size() != 0) {
316 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
317 memset(keyState, 0, sizeof(keyState));
318 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
319 for (size_t i = 0; i < scanCodes.size(); i++) {
320 int32_t sc = scanCodes.itemAt(i);
321 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
322 return AKEY_STATE_DOWN;
323 }
324 }
325 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327 }
328 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700329 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700330}
331
Jeff Brown6d0fec22010-07-23 21:28:06 -0700332int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700333 if (sw >= 0 && sw <= SW_MAX) {
334 AutoMutex _l(mLock);
335
Jeff Brown90655042010-12-02 13:50:46 -0800336 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700337 if (device && test_bit(sw, device->swBitmask)) {
338 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
339 memset(swState, 0, sizeof(swState));
340 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
341 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
342 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700343 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700344 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700345 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700346}
347
Jeff Brown2717eff2011-06-30 23:53:07 -0700348status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
Jeff Brown06309752011-08-11 17:10:06 -0700349 *outValue = 0;
350
Jeff Brown2717eff2011-06-30 23:53:07 -0700351 if (axis >= 0 && axis <= ABS_MAX) {
352 AutoMutex _l(mLock);
353
354 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700355 if (device && test_bit(axis, device->absBitmask)) {
356 struct input_absinfo info;
357 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
358 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
359 axis, device->identifier.name.string(), device->fd, errno);
360 return -errno;
361 }
362
363 *outValue = info.value;
364 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700365 }
366 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700367 return -1;
368}
369
Jeff Brown6d0fec22010-07-23 21:28:06 -0700370bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
371 const int32_t* keyCodes, uint8_t* outFlags) const {
372 AutoMutex _l(mLock);
373
Jeff Brown90655042010-12-02 13:50:46 -0800374 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700375 if (device && device->keyMap.haveKeyLayout()) {
376 Vector<int32_t> scanCodes;
377 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
378 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700379
Jeff Brownba421dd2011-08-10 15:07:05 -0700380 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
381 keyCodes[codeIndex], &scanCodes);
382 if (! err) {
383 // check the possible scan codes identified by the layout map against the
384 // map of codes actually emitted by the driver
385 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
386 if (test_bit(scanCodes[sc], device->keyBitmask)) {
387 outFlags[codeIndex] = 1;
388 break;
389 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700390 }
391 }
392 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700393 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700394 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700395 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700396}
397
Jeff Brown6f2fba42011-02-19 01:08:02 -0800398status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700399 int32_t* outKeycode, uint32_t* outFlags) const
400{
401 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800402 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700403
Jeff Brown90655042010-12-02 13:50:46 -0800404 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800405 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700406 if (err == NO_ERROR) {
407 return NO_ERROR;
408 }
409 }
410
Jeff Brown90655042010-12-02 13:50:46 -0800411 if (mBuiltInKeyboardId != -1) {
412 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700413
Jeff Brown90655042010-12-02 13:50:46 -0800414 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800415 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700416 if (err == NO_ERROR) {
417 return NO_ERROR;
418 }
419 }
420 }
421
422 *outKeycode = 0;
423 *outFlags = 0;
424 return NAME_NOT_FOUND;
425}
426
Jeff Brown85297452011-03-04 13:07:49 -0800427status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800428{
429 AutoMutex _l(mLock);
430 Device* device = getDeviceLocked(deviceId);
431
432 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800433 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800434 if (err == NO_ERROR) {
435 return NO_ERROR;
436 }
437 }
438
439 if (mBuiltInKeyboardId != -1) {
440 device = getDeviceLocked(mBuiltInKeyboardId);
441
442 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800443 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800444 if (err == NO_ERROR) {
445 return NO_ERROR;
446 }
447 }
448 }
449
Jeff Brown6f2fba42011-02-19 01:08:02 -0800450 return NAME_NOT_FOUND;
451}
452
Jeff Brown1a84fd12011-06-02 01:26:32 -0700453void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700454 AutoMutex _l(mLock);
455
Jeff Brown1a84fd12011-06-02 01:26:32 -0700456 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400457}
458
Jeff Brown49754db2011-07-01 17:37:58 -0700459bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
460 AutoMutex _l(mLock);
461 Device* device = getDeviceLocked(deviceId);
462 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
463 if (test_bit(scanCode, device->keyBitmask)) {
464 return true;
465 }
466 }
467 return false;
468}
469
Jeff Brown497a92c2010-09-12 17:55:08 -0700470bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
471 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800472 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700473 if (device && led >= 0 && led <= LED_MAX) {
474 if (test_bit(led, device->ledBitmask)) {
475 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700476 }
477 }
478 return false;
479}
480
481void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
482 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800483 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700484 if (device && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700485 struct input_event ev;
486 ev.time.tv_sec = 0;
487 ev.time.tv_usec = 0;
488 ev.type = EV_LED;
489 ev.code = led;
490 ev.value = on ? 1 : 0;
491
492 ssize_t nWrite;
493 do {
494 nWrite = write(device->fd, &ev, sizeof(struct input_event));
495 } while (nWrite == -1 && errno == EINTR);
496 }
497}
498
Jeff Brown90655042010-12-02 13:50:46 -0800499void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
500 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
501 outVirtualKeys.clear();
502
503 AutoMutex _l(mLock);
504 Device* device = getDeviceLocked(deviceId);
505 if (device && device->virtualKeyMap) {
506 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
507 }
508}
509
510EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
511 if (deviceId == 0) {
512 deviceId = mBuiltInKeyboardId;
513 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700514 ssize_t index = mDevices.indexOfKey(deviceId);
515 return index >= 0 ? mDevices.valueAt(index) : NULL;
516}
Jeff Brown90655042010-12-02 13:50:46 -0800517
Jeff Brown93fa9b32011-06-14 17:09:25 -0700518EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
519 for (size_t i = 0; i < mDevices.size(); i++) {
520 Device* device = mDevices.valueAt(i);
521 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800522 return device;
523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 return NULL;
526}
527
Jeff Brownb7198742011-03-18 18:14:26 -0700528size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Jeff Brownb6110c22011-04-01 16:15:13 -0700529 LOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400530
Jeff Brown93fa9b32011-06-14 17:09:25 -0700531 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400532
Jeff Brownb7198742011-03-18 18:14:26 -0700533 struct input_event readBuffer[bufferSize];
534
535 RawEvent* event = buffer;
536 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700537 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700538 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700539 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
540
Jeff Brown1a84fd12011-06-02 01:26:32 -0700541 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700542 if (mNeedToReopenDevices) {
543 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700544
545 LOGI("Reopening all input devices due to a configuration change.");
546
Jeff Brown93fa9b32011-06-14 17:09:25 -0700547 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700548 mNeedToScanDevices = true;
549 break; // return to the caller before we actually rescan
550 }
551
Jeff Browncc2e7172010-08-17 16:48:25 -0700552 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700553 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800554 Device* device = mClosingDevices;
555 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 device->id, device->path.string());
557 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700558 event->when = now;
559 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
560 event->type = DEVICE_REMOVED;
561 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700563 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700564 if (--capacity == 0) {
565 break;
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700568
Jeff Brown1a84fd12011-06-02 01:26:32 -0700569 if (mNeedToScanDevices) {
570 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700571 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700572 mNeedToSendFinishedDeviceScan = true;
573 }
574
Jeff Brownb7198742011-03-18 18:14:26 -0700575 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800576 Device* device = mOpeningDevices;
577 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 device->id, device->path.string());
579 mOpeningDevices = 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_ADDED;
583 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700584 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700585 if (--capacity == 0) {
586 break;
587 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700588 }
589
590 if (mNeedToSendFinishedDeviceScan) {
591 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700592 event->when = now;
593 event->type = FINISHED_DEVICE_SCAN;
594 event += 1;
595 if (--capacity == 0) {
596 break;
597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 }
599
Jeff Browncc2e7172010-08-17 16:48:25 -0700600 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700601 bool deviceChanged = false;
602 while (mPendingEventIndex < mPendingEventCount) {
603 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
604 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
605 if (eventItem.events & EPOLLIN) {
606 mPendingINotify = true;
607 } else {
608 LOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
609 }
610 continue;
611 }
612
613 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
614 if (eventItem.events & EPOLLIN) {
615 LOGV("awoken after wake()");
616 awoken = true;
617 char buffer[16];
618 ssize_t nRead;
619 do {
620 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
621 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
622 } else {
623 LOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
624 eventItem.events);
625 }
626 continue;
627 }
628
629 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
630 if (deviceIndex < 0) {
631 LOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
632 eventItem.events, eventItem.data.u32);
633 continue;
634 }
635
636 Device* device = mDevices.valueAt(deviceIndex);
637 if (eventItem.events & EPOLLIN) {
638 int32_t readSize = read(device->fd, readBuffer,
639 sizeof(struct input_event) * capacity);
640 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
641 // Device was removed before INotify noticed.
642 deviceChanged = true;
643 closeDeviceLocked(device);
644 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700645 if (errno != EAGAIN && errno != EINTR) {
646 LOGW("could not get event (errno=%d)", errno);
647 }
648 } else if ((readSize % sizeof(struct input_event)) != 0) {
649 LOGE("could not get event (wrong size: %d)", readSize);
650 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700651 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
652
653 size_t count = size_t(readSize) / sizeof(struct input_event);
654 for (size_t i = 0; i < count; i++) {
655 const struct input_event& iev = readBuffer[i];
656 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
657 device->path.string(),
658 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
659 iev.type, iev.code, iev.value);
660
Jeff Brown4e91a182011-04-07 11:38:09 -0700661#ifdef HAVE_POSIX_CLOCKS
662 // Use the time specified in the event instead of the current time
663 // so that downstream code can get more accurate estimates of
664 // event dispatch latency from the time the event is enqueued onto
665 // the evdev client buffer.
666 //
667 // The event's timestamp fortuitously uses the same monotonic clock
668 // time base as the rest of Android. The kernel event device driver
669 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
670 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
671 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
672 // system call that also queries ktime_get_ts().
673 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
674 + nsecs_t(iev.time.tv_usec) * 1000LL;
675 LOGV("event time %lld, now %lld", event->when, now);
676#else
Jeff Brownb7198742011-03-18 18:14:26 -0700677 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700678#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700679 event->deviceId = deviceId;
680 event->type = iev.type;
681 event->scanCode = iev.code;
682 event->value = iev.value;
683 event->keyCode = AKEYCODE_UNKNOWN;
684 event->flags = 0;
685 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
686 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
687 &event->keyCode, &event->flags);
688 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
689 iev.code, event->keyCode, event->flags, err);
690 }
691 event += 1;
692 }
693 capacity -= count;
694 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700695 // The result buffer is full. Reset the pending event index
696 // so we will try to read the device again on the next iteration.
697 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700698 break;
699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700701 } else {
702 LOGW("Received unexpected epoll event 0x%08x for device %s.",
703 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700706
Jeff Brown93fa9b32011-06-14 17:09:25 -0700707 // readNotify() will modify the list of devices so this must be done after
708 // processing all other events to ensure that we read all remaining events
709 // before closing the devices.
710 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
711 mPendingINotify = false;
712 readNotifyLocked();
713 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800714 }
715
Jeff Brown93fa9b32011-06-14 17:09:25 -0700716 // Report added or removed devices immediately.
717 if (deviceChanged) {
718 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
Jeff Browna9b84222010-10-14 02:23:43 -0700720
Jeff Brown93fa9b32011-06-14 17:09:25 -0700721 // Return now if we have collected any events or if we were explicitly awoken.
722 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700723 break;
724 }
725
Jeff Browncc2e7172010-08-17 16:48:25 -0700726 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700727 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700728 // subtle choreography. When a device driver has pending (unread) events, it acquires
729 // a kernel wake lock. However, once the last pending event has been read, the device
730 // driver will release the kernel wake lock. To prevent the system from going to sleep
731 // when this happens, the EventHub holds onto its own user wake lock while the client
732 // is processing events. Thus the system can only sleep if there are no events
733 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700734 //
735 // The timeout is advisory only. If the device is asleep, it will not wake just to
736 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700737 mPendingEventIndex = 0;
738
739 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700740 release_wake_lock(WAKE_LOCK_ID);
741
Jeff Brown93fa9b32011-06-14 17:09:25 -0700742 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700743
744 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700745 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700746
Jeff Brownaa3855d2011-03-17 01:34:19 -0700747 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700748 // Timed out.
749 mPendingEventCount = 0;
750 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700751 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700752
Jeff Brownaa3855d2011-03-17 01:34:19 -0700753 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700754 // An error occurred.
755 mPendingEventCount = 0;
756
Jeff Brownb7198742011-03-18 18:14:26 -0700757 // Sleep after errors to avoid locking up the system.
758 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700759 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700760 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700761 usleep(100000);
762 }
Jeff Brownb7198742011-03-18 18:14:26 -0700763 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700764 // Some events occurred.
765 mPendingEventCount = size_t(pollResult);
766
Jeff Brownb7198742011-03-18 18:14:26 -0700767 // On an SMP system, it is possible for the framework to read input events
768 // faster than the kernel input device driver can produce a complete packet.
769 // Because poll() wakes up as soon as the first input event becomes available,
770 // the framework will often end up reading one event at a time until the
771 // packet is complete. Instead of one call to read() returning 71 events,
772 // it could take 71 calls to read() each returning 1 event.
773 //
774 // Sleep for a short period of time after waking up from the poll() to give
775 // the kernel time to finish writing the entire packet of input events.
776 if (mNumCpus > 1) {
777 usleep(250);
778 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
Jeff Brownb7198742011-03-18 18:14:26 -0700781
782 // All done, return the number of events we read.
783 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784}
785
Jeff Brown93fa9b32011-06-14 17:09:25 -0700786void EventHub::wake() {
787 LOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
Jeff Brown93fa9b32011-06-14 17:09:25 -0700789 ssize_t nWrite;
790 do {
791 nWrite = write(mWakeWritePipeFd, "W", 1);
792 } while (nWrite == -1 && errno == EINTR);
793
794 if (nWrite != 1 && errno != EAGAIN) {
795 LOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700797}
Jeff Brown90655042010-12-02 13:50:46 -0800798
Jeff Brown93fa9b32011-06-14 17:09:25 -0700799void EventHub::scanDevicesLocked() {
800 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800802 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804}
805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806// ----------------------------------------------------------------------------
807
Jeff Brownfd035822010-06-30 16:10:35 -0700808static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
809 const uint8_t* end = array + endIndex;
810 array += startIndex;
811 while (array != end) {
812 if (*(array++) != 0) {
813 return true;
814 }
815 }
816 return false;
817}
818
819static const int32_t GAMEPAD_KEYCODES[] = {
820 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
821 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
822 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
823 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
824 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800825 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
826 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
827 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
828 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
829 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd035822010-06-30 16:10:35 -0700830};
831
Jeff Brown93fa9b32011-06-14 17:09:25 -0700832status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800833 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834
Jeff Brown90655042010-12-02 13:50:46 -0800835 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
Jeff Brown90655042010-12-02 13:50:46 -0800837 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800839 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 return -1;
841 }
842
Jeff Brown90655042010-12-02 13:50:46 -0800843 InputDeviceIdentifier identifier;
844
845 // Get device name.
846 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
847 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
848 } else {
849 buffer[sizeof(buffer) - 1] = '\0';
850 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400852
Jeff Brown90655042010-12-02 13:50:46 -0800853 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700854 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
855 const String8& item = mExcludedDevices.itemAt(i);
856 if (identifier.name == item) {
857 LOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400858 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400859 return -1;
860 }
861 }
862
Jeff Brown90655042010-12-02 13:50:46 -0800863 // Get device driver version.
864 int driverVersion;
865 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
866 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
867 close(fd);
868 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870
Jeff Brown90655042010-12-02 13:50:46 -0800871 // Get device identifier.
872 struct input_id inputId;
873 if(ioctl(fd, EVIOCGID, &inputId)) {
874 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
875 close(fd);
876 return -1;
877 }
878 identifier.bus = inputId.bustype;
879 identifier.product = inputId.product;
880 identifier.vendor = inputId.vendor;
881 identifier.version = inputId.version;
882
883 // Get device physical location.
884 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
885 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
886 } else {
887 buffer[sizeof(buffer) - 1] = '\0';
888 identifier.location.setTo(buffer);
889 }
890
891 // Get device unique id.
892 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
893 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
894 } else {
895 buffer[sizeof(buffer) - 1] = '\0';
896 identifier.uniqueId.setTo(buffer);
897 }
898
899 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700900 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
901 LOGE("Error %d making device file descriptor non-blocking.", errno);
902 close(fd);
903 return -1;
904 }
905
Jeff Brown90655042010-12-02 13:50:46 -0800906 // Allocate device. (The device object takes ownership of the fd at this point.)
907 int32_t deviceId = mNextDeviceId++;
908 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
910#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800911 LOGI("add device %d: %s\n", deviceId, devicePath);
912 LOGI(" bus: %04x\n"
913 " vendor %04x\n"
914 " product %04x\n"
915 " version %04x\n",
916 identifier.bus, identifier.vendor, identifier.product, identifier.version);
917 LOGI(" name: \"%s\"\n", identifier.name.string());
918 LOGI(" location: \"%s\"\n", identifier.location.string());
919 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
920 LOGI(" driver: v%d.%d.%d\n",
921 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922#endif
923
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800924 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700925 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800926
Jeff Brownfd035822010-06-30 16:10:35 -0700927 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700928 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
929 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
930 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
931 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
932 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
933 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -0800934
Jeff Brown6f2fba42011-02-19 01:08:02 -0800935 // See if this is a keyboard. Ignore everything in the button range except for
936 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700937 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
938 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -0800939 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700940 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800941 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -0700942 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800943 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800944 if (haveKeyboardKeys || haveGamepadButtons) {
945 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800947
Jeff Brown83c09682010-12-23 17:50:18 -0800948 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700949 if (test_bit(BTN_MOUSE, device->keyBitmask)
950 && test_bit(REL_X, device->relBitmask)
951 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800952 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
Jeff Brownfd035822010-06-30 16:10:35 -0700954
955 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800956 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700957 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
958 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800959 // Some joysticks such as the PS3 controller report axes that conflict
960 // with the ABS_MT range. Try to confirm that the device really is
961 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700962 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800963 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd035822010-06-30 16:10:35 -0700964 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800965 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700966 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
967 && test_bit(ABS_X, device->absBitmask)
968 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800969 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800972 // See if this device is a joystick.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800973 // Assumes that joysticks always have gamepad buttons in order to distinguish them
974 // from other devices such as accelerometers that also have absolute axes.
Jeff Brown9ee285a2011-08-31 12:56:34 -0700975 if (haveGamepadButtons) {
976 uint32_t assumedClasses = device->classes | INPUT_DEVICE_CLASS_JOYSTICK;
977 for (int i = 0; i <= ABS_MAX; i++) {
978 if (test_bit(i, device->absBitmask)
979 && (getAbsAxisUsage(i, assumedClasses) & INPUT_DEVICE_CLASS_JOYSTICK)) {
980 device->classes = assumedClasses;
981 break;
982 }
983 }
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800984 }
985
Jeff Brown93fa9b32011-06-14 17:09:25 -0700986 // Check whether this device has switches.
987 for (int i = 0; i <= SW_MAX; i++) {
988 if (test_bit(i, device->swBitmask)) {
989 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
990 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993
Jeff Brown93fa9b32011-06-14 17:09:25 -0700994 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -0800995 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800996 // Load the virtual keys for the touch screen, if any.
997 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700998 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -0800999 if (!status) {
1000 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
Jeff Brown90655042010-12-02 13:50:46 -08001002 }
1003
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001004 // Load the key map.
1005 // We need to do this for joysticks too because the key layout may specify axes.
1006 status_t keyMapStatus = NAME_NOT_FOUND;
1007 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -08001008 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001009 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001010 }
Jeff Brown90655042010-12-02 13:50:46 -08001011
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001012 // Configure the keyboard, gamepad or virtual keyboard.
1013 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001014 // Set system properties for the keyboard.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001015 setKeyboardPropertiesLocked(device, false);
Jeff Brown497a92c2010-09-12 17:55:08 -07001016
Jeff Brown90655042010-12-02 13:50:46 -08001017 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001018 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -08001019 && mBuiltInKeyboardId == -1
1020 && isEligibleBuiltInKeyboard(device->identifier,
1021 device->configuration, &device->keyMap)) {
1022 mBuiltInKeyboardId = device->id;
Jeff Brown93fa9b32011-06-14 17:09:25 -07001023 setKeyboardPropertiesLocked(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -07001024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001026 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f48712010-10-01 17:46:21 -07001027 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001028 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001029 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001030
Jeff Brownfd035822010-06-30 16:10:35 -07001031 // See if this device has a DPAD.
Jeff Brownf2f48712010-10-01 17:46:21 -07001032 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1033 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1034 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1035 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1036 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001037 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001038 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001039
Jeff Brownfd035822010-06-30 16:10:35 -07001040 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001041 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f48712010-10-01 17:46:21 -07001042 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd035822010-06-30 16:10:35 -07001043 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1044 break;
1045 }
1046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048
Sean McNeilaeb00c42010-06-23 16:00:37 +07001049 // If the device isn't recognized as something we handle, don't monitor it.
1050 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -08001051 LOGV("Dropping device: id=%d, path='%s', name='%s'",
1052 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001053 delete device;
1054 return -1;
1055 }
1056
Jeff Brown56194eb2011-03-02 19:23:13 -08001057 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001058 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001059 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1060 }
1061
Jeff Brown93fa9b32011-06-14 17:09:25 -07001062 // Register with epoll.
1063 struct epoll_event eventItem;
1064 memset(&eventItem, 0, sizeof(eventItem));
1065 eventItem.events = EPOLLIN;
1066 eventItem.data.u32 = deviceId;
1067 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1068 LOGE("Could not add device fd to epoll instance. errno=%d", errno);
1069 delete device;
1070 return -1;
1071 }
1072
Jeff Brown90655042010-12-02 13:50:46 -08001073 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1074 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
1075 deviceId, fd, devicePath, device->identifier.name.string(),
1076 device->classes,
1077 device->configurationFile.string(),
1078 device->keyMap.keyLayoutFile.string(),
1079 device->keyMap.keyCharacterMapFile.string(),
1080 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001081
Jeff Brown93fa9b32011-06-14 17:09:25 -07001082 mDevices.add(deviceId, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 device->next = mOpeningDevices;
1085 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 return 0;
1087}
1088
Jeff Brown93fa9b32011-06-14 17:09:25 -07001089void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001090 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1091 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001092 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -08001093 LOGD("No input device configuration file found for device '%s'.",
1094 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001095 } else {
1096 status_t status = PropertyMap::load(device->configurationFile,
1097 &device->configuration);
1098 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -08001099 LOGE("Error loading input device configuration file for device '%s'. "
1100 "Using default configuration.",
1101 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001102 }
1103 }
1104}
1105
Jeff Brown93fa9b32011-06-14 17:09:25 -07001106status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001107 // The virtual key map is supplied by the kernel as a system board property file.
1108 String8 path;
1109 path.append("/sys/board_properties/virtualkeys.");
1110 path.append(device->identifier.name);
1111 if (access(path.string(), R_OK)) {
1112 return NAME_NOT_FOUND;
1113 }
1114 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001115}
1116
Jeff Brown93fa9b32011-06-14 17:09:25 -07001117status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001118 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001119}
1120
Jeff Brown93fa9b32011-06-14 17:09:25 -07001121void EventHub::setKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001122 int32_t id = builtInKeyboard ? 0 : device->id;
1123 android::setKeyboardProperties(id, device->identifier,
1124 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1125}
1126
Jeff Brown93fa9b32011-06-14 17:09:25 -07001127void EventHub::clearKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001128 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001129 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -07001130}
1131
Jeff Brown93fa9b32011-06-14 17:09:25 -07001132bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001133 if (device->configuration) {
1134 bool value;
1135 if (device->configuration->tryGetProperty(String8("device.internal"), value)
1136 && value) {
1137 return false;
1138 }
1139 }
1140 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1141}
1142
Jeff Brown90655042010-12-02 13:50:46 -08001143bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1144 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001145 return false;
1146 }
1147
1148 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001149 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001150 const size_t N = scanCodes.size();
1151 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1152 int32_t sc = scanCodes.itemAt(i);
1153 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1154 return true;
1155 }
1156 }
1157
1158 return false;
1159}
1160
Jeff Brown93fa9b32011-06-14 17:09:25 -07001161status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1162 Device* device = getDeviceByPathLocked(devicePath);
1163 if (device) {
1164 closeDeviceLocked(device);
1165 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001167 LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 return -1;
1169}
1170
Jeff Brown93fa9b32011-06-14 17:09:25 -07001171void EventHub::closeAllDevicesLocked() {
1172 while (mDevices.size() > 0) {
1173 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1174 }
1175}
1176
1177void EventHub::closeDeviceLocked(Device* device) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001178 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1179 device->path.string(), device->identifier.name.string(), device->id,
1180 device->fd, device->classes);
1181
Jeff Brown33bbfd22011-02-24 20:55:35 -08001182 if (device->id == mBuiltInKeyboardId) {
1183 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
1184 device->path.string(), mBuiltInKeyboardId);
1185 mBuiltInKeyboardId = -1;
Jeff Brown93fa9b32011-06-14 17:09:25 -07001186 clearKeyboardPropertiesLocked(device, true);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001187 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001188 clearKeyboardPropertiesLocked(device, false);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001189
Jeff Brown93fa9b32011-06-14 17:09:25 -07001190 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
1191 LOGW("Could not remove device fd from epoll instance. errno=%d", errno);
1192 }
1193
1194 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001195 device->close();
1196
Jeff Brown8e9d4432011-03-12 19:46:59 -08001197 // Unlink for opening devices list if it is present.
1198 Device* pred = NULL;
1199 bool found = false;
1200 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1201 if (entry == device) {
1202 found = true;
1203 break;
1204 }
1205 pred = entry;
1206 entry = entry->next;
1207 }
1208 if (found) {
1209 // Unlink the device from the opening devices list then delete it.
1210 // We don't need to tell the client that the device was closed because
1211 // it does not even know it was opened in the first place.
1212 LOGI("Device %s was immediately closed after opening.", device->path.string());
1213 if (pred) {
1214 pred->next = device->next;
1215 } else {
1216 mOpeningDevices = device->next;
1217 }
1218 delete device;
1219 } else {
1220 // Link into closing devices list.
1221 // The device will be deleted later after we have informed the client.
1222 device->next = mClosingDevices;
1223 mClosingDevices = device;
1224 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001225}
1226
Jeff Brown93fa9b32011-06-14 17:09:25 -07001227status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 int res;
1229 char devname[PATH_MAX];
1230 char *filename;
1231 char event_buf[512];
1232 int event_size;
1233 int event_pos = 0;
1234 struct inotify_event *event;
1235
Jeff Brown93fa9b32011-06-14 17:09:25 -07001236 LOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
1237 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 if(res < (int)sizeof(*event)) {
1239 if(errno == EINTR)
1240 return 0;
1241 LOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001242 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244 //printf("got %d bytes of event information\n", res);
1245
Jeff Brown90655042010-12-02 13:50:46 -08001246 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 filename = devname + strlen(devname);
1248 *filename++ = '/';
1249
1250 while(res >= (int)sizeof(*event)) {
1251 event = (struct inotify_event *)(event_buf + event_pos);
1252 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1253 if(event->len) {
1254 strcpy(filename, event->name);
1255 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001256 openDeviceLocked(devname);
1257 } else {
1258 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
1260 }
1261 event_size = sizeof(*event) + event->len;
1262 res -= event_size;
1263 event_pos += event_size;
1264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 return 0;
1266}
1267
Jeff Brown93fa9b32011-06-14 17:09:25 -07001268status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269{
1270 char devname[PATH_MAX];
1271 char *filename;
1272 DIR *dir;
1273 struct dirent *de;
1274 dir = opendir(dirname);
1275 if(dir == NULL)
1276 return -1;
1277 strcpy(devname, dirname);
1278 filename = devname + strlen(devname);
1279 *filename++ = '/';
1280 while((de = readdir(dir))) {
1281 if(de->d_name[0] == '.' &&
1282 (de->d_name[1] == '\0' ||
1283 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1284 continue;
1285 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001286 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
1288 closedir(dir);
1289 return 0;
1290}
1291
Jeff Brown93fa9b32011-06-14 17:09:25 -07001292void EventHub::requestReopenDevices() {
1293 LOGV("requestReopenDevices() called");
1294
1295 AutoMutex _l(mLock);
1296 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001297}
1298
Jeff Brownf2f48712010-10-01 17:46:21 -07001299void EventHub::dump(String8& dump) {
1300 dump.append("Event Hub State:\n");
1301
1302 { // acquire lock
1303 AutoMutex _l(mLock);
1304
Jeff Brown90655042010-12-02 13:50:46 -08001305 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f48712010-10-01 17:46:21 -07001306
1307 dump.append(INDENT "Devices:\n");
1308
Jeff Brown93fa9b32011-06-14 17:09:25 -07001309 for (size_t i = 0; i < mDevices.size(); i++) {
1310 const Device* device = mDevices.valueAt(i);
1311 if (mBuiltInKeyboardId == device->id) {
1312 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1313 device->id, device->identifier.name.string());
1314 } else {
1315 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1316 device->identifier.name.string());
Jeff Brownf2f48712010-10-01 17:46:21 -07001317 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001318 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1319 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
1320 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1321 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1322 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1323 "product=0x%04x, version=0x%04x\n",
1324 device->identifier.bus, device->identifier.vendor,
1325 device->identifier.product, device->identifier.version);
1326 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1327 device->keyMap.keyLayoutFile.string());
1328 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1329 device->keyMap.keyCharacterMapFile.string());
1330 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1331 device->configurationFile.string());
Jeff Brownf2f48712010-10-01 17:46:21 -07001332 }
1333 } // release lock
1334}
1335
Jeff Brown89ef0722011-08-10 16:25:21 -07001336void EventHub::monitor() {
1337 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1338 mLock.lock();
1339 mLock.unlock();
1340}
1341
1342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343}; // namespace android