blob: 6e803a441356ed78a2621533214bd3b15cef23fb [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017//
18// Handle events, like key input and vsync.
19//
20// The goal is to provide an optimized solution for Linux, not an
21// implementation that works well across all platforms. We expect
22// events to arrive on file descriptors, so that we can use a select()
23// select() call to sleep.
24//
25// We can't select() on anything but network sockets in Windows, so we
26// provide an alternative implementation of waitEvent for that platform.
27//
28#define LOG_TAG "EventHub"
29
30//#define LOG_NDEBUG 0
31
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032#include "EventHub.h"
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <hardware_legacy/power.h>
35
Jeff Brown1a84fd12011-06-02 01:26:32 -070036#include <cutils/atomic.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include <utils/Log.h>
39#include <utils/Timers.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070040#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070041#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43#include <stdlib.h>
44#include <stdio.h>
45#include <unistd.h>
46#include <fcntl.h>
47#include <memory.h>
48#include <errno.h>
49#include <assert.h>
50
Jeff Brown6b53e8d2010-11-10 16:03:06 -080051#include <ui/KeyLayoutMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080052#include <ui/KeyCharacterMap.h>
53#include <ui/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55#include <string.h>
56#include <stdint.h>
57#include <dirent.h>
58#ifdef HAVE_INOTIFY
59# include <sys/inotify.h>
60#endif
61#ifdef HAVE_ANDROID_OS
62# include <sys/limits.h> /* not part of Linux */
63#endif
64#include <sys/poll.h>
65#include <sys/ioctl.h>
66
67/* this macro is used to tell if "bit" is set in "array"
68 * it selects a byte from the array, and does a boolean AND
69 * operation with a byte that only has the relevant bit set.
70 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
71 */
72#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
73
Jeff Brownfd035822010-06-30 16:10:35 -070074/* this macro computes the number of bytes needed to represent a bit array of the specified size */
75#define sizeof_bit_array(bits) ((bits + 7) / 8)
76
Jeff Brown90655042010-12-02 13:50:46 -080077// Fd at index 0 is always reserved for inotify
78#define FIRST_ACTUAL_DEVICE_INDEX 1
79
Jeff Brownf2f487182010-10-01 17:46:21 -070080#define INDENT " "
81#define INDENT2 " "
82#define INDENT3 " "
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084namespace android {
85
86static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080087static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89/* return the larger integer */
90static inline int max(int v1, int v2)
91{
92 return (v1 > v2) ? v1 : v2;
93}
94
Jeff Brownf2f487182010-10-01 17:46:21 -070095static inline const char* toString(bool value) {
96 return value ? "true" : "false";
97}
98
Jeff Brown90655042010-12-02 13:50:46 -080099// --- EventHub::Device ---
100
101EventHub::Device::Device(int fd, int32_t id, const String8& path,
102 const InputDeviceIdentifier& identifier) :
103 next(NULL),
104 fd(fd), id(id), path(path), identifier(identifier),
Jeff Brown80fd47c2011-05-24 01:07:44 -0700105 classes(0), keyBitmask(NULL), relBitmask(NULL), propBitmask(NULL),
Jeff Browncc0c1592011-02-19 05:07:28 -0800106 configuration(NULL), virtualKeyMap(NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107}
108
Jeff Brown90655042010-12-02 13:50:46 -0800109EventHub::Device::~Device() {
110 close();
111 delete[] keyBitmask;
Jeff Browncc0c1592011-02-19 05:07:28 -0800112 delete[] relBitmask;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700113 delete[] propBitmask;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800114 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -0800115 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116}
117
Jeff Brown90655042010-12-02 13:50:46 -0800118void EventHub::Device::close() {
119 if (fd >= 0) {
120 ::close(fd);
121 fd = -1;
122 }
123}
124
125
126// --- EventHub ---
127
128EventHub::EventHub(void) :
129 mError(NO_INIT), mBuiltInKeyboardId(-1), mNextDeviceId(1),
130 mOpeningDevices(0), mClosingDevices(0),
131 mOpened(false), mNeedToSendFinishedDeviceScan(false),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700132 mNeedToReopenDevices(0), mNeedToScanDevices(false),
Jeff Brownb7198742011-03-18 18:14:26 -0700133 mInputFdIndex(1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 memset(mSwitches, 0, sizeof(mSwitches));
Jeff Brownb7198742011-03-18 18:14:26 -0700137 mNumCpus = sysconf(_SC_NPROCESSORS_ONLN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
Jeff Brown90655042010-12-02 13:50:46 -0800140EventHub::~EventHub(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 release_wake_lock(WAKE_LOCK_ID);
142 // we should free stuff here...
143}
144
Jeff Brown90655042010-12-02 13:50:46 -0800145status_t EventHub::errorCheck() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 return mError;
147}
148
Jeff Brown90655042010-12-02 13:50:46 -0800149String8 EventHub::getDeviceName(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 String8();
Jeff Brown90655042010-12-02 13:50:46 -0800153 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154}
155
Jeff Brown90655042010-12-02 13:50:46 -0800156uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800158 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 if (device == NULL) return 0;
160 return device->classes;
161}
162
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800163void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800164 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800165 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800166 if (device && device->configuration) {
167 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800168 } else {
169 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800170 }
171}
172
Jeff Brown6d0fec22010-07-23 21:28:06 -0700173status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
174 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700175 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800178 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 if (device == NULL) return -1;
180
181 struct input_absinfo info;
182
Jens Gulinc4554b92010-06-22 22:21:57 +0200183 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700184 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jeff Brown90655042010-12-02 13:50:46 -0800185 axis, device->identifier.name.string(), device->fd);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700186 return -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700188
189 if (info.minimum != info.maximum) {
190 outAxisInfo->valid = true;
191 outAxisInfo->minValue = info.minimum;
192 outAxisInfo->maxValue = info.maximum;
193 outAxisInfo->flat = info.flat;
194 outAxisInfo->fuzz = info.fuzz;
Jeff Brownb3a2d132011-06-12 18:14:50 -0700195 outAxisInfo->resolution = info.resolution;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700196 }
197 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198}
199
Jeff Browncc0c1592011-02-19 05:07:28 -0800200bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
201 if (axis >= 0 && axis <= REL_MAX) {
202 AutoMutex _l(mLock);
203
204 Device* device = getDeviceLocked(deviceId);
205 if (device && device->relBitmask) {
206 return test_bit(axis, device->relBitmask);
207 }
208 }
209 return false;
210}
211
Jeff Brown80fd47c2011-05-24 01:07:44 -0700212bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
213 if (property >= 0 && property <= INPUT_PROP_MAX) {
214 AutoMutex _l(mLock);
215
216 Device* device = getDeviceLocked(deviceId);
217 if (device && device->propBitmask) {
218 return test_bit(property, device->propBitmask);
219 }
220 }
221 return false;
222}
223
Jeff Brown6d0fec22010-07-23 21:28:06 -0700224int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700225 if (scanCode >= 0 && scanCode <= KEY_MAX) {
226 AutoMutex _l(mLock);
227
Jeff Brown90655042010-12-02 13:50:46 -0800228 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229 if (device != NULL) {
230 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700233 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234}
235
Jeff Brown90655042010-12-02 13:50:46 -0800236int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const {
Jeff Brownfd035822010-06-30 16:10:35 -0700237 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700238 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200239 if (ioctl(device->fd,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700241 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700242 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700243 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700244}
245
Jeff Brown6d0fec22010-07-23 21:28:06 -0700246int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
247 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700248
Jeff Brown90655042010-12-02 13:50:46 -0800249 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700250 if (device != NULL) {
251 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700253 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254}
255
Jeff Brown90655042010-12-02 13:50:46 -0800256int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const {
257 if (!device->keyMap.haveKeyLayout()) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800258 return AKEY_STATE_UNKNOWN;
259 }
260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800262 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700263
Jeff Brownfd035822010-06-30 16:10:35 -0700264 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200266 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 #if 0
268 for (size_t i=0; i<=KEY_MAX; i++) {
269 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
270 }
271 #endif
272 const size_t N = scanCodes.size();
273 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
274 int32_t sc = scanCodes.itemAt(i);
275 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
276 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700277 return AKEY_STATE_DOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
279 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700280 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700282 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700283}
284
Jeff Brown6d0fec22010-07-23 21:28:06 -0700285int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700286 if (sw >= 0 && sw <= SW_MAX) {
287 AutoMutex _l(mLock);
288
Jeff Brown90655042010-12-02 13:50:46 -0800289 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700290 if (device != NULL) {
291 return getSwitchStateLocked(device, sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700293 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700294 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700295}
296
Jeff Brown90655042010-12-02 13:50:46 -0800297int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const {
Jeff Brownfd035822010-06-30 16:10:35 -0700298 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700299 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200300 if (ioctl(device->fd,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700301 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700302 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700303 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700304 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305}
306
Jeff Brown6d0fec22010-07-23 21:28:06 -0700307bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
308 const int32_t* keyCodes, uint8_t* outFlags) const {
309 AutoMutex _l(mLock);
310
Jeff Brown90655042010-12-02 13:50:46 -0800311 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700312 if (device != NULL) {
313 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
314 }
315 return false;
316}
317
Jeff Brown90655042010-12-02 13:50:46 -0800318bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700319 const int32_t* keyCodes, uint8_t* outFlags) const {
Jeff Brown90655042010-12-02 13:50:46 -0800320 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700321 return false;
322 }
323
324 Vector<int32_t> scanCodes;
325 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
326 scanCodes.clear();
327
Jeff Brown6f2fba42011-02-19 01:08:02 -0800328 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
329 keyCodes[codeIndex], &scanCodes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700330 if (! err) {
331 // check the possible scan codes identified by the layout map against the
332 // map of codes actually emitted by the driver
333 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
334 if (test_bit(scanCodes[sc], device->keyBitmask)) {
335 outFlags[codeIndex] = 1;
336 break;
337 }
338 }
339 }
340 }
341 return true;
342}
343
Jeff Brown6f2fba42011-02-19 01:08:02 -0800344status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700345 int32_t* outKeycode, uint32_t* outFlags) const
346{
347 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800348 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700349
Jeff Brown90655042010-12-02 13:50:46 -0800350 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800351 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700352 if (err == NO_ERROR) {
353 return NO_ERROR;
354 }
355 }
356
Jeff Brown90655042010-12-02 13:50:46 -0800357 if (mBuiltInKeyboardId != -1) {
358 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700359
Jeff Brown90655042010-12-02 13:50:46 -0800360 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800361 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700362 if (err == NO_ERROR) {
363 return NO_ERROR;
364 }
365 }
366 }
367
368 *outKeycode = 0;
369 *outFlags = 0;
370 return NAME_NOT_FOUND;
371}
372
Jeff Brown85297452011-03-04 13:07:49 -0800373status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800374{
375 AutoMutex _l(mLock);
376 Device* device = getDeviceLocked(deviceId);
377
378 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800379 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800380 if (err == NO_ERROR) {
381 return NO_ERROR;
382 }
383 }
384
385 if (mBuiltInKeyboardId != -1) {
386 device = getDeviceLocked(mBuiltInKeyboardId);
387
388 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800389 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800390 if (err == NO_ERROR) {
391 return NO_ERROR;
392 }
393 }
394 }
395
Jeff Brown6f2fba42011-02-19 01:08:02 -0800396 return NAME_NOT_FOUND;
397}
398
Jeff Brown1a84fd12011-06-02 01:26:32 -0700399void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700400 AutoMutex _l(mLock);
401
Jeff Brown1a84fd12011-06-02 01:26:32 -0700402 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400403}
404
Jeff Brown497a92c2010-09-12 17:55:08 -0700405bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
406 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800407 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700408 if (device) {
409 uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)];
410 memset(bitmask, 0, sizeof(bitmask));
411 if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) {
412 if (test_bit(led, bitmask)) {
413 return true;
414 }
415 }
416 }
417 return false;
418}
419
420void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
421 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800422 Device* device = getDeviceLocked(deviceId);
Jeff Brown497a92c2010-09-12 17:55:08 -0700423 if (device) {
424 struct input_event ev;
425 ev.time.tv_sec = 0;
426 ev.time.tv_usec = 0;
427 ev.type = EV_LED;
428 ev.code = led;
429 ev.value = on ? 1 : 0;
430
431 ssize_t nWrite;
432 do {
433 nWrite = write(device->fd, &ev, sizeof(struct input_event));
434 } while (nWrite == -1 && errno == EINTR);
435 }
436}
437
Jeff Brown90655042010-12-02 13:50:46 -0800438void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
439 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
440 outVirtualKeys.clear();
441
442 AutoMutex _l(mLock);
443 Device* device = getDeviceLocked(deviceId);
444 if (device && device->virtualKeyMap) {
445 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
446 }
447}
448
449EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
450 if (deviceId == 0) {
451 deviceId = mBuiltInKeyboardId;
452 }
453
454 size_t numDevices = mDevices.size();
455 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < numDevices; i++) {
456 Device* device = mDevices[i];
457 if (device->id == deviceId) {
458 return device;
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 return NULL;
462}
463
Jeff Brownb7198742011-03-18 18:14:26 -0700464size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
465 // Note that we only allow one caller to getEvents(), so don't need
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 // to do locking here... only when adding/removing devices.
Jeff Brownb6110c22011-04-01 16:15:13 -0700467 LOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400468
469 if (!mOpened) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700470 android_atomic_acquire_store(0, &mNeedToReopenDevices);
471
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400472 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
473 mOpened = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700474 mNeedToScanDevices = true;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400475 }
476
Jeff Brownb7198742011-03-18 18:14:26 -0700477 struct input_event readBuffer[bufferSize];
478
479 RawEvent* event = buffer;
480 size_t capacity = bufferSize;
Jeff Browncc2e7172010-08-17 16:48:25 -0700481 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700482 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
483
Jeff Brown1a84fd12011-06-02 01:26:32 -0700484 // Reopen input devices if needed.
485 if (android_atomic_acquire_load(&mNeedToReopenDevices)) {
486 android_atomic_acquire_store(0, &mNeedToReopenDevices);
487
488 LOGI("Reopening all input devices due to a configuration change.");
489
490 AutoMutex _l(mLock);
491 while (mDevices.size() > 1) {
492 closeDeviceAtIndexLocked(mDevices.size() - 1);
493 }
494 mNeedToScanDevices = true;
495 break; // return to the caller before we actually rescan
496 }
497
Jeff Browncc2e7172010-08-17 16:48:25 -0700498 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700499 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800500 Device* device = mClosingDevices;
501 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 device->id, device->path.string());
503 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700504 event->when = now;
505 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
506 event->type = DEVICE_REMOVED;
507 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700509 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700510 if (--capacity == 0) {
511 break;
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700514
Jeff Brown1a84fd12011-06-02 01:26:32 -0700515 if (mNeedToScanDevices) {
516 mNeedToScanDevices = false;
517 scanDevices();
518 mNeedToSendFinishedDeviceScan = true;
519 }
520
Jeff Brownb7198742011-03-18 18:14:26 -0700521 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800522 Device* device = mOpeningDevices;
523 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 device->id, device->path.string());
525 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700526 event->when = now;
527 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
528 event->type = DEVICE_ADDED;
529 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700530 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700531 if (--capacity == 0) {
532 break;
533 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700534 }
535
536 if (mNeedToSendFinishedDeviceScan) {
537 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700538 event->when = now;
539 event->type = FINISHED_DEVICE_SCAN;
540 event += 1;
541 if (--capacity == 0) {
542 break;
543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
545
Jeff Browncc2e7172010-08-17 16:48:25 -0700546 // Grab the next input event.
Jeff Brownb7198742011-03-18 18:14:26 -0700547 // mInputFdIndex is initially 1 because index 0 is used for inotify.
Jeff Brown33bbfd22011-02-24 20:55:35 -0800548 bool deviceWasRemoved = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700549 while (mInputFdIndex < mFds.size()) {
Jeff Brown90655042010-12-02 13:50:46 -0800550 const struct pollfd& pfd = mFds[mInputFdIndex];
Jeff Browncc2e7172010-08-17 16:48:25 -0700551 if (pfd.revents & POLLIN) {
Jeff Brownb7198742011-03-18 18:14:26 -0700552 int32_t readSize = read(pfd.fd, readBuffer, sizeof(struct input_event) * capacity);
Jeff Browncc2e7172010-08-17 16:48:25 -0700553 if (readSize < 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -0800554 if (errno == ENODEV) {
555 deviceWasRemoved = true;
556 break;
557 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700558 if (errno != EAGAIN && errno != EINTR) {
559 LOGW("could not get event (errno=%d)", errno);
560 }
561 } else if ((readSize % sizeof(struct input_event)) != 0) {
562 LOGE("could not get event (wrong size: %d)", readSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700563 } else if (readSize == 0) { // eof
564 deviceWasRemoved = true;
565 break;
Jeff Browncc2e7172010-08-17 16:48:25 -0700566 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700567 const Device* device = mDevices[mInputFdIndex];
568 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
569
570 size_t count = size_t(readSize) / sizeof(struct input_event);
571 for (size_t i = 0; i < count; i++) {
572 const struct input_event& iev = readBuffer[i];
573 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
574 device->path.string(),
575 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
576 iev.type, iev.code, iev.value);
577
Jeff Brown4e91a182011-04-07 11:38:09 -0700578#ifdef HAVE_POSIX_CLOCKS
579 // Use the time specified in the event instead of the current time
580 // so that downstream code can get more accurate estimates of
581 // event dispatch latency from the time the event is enqueued onto
582 // the evdev client buffer.
583 //
584 // The event's timestamp fortuitously uses the same monotonic clock
585 // time base as the rest of Android. The kernel event device driver
586 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
587 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
588 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
589 // system call that also queries ktime_get_ts().
590 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
591 + nsecs_t(iev.time.tv_usec) * 1000LL;
592 LOGV("event time %lld, now %lld", event->when, now);
593#else
Jeff Brownb7198742011-03-18 18:14:26 -0700594 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700595#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700596 event->deviceId = deviceId;
597 event->type = iev.type;
598 event->scanCode = iev.code;
599 event->value = iev.value;
600 event->keyCode = AKEYCODE_UNKNOWN;
601 event->flags = 0;
602 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
603 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
604 &event->keyCode, &event->flags);
605 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
606 iev.code, event->keyCode, event->flags, err);
607 }
608 event += 1;
609 }
610 capacity -= count;
611 if (capacity == 0) {
612 break;
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615 }
Jeff Brownb7198742011-03-18 18:14:26 -0700616 mInputFdIndex += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700618
Jeff Brown33bbfd22011-02-24 20:55:35 -0800619 // Handle the case where a device has been removed but INotify has not yet noticed.
620 if (deviceWasRemoved) {
621 AutoMutex _l(mLock);
622 closeDeviceAtIndexLocked(mInputFdIndex);
623 continue; // report added or removed devices immediately
624 }
625
Jeff Browna9b84222010-10-14 02:23:43 -0700626#if HAVE_INOTIFY
Jeff Brown7342bb92010-10-01 18:55:43 -0700627 // readNotify() will modify mFDs and mFDCount, so this must be done after
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 // processing all other events.
Jeff Brown90655042010-12-02 13:50:46 -0800629 if(mFds[0].revents & POLLIN) {
630 readNotify(mFds[0].fd);
631 mFds.editItemAt(0).revents = 0;
Jeff Brownb7198742011-03-18 18:14:26 -0700632 mInputFdIndex = mFds.size();
Jeff Browna9b84222010-10-14 02:23:43 -0700633 continue; // report added or removed devices immediately
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
Jeff Browna9b84222010-10-14 02:23:43 -0700635#endif
636
Jeff Brownb7198742011-03-18 18:14:26 -0700637 // Return now if we have collected any events, otherwise poll.
638 if (event != buffer) {
639 break;
640 }
641
Jeff Browncc2e7172010-08-17 16:48:25 -0700642 // Poll for events. Mind the wake lock dance!
643 // We hold a wake lock at all times except during poll(). This works due to some
644 // subtle choreography. When a device driver has pending (unread) events, it acquires
645 // a kernel wake lock. However, once the last pending event has been read, the device
646 // driver will release the kernel wake lock. To prevent the system from going to sleep
647 // when this happens, the EventHub holds onto its own user wake lock while the client
648 // is processing events. Thus the system can only sleep if there are no events
649 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700650 //
651 // The timeout is advisory only. If the device is asleep, it will not wake just to
652 // service the timeout.
Jeff Browncc2e7172010-08-17 16:48:25 -0700653 release_wake_lock(WAKE_LOCK_ID);
654
Jeff Brownaa3855d2011-03-17 01:34:19 -0700655 int pollResult = poll(mFds.editArray(), mFds.size(), timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700656
657 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
658
Jeff Brownaa3855d2011-03-17 01:34:19 -0700659 if (pollResult == 0) {
Jeff Brownb7198742011-03-18 18:14:26 -0700660 break; // timed out
Jeff Brownaa3855d2011-03-17 01:34:19 -0700661 }
662 if (pollResult < 0) {
Jeff Brownb7198742011-03-18 18:14:26 -0700663 // Sleep after errors to avoid locking up the system.
664 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700665 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700666 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700667 usleep(100000);
668 }
Jeff Brownb7198742011-03-18 18:14:26 -0700669 } else {
670 // On an SMP system, it is possible for the framework to read input events
671 // faster than the kernel input device driver can produce a complete packet.
672 // Because poll() wakes up as soon as the first input event becomes available,
673 // the framework will often end up reading one event at a time until the
674 // packet is complete. Instead of one call to read() returning 71 events,
675 // it could take 71 calls to read() each returning 1 event.
676 //
677 // Sleep for a short period of time after waking up from the poll() to give
678 // the kernel time to finish writing the entire packet of input events.
679 if (mNumCpus > 1) {
680 usleep(250);
681 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700682 }
Jeff Brown33bbfd22011-02-24 20:55:35 -0800683
684 // Prepare to process all of the FDs we just polled.
Jeff Brownb7198742011-03-18 18:14:26 -0700685 mInputFdIndex = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
Jeff Brownb7198742011-03-18 18:14:26 -0700687
688 // All done, return the number of events we read.
689 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690}
691
692/*
693 * Open the platform-specific input device.
694 */
Jeff Brown90655042010-12-02 13:50:46 -0800695bool EventHub::openPlatformInput(void) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 /*
697 * Open platform-specific input device(s).
698 */
Jeff Brown90655042010-12-02 13:50:46 -0800699 int res, fd;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701#ifdef HAVE_INOTIFY
Jeff Brown90655042010-12-02 13:50:46 -0800702 fd = inotify_init();
703 res = inotify_add_watch(fd, DEVICE_PATH, IN_DELETE | IN_CREATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800705 LOGE("could not add watch for %s, %s\n", DEVICE_PATH, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707#else
708 /*
709 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
710 * We allocate space for it and set it to something invalid.
711 */
Jeff Brown90655042010-12-02 13:50:46 -0800712 fd = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713#endif
714
Jeff Brown90655042010-12-02 13:50:46 -0800715 // Reserve fd index 0 for inotify.
716 struct pollfd pollfd;
717 pollfd.fd = fd;
718 pollfd.events = POLLIN;
719 pollfd.revents = 0;
720 mFds.push(pollfd);
721 mDevices.push(NULL);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700722 return true;
723}
Jeff Brown90655042010-12-02 13:50:46 -0800724
Jeff Brown1a84fd12011-06-02 01:26:32 -0700725void EventHub::scanDevices() {
726 int res = scanDir(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800728 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730}
731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732// ----------------------------------------------------------------------------
733
Jeff Brownfd035822010-06-30 16:10:35 -0700734static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
735 const uint8_t* end = array + endIndex;
736 array += startIndex;
737 while (array != end) {
738 if (*(array++) != 0) {
739 return true;
740 }
741 }
742 return false;
743}
744
745static const int32_t GAMEPAD_KEYCODES[] = {
746 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
747 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
748 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
749 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
750 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800751 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
752 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
753 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
754 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
755 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd035822010-06-30 16:10:35 -0700756};
757
Jeff Brown90655042010-12-02 13:50:46 -0800758int EventHub::openDevice(const char *devicePath) {
759 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760
Jeff Brown90655042010-12-02 13:50:46 -0800761 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762
763 AutoMutex _l(mLock);
Nick Pellye6b1bbd2010-01-20 19:36:49 -0800764
Jeff Brown90655042010-12-02 13:50:46 -0800765 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800767 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 return -1;
769 }
770
Jeff Brown90655042010-12-02 13:50:46 -0800771 InputDeviceIdentifier identifier;
772
773 // Get device name.
774 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
775 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
776 } else {
777 buffer[sizeof(buffer) - 1] = '\0';
778 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400780
Jeff Brown90655042010-12-02 13:50:46 -0800781 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700782 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
783 const String8& item = mExcludedDevices.itemAt(i);
784 if (identifier.name == item) {
785 LOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400786 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400787 return -1;
788 }
789 }
790
Jeff Brown90655042010-12-02 13:50:46 -0800791 // Get device driver version.
792 int driverVersion;
793 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
794 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
795 close(fd);
796 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798
Jeff Brown90655042010-12-02 13:50:46 -0800799 // Get device identifier.
800 struct input_id inputId;
801 if(ioctl(fd, EVIOCGID, &inputId)) {
802 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
803 close(fd);
804 return -1;
805 }
806 identifier.bus = inputId.bustype;
807 identifier.product = inputId.product;
808 identifier.vendor = inputId.vendor;
809 identifier.version = inputId.version;
810
811 // Get device physical location.
812 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
813 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
814 } else {
815 buffer[sizeof(buffer) - 1] = '\0';
816 identifier.location.setTo(buffer);
817 }
818
819 // Get device unique id.
820 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
821 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
822 } else {
823 buffer[sizeof(buffer) - 1] = '\0';
824 identifier.uniqueId.setTo(buffer);
825 }
826
827 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700828 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
829 LOGE("Error %d making device file descriptor non-blocking.", errno);
830 close(fd);
831 return -1;
832 }
833
Jeff Brown90655042010-12-02 13:50:46 -0800834 // Allocate device. (The device object takes ownership of the fd at this point.)
835 int32_t deviceId = mNextDeviceId++;
836 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
838#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800839 LOGI("add device %d: %s\n", deviceId, devicePath);
840 LOGI(" bus: %04x\n"
841 " vendor %04x\n"
842 " product %04x\n"
843 " version %04x\n",
844 identifier.bus, identifier.vendor, identifier.product, identifier.version);
845 LOGI(" name: \"%s\"\n", identifier.name.string());
846 LOGI(" location: \"%s\"\n", identifier.location.string());
847 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
848 LOGI(" driver: v%d.%d.%d\n",
849 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850#endif
851
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800852 // Load the configuration file for the device.
853 loadConfiguration(device);
854
Jeff Brownfd035822010-06-30 16:10:35 -0700855 // Figure out the kinds of events the device reports.
Jeff Brownfd035822010-06-30 16:10:35 -0700856 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800858 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
Jeff Brownfd035822010-06-30 16:10:35 -0700859
Jeff Brown6f2fba42011-02-19 01:08:02 -0800860 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
861 memset(abs_bitmask, 0, sizeof(abs_bitmask));
862 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
Jeff Brownfd035822010-06-30 16:10:35 -0700863
Jeff Brown6f2fba42011-02-19 01:08:02 -0800864 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
865 memset(rel_bitmask, 0, sizeof(rel_bitmask));
866 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask);
Jeff Brownfd035822010-06-30 16:10:35 -0700867
Jeff Brown6f2fba42011-02-19 01:08:02 -0800868 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
869 memset(sw_bitmask, 0, sizeof(sw_bitmask));
870 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask);
871
Jeff Brown80fd47c2011-05-24 01:07:44 -0700872 uint8_t prop_bitmask[sizeof_bit_array(INPUT_PROP_MAX + 1)];
873 memset(prop_bitmask, 0, sizeof(prop_bitmask));
874 ioctl(fd, EVIOCGPROP(sizeof(prop_bitmask)), prop_bitmask);
875
Jeff Browncc0c1592011-02-19 05:07:28 -0800876 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
Jeff Brown80fd47c2011-05-24 01:07:44 -0700877 device->relBitmask = new uint8_t[sizeof(rel_bitmask)];
878 device->propBitmask = new uint8_t[sizeof(prop_bitmask)];
879
880 if (!device->keyBitmask || !device->relBitmask || !device->propBitmask) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800881 delete device;
Jeff Brown80fd47c2011-05-24 01:07:44 -0700882 LOGE("out of memory allocating bitmasks");
Jeff Browncc0c1592011-02-19 05:07:28 -0800883 return -1;
884 }
885
Jeff Brown80fd47c2011-05-24 01:07:44 -0700886 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
887 memcpy(device->relBitmask, rel_bitmask, sizeof(rel_bitmask));
888 memcpy(device->propBitmask, prop_bitmask, sizeof(prop_bitmask));
Jeff Browncc0c1592011-02-19 05:07:28 -0800889
Jeff Brown6f2fba42011-02-19 01:08:02 -0800890 // See if this is a keyboard. Ignore everything in the button range except for
891 // joystick and gamepad buttons which are handled like keyboards for the most part.
892 bool haveKeyboardKeys = containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
893 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
894 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800895 bool haveGamepadButtons = containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_MISC),
896 sizeof_bit_array(BTN_MOUSE))
897 || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_JOYSTICK),
898 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800899 if (haveKeyboardKeys || haveGamepadButtons) {
900 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800902
Jeff Brown83c09682010-12-23 17:50:18 -0800903 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800904 if (test_bit(BTN_MOUSE, key_bitmask)
905 && test_bit(REL_X, rel_bitmask)
906 && test_bit(REL_Y, rel_bitmask)) {
907 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
Jeff Brownfd035822010-06-30 16:10:35 -0700909
910 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800911 // Is this a new modern multi-touch driver?
912 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
913 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
914 // Some joysticks such as the PS3 controller report axes that conflict
915 // with the ABS_MT range. Try to confirm that the device really is
916 // a touch screen.
917 if (test_bit(BTN_TOUCH, key_bitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800918 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd035822010-06-30 16:10:35 -0700919 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800920 // Is this an old style single-touch driver?
921 } else if (test_bit(BTN_TOUCH, key_bitmask)
922 && test_bit(ABS_X, abs_bitmask)
923 && test_bit(ABS_Y, abs_bitmask)) {
924 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
926
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800927 // See if this device is a joystick.
928 // Ignore touchscreens because they use the same absolute axes for other purposes.
929 // Assumes that joysticks always have gamepad buttons in order to distinguish them
930 // from other devices such as accelerometers that also have absolute axes.
931 if (haveGamepadButtons
932 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)
933 && containsNonZeroByte(abs_bitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
934 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
935 }
936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 // figure out the switches this device reports
Jeff Brown6f2fba42011-02-19 01:08:02 -0800938 bool haveSwitches = false;
939 for (int i=0; i<EV_SW; i++) {
940 //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
941 if (test_bit(i, sw_bitmask)) {
942 haveSwitches = true;
943 if (mSwitches[i] == 0) {
944 mSwitches[i] = device->id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 }
946 }
947 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800948 if (haveSwitches) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700949 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951
Jeff Brown58a2da82011-01-25 16:02:22 -0800952 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800953 // Load the virtual keys for the touch screen, if any.
954 // We do this now so that we can make sure to load the keymap if necessary.
955 status_t status = loadVirtualKeyMap(device);
956 if (!status) {
957 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
Jeff Brown90655042010-12-02 13:50:46 -0800959 }
960
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800961 // Load the key map.
962 // We need to do this for joysticks too because the key layout may specify axes.
963 status_t keyMapStatus = NAME_NOT_FOUND;
964 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -0800965 // Load the keymap for the device.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800966 keyMapStatus = loadKeyMap(device);
967 }
Jeff Brown90655042010-12-02 13:50:46 -0800968
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800969 // Configure the keyboard, gamepad or virtual keyboard.
970 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -0800971 // Set system properties for the keyboard.
Jeff Brown497a92c2010-09-12 17:55:08 -0700972 setKeyboardProperties(device, false);
973
Jeff Brown90655042010-12-02 13:50:46 -0800974 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800975 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -0800976 && mBuiltInKeyboardId == -1
977 && isEligibleBuiltInKeyboard(device->identifier,
978 device->configuration, &device->keyMap)) {
979 mBuiltInKeyboardId = device->id;
980 setKeyboardProperties(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -0700981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700983 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -0700984 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700985 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700986 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700987
Jeff Brownfd035822010-06-30 16:10:35 -0700988 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -0700989 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
990 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
991 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
992 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
993 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700994 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700995 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700996
Jeff Brownfd035822010-06-30 16:10:35 -0700997 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -0700998 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700999 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd035822010-06-30 16:10:35 -07001000 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1001 break;
1002 }
1003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005
Sean McNeilaeb00c42010-06-23 16:00:37 +07001006 // If the device isn't recognized as something we handle, don't monitor it.
1007 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -08001008 LOGV("Dropping device: id=%d, path='%s', name='%s'",
1009 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001010 delete device;
1011 return -1;
1012 }
1013
Jeff Brown56194eb2011-03-02 19:23:13 -08001014 // Determine whether the device is external or internal.
1015 if (isExternalDevice(device)) {
1016 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1017 }
1018
Jeff Brown90655042010-12-02 13:50:46 -08001019 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1020 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
1021 deviceId, fd, devicePath, device->identifier.name.string(),
1022 device->classes,
1023 device->configurationFile.string(),
1024 device->keyMap.keyLayoutFile.string(),
1025 device->keyMap.keyCharacterMapFile.string(),
1026 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001027
Jeff Brown90655042010-12-02 13:50:46 -08001028 struct pollfd pollfd;
1029 pollfd.fd = fd;
1030 pollfd.events = POLLIN;
1031 pollfd.revents = 0;
1032 mFds.push(pollfd);
1033 mDevices.push(device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 device->next = mOpeningDevices;
1036 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 return 0;
1038}
1039
Jeff Brown90655042010-12-02 13:50:46 -08001040void EventHub::loadConfiguration(Device* device) {
1041 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1042 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001043 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -08001044 LOGD("No input device configuration file found for device '%s'.",
1045 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001046 } else {
1047 status_t status = PropertyMap::load(device->configurationFile,
1048 &device->configuration);
1049 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -08001050 LOGE("Error loading input device configuration file for device '%s'. "
1051 "Using default configuration.",
1052 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001053 }
1054 }
1055}
1056
Jeff Brown90655042010-12-02 13:50:46 -08001057status_t EventHub::loadVirtualKeyMap(Device* device) {
1058 // The virtual key map is supplied by the kernel as a system board property file.
1059 String8 path;
1060 path.append("/sys/board_properties/virtualkeys.");
1061 path.append(device->identifier.name);
1062 if (access(path.string(), R_OK)) {
1063 return NAME_NOT_FOUND;
1064 }
1065 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001066}
1067
Jeff Brown90655042010-12-02 13:50:46 -08001068status_t EventHub::loadKeyMap(Device* device) {
1069 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001070}
1071
Jeff Brown90655042010-12-02 13:50:46 -08001072void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) {
1073 int32_t id = builtInKeyboard ? 0 : device->id;
1074 android::setKeyboardProperties(id, device->identifier,
1075 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1076}
1077
1078void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) {
1079 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001080 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -07001081}
1082
Jeff Brown56194eb2011-03-02 19:23:13 -08001083bool EventHub::isExternalDevice(Device* device) {
1084 if (device->configuration) {
1085 bool value;
1086 if (device->configuration->tryGetProperty(String8("device.internal"), value)
1087 && value) {
1088 return false;
1089 }
1090 }
1091 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1092}
1093
Jeff Brown90655042010-12-02 13:50:46 -08001094bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1095 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001096 return false;
1097 }
1098
1099 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001100 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001101 const size_t N = scanCodes.size();
1102 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1103 int32_t sc = scanCodes.itemAt(i);
1104 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1105 return true;
1106 }
1107 }
1108
1109 return false;
1110}
1111
Jeff Brown90655042010-12-02 13:50:46 -08001112int EventHub::closeDevice(const char *devicePath) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 AutoMutex _l(mLock);
Jeff Brown7342bb92010-10-01 18:55:43 -07001114
Jeff Brown90655042010-12-02 13:50:46 -08001115 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1116 Device* device = mDevices[i];
1117 if (device->path == devicePath) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001118 return closeDeviceAtIndexLocked(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001121 LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 return -1;
1123}
1124
Jeff Brown33bbfd22011-02-24 20:55:35 -08001125int EventHub::closeDeviceAtIndexLocked(int index) {
1126 Device* device = mDevices[index];
1127 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1128 device->path.string(), device->identifier.name.string(), device->id,
1129 device->fd, device->classes);
1130
1131 for (int j=0; j<EV_SW; j++) {
1132 if (mSwitches[j] == device->id) {
1133 mSwitches[j] = 0;
1134 }
1135 }
1136
1137 if (device->id == mBuiltInKeyboardId) {
1138 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
1139 device->path.string(), mBuiltInKeyboardId);
1140 mBuiltInKeyboardId = -1;
1141 clearKeyboardProperties(device, true);
1142 }
1143 clearKeyboardProperties(device, false);
1144
1145 mFds.removeAt(index);
1146 mDevices.removeAt(index);
1147 device->close();
1148
Jeff Brown8e9d4432011-03-12 19:46:59 -08001149 // Unlink for opening devices list if it is present.
1150 Device* pred = NULL;
1151 bool found = false;
1152 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1153 if (entry == device) {
1154 found = true;
1155 break;
1156 }
1157 pred = entry;
1158 entry = entry->next;
1159 }
1160 if (found) {
1161 // Unlink the device from the opening devices list then delete it.
1162 // We don't need to tell the client that the device was closed because
1163 // it does not even know it was opened in the first place.
1164 LOGI("Device %s was immediately closed after opening.", device->path.string());
1165 if (pred) {
1166 pred->next = device->next;
1167 } else {
1168 mOpeningDevices = device->next;
1169 }
1170 delete device;
1171 } else {
1172 // Link into closing devices list.
1173 // The device will be deleted later after we have informed the client.
1174 device->next = mClosingDevices;
1175 mClosingDevices = device;
1176 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001177 return 0;
1178}
1179
Jeff Brown7342bb92010-10-01 18:55:43 -07001180int EventHub::readNotify(int nfd) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181#ifdef HAVE_INOTIFY
1182 int res;
1183 char devname[PATH_MAX];
1184 char *filename;
1185 char event_buf[512];
1186 int event_size;
1187 int event_pos = 0;
1188 struct inotify_event *event;
1189
Jeff Brown7342bb92010-10-01 18:55:43 -07001190 LOGV("EventHub::readNotify nfd: %d\n", nfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 res = read(nfd, event_buf, sizeof(event_buf));
1192 if(res < (int)sizeof(*event)) {
1193 if(errno == EINTR)
1194 return 0;
1195 LOGW("could not get event, %s\n", strerror(errno));
1196 return 1;
1197 }
1198 //printf("got %d bytes of event information\n", res);
1199
Jeff Brown90655042010-12-02 13:50:46 -08001200 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 filename = devname + strlen(devname);
1202 *filename++ = '/';
1203
1204 while(res >= (int)sizeof(*event)) {
1205 event = (struct inotify_event *)(event_buf + event_pos);
1206 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1207 if(event->len) {
1208 strcpy(filename, event->name);
1209 if(event->mask & IN_CREATE) {
Jeff Brown7342bb92010-10-01 18:55:43 -07001210 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 else {
Jeff Brown7342bb92010-10-01 18:55:43 -07001213 closeDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215 }
1216 event_size = sizeof(*event) + event->len;
1217 res -= event_size;
1218 event_pos += event_size;
1219 }
1220#endif
1221 return 0;
1222}
1223
Jeff Brown7342bb92010-10-01 18:55:43 -07001224int EventHub::scanDir(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225{
1226 char devname[PATH_MAX];
1227 char *filename;
1228 DIR *dir;
1229 struct dirent *de;
1230 dir = opendir(dirname);
1231 if(dir == NULL)
1232 return -1;
1233 strcpy(devname, dirname);
1234 filename = devname + strlen(devname);
1235 *filename++ = '/';
1236 while((de = readdir(dir))) {
1237 if(de->d_name[0] == '.' &&
1238 (de->d_name[1] == '\0' ||
1239 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1240 continue;
1241 strcpy(filename, de->d_name);
Jeff Brown7342bb92010-10-01 18:55:43 -07001242 openDevice(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244 closedir(dir);
1245 return 0;
1246}
1247
Jeff Brown1a84fd12011-06-02 01:26:32 -07001248void EventHub::reopenDevices() {
1249 android_atomic_release_store(1, &mNeedToReopenDevices);
1250}
1251
Jeff Brownf2f487182010-10-01 17:46:21 -07001252void EventHub::dump(String8& dump) {
1253 dump.append("Event Hub State:\n");
1254
1255 { // acquire lock
1256 AutoMutex _l(mLock);
1257
Jeff Brown90655042010-12-02 13:50:46 -08001258 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001259
1260 dump.append(INDENT "Devices:\n");
1261
Jeff Brown90655042010-12-02 13:50:46 -08001262 for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1263 const Device* device = mDevices[i];
Jeff Brownf2f487182010-10-01 17:46:21 -07001264 if (device) {
Jeff Brown90655042010-12-02 13:50:46 -08001265 if (mBuiltInKeyboardId == device->id) {
1266 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1267 device->id, device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001268 } else {
Jeff Brown90655042010-12-02 13:50:46 -08001269 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1270 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001271 }
1272 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1273 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
Jeff Brown90655042010-12-02 13:50:46 -08001274 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1275 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1276 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1277 "product=0x%04x, version=0x%04x\n",
1278 device->identifier.bus, device->identifier.vendor,
1279 device->identifier.product, device->identifier.version);
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001280 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001281 device->keyMap.keyLayoutFile.string());
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001282 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
Jeff Brown90655042010-12-02 13:50:46 -08001283 device->keyMap.keyCharacterMapFile.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001284 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1285 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001286 }
1287 }
1288 } // release lock
1289}
1290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291}; // namespace android