blob: f748e7cd9dd2d488c07f41d01fc1e294a8a9643f [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
Jeff Brown1a84fd12011-06-02 01:26:32 -070036#include <cutils/atomic.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include <utils/Log.h>
39#include <utils/Timers.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070040#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070041#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43#include <stdlib.h>
44#include <stdio.h>
45#include <unistd.h>
46#include <fcntl.h>
47#include <memory.h>
48#include <errno.h>
49#include <assert.h>
50
Jeff Brown6b53e8d2010-11-10 16:03:06 -080051#include <ui/KeyLayoutMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080052#include <ui/KeyCharacterMap.h>
53#include <ui/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55#include <string.h>
56#include <stdint.h>
57#include <dirent.h>
58#ifdef HAVE_INOTIFY
59# include <sys/inotify.h>
60#endif
61#ifdef HAVE_ANDROID_OS
62# include <sys/limits.h> /* not part of Linux */
63#endif
64#include <sys/poll.h>
65#include <sys/ioctl.h>
66
67/* this macro is used to tell if "bit" is set in "array"
68 * it selects a byte from the array, and does a boolean AND
69 * operation with a byte that only has the relevant bit set.
70 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
71 */
72#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
73
Jeff Brownfd0358292010-06-30 16:10:35 -070074/* this macro computes the number of bytes needed to represent a bit array of the specified size */
75#define sizeof_bit_array(bits) ((bits + 7) / 8)
76
Jeff Brown90655042010-12-02 13:50:46 -080077// Fd at index 0 is always reserved for inotify
78#define FIRST_ACTUAL_DEVICE_INDEX 1
79
Jeff Brownf2f487182010-10-01 17:46:21 -070080#define INDENT " "
81#define INDENT2 " "
82#define INDENT3 " "
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084namespace android {
85
86static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080087static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89/* return the larger integer */
90static inline int max(int v1, int v2)
91{
92 return (v1 > v2) ? v1 : v2;
93}
94
Jeff Brownf2f487182010-10-01 17:46:21 -070095static inline const char* toString(bool value) {
96 return value ? "true" : "false";
97}
98
Jeff Brown90655042010-12-02 13:50:46 -080099// --- EventHub::Device ---
100
101EventHub::Device::Device(int fd, int32_t id, const String8& path,
102 const InputDeviceIdentifier& identifier) :
103 next(NULL),
104 fd(fd), id(id), path(path), identifier(identifier),
Jeff Browncc0c1592011-02-19 05:07:28 -0800105 classes(0), keyBitmask(NULL), relBitmask(NULL),
106 configuration(NULL), virtualKeyMap(NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107}
108
Jeff Brown90655042010-12-02 13:50:46 -0800109EventHub::Device::~Device() {
110 close();
111 delete[] keyBitmask;
Jeff Browncc0c1592011-02-19 05:07:28 -0800112 delete[] relBitmask;
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 Brown1a84fd12011-06-02 01:26:32 -0700131 mNeedToReopenDevices(0), mNeedToScanDevices(false),
Jeff Browndbf8d272011-03-18 18:14:26 -0700132 mInputFdIndex(1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Browndbf8d272011-03-18 18:14:26 -0700134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 memset(mSwitches, 0, sizeof(mSwitches));
Jeff Browndbf8d272011-03-18 18:14:26 -0700136 mNumCpus = sysconf(_SC_NPROCESSORS_ONLN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137}
138
Jeff Brown90655042010-12-02 13:50:46 -0800139EventHub::~EventHub(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 release_wake_lock(WAKE_LOCK_ID);
141 // we should free stuff here...
142}
143
Jeff Brown90655042010-12-02 13:50:46 -0800144status_t EventHub::errorCheck() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 return mError;
146}
147
Jeff Brown90655042010-12-02 13:50:46 -0800148String8 EventHub::getDeviceName(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800150 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 if (device == NULL) return String8();
Jeff Brown90655042010-12-02 13:50:46 -0800152 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153}
154
Jeff Brown90655042010-12-02 13:50:46 -0800155uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800157 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 if (device == NULL) return 0;
159 return device->classes;
160}
161
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800162void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800163 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800164 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800165 if (device && device->configuration) {
166 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800167 } else {
168 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800169 }
170}
171
Jeff Brown6d0fec22010-07-23 21:28:06 -0700172status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
173 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700174 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800177 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 if (device == NULL) return -1;
179
180 struct input_absinfo info;
181
Jens Gulinc4554b92010-06-22 22:21:57 +0200182 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700183 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jeff Brown90655042010-12-02 13:50:46 -0800184 axis, device->identifier.name.string(), device->fd);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700185 return -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700187
188 if (info.minimum != info.maximum) {
189 outAxisInfo->valid = true;
190 outAxisInfo->minValue = info.minimum;
191 outAxisInfo->maxValue = info.maximum;
192 outAxisInfo->flat = info.flat;
193 outAxisInfo->fuzz = info.fuzz;
194 }
195 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196}
197
Jeff Browncc0c1592011-02-19 05:07:28 -0800198bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
199 if (axis >= 0 && axis <= REL_MAX) {
200 AutoMutex _l(mLock);
201
202 Device* device = getDeviceLocked(deviceId);
203 if (device && device->relBitmask) {
204 return test_bit(axis, device->relBitmask);
205 }
206 }
207 return false;
208}
209
Jeff Brown6d0fec22010-07-23 21:28:06 -0700210int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700211 if (scanCode >= 0 && scanCode <= KEY_MAX) {
212 AutoMutex _l(mLock);
213
Jeff Brown90655042010-12-02 13:50:46 -0800214 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700215 if (device != NULL) {
216 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
218 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700219 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220}
221
Jeff Brown90655042010-12-02 13:50:46 -0800222int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700223 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700224 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200225 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700226 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700227 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700228 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700229 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230}
231
Jeff Brown6d0fec22010-07-23 21:28:06 -0700232int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
233 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234
Jeff Brown90655042010-12-02 13:50:46 -0800235 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700236 if (device != NULL) {
237 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700239 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240}
241
Jeff Brown90655042010-12-02 13:50:46 -0800242int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const {
243 if (!device->keyMap.haveKeyLayout()) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800244 return AKEY_STATE_UNKNOWN;
245 }
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800248 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700249
Jeff Brownfd0358292010-06-30 16:10:35 -0700250 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200252 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 #if 0
254 for (size_t i=0; i<=KEY_MAX; i++) {
255 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
256 }
257 #endif
258 const size_t N = scanCodes.size();
259 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
260 int32_t sc = scanCodes.itemAt(i);
261 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
262 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700263 return AKEY_STATE_DOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700266 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700268 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700269}
270
Jeff Brown6d0fec22010-07-23 21:28:06 -0700271int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700272 if (sw >= 0 && sw <= SW_MAX) {
273 AutoMutex _l(mLock);
274
Jeff Brown90655042010-12-02 13:50:46 -0800275 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700276 if (device != NULL) {
277 return getSwitchStateLocked(device, sw);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700278 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700279 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700280 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700281}
282
Jeff Brown90655042010-12-02 13:50:46 -0800283int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700284 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700285 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200286 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700287 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700288 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700289 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700290 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291}
292
Jeff Brown6d0fec22010-07-23 21:28:06 -0700293bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
294 const int32_t* keyCodes, uint8_t* outFlags) const {
295 AutoMutex _l(mLock);
296
Jeff Brown90655042010-12-02 13:50:46 -0800297 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700298 if (device != NULL) {
299 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
300 }
301 return false;
302}
303
Jeff Brown90655042010-12-02 13:50:46 -0800304bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700305 const int32_t* keyCodes, uint8_t* outFlags) const {
Jeff Brown90655042010-12-02 13:50:46 -0800306 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700307 return false;
308 }
309
310 Vector<int32_t> scanCodes;
311 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
312 scanCodes.clear();
313
Jeff Brown6f2fba42011-02-19 01:08:02 -0800314 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
315 keyCodes[codeIndex], &scanCodes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700316 if (! err) {
317 // check the possible scan codes identified by the layout map against the
318 // map of codes actually emitted by the driver
319 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
320 if (test_bit(scanCodes[sc], device->keyBitmask)) {
321 outFlags[codeIndex] = 1;
322 break;
323 }
324 }
325 }
326 }
327 return true;
328}
329
Jeff Brown6f2fba42011-02-19 01:08:02 -0800330status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700331 int32_t* outKeycode, uint32_t* outFlags) const
332{
333 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800334 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700335
Jeff Brown90655042010-12-02 13:50:46 -0800336 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800337 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700338 if (err == NO_ERROR) {
339 return NO_ERROR;
340 }
341 }
342
Jeff Brown90655042010-12-02 13:50:46 -0800343 if (mBuiltInKeyboardId != -1) {
344 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700345
Jeff Brown90655042010-12-02 13:50:46 -0800346 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800347 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700348 if (err == NO_ERROR) {
349 return NO_ERROR;
350 }
351 }
352 }
353
354 *outKeycode = 0;
355 *outFlags = 0;
356 return NAME_NOT_FOUND;
357}
358
Jeff Brown85297452011-03-04 13:07:49 -0800359status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800360{
361 AutoMutex _l(mLock);
362 Device* device = getDeviceLocked(deviceId);
363
364 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800365 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800366 if (err == NO_ERROR) {
367 return NO_ERROR;
368 }
369 }
370
371 if (mBuiltInKeyboardId != -1) {
372 device = getDeviceLocked(mBuiltInKeyboardId);
373
374 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800375 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800376 if (err == NO_ERROR) {
377 return NO_ERROR;
378 }
379 }
380 }
381
Jeff Brown6f2fba42011-02-19 01:08:02 -0800382 return NAME_NOT_FOUND;
383}
384
Jeff Brown1a84fd12011-06-02 01:26:32 -0700385void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700386 AutoMutex _l(mLock);
387
Jeff Brown1a84fd12011-06-02 01:26:32 -0700388 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400389}
390
Jeff Brown497a92c2010-09-12 17:55:08 -0700391bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
392 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800393 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700394 if (device) {
395 uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)];
396 memset(bitmask, 0, sizeof(bitmask));
397 if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) {
398 if (test_bit(led, bitmask)) {
399 return true;
400 }
401 }
402 }
403 return false;
404}
405
406void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
407 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800408 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700409 if (device) {
410 struct input_event ev;
411 ev.time.tv_sec = 0;
412 ev.time.tv_usec = 0;
413 ev.type = EV_LED;
414 ev.code = led;
415 ev.value = on ? 1 : 0;
416
417 ssize_t nWrite;
418 do {
419 nWrite = write(device->fd, &ev, sizeof(struct input_event));
420 } while (nWrite == -1 && errno == EINTR);
421 }
422}
423
Jeff Brown90655042010-12-02 13:50:46 -0800424void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
425 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
426 outVirtualKeys.clear();
427
428 AutoMutex _l(mLock);
429 Device* device = getDeviceLocked(deviceId);
430 if (device && device->virtualKeyMap) {
431 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
432 }
433}
434
435EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
436 if (deviceId == 0) {
437 deviceId = mBuiltInKeyboardId;
438 }
439
440 size_t numDevices = mDevices.size();
441 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < numDevices; i++) {
442 Device* device = mDevices[i];
443 if (device->id == deviceId) {
444 return device;
445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447 return NULL;
448}
449
Jeff Browndbf8d272011-03-18 18:14:26 -0700450size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
451 // Note that we only allow one caller to getEvents(), so don't need
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 // to do locking here... only when adding/removing devices.
Jeff Browndbf8d272011-03-18 18:14:26 -0700453 assert(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400454
455 if (!mOpened) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700456 android_atomic_acquire_store(0, &mNeedToReopenDevices);
457
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400458 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
459 mOpened = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700460 mNeedToScanDevices = true;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400461 }
462
Jeff Browndbf8d272011-03-18 18:14:26 -0700463 struct input_event readBuffer[bufferSize];
464
465 RawEvent* event = buffer;
466 size_t capacity = bufferSize;
Jeff Browncc2e7172010-08-17 16:48:25 -0700467 for (;;) {
Jeff Browndbf8d272011-03-18 18:14:26 -0700468 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
469
Jeff Brown1a84fd12011-06-02 01:26:32 -0700470 // Reopen input devices if needed.
471 if (android_atomic_acquire_load(&mNeedToReopenDevices)) {
472 android_atomic_acquire_store(0, &mNeedToReopenDevices);
473
474 LOGI("Reopening all input devices due to a configuration change.");
475
476 AutoMutex _l(mLock);
477 while (mDevices.size() > 1) {
478 closeDeviceAtIndexLocked(mDevices.size() - 1);
479 }
480 mNeedToScanDevices = true;
481 break; // return to the caller before we actually rescan
482 }
483
Jeff Browncc2e7172010-08-17 16:48:25 -0700484 // Report any devices that had last been added/removed.
Jeff Browndbf8d272011-03-18 18:14:26 -0700485 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800486 Device* device = mClosingDevices;
487 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 device->id, device->path.string());
489 mClosingDevices = device->next;
Jeff Browndbf8d272011-03-18 18:14:26 -0700490 event->when = now;
491 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
492 event->type = DEVICE_REMOVED;
493 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700495 mNeedToSendFinishedDeviceScan = true;
Jeff Browndbf8d272011-03-18 18:14:26 -0700496 if (--capacity == 0) {
497 break;
498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700500
Jeff Brown1a84fd12011-06-02 01:26:32 -0700501 if (mNeedToScanDevices) {
502 mNeedToScanDevices = false;
503 scanDevices();
504 mNeedToSendFinishedDeviceScan = true;
505 }
506
Jeff Browndbf8d272011-03-18 18:14:26 -0700507 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800508 Device* device = mOpeningDevices;
509 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 device->id, device->path.string());
511 mOpeningDevices = device->next;
Jeff Browndbf8d272011-03-18 18:14:26 -0700512 event->when = now;
513 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
514 event->type = DEVICE_ADDED;
515 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700516 mNeedToSendFinishedDeviceScan = true;
Jeff Browndbf8d272011-03-18 18:14:26 -0700517 if (--capacity == 0) {
518 break;
519 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700520 }
521
522 if (mNeedToSendFinishedDeviceScan) {
523 mNeedToSendFinishedDeviceScan = false;
Jeff Browndbf8d272011-03-18 18:14:26 -0700524 event->when = now;
525 event->type = FINISHED_DEVICE_SCAN;
526 event += 1;
527 if (--capacity == 0) {
528 break;
529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531
Jeff Browncc2e7172010-08-17 16:48:25 -0700532 // Grab the next input event.
Jeff Browndbf8d272011-03-18 18:14:26 -0700533 // mInputFdIndex is initially 1 because index 0 is used for inotify.
Jeff Brown33bbfd22011-02-24 20:55:35 -0800534 bool deviceWasRemoved = false;
Jeff Browndbf8d272011-03-18 18:14:26 -0700535 while (mInputFdIndex < mFds.size()) {
Jeff Brown90655042010-12-02 13:50:46 -0800536 const struct pollfd& pfd = mFds[mInputFdIndex];
Jeff Browncc2e7172010-08-17 16:48:25 -0700537 if (pfd.revents & POLLIN) {
Jeff Browndbf8d272011-03-18 18:14:26 -0700538 int32_t readSize = read(pfd.fd, readBuffer, sizeof(struct input_event) * capacity);
Jeff Browncc2e7172010-08-17 16:48:25 -0700539 if (readSize < 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -0800540 if (errno == ENODEV) {
541 deviceWasRemoved = true;
542 break;
543 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700544 if (errno != EAGAIN && errno != EINTR) {
545 LOGW("could not get event (errno=%d)", errno);
546 }
547 } else if ((readSize % sizeof(struct input_event)) != 0) {
548 LOGE("could not get event (wrong size: %d)", readSize);
Jeff Browndbf8d272011-03-18 18:14:26 -0700549 } else if (readSize == 0) { // eof
550 deviceWasRemoved = true;
551 break;
Jeff Browncc2e7172010-08-17 16:48:25 -0700552 } else {
Jeff Browndbf8d272011-03-18 18:14:26 -0700553 const Device* device = mDevices[mInputFdIndex];
554 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
555
556 size_t count = size_t(readSize) / sizeof(struct input_event);
557 for (size_t i = 0; i < count; i++) {
558 const struct input_event& iev = readBuffer[i];
559 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
560 device->path.string(),
561 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
562 iev.type, iev.code, iev.value);
563
Jeff Brown5ced76a2011-05-24 11:23:27 -0700564#ifdef HAVE_POSIX_CLOCKS
565 // Use the time specified in the event instead of the current time
566 // so that downstream code can get more accurate estimates of
567 // event dispatch latency from the time the event is enqueued onto
568 // the evdev client buffer.
569 //
570 // The event's timestamp fortuitously uses the same monotonic clock
571 // time base as the rest of Android. The kernel event device driver
572 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
573 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
574 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
575 // system call that also queries ktime_get_ts().
576 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
577 + nsecs_t(iev.time.tv_usec) * 1000LL;
578 LOGV("event time %lld, now %lld", event->when, now);
579#else
Jeff Browndbf8d272011-03-18 18:14:26 -0700580 event->when = now;
Jeff Brown5ced76a2011-05-24 11:23:27 -0700581#endif
Jeff Browndbf8d272011-03-18 18:14:26 -0700582 event->deviceId = deviceId;
583 event->type = iev.type;
584 event->scanCode = iev.code;
585 event->value = iev.value;
586 event->keyCode = AKEYCODE_UNKNOWN;
587 event->flags = 0;
588 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
589 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
590 &event->keyCode, &event->flags);
591 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
592 iev.code, event->keyCode, event->flags, err);
593 }
594 event += 1;
595 }
596 capacity -= count;
597 if (capacity == 0) {
598 break;
599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601 }
Jeff Browndbf8d272011-03-18 18:14:26 -0700602 mInputFdIndex += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700604
Jeff Brown33bbfd22011-02-24 20:55:35 -0800605 // Handle the case where a device has been removed but INotify has not yet noticed.
606 if (deviceWasRemoved) {
607 AutoMutex _l(mLock);
608 closeDeviceAtIndexLocked(mInputFdIndex);
609 continue; // report added or removed devices immediately
610 }
611
Jeff Browna9b84222010-10-14 02:23:43 -0700612#if HAVE_INOTIFY
Jeff Brown7342bb92010-10-01 18:55:43 -0700613 // readNotify() will modify mFDs and mFDCount, so this must be done after
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 // processing all other events.
Jeff Brown90655042010-12-02 13:50:46 -0800615 if(mFds[0].revents & POLLIN) {
616 readNotify(mFds[0].fd);
617 mFds.editItemAt(0).revents = 0;
Jeff Browndbf8d272011-03-18 18:14:26 -0700618 mInputFdIndex = mFds.size();
Jeff Browna9b84222010-10-14 02:23:43 -0700619 continue; // report added or removed devices immediately
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
Jeff Browna9b84222010-10-14 02:23:43 -0700621#endif
622
Jeff Browndbf8d272011-03-18 18:14:26 -0700623 // Return now if we have collected any events, otherwise poll.
624 if (event != buffer) {
625 break;
626 }
627
Jeff Browncc2e7172010-08-17 16:48:25 -0700628 // Poll for events. Mind the wake lock dance!
629 // We hold a wake lock at all times except during poll(). This works due to some
630 // subtle choreography. When a device driver has pending (unread) events, it acquires
631 // a kernel wake lock. However, once the last pending event has been read, the device
632 // driver will release the kernel wake lock. To prevent the system from going to sleep
633 // when this happens, the EventHub holds onto its own user wake lock while the client
634 // is processing events. Thus the system can only sleep if there are no events
635 // pending or currently being processed.
Jeff Brown68d60752011-03-17 01:34:19 -0700636 //
637 // The timeout is advisory only. If the device is asleep, it will not wake just to
638 // service the timeout.
Jeff Browncc2e7172010-08-17 16:48:25 -0700639 release_wake_lock(WAKE_LOCK_ID);
640
Jeff Brown68d60752011-03-17 01:34:19 -0700641 int pollResult = poll(mFds.editArray(), mFds.size(), timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700642
643 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
644
Jeff Brown68d60752011-03-17 01:34:19 -0700645 if (pollResult == 0) {
Jeff Browndbf8d272011-03-18 18:14:26 -0700646 break; // timed out
Jeff Brown68d60752011-03-17 01:34:19 -0700647 }
648 if (pollResult < 0) {
Jeff Browndbf8d272011-03-18 18:14:26 -0700649 // Sleep after errors to avoid locking up the system.
650 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700651 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700652 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700653 usleep(100000);
654 }
Jeff Browndbf8d272011-03-18 18:14:26 -0700655 } else {
656 // On an SMP system, it is possible for the framework to read input events
657 // faster than the kernel input device driver can produce a complete packet.
658 // Because poll() wakes up as soon as the first input event becomes available,
659 // the framework will often end up reading one event at a time until the
660 // packet is complete. Instead of one call to read() returning 71 events,
661 // it could take 71 calls to read() each returning 1 event.
662 //
663 // Sleep for a short period of time after waking up from the poll() to give
664 // the kernel time to finish writing the entire packet of input events.
665 if (mNumCpus > 1) {
666 usleep(250);
667 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700668 }
Jeff Brown33bbfd22011-02-24 20:55:35 -0800669
670 // Prepare to process all of the FDs we just polled.
Jeff Browndbf8d272011-03-18 18:14:26 -0700671 mInputFdIndex = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 }
Jeff Browndbf8d272011-03-18 18:14:26 -0700673
674 // All done, return the number of events we read.
675 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676}
677
678/*
679 * Open the platform-specific input device.
680 */
Jeff Brown90655042010-12-02 13:50:46 -0800681bool EventHub::openPlatformInput(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 /*
683 * Open platform-specific input device(s).
684 */
Jeff Brown90655042010-12-02 13:50:46 -0800685 int res, fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687#ifdef HAVE_INOTIFY
Jeff Brown90655042010-12-02 13:50:46 -0800688 fd = inotify_init();
689 res = inotify_add_watch(fd, DEVICE_PATH, IN_DELETE | IN_CREATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800691 LOGE("could not add watch for %s, %s\n", DEVICE_PATH, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693#else
694 /*
695 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
696 * We allocate space for it and set it to something invalid.
697 */
Jeff Brown90655042010-12-02 13:50:46 -0800698 fd = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699#endif
700
Jeff Brown90655042010-12-02 13:50:46 -0800701 // Reserve fd index 0 for inotify.
702 struct pollfd pollfd;
703 pollfd.fd = fd;
704 pollfd.events = POLLIN;
705 pollfd.revents = 0;
706 mFds.push(pollfd);
707 mDevices.push(NULL);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700708 return true;
709}
Jeff Brown90655042010-12-02 13:50:46 -0800710
Jeff Brown1a84fd12011-06-02 01:26:32 -0700711void EventHub::scanDevices() {
712 int res = scanDir(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800714 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716}
717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718// ----------------------------------------------------------------------------
719
Jeff Brownfd0358292010-06-30 16:10:35 -0700720static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
721 const uint8_t* end = array + endIndex;
722 array += startIndex;
723 while (array != end) {
724 if (*(array++) != 0) {
725 return true;
726 }
727 }
728 return false;
729}
730
731static const int32_t GAMEPAD_KEYCODES[] = {
732 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
733 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
734 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
735 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
736 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800737 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
738 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
739 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
740 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
741 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -0700742};
743
Jeff Brown90655042010-12-02 13:50:46 -0800744int EventHub::openDevice(const char *devicePath) {
745 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746
Jeff Brown90655042010-12-02 13:50:46 -0800747 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748
749 AutoMutex _l(mLock);
Nick Pellye6b1bbd2010-01-20 19:36:49 -0800750
Jeff Brown90655042010-12-02 13:50:46 -0800751 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800753 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 return -1;
755 }
756
Jeff Brown90655042010-12-02 13:50:46 -0800757 InputDeviceIdentifier identifier;
758
759 // Get device name.
760 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
761 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
762 } else {
763 buffer[sizeof(buffer) - 1] = '\0';
764 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400766
Jeff Brown90655042010-12-02 13:50:46 -0800767 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700768 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
769 const String8& item = mExcludedDevices.itemAt(i);
770 if (identifier.name == item) {
771 LOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400772 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400773 return -1;
774 }
775 }
776
Jeff Brown90655042010-12-02 13:50:46 -0800777 // Get device driver version.
778 int driverVersion;
779 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
780 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
781 close(fd);
782 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784
Jeff Brown90655042010-12-02 13:50:46 -0800785 // Get device identifier.
786 struct input_id inputId;
787 if(ioctl(fd, EVIOCGID, &inputId)) {
788 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
789 close(fd);
790 return -1;
791 }
792 identifier.bus = inputId.bustype;
793 identifier.product = inputId.product;
794 identifier.vendor = inputId.vendor;
795 identifier.version = inputId.version;
796
797 // Get device physical location.
798 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
799 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
800 } else {
801 buffer[sizeof(buffer) - 1] = '\0';
802 identifier.location.setTo(buffer);
803 }
804
805 // Get device unique id.
806 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
807 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
808 } else {
809 buffer[sizeof(buffer) - 1] = '\0';
810 identifier.uniqueId.setTo(buffer);
811 }
812
813 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700814 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
815 LOGE("Error %d making device file descriptor non-blocking.", errno);
816 close(fd);
817 return -1;
818 }
819
Jeff Brown90655042010-12-02 13:50:46 -0800820 // Allocate device. (The device object takes ownership of the fd at this point.)
821 int32_t deviceId = mNextDeviceId++;
822 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
824#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800825 LOGI("add device %d: %s\n", deviceId, devicePath);
826 LOGI(" bus: %04x\n"
827 " vendor %04x\n"
828 " product %04x\n"
829 " version %04x\n",
830 identifier.bus, identifier.vendor, identifier.product, identifier.version);
831 LOGI(" name: \"%s\"\n", identifier.name.string());
832 LOGI(" location: \"%s\"\n", identifier.location.string());
833 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
834 LOGI(" driver: v%d.%d.%d\n",
835 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836#endif
837
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800838 // Load the configuration file for the device.
839 loadConfiguration(device);
840
Jeff Brownfd0358292010-06-30 16:10:35 -0700841 // Figure out the kinds of events the device reports.
Jeff Brownfd0358292010-06-30 16:10:35 -0700842 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800844 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700845
Jeff Brown6f2fba42011-02-19 01:08:02 -0800846 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
847 memset(abs_bitmask, 0, sizeof(abs_bitmask));
848 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700849
Jeff Brown6f2fba42011-02-19 01:08:02 -0800850 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
851 memset(rel_bitmask, 0, sizeof(rel_bitmask));
852 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700853
Jeff Brown6f2fba42011-02-19 01:08:02 -0800854 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
855 memset(sw_bitmask, 0, sizeof(sw_bitmask));
856 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask);
857
Jeff Browncc0c1592011-02-19 05:07:28 -0800858 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
859 if (device->keyBitmask != NULL) {
860 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
861 } else {
862 delete device;
863 LOGE("out of memory allocating key bitmask");
864 return -1;
865 }
866
867 device->relBitmask = new uint8_t[sizeof(rel_bitmask)];
868 if (device->relBitmask != NULL) {
869 memcpy(device->relBitmask, rel_bitmask, sizeof(rel_bitmask));
870 } else {
871 delete device;
872 LOGE("out of memory allocating rel bitmask");
873 return -1;
874 }
875
Jeff Brown6f2fba42011-02-19 01:08:02 -0800876 // See if this is a keyboard. Ignore everything in the button range except for
877 // joystick and gamepad buttons which are handled like keyboards for the most part.
878 bool haveKeyboardKeys = containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
879 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
880 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800881 bool haveGamepadButtons = containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_MISC),
882 sizeof_bit_array(BTN_MOUSE))
883 || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_JOYSTICK),
884 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800885 if (haveKeyboardKeys || haveGamepadButtons) {
886 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800888
Jeff Brown83c09682010-12-23 17:50:18 -0800889 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800890 if (test_bit(BTN_MOUSE, key_bitmask)
891 && test_bit(REL_X, rel_bitmask)
892 && test_bit(REL_Y, rel_bitmask)) {
893 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
Jeff Brownfd0358292010-06-30 16:10:35 -0700895
896 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800897 // Is this a new modern multi-touch driver?
898 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
899 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
900 // Some joysticks such as the PS3 controller report axes that conflict
901 // with the ABS_MT range. Try to confirm that the device really is
902 // a touch screen.
903 if (test_bit(BTN_TOUCH, key_bitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800904 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -0700905 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800906 // Is this an old style single-touch driver?
907 } else if (test_bit(BTN_TOUCH, key_bitmask)
908 && test_bit(ABS_X, abs_bitmask)
909 && test_bit(ABS_Y, abs_bitmask)) {
910 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
912
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800913 // See if this device is a joystick.
914 // Ignore touchscreens because they use the same absolute axes for other purposes.
915 // Assumes that joysticks always have gamepad buttons in order to distinguish them
916 // from other devices such as accelerometers that also have absolute axes.
917 if (haveGamepadButtons
918 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)
919 && containsNonZeroByte(abs_bitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
920 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
921 }
922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // figure out the switches this device reports
Jeff Brown6f2fba42011-02-19 01:08:02 -0800924 bool haveSwitches = false;
925 for (int i=0; i<EV_SW; i++) {
926 //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
927 if (test_bit(i, sw_bitmask)) {
928 haveSwitches = true;
929 if (mSwitches[i] == 0) {
930 mSwitches[i] = device->id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
932 }
933 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800934 if (haveSwitches) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700935 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937
Jeff Brown58a2da82011-01-25 16:02:22 -0800938 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800939 // Load the virtual keys for the touch screen, if any.
940 // We do this now so that we can make sure to load the keymap if necessary.
941 status_t status = loadVirtualKeyMap(device);
942 if (!status) {
943 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
Jeff Brown90655042010-12-02 13:50:46 -0800945 }
946
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800947 // Load the key map.
948 // We need to do this for joysticks too because the key layout may specify axes.
949 status_t keyMapStatus = NAME_NOT_FOUND;
950 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -0800951 // Load the keymap for the device.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800952 keyMapStatus = loadKeyMap(device);
953 }
Jeff Brown90655042010-12-02 13:50:46 -0800954
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800955 // Configure the keyboard, gamepad or virtual keyboard.
956 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -0800957 // Set system properties for the keyboard.
Jeff Brown497a92c2010-09-12 17:55:08 -0700958 setKeyboardProperties(device, false);
959
Jeff Brown90655042010-12-02 13:50:46 -0800960 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800961 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -0800962 && mBuiltInKeyboardId == -1
963 && isEligibleBuiltInKeyboard(device->identifier,
964 device->configuration, &device->keyMap)) {
965 mBuiltInKeyboardId = device->id;
966 setKeyboardProperties(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -0700967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700969 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -0700970 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700971 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700972 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700973
Jeff Brownfd0358292010-06-30 16:10:35 -0700974 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -0700975 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
976 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
977 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
978 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
979 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700980 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700981 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700982
Jeff Brownfd0358292010-06-30 16:10:35 -0700983 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -0700984 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700985 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -0700986 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
987 break;
988 }
989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
Sean McNeilaeb00c42010-06-23 16:00:37 +0700992 // If the device isn't recognized as something we handle, don't monitor it.
993 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800994 LOGV("Dropping device: id=%d, path='%s', name='%s'",
995 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +0700996 delete device;
997 return -1;
998 }
999
Jeff Brown56194eb2011-03-02 19:23:13 -08001000 // Determine whether the device is external or internal.
1001 if (isExternalDevice(device)) {
1002 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1003 }
1004
Jeff Brown90655042010-12-02 13:50:46 -08001005 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1006 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
1007 deviceId, fd, devicePath, device->identifier.name.string(),
1008 device->classes,
1009 device->configurationFile.string(),
1010 device->keyMap.keyLayoutFile.string(),
1011 device->keyMap.keyCharacterMapFile.string(),
1012 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001013
Jeff Brown90655042010-12-02 13:50:46 -08001014 struct pollfd pollfd;
1015 pollfd.fd = fd;
1016 pollfd.events = POLLIN;
1017 pollfd.revents = 0;
1018 mFds.push(pollfd);
1019 mDevices.push(device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 device->next = mOpeningDevices;
1022 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 return 0;
1024}
1025
Jeff Brown90655042010-12-02 13:50:46 -08001026void EventHub::loadConfiguration(Device* device) {
1027 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1028 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001029 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -08001030 LOGD("No input device configuration file found for device '%s'.",
1031 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001032 } else {
1033 status_t status = PropertyMap::load(device->configurationFile,
1034 &device->configuration);
1035 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -08001036 LOGE("Error loading input device configuration file for device '%s'. "
1037 "Using default configuration.",
1038 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001039 }
1040 }
1041}
1042
Jeff Brown90655042010-12-02 13:50:46 -08001043status_t EventHub::loadVirtualKeyMap(Device* device) {
1044 // The virtual key map is supplied by the kernel as a system board property file.
1045 String8 path;
1046 path.append("/sys/board_properties/virtualkeys.");
1047 path.append(device->identifier.name);
1048 if (access(path.string(), R_OK)) {
1049 return NAME_NOT_FOUND;
1050 }
1051 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001052}
1053
Jeff Brown90655042010-12-02 13:50:46 -08001054status_t EventHub::loadKeyMap(Device* device) {
1055 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001056}
1057
Jeff Brown90655042010-12-02 13:50:46 -08001058void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) {
1059 int32_t id = builtInKeyboard ? 0 : device->id;
1060 android::setKeyboardProperties(id, device->identifier,
1061 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1062}
1063
1064void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) {
1065 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001066 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -07001067}
1068
Jeff Brown56194eb2011-03-02 19:23:13 -08001069bool EventHub::isExternalDevice(Device* device) {
1070 if (device->configuration) {
1071 bool value;
1072 if (device->configuration->tryGetProperty(String8("device.internal"), value)
1073 && value) {
1074 return false;
1075 }
1076 }
1077 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1078}
1079
Jeff Brown90655042010-12-02 13:50:46 -08001080bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1081 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001082 return false;
1083 }
1084
1085 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001086 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001087 const size_t N = scanCodes.size();
1088 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1089 int32_t sc = scanCodes.itemAt(i);
1090 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1091 return true;
1092 }
1093 }
1094
1095 return false;
1096}
1097
Jeff Brown90655042010-12-02 13:50:46 -08001098int EventHub::closeDevice(const char *devicePath) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 AutoMutex _l(mLock);
Jeff Brown7342bb92010-10-01 18:55:43 -07001100
Jeff Brown90655042010-12-02 13:50:46 -08001101 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1102 Device* device = mDevices[i];
1103 if (device->path == devicePath) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001104 return closeDeviceAtIndexLocked(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001107 LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 return -1;
1109}
1110
Jeff Brown33bbfd22011-02-24 20:55:35 -08001111int EventHub::closeDeviceAtIndexLocked(int index) {
1112 Device* device = mDevices[index];
1113 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1114 device->path.string(), device->identifier.name.string(), device->id,
1115 device->fd, device->classes);
1116
1117 for (int j=0; j<EV_SW; j++) {
1118 if (mSwitches[j] == device->id) {
1119 mSwitches[j] = 0;
1120 }
1121 }
1122
1123 if (device->id == mBuiltInKeyboardId) {
1124 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
1125 device->path.string(), mBuiltInKeyboardId);
1126 mBuiltInKeyboardId = -1;
1127 clearKeyboardProperties(device, true);
1128 }
1129 clearKeyboardProperties(device, false);
1130
1131 mFds.removeAt(index);
1132 mDevices.removeAt(index);
1133 device->close();
1134
Jeff Brown8e9d4432011-03-12 19:46:59 -08001135 // Unlink for opening devices list if it is present.
1136 Device* pred = NULL;
1137 bool found = false;
1138 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1139 if (entry == device) {
1140 found = true;
1141 break;
1142 }
1143 pred = entry;
1144 entry = entry->next;
1145 }
1146 if (found) {
1147 // Unlink the device from the opening devices list then delete it.
1148 // We don't need to tell the client that the device was closed because
1149 // it does not even know it was opened in the first place.
1150 LOGI("Device %s was immediately closed after opening.", device->path.string());
1151 if (pred) {
1152 pred->next = device->next;
1153 } else {
1154 mOpeningDevices = device->next;
1155 }
1156 delete device;
1157 } else {
1158 // Link into closing devices list.
1159 // The device will be deleted later after we have informed the client.
1160 device->next = mClosingDevices;
1161 mClosingDevices = device;
1162 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001163 return 0;
1164}
1165
Jeff Brown7342bb92010-10-01 18:55:43 -07001166int EventHub::readNotify(int nfd) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167#ifdef HAVE_INOTIFY
1168 int res;
1169 char devname[PATH_MAX];
1170 char *filename;
1171 char event_buf[512];
1172 int event_size;
1173 int event_pos = 0;
1174 struct inotify_event *event;
1175
Jeff Brown7342bb92010-10-01 18:55:43 -07001176 LOGV("EventHub::readNotify nfd: %d\n", nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 res = read(nfd, event_buf, sizeof(event_buf));
1178 if(res < (int)sizeof(*event)) {
1179 if(errno == EINTR)
1180 return 0;
1181 LOGW("could not get event, %s\n", strerror(errno));
1182 return 1;
1183 }
1184 //printf("got %d bytes of event information\n", res);
1185
Jeff Brown90655042010-12-02 13:50:46 -08001186 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 filename = devname + strlen(devname);
1188 *filename++ = '/';
1189
1190 while(res >= (int)sizeof(*event)) {
1191 event = (struct inotify_event *)(event_buf + event_pos);
1192 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1193 if(event->len) {
1194 strcpy(filename, event->name);
1195 if(event->mask & IN_CREATE) {
Jeff Brown7342bb92010-10-01 18:55:43 -07001196 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198 else {
Jeff Brown7342bb92010-10-01 18:55:43 -07001199 closeDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 }
1202 event_size = sizeof(*event) + event->len;
1203 res -= event_size;
1204 event_pos += event_size;
1205 }
1206#endif
1207 return 0;
1208}
1209
Jeff Brown7342bb92010-10-01 18:55:43 -07001210int EventHub::scanDir(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211{
1212 char devname[PATH_MAX];
1213 char *filename;
1214 DIR *dir;
1215 struct dirent *de;
1216 dir = opendir(dirname);
1217 if(dir == NULL)
1218 return -1;
1219 strcpy(devname, dirname);
1220 filename = devname + strlen(devname);
1221 *filename++ = '/';
1222 while((de = readdir(dir))) {
1223 if(de->d_name[0] == '.' &&
1224 (de->d_name[1] == '\0' ||
1225 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1226 continue;
1227 strcpy(filename, de->d_name);
Jeff Brown7342bb92010-10-01 18:55:43 -07001228 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 }
1230 closedir(dir);
1231 return 0;
1232}
1233
Jeff Brown1a84fd12011-06-02 01:26:32 -07001234void EventHub::reopenDevices() {
1235 android_atomic_release_store(1, &mNeedToReopenDevices);
1236}
1237
Jeff Brownf2f487182010-10-01 17:46:21 -07001238void EventHub::dump(String8& dump) {
1239 dump.append("Event Hub State:\n");
1240
1241 { // acquire lock
1242 AutoMutex _l(mLock);
1243
Jeff Brown90655042010-12-02 13:50:46 -08001244 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001245
1246 dump.append(INDENT "Devices:\n");
1247
Jeff Brown90655042010-12-02 13:50:46 -08001248 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1249 const Device* device = mDevices[i];
Jeff Brownf2f487182010-10-01 17:46:21 -07001250 if (device) {
Jeff Brown90655042010-12-02 13:50:46 -08001251 if (mBuiltInKeyboardId == device->id) {
1252 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1253 device->id, device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001254 } else {
Jeff Brown90655042010-12-02 13:50:46 -08001255 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1256 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001257 }
1258 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1259 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Brown90655042010-12-02 13:50:46 -08001260 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1261 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1262 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1263 "product=0x%04x, version=0x%04x\n",
1264 device->identifier.bus, device->identifier.vendor,
1265 device->identifier.product, device->identifier.version);
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001266 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001267 device->keyMap.keyLayoutFile.string());
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001268 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001269 device->keyMap.keyCharacterMapFile.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001270 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1271 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001272 }
1273 }
1274 } // release lock
1275}
1276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277}; // namespace android