blob: af3088745b23e4c207ec014741a811d073960e4a [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//
18// Handle events, like key input and vsync.
19//
20// The goal is to provide an optimized solution for Linux, not an
21// implementation that works well across all platforms. We expect
22// events to arrive on file descriptors, so that we can use a select()
23// select() call to sleep.
24//
25// We can't select() on anything but network sockets in Windows, so we
26// provide an alternative implementation of waitEvent for that platform.
27//
28#define LOG_TAG "EventHub"
29
30//#define LOG_NDEBUG 0
31
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032#include "EventHub.h"
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <hardware_legacy/power.h>
35
36#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/Log.h>
38#include <utils/Timers.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070039#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070040#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42#include <stdlib.h>
43#include <stdio.h>
44#include <unistd.h>
45#include <fcntl.h>
46#include <memory.h>
47#include <errno.h>
48#include <assert.h>
49
Jeff Brown6b53e8d2010-11-10 16:03:06 -080050#include <ui/KeyLayoutMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080051#include <ui/KeyCharacterMap.h>
52#include <ui/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54#include <string.h>
55#include <stdint.h>
56#include <dirent.h>
57#ifdef HAVE_INOTIFY
58# include <sys/inotify.h>
59#endif
60#ifdef HAVE_ANDROID_OS
61# include <sys/limits.h> /* not part of Linux */
62#endif
63#include <sys/poll.h>
64#include <sys/ioctl.h>
65
66/* this macro is used to tell if "bit" is set in "array"
67 * it selects a byte from the array, and does a boolean AND
68 * operation with a byte that only has the relevant bit set.
69 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
70 */
71#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
72
Jeff Brownfd0358292010-06-30 16:10:35 -070073/* this macro computes the number of bytes needed to represent a bit array of the specified size */
74#define sizeof_bit_array(bits) ((bits + 7) / 8)
75
Jeff Brown90655042010-12-02 13:50:46 -080076// Fd at index 0 is always reserved for inotify
77#define FIRST_ACTUAL_DEVICE_INDEX 1
78
Jeff Brownf2f487182010-10-01 17:46:21 -070079#define INDENT " "
80#define INDENT2 " "
81#define INDENT3 " "
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083namespace android {
84
85static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080086static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/* return the larger integer */
89static inline int max(int v1, int v2)
90{
91 return (v1 > v2) ? v1 : v2;
92}
93
Jeff Brownf2f487182010-10-01 17:46:21 -070094static inline const char* toString(bool value) {
95 return value ? "true" : "false";
96}
97
Jeff Brown90655042010-12-02 13:50:46 -080098// --- EventHub::Device ---
99
100EventHub::Device::Device(int fd, int32_t id, const String8& path,
101 const InputDeviceIdentifier& identifier) :
102 next(NULL),
103 fd(fd), id(id), path(path), identifier(identifier),
Jeff Brown80fd47c2011-05-24 01:07:44 -0700104 classes(0), keyBitmask(NULL), relBitmask(NULL), propBitmask(NULL),
Jeff Browncc0c1592011-02-19 05:07:28 -0800105 configuration(NULL), virtualKeyMap(NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106}
107
Jeff Brown90655042010-12-02 13:50:46 -0800108EventHub::Device::~Device() {
109 close();
110 delete[] keyBitmask;
Jeff Browncc0c1592011-02-19 05:07:28 -0800111 delete[] relBitmask;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700112 delete[] propBitmask;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800113 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800114 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115}
116
Jeff Brown90655042010-12-02 13:50:46 -0800117void EventHub::Device::close() {
118 if (fd >= 0) {
119 ::close(fd);
120 fd = -1;
121 }
122}
123
124
125// --- EventHub ---
126
127EventHub::EventHub(void) :
128 mError(NO_INIT), mBuiltInKeyboardId(-1), mNextDeviceId(1),
129 mOpeningDevices(0), mClosingDevices(0),
130 mOpened(false), mNeedToSendFinishedDeviceScan(false),
Jeff Brownb7198742011-03-18 18:14:26 -0700131 mInputFdIndex(1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 memset(mSwitches, 0, sizeof(mSwitches));
Jeff Brownb7198742011-03-18 18:14:26 -0700135 mNumCpus = sysconf(_SC_NPROCESSORS_ONLN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136}
137
Jeff Brown90655042010-12-02 13:50:46 -0800138EventHub::~EventHub(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 release_wake_lock(WAKE_LOCK_ID);
140 // we should free stuff here...
141}
142
Jeff Brown90655042010-12-02 13:50:46 -0800143status_t EventHub::errorCheck() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 return mError;
145}
146
Jeff Brown90655042010-12-02 13:50:46 -0800147String8 EventHub::getDeviceName(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800149 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 if (device == NULL) return String8();
Jeff Brown90655042010-12-02 13:50:46 -0800151 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152}
153
Jeff Brown90655042010-12-02 13:50:46 -0800154uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800156 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 if (device == NULL) return 0;
158 return device->classes;
159}
160
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800161void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800162 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800163 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800164 if (device && device->configuration) {
165 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800166 } else {
167 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800168 }
169}
170
Jeff Brown6d0fec22010-07-23 21:28:06 -0700171status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
172 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700173 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800176 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 if (device == NULL) return -1;
178
179 struct input_absinfo info;
180
Jens Gulinc4554b92010-06-22 22:21:57 +0200181 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700182 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jeff Brown90655042010-12-02 13:50:46 -0800183 axis, device->identifier.name.string(), device->fd);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700184 return -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700186
187 if (info.minimum != info.maximum) {
188 outAxisInfo->valid = true;
189 outAxisInfo->minValue = info.minimum;
190 outAxisInfo->maxValue = info.maximum;
191 outAxisInfo->flat = info.flat;
192 outAxisInfo->fuzz = info.fuzz;
193 }
194 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195}
196
Jeff Browncc0c1592011-02-19 05:07:28 -0800197bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
198 if (axis >= 0 && axis <= REL_MAX) {
199 AutoMutex _l(mLock);
200
201 Device* device = getDeviceLocked(deviceId);
202 if (device && device->relBitmask) {
203 return test_bit(axis, device->relBitmask);
204 }
205 }
206 return false;
207}
208
Jeff Brown80fd47c2011-05-24 01:07:44 -0700209bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
210 if (property >= 0 && property <= INPUT_PROP_MAX) {
211 AutoMutex _l(mLock);
212
213 Device* device = getDeviceLocked(deviceId);
214 if (device && device->propBitmask) {
215 return test_bit(property, device->propBitmask);
216 }
217 }
218 return false;
219}
220
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700222 if (scanCode >= 0 && scanCode <= KEY_MAX) {
223 AutoMutex _l(mLock);
224
Jeff Brown90655042010-12-02 13:50:46 -0800225 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700226 if (device != NULL) {
227 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 }
229 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700230 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231}
232
Jeff Brown90655042010-12-02 13:50:46 -0800233int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700234 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200236 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700237 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700238 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700239 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700240 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700241}
242
Jeff Brown6d0fec22010-07-23 21:28:06 -0700243int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
244 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700245
Jeff Brown90655042010-12-02 13:50:46 -0800246 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700247 if (device != NULL) {
248 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700250 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251}
252
Jeff Brown90655042010-12-02 13:50:46 -0800253int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const {
254 if (!device->keyMap.haveKeyLayout()) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800255 return AKEY_STATE_UNKNOWN;
256 }
257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800259 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260
Jeff Brownfd0358292010-06-30 16:10:35 -0700261 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200263 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 #if 0
265 for (size_t i=0; i<=KEY_MAX; i++) {
266 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
267 }
268 #endif
269 const size_t N = scanCodes.size();
270 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
271 int32_t sc = scanCodes.itemAt(i);
272 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
273 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700274 return AKEY_STATE_DOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700277 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700279 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700280}
281
Jeff Brown6d0fec22010-07-23 21:28:06 -0700282int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700283 if (sw >= 0 && sw <= SW_MAX) {
284 AutoMutex _l(mLock);
285
Jeff Brown90655042010-12-02 13:50:46 -0800286 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700287 if (device != NULL) {
288 return getSwitchStateLocked(device, sw);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700289 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700290 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700291 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700292}
293
Jeff Brown90655042010-12-02 13:50:46 -0800294int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700295 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200297 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700298 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700299 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700300 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700301 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302}
303
Jeff Brown6d0fec22010-07-23 21:28:06 -0700304bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
305 const int32_t* keyCodes, uint8_t* outFlags) const {
306 AutoMutex _l(mLock);
307
Jeff Brown90655042010-12-02 13:50:46 -0800308 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700309 if (device != NULL) {
310 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
311 }
312 return false;
313}
314
Jeff Brown90655042010-12-02 13:50:46 -0800315bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700316 const int32_t* keyCodes, uint8_t* outFlags) const {
Jeff Brown90655042010-12-02 13:50:46 -0800317 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700318 return false;
319 }
320
321 Vector<int32_t> scanCodes;
322 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
323 scanCodes.clear();
324
Jeff Brown6f2fba42011-02-19 01:08:02 -0800325 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
326 keyCodes[codeIndex], &scanCodes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700327 if (! err) {
328 // check the possible scan codes identified by the layout map against the
329 // map of codes actually emitted by the driver
330 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
331 if (test_bit(scanCodes[sc], device->keyBitmask)) {
332 outFlags[codeIndex] = 1;
333 break;
334 }
335 }
336 }
337 }
338 return true;
339}
340
Jeff Brown6f2fba42011-02-19 01:08:02 -0800341status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700342 int32_t* outKeycode, uint32_t* outFlags) const
343{
344 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800345 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700346
Jeff Brown90655042010-12-02 13:50:46 -0800347 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800348 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700349 if (err == NO_ERROR) {
350 return NO_ERROR;
351 }
352 }
353
Jeff Brown90655042010-12-02 13:50:46 -0800354 if (mBuiltInKeyboardId != -1) {
355 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700356
Jeff Brown90655042010-12-02 13:50:46 -0800357 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800358 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700359 if (err == NO_ERROR) {
360 return NO_ERROR;
361 }
362 }
363 }
364
365 *outKeycode = 0;
366 *outFlags = 0;
367 return NAME_NOT_FOUND;
368}
369
Jeff Brown85297452011-03-04 13:07:49 -0800370status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800371{
372 AutoMutex _l(mLock);
373 Device* device = getDeviceLocked(deviceId);
374
375 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800376 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800377 if (err == NO_ERROR) {
378 return NO_ERROR;
379 }
380 }
381
382 if (mBuiltInKeyboardId != -1) {
383 device = getDeviceLocked(mBuiltInKeyboardId);
384
385 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800386 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800387 if (err == NO_ERROR) {
388 return NO_ERROR;
389 }
390 }
391 }
392
Jeff Brown6f2fba42011-02-19 01:08:02 -0800393 return NAME_NOT_FOUND;
394}
395
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400396void EventHub::addExcludedDevice(const char* deviceName)
397{
Jeff Brownf2f487182010-10-01 17:46:21 -0700398 AutoMutex _l(mLock);
399
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400400 String8 name(deviceName);
401 mExcludedDevices.push_back(name);
402}
403
Jeff Brown497a92c2010-09-12 17:55:08 -0700404bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
405 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800406 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700407 if (device) {
408 uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)];
409 memset(bitmask, 0, sizeof(bitmask));
410 if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) {
411 if (test_bit(led, bitmask)) {
412 return true;
413 }
414 }
415 }
416 return false;
417}
418
419void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
420 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800421 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700422 if (device) {
423 struct input_event ev;
424 ev.time.tv_sec = 0;
425 ev.time.tv_usec = 0;
426 ev.type = EV_LED;
427 ev.code = led;
428 ev.value = on ? 1 : 0;
429
430 ssize_t nWrite;
431 do {
432 nWrite = write(device->fd, &ev, sizeof(struct input_event));
433 } while (nWrite == -1 && errno == EINTR);
434 }
435}
436
Jeff Brown90655042010-12-02 13:50:46 -0800437void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
438 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
439 outVirtualKeys.clear();
440
441 AutoMutex _l(mLock);
442 Device* device = getDeviceLocked(deviceId);
443 if (device && device->virtualKeyMap) {
444 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
445 }
446}
447
448EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
449 if (deviceId == 0) {
450 deviceId = mBuiltInKeyboardId;
451 }
452
453 size_t numDevices = mDevices.size();
454 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < numDevices; i++) {
455 Device* device = mDevices[i];
456 if (device->id == deviceId) {
457 return device;
458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460 return NULL;
461}
462
Jeff Brownb7198742011-03-18 18:14:26 -0700463size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
464 // Note that we only allow one caller to getEvents(), so don't need
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 // to do locking here... only when adding/removing devices.
Jeff Brownb6110c22011-04-01 16:15:13 -0700466 LOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400467
468 if (!mOpened) {
469 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
470 mOpened = true;
Jeff Brown7342bb92010-10-01 18:55:43 -0700471 mNeedToSendFinishedDeviceScan = true;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400472 }
473
Jeff Brownb7198742011-03-18 18:14:26 -0700474 struct input_event readBuffer[bufferSize];
475
476 RawEvent* event = buffer;
477 size_t capacity = bufferSize;
Jeff Browncc2e7172010-08-17 16:48:25 -0700478 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700479 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
480
Jeff Browncc2e7172010-08-17 16:48:25 -0700481 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700482 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800483 Device* device = mClosingDevices;
484 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 device->id, device->path.string());
486 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700487 event->when = now;
488 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
489 event->type = DEVICE_REMOVED;
490 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700492 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700493 if (--capacity == 0) {
494 break;
495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700497
Jeff Brownb7198742011-03-18 18:14:26 -0700498 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800499 Device* device = mOpeningDevices;
500 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 device->id, device->path.string());
502 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700503 event->when = now;
504 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
505 event->type = DEVICE_ADDED;
506 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700507 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700508 if (--capacity == 0) {
509 break;
510 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700511 }
512
513 if (mNeedToSendFinishedDeviceScan) {
514 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700515 event->when = now;
516 event->type = FINISHED_DEVICE_SCAN;
517 event += 1;
518 if (--capacity == 0) {
519 break;
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522
Jeff Browncc2e7172010-08-17 16:48:25 -0700523 // Grab the next input event.
Jeff Brownb7198742011-03-18 18:14:26 -0700524 // mInputFdIndex is initially 1 because index 0 is used for inotify.
Jeff Brown33bbfd22011-02-24 20:55:35 -0800525 bool deviceWasRemoved = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700526 while (mInputFdIndex < mFds.size()) {
Jeff Brown90655042010-12-02 13:50:46 -0800527 const struct pollfd& pfd = mFds[mInputFdIndex];
Jeff Browncc2e7172010-08-17 16:48:25 -0700528 if (pfd.revents & POLLIN) {
Jeff Brownb7198742011-03-18 18:14:26 -0700529 int32_t readSize = read(pfd.fd, readBuffer, sizeof(struct input_event) * capacity);
Jeff Browncc2e7172010-08-17 16:48:25 -0700530 if (readSize < 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -0800531 if (errno == ENODEV) {
532 deviceWasRemoved = true;
533 break;
534 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700535 if (errno != EAGAIN && errno != EINTR) {
536 LOGW("could not get event (errno=%d)", errno);
537 }
538 } else if ((readSize % sizeof(struct input_event)) != 0) {
539 LOGE("could not get event (wrong size: %d)", readSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700540 } else if (readSize == 0) { // eof
541 deviceWasRemoved = true;
542 break;
Jeff Browncc2e7172010-08-17 16:48:25 -0700543 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700544 const Device* device = mDevices[mInputFdIndex];
545 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
546
547 size_t count = size_t(readSize) / sizeof(struct input_event);
548 for (size_t i = 0; i < count; i++) {
549 const struct input_event& iev = readBuffer[i];
550 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
551 device->path.string(),
552 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
553 iev.type, iev.code, iev.value);
554
Jeff Brown4e91a182011-04-07 11:38:09 -0700555#ifdef HAVE_POSIX_CLOCKS
556 // Use the time specified in the event instead of the current time
557 // so that downstream code can get more accurate estimates of
558 // event dispatch latency from the time the event is enqueued onto
559 // the evdev client buffer.
560 //
561 // The event's timestamp fortuitously uses the same monotonic clock
562 // time base as the rest of Android. The kernel event device driver
563 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
564 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
565 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
566 // system call that also queries ktime_get_ts().
567 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
568 + nsecs_t(iev.time.tv_usec) * 1000LL;
569 LOGV("event time %lld, now %lld", event->when, now);
570#else
Jeff Brownb7198742011-03-18 18:14:26 -0700571 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700572#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700573 event->deviceId = deviceId;
574 event->type = iev.type;
575 event->scanCode = iev.code;
576 event->value = iev.value;
577 event->keyCode = AKEYCODE_UNKNOWN;
578 event->flags = 0;
579 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
580 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
581 &event->keyCode, &event->flags);
582 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
583 iev.code, event->keyCode, event->flags, err);
584 }
585 event += 1;
586 }
587 capacity -= count;
588 if (capacity == 0) {
589 break;
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592 }
Jeff Brownb7198742011-03-18 18:14:26 -0700593 mInputFdIndex += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700595
Jeff Brown33bbfd22011-02-24 20:55:35 -0800596 // Handle the case where a device has been removed but INotify has not yet noticed.
597 if (deviceWasRemoved) {
598 AutoMutex _l(mLock);
599 closeDeviceAtIndexLocked(mInputFdIndex);
600 continue; // report added or removed devices immediately
601 }
602
Jeff Browna9b84222010-10-14 02:23:43 -0700603#if HAVE_INOTIFY
Jeff Brown7342bb92010-10-01 18:55:43 -0700604 // readNotify() will modify mFDs and mFDCount, so this must be done after
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 // processing all other events.
Jeff Brown90655042010-12-02 13:50:46 -0800606 if(mFds[0].revents & POLLIN) {
607 readNotify(mFds[0].fd);
608 mFds.editItemAt(0).revents = 0;
Jeff Brownb7198742011-03-18 18:14:26 -0700609 mInputFdIndex = mFds.size();
Jeff Browna9b84222010-10-14 02:23:43 -0700610 continue; // report added or removed devices immediately
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
Jeff Browna9b84222010-10-14 02:23:43 -0700612#endif
613
Jeff Brownb7198742011-03-18 18:14:26 -0700614 // Return now if we have collected any events, otherwise poll.
615 if (event != buffer) {
616 break;
617 }
618
Jeff Browncc2e7172010-08-17 16:48:25 -0700619 // Poll for events. Mind the wake lock dance!
620 // We hold a wake lock at all times except during poll(). This works due to some
621 // subtle choreography. When a device driver has pending (unread) events, it acquires
622 // a kernel wake lock. However, once the last pending event has been read, the device
623 // driver will release the kernel wake lock. To prevent the system from going to sleep
624 // when this happens, the EventHub holds onto its own user wake lock while the client
625 // is processing events. Thus the system can only sleep if there are no events
626 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700627 //
628 // The timeout is advisory only. If the device is asleep, it will not wake just to
629 // service the timeout.
Jeff Browncc2e7172010-08-17 16:48:25 -0700630 release_wake_lock(WAKE_LOCK_ID);
631
Jeff Brownaa3855d2011-03-17 01:34:19 -0700632 int pollResult = poll(mFds.editArray(), mFds.size(), timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700633
634 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
635
Jeff Brownaa3855d2011-03-17 01:34:19 -0700636 if (pollResult == 0) {
Jeff Brownb7198742011-03-18 18:14:26 -0700637 break; // timed out
Jeff Brownaa3855d2011-03-17 01:34:19 -0700638 }
639 if (pollResult < 0) {
Jeff Brownb7198742011-03-18 18:14:26 -0700640 // Sleep after errors to avoid locking up the system.
641 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700642 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700643 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700644 usleep(100000);
645 }
Jeff Brownb7198742011-03-18 18:14:26 -0700646 } else {
647 // On an SMP system, it is possible for the framework to read input events
648 // faster than the kernel input device driver can produce a complete packet.
649 // Because poll() wakes up as soon as the first input event becomes available,
650 // the framework will often end up reading one event at a time until the
651 // packet is complete. Instead of one call to read() returning 71 events,
652 // it could take 71 calls to read() each returning 1 event.
653 //
654 // Sleep for a short period of time after waking up from the poll() to give
655 // the kernel time to finish writing the entire packet of input events.
656 if (mNumCpus > 1) {
657 usleep(250);
658 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700659 }
Jeff Brown33bbfd22011-02-24 20:55:35 -0800660
661 // Prepare to process all of the FDs we just polled.
Jeff Brownb7198742011-03-18 18:14:26 -0700662 mInputFdIndex = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
Jeff Brownb7198742011-03-18 18:14:26 -0700664
665 // All done, return the number of events we read.
666 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667}
668
669/*
670 * Open the platform-specific input device.
671 */
Jeff Brown90655042010-12-02 13:50:46 -0800672bool EventHub::openPlatformInput(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 /*
674 * Open platform-specific input device(s).
675 */
Jeff Brown90655042010-12-02 13:50:46 -0800676 int res, fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678#ifdef HAVE_INOTIFY
Jeff Brown90655042010-12-02 13:50:46 -0800679 fd = inotify_init();
680 res = inotify_add_watch(fd, DEVICE_PATH, IN_DELETE | IN_CREATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800682 LOGE("could not add watch for %s, %s\n", DEVICE_PATH, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
684#else
685 /*
686 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
687 * We allocate space for it and set it to something invalid.
688 */
Jeff Brown90655042010-12-02 13:50:46 -0800689 fd = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690#endif
691
Jeff Brown90655042010-12-02 13:50:46 -0800692 // Reserve fd index 0 for inotify.
693 struct pollfd pollfd;
694 pollfd.fd = fd;
695 pollfd.events = POLLIN;
696 pollfd.revents = 0;
697 mFds.push(pollfd);
698 mDevices.push(NULL);
699
700 res = scanDir(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800702 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704
705 return true;
706}
707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708// ----------------------------------------------------------------------------
709
Jeff Brownfd0358292010-06-30 16:10:35 -0700710static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
711 const uint8_t* end = array + endIndex;
712 array += startIndex;
713 while (array != end) {
714 if (*(array++) != 0) {
715 return true;
716 }
717 }
718 return false;
719}
720
721static const int32_t GAMEPAD_KEYCODES[] = {
722 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
723 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
724 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
725 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
726 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800727 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
728 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
729 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
730 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
731 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -0700732};
733
Jeff Brown90655042010-12-02 13:50:46 -0800734int EventHub::openDevice(const char *devicePath) {
735 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
Jeff Brown90655042010-12-02 13:50:46 -0800737 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738
739 AutoMutex _l(mLock);
Nick Pellye6b1bbd2010-01-20 19:36:49 -0800740
Jeff Brown90655042010-12-02 13:50:46 -0800741 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800743 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 return -1;
745 }
746
Jeff Brown90655042010-12-02 13:50:46 -0800747 InputDeviceIdentifier identifier;
748
749 // Get device name.
750 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
751 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
752 } else {
753 buffer[sizeof(buffer) - 1] = '\0';
754 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400756
Jeff Brown90655042010-12-02 13:50:46 -0800757 // Check to see if the device is on our excluded list
Mike Lockwood15431a92009-07-17 00:10:10 -0400758 List<String8>::iterator iter = mExcludedDevices.begin();
759 List<String8>::iterator end = mExcludedDevices.end();
760 for ( ; iter != end; iter++) {
761 const char* test = *iter;
Jeff Brown90655042010-12-02 13:50:46 -0800762 if (identifier.name == test) {
763 LOGI("ignoring event id %s driver %s\n", devicePath, test);
Mike Lockwood15431a92009-07-17 00:10:10 -0400764 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400765 return -1;
766 }
767 }
768
Jeff Brown90655042010-12-02 13:50:46 -0800769 // Get device driver version.
770 int driverVersion;
771 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
772 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
773 close(fd);
774 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
776
Jeff Brown90655042010-12-02 13:50:46 -0800777 // Get device identifier.
778 struct input_id inputId;
779 if(ioctl(fd, EVIOCGID, &inputId)) {
780 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
781 close(fd);
782 return -1;
783 }
784 identifier.bus = inputId.bustype;
785 identifier.product = inputId.product;
786 identifier.vendor = inputId.vendor;
787 identifier.version = inputId.version;
788
789 // Get device physical location.
790 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
791 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
792 } else {
793 buffer[sizeof(buffer) - 1] = '\0';
794 identifier.location.setTo(buffer);
795 }
796
797 // Get device unique id.
798 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
799 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
800 } else {
801 buffer[sizeof(buffer) - 1] = '\0';
802 identifier.uniqueId.setTo(buffer);
803 }
804
805 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700806 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
807 LOGE("Error %d making device file descriptor non-blocking.", errno);
808 close(fd);
809 return -1;
810 }
811
Jeff Brown90655042010-12-02 13:50:46 -0800812 // Allocate device. (The device object takes ownership of the fd at this point.)
813 int32_t deviceId = mNextDeviceId++;
814 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815
816#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800817 LOGI("add device %d: %s\n", deviceId, devicePath);
818 LOGI(" bus: %04x\n"
819 " vendor %04x\n"
820 " product %04x\n"
821 " version %04x\n",
822 identifier.bus, identifier.vendor, identifier.product, identifier.version);
823 LOGI(" name: \"%s\"\n", identifier.name.string());
824 LOGI(" location: \"%s\"\n", identifier.location.string());
825 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
826 LOGI(" driver: v%d.%d.%d\n",
827 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828#endif
829
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800830 // Load the configuration file for the device.
831 loadConfiguration(device);
832
Jeff Brownfd0358292010-06-30 16:10:35 -0700833 // Figure out the kinds of events the device reports.
Jeff Brownfd0358292010-06-30 16:10:35 -0700834 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800836 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700837
Jeff Brown6f2fba42011-02-19 01:08:02 -0800838 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
839 memset(abs_bitmask, 0, sizeof(abs_bitmask));
840 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700841
Jeff Brown6f2fba42011-02-19 01:08:02 -0800842 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
843 memset(rel_bitmask, 0, sizeof(rel_bitmask));
844 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700845
Jeff Brown6f2fba42011-02-19 01:08:02 -0800846 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
847 memset(sw_bitmask, 0, sizeof(sw_bitmask));
848 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask);
849
Jeff Brown80fd47c2011-05-24 01:07:44 -0700850 uint8_t prop_bitmask[sizeof_bit_array(INPUT_PROP_MAX + 1)];
851 memset(prop_bitmask, 0, sizeof(prop_bitmask));
852 ioctl(fd, EVIOCGPROP(sizeof(prop_bitmask)), prop_bitmask);
853
Jeff Browncc0c1592011-02-19 05:07:28 -0800854 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
Jeff Brown80fd47c2011-05-24 01:07:44 -0700855 device->relBitmask = new uint8_t[sizeof(rel_bitmask)];
856 device->propBitmask = new uint8_t[sizeof(prop_bitmask)];
857
858 if (!device->keyBitmask || !device->relBitmask || !device->propBitmask) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800859 delete device;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700860 LOGE("out of memory allocating bitmasks");
Jeff Browncc0c1592011-02-19 05:07:28 -0800861 return -1;
862 }
863
Jeff Brown80fd47c2011-05-24 01:07:44 -0700864 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
865 memcpy(device->relBitmask, rel_bitmask, sizeof(rel_bitmask));
866 memcpy(device->propBitmask, prop_bitmask, sizeof(prop_bitmask));
Jeff Browncc0c1592011-02-19 05:07:28 -0800867
Jeff Brown6f2fba42011-02-19 01:08:02 -0800868 // See if this is a keyboard. Ignore everything in the button range except for
869 // joystick and gamepad buttons which are handled like keyboards for the most part.
870 bool haveKeyboardKeys = containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
871 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
872 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800873 bool haveGamepadButtons = containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_MISC),
874 sizeof_bit_array(BTN_MOUSE))
875 || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_JOYSTICK),
876 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800877 if (haveKeyboardKeys || haveGamepadButtons) {
878 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800880
Jeff Brown83c09682010-12-23 17:50:18 -0800881 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800882 if (test_bit(BTN_MOUSE, key_bitmask)
883 && test_bit(REL_X, rel_bitmask)
884 && test_bit(REL_Y, rel_bitmask)) {
885 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
Jeff Brownfd0358292010-06-30 16:10:35 -0700887
888 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800889 // Is this a new modern multi-touch driver?
890 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
891 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
892 // Some joysticks such as the PS3 controller report axes that conflict
893 // with the ABS_MT range. Try to confirm that the device really is
894 // a touch screen.
895 if (test_bit(BTN_TOUCH, key_bitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800896 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -0700897 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800898 // Is this an old style single-touch driver?
899 } else if (test_bit(BTN_TOUCH, key_bitmask)
900 && test_bit(ABS_X, abs_bitmask)
901 && test_bit(ABS_Y, abs_bitmask)) {
902 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800905 // See if this device is a joystick.
906 // Ignore touchscreens because they use the same absolute axes for other purposes.
907 // Assumes that joysticks always have gamepad buttons in order to distinguish them
908 // from other devices such as accelerometers that also have absolute axes.
909 if (haveGamepadButtons
910 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)
911 && containsNonZeroByte(abs_bitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
912 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
913 }
914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 // figure out the switches this device reports
Jeff Brown6f2fba42011-02-19 01:08:02 -0800916 bool haveSwitches = false;
917 for (int i=0; i<EV_SW; i++) {
918 //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
919 if (test_bit(i, sw_bitmask)) {
920 haveSwitches = true;
921 if (mSwitches[i] == 0) {
922 mSwitches[i] = device->id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924 }
925 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800926 if (haveSwitches) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700927 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929
Jeff Brown58a2da82011-01-25 16:02:22 -0800930 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800931 // Load the virtual keys for the touch screen, if any.
932 // We do this now so that we can make sure to load the keymap if necessary.
933 status_t status = loadVirtualKeyMap(device);
934 if (!status) {
935 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
Jeff Brown90655042010-12-02 13:50:46 -0800937 }
938
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800939 // Load the key map.
940 // We need to do this for joysticks too because the key layout may specify axes.
941 status_t keyMapStatus = NAME_NOT_FOUND;
942 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -0800943 // Load the keymap for the device.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800944 keyMapStatus = loadKeyMap(device);
945 }
Jeff Brown90655042010-12-02 13:50:46 -0800946
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800947 // Configure the keyboard, gamepad or virtual keyboard.
948 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -0800949 // Set system properties for the keyboard.
Jeff Brown497a92c2010-09-12 17:55:08 -0700950 setKeyboardProperties(device, false);
951
Jeff Brown90655042010-12-02 13:50:46 -0800952 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800953 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -0800954 && mBuiltInKeyboardId == -1
955 && isEligibleBuiltInKeyboard(device->identifier,
956 device->configuration, &device->keyMap)) {
957 mBuiltInKeyboardId = device->id;
958 setKeyboardProperties(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -0700959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700961 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -0700962 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700963 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700964 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700965
Jeff Brownfd0358292010-06-30 16:10:35 -0700966 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -0700967 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
968 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
969 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
970 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
971 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700972 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700973 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700974
Jeff Brownfd0358292010-06-30 16:10:35 -0700975 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -0700976 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700977 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -0700978 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
979 break;
980 }
981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
983
Sean McNeilaeb00c42010-06-23 16:00:37 +0700984 // If the device isn't recognized as something we handle, don't monitor it.
985 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800986 LOGV("Dropping device: id=%d, path='%s', name='%s'",
987 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +0700988 delete device;
989 return -1;
990 }
991
Jeff Brown56194eb2011-03-02 19:23:13 -0800992 // Determine whether the device is external or internal.
993 if (isExternalDevice(device)) {
994 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
995 }
996
Jeff Brown90655042010-12-02 13:50:46 -0800997 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
998 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
999 deviceId, fd, devicePath, device->identifier.name.string(),
1000 device->classes,
1001 device->configurationFile.string(),
1002 device->keyMap.keyLayoutFile.string(),
1003 device->keyMap.keyCharacterMapFile.string(),
1004 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001005
Jeff Brown90655042010-12-02 13:50:46 -08001006 struct pollfd pollfd;
1007 pollfd.fd = fd;
1008 pollfd.events = POLLIN;
1009 pollfd.revents = 0;
1010 mFds.push(pollfd);
1011 mDevices.push(device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 device->next = mOpeningDevices;
1014 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 return 0;
1016}
1017
Jeff Brown90655042010-12-02 13:50:46 -08001018void EventHub::loadConfiguration(Device* device) {
1019 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1020 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001021 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -08001022 LOGD("No input device configuration file found for device '%s'.",
1023 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001024 } else {
1025 status_t status = PropertyMap::load(device->configurationFile,
1026 &device->configuration);
1027 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -08001028 LOGE("Error loading input device configuration file for device '%s'. "
1029 "Using default configuration.",
1030 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001031 }
1032 }
1033}
1034
Jeff Brown90655042010-12-02 13:50:46 -08001035status_t EventHub::loadVirtualKeyMap(Device* device) {
1036 // The virtual key map is supplied by the kernel as a system board property file.
1037 String8 path;
1038 path.append("/sys/board_properties/virtualkeys.");
1039 path.append(device->identifier.name);
1040 if (access(path.string(), R_OK)) {
1041 return NAME_NOT_FOUND;
1042 }
1043 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001044}
1045
Jeff Brown90655042010-12-02 13:50:46 -08001046status_t EventHub::loadKeyMap(Device* device) {
1047 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001048}
1049
Jeff Brown90655042010-12-02 13:50:46 -08001050void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) {
1051 int32_t id = builtInKeyboard ? 0 : device->id;
1052 android::setKeyboardProperties(id, device->identifier,
1053 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1054}
1055
1056void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) {
1057 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001058 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -07001059}
1060
Jeff Brown56194eb2011-03-02 19:23:13 -08001061bool EventHub::isExternalDevice(Device* device) {
1062 if (device->configuration) {
1063 bool value;
1064 if (device->configuration->tryGetProperty(String8("device.internal"), value)
1065 && value) {
1066 return false;
1067 }
1068 }
1069 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1070}
1071
Jeff Brown90655042010-12-02 13:50:46 -08001072bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1073 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001074 return false;
1075 }
1076
1077 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001078 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001079 const size_t N = scanCodes.size();
1080 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1081 int32_t sc = scanCodes.itemAt(i);
1082 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1083 return true;
1084 }
1085 }
1086
1087 return false;
1088}
1089
Jeff Brown90655042010-12-02 13:50:46 -08001090int EventHub::closeDevice(const char *devicePath) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 AutoMutex _l(mLock);
Jeff Brown7342bb92010-10-01 18:55:43 -07001092
Jeff Brown90655042010-12-02 13:50:46 -08001093 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1094 Device* device = mDevices[i];
1095 if (device->path == devicePath) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001096 return closeDeviceAtIndexLocked(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001099 LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 return -1;
1101}
1102
Jeff Brown33bbfd22011-02-24 20:55:35 -08001103int EventHub::closeDeviceAtIndexLocked(int index) {
1104 Device* device = mDevices[index];
1105 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1106 device->path.string(), device->identifier.name.string(), device->id,
1107 device->fd, device->classes);
1108
1109 for (int j=0; j<EV_SW; j++) {
1110 if (mSwitches[j] == device->id) {
1111 mSwitches[j] = 0;
1112 }
1113 }
1114
1115 if (device->id == mBuiltInKeyboardId) {
1116 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
1117 device->path.string(), mBuiltInKeyboardId);
1118 mBuiltInKeyboardId = -1;
1119 clearKeyboardProperties(device, true);
1120 }
1121 clearKeyboardProperties(device, false);
1122
1123 mFds.removeAt(index);
1124 mDevices.removeAt(index);
1125 device->close();
1126
Jeff Brown8e9d4432011-03-12 19:46:59 -08001127 // Unlink for opening devices list if it is present.
1128 Device* pred = NULL;
1129 bool found = false;
1130 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1131 if (entry == device) {
1132 found = true;
1133 break;
1134 }
1135 pred = entry;
1136 entry = entry->next;
1137 }
1138 if (found) {
1139 // Unlink the device from the opening devices list then delete it.
1140 // We don't need to tell the client that the device was closed because
1141 // it does not even know it was opened in the first place.
1142 LOGI("Device %s was immediately closed after opening.", device->path.string());
1143 if (pred) {
1144 pred->next = device->next;
1145 } else {
1146 mOpeningDevices = device->next;
1147 }
1148 delete device;
1149 } else {
1150 // Link into closing devices list.
1151 // The device will be deleted later after we have informed the client.
1152 device->next = mClosingDevices;
1153 mClosingDevices = device;
1154 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001155 return 0;
1156}
1157
Jeff Brown7342bb92010-10-01 18:55:43 -07001158int EventHub::readNotify(int nfd) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159#ifdef HAVE_INOTIFY
1160 int res;
1161 char devname[PATH_MAX];
1162 char *filename;
1163 char event_buf[512];
1164 int event_size;
1165 int event_pos = 0;
1166 struct inotify_event *event;
1167
Jeff Brown7342bb92010-10-01 18:55:43 -07001168 LOGV("EventHub::readNotify nfd: %d\n", nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 res = read(nfd, event_buf, sizeof(event_buf));
1170 if(res < (int)sizeof(*event)) {
1171 if(errno == EINTR)
1172 return 0;
1173 LOGW("could not get event, %s\n", strerror(errno));
1174 return 1;
1175 }
1176 //printf("got %d bytes of event information\n", res);
1177
Jeff Brown90655042010-12-02 13:50:46 -08001178 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 filename = devname + strlen(devname);
1180 *filename++ = '/';
1181
1182 while(res >= (int)sizeof(*event)) {
1183 event = (struct inotify_event *)(event_buf + event_pos);
1184 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1185 if(event->len) {
1186 strcpy(filename, event->name);
1187 if(event->mask & IN_CREATE) {
Jeff Brown7342bb92010-10-01 18:55:43 -07001188 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 else {
Jeff Brown7342bb92010-10-01 18:55:43 -07001191 closeDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
1194 event_size = sizeof(*event) + event->len;
1195 res -= event_size;
1196 event_pos += event_size;
1197 }
1198#endif
1199 return 0;
1200}
1201
Jeff Brown7342bb92010-10-01 18:55:43 -07001202int EventHub::scanDir(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203{
1204 char devname[PATH_MAX];
1205 char *filename;
1206 DIR *dir;
1207 struct dirent *de;
1208 dir = opendir(dirname);
1209 if(dir == NULL)
1210 return -1;
1211 strcpy(devname, dirname);
1212 filename = devname + strlen(devname);
1213 *filename++ = '/';
1214 while((de = readdir(dir))) {
1215 if(de->d_name[0] == '.' &&
1216 (de->d_name[1] == '\0' ||
1217 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1218 continue;
1219 strcpy(filename, de->d_name);
Jeff Brown7342bb92010-10-01 18:55:43 -07001220 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222 closedir(dir);
1223 return 0;
1224}
1225
Jeff Brownf2f487182010-10-01 17:46:21 -07001226void EventHub::dump(String8& dump) {
1227 dump.append("Event Hub State:\n");
1228
1229 { // acquire lock
1230 AutoMutex _l(mLock);
1231
Jeff Brown90655042010-12-02 13:50:46 -08001232 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001233
1234 dump.append(INDENT "Devices:\n");
1235
Jeff Brown90655042010-12-02 13:50:46 -08001236 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1237 const Device* device = mDevices[i];
Jeff Brownf2f487182010-10-01 17:46:21 -07001238 if (device) {
Jeff Brown90655042010-12-02 13:50:46 -08001239 if (mBuiltInKeyboardId == device->id) {
1240 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1241 device->id, device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001242 } else {
Jeff Brown90655042010-12-02 13:50:46 -08001243 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1244 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001245 }
1246 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1247 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Brown90655042010-12-02 13:50:46 -08001248 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1249 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1250 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1251 "product=0x%04x, version=0x%04x\n",
1252 device->identifier.bus, device->identifier.vendor,
1253 device->identifier.product, device->identifier.version);
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001254 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001255 device->keyMap.keyLayoutFile.string());
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001256 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001257 device->keyMap.keyCharacterMapFile.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001258 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1259 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001260 }
1261 }
1262 } // release lock
1263}
1264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265}; // namespace android