blob: 960e4143e659b613ca47142e341b71661d476731 [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#define LOG_TAG "EventHub"
18
Jeff Brown93fa9b32011-06-14 17:09:25 -070019// #define LOG_NDEBUG 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Jeff Brownb4ff35d2011-01-02 16:37:43 -080021#include "EventHub.h"
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <hardware_legacy/power.h>
24
25#include <cutils/properties.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <utils/Log.h>
27#include <utils/Timers.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070028#include <utils/threads.h>
Mathias Agopian3b4062e2009-05-31 19:13:00 -070029#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31#include <stdlib.h>
32#include <stdio.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <memory.h>
36#include <errno.h>
37#include <assert.h>
38
Jeff Brown6b53e8d2010-11-10 16:03:06 -080039#include <ui/KeyLayoutMap.h>
Jeff Brown90655042010-12-02 13:50:46 -080040#include <ui/KeyCharacterMap.h>
41#include <ui/VirtualKeyMap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43#include <string.h>
44#include <stdint.h>
45#include <dirent.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070046
47#include <sys/inotify.h>
48#include <sys/epoll.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include <sys/ioctl.h>
Jeff Brown93fa9b32011-06-14 17:09:25 -070050#include <sys/limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52/* this macro is used to tell if "bit" is set in "array"
53 * it selects a byte from the array, and does a boolean AND
54 * operation with a byte that only has the relevant bit set.
55 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
56 */
57#define test_bit(bit, array) (array[bit/8] & (1<<(bit%8)))
58
Jeff Brownfd0358292010-06-30 16:10:35 -070059/* this macro computes the number of bytes needed to represent a bit array of the specified size */
60#define sizeof_bit_array(bits) ((bits + 7) / 8)
61
Jeff Brownf2f487182010-10-01 17:46:21 -070062#define INDENT " "
63#define INDENT2 " "
64#define INDENT3 " "
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066namespace android {
67
68static const char *WAKE_LOCK_ID = "KeyEvents";
Jeff Brown90655042010-12-02 13:50:46 -080069static const char *DEVICE_PATH = "/dev/input";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71/* return the larger integer */
72static inline int max(int v1, int v2)
73{
74 return (v1 > v2) ? v1 : v2;
75}
76
Jeff Brownf2f487182010-10-01 17:46:21 -070077static inline const char* toString(bool value) {
78 return value ? "true" : "false";
79}
80
Jeff Brown90655042010-12-02 13:50:46 -080081// --- EventHub::Device ---
82
83EventHub::Device::Device(int fd, int32_t id, const String8& path,
84 const InputDeviceIdentifier& identifier) :
85 next(NULL),
86 fd(fd), id(id), path(path), identifier(identifier),
Jeff Brown93fa9b32011-06-14 17:09:25 -070087 classes(0), configuration(NULL), virtualKeyMap(NULL) {
88 memset(keyBitmask, 0, sizeof(keyBitmask));
89 memset(absBitmask, 0, sizeof(absBitmask));
90 memset(relBitmask, 0, sizeof(relBitmask));
91 memset(swBitmask, 0, sizeof(swBitmask));
92 memset(ledBitmask, 0, sizeof(ledBitmask));
93 memset(propBitmask, 0, sizeof(propBitmask));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094}
95
Jeff Brown90655042010-12-02 13:50:46 -080096EventHub::Device::~Device() {
97 close();
Jeff Brown47e6b1b2010-11-29 17:37:49 -080098 delete configuration;
Jeff Brown90655042010-12-02 13:50:46 -080099 delete virtualKeyMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100}
101
Jeff Brown90655042010-12-02 13:50:46 -0800102void EventHub::Device::close() {
103 if (fd >= 0) {
104 ::close(fd);
105 fd = -1;
106 }
107}
108
109
110// --- EventHub ---
111
Jeff Brown93fa9b32011-06-14 17:09:25 -0700112const uint32_t EventHub::EPOLL_ID_INOTIFY;
113const uint32_t EventHub::EPOLL_ID_WAKE;
114const int EventHub::EPOLL_SIZE_HINT;
115const int EventHub::EPOLL_MAX_EVENTS;
116
Jeff Brown90655042010-12-02 13:50:46 -0800117EventHub::EventHub(void) :
Jeff Brown93fa9b32011-06-14 17:09:25 -0700118 mBuiltInKeyboardId(-1), mNextDeviceId(1),
Jeff Brown90655042010-12-02 13:50:46 -0800119 mOpeningDevices(0), mClosingDevices(0),
Jeff Brown93fa9b32011-06-14 17:09:25 -0700120 mNeedToSendFinishedDeviceScan(false),
121 mNeedToReopenDevices(false), mNeedToScanDevices(true),
122 mPendingEventCount(0), mPendingEventIndex(0), mPendingINotify(false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brownb7198742011-03-18 18:14:26 -0700124
Jeff Brownb7198742011-03-18 18:14:26 -0700125 mNumCpus = sysconf(_SC_NPROCESSORS_ONLN);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700126
127 mEpollFd = epoll_create(EPOLL_SIZE_HINT);
128 LOG_ALWAYS_FATAL_IF(mEpollFd < 0, "Could not create epoll instance. errno=%d", errno);
129
130 mINotifyFd = inotify_init();
131 int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
132 LOG_ALWAYS_FATAL_IF(result < 0, "Could not register INotify for %s. errno=%d",
133 DEVICE_PATH, errno);
134
135 struct epoll_event eventItem;
136 memset(&eventItem, 0, sizeof(eventItem));
137 eventItem.events = EPOLLIN;
138 eventItem.data.u32 = EPOLL_ID_INOTIFY;
139 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
140 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add INotify to epoll instance. errno=%d", errno);
141
142 int wakeFds[2];
143 result = pipe(wakeFds);
144 LOG_ALWAYS_FATAL_IF(result != 0, "Could not create wake pipe. errno=%d", errno);
145
146 mWakeReadPipeFd = wakeFds[0];
147 mWakeWritePipeFd = wakeFds[1];
148
149 result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);
150 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake read pipe non-blocking. errno=%d",
151 errno);
152
153 result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);
154 LOG_ALWAYS_FATAL_IF(result != 0, "Could not make wake write pipe non-blocking. errno=%d",
155 errno);
156
157 eventItem.data.u32 = EPOLL_ID_WAKE;
158 result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
159 LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance. errno=%d",
160 errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161}
162
Jeff Brown90655042010-12-02 13:50:46 -0800163EventHub::~EventHub(void) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700164 closeAllDevicesLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Jeff Brown93fa9b32011-06-14 17:09:25 -0700166 while (mClosingDevices) {
167 Device* device = mClosingDevices;
168 mClosingDevices = device->next;
169 delete device;
170 }
171
172 ::close(mEpollFd);
173 ::close(mINotifyFd);
174 ::close(mWakeReadPipeFd);
175 ::close(mWakeWritePipeFd);
176
177 release_wake_lock(WAKE_LOCK_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178}
179
Jeff Brown90655042010-12-02 13:50:46 -0800180String8 EventHub::getDeviceName(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800182 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 if (device == NULL) return String8();
Jeff Brown90655042010-12-02 13:50:46 -0800184 return device->identifier.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185}
186
Jeff Brown90655042010-12-02 13:50:46 -0800187uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800189 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 if (device == NULL) return 0;
191 return device->classes;
192}
193
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800194void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800195 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800196 Device* device = getDeviceLocked(deviceId);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800197 if (device && device->configuration) {
198 *outConfiguration = *device->configuration;
Jeff Brown1f245102010-11-18 20:53:46 -0800199 } else {
200 outConfiguration->clear();
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800201 }
202}
203
Jeff Brown6d0fec22010-07-23 21:28:06 -0700204status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
205 RawAbsoluteAxisInfo* outAxisInfo) const {
Jeff Brown8d608662010-08-30 03:02:23 -0700206 outAxisInfo->clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700207
Jeff Brownba421dd2011-08-10 15:07:05 -0700208 if (axis >= 0 && axis <= ABS_MAX) {
209 AutoMutex _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
Jeff Brownba421dd2011-08-10 15:07:05 -0700211 Device* device = getDeviceLocked(deviceId);
212 if (device && test_bit(axis, device->absBitmask)) {
213 struct input_absinfo info;
214 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
215 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
216 axis, device->identifier.name.string(), device->fd, errno);
217 return -errno;
218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Jeff Brownba421dd2011-08-10 15:07:05 -0700220 if (info.minimum != info.maximum) {
221 outAxisInfo->valid = true;
222 outAxisInfo->minValue = info.minimum;
223 outAxisInfo->maxValue = info.maximum;
224 outAxisInfo->flat = info.flat;
225 outAxisInfo->fuzz = info.fuzz;
226 outAxisInfo->resolution = info.resolution;
227 }
228 return OK;
229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700231 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232}
233
Jeff Browncc0c1592011-02-19 05:07:28 -0800234bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
235 if (axis >= 0 && axis <= REL_MAX) {
236 AutoMutex _l(mLock);
237
238 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700239 if (device) {
Jeff Browncc0c1592011-02-19 05:07:28 -0800240 return test_bit(axis, device->relBitmask);
241 }
242 }
243 return false;
244}
245
Jeff Brown80fd47c2011-05-24 01:07:44 -0700246bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
247 if (property >= 0 && property <= INPUT_PROP_MAX) {
248 AutoMutex _l(mLock);
249
250 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700251 if (device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700252 return test_bit(property, device->propBitmask);
253 }
254 }
255 return false;
256}
257
Jeff Brown6d0fec22010-07-23 21:28:06 -0700258int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700259 if (scanCode >= 0 && scanCode <= KEY_MAX) {
260 AutoMutex _l(mLock);
261
Jeff Brown90655042010-12-02 13:50:46 -0800262 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700263 if (device && test_bit(scanCode, device->keyBitmask)) {
264 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
265 memset(keyState, 0, sizeof(keyState));
266 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
267 return test_bit(scanCode, keyState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
270 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700271 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272}
273
Jeff Brown6d0fec22010-07-23 21:28:06 -0700274int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
275 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700276
Jeff Brown90655042010-12-02 13:50:46 -0800277 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700278 if (device && device->keyMap.haveKeyLayout()) {
279 Vector<int32_t> scanCodes;
280 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
281 if (scanCodes.size() != 0) {
282 uint8_t keyState[sizeof_bit_array(KEY_MAX + 1)];
283 memset(keyState, 0, sizeof(keyState));
284 if (ioctl(device->fd, EVIOCGKEY(sizeof(keyState)), keyState) >= 0) {
285 for (size_t i = 0; i < scanCodes.size(); i++) {
286 int32_t sc = scanCodes.itemAt(i);
287 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, keyState)) {
288 return AKEY_STATE_DOWN;
289 }
290 }
291 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293 }
294 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700295 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296}
297
Jeff Brown6d0fec22010-07-23 21:28:06 -0700298int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700299 if (sw >= 0 && sw <= SW_MAX) {
300 AutoMutex _l(mLock);
301
Jeff Brown90655042010-12-02 13:50:46 -0800302 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700303 if (device && test_bit(sw, device->swBitmask)) {
304 uint8_t swState[sizeof_bit_array(SW_MAX + 1)];
305 memset(swState, 0, sizeof(swState));
306 if (ioctl(device->fd, EVIOCGSW(sizeof(swState)), swState) >= 0) {
307 return test_bit(sw, swState) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
308 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700309 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700310 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700311 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700312}
313
Jeff Brown2717eff2011-06-30 23:53:07 -0700314status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
315 if (axis >= 0 && axis <= ABS_MAX) {
316 AutoMutex _l(mLock);
317
318 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700319 if (device && test_bit(axis, device->absBitmask)) {
320 struct input_absinfo info;
321 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
322 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
323 axis, device->identifier.name.string(), device->fd, errno);
324 return -errno;
325 }
326
327 *outValue = info.value;
328 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700329 }
330 }
331 *outValue = 0;
332 return -1;
333}
334
Jeff Brown6d0fec22010-07-23 21:28:06 -0700335bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
336 const int32_t* keyCodes, uint8_t* outFlags) const {
337 AutoMutex _l(mLock);
338
Jeff Brown90655042010-12-02 13:50:46 -0800339 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700340 if (device && device->keyMap.haveKeyLayout()) {
341 Vector<int32_t> scanCodes;
342 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
343 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344
Jeff Brownba421dd2011-08-10 15:07:05 -0700345 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
346 keyCodes[codeIndex], &scanCodes);
347 if (! err) {
348 // check the possible scan codes identified by the layout map against the
349 // map of codes actually emitted by the driver
350 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
351 if (test_bit(scanCodes[sc], device->keyBitmask)) {
352 outFlags[codeIndex] = 1;
353 break;
354 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700355 }
356 }
357 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700358 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700359 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700360 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700361}
362
Jeff Brown6f2fba42011-02-19 01:08:02 -0800363status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700364 int32_t* outKeycode, uint32_t* outFlags) const
365{
366 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800367 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700368
Jeff Brown90655042010-12-02 13:50:46 -0800369 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800370 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700371 if (err == NO_ERROR) {
372 return NO_ERROR;
373 }
374 }
375
Jeff Brown90655042010-12-02 13:50:46 -0800376 if (mBuiltInKeyboardId != -1) {
377 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700378
Jeff Brown90655042010-12-02 13:50:46 -0800379 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800380 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700381 if (err == NO_ERROR) {
382 return NO_ERROR;
383 }
384 }
385 }
386
387 *outKeycode = 0;
388 *outFlags = 0;
389 return NAME_NOT_FOUND;
390}
391
Jeff Brown85297452011-03-04 13:07:49 -0800392status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800393{
394 AutoMutex _l(mLock);
395 Device* device = getDeviceLocked(deviceId);
396
397 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800398 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800399 if (err == NO_ERROR) {
400 return NO_ERROR;
401 }
402 }
403
404 if (mBuiltInKeyboardId != -1) {
405 device = getDeviceLocked(mBuiltInKeyboardId);
406
407 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800408 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800409 if (err == NO_ERROR) {
410 return NO_ERROR;
411 }
412 }
413 }
414
Jeff Brown6f2fba42011-02-19 01:08:02 -0800415 return NAME_NOT_FOUND;
416}
417
Jeff Brown1a84fd12011-06-02 01:26:32 -0700418void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700419 AutoMutex _l(mLock);
420
Jeff Brown1a84fd12011-06-02 01:26:32 -0700421 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400422}
423
Jeff Brown49754db2011-07-01 17:37:58 -0700424bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
425 AutoMutex _l(mLock);
426 Device* device = getDeviceLocked(deviceId);
427 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
428 if (test_bit(scanCode, device->keyBitmask)) {
429 return true;
430 }
431 }
432 return false;
433}
434
Jeff Brown497a92c2010-09-12 17:55:08 -0700435bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
436 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800437 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700438 if (device && led >= 0 && led <= LED_MAX) {
439 if (test_bit(led, device->ledBitmask)) {
440 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700441 }
442 }
443 return false;
444}
445
446void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
447 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800448 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700449 if (device && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700450 struct input_event ev;
451 ev.time.tv_sec = 0;
452 ev.time.tv_usec = 0;
453 ev.type = EV_LED;
454 ev.code = led;
455 ev.value = on ? 1 : 0;
456
457 ssize_t nWrite;
458 do {
459 nWrite = write(device->fd, &ev, sizeof(struct input_event));
460 } while (nWrite == -1 && errno == EINTR);
461 }
462}
463
Jeff Brown90655042010-12-02 13:50:46 -0800464void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
465 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
466 outVirtualKeys.clear();
467
468 AutoMutex _l(mLock);
469 Device* device = getDeviceLocked(deviceId);
470 if (device && device->virtualKeyMap) {
471 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
472 }
473}
474
475EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
476 if (deviceId == 0) {
477 deviceId = mBuiltInKeyboardId;
478 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700479 ssize_t index = mDevices.indexOfKey(deviceId);
480 return index >= 0 ? mDevices.valueAt(index) : NULL;
481}
Jeff Brown90655042010-12-02 13:50:46 -0800482
Jeff Brown93fa9b32011-06-14 17:09:25 -0700483EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
484 for (size_t i = 0; i < mDevices.size(); i++) {
485 Device* device = mDevices.valueAt(i);
486 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800487 return device;
488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 }
490 return NULL;
491}
492
Jeff Brownb7198742011-03-18 18:14:26 -0700493size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Jeff Brownb6110c22011-04-01 16:15:13 -0700494 LOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400495
Jeff Brown93fa9b32011-06-14 17:09:25 -0700496 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400497
Jeff Brownb7198742011-03-18 18:14:26 -0700498 struct input_event readBuffer[bufferSize];
499
500 RawEvent* event = buffer;
501 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700502 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700503 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700504 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
505
Jeff Brown1a84fd12011-06-02 01:26:32 -0700506 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700507 if (mNeedToReopenDevices) {
508 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700509
510 LOGI("Reopening all input devices due to a configuration change.");
511
Jeff Brown93fa9b32011-06-14 17:09:25 -0700512 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700513 mNeedToScanDevices = true;
514 break; // return to the caller before we actually rescan
515 }
516
Jeff Browncc2e7172010-08-17 16:48:25 -0700517 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700518 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800519 Device* device = mClosingDevices;
520 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 device->id, device->path.string());
522 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700523 event->when = now;
524 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
525 event->type = DEVICE_REMOVED;
526 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700528 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700529 if (--capacity == 0) {
530 break;
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700533
Jeff Brown1a84fd12011-06-02 01:26:32 -0700534 if (mNeedToScanDevices) {
535 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700536 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700537 mNeedToSendFinishedDeviceScan = true;
538 }
539
Jeff Brownb7198742011-03-18 18:14:26 -0700540 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800541 Device* device = mOpeningDevices;
542 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 device->id, device->path.string());
544 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700545 event->when = now;
546 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
547 event->type = DEVICE_ADDED;
548 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700549 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700550 if (--capacity == 0) {
551 break;
552 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700553 }
554
555 if (mNeedToSendFinishedDeviceScan) {
556 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700557 event->when = now;
558 event->type = FINISHED_DEVICE_SCAN;
559 event += 1;
560 if (--capacity == 0) {
561 break;
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564
Jeff Browncc2e7172010-08-17 16:48:25 -0700565 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700566 bool deviceChanged = false;
567 while (mPendingEventIndex < mPendingEventCount) {
568 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
569 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
570 if (eventItem.events & EPOLLIN) {
571 mPendingINotify = true;
572 } else {
573 LOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
574 }
575 continue;
576 }
577
578 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
579 if (eventItem.events & EPOLLIN) {
580 LOGV("awoken after wake()");
581 awoken = true;
582 char buffer[16];
583 ssize_t nRead;
584 do {
585 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
586 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
587 } else {
588 LOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
589 eventItem.events);
590 }
591 continue;
592 }
593
594 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
595 if (deviceIndex < 0) {
596 LOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
597 eventItem.events, eventItem.data.u32);
598 continue;
599 }
600
601 Device* device = mDevices.valueAt(deviceIndex);
602 if (eventItem.events & EPOLLIN) {
603 int32_t readSize = read(device->fd, readBuffer,
604 sizeof(struct input_event) * capacity);
605 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
606 // Device was removed before INotify noticed.
607 deviceChanged = true;
608 closeDeviceLocked(device);
609 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700610 if (errno != EAGAIN && errno != EINTR) {
611 LOGW("could not get event (errno=%d)", errno);
612 }
613 } else if ((readSize % sizeof(struct input_event)) != 0) {
614 LOGE("could not get event (wrong size: %d)", readSize);
615 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700616 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
617
618 size_t count = size_t(readSize) / sizeof(struct input_event);
619 for (size_t i = 0; i < count; i++) {
620 const struct input_event& iev = readBuffer[i];
621 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
622 device->path.string(),
623 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
624 iev.type, iev.code, iev.value);
625
Jeff Brown4e91a182011-04-07 11:38:09 -0700626#ifdef HAVE_POSIX_CLOCKS
627 // Use the time specified in the event instead of the current time
628 // so that downstream code can get more accurate estimates of
629 // event dispatch latency from the time the event is enqueued onto
630 // the evdev client buffer.
631 //
632 // The event's timestamp fortuitously uses the same monotonic clock
633 // time base as the rest of Android. The kernel event device driver
634 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
635 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
636 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
637 // system call that also queries ktime_get_ts().
638 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
639 + nsecs_t(iev.time.tv_usec) * 1000LL;
640 LOGV("event time %lld, now %lld", event->when, now);
641#else
Jeff Brownb7198742011-03-18 18:14:26 -0700642 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700643#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700644 event->deviceId = deviceId;
645 event->type = iev.type;
646 event->scanCode = iev.code;
647 event->value = iev.value;
648 event->keyCode = AKEYCODE_UNKNOWN;
649 event->flags = 0;
650 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
651 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
652 &event->keyCode, &event->flags);
653 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
654 iev.code, event->keyCode, event->flags, err);
655 }
656 event += 1;
657 }
658 capacity -= count;
659 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700660 // The result buffer is full. Reset the pending event index
661 // so we will try to read the device again on the next iteration.
662 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700663 break;
664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700666 } else {
667 LOGW("Received unexpected epoll event 0x%08x for device %s.",
668 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700671
Jeff Brown93fa9b32011-06-14 17:09:25 -0700672 // readNotify() will modify the list of devices so this must be done after
673 // processing all other events to ensure that we read all remaining events
674 // before closing the devices.
675 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
676 mPendingINotify = false;
677 readNotifyLocked();
678 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800679 }
680
Jeff Brown93fa9b32011-06-14 17:09:25 -0700681 // Report added or removed devices immediately.
682 if (deviceChanged) {
683 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
Jeff Browna9b84222010-10-14 02:23:43 -0700685
Jeff Brown93fa9b32011-06-14 17:09:25 -0700686 // Return now if we have collected any events or if we were explicitly awoken.
687 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700688 break;
689 }
690
Jeff Browncc2e7172010-08-17 16:48:25 -0700691 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700692 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700693 // subtle choreography. When a device driver has pending (unread) events, it acquires
694 // a kernel wake lock. However, once the last pending event has been read, the device
695 // driver will release the kernel wake lock. To prevent the system from going to sleep
696 // when this happens, the EventHub holds onto its own user wake lock while the client
697 // is processing events. Thus the system can only sleep if there are no events
698 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700699 //
700 // The timeout is advisory only. If the device is asleep, it will not wake just to
701 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700702 mPendingEventIndex = 0;
703
704 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700705 release_wake_lock(WAKE_LOCK_ID);
706
Jeff Brown93fa9b32011-06-14 17:09:25 -0700707 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700708
709 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700710 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700711
Jeff Brownaa3855d2011-03-17 01:34:19 -0700712 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700713 // Timed out.
714 mPendingEventCount = 0;
715 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700716 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700717
Jeff Brownaa3855d2011-03-17 01:34:19 -0700718 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700719 // An error occurred.
720 mPendingEventCount = 0;
721
Jeff Brownb7198742011-03-18 18:14:26 -0700722 // Sleep after errors to avoid locking up the system.
723 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700724 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700725 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700726 usleep(100000);
727 }
Jeff Brownb7198742011-03-18 18:14:26 -0700728 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700729 // Some events occurred.
730 mPendingEventCount = size_t(pollResult);
731
Jeff Brownb7198742011-03-18 18:14:26 -0700732 // On an SMP system, it is possible for the framework to read input events
733 // faster than the kernel input device driver can produce a complete packet.
734 // Because poll() wakes up as soon as the first input event becomes available,
735 // the framework will often end up reading one event at a time until the
736 // packet is complete. Instead of one call to read() returning 71 events,
737 // it could take 71 calls to read() each returning 1 event.
738 //
739 // Sleep for a short period of time after waking up from the poll() to give
740 // the kernel time to finish writing the entire packet of input events.
741 if (mNumCpus > 1) {
742 usleep(250);
743 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
Jeff Brownb7198742011-03-18 18:14:26 -0700746
747 // All done, return the number of events we read.
748 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749}
750
Jeff Brown93fa9b32011-06-14 17:09:25 -0700751void EventHub::wake() {
752 LOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753
Jeff Brown93fa9b32011-06-14 17:09:25 -0700754 ssize_t nWrite;
755 do {
756 nWrite = write(mWakeWritePipeFd, "W", 1);
757 } while (nWrite == -1 && errno == EINTR);
758
759 if (nWrite != 1 && errno != EAGAIN) {
760 LOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700762}
Jeff Brown90655042010-12-02 13:50:46 -0800763
Jeff Brown93fa9b32011-06-14 17:09:25 -0700764void EventHub::scanDevicesLocked() {
765 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800767 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769}
770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771// ----------------------------------------------------------------------------
772
Jeff Brownfd0358292010-06-30 16:10:35 -0700773static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
774 const uint8_t* end = array + endIndex;
775 array += startIndex;
776 while (array != end) {
777 if (*(array++) != 0) {
778 return true;
779 }
780 }
781 return false;
782}
783
784static const int32_t GAMEPAD_KEYCODES[] = {
785 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
786 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
787 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
788 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
789 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800790 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
791 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
792 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
793 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
794 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -0700795};
796
Jeff Brown93fa9b32011-06-14 17:09:25 -0700797status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800798 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799
Jeff Brown90655042010-12-02 13:50:46 -0800800 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801
Jeff Brown90655042010-12-02 13:50:46 -0800802 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800804 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 return -1;
806 }
807
Jeff Brown90655042010-12-02 13:50:46 -0800808 InputDeviceIdentifier identifier;
809
810 // Get device name.
811 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
812 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
813 } else {
814 buffer[sizeof(buffer) - 1] = '\0';
815 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400817
Jeff Brown90655042010-12-02 13:50:46 -0800818 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700819 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
820 const String8& item = mExcludedDevices.itemAt(i);
821 if (identifier.name == item) {
822 LOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400823 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400824 return -1;
825 }
826 }
827
Jeff Brown90655042010-12-02 13:50:46 -0800828 // Get device driver version.
829 int driverVersion;
830 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
831 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
832 close(fd);
833 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 }
835
Jeff Brown90655042010-12-02 13:50:46 -0800836 // Get device identifier.
837 struct input_id inputId;
838 if(ioctl(fd, EVIOCGID, &inputId)) {
839 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
840 close(fd);
841 return -1;
842 }
843 identifier.bus = inputId.bustype;
844 identifier.product = inputId.product;
845 identifier.vendor = inputId.vendor;
846 identifier.version = inputId.version;
847
848 // Get device physical location.
849 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
850 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
851 } else {
852 buffer[sizeof(buffer) - 1] = '\0';
853 identifier.location.setTo(buffer);
854 }
855
856 // Get device unique id.
857 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
858 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
859 } else {
860 buffer[sizeof(buffer) - 1] = '\0';
861 identifier.uniqueId.setTo(buffer);
862 }
863
864 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700865 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
866 LOGE("Error %d making device file descriptor non-blocking.", errno);
867 close(fd);
868 return -1;
869 }
870
Jeff Brown90655042010-12-02 13:50:46 -0800871 // Allocate device. (The device object takes ownership of the fd at this point.)
872 int32_t deviceId = mNextDeviceId++;
873 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874
875#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800876 LOGI("add device %d: %s\n", deviceId, devicePath);
877 LOGI(" bus: %04x\n"
878 " vendor %04x\n"
879 " product %04x\n"
880 " version %04x\n",
881 identifier.bus, identifier.vendor, identifier.product, identifier.version);
882 LOGI(" name: \"%s\"\n", identifier.name.string());
883 LOGI(" location: \"%s\"\n", identifier.location.string());
884 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
885 LOGI(" driver: v%d.%d.%d\n",
886 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887#endif
888
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800889 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700890 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800891
Jeff Brownfd0358292010-06-30 16:10:35 -0700892 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700893 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
894 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
895 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
896 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
897 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
898 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -0800899
Jeff Brown6f2fba42011-02-19 01:08:02 -0800900 // See if this is a keyboard. Ignore everything in the button range except for
901 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700902 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
903 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -0800904 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700905 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800906 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -0700907 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800908 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800909 if (haveKeyboardKeys || haveGamepadButtons) {
910 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800912
Jeff Brown83c09682010-12-23 17:50:18 -0800913 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700914 if (test_bit(BTN_MOUSE, device->keyBitmask)
915 && test_bit(REL_X, device->relBitmask)
916 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800917 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
Jeff Brownfd0358292010-06-30 16:10:35 -0700919
920 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800921 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700922 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
923 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800924 // Some joysticks such as the PS3 controller report axes that conflict
925 // with the ABS_MT range. Try to confirm that the device really is
926 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700927 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800928 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -0700929 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800930 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700931 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
932 && test_bit(ABS_X, device->absBitmask)
933 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800934 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800937 // See if this device is a joystick.
938 // Ignore touchscreens because they use the same absolute axes for other purposes.
939 // Assumes that joysticks always have gamepad buttons in order to distinguish them
940 // from other devices such as accelerometers that also have absolute axes.
941 if (haveGamepadButtons
942 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)
Jeff Brown93fa9b32011-06-14 17:09:25 -0700943 && containsNonZeroByte(device->absBitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800944 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
945 }
946
Jeff Brown93fa9b32011-06-14 17:09:25 -0700947 // Check whether this device has switches.
948 for (int i = 0; i <= SW_MAX; i++) {
949 if (test_bit(i, device->swBitmask)) {
950 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
951 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
Jeff Brown93fa9b32011-06-14 17:09:25 -0700955 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -0800956 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800957 // Load the virtual keys for the touch screen, if any.
958 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700959 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -0800960 if (!status) {
961 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
Jeff Brown90655042010-12-02 13:50:46 -0800963 }
964
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800965 // Load the key map.
966 // We need to do this for joysticks too because the key layout may specify axes.
967 status_t keyMapStatus = NAME_NOT_FOUND;
968 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -0800969 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700970 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800971 }
Jeff Brown90655042010-12-02 13:50:46 -0800972
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800973 // Configure the keyboard, gamepad or virtual keyboard.
974 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -0800975 // Set system properties for the keyboard.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700976 setKeyboardPropertiesLocked(device, false);
Jeff Brown497a92c2010-09-12 17:55:08 -0700977
Jeff Brown90655042010-12-02 13:50:46 -0800978 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800979 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -0800980 && mBuiltInKeyboardId == -1
981 && isEligibleBuiltInKeyboard(device->identifier,
982 device->configuration, &device->keyMap)) {
983 mBuiltInKeyboardId = device->id;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700984 setKeyboardPropertiesLocked(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -0700985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700987 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -0700988 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700989 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700990 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700991
Jeff Brownfd0358292010-06-30 16:10:35 -0700992 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -0700993 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
994 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
995 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
996 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
997 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700998 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700999 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001000
Jeff Brownfd0358292010-06-30 16:10:35 -07001001 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001002 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001003 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -07001004 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1005 break;
1006 }
1007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009
Sean McNeilaeb00c42010-06-23 16:00:37 +07001010 // If the device isn't recognized as something we handle, don't monitor it.
1011 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -08001012 LOGV("Dropping device: id=%d, path='%s', name='%s'",
1013 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001014 delete device;
1015 return -1;
1016 }
1017
Jeff Brown56194eb2011-03-02 19:23:13 -08001018 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001019 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001020 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1021 }
1022
Jeff Brown93fa9b32011-06-14 17:09:25 -07001023 // Register with epoll.
1024 struct epoll_event eventItem;
1025 memset(&eventItem, 0, sizeof(eventItem));
1026 eventItem.events = EPOLLIN;
1027 eventItem.data.u32 = deviceId;
1028 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1029 LOGE("Could not add device fd to epoll instance. errno=%d", errno);
1030 delete device;
1031 return -1;
1032 }
1033
Jeff Brown90655042010-12-02 13:50:46 -08001034 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1035 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
1036 deviceId, fd, devicePath, device->identifier.name.string(),
1037 device->classes,
1038 device->configurationFile.string(),
1039 device->keyMap.keyLayoutFile.string(),
1040 device->keyMap.keyCharacterMapFile.string(),
1041 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001042
Jeff Brown93fa9b32011-06-14 17:09:25 -07001043 mDevices.add(deviceId, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 device->next = mOpeningDevices;
1046 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 return 0;
1048}
1049
Jeff Brown93fa9b32011-06-14 17:09:25 -07001050void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001051 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1052 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001053 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -08001054 LOGD("No input device configuration file found for device '%s'.",
1055 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001056 } else {
1057 status_t status = PropertyMap::load(device->configurationFile,
1058 &device->configuration);
1059 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -08001060 LOGE("Error loading input device configuration file for device '%s'. "
1061 "Using default configuration.",
1062 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001063 }
1064 }
1065}
1066
Jeff Brown93fa9b32011-06-14 17:09:25 -07001067status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001068 // The virtual key map is supplied by the kernel as a system board property file.
1069 String8 path;
1070 path.append("/sys/board_properties/virtualkeys.");
1071 path.append(device->identifier.name);
1072 if (access(path.string(), R_OK)) {
1073 return NAME_NOT_FOUND;
1074 }
1075 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001076}
1077
Jeff Brown93fa9b32011-06-14 17:09:25 -07001078status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001079 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001080}
1081
Jeff Brown93fa9b32011-06-14 17:09:25 -07001082void EventHub::setKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001083 int32_t id = builtInKeyboard ? 0 : device->id;
1084 android::setKeyboardProperties(id, device->identifier,
1085 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1086}
1087
Jeff Brown93fa9b32011-06-14 17:09:25 -07001088void EventHub::clearKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001089 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001090 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -07001091}
1092
Jeff Brown93fa9b32011-06-14 17:09:25 -07001093bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001094 if (device->configuration) {
1095 bool value;
1096 if (device->configuration->tryGetProperty(String8("device.internal"), value)
1097 && value) {
1098 return false;
1099 }
1100 }
1101 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1102}
1103
Jeff Brown90655042010-12-02 13:50:46 -08001104bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1105 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001106 return false;
1107 }
1108
1109 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001110 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001111 const size_t N = scanCodes.size();
1112 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1113 int32_t sc = scanCodes.itemAt(i);
1114 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1115 return true;
1116 }
1117 }
1118
1119 return false;
1120}
1121
Jeff Brown93fa9b32011-06-14 17:09:25 -07001122status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1123 Device* device = getDeviceByPathLocked(devicePath);
1124 if (device) {
1125 closeDeviceLocked(device);
1126 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001128 LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 return -1;
1130}
1131
Jeff Brown93fa9b32011-06-14 17:09:25 -07001132void EventHub::closeAllDevicesLocked() {
1133 while (mDevices.size() > 0) {
1134 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1135 }
1136}
1137
1138void EventHub::closeDeviceLocked(Device* device) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001139 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1140 device->path.string(), device->identifier.name.string(), device->id,
1141 device->fd, device->classes);
1142
Jeff Brown33bbfd22011-02-24 20:55:35 -08001143 if (device->id == mBuiltInKeyboardId) {
1144 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
1145 device->path.string(), mBuiltInKeyboardId);
1146 mBuiltInKeyboardId = -1;
Jeff Brown93fa9b32011-06-14 17:09:25 -07001147 clearKeyboardPropertiesLocked(device, true);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001148 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001149 clearKeyboardPropertiesLocked(device, false);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001150
Jeff Brown93fa9b32011-06-14 17:09:25 -07001151 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
1152 LOGW("Could not remove device fd from epoll instance. errno=%d", errno);
1153 }
1154
1155 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001156 device->close();
1157
Jeff Brown8e9d4432011-03-12 19:46:59 -08001158 // Unlink for opening devices list if it is present.
1159 Device* pred = NULL;
1160 bool found = false;
1161 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1162 if (entry == device) {
1163 found = true;
1164 break;
1165 }
1166 pred = entry;
1167 entry = entry->next;
1168 }
1169 if (found) {
1170 // Unlink the device from the opening devices list then delete it.
1171 // We don't need to tell the client that the device was closed because
1172 // it does not even know it was opened in the first place.
1173 LOGI("Device %s was immediately closed after opening.", device->path.string());
1174 if (pred) {
1175 pred->next = device->next;
1176 } else {
1177 mOpeningDevices = device->next;
1178 }
1179 delete device;
1180 } else {
1181 // Link into closing devices list.
1182 // The device will be deleted later after we have informed the client.
1183 device->next = mClosingDevices;
1184 mClosingDevices = device;
1185 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001186}
1187
Jeff Brown93fa9b32011-06-14 17:09:25 -07001188status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 int res;
1190 char devname[PATH_MAX];
1191 char *filename;
1192 char event_buf[512];
1193 int event_size;
1194 int event_pos = 0;
1195 struct inotify_event *event;
1196
Jeff Brown93fa9b32011-06-14 17:09:25 -07001197 LOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
1198 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 if(res < (int)sizeof(*event)) {
1200 if(errno == EINTR)
1201 return 0;
1202 LOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001203 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205 //printf("got %d bytes of event information\n", res);
1206
Jeff Brown90655042010-12-02 13:50:46 -08001207 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 filename = devname + strlen(devname);
1209 *filename++ = '/';
1210
1211 while(res >= (int)sizeof(*event)) {
1212 event = (struct inotify_event *)(event_buf + event_pos);
1213 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1214 if(event->len) {
1215 strcpy(filename, event->name);
1216 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001217 openDeviceLocked(devname);
1218 } else {
1219 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221 }
1222 event_size = sizeof(*event) + event->len;
1223 res -= event_size;
1224 event_pos += event_size;
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 return 0;
1227}
1228
Jeff Brown93fa9b32011-06-14 17:09:25 -07001229status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230{
1231 char devname[PATH_MAX];
1232 char *filename;
1233 DIR *dir;
1234 struct dirent *de;
1235 dir = opendir(dirname);
1236 if(dir == NULL)
1237 return -1;
1238 strcpy(devname, dirname);
1239 filename = devname + strlen(devname);
1240 *filename++ = '/';
1241 while((de = readdir(dir))) {
1242 if(de->d_name[0] == '.' &&
1243 (de->d_name[1] == '\0' ||
1244 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1245 continue;
1246 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001247 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249 closedir(dir);
1250 return 0;
1251}
1252
Jeff Brown93fa9b32011-06-14 17:09:25 -07001253void EventHub::requestReopenDevices() {
1254 LOGV("requestReopenDevices() called");
1255
1256 AutoMutex _l(mLock);
1257 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001258}
1259
Jeff Brownf2f487182010-10-01 17:46:21 -07001260void EventHub::dump(String8& dump) {
1261 dump.append("Event Hub State:\n");
1262
1263 { // acquire lock
1264 AutoMutex _l(mLock);
1265
Jeff Brown90655042010-12-02 13:50:46 -08001266 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001267
1268 dump.append(INDENT "Devices:\n");
1269
Jeff Brown93fa9b32011-06-14 17:09:25 -07001270 for (size_t i = 0; i < mDevices.size(); i++) {
1271 const Device* device = mDevices.valueAt(i);
1272 if (mBuiltInKeyboardId == device->id) {
1273 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1274 device->id, device->identifier.name.string());
1275 } else {
1276 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1277 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001278 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001279 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1280 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
1281 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1282 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1283 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1284 "product=0x%04x, version=0x%04x\n",
1285 device->identifier.bus, device->identifier.vendor,
1286 device->identifier.product, device->identifier.version);
1287 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1288 device->keyMap.keyLayoutFile.string());
1289 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1290 device->keyMap.keyCharacterMapFile.string());
1291 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1292 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001293 }
1294 } // release lock
1295}
1296
Jeff Brown89ef0722011-08-10 16:25:21 -07001297void EventHub::monitor() {
1298 // Acquire and release the lock to ensure that the event hub has not deadlocked.
1299 mLock.lock();
1300 mLock.unlock();
1301}
1302
1303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304}; // namespace android