blob: 4302678f2872e53fbcd0445b068ad78ce70fed34 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001//
2// Copyright 2005 The Android Open Source Project
3//
4// Handle events, like key input and vsync.
5//
6// The goal is to provide an optimized solution for Linux, not an
7// implementation that works well across all platforms. We expect
8// events to arrive on file descriptors, so that we can use a select()
9// select() call to sleep.
10//
11// We can't select() on anything but network sockets in Windows, so we
12// provide an alternative implementation of waitEvent for that platform.
13//
14#define LOG_TAG "EventHub"
15
16//#define LOG_NDEBUG 0
17
18#include <ui/EventHub.h>
Dianne Hackbornc5917362009-08-04 05:49:43 -070019#include <ui/KeycodeLabels.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <hardware_legacy/power.h>
21
22#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
24#include <utils/Timers.h>
Mathias Agopiane0c32202009-05-31 19:13:00 -070025#include <utils/threads.h>
Mathias Agopiane0c32202009-05-31 19:13:00 -070026#include <utils/Errors.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <stdlib.h>
29#include <stdio.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <memory.h>
33#include <errno.h>
34#include <assert.h>
35
36#include "KeyLayoutMap.h"
37
38#include <string.h>
39#include <stdint.h>
40#include <dirent.h>
41#ifdef HAVE_INOTIFY
42# include <sys/inotify.h>
43#endif
44#ifdef HAVE_ANDROID_OS
45# include <sys/limits.h> /* not part of Linux */
46#endif
47#include <sys/poll.h>
48#include <sys/ioctl.h>
49
50/* this macro is used to tell if "bit" is set in "array"
51 * it selects a byte from the array, and does a boolean AND
52 * operation with a byte that only has the relevant bit set.
53 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
54 */
55#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
56
Jeff Brown8575a872010-06-30 16:10:35 -070057/* this macro computes the number of bytes needed to represent a bit array of the specified size */
58#define sizeof_bit_array(bits) ((bits + 7) / 8)
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#define ID_MASK 0x0000ffff
61#define SEQ_MASK 0x7fff0000
62#define SEQ_SHIFT 16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Dianne Hackbornc5917362009-08-04 05:49:43 -070064#ifndef ABS_MT_TOUCH_MAJOR
65#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
66#endif
67
68#ifndef ABS_MT_POSITION_X
69#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
70#endif
71
72#ifndef ABS_MT_POSITION_Y
73#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
74#endif
75
Jeff Brown2806e382010-10-01 17:46:21 -070076#define INDENT " "
77#define INDENT2 " "
78#define INDENT3 " "
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080namespace android {
81
82static const char *WAKE_LOCK_ID = "KeyEvents";
83static const char *device_path = "/dev/input";
84
85/* return the larger integer */
86static inline int max(int v1, int v2)
87{
88 return (v1 > v2) ? v1 : v2;
89}
90
Jeff Brown2806e382010-10-01 17:46:21 -070091static inline const char* toString(bool value) {
92 return value ? "true" : "false";
93}
94
Iliyan Malchev34193b32009-08-06 14:50:08 -070095EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name)
96 : id(_id), path(_path), name(name), classes(0)
Michaël Burtin5aa3d792011-07-29 14:25:47 +020097 , keyBitmask(NULL), switchBitmask(NULL)
98 , layoutMap(new KeyLayoutMap()), fd(-1), next(NULL) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099}
100
101EventHub::device_t::~device_t() {
102 delete [] keyBitmask;
Michaël Burtin5aa3d792011-07-29 14:25:47 +0200103 delete [] switchBitmask;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 delete layoutMap;
105}
106
107EventHub::EventHub(void)
108 : mError(NO_INIT), mHaveFirstKeyboard(false), mFirstKeyboardId(0)
109 , mDevicesById(0), mNumDevicesById(0)
110 , mOpeningDevices(0), mClosingDevices(0)
Jeff Brown1ad00e92010-10-01 18:55:43 -0700111 , mDevices(0), mFDs(0), mFDCount(0), mOpened(false), mNeedToSendFinishedDeviceScan(false)
Jeff Brown82763072010-08-17 16:48:25 -0700112 , mInputBufferIndex(0), mInputBufferCount(0), mInputDeviceIndex(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113{
114 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
115#ifdef EV_SW
116 memset(mSwitches, 0, sizeof(mSwitches));
117#endif
118}
119
120/*
121 * Clean up.
122 */
123EventHub::~EventHub(void)
124{
125 release_wake_lock(WAKE_LOCK_ID);
126 // we should free stuff here...
127}
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129status_t EventHub::errorCheck() const
130{
131 return mError;
132}
133
134String8 EventHub::getDeviceName(int32_t deviceId) const
135{
136 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700137 device_t* device = getDeviceLocked(deviceId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 if (device == NULL) return String8();
139 return device->name;
140}
141
142uint32_t EventHub::getDeviceClasses(int32_t deviceId) const
143{
144 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700145 device_t* device = getDeviceLocked(deviceId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 if (device == NULL) return 0;
147 return device->classes;
148}
149
Jeff Browne57e8952010-07-23 21:28:06 -0700150status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
151 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700152 outAxisInfo->clear();
Jeff Browne57e8952010-07-23 21:28:06 -0700153
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700155 device_t* device = getDeviceLocked(deviceId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 if (device == NULL) return -1;
157
158 struct input_absinfo info;
159
Jens Gulin7dcaa582010-06-22 22:21:57 +0200160 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Browne57e8952010-07-23 21:28:06 -0700161 LOGW("Error reading absolute controller %d for device %s fd %d\n",
Jens Gulin7dcaa582010-06-22 22:21:57 +0200162 axis, device->name.string(), device->fd);
Jeff Browne57e8952010-07-23 21:28:06 -0700163 return -errno;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 }
Jeff Browne57e8952010-07-23 21:28:06 -0700165
166 if (info.minimum != info.maximum) {
167 outAxisInfo->valid = true;
168 outAxisInfo->minValue = info.minimum;
169 outAxisInfo->maxValue = info.maximum;
170 outAxisInfo->flat = info.flat;
171 outAxisInfo->fuzz = info.fuzz;
172 }
173 return OK;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
Jeff Browne57e8952010-07-23 21:28:06 -0700176int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Browne839a582010-04-22 18:58:52 -0700177 if (scanCode >= 0 && scanCode <= KEY_MAX) {
178 AutoMutex _l(mLock);
179
Jeff Brown2806e382010-10-01 17:46:21 -0700180 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700181 if (device != NULL) {
182 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 }
184 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700185 return AKEY_STATE_UNKNOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
Jeff Browne839a582010-04-22 18:58:52 -0700188int32_t EventHub::getScanCodeStateLocked(device_t* device, int32_t scanCode) const {
Jeff Brown8575a872010-06-30 16:10:35 -0700189 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Browne839a582010-04-22 18:58:52 -0700190 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulin7dcaa582010-06-22 22:21:57 +0200191 if (ioctl(device->fd,
Jeff Browne839a582010-04-22 18:58:52 -0700192 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700193 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Browne839a582010-04-22 18:58:52 -0700194 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700195 return AKEY_STATE_UNKNOWN;
Jeff Browne839a582010-04-22 18:58:52 -0700196}
197
Jeff Browne57e8952010-07-23 21:28:06 -0700198int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
199 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -0700200
Jeff Brown2806e382010-10-01 17:46:21 -0700201 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700202 if (device != NULL) {
203 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700205 return AKEY_STATE_UNKNOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206}
207
Jeff Browne839a582010-04-22 18:58:52 -0700208int32_t EventHub::getKeyCodeStateLocked(device_t* device, int32_t keyCode) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 Vector<int32_t> scanCodes;
Jeff Browne839a582010-04-22 18:58:52 -0700210 device->layoutMap->findScancodes(keyCode, &scanCodes);
211
Jeff Brown8575a872010-06-30 16:10:35 -0700212 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulin7dcaa582010-06-22 22:21:57 +0200214 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 #if 0
216 for (size_t i=0; i<=KEY_MAX; i++) {
217 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
218 }
219 #endif
220 const size_t N = scanCodes.size();
221 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
222 int32_t sc = scanCodes.itemAt(i);
223 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
224 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brown5c1ed842010-07-14 18:48:53 -0700225 return AKEY_STATE_DOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 }
227 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700228 return AKEY_STATE_UP;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700230 return AKEY_STATE_UNKNOWN;
Jeff Browne839a582010-04-22 18:58:52 -0700231}
232
Jeff Browne57e8952010-07-23 21:28:06 -0700233int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Browne839a582010-04-22 18:58:52 -0700234#ifdef EV_SW
235 if (sw >= 0 && sw <= SW_MAX) {
236 AutoMutex _l(mLock);
237
Jeff Brown2806e382010-10-01 17:46:21 -0700238 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700239 if (device != NULL) {
240 return getSwitchStateLocked(device, sw);
Jeff Browne839a582010-04-22 18:58:52 -0700241 }
Jeff Browne839a582010-04-22 18:58:52 -0700242 }
243#endif
Jeff Brown5c1ed842010-07-14 18:48:53 -0700244 return AKEY_STATE_UNKNOWN;
Jeff Browne839a582010-04-22 18:58:52 -0700245}
246
247int32_t EventHub::getSwitchStateLocked(device_t* device, int32_t sw) const {
Michaël Burtin5aa3d792011-07-29 14:25:47 +0200248 if (device->switchBitmask != NULL
249 && test_bit(sw, device->switchBitmask)) {
250 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
251 memset(sw_bitmask, 0, sizeof(sw_bitmask));
252 if (ioctl(device->fd,
253 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
254 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
255 }
Jeff Browne839a582010-04-22 18:58:52 -0700256 }
Jeff Brown5c1ed842010-07-14 18:48:53 -0700257 return AKEY_STATE_UNKNOWN;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258}
259
Jeff Browne57e8952010-07-23 21:28:06 -0700260bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
261 const int32_t* keyCodes, uint8_t* outFlags) const {
262 AutoMutex _l(mLock);
263
Jeff Brown2806e382010-10-01 17:46:21 -0700264 device_t* device = getDeviceLocked(deviceId);
Jeff Browne57e8952010-07-23 21:28:06 -0700265 if (device != NULL) {
266 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
267 }
268 return false;
269}
270
271bool EventHub::markSupportedKeyCodesLocked(device_t* device, size_t numCodes,
272 const int32_t* keyCodes, uint8_t* outFlags) const {
273 if (device->layoutMap == NULL || device->keyBitmask == NULL) {
274 return false;
275 }
276
277 Vector<int32_t> scanCodes;
278 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
279 scanCodes.clear();
280
281 status_t err = device->layoutMap->findScancodes(keyCodes[codeIndex], &scanCodes);
282 if (! err) {
283 // check the possible scan codes identified by the layout map against the
284 // map of codes actually emitted by the driver
285 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
286 if (test_bit(scanCodes[sc], device->keyBitmask)) {
287 outFlags[codeIndex] = 1;
288 break;
289 }
290 }
291 }
292 }
293 return true;
294}
295
Dianne Hackbornc968c3a2009-07-14 12:06:54 -0700296status_t EventHub::scancodeToKeycode(int32_t deviceId, int scancode,
297 int32_t* outKeycode, uint32_t* outFlags) const
298{
299 AutoMutex _l(mLock);
Jeff Brown2806e382010-10-01 17:46:21 -0700300 device_t* device = getDeviceLocked(deviceId);
Dianne Hackbornc968c3a2009-07-14 12:06:54 -0700301
302 if (device != NULL && device->layoutMap != NULL) {
303 status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
304 if (err == NO_ERROR) {
305 return NO_ERROR;
306 }
307 }
308
309 if (mHaveFirstKeyboard) {
Jeff Brown2806e382010-10-01 17:46:21 -0700310 device = getDeviceLocked(mFirstKeyboardId);
Dianne Hackbornc968c3a2009-07-14 12:06:54 -0700311
312 if (device != NULL && device->layoutMap != NULL) {
313 status_t err = device->layoutMap->map(scancode, outKeycode, outFlags);
314 if (err == NO_ERROR) {
315 return NO_ERROR;
316 }
317 }
318 }
319
320 *outKeycode = 0;
321 *outFlags = 0;
322 return NAME_NOT_FOUND;
323}
324
Mike Lockwoodb4411062009-07-16 11:11:18 -0400325void EventHub::addExcludedDevice(const char* deviceName)
326{
Jeff Brown2806e382010-10-01 17:46:21 -0700327 AutoMutex _l(mLock);
328
Mike Lockwoodb4411062009-07-16 11:11:18 -0400329 String8 name(deviceName);
330 mExcludedDevices.push_back(name);
331}
332
Jeff Brown2806e382010-10-01 17:46:21 -0700333EventHub::device_t* EventHub::getDeviceLocked(int32_t deviceId) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334{
335 if (deviceId == 0) deviceId = mFirstKeyboardId;
336 int32_t id = deviceId & ID_MASK;
337 if (id >= mNumDevicesById || id < 0) return NULL;
338 device_t* dev = mDevicesById[id].device;
Dianne Hackbornc3aa00b2009-03-25 16:21:55 -0700339 if (dev == NULL) return NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 if (dev->id == deviceId) {
341 return dev;
342 }
343 return NULL;
344}
345
Jeff Browne57e8952010-07-23 21:28:06 -0700346bool EventHub::getEvent(RawEvent* outEvent)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347{
Jeff Browne57e8952010-07-23 21:28:06 -0700348 outEvent->deviceId = 0;
349 outEvent->type = 0;
350 outEvent->scanCode = 0;
351 outEvent->keyCode = 0;
352 outEvent->flags = 0;
353 outEvent->value = 0;
354 outEvent->when = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800356 // Note that we only allow one caller to getEvent(), so don't need
357 // to do locking here... only when adding/removing devices.
Mike Lockwoodb4411062009-07-16 11:11:18 -0400358
359 if (!mOpened) {
360 mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
361 mOpened = true;
Jeff Brown1ad00e92010-10-01 18:55:43 -0700362 mNeedToSendFinishedDeviceScan = true;
Mike Lockwoodb4411062009-07-16 11:11:18 -0400363 }
364
Jeff Brown82763072010-08-17 16:48:25 -0700365 for (;;) {
366 // Report any devices that had last been added/removed.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 if (mClosingDevices != NULL) {
368 device_t* device = mClosingDevices;
369 LOGV("Reporting device closed: id=0x%x, name=%s\n",
370 device->id, device->path.string());
371 mClosingDevices = device->next;
Jeff Browne57e8952010-07-23 21:28:06 -0700372 if (device->id == mFirstKeyboardId) {
373 outEvent->deviceId = 0;
374 } else {
375 outEvent->deviceId = device->id;
376 }
377 outEvent->type = DEVICE_REMOVED;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700378 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 delete device;
Jeff Brown1ad00e92010-10-01 18:55:43 -0700380 mNeedToSendFinishedDeviceScan = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381 return true;
382 }
Jeff Browne57e8952010-07-23 21:28:06 -0700383
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 if (mOpeningDevices != NULL) {
385 device_t* device = mOpeningDevices;
386 LOGV("Reporting device opened: id=0x%x, name=%s\n",
387 device->id, device->path.string());
388 mOpeningDevices = device->next;
Jeff Browne57e8952010-07-23 21:28:06 -0700389 if (device->id == mFirstKeyboardId) {
390 outEvent->deviceId = 0;
391 } else {
392 outEvent->deviceId = device->id;
393 }
394 outEvent->type = DEVICE_ADDED;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700395 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown1ad00e92010-10-01 18:55:43 -0700396 mNeedToSendFinishedDeviceScan = true;
397 return true;
398 }
399
400 if (mNeedToSendFinishedDeviceScan) {
401 mNeedToSendFinishedDeviceScan = false;
402 outEvent->type = FINISHED_DEVICE_SCAN;
Jeff Brown3c3cc622010-10-20 15:33:38 -0700403 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 return true;
405 }
406
Jeff Brown82763072010-08-17 16:48:25 -0700407 // Grab the next input event.
408 for (;;) {
409 // Consume buffered input events, if any.
410 if (mInputBufferIndex < mInputBufferCount) {
411 const struct input_event& iev = mInputBufferData[mInputBufferIndex++];
412 const device_t* device = mDevices[mInputDeviceIndex];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413
Jeff Brown82763072010-08-17 16:48:25 -0700414 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d", device->path.string(),
415 (int) iev.time.tv_sec, (int) iev.time.tv_usec, iev.type, iev.code, iev.value);
416 if (device->id == mFirstKeyboardId) {
417 outEvent->deviceId = 0;
418 } else {
419 outEvent->deviceId = device->id;
420 }
421 outEvent->type = iev.type;
422 outEvent->scanCode = iev.code;
423 if (iev.type == EV_KEY) {
424 status_t err = device->layoutMap->map(iev.code,
425 & outEvent->keyCode, & outEvent->flags);
426 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
427 iev.code, outEvent->keyCode, outEvent->flags, err);
428 if (err != 0) {
429 outEvent->keyCode = AKEYCODE_UNKNOWN;
430 outEvent->flags = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 }
Jeff Brown82763072010-08-17 16:48:25 -0700432 } else {
433 outEvent->keyCode = iev.code;
434 }
435 outEvent->value = iev.value;
436
437 // Use an event timestamp in the same timebase as
438 // java.lang.System.nanoTime() and android.os.SystemClock.uptimeMillis()
439 // as expected by the rest of the system.
440 outEvent->when = systemTime(SYSTEM_TIME_MONOTONIC);
441 return true;
442 }
443
444 // Finish reading all events from devices identified in previous poll().
445 // This code assumes that mInputDeviceIndex is initially 0 and that the
446 // revents member of pollfd is initialized to 0 when the device is first added.
447 // Since mFDs[0] is used for inotify, we process regular events starting at index 1.
448 mInputDeviceIndex += 1;
449 if (mInputDeviceIndex >= mFDCount) {
Jeff Brown82763072010-08-17 16:48:25 -0700450 break;
451 }
452
Jeff Brown7e40f362010-10-14 02:23:43 -0700453 const struct pollfd& pfd = mFDs[mInputDeviceIndex];
Jeff Brown82763072010-08-17 16:48:25 -0700454 if (pfd.revents & POLLIN) {
455 int32_t readSize = read(pfd.fd, mInputBufferData,
456 sizeof(struct input_event) * INPUT_BUFFER_SIZE);
457 if (readSize < 0) {
458 if (errno != EAGAIN && errno != EINTR) {
459 LOGW("could not get event (errno=%d)", errno);
460 }
461 } else if ((readSize % sizeof(struct input_event)) != 0) {
462 LOGE("could not get event (wrong size: %d)", readSize);
463 } else {
464 mInputBufferCount = readSize / sizeof(struct input_event);
465 mInputBufferIndex = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466 }
467 }
468 }
Jeff Brown82763072010-08-17 16:48:25 -0700469
Jeff Brown7e40f362010-10-14 02:23:43 -0700470#if HAVE_INOTIFY
Jeff Brown1ad00e92010-10-01 18:55:43 -0700471 // readNotify() will modify mFDs and mFDCount, so this must be done after
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472 // processing all other events.
473 if(mFDs[0].revents & POLLIN) {
Jeff Brown1ad00e92010-10-01 18:55:43 -0700474 readNotify(mFDs[0].fd);
Jeff Brown7e40f362010-10-14 02:23:43 -0700475 mFDs[0].revents = 0;
476 continue; // report added or removed devices immediately
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477 }
Jeff Brown7e40f362010-10-14 02:23:43 -0700478#endif
479
480 mInputDeviceIndex = 0;
Jeff Brown82763072010-08-17 16:48:25 -0700481
482 // Poll for events. Mind the wake lock dance!
483 // We hold a wake lock at all times except during poll(). This works due to some
484 // subtle choreography. When a device driver has pending (unread) events, it acquires
485 // a kernel wake lock. However, once the last pending event has been read, the device
486 // driver will release the kernel wake lock. To prevent the system from going to sleep
487 // when this happens, the EventHub holds onto its own user wake lock while the client
488 // is processing events. Thus the system can only sleep if there are no events
489 // pending or currently being processed.
490 release_wake_lock(WAKE_LOCK_ID);
491
492 int pollResult = poll(mFDs, mFDCount, -1);
493
494 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
495
496 if (pollResult <= 0) {
497 if (errno != EINTR) {
Jeff Brown7e40f362010-10-14 02:23:43 -0700498 LOGW("poll failed (errno=%d)\n", errno);
Jeff Brown82763072010-08-17 16:48:25 -0700499 usleep(100000);
500 }
501 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 }
503}
504
505/*
506 * Open the platform-specific input device.
507 */
508bool EventHub::openPlatformInput(void)
509{
510 /*
511 * Open platform-specific input device(s).
512 */
513 int res;
514
515 mFDCount = 1;
516 mFDs = (pollfd *)calloc(1, sizeof(mFDs[0]));
517 mDevices = (device_t **)calloc(1, sizeof(mDevices[0]));
518 mFDs[0].events = POLLIN;
Jeff Brown82763072010-08-17 16:48:25 -0700519 mFDs[0].revents = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 mDevices[0] = NULL;
521#ifdef HAVE_INOTIFY
522 mFDs[0].fd = inotify_init();
523 res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE);
524 if(res < 0) {
525 LOGE("could not add watch for %s, %s\n", device_path, strerror(errno));
526 }
527#else
528 /*
529 * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
530 * We allocate space for it and set it to something invalid.
531 */
532 mFDs[0].fd = -1;
533#endif
534
Jeff Brown1ad00e92010-10-01 18:55:43 -0700535 res = scanDir(device_path);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536 if(res < 0) {
537 LOGE("scan dir failed for %s\n", device_path);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538 }
539
540 return true;
541}
542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543// ----------------------------------------------------------------------------
544
Jeff Brown8575a872010-06-30 16:10:35 -0700545static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
546 const uint8_t* end = array + endIndex;
547 array += startIndex;
548 while (array != end) {
549 if (*(array++) != 0) {
550 return true;
551 }
552 }
553 return false;
554}
555
556static const int32_t GAMEPAD_KEYCODES[] = {
557 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
558 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
559 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
560 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
561 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
562 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE
563};
564
Jeff Brown1ad00e92010-10-01 18:55:43 -0700565int EventHub::openDevice(const char *deviceName) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 int version;
567 int fd;
568 struct pollfd *new_mFDs;
569 device_t **new_devices;
570 char **new_device_names;
571 char name[80];
572 char location[80];
573 char idstr[80];
574 struct input_id id;
575
576 LOGV("Opening device: %s", deviceName);
577
578 AutoMutex _l(mLock);
Nick Pellyc81bb202010-01-20 19:36:49 -0800579
Nick Pelly1b5cf322010-01-26 10:27:15 -0800580 fd = open(deviceName, O_RDWR);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 if(fd < 0) {
582 LOGE("could not open %s, %s\n", deviceName, strerror(errno));
583 return -1;
584 }
585
586 if(ioctl(fd, EVIOCGVERSION, &version)) {
587 LOGE("could not get driver version for %s, %s\n", deviceName, strerror(errno));
588 return -1;
589 }
590 if(ioctl(fd, EVIOCGID, &id)) {
591 LOGE("could not get driver id for %s, %s\n", deviceName, strerror(errno));
592 return -1;
593 }
594 name[sizeof(name) - 1] = '\0';
595 location[sizeof(location) - 1] = '\0';
596 idstr[sizeof(idstr) - 1] = '\0';
597 if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
598 //fprintf(stderr, "could not get device name for %s, %s\n", deviceName, strerror(errno));
599 name[0] = '\0';
600 }
Mike Lockwood24a7e042009-07-17 00:10:10 -0400601
602 // check to see if the device is on our excluded list
603 List<String8>::iterator iter = mExcludedDevices.begin();
604 List<String8>::iterator end = mExcludedDevices.end();
605 for ( ; iter != end; iter++) {
606 const char* test = *iter;
607 if (strcmp(name, test) == 0) {
608 LOGI("ignoring event id %s driver %s\n", deviceName, test);
609 close(fd);
Mike Lockwood24a7e042009-07-17 00:10:10 -0400610 return -1;
611 }
612 }
613
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 if(ioctl(fd, EVIOCGPHYS(sizeof(location) - 1), &location) < 1) {
615 //fprintf(stderr, "could not get location for %s, %s\n", deviceName, strerror(errno));
616 location[0] = '\0';
617 }
618 if(ioctl(fd, EVIOCGUNIQ(sizeof(idstr) - 1), &idstr) < 1) {
619 //fprintf(stderr, "could not get idstring for %s, %s\n", deviceName, strerror(errno));
620 idstr[0] = '\0';
621 }
622
Jeff Brown82763072010-08-17 16:48:25 -0700623 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
624 LOGE("Error %d making device file descriptor non-blocking.", errno);
625 close(fd);
626 return -1;
627 }
628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 int devid = 0;
630 while (devid < mNumDevicesById) {
631 if (mDevicesById[devid].device == NULL) {
632 break;
633 }
634 devid++;
635 }
636 if (devid >= mNumDevicesById) {
637 device_ent* new_devids = (device_ent*)realloc(mDevicesById,
638 sizeof(mDevicesById[0]) * (devid + 1));
639 if (new_devids == NULL) {
640 LOGE("out of memory");
641 return -1;
642 }
643 mDevicesById = new_devids;
644 mNumDevicesById = devid+1;
645 mDevicesById[devid].device = NULL;
646 mDevicesById[devid].seq = 0;
647 }
648
649 mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
650 if (mDevicesById[devid].seq == 0) {
651 mDevicesById[devid].seq = 1<<SEQ_SHIFT;
652 }
653
654 new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1));
655 new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1));
656 if (new_mFDs == NULL || new_devices == NULL) {
657 LOGE("out of memory");
658 return -1;
659 }
660 mFDs = new_mFDs;
661 mDevices = new_devices;
662
663#if 0
664 LOGI("add device %d: %s\n", mFDCount, deviceName);
665 LOGI(" bus: %04x\n"
666 " vendor %04x\n"
667 " product %04x\n"
668 " version %04x\n",
669 id.bustype, id.vendor, id.product, id.version);
670 LOGI(" name: \"%s\"\n", name);
671 LOGI(" location: \"%s\"\n"
672 " id: \"%s\"\n", location, idstr);
673 LOGI(" version: %d.%d.%d\n",
674 version >> 16, (version >> 8) & 0xff, version & 0xff);
675#endif
676
Iliyan Malchev34193b32009-08-06 14:50:08 -0700677 device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName, name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 if (device == NULL) {
679 LOGE("out of memory");
680 return -1;
681 }
682
Jens Gulin7dcaa582010-06-22 22:21:57 +0200683 device->fd = fd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 mFDs[mFDCount].fd = fd;
685 mFDs[mFDCount].events = POLLIN;
Jeff Brown82763072010-08-17 16:48:25 -0700686 mFDs[mFDCount].revents = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687
Jeff Brown8575a872010-06-30 16:10:35 -0700688 // Figure out the kinds of events the device reports.
Dianne Hackbornc5917362009-08-04 05:49:43 -0700689
Jeff Brown8575a872010-06-30 16:10:35 -0700690 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 memset(key_bitmask, 0, sizeof(key_bitmask));
Jeff Brown8575a872010-06-30 16:10:35 -0700692
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693 LOGV("Getting keys...");
694 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {
695 //LOGI("MAP\n");
Jeff Brown8575a872010-06-30 16:10:35 -0700696 //for (int i = 0; i < sizeof(key_bitmask); i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 // LOGI("%d: 0x%02x\n", i, key_bitmask[i]);
698 //}
Jeff Brown8575a872010-06-30 16:10:35 -0700699
700 // See if this is a keyboard. Ignore everything in the button range except for
701 // gamepads which are also considered keyboards.
702 if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
703 || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD),
704 sizeof_bit_array(BTN_DIGI))
705 || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
706 sizeof_bit_array(KEY_MAX + 1))) {
707 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
708
Dianne Hackbornc5917362009-08-04 05:49:43 -0700709 device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 if (device->keyBitmask != NULL) {
711 memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
712 } else {
713 delete device;
714 LOGE("out of memory allocating key bitmask");
715 return -1;
716 }
717 }
718 }
Dianne Hackbornc5917362009-08-04 05:49:43 -0700719
Jeff Brown8575a872010-06-30 16:10:35 -0700720 // See if this is a trackball (or mouse).
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 if (test_bit(BTN_MOUSE, key_bitmask)) {
Jeff Brown8575a872010-06-30 16:10:35 -0700722 uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 memset(rel_bitmask, 0, sizeof(rel_bitmask));
724 LOGV("Getting relative controllers...");
Jeff Brown8575a872010-06-30 16:10:35 -0700725 if (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) >= 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726 if (test_bit(REL_X, rel_bitmask) && test_bit(REL_Y, rel_bitmask)) {
Jeff Browne839a582010-04-22 18:58:52 -0700727 device->classes |= INPUT_DEVICE_CLASS_TRACKBALL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728 }
729 }
730 }
Jeff Brown8575a872010-06-30 16:10:35 -0700731
732 // See if this is a touch pad.
733 uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
Dianne Hackbornc5917362009-08-04 05:49:43 -0700734 memset(abs_bitmask, 0, sizeof(abs_bitmask));
735 LOGV("Getting absolute controllers...");
Jeff Brown8575a872010-06-30 16:10:35 -0700736 if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) >= 0) {
737 // Is this a new modern multi-touch driver?
Jeff Brown38a7fab2010-08-30 03:02:23 -0700738 if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
Jeff Brown8575a872010-06-30 16:10:35 -0700739 && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
740 device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN | INPUT_DEVICE_CLASS_TOUCHSCREEN_MT;
741
742 // Is this an old style single-touch driver?
743 } else if (test_bit(BTN_TOUCH, key_bitmask)
744 && test_bit(ABS_X, abs_bitmask) && test_bit(ABS_Y, abs_bitmask)) {
745 device->classes |= INPUT_DEVICE_CLASS_TOUCHSCREEN;
746 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747 }
748
749#ifdef EV_SW
750 // figure out the switches this device reports
Jeff Brown8575a872010-06-30 16:10:35 -0700751 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jeff Browne57e8952010-07-23 21:28:06 -0700753 bool hasSwitches = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask) >= 0) {
755 for (int i=0; i<EV_SW; i++) {
756 //LOGI("Device 0x%x sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
757 if (test_bit(i, sw_bitmask)) {
Jeff Browne57e8952010-07-23 21:28:06 -0700758 hasSwitches = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 if (mSwitches[i] == 0) {
760 mSwitches[i] = device->id;
761 }
762 }
763 }
764 }
Jeff Browne57e8952010-07-23 21:28:06 -0700765 if (hasSwitches) {
766 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
Michaël Burtin5aa3d792011-07-29 14:25:47 +0200767 device->switchBitmask = new uint8_t[sizeof(sw_bitmask)];
768 if (device->switchBitmask != NULL) {
769 memcpy(device->switchBitmask, sw_bitmask, sizeof(sw_bitmask));
770 } else {
771 delete device;
772 LOGE("out of memory allocating switch bitmask");
773 return -1;
774 }
Jeff Browne57e8952010-07-23 21:28:06 -0700775 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776#endif
777
Jeff Browne839a582010-04-22 18:58:52 -0700778 if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) {
Iliyan Malchev34193b32009-08-06 14:50:08 -0700779 char tmpfn[sizeof(name)];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 char keylayoutFilename[300];
781
782 // a more descriptive name
Iliyan Malchev34193b32009-08-06 14:50:08 -0700783 device->name = name;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784
785 // replace all the spaces with underscores
Iliyan Malchev34193b32009-08-06 14:50:08 -0700786 strcpy(tmpfn, name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 for (char *p = strchr(tmpfn, ' '); p && *p; p = strchr(tmpfn, ' '))
788 *p = '_';
789
790 // find the .kl file we need for this device
791 const char* root = getenv("ANDROID_ROOT");
792 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
793 "%s/usr/keylayout/%s.kl", root, tmpfn);
794 bool defaultKeymap = false;
795 if (access(keylayoutFilename, R_OK)) {
796 snprintf(keylayoutFilename, sizeof(keylayoutFilename),
797 "%s/usr/keylayout/%s", root, "qwerty.kl");
798 defaultKeymap = true;
799 }
Jeff Brown8575a872010-06-30 16:10:35 -0700800 status_t status = device->layoutMap->load(keylayoutFilename);
801 if (status) {
802 LOGE("Error %d loading key layout.", status);
803 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804
805 // tell the world about the devname (the descriptive name)
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800806 if (!mHaveFirstKeyboard && !defaultKeymap && strstr(name, "-keypad")) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807 // the built-in keyboard has a well-known device ID of 0,
808 // this device better not go away.
809 mHaveFirstKeyboard = true;
810 mFirstKeyboardId = device->id;
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800811 property_set("hw.keyboards.0.devname", name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800812 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813 // ensure mFirstKeyboardId is set to -something-.
814 if (mFirstKeyboardId == 0) {
815 mFirstKeyboardId = device->id;
816 }
817 }
818 char propName[100];
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800819 sprintf(propName, "hw.keyboards.%u.devname", device->id);
Iliyan Malchev34193b32009-08-06 14:50:08 -0700820 property_set(propName, name);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821
Dianne Hackbornc5917362009-08-04 05:49:43 -0700822 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brown2806e382010-10-01 17:46:21 -0700823 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Browne839a582010-04-22 18:58:52 -0700824 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackbornc5917362009-08-04 05:49:43 -0700825 }
826
Jeff Brown8575a872010-06-30 16:10:35 -0700827 // See if this device has a DPAD.
Jeff Brown2806e382010-10-01 17:46:21 -0700828 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
829 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
830 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
831 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
832 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Browne839a582010-04-22 18:58:52 -0700833 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackbornc5917362009-08-04 05:49:43 -0700834 }
835
Jeff Brown8575a872010-06-30 16:10:35 -0700836 // See if this device has a gamepad.
Kenny Rootbc9c82f2010-10-21 15:46:03 -0700837 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brown2806e382010-10-01 17:46:21 -0700838 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brown8575a872010-06-30 16:10:35 -0700839 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
840 break;
841 }
842 }
843
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800844 LOGI("New keyboard: device->id=0x%x devname='%s' propName='%s' keylayout='%s'\n",
845 device->id, name, propName, keylayoutFilename);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 }
847
Sean McNeil2e7a5302010-06-23 16:00:37 +0700848 // If the device isn't recognized as something we handle, don't monitor it.
849 if (device->classes == 0) {
850 LOGV("Dropping device %s %p, id = %d\n", deviceName, device, devid);
851 close(fd);
852 delete device;
853 return -1;
854 }
855
Dianne Hackbornc5917362009-08-04 05:49:43 -0700856 LOGI("New device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
857 deviceName, name, device->id, mNumDevicesById, mFDCount, fd, device->classes);
858
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 LOGV("Adding device %s %p at %d, id = %d, classes = 0x%x\n",
860 deviceName, device, mFDCount, devid, device->classes);
861
862 mDevicesById[devid].device = device;
863 device->next = mOpeningDevices;
864 mOpeningDevices = device;
865 mDevices[mFDCount] = device;
866
867 mFDCount++;
868 return 0;
869}
870
Jeff Brown2806e382010-10-01 17:46:21 -0700871bool EventHub::hasKeycodeLocked(device_t* device, int keycode) const
Dianne Hackbornc5917362009-08-04 05:49:43 -0700872{
873 if (device->keyBitmask == NULL || device->layoutMap == NULL) {
874 return false;
875 }
876
877 Vector<int32_t> scanCodes;
878 device->layoutMap->findScancodes(keycode, &scanCodes);
879 const size_t N = scanCodes.size();
880 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
881 int32_t sc = scanCodes.itemAt(i);
882 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
883 return true;
884 }
885 }
886
887 return false;
888}
889
Jeff Brown1ad00e92010-10-01 18:55:43 -0700890int EventHub::closeDevice(const char *deviceName) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 AutoMutex _l(mLock);
Jeff Brown1ad00e92010-10-01 18:55:43 -0700892
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893 int i;
894 for(i = 1; i < mFDCount; i++) {
895 if(strcmp(mDevices[i]->path.string(), deviceName) == 0) {
896 //LOGD("remove device %d: %s\n", i, deviceName);
897 device_t* device = mDevices[i];
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700898
899 LOGI("Removed device: path=%s name=%s id=0x%x (of 0x%x) index=%d fd=%d classes=0x%x\n",
900 device->path.string(), device->name.string(), device->id,
901 mNumDevicesById, mFDCount, mFDs[i].fd, device->classes);
902
903 // Clear this device's entry.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 int index = (device->id&ID_MASK);
905 mDevicesById[index].device = NULL;
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700906
907 // Close the file descriptor and compact the fd array.
Mike Lockwood07549f92009-08-28 13:29:06 -0700908 close(mFDs[i].fd);
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700909 int count = mFDCount - i - 1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 memmove(mDevices + i, mDevices + i + 1, sizeof(mDevices[0]) * count);
911 memmove(mFDs + i, mFDs + i + 1, sizeof(mFDs[0]) * count);
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700912 mFDCount--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913
914#ifdef EV_SW
915 for (int j=0; j<EV_SW; j++) {
916 if (mSwitches[j] == device->id) {
917 mSwitches[j] = 0;
918 }
919 }
920#endif
921
922 device->next = mClosingDevices;
923 mClosingDevices = device;
924
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 if (device->id == mFirstKeyboardId) {
926 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
927 device->path.string(), mFirstKeyboardId);
928 mFirstKeyboardId = 0;
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800929 property_set("hw.keyboards.0.devname", NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930 }
931 // clear the property
932 char propName[100];
Dianne Hackborn71c3eb22010-03-02 17:19:29 -0800933 sprintf(propName, "hw.keyboards.%u.devname", device->id);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 property_set(propName, NULL);
935 return 0;
936 }
937 }
Dianne Hackborn39bf9182009-09-01 19:01:50 -0700938 LOGE("remove device: %s not found\n", deviceName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 return -1;
940}
941
Jeff Brown1ad00e92010-10-01 18:55:43 -0700942int EventHub::readNotify(int nfd) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943#ifdef HAVE_INOTIFY
944 int res;
945 char devname[PATH_MAX];
946 char *filename;
947 char event_buf[512];
948 int event_size;
949 int event_pos = 0;
950 struct inotify_event *event;
951
Jeff Brown1ad00e92010-10-01 18:55:43 -0700952 LOGV("EventHub::readNotify nfd: %d\n", nfd);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953 res = read(nfd, event_buf, sizeof(event_buf));
954 if(res < (int)sizeof(*event)) {
955 if(errno == EINTR)
956 return 0;
957 LOGW("could not get event, %s\n", strerror(errno));
958 return 1;
959 }
960 //printf("got %d bytes of event information\n", res);
961
962 strcpy(devname, device_path);
963 filename = devname + strlen(devname);
964 *filename++ = '/';
965
966 while(res >= (int)sizeof(*event)) {
967 event = (struct inotify_event *)(event_buf + event_pos);
968 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
969 if(event->len) {
970 strcpy(filename, event->name);
971 if(event->mask & IN_CREATE) {
Jeff Brown1ad00e92010-10-01 18:55:43 -0700972 openDevice(devname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 }
974 else {
Jeff Brown1ad00e92010-10-01 18:55:43 -0700975 closeDevice(devname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 }
977 }
978 event_size = sizeof(*event) + event->len;
979 res -= event_size;
980 event_pos += event_size;
981 }
982#endif
983 return 0;
984}
985
986
Jeff Brown1ad00e92010-10-01 18:55:43 -0700987int EventHub::scanDir(const char *dirname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988{
989 char devname[PATH_MAX];
990 char *filename;
991 DIR *dir;
992 struct dirent *de;
993 dir = opendir(dirname);
994 if(dir == NULL)
995 return -1;
996 strcpy(devname, dirname);
997 filename = devname + strlen(devname);
998 *filename++ = '/';
999 while((de = readdir(dir))) {
1000 if(de->d_name[0] == '.' &&
1001 (de->d_name[1] == '\0' ||
1002 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1003 continue;
1004 strcpy(filename, de->d_name);
Jeff Brown1ad00e92010-10-01 18:55:43 -07001005 openDevice(devname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 }
1007 closedir(dir);
1008 return 0;
1009}
1010
Jeff Brown2806e382010-10-01 17:46:21 -07001011void EventHub::dump(String8& dump) {
1012 dump.append("Event Hub State:\n");
1013
1014 { // acquire lock
1015 AutoMutex _l(mLock);
1016
1017 dump.appendFormat(INDENT "HaveFirstKeyboard: %s\n", toString(mHaveFirstKeyboard));
1018 dump.appendFormat(INDENT "FirstKeyboardId: 0x%x\n", mFirstKeyboardId);
1019
1020 dump.append(INDENT "Devices:\n");
1021
1022 for (int i = 0; i < mNumDevicesById; i++) {
1023 const device_t* device = mDevicesById[i].device;
1024 if (device) {
1025 if (mFirstKeyboardId == device->id) {
1026 dump.appendFormat(INDENT2 "0x%x: %s (aka device 0 - first keyboard)\n",
1027 device->id, device->name.string());
1028 } else {
1029 dump.appendFormat(INDENT2 "0x%x: %s\n", device->id, device->name.string());
1030 }
1031 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1032 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
1033 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n", device->keylayoutFilename.string());
1034 }
1035 }
1036 } // release lock
1037}
1038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039}; // namespace android