blob: b31381a3531efc0b8ca0bc6a6566952df15ad2d8 [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 Browncc0c1592011-02-19 05:07:28 -0800104 classes(0), keyBitmask(NULL), relBitmask(NULL),
105 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 Brown47e6b1b2010-11-29 17:37:49 -0800112 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800113 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114}
115
Jeff Brown90655042010-12-02 13:50:46 -0800116void EventHub::Device::close() {
117 if (fd >= 0) {
118 ::close(fd);
119 fd = -1;
120 }
121}
122
123
124// --- EventHub ---
125
126EventHub::EventHub(void) :
127 mError(NO_INIT), mBuiltInKeyboardId(-1), mNextDeviceId(1),
128 mOpeningDevices(0), mClosingDevices(0),
129 mOpened(false), mNeedToSendFinishedDeviceScan(false),
130 mInputBufferIndex(0), mInputBufferCount(0), mInputFdIndex(0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 memset(mSwitches, 0, sizeof(mSwitches));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133}
134
Jeff Brown90655042010-12-02 13:50:46 -0800135EventHub::~EventHub(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 release_wake_lock(WAKE_LOCK_ID);
137 // we should free stuff here...
138}
139
Jeff Brown90655042010-12-02 13:50:46 -0800140status_t EventHub::errorCheck() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return mError;
142}
143
Jeff Brown90655042010-12-02 13:50:46 -0800144String8 EventHub::getDeviceName(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800146 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 if (device == NULL) return String8();
Jeff Brown90655042010-12-02 13:50:46 -0800148 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149}
150
Jeff Brown90655042010-12-02 13:50:46 -0800151uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800153 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 if (device == NULL) return 0;
155 return device->classes;
156}
157
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800158void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800159 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800160 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800161 if (device && device->configuration) {
162 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800163 } else {
164 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800165 }
166}
167
Jeff Brown6d0fec22010-07-23 21:28:06 -0700168status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
169 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700170 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800173 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 if (device == NULL) return -1;
175
176 struct input_absinfo info;
177
Jens Gulinc4554b92010-06-22 22:21:57 +0200178 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700179 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jeff Brown90655042010-12-02 13:50:46 -0800180 axis, device->identifier.name.string(), device->fd);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700181 return -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700183
184 if (info.minimum != info.maximum) {
185 outAxisInfo->valid = true;
186 outAxisInfo->minValue = info.minimum;
187 outAxisInfo->maxValue = info.maximum;
188 outAxisInfo->flat = info.flat;
189 outAxisInfo->fuzz = info.fuzz;
190 }
191 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192}
193
Jeff Browncc0c1592011-02-19 05:07:28 -0800194bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
195 if (axis >= 0 && axis <= REL_MAX) {
196 AutoMutex _l(mLock);
197
198 Device* device = getDeviceLocked(deviceId);
199 if (device && device->relBitmask) {
200 return test_bit(axis, device->relBitmask);
201 }
202 }
203 return false;
204}
205
Jeff Brown6d0fec22010-07-23 21:28:06 -0700206int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700207 if (scanCode >= 0 && scanCode <= KEY_MAX) {
208 AutoMutex _l(mLock);
209
Jeff Brown90655042010-12-02 13:50:46 -0800210 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700211 if (device != NULL) {
212 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 }
214 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700215 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216}
217
Jeff Brown90655042010-12-02 13:50:46 -0800218int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700219 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700220 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200221 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700222 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700223 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700224 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700225 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700226}
227
Jeff Brown6d0fec22010-07-23 21:28:06 -0700228int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
229 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230
Jeff Brown90655042010-12-02 13:50:46 -0800231 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700232 if (device != NULL) {
233 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700235 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236}
237
Jeff Brown90655042010-12-02 13:50:46 -0800238int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const {
239 if (!device->keyMap.haveKeyLayout()) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800240 return AKEY_STATE_UNKNOWN;
241 }
242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800244 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700245
Jeff Brownfd0358292010-06-30 16:10:35 -0700246 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200248 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 #if 0
250 for (size_t i=0; i<=KEY_MAX; i++) {
251 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
252 }
253 #endif
254 const size_t N = scanCodes.size();
255 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
256 int32_t sc = scanCodes.itemAt(i);
257 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
258 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700259 return AKEY_STATE_DOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700262 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700264 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700265}
266
Jeff Brown6d0fec22010-07-23 21:28:06 -0700267int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700268 if (sw >= 0 && sw <= SW_MAX) {
269 AutoMutex _l(mLock);
270
Jeff Brown90655042010-12-02 13:50:46 -0800271 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700272 if (device != NULL) {
273 return getSwitchStateLocked(device, sw);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700274 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700275 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700276 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700277}
278
Jeff Brown90655042010-12-02 13:50:46 -0800279int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700280 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700281 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200282 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700283 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700284 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700285 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700286 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287}
288
Jeff Brown6d0fec22010-07-23 21:28:06 -0700289bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
290 const int32_t* keyCodes, uint8_t* outFlags) const {
291 AutoMutex _l(mLock);
292
Jeff Brown90655042010-12-02 13:50:46 -0800293 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700294 if (device != NULL) {
295 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
296 }
297 return false;
298}
299
Jeff Brown90655042010-12-02 13:50:46 -0800300bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700301 const int32_t* keyCodes, uint8_t* outFlags) const {
Jeff Brown90655042010-12-02 13:50:46 -0800302 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700303 return false;
304 }
305
306 Vector<int32_t> scanCodes;
307 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
308 scanCodes.clear();
309
Jeff Brown6f2fba42011-02-19 01:08:02 -0800310 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
311 keyCodes[codeIndex], &scanCodes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700312 if (! err) {
313 // check the possible scan codes identified by the layout map against the
314 // map of codes actually emitted by the driver
315 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
316 if (test_bit(scanCodes[sc], device->keyBitmask)) {
317 outFlags[codeIndex] = 1;
318 break;
319 }
320 }
321 }
322 }
323 return true;
324}
325
Jeff Brown6f2fba42011-02-19 01:08:02 -0800326status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700327 int32_t* outKeycode, uint32_t* outFlags) const
328{
329 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800330 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700331
Jeff Brown90655042010-12-02 13:50:46 -0800332 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800333 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700334 if (err == NO_ERROR) {
335 return NO_ERROR;
336 }
337 }
338
Jeff Brown90655042010-12-02 13:50:46 -0800339 if (mBuiltInKeyboardId != -1) {
340 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700341
Jeff Brown90655042010-12-02 13:50:46 -0800342 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800343 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700344 if (err == NO_ERROR) {
345 return NO_ERROR;
346 }
347 }
348 }
349
350 *outKeycode = 0;
351 *outFlags = 0;
352 return NAME_NOT_FOUND;
353}
354
Jeff Brown6f2fba42011-02-19 01:08:02 -0800355status_t EventHub::mapAxis(int32_t deviceId, int scancode,
356 int32_t* outAxis) const
357{
358 AutoMutex _l(mLock);
359 Device* device = getDeviceLocked(deviceId);
360
361 if (device && device->keyMap.haveKeyLayout()) {
362 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxis);
363 if (err == NO_ERROR) {
364 return NO_ERROR;
365 }
366 }
367
368 if (mBuiltInKeyboardId != -1) {
369 device = getDeviceLocked(mBuiltInKeyboardId);
370
371 if (device && device->keyMap.haveKeyLayout()) {
372 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxis);
373 if (err == NO_ERROR) {
374 return NO_ERROR;
375 }
376 }
377 }
378
379 *outAxis = -1;
380 return NAME_NOT_FOUND;
381}
382
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400383void EventHub::addExcludedDevice(const char* deviceName)
384{
Jeff Brownf2f487182010-10-01 17:46:21 -0700385 AutoMutex _l(mLock);
386
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400387 String8 name(deviceName);
388 mExcludedDevices.push_back(name);
389}
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 Brown90655042010-12-02 13:50:46 -0800450bool EventHub::getEvent(RawEvent* outEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700451 outEvent->deviceId = 0;
452 outEvent->type = 0;
453 outEvent->scanCode = 0;
454 outEvent->keyCode = 0;
455 outEvent->flags = 0;
456 outEvent->value = 0;
457 outEvent->when = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 // Note that we only allow one caller to getEvent(), so don't need
460 // to do locking here... only when adding/removing devices.
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400461
462 if (!mOpened) {
463 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
464 mOpened = true;
Jeff Brown7342bb92010-10-01 18:55:43 -0700465 mNeedToSendFinishedDeviceScan = true;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400466 }
467
Jeff Browncc2e7172010-08-17 16:48:25 -0700468 for (;;) {
469 // Report any devices that had last been added/removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 if (mClosingDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800471 Device* device = mClosingDevices;
472 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 device->id, device->path.string());
474 mClosingDevices = device->next;
Jeff Brown90655042010-12-02 13:50:46 -0800475 if (device->id == mBuiltInKeyboardId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476 outEvent->deviceId = 0;
477 } else {
478 outEvent->deviceId = device->id;
479 }
480 outEvent->type = DEVICE_REMOVED;
Jeff Brownc3db8582010-10-20 15:33:38 -0700481 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700483 mNeedToSendFinishedDeviceScan = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 return true;
485 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 if (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800488 Device* device = mOpeningDevices;
489 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 device->id, device->path.string());
491 mOpeningDevices = device->next;
Jeff Brown90655042010-12-02 13:50:46 -0800492 if (device->id == mBuiltInKeyboardId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493 outEvent->deviceId = 0;
494 } else {
495 outEvent->deviceId = device->id;
496 }
497 outEvent->type = DEVICE_ADDED;
Jeff Brownc3db8582010-10-20 15:33:38 -0700498 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown7342bb92010-10-01 18:55:43 -0700499 mNeedToSendFinishedDeviceScan = true;
500 return true;
501 }
502
503 if (mNeedToSendFinishedDeviceScan) {
504 mNeedToSendFinishedDeviceScan = false;
505 outEvent->type = FINISHED_DEVICE_SCAN;
Jeff Brownc3db8582010-10-20 15:33:38 -0700506 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 return true;
508 }
509
Jeff Browncc2e7172010-08-17 16:48:25 -0700510 // Grab the next input event.
511 for (;;) {
512 // Consume buffered input events, if any.
513 if (mInputBufferIndex < mInputBufferCount) {
514 const struct input_event& iev = mInputBufferData[mInputBufferIndex++];
Jeff Brown90655042010-12-02 13:50:46 -0800515 const Device* device = mDevices[mInputFdIndex];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
Jeff Browncc2e7172010-08-17 16:48:25 -0700517 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(),
518 (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value);
Jeff Brown90655042010-12-02 13:50:46 -0800519 if (device->id == mBuiltInKeyboardId) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700520 outEvent->deviceId = 0;
521 } else {
522 outEvent->deviceId = device->id;
523 }
524 outEvent->type = iev.type;
525 outEvent->scanCode = iev.code;
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800526 outEvent->flags = 0;
Jeff Browncc2e7172010-08-17 16:48:25 -0700527 if (iev.type == EV_KEY) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800528 outEvent->keyCode = AKEYCODE_UNKNOWN;
Jeff Brown90655042010-12-02 13:50:46 -0800529 if (device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800530 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800531 &outEvent->keyCode, &outEvent->flags);
532 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
533 iev.code, outEvent->keyCode, outEvent->flags, err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700535 } else {
536 outEvent->keyCode = iev.code;
537 }
538 outEvent->value = iev.value;
539
540 // Use an event timestamp in the same timebase as
541 // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis()
542 // as expected by the rest of the system.
543 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
544 return true;
545 }
546
547 // Finish reading all events from devices identified in previous poll().
548 // This code assumes that mInputDeviceIndex is initially 0 and that the
549 // revents member of pollfd is initialized to 0 when the device is first added.
Jeff Brown90655042010-12-02 13:50:46 -0800550 // Since mFds[0] is used for inotify, we process regular events starting at index 1.
551 mInputFdIndex += 1;
552 if (mInputFdIndex >= mFds.size()) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700553 break;
554 }
555
Jeff Brown90655042010-12-02 13:50:46 -0800556 const struct pollfd& pfd = mFds[mInputFdIndex];
Jeff Browncc2e7172010-08-17 16:48:25 -0700557 if (pfd.revents & POLLIN) {
558 int32_t readSize = read(pfd.fd, mInputBufferData,
559 sizeof(struct input_event) * INPUT_BUFFER_SIZE);
560 if (readSize < 0) {
561 if (errno != EAGAIN && errno != EINTR) {
562 LOGW("could not get event (errno=%d)", errno);
563 }
564 } else if ((readSize % sizeof(struct input_event)) != 0) {
565 LOGE("could not get event (wrong size: %d)", readSize);
566 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800567 mInputBufferCount = size_t(readSize) / sizeof(struct input_event);
Jeff Browncc2e7172010-08-17 16:48:25 -0700568 mInputBufferIndex = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
571 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700572
Jeff Browna9b84222010-10-14 02:23:43 -0700573#if HAVE_INOTIFY
Jeff Brown7342bb92010-10-01 18:55:43 -0700574 // readNotify() will modify mFDs and mFDCount, so this must be done after
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 // processing all other events.
Jeff Brown90655042010-12-02 13:50:46 -0800576 if(mFds[0].revents & POLLIN) {
577 readNotify(mFds[0].fd);
578 mFds.editItemAt(0).revents = 0;
Jeff Browna9b84222010-10-14 02:23:43 -0700579 continue; // report added or removed devices immediately
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
Jeff Browna9b84222010-10-14 02:23:43 -0700581#endif
582
Jeff Brown90655042010-12-02 13:50:46 -0800583 mInputFdIndex = 0;
Jeff Browncc2e7172010-08-17 16:48:25 -0700584
585 // Poll for events. Mind the wake lock dance!
586 // We hold a wake lock at all times except during poll(). This works due to some
587 // subtle choreography. When a device driver has pending (unread) events, it acquires
588 // a kernel wake lock. However, once the last pending event has been read, the device
589 // driver will release the kernel wake lock. To prevent the system from going to sleep
590 // when this happens, the EventHub holds onto its own user wake lock while the client
591 // is processing events. Thus the system can only sleep if there are no events
592 // pending or currently being processed.
593 release_wake_lock(WAKE_LOCK_ID);
594
Jeff Brown90655042010-12-02 13:50:46 -0800595 int pollResult = poll(mFds.editArray(), mFds.size(), -1);
Jeff Browncc2e7172010-08-17 16:48:25 -0700596
597 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
598
599 if (pollResult <= 0) {
600 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700601 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700602 usleep(100000);
603 }
604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606}
607
608/*
609 * Open the platform-specific input device.
610 */
Jeff Brown90655042010-12-02 13:50:46 -0800611bool EventHub::openPlatformInput(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 /*
613 * Open platform-specific input device(s).
614 */
Jeff Brown90655042010-12-02 13:50:46 -0800615 int res, fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617#ifdef HAVE_INOTIFY
Jeff Brown90655042010-12-02 13:50:46 -0800618 fd = inotify_init();
619 res = inotify_add_watch(fd, DEVICE_PATH, IN_DELETE | IN_CREATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800621 LOGE("could not add watch for %s, %s\n", DEVICE_PATH, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623#else
624 /*
625 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
626 * We allocate space for it and set it to something invalid.
627 */
Jeff Brown90655042010-12-02 13:50:46 -0800628 fd = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629#endif
630
Jeff Brown90655042010-12-02 13:50:46 -0800631 // Reserve fd index 0 for inotify.
632 struct pollfd pollfd;
633 pollfd.fd = fd;
634 pollfd.events = POLLIN;
635 pollfd.revents = 0;
636 mFds.push(pollfd);
637 mDevices.push(NULL);
638
639 res = scanDir(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800641 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
644 return true;
645}
646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647// ----------------------------------------------------------------------------
648
Jeff Brownfd0358292010-06-30 16:10:35 -0700649static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
650 const uint8_t* end = array + endIndex;
651 array += startIndex;
652 while (array != end) {
653 if (*(array++) != 0) {
654 return true;
655 }
656 }
657 return false;
658}
659
660static const int32_t GAMEPAD_KEYCODES[] = {
661 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
662 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
663 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
664 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
665 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800666 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
667 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
668 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
669 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
670 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -0700671};
672
Jeff Brown90655042010-12-02 13:50:46 -0800673int EventHub::openDevice(const char *devicePath) {
674 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675
Jeff Brown90655042010-12-02 13:50:46 -0800676 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
678 AutoMutex _l(mLock);
Nick Pellye6b1bbd2010-01-20 19:36:49 -0800679
Jeff Brown90655042010-12-02 13:50:46 -0800680 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800682 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 return -1;
684 }
685
Jeff Brown90655042010-12-02 13:50:46 -0800686 InputDeviceIdentifier identifier;
687
688 // Get device name.
689 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
690 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
691 } else {
692 buffer[sizeof(buffer) - 1] = '\0';
693 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400695
Jeff Brown90655042010-12-02 13:50:46 -0800696 // Check to see if the device is on our excluded list
Mike Lockwood15431a92009-07-17 00:10:10 -0400697 List<String8>::iterator iter = mExcludedDevices.begin();
698 List<String8>::iterator end = mExcludedDevices.end();
699 for ( ; iter != end; iter++) {
700 const char* test = *iter;
Jeff Brown90655042010-12-02 13:50:46 -0800701 if (identifier.name == test) {
702 LOGI("ignoring event id %s driver %s\n", devicePath, test);
Mike Lockwood15431a92009-07-17 00:10:10 -0400703 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400704 return -1;
705 }
706 }
707
Jeff Brown90655042010-12-02 13:50:46 -0800708 // Get device driver version.
709 int driverVersion;
710 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
711 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
712 close(fd);
713 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715
Jeff Brown90655042010-12-02 13:50:46 -0800716 // Get device identifier.
717 struct input_id inputId;
718 if(ioctl(fd, EVIOCGID, &inputId)) {
719 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
720 close(fd);
721 return -1;
722 }
723 identifier.bus = inputId.bustype;
724 identifier.product = inputId.product;
725 identifier.vendor = inputId.vendor;
726 identifier.version = inputId.version;
727
728 // Get device physical location.
729 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
730 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
731 } else {
732 buffer[sizeof(buffer) - 1] = '\0';
733 identifier.location.setTo(buffer);
734 }
735
736 // Get device unique id.
737 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
738 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
739 } else {
740 buffer[sizeof(buffer) - 1] = '\0';
741 identifier.uniqueId.setTo(buffer);
742 }
743
744 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700745 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
746 LOGE("Error %d making device file descriptor non-blocking.", errno);
747 close(fd);
748 return -1;
749 }
750
Jeff Brown90655042010-12-02 13:50:46 -0800751 // Allocate device. (The device object takes ownership of the fd at this point.)
752 int32_t deviceId = mNextDeviceId++;
753 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754
755#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800756 LOGI("add device %d: %s\n", deviceId, devicePath);
757 LOGI(" bus: %04x\n"
758 " vendor %04x\n"
759 " product %04x\n"
760 " version %04x\n",
761 identifier.bus, identifier.vendor, identifier.product, identifier.version);
762 LOGI(" name: \"%s\"\n", identifier.name.string());
763 LOGI(" location: \"%s\"\n", identifier.location.string());
764 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
765 LOGI(" driver: v%d.%d.%d\n",
766 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767#endif
768
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800769 // Load the configuration file for the device.
770 loadConfiguration(device);
771
Jeff Brownfd0358292010-06-30 16:10:35 -0700772 // Figure out the kinds of events the device reports.
Jeff Brownfd0358292010-06-30 16:10:35 -0700773 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800775 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700776
Jeff Brown6f2fba42011-02-19 01:08:02 -0800777 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
778 memset(abs_bitmask, 0, sizeof(abs_bitmask));
779 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700780
Jeff Brown6f2fba42011-02-19 01:08:02 -0800781 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
782 memset(rel_bitmask, 0, sizeof(rel_bitmask));
783 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700784
Jeff Brown6f2fba42011-02-19 01:08:02 -0800785 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
786 memset(sw_bitmask, 0, sizeof(sw_bitmask));
787 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask);
788
Jeff Browncc0c1592011-02-19 05:07:28 -0800789 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
790 if (device->keyBitmask != NULL) {
791 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
792 } else {
793 delete device;
794 LOGE("out of memory allocating key bitmask");
795 return -1;
796 }
797
798 device->relBitmask = new uint8_t[sizeof(rel_bitmask)];
799 if (device->relBitmask != NULL) {
800 memcpy(device->relBitmask, rel_bitmask, sizeof(rel_bitmask));
801 } else {
802 delete device;
803 LOGE("out of memory allocating rel bitmask");
804 return -1;
805 }
806
Jeff Brown6f2fba42011-02-19 01:08:02 -0800807 // See if this is a keyboard. Ignore everything in the button range except for
808 // joystick and gamepad buttons which are handled like keyboards for the most part.
809 bool haveKeyboardKeys = containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
810 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
811 sizeof_bit_array(KEY_MAX + 1));
812 bool haveGamepadButtons =containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_JOYSTICK),
813 sizeof_bit_array(BTN_DIGI));
814 if (haveKeyboardKeys || haveGamepadButtons) {
815 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800817
Jeff Brown83c09682010-12-23 17:50:18 -0800818 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800819 if (test_bit(BTN_MOUSE, key_bitmask)
820 && test_bit(REL_X, rel_bitmask)
821 && test_bit(REL_Y, rel_bitmask)) {
822 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 }
Jeff Brownfd0358292010-06-30 16:10:35 -0700824
825 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800826 // Is this a new modern multi-touch driver?
827 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
828 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
829 // Some joysticks such as the PS3 controller report axes that conflict
830 // with the ABS_MT range. Try to confirm that the device really is
831 // a touch screen.
832 if (test_bit(BTN_TOUCH, key_bitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800833 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -0700834 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800835 // Is this an old style single-touch driver?
836 } else if (test_bit(BTN_TOUCH, key_bitmask)
837 && test_bit(ABS_X, abs_bitmask)
838 && test_bit(ABS_Y, abs_bitmask)) {
839 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 }
841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 // figure out the switches this device reports
Jeff Brown6f2fba42011-02-19 01:08:02 -0800843 bool haveSwitches = false;
844 for (int i=0; i<EV_SW; i++) {
845 //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
846 if (test_bit(i, sw_bitmask)) {
847 haveSwitches = true;
848 if (mSwitches[i] == 0) {
849 mSwitches[i] = device->id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851 }
852 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800853 if (haveSwitches) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700854 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856
Jeff Brown58a2da82011-01-25 16:02:22 -0800857 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800858 // Load the virtual keys for the touch screen, if any.
859 // We do this now so that we can make sure to load the keymap if necessary.
860 status_t status = loadVirtualKeyMap(device);
861 if (!status) {
862 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Jeff Brown90655042010-12-02 13:50:46 -0800864 }
865
866 if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) {
867 // Load the keymap for the device.
868 status_t status = loadKeyMap(device);
869
870 // Set system properties for the keyboard.
Jeff Brown497a92c2010-09-12 17:55:08 -0700871 setKeyboardProperties(device, false);
872
Jeff Brown90655042010-12-02 13:50:46 -0800873 // Register the keyboard as a built-in keyboard if it is eligible.
874 if (!status
875 && mBuiltInKeyboardId == -1
876 && isEligibleBuiltInKeyboard(device->identifier,
877 device->configuration, &device->keyMap)) {
878 mBuiltInKeyboardId = device->id;
879 setKeyboardProperties(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -0700880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700882 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -0700883 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700884 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700885 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700886
Jeff Brownfd0358292010-06-30 16:10:35 -0700887 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -0700888 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
889 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
890 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
891 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
892 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700893 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700894 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700895
Jeff Brownfd0358292010-06-30 16:10:35 -0700896 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -0700897 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700898 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -0700899 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
900 break;
901 }
902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
Jeff Browncb1404e2011-01-15 18:14:15 -0800905 // See if this device is a joystick.
906 // Ignore touchscreens because they use the same absolute axes for other purposes.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800907 // Assumes that joysticks always have buttons and the keymap has been loaded.
Jeff Browncb1404e2011-01-15 18:14:15 -0800908 if (device->classes & INPUT_DEVICE_CLASS_GAMEPAD
Jeff Brown0a9f3352011-01-25 18:52:34 -0800909 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800910 if (containsNonZeroByte(abs_bitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800911 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
912 }
913 }
914
Sean McNeilaeb00c42010-06-23 16:00:37 +0700915 // If the device isn't recognized as something we handle, don't monitor it.
916 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800917 LOGV("Dropping device: id=%d, path='%s', name='%s'",
918 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +0700919 delete device;
920 return -1;
921 }
922
Jeff Brown90655042010-12-02 13:50:46 -0800923 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
924 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
925 deviceId, fd, devicePath, device->identifier.name.string(),
926 device->classes,
927 device->configurationFile.string(),
928 device->keyMap.keyLayoutFile.string(),
929 device->keyMap.keyCharacterMapFile.string(),
930 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800931
Jeff Brown90655042010-12-02 13:50:46 -0800932 struct pollfd pollfd;
933 pollfd.fd = fd;
934 pollfd.events = POLLIN;
935 pollfd.revents = 0;
936 mFds.push(pollfd);
937 mDevices.push(device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 device->next = mOpeningDevices;
940 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 return 0;
942}
943
Jeff Brown90655042010-12-02 13:50:46 -0800944void EventHub::loadConfiguration(Device* device) {
945 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
946 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800947 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -0800948 LOGD("No input device configuration file found for device '%s'.",
949 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800950 } else {
951 status_t status = PropertyMap::load(device->configurationFile,
952 &device->configuration);
953 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -0800954 LOGE("Error loading input device configuration file for device '%s'. "
955 "Using default configuration.",
956 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800957 }
958 }
959}
960
Jeff Brown90655042010-12-02 13:50:46 -0800961status_t EventHub::loadVirtualKeyMap(Device* device) {
962 // The virtual key map is supplied by the kernel as a system board property file.
963 String8 path;
964 path.append("/sys/board_properties/virtualkeys.");
965 path.append(device->identifier.name);
966 if (access(path.string(), R_OK)) {
967 return NAME_NOT_FOUND;
968 }
969 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -0700970}
971
Jeff Brown90655042010-12-02 13:50:46 -0800972status_t EventHub::loadKeyMap(Device* device) {
973 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -0700974}
975
Jeff Brown90655042010-12-02 13:50:46 -0800976void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) {
977 int32_t id = builtInKeyboard ? 0 : device->id;
978 android::setKeyboardProperties(id, device->identifier,
979 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
980}
981
982void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) {
983 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800984 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -0700985}
986
Jeff Brown90655042010-12-02 13:50:46 -0800987bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
988 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700989 return false;
990 }
991
992 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800993 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700994 const size_t N = scanCodes.size();
995 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
996 int32_t sc = scanCodes.itemAt(i);
997 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
998 return true;
999 }
1000 }
1001
1002 return false;
1003}
1004
Jeff Brown90655042010-12-02 13:50:46 -08001005int EventHub::closeDevice(const char *devicePath) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 AutoMutex _l(mLock);
Jeff Brown7342bb92010-10-01 18:55:43 -07001007
Jeff Brown90655042010-12-02 13:50:46 -08001008 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1009 Device* device = mDevices[i];
1010 if (device->path == devicePath) {
1011 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1012 device->path.string(), device->identifier.name.string(), device->id,
1013 device->fd, device->classes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 for (int j=0; j<EV_SW; j++) {
1016 if (mSwitches[j] == device->id) {
1017 mSwitches[j] = 0;
1018 }
1019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020
Jeff Brown90655042010-12-02 13:50:46 -08001021 if (device->id == mBuiltInKeyboardId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown90655042010-12-02 13:50:46 -08001023 device->path.string(), mBuiltInKeyboardId);
1024 mBuiltInKeyboardId = -1;
Jeff Brown497a92c2010-09-12 17:55:08 -07001025 clearKeyboardProperties(device, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001027 clearKeyboardProperties(device, false);
Jeff Brown90655042010-12-02 13:50:46 -08001028
1029 mFds.removeAt(i);
1030 mDevices.removeAt(i);
1031 device->close();
1032
1033 device->next = mClosingDevices;
1034 mClosingDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 return 0;
1036 }
1037 }
Jeff Brown90655042010-12-02 13:50:46 -08001038 LOGE("remove device: %s not found\n", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 return -1;
1040}
1041
Jeff Brown7342bb92010-10-01 18:55:43 -07001042int EventHub::readNotify(int nfd) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043#ifdef HAVE_INOTIFY
1044 int res;
1045 char devname[PATH_MAX];
1046 char *filename;
1047 char event_buf[512];
1048 int event_size;
1049 int event_pos = 0;
1050 struct inotify_event *event;
1051
Jeff Brown7342bb92010-10-01 18:55:43 -07001052 LOGV("EventHub::readNotify nfd: %d\n", nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 res = read(nfd, event_buf, sizeof(event_buf));
1054 if(res < (int)sizeof(*event)) {
1055 if(errno == EINTR)
1056 return 0;
1057 LOGW("could not get event, %s\n", strerror(errno));
1058 return 1;
1059 }
1060 //printf("got %d bytes of event information\n", res);
1061
Jeff Brown90655042010-12-02 13:50:46 -08001062 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 filename = devname + strlen(devname);
1064 *filename++ = '/';
1065
1066 while(res >= (int)sizeof(*event)) {
1067 event = (struct inotify_event *)(event_buf + event_pos);
1068 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1069 if(event->len) {
1070 strcpy(filename, event->name);
1071 if(event->mask & IN_CREATE) {
Jeff Brown7342bb92010-10-01 18:55:43 -07001072 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074 else {
Jeff Brown7342bb92010-10-01 18:55:43 -07001075 closeDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077 }
1078 event_size = sizeof(*event) + event->len;
1079 res -= event_size;
1080 event_pos += event_size;
1081 }
1082#endif
1083 return 0;
1084}
1085
Jeff Brown7342bb92010-10-01 18:55:43 -07001086int EventHub::scanDir(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087{
1088 char devname[PATH_MAX];
1089 char *filename;
1090 DIR *dir;
1091 struct dirent *de;
1092 dir = opendir(dirname);
1093 if(dir == NULL)
1094 return -1;
1095 strcpy(devname, dirname);
1096 filename = devname + strlen(devname);
1097 *filename++ = '/';
1098 while((de = readdir(dir))) {
1099 if(de->d_name[0] == '.' &&
1100 (de->d_name[1] == '\0' ||
1101 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1102 continue;
1103 strcpy(filename, de->d_name);
Jeff Brown7342bb92010-10-01 18:55:43 -07001104 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106 closedir(dir);
1107 return 0;
1108}
1109
Jeff Brownf2f487182010-10-01 17:46:21 -07001110void EventHub::dump(String8& dump) {
1111 dump.append("Event Hub State:\n");
1112
1113 { // acquire lock
1114 AutoMutex _l(mLock);
1115
Jeff Brown90655042010-12-02 13:50:46 -08001116 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001117
1118 dump.append(INDENT "Devices:\n");
1119
Jeff Brown90655042010-12-02 13:50:46 -08001120 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1121 const Device* device = mDevices[i];
Jeff Brownf2f487182010-10-01 17:46:21 -07001122 if (device) {
Jeff Brown90655042010-12-02 13:50:46 -08001123 if (mBuiltInKeyboardId == device->id) {
1124 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1125 device->id, device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001126 } else {
Jeff Brown90655042010-12-02 13:50:46 -08001127 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1128 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001129 }
1130 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1131 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Brown90655042010-12-02 13:50:46 -08001132 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1133 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1134 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1135 "product=0x%04x, version=0x%04x\n",
1136 device->identifier.bus, device->identifier.vendor,
1137 device->identifier.product, device->identifier.version);
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001138 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001139 device->keyMap.keyLayoutFile.string());
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001140 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001141 device->keyMap.keyCharacterMapFile.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001142 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1143 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001144 }
1145 }
1146 } // release lock
1147}
1148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149}; // namespace android