blob: f79d1069092927b6e6fc32de2e285f77f88b073d [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),
104 classes(0), keyBitmask(NULL), configuration(NULL), virtualKeyMap(NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105}
106
Jeff Brown90655042010-12-02 13:50:46 -0800107EventHub::Device::~Device() {
108 close();
109 delete[] keyBitmask;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800110 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800111 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112}
113
Jeff Brown90655042010-12-02 13:50:46 -0800114void EventHub::Device::close() {
115 if (fd >= 0) {
116 ::close(fd);
117 fd = -1;
118 }
119}
120
121
122// --- EventHub ---
123
124EventHub::EventHub(void) :
125 mError(NO_INIT), mBuiltInKeyboardId(-1), mNextDeviceId(1),
126 mOpeningDevices(0), mClosingDevices(0),
127 mOpened(false), mNeedToSendFinishedDeviceScan(false),
128 mInputBufferIndex(0), mInputBufferCount(0), mInputFdIndex(0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 memset(mSwitches, 0, sizeof(mSwitches));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131}
132
Jeff Brown90655042010-12-02 13:50:46 -0800133EventHub::~EventHub(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 release_wake_lock(WAKE_LOCK_ID);
135 // we should free stuff here...
136}
137
Jeff Brown90655042010-12-02 13:50:46 -0800138status_t EventHub::errorCheck() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 return mError;
140}
141
Jeff Brown90655042010-12-02 13:50:46 -0800142String8 EventHub::getDeviceName(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800144 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 if (device == NULL) return String8();
Jeff Brown90655042010-12-02 13:50:46 -0800146 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147}
148
Jeff Brown90655042010-12-02 13:50:46 -0800149uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800151 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 if (device == NULL) return 0;
153 return device->classes;
154}
155
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800156void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800157 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800158 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800159 if (device && device->configuration) {
160 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800161 } else {
162 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800163 }
164}
165
Jeff Brown6d0fec22010-07-23 21:28:06 -0700166status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
167 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700168 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800171 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 if (device == NULL) return -1;
173
174 struct input_absinfo info;
175
Jens Gulinc4554b92010-06-22 22:21:57 +0200176 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700177 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jeff Brown90655042010-12-02 13:50:46 -0800178 axis, device->identifier.name.string(), device->fd);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700179 return -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700181
182 if (info.minimum != info.maximum) {
183 outAxisInfo->valid = true;
184 outAxisInfo->minValue = info.minimum;
185 outAxisInfo->maxValue = info.maximum;
186 outAxisInfo->flat = info.flat;
187 outAxisInfo->fuzz = info.fuzz;
188 }
189 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190}
191
Jeff Brown6d0fec22010-07-23 21:28:06 -0700192int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700193 if (scanCode >= 0 && scanCode <= KEY_MAX) {
194 AutoMutex _l(mLock);
195
Jeff Brown90655042010-12-02 13:50:46 -0800196 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700197 if (device != NULL) {
198 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
200 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700201 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202}
203
Jeff Brown90655042010-12-02 13:50:46 -0800204int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700205 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700206 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200207 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700208 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700209 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700210 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700211 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700212}
213
Jeff Brown6d0fec22010-07-23 21:28:06 -0700214int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
215 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700216
Jeff Brown90655042010-12-02 13:50:46 -0800217 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700218 if (device != NULL) {
219 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700221 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222}
223
Jeff Brown90655042010-12-02 13:50:46 -0800224int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const {
225 if (!device->keyMap.haveKeyLayout()) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800226 return AKEY_STATE_UNKNOWN;
227 }
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800230 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700231
Jeff Brownfd0358292010-06-30 16:10:35 -0700232 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200234 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 #if 0
236 for (size_t i=0; i<=KEY_MAX; i++) {
237 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
238 }
239 #endif
240 const size_t N = scanCodes.size();
241 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
242 int32_t sc = scanCodes.itemAt(i);
243 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
244 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700245 return AKEY_STATE_DOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
247 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700248 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700250 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700251}
252
Jeff Brown6d0fec22010-07-23 21:28:06 -0700253int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700254 if (sw >= 0 && sw <= SW_MAX) {
255 AutoMutex _l(mLock);
256
Jeff Brown90655042010-12-02 13:50:46 -0800257 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700258 if (device != NULL) {
259 return getSwitchStateLocked(device, sw);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700261 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700262 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700263}
264
Jeff Brown90655042010-12-02 13:50:46 -0800265int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const {
Jeff Brownfd0358292010-06-30 16:10:35 -0700266 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700267 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200268 if (ioctl(device->fd,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700269 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700270 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700271 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700272 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273}
274
Jeff Brown6d0fec22010-07-23 21:28:06 -0700275bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
276 const int32_t* keyCodes, uint8_t* outFlags) const {
277 AutoMutex _l(mLock);
278
Jeff Brown90655042010-12-02 13:50:46 -0800279 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700280 if (device != NULL) {
281 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
282 }
283 return false;
284}
285
Jeff Brown90655042010-12-02 13:50:46 -0800286bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700287 const int32_t* keyCodes, uint8_t* outFlags) const {
Jeff Brown90655042010-12-02 13:50:46 -0800288 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700289 return false;
290 }
291
292 Vector<int32_t> scanCodes;
293 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
294 scanCodes.clear();
295
Jeff Brown6f2fba42011-02-19 01:08:02 -0800296 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
297 keyCodes[codeIndex], &scanCodes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700298 if (! err) {
299 // check the possible scan codes identified by the layout map against the
300 // map of codes actually emitted by the driver
301 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
302 if (test_bit(scanCodes[sc], device->keyBitmask)) {
303 outFlags[codeIndex] = 1;
304 break;
305 }
306 }
307 }
308 }
309 return true;
310}
311
Jeff Brown6f2fba42011-02-19 01:08:02 -0800312status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700313 int32_t* outKeycode, uint32_t* outFlags) const
314{
315 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800316 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700317
Jeff Brown90655042010-12-02 13:50:46 -0800318 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800319 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700320 if (err == NO_ERROR) {
321 return NO_ERROR;
322 }
323 }
324
Jeff Brown90655042010-12-02 13:50:46 -0800325 if (mBuiltInKeyboardId != -1) {
326 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700327
Jeff Brown90655042010-12-02 13:50:46 -0800328 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800329 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700330 if (err == NO_ERROR) {
331 return NO_ERROR;
332 }
333 }
334 }
335
336 *outKeycode = 0;
337 *outFlags = 0;
338 return NAME_NOT_FOUND;
339}
340
Jeff Brown6f2fba42011-02-19 01:08:02 -0800341status_t EventHub::mapAxis(int32_t deviceId, int scancode,
342 int32_t* outAxis) const
343{
344 AutoMutex _l(mLock);
345 Device* device = getDeviceLocked(deviceId);
346
347 if (device && device->keyMap.haveKeyLayout()) {
348 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxis);
349 if (err == NO_ERROR) {
350 return NO_ERROR;
351 }
352 }
353
354 if (mBuiltInKeyboardId != -1) {
355 device = getDeviceLocked(mBuiltInKeyboardId);
356
357 if (device && device->keyMap.haveKeyLayout()) {
358 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxis);
359 if (err == NO_ERROR) {
360 return NO_ERROR;
361 }
362 }
363 }
364
365 *outAxis = -1;
366 return NAME_NOT_FOUND;
367}
368
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400369void EventHub::addExcludedDevice(const char* deviceName)
370{
Jeff Brownf2f487182010-10-01 17:46:21 -0700371 AutoMutex _l(mLock);
372
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400373 String8 name(deviceName);
374 mExcludedDevices.push_back(name);
375}
376
Jeff Brown497a92c2010-09-12 17:55:08 -0700377bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
378 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800379 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700380 if (device) {
381 uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)];
382 memset(bitmask, 0, sizeof(bitmask));
383 if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) {
384 if (test_bit(led, bitmask)) {
385 return true;
386 }
387 }
388 }
389 return false;
390}
391
392void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
393 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800394 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700395 if (device) {
396 struct input_event ev;
397 ev.time.tv_sec = 0;
398 ev.time.tv_usec = 0;
399 ev.type = EV_LED;
400 ev.code = led;
401 ev.value = on ? 1 : 0;
402
403 ssize_t nWrite;
404 do {
405 nWrite = write(device->fd, &ev, sizeof(struct input_event));
406 } while (nWrite == -1 && errno == EINTR);
407 }
408}
409
Jeff Brown90655042010-12-02 13:50:46 -0800410void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
411 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
412 outVirtualKeys.clear();
413
414 AutoMutex _l(mLock);
415 Device* device = getDeviceLocked(deviceId);
416 if (device && device->virtualKeyMap) {
417 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
418 }
419}
420
421EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
422 if (deviceId == 0) {
423 deviceId = mBuiltInKeyboardId;
424 }
425
426 size_t numDevices = mDevices.size();
427 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < numDevices; i++) {
428 Device* device = mDevices[i];
429 if (device->id == deviceId) {
430 return device;
431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433 return NULL;
434}
435
Jeff Brown90655042010-12-02 13:50:46 -0800436bool EventHub::getEvent(RawEvent* outEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700437 outEvent->deviceId = 0;
438 outEvent->type = 0;
439 outEvent->scanCode = 0;
440 outEvent->keyCode = 0;
441 outEvent->flags = 0;
442 outEvent->value = 0;
443 outEvent->when = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 // Note that we only allow one caller to getEvent(), so don't need
446 // to do locking here... only when adding/removing devices.
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400447
448 if (!mOpened) {
449 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
450 mOpened = true;
Jeff Brown7342bb92010-10-01 18:55:43 -0700451 mNeedToSendFinishedDeviceScan = true;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400452 }
453
Jeff Browncc2e7172010-08-17 16:48:25 -0700454 for (;;) {
455 // Report any devices that had last been added/removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 if (mClosingDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800457 Device* device = mClosingDevices;
458 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 device->id, device->path.string());
460 mClosingDevices = device->next;
Jeff Brown90655042010-12-02 13:50:46 -0800461 if (device->id == mBuiltInKeyboardId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462 outEvent->deviceId = 0;
463 } else {
464 outEvent->deviceId = device->id;
465 }
466 outEvent->type = DEVICE_REMOVED;
Jeff Brownc3db8582010-10-20 15:33:38 -0700467 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700469 mNeedToSendFinishedDeviceScan = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 return true;
471 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 if (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800474 Device* device = mOpeningDevices;
475 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 device->id, device->path.string());
477 mOpeningDevices = device->next;
Jeff Brown90655042010-12-02 13:50:46 -0800478 if (device->id == mBuiltInKeyboardId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700479 outEvent->deviceId = 0;
480 } else {
481 outEvent->deviceId = device->id;
482 }
483 outEvent->type = DEVICE_ADDED;
Jeff Brownc3db8582010-10-20 15:33:38 -0700484 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown7342bb92010-10-01 18:55:43 -0700485 mNeedToSendFinishedDeviceScan = true;
486 return true;
487 }
488
489 if (mNeedToSendFinishedDeviceScan) {
490 mNeedToSendFinishedDeviceScan = false;
491 outEvent->type = FINISHED_DEVICE_SCAN;
Jeff Brownc3db8582010-10-20 15:33:38 -0700492 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 return true;
494 }
495
Jeff Browncc2e7172010-08-17 16:48:25 -0700496 // Grab the next input event.
497 for (;;) {
498 // Consume buffered input events, if any.
499 if (mInputBufferIndex < mInputBufferCount) {
500 const struct input_event& iev = mInputBufferData[mInputBufferIndex++];
Jeff Brown90655042010-12-02 13:50:46 -0800501 const Device* device = mDevices[mInputFdIndex];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
Jeff Browncc2e7172010-08-17 16:48:25 -0700503 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(),
504 (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value);
Jeff Brown90655042010-12-02 13:50:46 -0800505 if (device->id == mBuiltInKeyboardId) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700506 outEvent->deviceId = 0;
507 } else {
508 outEvent->deviceId = device->id;
509 }
510 outEvent->type = iev.type;
511 outEvent->scanCode = iev.code;
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800512 outEvent->flags = 0;
Jeff Browncc2e7172010-08-17 16:48:25 -0700513 if (iev.type == EV_KEY) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800514 outEvent->keyCode = AKEYCODE_UNKNOWN;
Jeff Brown90655042010-12-02 13:50:46 -0800515 if (device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800516 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800517 &outEvent->keyCode, &outEvent->flags);
518 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
519 iev.code, outEvent->keyCode, outEvent->flags, err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700521 } else {
522 outEvent->keyCode = iev.code;
523 }
524 outEvent->value = iev.value;
525
526 // Use an event timestamp in the same timebase as
527 // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis()
528 // as expected by the rest of the system.
529 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
530 return true;
531 }
532
533 // Finish reading all events from devices identified in previous poll().
534 // This code assumes that mInputDeviceIndex is initially 0 and that the
535 // revents member of pollfd is initialized to 0 when the device is first added.
Jeff Brown90655042010-12-02 13:50:46 -0800536 // Since mFds[0] is used for inotify, we process regular events starting at index 1.
537 mInputFdIndex += 1;
538 if (mInputFdIndex >= mFds.size()) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700539 break;
540 }
541
Jeff Brown90655042010-12-02 13:50:46 -0800542 const struct pollfd& pfd = mFds[mInputFdIndex];
Jeff Browncc2e7172010-08-17 16:48:25 -0700543 if (pfd.revents & POLLIN) {
544 int32_t readSize = read(pfd.fd, mInputBufferData,
545 sizeof(struct input_event) * INPUT_BUFFER_SIZE);
546 if (readSize < 0) {
547 if (errno != EAGAIN && errno != EINTR) {
548 LOGW("could not get event (errno=%d)", errno);
549 }
550 } else if ((readSize % sizeof(struct input_event)) != 0) {
551 LOGE("could not get event (wrong size: %d)", readSize);
552 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800553 mInputBufferCount = size_t(readSize) / sizeof(struct input_event);
Jeff Browncc2e7172010-08-17 16:48:25 -0700554 mInputBufferIndex = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 }
557 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700558
Jeff Browna9b84222010-10-14 02:23:43 -0700559#if HAVE_INOTIFY
Jeff Brown7342bb92010-10-01 18:55:43 -0700560 // readNotify() will modify mFDs and mFDCount, so this must be done after
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 // processing all other events.
Jeff Brown90655042010-12-02 13:50:46 -0800562 if(mFds[0].revents & POLLIN) {
563 readNotify(mFds[0].fd);
564 mFds.editItemAt(0).revents = 0;
Jeff Browna9b84222010-10-14 02:23:43 -0700565 continue; // report added or removed devices immediately
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
Jeff Browna9b84222010-10-14 02:23:43 -0700567#endif
568
Jeff Brown90655042010-12-02 13:50:46 -0800569 mInputFdIndex = 0;
Jeff Browncc2e7172010-08-17 16:48:25 -0700570
571 // Poll for events. Mind the wake lock dance!
572 // We hold a wake lock at all times except during poll(). This works due to some
573 // subtle choreography. When a device driver has pending (unread) events, it acquires
574 // a kernel wake lock. However, once the last pending event has been read, the device
575 // driver will release the kernel wake lock. To prevent the system from going to sleep
576 // when this happens, the EventHub holds onto its own user wake lock while the client
577 // is processing events. Thus the system can only sleep if there are no events
578 // pending or currently being processed.
579 release_wake_lock(WAKE_LOCK_ID);
580
Jeff Brown90655042010-12-02 13:50:46 -0800581 int pollResult = poll(mFds.editArray(), mFds.size(), -1);
Jeff Browncc2e7172010-08-17 16:48:25 -0700582
583 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
584
585 if (pollResult <= 0) {
586 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700587 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700588 usleep(100000);
589 }
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592}
593
594/*
595 * Open the platform-specific input device.
596 */
Jeff Brown90655042010-12-02 13:50:46 -0800597bool EventHub::openPlatformInput(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 /*
599 * Open platform-specific input device(s).
600 */
Jeff Brown90655042010-12-02 13:50:46 -0800601 int res, fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603#ifdef HAVE_INOTIFY
Jeff Brown90655042010-12-02 13:50:46 -0800604 fd = inotify_init();
605 res = inotify_add_watch(fd, DEVICE_PATH, IN_DELETE | IN_CREATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800607 LOGE("could not add watch for %s, %s\n", DEVICE_PATH, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
609#else
610 /*
611 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
612 * We allocate space for it and set it to something invalid.
613 */
Jeff Brown90655042010-12-02 13:50:46 -0800614 fd = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615#endif
616
Jeff Brown90655042010-12-02 13:50:46 -0800617 // Reserve fd index 0 for inotify.
618 struct pollfd pollfd;
619 pollfd.fd = fd;
620 pollfd.events = POLLIN;
621 pollfd.revents = 0;
622 mFds.push(pollfd);
623 mDevices.push(NULL);
624
625 res = scanDir(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800627 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629
630 return true;
631}
632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633// ----------------------------------------------------------------------------
634
Jeff Brownfd0358292010-06-30 16:10:35 -0700635static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
636 const uint8_t* end = array + endIndex;
637 array += startIndex;
638 while (array != end) {
639 if (*(array++) != 0) {
640 return true;
641 }
642 }
643 return false;
644}
645
646static const int32_t GAMEPAD_KEYCODES[] = {
647 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
648 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
649 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
650 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
651 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800652 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
653 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
654 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
655 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
656 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -0700657};
658
Jeff Brown90655042010-12-02 13:50:46 -0800659int EventHub::openDevice(const char *devicePath) {
660 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661
Jeff Brown90655042010-12-02 13:50:46 -0800662 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663
664 AutoMutex _l(mLock);
Nick Pellye6b1bbd2010-01-20 19:36:49 -0800665
Jeff Brown90655042010-12-02 13:50:46 -0800666 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800668 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 return -1;
670 }
671
Jeff Brown90655042010-12-02 13:50:46 -0800672 InputDeviceIdentifier identifier;
673
674 // Get device name.
675 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
676 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
677 } else {
678 buffer[sizeof(buffer) - 1] = '\0';
679 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400681
Jeff Brown90655042010-12-02 13:50:46 -0800682 // Check to see if the device is on our excluded list
Mike Lockwood15431a92009-07-17 00:10:10 -0400683 List<String8>::iterator iter = mExcludedDevices.begin();
684 List<String8>::iterator end = mExcludedDevices.end();
685 for ( ; iter != end; iter++) {
686 const char* test = *iter;
Jeff Brown90655042010-12-02 13:50:46 -0800687 if (identifier.name == test) {
688 LOGI("ignoring event id %s driver %s\n", devicePath, test);
Mike Lockwood15431a92009-07-17 00:10:10 -0400689 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400690 return -1;
691 }
692 }
693
Jeff Brown90655042010-12-02 13:50:46 -0800694 // Get device driver version.
695 int driverVersion;
696 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
697 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
698 close(fd);
699 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701
Jeff Brown90655042010-12-02 13:50:46 -0800702 // Get device identifier.
703 struct input_id inputId;
704 if(ioctl(fd, EVIOCGID, &inputId)) {
705 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
706 close(fd);
707 return -1;
708 }
709 identifier.bus = inputId.bustype;
710 identifier.product = inputId.product;
711 identifier.vendor = inputId.vendor;
712 identifier.version = inputId.version;
713
714 // Get device physical location.
715 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
716 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
717 } else {
718 buffer[sizeof(buffer) - 1] = '\0';
719 identifier.location.setTo(buffer);
720 }
721
722 // Get device unique id.
723 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
724 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
725 } else {
726 buffer[sizeof(buffer) - 1] = '\0';
727 identifier.uniqueId.setTo(buffer);
728 }
729
730 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700731 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
732 LOGE("Error %d making device file descriptor non-blocking.", errno);
733 close(fd);
734 return -1;
735 }
736
Jeff Brown90655042010-12-02 13:50:46 -0800737 // Allocate device. (The device object takes ownership of the fd at this point.)
738 int32_t deviceId = mNextDeviceId++;
739 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740
741#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800742 LOGI("add device %d: %s\n", deviceId, devicePath);
743 LOGI(" bus: %04x\n"
744 " vendor %04x\n"
745 " product %04x\n"
746 " version %04x\n",
747 identifier.bus, identifier.vendor, identifier.product, identifier.version);
748 LOGI(" name: \"%s\"\n", identifier.name.string());
749 LOGI(" location: \"%s\"\n", identifier.location.string());
750 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
751 LOGI(" driver: v%d.%d.%d\n",
752 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753#endif
754
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800755 // Load the configuration file for the device.
756 loadConfiguration(device);
757
Jeff Brownfd0358292010-06-30 16:10:35 -0700758 // Figure out the kinds of events the device reports.
Jeff Brownfd0358292010-06-30 16:10:35 -0700759 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800761 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700762
Jeff Brown6f2fba42011-02-19 01:08:02 -0800763 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
764 memset(abs_bitmask, 0, sizeof(abs_bitmask));
765 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700766
Jeff Brown6f2fba42011-02-19 01:08:02 -0800767 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
768 memset(rel_bitmask, 0, sizeof(rel_bitmask));
769 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask);
Jeff Brownfd0358292010-06-30 16:10:35 -0700770
Jeff Brown6f2fba42011-02-19 01:08:02 -0800771 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
772 memset(sw_bitmask, 0, sizeof(sw_bitmask));
773 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask);
774
775 // See if this is a keyboard. Ignore everything in the button range except for
776 // joystick and gamepad buttons which are handled like keyboards for the most part.
777 bool haveKeyboardKeys = containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
778 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
779 sizeof_bit_array(KEY_MAX + 1));
780 bool haveGamepadButtons =containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_JOYSTICK),
781 sizeof_bit_array(BTN_DIGI));
782 if (haveKeyboardKeys || haveGamepadButtons) {
783 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
784 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
785 if (device->keyBitmask != NULL) {
786 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
787 } else {
788 delete device;
789 LOGE("out of memory allocating key bitmask");
790 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800793
Jeff Brown83c09682010-12-23 17:50:18 -0800794 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800795 if (test_bit(BTN_MOUSE, key_bitmask)
796 && test_bit(REL_X, rel_bitmask)
797 && test_bit(REL_Y, rel_bitmask)) {
798 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
Jeff Brownfd0358292010-06-30 16:10:35 -0700800
801 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800802 // Is this a new modern multi-touch driver?
803 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
804 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
805 // Some joysticks such as the PS3 controller report axes that conflict
806 // with the ABS_MT range. Try to confirm that the device really is
807 // a touch screen.
808 if (test_bit(BTN_TOUCH, key_bitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800809 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -0700810 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800811 // Is this an old style single-touch driver?
812 } else if (test_bit(BTN_TOUCH, key_bitmask)
813 && test_bit(ABS_X, abs_bitmask)
814 && test_bit(ABS_Y, abs_bitmask)) {
815 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 // figure out the switches this device reports
Jeff Brown6f2fba42011-02-19 01:08:02 -0800819 bool haveSwitches = false;
820 for (int i=0; i<EV_SW; i++) {
821 //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
822 if (test_bit(i, sw_bitmask)) {
823 haveSwitches = true;
824 if (mSwitches[i] == 0) {
825 mSwitches[i] = device->id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827 }
828 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800829 if (haveSwitches) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700830 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832
Jeff Brown58a2da82011-01-25 16:02:22 -0800833 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800834 // Load the virtual keys for the touch screen, if any.
835 // We do this now so that we can make sure to load the keymap if necessary.
836 status_t status = loadVirtualKeyMap(device);
837 if (!status) {
838 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
Jeff Brown90655042010-12-02 13:50:46 -0800840 }
841
842 if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) {
843 // Load the keymap for the device.
844 status_t status = loadKeyMap(device);
845
846 // Set system properties for the keyboard.
Jeff Brown497a92c2010-09-12 17:55:08 -0700847 setKeyboardProperties(device, false);
848
Jeff Brown90655042010-12-02 13:50:46 -0800849 // Register the keyboard as a built-in keyboard if it is eligible.
850 if (!status
851 && mBuiltInKeyboardId == -1
852 && isEligibleBuiltInKeyboard(device->identifier,
853 device->configuration, &device->keyMap)) {
854 mBuiltInKeyboardId = device->id;
855 setKeyboardProperties(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -0700856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700858 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -0700859 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700860 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700861 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700862
Jeff Brownfd0358292010-06-30 16:10:35 -0700863 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -0700864 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
865 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
866 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
867 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
868 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700869 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700870 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700871
Jeff Brownfd0358292010-06-30 16:10:35 -0700872 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -0700873 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700874 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -0700875 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
876 break;
877 }
878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880
Jeff Browncb1404e2011-01-15 18:14:15 -0800881 // See if this device is a joystick.
882 // Ignore touchscreens because they use the same absolute axes for other purposes.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800883 // Assumes that joysticks always have buttons and the keymap has been loaded.
Jeff Browncb1404e2011-01-15 18:14:15 -0800884 if (device->classes & INPUT_DEVICE_CLASS_GAMEPAD
Jeff Brown0a9f3352011-01-25 18:52:34 -0800885 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800886 if (containsNonZeroByte(abs_bitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800887 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
888 }
889 }
890
Sean McNeilaeb00c42010-06-23 16:00:37 +0700891 // If the device isn't recognized as something we handle, don't monitor it.
892 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800893 LOGV("Dropping device: id=%d, path='%s', name='%s'",
894 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +0700895 delete device;
896 return -1;
897 }
898
Jeff Brown90655042010-12-02 13:50:46 -0800899 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
900 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
901 deviceId, fd, devicePath, device->identifier.name.string(),
902 device->classes,
903 device->configurationFile.string(),
904 device->keyMap.keyLayoutFile.string(),
905 device->keyMap.keyCharacterMapFile.string(),
906 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800907
Jeff Brown90655042010-12-02 13:50:46 -0800908 struct pollfd pollfd;
909 pollfd.fd = fd;
910 pollfd.events = POLLIN;
911 pollfd.revents = 0;
912 mFds.push(pollfd);
913 mDevices.push(device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 device->next = mOpeningDevices;
916 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 return 0;
918}
919
Jeff Brown90655042010-12-02 13:50:46 -0800920void EventHub::loadConfiguration(Device* device) {
921 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
922 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800923 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -0800924 LOGD("No input device configuration file found for device '%s'.",
925 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800926 } else {
927 status_t status = PropertyMap::load(device->configurationFile,
928 &device->configuration);
929 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -0800930 LOGE("Error loading input device configuration file for device '%s'. "
931 "Using default configuration.",
932 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800933 }
934 }
935}
936
Jeff Brown90655042010-12-02 13:50:46 -0800937status_t EventHub::loadVirtualKeyMap(Device* device) {
938 // The virtual key map is supplied by the kernel as a system board property file.
939 String8 path;
940 path.append("/sys/board_properties/virtualkeys.");
941 path.append(device->identifier.name);
942 if (access(path.string(), R_OK)) {
943 return NAME_NOT_FOUND;
944 }
945 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -0700946}
947
Jeff Brown90655042010-12-02 13:50:46 -0800948status_t EventHub::loadKeyMap(Device* device) {
949 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -0700950}
951
Jeff Brown90655042010-12-02 13:50:46 -0800952void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) {
953 int32_t id = builtInKeyboard ? 0 : device->id;
954 android::setKeyboardProperties(id, device->identifier,
955 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
956}
957
958void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) {
959 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800960 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -0700961}
962
Jeff Brown90655042010-12-02 13:50:46 -0800963bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
964 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700965 return false;
966 }
967
968 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800969 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700970 const size_t N = scanCodes.size();
971 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
972 int32_t sc = scanCodes.itemAt(i);
973 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
974 return true;
975 }
976 }
977
978 return false;
979}
980
Jeff Brown90655042010-12-02 13:50:46 -0800981int EventHub::closeDevice(const char *devicePath) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 AutoMutex _l(mLock);
Jeff Brown7342bb92010-10-01 18:55:43 -0700983
Jeff Brown90655042010-12-02 13:50:46 -0800984 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
985 Device* device = mDevices[i];
986 if (device->path == devicePath) {
987 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
988 device->path.string(), device->identifier.name.string(), device->id,
989 device->fd, device->classes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 for (int j=0; j<EV_SW; j++) {
992 if (mSwitches[j] == device->id) {
993 mSwitches[j] = 0;
994 }
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996
Jeff Brown90655042010-12-02 13:50:46 -0800997 if (device->id == mBuiltInKeyboardId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
Jeff Brown90655042010-12-02 13:50:46 -0800999 device->path.string(), mBuiltInKeyboardId);
1000 mBuiltInKeyboardId = -1;
Jeff Brown497a92c2010-09-12 17:55:08 -07001001 clearKeyboardProperties(device, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001003 clearKeyboardProperties(device, false);
Jeff Brown90655042010-12-02 13:50:46 -08001004
1005 mFds.removeAt(i);
1006 mDevices.removeAt(i);
1007 device->close();
1008
1009 device->next = mClosingDevices;
1010 mClosingDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 return 0;
1012 }
1013 }
Jeff Brown90655042010-12-02 13:50:46 -08001014 LOGE("remove device: %s not found\n", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 return -1;
1016}
1017
Jeff Brown7342bb92010-10-01 18:55:43 -07001018int EventHub::readNotify(int nfd) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019#ifdef HAVE_INOTIFY
1020 int res;
1021 char devname[PATH_MAX];
1022 char *filename;
1023 char event_buf[512];
1024 int event_size;
1025 int event_pos = 0;
1026 struct inotify_event *event;
1027
Jeff Brown7342bb92010-10-01 18:55:43 -07001028 LOGV("EventHub::readNotify nfd: %d\n", nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 res = read(nfd, event_buf, sizeof(event_buf));
1030 if(res < (int)sizeof(*event)) {
1031 if(errno == EINTR)
1032 return 0;
1033 LOGW("could not get event, %s\n", strerror(errno));
1034 return 1;
1035 }
1036 //printf("got %d bytes of event information\n", res);
1037
Jeff Brown90655042010-12-02 13:50:46 -08001038 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 filename = devname + strlen(devname);
1040 *filename++ = '/';
1041
1042 while(res >= (int)sizeof(*event)) {
1043 event = (struct inotify_event *)(event_buf + event_pos);
1044 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1045 if(event->len) {
1046 strcpy(filename, event->name);
1047 if(event->mask & IN_CREATE) {
Jeff Brown7342bb92010-10-01 18:55:43 -07001048 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050 else {
Jeff Brown7342bb92010-10-01 18:55:43 -07001051 closeDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 }
1054 event_size = sizeof(*event) + event->len;
1055 res -= event_size;
1056 event_pos += event_size;
1057 }
1058#endif
1059 return 0;
1060}
1061
Jeff Brown7342bb92010-10-01 18:55:43 -07001062int EventHub::scanDir(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063{
1064 char devname[PATH_MAX];
1065 char *filename;
1066 DIR *dir;
1067 struct dirent *de;
1068 dir = opendir(dirname);
1069 if(dir == NULL)
1070 return -1;
1071 strcpy(devname, dirname);
1072 filename = devname + strlen(devname);
1073 *filename++ = '/';
1074 while((de = readdir(dir))) {
1075 if(de->d_name[0] == '.' &&
1076 (de->d_name[1] == '\0' ||
1077 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1078 continue;
1079 strcpy(filename, de->d_name);
Jeff Brown7342bb92010-10-01 18:55:43 -07001080 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 closedir(dir);
1083 return 0;
1084}
1085
Jeff Brownf2f487182010-10-01 17:46:21 -07001086void EventHub::dump(String8& dump) {
1087 dump.append("Event Hub State:\n");
1088
1089 { // acquire lock
1090 AutoMutex _l(mLock);
1091
Jeff Brown90655042010-12-02 13:50:46 -08001092 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001093
1094 dump.append(INDENT "Devices:\n");
1095
Jeff Brown90655042010-12-02 13:50:46 -08001096 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1097 const Device* device = mDevices[i];
Jeff Brownf2f487182010-10-01 17:46:21 -07001098 if (device) {
Jeff Brown90655042010-12-02 13:50:46 -08001099 if (mBuiltInKeyboardId == device->id) {
1100 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1101 device->id, device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001102 } else {
Jeff Brown90655042010-12-02 13:50:46 -08001103 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1104 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001105 }
1106 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1107 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Brown90655042010-12-02 13:50:46 -08001108 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1109 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1110 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1111 "product=0x%04x, version=0x%04x\n",
1112 device->identifier.bus, device->identifier.vendor,
1113 device->identifier.product, device->identifier.version);
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001114 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001115 device->keyMap.keyLayoutFile.string());
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001116 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001117 device->keyMap.keyCharacterMapFile.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001118 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1119 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001120 }
1121 }
1122 } // release lock
1123}
1124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125}; // namespace android