blob: 06dea36b3ff72d442af0135f5ee46b65abfa6075 [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 {
Jeff Brown06309752011-08-11 17:10:06 -0700315 *outValue = 0;
316
Jeff Brown2717eff2011-06-30 23:53:07 -0700317 if (axis >= 0 && axis <= ABS_MAX) {
318 AutoMutex _l(mLock);
319
320 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700321 if (device && test_bit(axis, device->absBitmask)) {
322 struct input_absinfo info;
323 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
324 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
325 axis, device->identifier.name.string(), device->fd, errno);
326 return -errno;
327 }
328
329 *outValue = info.value;
330 return OK;
Jeff Brown2717eff2011-06-30 23:53:07 -0700331 }
332 }
Jeff Brown2717eff2011-06-30 23:53:07 -0700333 return -1;
334}
335
Jeff Brown6d0fec22010-07-23 21:28:06 -0700336bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
337 const int32_t* keyCodes, uint8_t* outFlags) const {
338 AutoMutex _l(mLock);
339
Jeff Brown90655042010-12-02 13:50:46 -0800340 Device* device = getDeviceLocked(deviceId);
Jeff Brownba421dd2011-08-10 15:07:05 -0700341 if (device && device->keyMap.haveKeyLayout()) {
342 Vector<int32_t> scanCodes;
343 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
344 scanCodes.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700345
Jeff Brownba421dd2011-08-10 15:07:05 -0700346 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
347 keyCodes[codeIndex], &scanCodes);
348 if (! err) {
349 // check the possible scan codes identified by the layout map against the
350 // map of codes actually emitted by the driver
351 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
352 if (test_bit(scanCodes[sc], device->keyBitmask)) {
353 outFlags[codeIndex] = 1;
354 break;
355 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356 }
357 }
358 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700359 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700360 }
Jeff Brownba421dd2011-08-10 15:07:05 -0700361 return false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362}
363
Jeff Brown6f2fba42011-02-19 01:08:02 -0800364status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700365 int32_t* outKeycode, uint32_t* outFlags) const
366{
367 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800368 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700369
Jeff Brown90655042010-12-02 13:50:46 -0800370 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800371 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700372 if (err == NO_ERROR) {
373 return NO_ERROR;
374 }
375 }
376
Jeff Brown90655042010-12-02 13:50:46 -0800377 if (mBuiltInKeyboardId != -1) {
378 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700379
Jeff Brown90655042010-12-02 13:50:46 -0800380 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800381 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700382 if (err == NO_ERROR) {
383 return NO_ERROR;
384 }
385 }
386 }
387
388 *outKeycode = 0;
389 *outFlags = 0;
390 return NAME_NOT_FOUND;
391}
392
Jeff Brown85297452011-03-04 13:07:49 -0800393status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800394{
395 AutoMutex _l(mLock);
396 Device* device = getDeviceLocked(deviceId);
397
398 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800399 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800400 if (err == NO_ERROR) {
401 return NO_ERROR;
402 }
403 }
404
405 if (mBuiltInKeyboardId != -1) {
406 device = getDeviceLocked(mBuiltInKeyboardId);
407
408 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800409 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800410 if (err == NO_ERROR) {
411 return NO_ERROR;
412 }
413 }
414 }
415
Jeff Brown6f2fba42011-02-19 01:08:02 -0800416 return NAME_NOT_FOUND;
417}
418
Jeff Brown1a84fd12011-06-02 01:26:32 -0700419void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700420 AutoMutex _l(mLock);
421
Jeff Brown1a84fd12011-06-02 01:26:32 -0700422 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400423}
424
Jeff Brown49754db2011-07-01 17:37:58 -0700425bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
426 AutoMutex _l(mLock);
427 Device* device = getDeviceLocked(deviceId);
428 if (device && scanCode >= 0 && scanCode <= KEY_MAX) {
429 if (test_bit(scanCode, device->keyBitmask)) {
430 return true;
431 }
432 }
433 return false;
434}
435
Jeff Brown497a92c2010-09-12 17:55:08 -0700436bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
437 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800438 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700439 if (device && led >= 0 && led <= LED_MAX) {
440 if (test_bit(led, device->ledBitmask)) {
441 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700442 }
443 }
444 return false;
445}
446
447void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
448 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800449 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700450 if (device && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700451 struct input_event ev;
452 ev.time.tv_sec = 0;
453 ev.time.tv_usec = 0;
454 ev.type = EV_LED;
455 ev.code = led;
456 ev.value = on ? 1 : 0;
457
458 ssize_t nWrite;
459 do {
460 nWrite = write(device->fd, &ev, sizeof(struct input_event));
461 } while (nWrite == -1 && errno == EINTR);
462 }
463}
464
Jeff Brown90655042010-12-02 13:50:46 -0800465void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
466 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
467 outVirtualKeys.clear();
468
469 AutoMutex _l(mLock);
470 Device* device = getDeviceLocked(deviceId);
471 if (device && device->virtualKeyMap) {
472 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
473 }
474}
475
476EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
477 if (deviceId == 0) {
478 deviceId = mBuiltInKeyboardId;
479 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700480 ssize_t index = mDevices.indexOfKey(deviceId);
481 return index >= 0 ? mDevices.valueAt(index) : NULL;
482}
Jeff Brown90655042010-12-02 13:50:46 -0800483
Jeff Brown93fa9b32011-06-14 17:09:25 -0700484EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
485 for (size_t i = 0; i < mDevices.size(); i++) {
486 Device* device = mDevices.valueAt(i);
487 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800488 return device;
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491 return NULL;
492}
493
Jeff Brownb7198742011-03-18 18:14:26 -0700494size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Jeff Brownb6110c22011-04-01 16:15:13 -0700495 LOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400496
Jeff Brown93fa9b32011-06-14 17:09:25 -0700497 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400498
Jeff Brownb7198742011-03-18 18:14:26 -0700499 struct input_event readBuffer[bufferSize];
500
501 RawEvent* event = buffer;
502 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700503 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700504 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700505 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
506
Jeff Brown1a84fd12011-06-02 01:26:32 -0700507 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700508 if (mNeedToReopenDevices) {
509 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700510
511 LOGI("Reopening all input devices due to a configuration change.");
512
Jeff Brown93fa9b32011-06-14 17:09:25 -0700513 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700514 mNeedToScanDevices = true;
515 break; // return to the caller before we actually rescan
516 }
517
Jeff Browncc2e7172010-08-17 16:48:25 -0700518 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700519 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800520 Device* device = mClosingDevices;
521 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 device->id, device->path.string());
523 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700524 event->when = now;
525 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
526 event->type = DEVICE_REMOVED;
527 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700529 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700530 if (--capacity == 0) {
531 break;
532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700534
Jeff Brown1a84fd12011-06-02 01:26:32 -0700535 if (mNeedToScanDevices) {
536 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700537 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700538 mNeedToSendFinishedDeviceScan = true;
539 }
540
Jeff Brownb7198742011-03-18 18:14:26 -0700541 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800542 Device* device = mOpeningDevices;
543 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 device->id, device->path.string());
545 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700546 event->when = now;
547 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
548 event->type = DEVICE_ADDED;
549 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700550 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700551 if (--capacity == 0) {
552 break;
553 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700554 }
555
556 if (mNeedToSendFinishedDeviceScan) {
557 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700558 event->when = now;
559 event->type = FINISHED_DEVICE_SCAN;
560 event += 1;
561 if (--capacity == 0) {
562 break;
563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
Jeff Browncc2e7172010-08-17 16:48:25 -0700566 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700567 bool deviceChanged = false;
568 while (mPendingEventIndex < mPendingEventCount) {
569 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
570 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
571 if (eventItem.events & EPOLLIN) {
572 mPendingINotify = true;
573 } else {
574 LOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
575 }
576 continue;
577 }
578
579 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
580 if (eventItem.events & EPOLLIN) {
581 LOGV("awoken after wake()");
582 awoken = true;
583 char buffer[16];
584 ssize_t nRead;
585 do {
586 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
587 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
588 } else {
589 LOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
590 eventItem.events);
591 }
592 continue;
593 }
594
595 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
596 if (deviceIndex < 0) {
597 LOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
598 eventItem.events, eventItem.data.u32);
599 continue;
600 }
601
602 Device* device = mDevices.valueAt(deviceIndex);
603 if (eventItem.events & EPOLLIN) {
604 int32_t readSize = read(device->fd, readBuffer,
605 sizeof(struct input_event) * capacity);
606 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
607 // Device was removed before INotify noticed.
608 deviceChanged = true;
609 closeDeviceLocked(device);
610 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700611 if (errno != EAGAIN && errno != EINTR) {
612 LOGW("could not get event (errno=%d)", errno);
613 }
614 } else if ((readSize % sizeof(struct input_event)) != 0) {
615 LOGE("could not get event (wrong size: %d)", readSize);
616 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700617 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
618
619 size_t count = size_t(readSize) / sizeof(struct input_event);
620 for (size_t i = 0; i < count; i++) {
621 const struct input_event& iev = readBuffer[i];
622 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
623 device->path.string(),
624 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
625 iev.type, iev.code, iev.value);
626
Jeff Brown4e91a182011-04-07 11:38:09 -0700627#ifdef HAVE_POSIX_CLOCKS
628 // Use the time specified in the event instead of the current time
629 // so that downstream code can get more accurate estimates of
630 // event dispatch latency from the time the event is enqueued onto
631 // the evdev client buffer.
632 //
633 // The event's timestamp fortuitously uses the same monotonic clock
634 // time base as the rest of Android. The kernel event device driver
635 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
636 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
637 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
638 // system call that also queries ktime_get_ts().
639 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
640 + nsecs_t(iev.time.tv_usec) * 1000LL;
641 LOGV("event time %lld, now %lld", event->when, now);
642#else
Jeff Brownb7198742011-03-18 18:14:26 -0700643 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700644#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700645 event->deviceId = deviceId;
646 event->type = iev.type;
647 event->scanCode = iev.code;
648 event->value = iev.value;
649 event->keyCode = AKEYCODE_UNKNOWN;
650 event->flags = 0;
651 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
652 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
653 &event->keyCode, &event->flags);
654 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
655 iev.code, event->keyCode, event->flags, err);
656 }
657 event += 1;
658 }
659 capacity -= count;
660 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700661 // The result buffer is full. Reset the pending event index
662 // so we will try to read the device again on the next iteration.
663 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700664 break;
665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700667 } else {
668 LOGW("Received unexpected epoll event 0x%08x for device %s.",
669 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
671 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700672
Jeff Brown93fa9b32011-06-14 17:09:25 -0700673 // readNotify() will modify the list of devices so this must be done after
674 // processing all other events to ensure that we read all remaining events
675 // before closing the devices.
676 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
677 mPendingINotify = false;
678 readNotifyLocked();
679 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800680 }
681
Jeff Brown93fa9b32011-06-14 17:09:25 -0700682 // Report added or removed devices immediately.
683 if (deviceChanged) {
684 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
Jeff Browna9b84222010-10-14 02:23:43 -0700686
Jeff Brown93fa9b32011-06-14 17:09:25 -0700687 // Return now if we have collected any events or if we were explicitly awoken.
688 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700689 break;
690 }
691
Jeff Browncc2e7172010-08-17 16:48:25 -0700692 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700693 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700694 // subtle choreography. When a device driver has pending (unread) events, it acquires
695 // a kernel wake lock. However, once the last pending event has been read, the device
696 // driver will release the kernel wake lock. To prevent the system from going to sleep
697 // when this happens, the EventHub holds onto its own user wake lock while the client
698 // is processing events. Thus the system can only sleep if there are no events
699 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700700 //
701 // The timeout is advisory only. If the device is asleep, it will not wake just to
702 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700703 mPendingEventIndex = 0;
704
705 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700706 release_wake_lock(WAKE_LOCK_ID);
707
Jeff Brown93fa9b32011-06-14 17:09:25 -0700708 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700709
710 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700711 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700712
Jeff Brownaa3855d2011-03-17 01:34:19 -0700713 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700714 // Timed out.
715 mPendingEventCount = 0;
716 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700717 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700718
Jeff Brownaa3855d2011-03-17 01:34:19 -0700719 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700720 // An error occurred.
721 mPendingEventCount = 0;
722
Jeff Brownb7198742011-03-18 18:14:26 -0700723 // Sleep after errors to avoid locking up the system.
724 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700725 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700726 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700727 usleep(100000);
728 }
Jeff Brownb7198742011-03-18 18:14:26 -0700729 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700730 // Some events occurred.
731 mPendingEventCount = size_t(pollResult);
732
Jeff Brownb7198742011-03-18 18:14:26 -0700733 // On an SMP system, it is possible for the framework to read input events
734 // faster than the kernel input device driver can produce a complete packet.
735 // Because poll() wakes up as soon as the first input event becomes available,
736 // the framework will often end up reading one event at a time until the
737 // packet is complete. Instead of one call to read() returning 71 events,
738 // it could take 71 calls to read() each returning 1 event.
739 //
740 // Sleep for a short period of time after waking up from the poll() to give
741 // the kernel time to finish writing the entire packet of input events.
742 if (mNumCpus > 1) {
743 usleep(250);
744 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
Jeff Brownb7198742011-03-18 18:14:26 -0700747
748 // All done, return the number of events we read.
749 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750}
751
Jeff Brown93fa9b32011-06-14 17:09:25 -0700752void EventHub::wake() {
753 LOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754
Jeff Brown93fa9b32011-06-14 17:09:25 -0700755 ssize_t nWrite;
756 do {
757 nWrite = write(mWakeWritePipeFd, "W", 1);
758 } while (nWrite == -1 && errno == EINTR);
759
760 if (nWrite != 1 && errno != EAGAIN) {
761 LOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700763}
Jeff Brown90655042010-12-02 13:50:46 -0800764
Jeff Brown93fa9b32011-06-14 17:09:25 -0700765void EventHub::scanDevicesLocked() {
766 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800768 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770}
771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772// ----------------------------------------------------------------------------
773
Jeff Brownfd0358292010-06-30 16:10:35 -0700774static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
775 const uint8_t* end = array + endIndex;
776 array += startIndex;
777 while (array != end) {
778 if (*(array++) != 0) {
779 return true;
780 }
781 }
782 return false;
783}
784
785static const int32_t GAMEPAD_KEYCODES[] = {
786 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
787 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
788 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
789 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
790 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800791 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
792 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
793 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
794 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
795 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd0358292010-06-30 16:10:35 -0700796};
797
Jeff Brown93fa9b32011-06-14 17:09:25 -0700798status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800799 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800
Jeff Brown90655042010-12-02 13:50:46 -0800801 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802
Jeff Brown90655042010-12-02 13:50:46 -0800803 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800805 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 return -1;
807 }
808
Jeff Brown90655042010-12-02 13:50:46 -0800809 InputDeviceIdentifier identifier;
810
811 // Get device name.
812 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
813 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
814 } else {
815 buffer[sizeof(buffer) - 1] = '\0';
816 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400818
Jeff Brown90655042010-12-02 13:50:46 -0800819 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700820 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
821 const String8& item = mExcludedDevices.itemAt(i);
822 if (identifier.name == item) {
823 LOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400824 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400825 return -1;
826 }
827 }
828
Jeff Brown90655042010-12-02 13:50:46 -0800829 // Get device driver version.
830 int driverVersion;
831 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
832 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
833 close(fd);
834 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
836
Jeff Brown90655042010-12-02 13:50:46 -0800837 // Get device identifier.
838 struct input_id inputId;
839 if(ioctl(fd, EVIOCGID, &inputId)) {
840 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
841 close(fd);
842 return -1;
843 }
844 identifier.bus = inputId.bustype;
845 identifier.product = inputId.product;
846 identifier.vendor = inputId.vendor;
847 identifier.version = inputId.version;
848
849 // Get device physical location.
850 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
851 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
852 } else {
853 buffer[sizeof(buffer) - 1] = '\0';
854 identifier.location.setTo(buffer);
855 }
856
857 // Get device unique id.
858 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
859 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
860 } else {
861 buffer[sizeof(buffer) - 1] = '\0';
862 identifier.uniqueId.setTo(buffer);
863 }
864
865 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700866 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
867 LOGE("Error %d making device file descriptor non-blocking.", errno);
868 close(fd);
869 return -1;
870 }
871
Jeff Brown90655042010-12-02 13:50:46 -0800872 // Allocate device. (The device object takes ownership of the fd at this point.)
873 int32_t deviceId = mNextDeviceId++;
874 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875
876#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800877 LOGI("add device %d: %s\n", deviceId, devicePath);
878 LOGI(" bus: %04x\n"
879 " vendor %04x\n"
880 " product %04x\n"
881 " version %04x\n",
882 identifier.bus, identifier.vendor, identifier.product, identifier.version);
883 LOGI(" name: \"%s\"\n", identifier.name.string());
884 LOGI(" location: \"%s\"\n", identifier.location.string());
885 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
886 LOGI(" driver: v%d.%d.%d\n",
887 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888#endif
889
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800890 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700891 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800892
Jeff Brownfd0358292010-06-30 16:10:35 -0700893 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700894 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
895 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
896 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
897 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
898 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
899 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -0800900
Jeff Brown6f2fba42011-02-19 01:08:02 -0800901 // See if this is a keyboard. Ignore everything in the button range except for
902 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700903 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
904 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -0800905 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700906 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800907 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -0700908 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800909 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800910 if (haveKeyboardKeys || haveGamepadButtons) {
911 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800913
Jeff Brown83c09682010-12-23 17:50:18 -0800914 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700915 if (test_bit(BTN_MOUSE, device->keyBitmask)
916 && test_bit(REL_X, device->relBitmask)
917 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800918 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
Jeff Brownfd0358292010-06-30 16:10:35 -0700920
921 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800922 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700923 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
924 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800925 // Some joysticks such as the PS3 controller report axes that conflict
926 // with the ABS_MT range. Try to confirm that the device really is
927 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700928 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800929 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd0358292010-06-30 16:10:35 -0700930 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800931 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700932 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
933 && test_bit(ABS_X, device->absBitmask)
934 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800935 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
937
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800938 // See if this device is a joystick.
939 // Ignore touchscreens because they use the same absolute axes for other purposes.
940 // Assumes that joysticks always have gamepad buttons in order to distinguish them
941 // from other devices such as accelerometers that also have absolute axes.
942 if (haveGamepadButtons
943 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)
Jeff Brown93fa9b32011-06-14 17:09:25 -0700944 && containsNonZeroByte(device->absBitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800945 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
946 }
947
Jeff Brown93fa9b32011-06-14 17:09:25 -0700948 // Check whether this device has switches.
949 for (int i = 0; i <= SW_MAX; i++) {
950 if (test_bit(i, device->swBitmask)) {
951 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
952 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955
Jeff Brown93fa9b32011-06-14 17:09:25 -0700956 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -0800957 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800958 // Load the virtual keys for the touch screen, if any.
959 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700960 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -0800961 if (!status) {
962 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
Jeff Brown90655042010-12-02 13:50:46 -0800964 }
965
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800966 // Load the key map.
967 // We need to do this for joysticks too because the key layout may specify axes.
968 status_t keyMapStatus = NAME_NOT_FOUND;
969 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -0800970 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700971 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800972 }
Jeff Brown90655042010-12-02 13:50:46 -0800973
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800974 // Configure the keyboard, gamepad or virtual keyboard.
975 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -0800976 // Set system properties for the keyboard.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700977 setKeyboardPropertiesLocked(device, false);
Jeff Brown497a92c2010-09-12 17:55:08 -0700978
Jeff Brown90655042010-12-02 13:50:46 -0800979 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800980 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -0800981 && mBuiltInKeyboardId == -1
982 && isEligibleBuiltInKeyboard(device->identifier,
983 device->configuration, &device->keyMap)) {
984 mBuiltInKeyboardId = device->id;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700985 setKeyboardPropertiesLocked(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -0700986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700988 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -0700989 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700990 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700991 }
Jeff Brown497a92c2010-09-12 17:55:08 -0700992
Jeff Brownfd0358292010-06-30 16:10:35 -0700993 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -0700994 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
995 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
996 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
997 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
998 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700999 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001000 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001001
Jeff Brownfd0358292010-06-30 16:10:35 -07001002 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001003 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001004 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd0358292010-06-30 16:10:35 -07001005 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1006 break;
1007 }
1008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
1010
Sean McNeilaeb00c42010-06-23 16:00:37 +07001011 // If the device isn't recognized as something we handle, don't monitor it.
1012 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -08001013 LOGV("Dropping device: id=%d, path='%s', name='%s'",
1014 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001015 delete device;
1016 return -1;
1017 }
1018
Jeff Brown56194eb2011-03-02 19:23:13 -08001019 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001020 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001021 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1022 }
1023
Jeff Brown93fa9b32011-06-14 17:09:25 -07001024 // Register with epoll.
1025 struct epoll_event eventItem;
1026 memset(&eventItem, 0, sizeof(eventItem));
1027 eventItem.events = EPOLLIN;
1028 eventItem.data.u32 = deviceId;
1029 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1030 LOGE("Could not add device fd to epoll instance. errno=%d", errno);
1031 delete device;
1032 return -1;
1033 }
1034
Jeff Brown90655042010-12-02 13:50:46 -08001035 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1036 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
1037 deviceId, fd, devicePath, device->identifier.name.string(),
1038 device->classes,
1039 device->configurationFile.string(),
1040 device->keyMap.keyLayoutFile.string(),
1041 device->keyMap.keyCharacterMapFile.string(),
1042 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001043
Jeff Brown93fa9b32011-06-14 17:09:25 -07001044 mDevices.add(deviceId, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 device->next = mOpeningDevices;
1047 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 return 0;
1049}
1050
Jeff Brown93fa9b32011-06-14 17:09:25 -07001051void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001052 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1053 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001054 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -08001055 LOGD("No input device configuration file found for device '%s'.",
1056 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001057 } else {
1058 status_t status = PropertyMap::load(device->configurationFile,
1059 &device->configuration);
1060 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -08001061 LOGE("Error loading input device configuration file for device '%s'. "
1062 "Using default configuration.",
1063 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001064 }
1065 }
1066}
1067
Jeff Brown93fa9b32011-06-14 17:09:25 -07001068status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001069 // The virtual key map is supplied by the kernel as a system board property file.
1070 String8 path;
1071 path.append("/sys/board_properties/virtualkeys.");
1072 path.append(device->identifier.name);
1073 if (access(path.string(), R_OK)) {
1074 return NAME_NOT_FOUND;
1075 }
1076 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001077}
1078
Jeff Brown93fa9b32011-06-14 17:09:25 -07001079status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001080 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001081}
1082
Jeff Brown93fa9b32011-06-14 17:09:25 -07001083void EventHub::setKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001084 int32_t id = builtInKeyboard ? 0 : device->id;
1085 android::setKeyboardProperties(id, device->identifier,
1086 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1087}
1088
Jeff Brown93fa9b32011-06-14 17:09:25 -07001089void EventHub::clearKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001090 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001091 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -07001092}
1093
Jeff Brown93fa9b32011-06-14 17:09:25 -07001094bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001095 if (device->configuration) {
1096 bool value;
Max Braune81056f2011-08-30 14:35:45 -07001097 if (device->configuration->tryGetProperty(String8("device.internal"), value)) {
1098 return !value;
Jeff Brown56194eb2011-03-02 19:23:13 -08001099 }
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