blob: ca2540bfad20f37748102d510cd73ff8d04f5685 [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 Brownfd035822010-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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800209 Device* device = getDeviceLocked(deviceId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 if (device == NULL) return -1;
211
212 struct input_absinfo info;
213
Jens Gulinc4554b92010-06-22 22:21:57 +0200214 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
Jeff Brown2717eff2011-06-30 23:53:07 -0700215 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
216 axis, device->identifier.name.string(), device->fd, errno);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700217 return -errno;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700219
220 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;
Jeff Brownb3a2d132011-06-12 18:14:50 -0700226 outAxisInfo->resolution = info.resolution;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700227 }
228 return OK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229}
230
Jeff Browncc0c1592011-02-19 05:07:28 -0800231bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
232 if (axis >= 0 && axis <= REL_MAX) {
233 AutoMutex _l(mLock);
234
235 Device* device = getDeviceLocked(deviceId);
236 if (device && device->relBitmask) {
237 return test_bit(axis, device->relBitmask);
238 }
239 }
240 return false;
241}
242
Jeff Brown80fd47c2011-05-24 01:07:44 -0700243bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
244 if (property >= 0 && property <= INPUT_PROP_MAX) {
245 AutoMutex _l(mLock);
246
247 Device* device = getDeviceLocked(deviceId);
248 if (device && device->propBitmask) {
249 return test_bit(property, device->propBitmask);
250 }
251 }
252 return false;
253}
254
Jeff Brown6d0fec22010-07-23 21:28:06 -0700255int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700256 if (scanCode >= 0 && scanCode <= KEY_MAX) {
257 AutoMutex _l(mLock);
258
Jeff Brown90655042010-12-02 13:50:46 -0800259 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700260 if (device != NULL) {
261 return getScanCodeStateLocked(device, scanCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700264 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265}
266
Jeff Brown90655042010-12-02 13:50:46 -0800267int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const {
Jeff Brownfd035822010-06-30 16:10:35 -0700268 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700269 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200270 if (ioctl(device->fd,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700271 EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700272 return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700273 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700274 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700275}
276
Jeff Brown6d0fec22010-07-23 21:28:06 -0700277int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
278 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700279
Jeff Brown90655042010-12-02 13:50:46 -0800280 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700281 if (device != NULL) {
282 return getKeyCodeStateLocked(device, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700284 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285}
286
Jeff Brown90655042010-12-02 13:50:46 -0800287int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const {
288 if (!device->keyMap.haveKeyLayout()) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800289 return AKEY_STATE_UNKNOWN;
290 }
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800293 device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700294
Jeff Brownfd035822010-06-30 16:10:35 -0700295 uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 memset(key_bitmask, 0, sizeof(key_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200297 if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 #if 0
299 for (size_t i=0; i<=KEY_MAX; i++) {
300 LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
301 }
302 #endif
303 const size_t N = scanCodes.size();
304 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
305 int32_t sc = scanCodes.itemAt(i);
306 //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
307 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700308 return AKEY_STATE_DOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 }
310 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700311 return AKEY_STATE_UP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700313 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700314}
315
Jeff Brown6d0fec22010-07-23 21:28:06 -0700316int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700317 if (sw >= 0 && sw <= SW_MAX) {
318 AutoMutex _l(mLock);
319
Jeff Brown90655042010-12-02 13:50:46 -0800320 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700321 if (device != NULL) {
322 return getSwitchStateLocked(device, sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700323 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700324 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700325 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700326}
327
Jeff Brown90655042010-12-02 13:50:46 -0800328int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const {
Jeff Brownfd035822010-06-30 16:10:35 -0700329 uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700330 memset(sw_bitmask, 0, sizeof(sw_bitmask));
Jens Gulinc4554b92010-06-22 22:21:57 +0200331 if (ioctl(device->fd,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700332 EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700333 return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700334 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700335 return AKEY_STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336}
337
Jeff Brown2717eff2011-06-30 23:53:07 -0700338status_t EventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t* outValue) const {
339 if (axis >= 0 && axis <= ABS_MAX) {
340 AutoMutex _l(mLock);
341
342 Device* device = getDeviceLocked(deviceId);
343 if (device != NULL) {
344 return getAbsoluteAxisValueLocked(device, axis, outValue);
345 }
346 }
347 *outValue = 0;
348 return -1;
349}
350
351status_t EventHub::getAbsoluteAxisValueLocked(Device* device, int32_t axis,
352 int32_t* outValue) const {
353 struct input_absinfo info;
354
355 if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
356 LOGW("Error reading absolute controller %d for device %s fd %d, errno=%d",
357 axis, device->identifier.name.string(), device->fd, errno);
358 return -errno;
359 }
360
361 *outValue = info.value;
362 return OK;
363}
364
Jeff Brown6d0fec22010-07-23 21:28:06 -0700365bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
366 const int32_t* keyCodes, uint8_t* outFlags) const {
367 AutoMutex _l(mLock);
368
Jeff Brown90655042010-12-02 13:50:46 -0800369 Device* device = getDeviceLocked(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700370 if (device != NULL) {
371 return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
372 }
373 return false;
374}
375
Jeff Brown90655042010-12-02 13:50:46 -0800376bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377 const int32_t* keyCodes, uint8_t* outFlags) const {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700378 if (!device->keyMap.haveKeyLayout()) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700379 return false;
380 }
381
382 Vector<int32_t> scanCodes;
383 for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
384 scanCodes.clear();
385
Jeff Brown6f2fba42011-02-19 01:08:02 -0800386 status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
387 keyCodes[codeIndex], &scanCodes);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 if (! err) {
389 // check the possible scan codes identified by the layout map against the
390 // map of codes actually emitted by the driver
391 for (size_t sc = 0; sc < scanCodes.size(); sc++) {
392 if (test_bit(scanCodes[sc], device->keyBitmask)) {
393 outFlags[codeIndex] = 1;
394 break;
395 }
396 }
397 }
398 }
399 return true;
400}
401
Jeff Brown6f2fba42011-02-19 01:08:02 -0800402status_t EventHub::mapKey(int32_t deviceId, int scancode,
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700403 int32_t* outKeycode, uint32_t* outFlags) const
404{
405 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800406 Device* device = getDeviceLocked(deviceId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700407
Jeff Brown90655042010-12-02 13:50:46 -0800408 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800409 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700410 if (err == NO_ERROR) {
411 return NO_ERROR;
412 }
413 }
414
Jeff Brown90655042010-12-02 13:50:46 -0800415 if (mBuiltInKeyboardId != -1) {
416 device = getDeviceLocked(mBuiltInKeyboardId);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700417
Jeff Brown90655042010-12-02 13:50:46 -0800418 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800419 status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
Dianne Hackborne3dd8842009-07-14 12:06:54 -0700420 if (err == NO_ERROR) {
421 return NO_ERROR;
422 }
423 }
424 }
425
426 *outKeycode = 0;
427 *outFlags = 0;
428 return NAME_NOT_FOUND;
429}
430
Jeff Brown85297452011-03-04 13:07:49 -0800431status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
Jeff Brown6f2fba42011-02-19 01:08:02 -0800432{
433 AutoMutex _l(mLock);
434 Device* device = getDeviceLocked(deviceId);
435
436 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800437 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800438 if (err == NO_ERROR) {
439 return NO_ERROR;
440 }
441 }
442
443 if (mBuiltInKeyboardId != -1) {
444 device = getDeviceLocked(mBuiltInKeyboardId);
445
446 if (device && device->keyMap.haveKeyLayout()) {
Jeff Brown85297452011-03-04 13:07:49 -0800447 status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -0800448 if (err == NO_ERROR) {
449 return NO_ERROR;
450 }
451 }
452 }
453
Jeff Brown6f2fba42011-02-19 01:08:02 -0800454 return NAME_NOT_FOUND;
455}
456
Jeff Brown1a84fd12011-06-02 01:26:32 -0700457void EventHub::setExcludedDevices(const Vector<String8>& devices) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700458 AutoMutex _l(mLock);
459
Jeff Brown1a84fd12011-06-02 01:26:32 -0700460 mExcludedDevices = devices;
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400461}
462
Jeff Brown497a92c2010-09-12 17:55:08 -0700463bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
464 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800465 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700466 if (device && led >= 0 && led <= LED_MAX) {
467 if (test_bit(led, device->ledBitmask)) {
468 return true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700469 }
470 }
471 return false;
472}
473
474void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
475 AutoMutex _l(mLock);
Jeff Brown90655042010-12-02 13:50:46 -0800476 Device* device = getDeviceLocked(deviceId);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700477 if (device && led >= 0 && led <= LED_MAX) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700478 struct input_event ev;
479 ev.time.tv_sec = 0;
480 ev.time.tv_usec = 0;
481 ev.type = EV_LED;
482 ev.code = led;
483 ev.value = on ? 1 : 0;
484
485 ssize_t nWrite;
486 do {
487 nWrite = write(device->fd, &ev, sizeof(struct input_event));
488 } while (nWrite == -1 && errno == EINTR);
489 }
490}
491
Jeff Brown90655042010-12-02 13:50:46 -0800492void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
493 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
494 outVirtualKeys.clear();
495
496 AutoMutex _l(mLock);
497 Device* device = getDeviceLocked(deviceId);
498 if (device && device->virtualKeyMap) {
499 outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
500 }
501}
502
503EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
504 if (deviceId == 0) {
505 deviceId = mBuiltInKeyboardId;
506 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700507 ssize_t index = mDevices.indexOfKey(deviceId);
508 return index >= 0 ? mDevices.valueAt(index) : NULL;
509}
Jeff Brown90655042010-12-02 13:50:46 -0800510
Jeff Brown93fa9b32011-06-14 17:09:25 -0700511EventHub::Device* EventHub::getDeviceByPathLocked(const char* devicePath) const {
512 for (size_t i = 0; i < mDevices.size(); i++) {
513 Device* device = mDevices.valueAt(i);
514 if (device->path == devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800515 return device;
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518 return NULL;
519}
520
Jeff Brownb7198742011-03-18 18:14:26 -0700521size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Jeff Brownb6110c22011-04-01 16:15:13 -0700522 LOG_ASSERT(bufferSize >= 1);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400523
Jeff Brown93fa9b32011-06-14 17:09:25 -0700524 AutoMutex _l(mLock);
Mike Lockwood1d9dfc52009-07-16 11:11:18 -0400525
Jeff Brownb7198742011-03-18 18:14:26 -0700526 struct input_event readBuffer[bufferSize];
527
528 RawEvent* event = buffer;
529 size_t capacity = bufferSize;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700530 bool awoken = false;
Jeff Browncc2e7172010-08-17 16:48:25 -0700531 for (;;) {
Jeff Brownb7198742011-03-18 18:14:26 -0700532 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
533
Jeff Brown1a84fd12011-06-02 01:26:32 -0700534 // Reopen input devices if needed.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700535 if (mNeedToReopenDevices) {
536 mNeedToReopenDevices = false;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700537
538 LOGI("Reopening all input devices due to a configuration change.");
539
Jeff Brown93fa9b32011-06-14 17:09:25 -0700540 closeAllDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700541 mNeedToScanDevices = true;
542 break; // return to the caller before we actually rescan
543 }
544
Jeff Browncc2e7172010-08-17 16:48:25 -0700545 // Report any devices that had last been added/removed.
Jeff Brownb7198742011-03-18 18:14:26 -0700546 while (mClosingDevices) {
Jeff Brown90655042010-12-02 13:50:46 -0800547 Device* device = mClosingDevices;
548 LOGV("Reporting device closed: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 device->id, device->path.string());
550 mClosingDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700551 event->when = now;
552 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
553 event->type = DEVICE_REMOVED;
554 event += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 delete device;
Jeff Brown7342bb92010-10-01 18:55:43 -0700556 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700557 if (--capacity == 0) {
558 break;
559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700561
Jeff Brown1a84fd12011-06-02 01:26:32 -0700562 if (mNeedToScanDevices) {
563 mNeedToScanDevices = false;
Jeff Brown93fa9b32011-06-14 17:09:25 -0700564 scanDevicesLocked();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700565 mNeedToSendFinishedDeviceScan = true;
566 }
567
Jeff Brownb7198742011-03-18 18:14:26 -0700568 while (mOpeningDevices != NULL) {
Jeff Brown90655042010-12-02 13:50:46 -0800569 Device* device = mOpeningDevices;
570 LOGV("Reporting device opened: id=%d, name=%s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 device->id, device->path.string());
572 mOpeningDevices = device->next;
Jeff Brownb7198742011-03-18 18:14:26 -0700573 event->when = now;
574 event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
575 event->type = DEVICE_ADDED;
576 event += 1;
Jeff Brown7342bb92010-10-01 18:55:43 -0700577 mNeedToSendFinishedDeviceScan = true;
Jeff Brownb7198742011-03-18 18:14:26 -0700578 if (--capacity == 0) {
579 break;
580 }
Jeff Brown7342bb92010-10-01 18:55:43 -0700581 }
582
583 if (mNeedToSendFinishedDeviceScan) {
584 mNeedToSendFinishedDeviceScan = false;
Jeff Brownb7198742011-03-18 18:14:26 -0700585 event->when = now;
586 event->type = FINISHED_DEVICE_SCAN;
587 event += 1;
588 if (--capacity == 0) {
589 break;
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592
Jeff Browncc2e7172010-08-17 16:48:25 -0700593 // Grab the next input event.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700594 bool deviceChanged = false;
595 while (mPendingEventIndex < mPendingEventCount) {
596 const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
597 if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
598 if (eventItem.events & EPOLLIN) {
599 mPendingINotify = true;
600 } else {
601 LOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
602 }
603 continue;
604 }
605
606 if (eventItem.data.u32 == EPOLL_ID_WAKE) {
607 if (eventItem.events & EPOLLIN) {
608 LOGV("awoken after wake()");
609 awoken = true;
610 char buffer[16];
611 ssize_t nRead;
612 do {
613 nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
614 } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
615 } else {
616 LOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
617 eventItem.events);
618 }
619 continue;
620 }
621
622 ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
623 if (deviceIndex < 0) {
624 LOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
625 eventItem.events, eventItem.data.u32);
626 continue;
627 }
628
629 Device* device = mDevices.valueAt(deviceIndex);
630 if (eventItem.events & EPOLLIN) {
631 int32_t readSize = read(device->fd, readBuffer,
632 sizeof(struct input_event) * capacity);
633 if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
634 // Device was removed before INotify noticed.
635 deviceChanged = true;
636 closeDeviceLocked(device);
637 } else if (readSize < 0) {
Jeff Browncc2e7172010-08-17 16:48:25 -0700638 if (errno != EAGAIN && errno != EINTR) {
639 LOGW("could not get event (errno=%d)", errno);
640 }
641 } else if ((readSize % sizeof(struct input_event)) != 0) {
642 LOGE("could not get event (wrong size: %d)", readSize);
643 } else {
Jeff Brownb7198742011-03-18 18:14:26 -0700644 int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
645
646 size_t count = size_t(readSize) / sizeof(struct input_event);
647 for (size_t i = 0; i < count; i++) {
648 const struct input_event& iev = readBuffer[i];
649 LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
650 device->path.string(),
651 (int) iev.time.tv_sec, (int) iev.time.tv_usec,
652 iev.type, iev.code, iev.value);
653
Jeff Brown4e91a182011-04-07 11:38:09 -0700654#ifdef HAVE_POSIX_CLOCKS
655 // Use the time specified in the event instead of the current time
656 // so that downstream code can get more accurate estimates of
657 // event dispatch latency from the time the event is enqueued onto
658 // the evdev client buffer.
659 //
660 // The event's timestamp fortuitously uses the same monotonic clock
661 // time base as the rest of Android. The kernel event device driver
662 // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
663 // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
664 // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
665 // system call that also queries ktime_get_ts().
666 event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
667 + nsecs_t(iev.time.tv_usec) * 1000LL;
668 LOGV("event time %lld, now %lld", event->when, now);
669#else
Jeff Brownb7198742011-03-18 18:14:26 -0700670 event->when = now;
Jeff Brown4e91a182011-04-07 11:38:09 -0700671#endif
Jeff Brownb7198742011-03-18 18:14:26 -0700672 event->deviceId = deviceId;
673 event->type = iev.type;
674 event->scanCode = iev.code;
675 event->value = iev.value;
676 event->keyCode = AKEYCODE_UNKNOWN;
677 event->flags = 0;
678 if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
679 status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
680 &event->keyCode, &event->flags);
681 LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
682 iev.code, event->keyCode, event->flags, err);
683 }
684 event += 1;
685 }
686 capacity -= count;
687 if (capacity == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700688 // The result buffer is full. Reset the pending event index
689 // so we will try to read the device again on the next iteration.
690 mPendingEventIndex -= 1;
Jeff Brownb7198742011-03-18 18:14:26 -0700691 break;
692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700694 } else {
695 LOGW("Received unexpected epoll event 0x%08x for device %s.",
696 eventItem.events, device->identifier.name.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700699
Jeff Brown93fa9b32011-06-14 17:09:25 -0700700 // readNotify() will modify the list of devices so this must be done after
701 // processing all other events to ensure that we read all remaining events
702 // before closing the devices.
703 if (mPendingINotify && mPendingEventIndex >= mPendingEventCount) {
704 mPendingINotify = false;
705 readNotifyLocked();
706 deviceChanged = true;
Jeff Brown33bbfd22011-02-24 20:55:35 -0800707 }
708
Jeff Brown93fa9b32011-06-14 17:09:25 -0700709 // Report added or removed devices immediately.
710 if (deviceChanged) {
711 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
Jeff Browna9b84222010-10-14 02:23:43 -0700713
Jeff Brown93fa9b32011-06-14 17:09:25 -0700714 // Return now if we have collected any events or if we were explicitly awoken.
715 if (event != buffer || awoken) {
Jeff Brownb7198742011-03-18 18:14:26 -0700716 break;
717 }
718
Jeff Browncc2e7172010-08-17 16:48:25 -0700719 // Poll for events. Mind the wake lock dance!
Jeff Brown93fa9b32011-06-14 17:09:25 -0700720 // We hold a wake lock at all times except during epoll_wait(). This works due to some
Jeff Browncc2e7172010-08-17 16:48:25 -0700721 // subtle choreography. When a device driver has pending (unread) events, it acquires
722 // a kernel wake lock. However, once the last pending event has been read, the device
723 // driver will release the kernel wake lock. To prevent the system from going to sleep
724 // when this happens, the EventHub holds onto its own user wake lock while the client
725 // is processing events. Thus the system can only sleep if there are no events
726 // pending or currently being processed.
Jeff Brownaa3855d2011-03-17 01:34:19 -0700727 //
728 // The timeout is advisory only. If the device is asleep, it will not wake just to
729 // service the timeout.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700730 mPendingEventIndex = 0;
731
732 mLock.unlock(); // release lock before poll, must be before release_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700733 release_wake_lock(WAKE_LOCK_ID);
734
Jeff Brown93fa9b32011-06-14 17:09:25 -0700735 int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
Jeff Browncc2e7172010-08-17 16:48:25 -0700736
737 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700738 mLock.lock(); // reacquire lock after poll, must be after acquire_wake_lock
Jeff Browncc2e7172010-08-17 16:48:25 -0700739
Jeff Brownaa3855d2011-03-17 01:34:19 -0700740 if (pollResult == 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700741 // Timed out.
742 mPendingEventCount = 0;
743 break;
Jeff Brownaa3855d2011-03-17 01:34:19 -0700744 }
Jeff Brown93fa9b32011-06-14 17:09:25 -0700745
Jeff Brownaa3855d2011-03-17 01:34:19 -0700746 if (pollResult < 0) {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700747 // An error occurred.
748 mPendingEventCount = 0;
749
Jeff Brownb7198742011-03-18 18:14:26 -0700750 // Sleep after errors to avoid locking up the system.
751 // Hopefully the error is transient.
Jeff Browncc2e7172010-08-17 16:48:25 -0700752 if (errno != EINTR) {
Jeff Browna9b84222010-10-14 02:23:43 -0700753 LOGW("poll failed (errno=%d)\n", errno);
Jeff Browncc2e7172010-08-17 16:48:25 -0700754 usleep(100000);
755 }
Jeff Brownb7198742011-03-18 18:14:26 -0700756 } else {
Jeff Brown93fa9b32011-06-14 17:09:25 -0700757 // Some events occurred.
758 mPendingEventCount = size_t(pollResult);
759
Jeff Brownb7198742011-03-18 18:14:26 -0700760 // On an SMP system, it is possible for the framework to read input events
761 // faster than the kernel input device driver can produce a complete packet.
762 // Because poll() wakes up as soon as the first input event becomes available,
763 // the framework will often end up reading one event at a time until the
764 // packet is complete. Instead of one call to read() returning 71 events,
765 // it could take 71 calls to read() each returning 1 event.
766 //
767 // Sleep for a short period of time after waking up from the poll() to give
768 // the kernel time to finish writing the entire packet of input events.
769 if (mNumCpus > 1) {
770 usleep(250);
771 }
Jeff Browncc2e7172010-08-17 16:48:25 -0700772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
Jeff Brownb7198742011-03-18 18:14:26 -0700774
775 // All done, return the number of events we read.
776 return event - buffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777}
778
Jeff Brown93fa9b32011-06-14 17:09:25 -0700779void EventHub::wake() {
780 LOGV("wake() called");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781
Jeff Brown93fa9b32011-06-14 17:09:25 -0700782 ssize_t nWrite;
783 do {
784 nWrite = write(mWakeWritePipeFd, "W", 1);
785 } while (nWrite == -1 && errno == EINTR);
786
787 if (nWrite != 1 && errno != EAGAIN) {
788 LOGW("Could not write wake signal, errno=%d", errno);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700790}
Jeff Brown90655042010-12-02 13:50:46 -0800791
Jeff Brown93fa9b32011-06-14 17:09:25 -0700792void EventHub::scanDevicesLocked() {
793 status_t res = scanDirLocked(DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 if(res < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800795 LOGE("scan dir failed for %s\n", DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797}
798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799// ----------------------------------------------------------------------------
800
Jeff Brownfd035822010-06-30 16:10:35 -0700801static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
802 const uint8_t* end = array + endIndex;
803 array += startIndex;
804 while (array != end) {
805 if (*(array++) != 0) {
806 return true;
807 }
808 }
809 return false;
810}
811
812static const int32_t GAMEPAD_KEYCODES[] = {
813 AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
814 AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
815 AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
816 AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
817 AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
Jeff Browncb1404e2011-01-15 18:14:15 -0800818 AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
819 AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
820 AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
821 AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
822 AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
Jeff Brownfd035822010-06-30 16:10:35 -0700823};
824
Jeff Brown93fa9b32011-06-14 17:09:25 -0700825status_t EventHub::openDeviceLocked(const char *devicePath) {
Jeff Brown90655042010-12-02 13:50:46 -0800826 char buffer[80];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
Jeff Brown90655042010-12-02 13:50:46 -0800828 LOGV("Opening device: %s", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
Jeff Brown90655042010-12-02 13:50:46 -0800830 int fd = open(devicePath, O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 if(fd < 0) {
Jeff Brown90655042010-12-02 13:50:46 -0800832 LOGE("could not open %s, %s\n", devicePath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 return -1;
834 }
835
Jeff Brown90655042010-12-02 13:50:46 -0800836 InputDeviceIdentifier identifier;
837
838 // Get device name.
839 if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
840 //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
841 } else {
842 buffer[sizeof(buffer) - 1] = '\0';
843 identifier.name.setTo(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
Mike Lockwood15431a92009-07-17 00:10:10 -0400845
Jeff Brown90655042010-12-02 13:50:46 -0800846 // Check to see if the device is on our excluded list
Jeff Brown1a84fd12011-06-02 01:26:32 -0700847 for (size_t i = 0; i < mExcludedDevices.size(); i++) {
848 const String8& item = mExcludedDevices.itemAt(i);
849 if (identifier.name == item) {
850 LOGI("ignoring event id %s driver %s\n", devicePath, item.string());
Mike Lockwood15431a92009-07-17 00:10:10 -0400851 close(fd);
Mike Lockwood15431a92009-07-17 00:10:10 -0400852 return -1;
853 }
854 }
855
Jeff Brown90655042010-12-02 13:50:46 -0800856 // Get device driver version.
857 int driverVersion;
858 if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
859 LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
860 close(fd);
861 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 }
863
Jeff Brown90655042010-12-02 13:50:46 -0800864 // Get device identifier.
865 struct input_id inputId;
866 if(ioctl(fd, EVIOCGID, &inputId)) {
867 LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
868 close(fd);
869 return -1;
870 }
871 identifier.bus = inputId.bustype;
872 identifier.product = inputId.product;
873 identifier.vendor = inputId.vendor;
874 identifier.version = inputId.version;
875
876 // Get device physical location.
877 if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
878 //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
879 } else {
880 buffer[sizeof(buffer) - 1] = '\0';
881 identifier.location.setTo(buffer);
882 }
883
884 // Get device unique id.
885 if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
886 //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
887 } else {
888 buffer[sizeof(buffer) - 1] = '\0';
889 identifier.uniqueId.setTo(buffer);
890 }
891
892 // Make file descriptor non-blocking for use with poll().
Jeff Browncc2e7172010-08-17 16:48:25 -0700893 if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
894 LOGE("Error %d making device file descriptor non-blocking.", errno);
895 close(fd);
896 return -1;
897 }
898
Jeff Brown90655042010-12-02 13:50:46 -0800899 // Allocate device. (The device object takes ownership of the fd at this point.)
900 int32_t deviceId = mNextDeviceId++;
901 Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902
903#if 0
Jeff Brown90655042010-12-02 13:50:46 -0800904 LOGI("add device %d: %s\n", deviceId, devicePath);
905 LOGI(" bus: %04x\n"
906 " vendor %04x\n"
907 " product %04x\n"
908 " version %04x\n",
909 identifier.bus, identifier.vendor, identifier.product, identifier.version);
910 LOGI(" name: \"%s\"\n", identifier.name.string());
911 LOGI(" location: \"%s\"\n", identifier.location.string());
912 LOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
913 LOGI(" driver: v%d.%d.%d\n",
914 driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915#endif
916
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800917 // Load the configuration file for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700918 loadConfigurationLocked(device);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800919
Jeff Brownfd035822010-06-30 16:10:35 -0700920 // Figure out the kinds of events the device reports.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700921 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(device->keyBitmask)), device->keyBitmask);
922 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(device->absBitmask)), device->absBitmask);
923 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(device->relBitmask)), device->relBitmask);
924 ioctl(fd, EVIOCGBIT(EV_SW, sizeof(device->swBitmask)), device->swBitmask);
925 ioctl(fd, EVIOCGBIT(EV_LED, sizeof(device->ledBitmask)), device->ledBitmask);
926 ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
Jeff Browncc0c1592011-02-19 05:07:28 -0800927
Jeff Brown6f2fba42011-02-19 01:08:02 -0800928 // See if this is a keyboard. Ignore everything in the button range except for
929 // joystick and gamepad buttons which are handled like keyboards for the most part.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700930 bool haveKeyboardKeys = containsNonZeroByte(device->keyBitmask, 0, sizeof_bit_array(BTN_MISC))
931 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(KEY_OK),
Jeff Brown6f2fba42011-02-19 01:08:02 -0800932 sizeof_bit_array(KEY_MAX + 1));
Jeff Brown93fa9b32011-06-14 17:09:25 -0700933 bool haveGamepadButtons = containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_MISC),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800934 sizeof_bit_array(BTN_MOUSE))
Jeff Brown93fa9b32011-06-14 17:09:25 -0700935 || containsNonZeroByte(device->keyBitmask, sizeof_bit_array(BTN_JOYSTICK),
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800936 sizeof_bit_array(BTN_DIGI));
Jeff Brown6f2fba42011-02-19 01:08:02 -0800937 if (haveKeyboardKeys || haveGamepadButtons) {
938 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800940
Jeff Brown83c09682010-12-23 17:50:18 -0800941 // See if this is a cursor device such as a trackball or mouse.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700942 if (test_bit(BTN_MOUSE, device->keyBitmask)
943 && test_bit(REL_X, device->relBitmask)
944 && test_bit(REL_Y, device->relBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800945 device->classes |= INPUT_DEVICE_CLASS_CURSOR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
Jeff Brownfd035822010-06-30 16:10:35 -0700947
948 // See if this is a touch pad.
Jeff Brown6f2fba42011-02-19 01:08:02 -0800949 // Is this a new modern multi-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700950 if (test_bit(ABS_MT_POSITION_X, device->absBitmask)
951 && test_bit(ABS_MT_POSITION_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800952 // Some joysticks such as the PS3 controller report axes that conflict
953 // with the ABS_MT range. Try to confirm that the device really is
954 // a touch screen.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700955 if (test_bit(BTN_TOUCH, device->keyBitmask) || !haveGamepadButtons) {
Jeff Brown58a2da82011-01-25 16:02:22 -0800956 device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
Jeff Brownfd035822010-06-30 16:10:35 -0700957 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800958 // Is this an old style single-touch driver?
Jeff Brown93fa9b32011-06-14 17:09:25 -0700959 } else if (test_bit(BTN_TOUCH, device->keyBitmask)
960 && test_bit(ABS_X, device->absBitmask)
961 && test_bit(ABS_Y, device->absBitmask)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800962 device->classes |= INPUT_DEVICE_CLASS_TOUCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800965 // See if this device is a joystick.
966 // Ignore touchscreens because they use the same absolute axes for other purposes.
967 // Assumes that joysticks always have gamepad buttons in order to distinguish them
968 // from other devices such as accelerometers that also have absolute axes.
969 if (haveGamepadButtons
970 && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)
Jeff Brown93fa9b32011-06-14 17:09:25 -0700971 && containsNonZeroByte(device->absBitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800972 device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
973 }
974
Jeff Brown93fa9b32011-06-14 17:09:25 -0700975 // Check whether this device has switches.
976 for (int i = 0; i <= SW_MAX; i++) {
977 if (test_bit(i, device->swBitmask)) {
978 device->classes |= INPUT_DEVICE_CLASS_SWITCH;
979 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982
Jeff Brown93fa9b32011-06-14 17:09:25 -0700983 // Configure virtual keys.
Jeff Brown58a2da82011-01-25 16:02:22 -0800984 if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
Jeff Brown90655042010-12-02 13:50:46 -0800985 // Load the virtual keys for the touch screen, if any.
986 // We do this now so that we can make sure to load the keymap if necessary.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700987 status_t status = loadVirtualKeyMapLocked(device);
Jeff Brown90655042010-12-02 13:50:46 -0800988 if (!status) {
989 device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
Jeff Brown90655042010-12-02 13:50:46 -0800991 }
992
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800993 // Load the key map.
994 // We need to do this for joysticks too because the key layout may specify axes.
995 status_t keyMapStatus = NAME_NOT_FOUND;
996 if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
Jeff Brown90655042010-12-02 13:50:46 -0800997 // Load the keymap for the device.
Jeff Brown93fa9b32011-06-14 17:09:25 -0700998 keyMapStatus = loadKeyMapLocked(device);
Jeff Brown9e8e40c2011-03-03 03:39:29 -0800999 }
Jeff Brown90655042010-12-02 13:50:46 -08001000
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001001 // Configure the keyboard, gamepad or virtual keyboard.
1002 if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brown90655042010-12-02 13:50:46 -08001003 // Set system properties for the keyboard.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001004 setKeyboardPropertiesLocked(device, false);
Jeff Brown497a92c2010-09-12 17:55:08 -07001005
Jeff Brown90655042010-12-02 13:50:46 -08001006 // Register the keyboard as a built-in keyboard if it is eligible.
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001007 if (!keyMapStatus
Jeff Brown90655042010-12-02 13:50:46 -08001008 && mBuiltInKeyboardId == -1
1009 && isEligibleBuiltInKeyboard(device->identifier,
1010 device->configuration, &device->keyMap)) {
1011 mBuiltInKeyboardId = device->id;
Jeff Brown93fa9b32011-06-14 17:09:25 -07001012 setKeyboardPropertiesLocked(device, true);
Jeff Brown497a92c2010-09-12 17:55:08 -07001013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001015 // 'Q' key support = cheap test of whether this is an alpha-capable kbd
Jeff Brownf2f487182010-10-01 17:46:21 -07001016 if (hasKeycodeLocked(device, AKEYCODE_Q)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001017 device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001018 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001019
Jeff Brownfd035822010-06-30 16:10:35 -07001020 // See if this device has a DPAD.
Jeff Brownf2f487182010-10-01 17:46:21 -07001021 if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
1022 hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
1023 hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
1024 hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
1025 hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001026 device->classes |= INPUT_DEVICE_CLASS_DPAD;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001027 }
Jeff Brown497a92c2010-09-12 17:55:08 -07001028
Jeff Brownfd035822010-06-30 16:10:35 -07001029 // See if this device has a gamepad.
Kenny Root1d79a9d2010-10-21 15:46:03 -07001030 for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
Jeff Brownf2f487182010-10-01 17:46:21 -07001031 if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
Jeff Brownfd035822010-06-30 16:10:35 -07001032 device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
1033 break;
1034 }
1035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037
Sean McNeilaeb00c42010-06-23 16:00:37 +07001038 // If the device isn't recognized as something we handle, don't monitor it.
1039 if (device->classes == 0) {
Jeff Brown90655042010-12-02 13:50:46 -08001040 LOGV("Dropping device: id=%d, path='%s', name='%s'",
1041 deviceId, devicePath, device->identifier.name.string());
Sean McNeilaeb00c42010-06-23 16:00:37 +07001042 delete device;
1043 return -1;
1044 }
1045
Jeff Brown56194eb2011-03-02 19:23:13 -08001046 // Determine whether the device is external or internal.
Jeff Brown93fa9b32011-06-14 17:09:25 -07001047 if (isExternalDeviceLocked(device)) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001048 device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1049 }
1050
Jeff Brown93fa9b32011-06-14 17:09:25 -07001051 // Register with epoll.
1052 struct epoll_event eventItem;
1053 memset(&eventItem, 0, sizeof(eventItem));
1054 eventItem.events = EPOLLIN;
1055 eventItem.data.u32 = deviceId;
1056 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
1057 LOGE("Could not add device fd to epoll instance. errno=%d", errno);
1058 delete device;
1059 return -1;
1060 }
1061
Jeff Brown90655042010-12-02 13:50:46 -08001062 LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1063 "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
1064 deviceId, fd, devicePath, device->identifier.name.string(),
1065 device->classes,
1066 device->configurationFile.string(),
1067 device->keyMap.keyLayoutFile.string(),
1068 device->keyMap.keyCharacterMapFile.string(),
1069 toString(mBuiltInKeyboardId == deviceId));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001070
Jeff Brown93fa9b32011-06-14 17:09:25 -07001071 mDevices.add(deviceId, device);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 device->next = mOpeningDevices;
1074 mOpeningDevices = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 return 0;
1076}
1077
Jeff Brown93fa9b32011-06-14 17:09:25 -07001078void EventHub::loadConfigurationLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001079 device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1080 device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001081 if (device->configurationFile.isEmpty()) {
Jeff Brown90655042010-12-02 13:50:46 -08001082 LOGD("No input device configuration file found for device '%s'.",
1083 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001084 } else {
1085 status_t status = PropertyMap::load(device->configurationFile,
1086 &device->configuration);
1087 if (status) {
Jeff Brown90655042010-12-02 13:50:46 -08001088 LOGE("Error loading input device configuration file for device '%s'. "
1089 "Using default configuration.",
1090 device->identifier.name.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001091 }
1092 }
1093}
1094
Jeff Brown93fa9b32011-06-14 17:09:25 -07001095status_t EventHub::loadVirtualKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001096 // The virtual key map is supplied by the kernel as a system board property file.
1097 String8 path;
1098 path.append("/sys/board_properties/virtualkeys.");
1099 path.append(device->identifier.name);
1100 if (access(path.string(), R_OK)) {
1101 return NAME_NOT_FOUND;
1102 }
1103 return VirtualKeyMap::load(path, &device->virtualKeyMap);
Jeff Brown497a92c2010-09-12 17:55:08 -07001104}
1105
Jeff Brown93fa9b32011-06-14 17:09:25 -07001106status_t EventHub::loadKeyMapLocked(Device* device) {
Jeff Brown90655042010-12-02 13:50:46 -08001107 return device->keyMap.load(device->identifier, device->configuration);
Jeff Brown497a92c2010-09-12 17:55:08 -07001108}
1109
Jeff Brown93fa9b32011-06-14 17:09:25 -07001110void EventHub::setKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001111 int32_t id = builtInKeyboard ? 0 : device->id;
1112 android::setKeyboardProperties(id, device->identifier,
1113 device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1114}
1115
Jeff Brown93fa9b32011-06-14 17:09:25 -07001116void EventHub::clearKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
Jeff Brown90655042010-12-02 13:50:46 -08001117 int32_t id = builtInKeyboard ? 0 : device->id;
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001118 android::clearKeyboardProperties(id);
Jeff Brown497a92c2010-09-12 17:55:08 -07001119}
1120
Jeff Brown93fa9b32011-06-14 17:09:25 -07001121bool EventHub::isExternalDeviceLocked(Device* device) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001122 if (device->configuration) {
1123 bool value;
1124 if (device->configuration->tryGetProperty(String8("device.internal"), value)
1125 && value) {
1126 return false;
1127 }
1128 }
1129 return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1130}
1131
Jeff Brown90655042010-12-02 13:50:46 -08001132bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1133 if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001134 return false;
1135 }
1136
1137 Vector<int32_t> scanCodes;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001138 device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001139 const size_t N = scanCodes.size();
1140 for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1141 int32_t sc = scanCodes.itemAt(i);
1142 if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1143 return true;
1144 }
1145 }
1146
1147 return false;
1148}
1149
Jeff Brown93fa9b32011-06-14 17:09:25 -07001150status_t EventHub::closeDeviceByPathLocked(const char *devicePath) {
1151 Device* device = getDeviceByPathLocked(devicePath);
1152 if (device) {
1153 closeDeviceLocked(device);
1154 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001156 LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 return -1;
1158}
1159
Jeff Brown93fa9b32011-06-14 17:09:25 -07001160void EventHub::closeAllDevicesLocked() {
1161 while (mDevices.size() > 0) {
1162 closeDeviceLocked(mDevices.valueAt(mDevices.size() - 1));
1163 }
1164}
1165
1166void EventHub::closeDeviceLocked(Device* device) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001167 LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1168 device->path.string(), device->identifier.name.string(), device->id,
1169 device->fd, device->classes);
1170
Jeff Brown33bbfd22011-02-24 20:55:35 -08001171 if (device->id == mBuiltInKeyboardId) {
1172 LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
1173 device->path.string(), mBuiltInKeyboardId);
1174 mBuiltInKeyboardId = -1;
Jeff Brown93fa9b32011-06-14 17:09:25 -07001175 clearKeyboardPropertiesLocked(device, true);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001176 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001177 clearKeyboardPropertiesLocked(device, false);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001178
Jeff Brown93fa9b32011-06-14 17:09:25 -07001179 if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
1180 LOGW("Could not remove device fd from epoll instance. errno=%d", errno);
1181 }
1182
1183 mDevices.removeItem(device->id);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001184 device->close();
1185
Jeff Brown8e9d4432011-03-12 19:46:59 -08001186 // Unlink for opening devices list if it is present.
1187 Device* pred = NULL;
1188 bool found = false;
1189 for (Device* entry = mOpeningDevices; entry != NULL; ) {
1190 if (entry == device) {
1191 found = true;
1192 break;
1193 }
1194 pred = entry;
1195 entry = entry->next;
1196 }
1197 if (found) {
1198 // Unlink the device from the opening devices list then delete it.
1199 // We don't need to tell the client that the device was closed because
1200 // it does not even know it was opened in the first place.
1201 LOGI("Device %s was immediately closed after opening.", device->path.string());
1202 if (pred) {
1203 pred->next = device->next;
1204 } else {
1205 mOpeningDevices = device->next;
1206 }
1207 delete device;
1208 } else {
1209 // Link into closing devices list.
1210 // The device will be deleted later after we have informed the client.
1211 device->next = mClosingDevices;
1212 mClosingDevices = device;
1213 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001214}
1215
Jeff Brown93fa9b32011-06-14 17:09:25 -07001216status_t EventHub::readNotifyLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 int res;
1218 char devname[PATH_MAX];
1219 char *filename;
1220 char event_buf[512];
1221 int event_size;
1222 int event_pos = 0;
1223 struct inotify_event *event;
1224
Jeff Brown93fa9b32011-06-14 17:09:25 -07001225 LOGV("EventHub::readNotify nfd: %d\n", mINotifyFd);
1226 res = read(mINotifyFd, event_buf, sizeof(event_buf));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 if(res < (int)sizeof(*event)) {
1228 if(errno == EINTR)
1229 return 0;
1230 LOGW("could not get event, %s\n", strerror(errno));
Jeff Brown93fa9b32011-06-14 17:09:25 -07001231 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 //printf("got %d bytes of event information\n", res);
1234
Jeff Brown90655042010-12-02 13:50:46 -08001235 strcpy(devname, DEVICE_PATH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 filename = devname + strlen(devname);
1237 *filename++ = '/';
1238
1239 while(res >= (int)sizeof(*event)) {
1240 event = (struct inotify_event *)(event_buf + event_pos);
1241 //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1242 if(event->len) {
1243 strcpy(filename, event->name);
1244 if(event->mask & IN_CREATE) {
Jeff Brown93fa9b32011-06-14 17:09:25 -07001245 openDeviceLocked(devname);
1246 } else {
1247 closeDeviceByPathLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249 }
1250 event_size = sizeof(*event) + event->len;
1251 res -= event_size;
1252 event_pos += event_size;
1253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 return 0;
1255}
1256
Jeff Brown93fa9b32011-06-14 17:09:25 -07001257status_t EventHub::scanDirLocked(const char *dirname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258{
1259 char devname[PATH_MAX];
1260 char *filename;
1261 DIR *dir;
1262 struct dirent *de;
1263 dir = opendir(dirname);
1264 if(dir == NULL)
1265 return -1;
1266 strcpy(devname, dirname);
1267 filename = devname + strlen(devname);
1268 *filename++ = '/';
1269 while((de = readdir(dir))) {
1270 if(de->d_name[0] == '.' &&
1271 (de->d_name[1] == '\0' ||
1272 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1273 continue;
1274 strcpy(filename, de->d_name);
Jeff Brown93fa9b32011-06-14 17:09:25 -07001275 openDeviceLocked(devname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277 closedir(dir);
1278 return 0;
1279}
1280
Jeff Brown93fa9b32011-06-14 17:09:25 -07001281void EventHub::requestReopenDevices() {
1282 LOGV("requestReopenDevices() called");
1283
1284 AutoMutex _l(mLock);
1285 mNeedToReopenDevices = true;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001286}
1287
Jeff Brownf2f487182010-10-01 17:46:21 -07001288void EventHub::dump(String8& dump) {
1289 dump.append("Event Hub State:\n");
1290
1291 { // acquire lock
1292 AutoMutex _l(mLock);
1293
Jeff Brown90655042010-12-02 13:50:46 -08001294 dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
Jeff Brownf2f487182010-10-01 17:46:21 -07001295
1296 dump.append(INDENT "Devices:\n");
1297
Jeff Brown93fa9b32011-06-14 17:09:25 -07001298 for (size_t i = 0; i < mDevices.size(); i++) {
1299 const Device* device = mDevices.valueAt(i);
1300 if (mBuiltInKeyboardId == device->id) {
1301 dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1302 device->id, device->identifier.name.string());
1303 } else {
1304 dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1305 device->identifier.name.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001306 }
Jeff Brown93fa9b32011-06-14 17:09:25 -07001307 dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1308 dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
1309 dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1310 dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1311 dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1312 "product=0x%04x, version=0x%04x\n",
1313 device->identifier.bus, device->identifier.vendor,
1314 device->identifier.product, device->identifier.version);
1315 dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1316 device->keyMap.keyLayoutFile.string());
1317 dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1318 device->keyMap.keyCharacterMapFile.string());
1319 dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1320 device->configurationFile.string());
Jeff Brownf2f487182010-10-01 17:46:21 -07001321 }
1322 } // release lock
1323}
1324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325}; // namespace android