EventHub.cpp revision 1a84fd1fb7a51f3fe4f8865e1cdd09f3490f696c
1/*
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
17//
18// Handle events, like key input and vsync.
19//
20// The goal is to provide an optimized solution for Linux, not an
21// implementation that works well across all platforms.  We expect
22// events to arrive on file descriptors, so that we can use a select()
23// select() call to sleep.
24//
25// We can't select() on anything but network sockets in Windows, so we
26// provide an alternative implementation of waitEvent for that platform.
27//
28#define LOG_TAG "EventHub"
29
30//#define LOG_NDEBUG 0
31
32#include "EventHub.h"
33
34#include <hardware_legacy/power.h>
35
36#include <cutils/atomic.h>
37#include <cutils/properties.h>
38#include <utils/Log.h>
39#include <utils/Timers.h>
40#include <utils/threads.h>
41#include <utils/Errors.h>
42
43#include <stdlib.h>
44#include <stdio.h>
45#include <unistd.h>
46#include <fcntl.h>
47#include <memory.h>
48#include <errno.h>
49#include <assert.h>
50
51#include <ui/KeyLayoutMap.h>
52#include <ui/KeyCharacterMap.h>
53#include <ui/VirtualKeyMap.h>
54
55#include <string.h>
56#include <stdint.h>
57#include <dirent.h>
58#ifdef HAVE_INOTIFY
59# include <sys/inotify.h>
60#endif
61#ifdef HAVE_ANDROID_OS
62# include <sys/limits.h>        /* not part of Linux */
63#endif
64#include <sys/poll.h>
65#include <sys/ioctl.h>
66
67/* this macro is used to tell if "bit" is set in "array"
68 * it selects a byte from the array, and does a boolean AND
69 * operation with a byte that only has the relevant bit set.
70 * eg. to check for the 12th bit, we do (array[1] & 1<<4)
71 */
72#define test_bit(bit, array)    (array[bit/8] & (1<<(bit%8)))
73
74/* this macro computes the number of bytes needed to represent a bit array of the specified size */
75#define sizeof_bit_array(bits)  ((bits + 7) / 8)
76
77// Fd at index 0 is always reserved for inotify
78#define FIRST_ACTUAL_DEVICE_INDEX 1
79
80#define INDENT "  "
81#define INDENT2 "    "
82#define INDENT3 "      "
83
84namespace android {
85
86static const char *WAKE_LOCK_ID = "KeyEvents";
87static const char *DEVICE_PATH = "/dev/input";
88
89/* return the larger integer */
90static inline int max(int v1, int v2)
91{
92    return (v1 > v2) ? v1 : v2;
93}
94
95static inline const char* toString(bool value) {
96    return value ? "true" : "false";
97}
98
99// --- EventHub::Device ---
100
101EventHub::Device::Device(int fd, int32_t id, const String8& path,
102        const InputDeviceIdentifier& identifier) :
103        next(NULL),
104        fd(fd), id(id), path(path), identifier(identifier),
105        classes(0), keyBitmask(NULL), relBitmask(NULL),
106        configuration(NULL), virtualKeyMap(NULL) {
107}
108
109EventHub::Device::~Device() {
110    close();
111    delete[] keyBitmask;
112    delete[] relBitmask;
113    delete configuration;
114    delete virtualKeyMap;
115}
116
117void EventHub::Device::close() {
118    if (fd >= 0) {
119        ::close(fd);
120        fd = -1;
121    }
122}
123
124
125// --- EventHub ---
126
127EventHub::EventHub(void) :
128        mError(NO_INIT), mBuiltInKeyboardId(-1), mNextDeviceId(1),
129        mOpeningDevices(0), mClosingDevices(0),
130        mOpened(false), mNeedToSendFinishedDeviceScan(false),
131        mNeedToReopenDevices(0), mNeedToScanDevices(false),
132        mInputFdIndex(1) {
133    acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
134
135    memset(mSwitches, 0, sizeof(mSwitches));
136    mNumCpus = sysconf(_SC_NPROCESSORS_ONLN);
137}
138
139EventHub::~EventHub(void) {
140    release_wake_lock(WAKE_LOCK_ID);
141    // we should free stuff here...
142}
143
144status_t EventHub::errorCheck() const {
145    return mError;
146}
147
148String8 EventHub::getDeviceName(int32_t deviceId) const {
149    AutoMutex _l(mLock);
150    Device* device = getDeviceLocked(deviceId);
151    if (device == NULL) return String8();
152    return device->identifier.name;
153}
154
155uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
156    AutoMutex _l(mLock);
157    Device* device = getDeviceLocked(deviceId);
158    if (device == NULL) return 0;
159    return device->classes;
160}
161
162void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
163    AutoMutex _l(mLock);
164    Device* device = getDeviceLocked(deviceId);
165    if (device && device->configuration) {
166        *outConfiguration = *device->configuration;
167    } else {
168        outConfiguration->clear();
169    }
170}
171
172status_t EventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
173        RawAbsoluteAxisInfo* outAxisInfo) const {
174    outAxisInfo->clear();
175
176    AutoMutex _l(mLock);
177    Device* device = getDeviceLocked(deviceId);
178    if (device == NULL) return -1;
179
180    struct input_absinfo info;
181
182    if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
183        LOGW("Error reading absolute controller %d for device %s fd %d\n",
184             axis, device->identifier.name.string(), device->fd);
185        return -errno;
186    }
187
188    if (info.minimum != info.maximum) {
189        outAxisInfo->valid = true;
190        outAxisInfo->minValue = info.minimum;
191        outAxisInfo->maxValue = info.maximum;
192        outAxisInfo->flat = info.flat;
193        outAxisInfo->fuzz = info.fuzz;
194    }
195    return OK;
196}
197
198bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
199    if (axis >= 0 && axis <= REL_MAX) {
200        AutoMutex _l(mLock);
201
202        Device* device = getDeviceLocked(deviceId);
203        if (device && device->relBitmask) {
204            return test_bit(axis, device->relBitmask);
205        }
206    }
207    return false;
208}
209
210int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
211    if (scanCode >= 0 && scanCode <= KEY_MAX) {
212        AutoMutex _l(mLock);
213
214        Device* device = getDeviceLocked(deviceId);
215        if (device != NULL) {
216            return getScanCodeStateLocked(device, scanCode);
217        }
218    }
219    return AKEY_STATE_UNKNOWN;
220}
221
222int32_t EventHub::getScanCodeStateLocked(Device* device, int32_t scanCode) const {
223    uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
224    memset(key_bitmask, 0, sizeof(key_bitmask));
225    if (ioctl(device->fd,
226               EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
227        return test_bit(scanCode, key_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
228    }
229    return AKEY_STATE_UNKNOWN;
230}
231
232int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
233    AutoMutex _l(mLock);
234
235    Device* device = getDeviceLocked(deviceId);
236    if (device != NULL) {
237        return getKeyCodeStateLocked(device, keyCode);
238    }
239    return AKEY_STATE_UNKNOWN;
240}
241
242int32_t EventHub::getKeyCodeStateLocked(Device* device, int32_t keyCode) const {
243    if (!device->keyMap.haveKeyLayout()) {
244        return AKEY_STATE_UNKNOWN;
245    }
246
247    Vector<int32_t> scanCodes;
248    device->keyMap.keyLayoutMap->findScanCodesForKey(keyCode, &scanCodes);
249
250    uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
251    memset(key_bitmask, 0, sizeof(key_bitmask));
252    if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
253        #if 0
254        for (size_t i=0; i<=KEY_MAX; i++) {
255            LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
256        }
257        #endif
258        const size_t N = scanCodes.size();
259        for (size_t i=0; i<N && i<=KEY_MAX; i++) {
260            int32_t sc = scanCodes.itemAt(i);
261            //LOGI("Code %d: down=%d", sc, test_bit(sc, key_bitmask));
262            if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, key_bitmask)) {
263                return AKEY_STATE_DOWN;
264            }
265        }
266        return AKEY_STATE_UP;
267    }
268    return AKEY_STATE_UNKNOWN;
269}
270
271int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
272    if (sw >= 0 && sw <= SW_MAX) {
273        AutoMutex _l(mLock);
274
275        Device* device = getDeviceLocked(deviceId);
276        if (device != NULL) {
277            return getSwitchStateLocked(device, sw);
278        }
279    }
280    return AKEY_STATE_UNKNOWN;
281}
282
283int32_t EventHub::getSwitchStateLocked(Device* device, int32_t sw) const {
284    uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
285    memset(sw_bitmask, 0, sizeof(sw_bitmask));
286    if (ioctl(device->fd,
287               EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
288        return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP;
289    }
290    return AKEY_STATE_UNKNOWN;
291}
292
293bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes,
294        const int32_t* keyCodes, uint8_t* outFlags) const {
295    AutoMutex _l(mLock);
296
297    Device* device = getDeviceLocked(deviceId);
298    if (device != NULL) {
299        return markSupportedKeyCodesLocked(device, numCodes, keyCodes, outFlags);
300    }
301    return false;
302}
303
304bool EventHub::markSupportedKeyCodesLocked(Device* device, size_t numCodes,
305        const int32_t* keyCodes, uint8_t* outFlags) const {
306    if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
307        return false;
308    }
309
310    Vector<int32_t> scanCodes;
311    for (size_t codeIndex = 0; codeIndex < numCodes; codeIndex++) {
312        scanCodes.clear();
313
314        status_t err = device->keyMap.keyLayoutMap->findScanCodesForKey(
315                keyCodes[codeIndex], &scanCodes);
316        if (! err) {
317            // check the possible scan codes identified by the layout map against the
318            // map of codes actually emitted by the driver
319            for (size_t sc = 0; sc < scanCodes.size(); sc++) {
320                if (test_bit(scanCodes[sc], device->keyBitmask)) {
321                    outFlags[codeIndex] = 1;
322                    break;
323                }
324            }
325        }
326    }
327    return true;
328}
329
330status_t EventHub::mapKey(int32_t deviceId, int scancode,
331        int32_t* outKeycode, uint32_t* outFlags) const
332{
333    AutoMutex _l(mLock);
334    Device* device = getDeviceLocked(deviceId);
335
336    if (device && device->keyMap.haveKeyLayout()) {
337        status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
338        if (err == NO_ERROR) {
339            return NO_ERROR;
340        }
341    }
342
343    if (mBuiltInKeyboardId != -1) {
344        device = getDeviceLocked(mBuiltInKeyboardId);
345
346        if (device && device->keyMap.haveKeyLayout()) {
347            status_t err = device->keyMap.keyLayoutMap->mapKey(scancode, outKeycode, outFlags);
348            if (err == NO_ERROR) {
349                return NO_ERROR;
350            }
351        }
352    }
353
354    *outKeycode = 0;
355    *outFlags = 0;
356    return NAME_NOT_FOUND;
357}
358
359status_t EventHub::mapAxis(int32_t deviceId, int scancode, AxisInfo* outAxisInfo) const
360{
361    AutoMutex _l(mLock);
362    Device* device = getDeviceLocked(deviceId);
363
364    if (device && device->keyMap.haveKeyLayout()) {
365        status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
366        if (err == NO_ERROR) {
367            return NO_ERROR;
368        }
369    }
370
371    if (mBuiltInKeyboardId != -1) {
372        device = getDeviceLocked(mBuiltInKeyboardId);
373
374        if (device && device->keyMap.haveKeyLayout()) {
375            status_t err = device->keyMap.keyLayoutMap->mapAxis(scancode, outAxisInfo);
376            if (err == NO_ERROR) {
377                return NO_ERROR;
378            }
379        }
380    }
381
382    return NAME_NOT_FOUND;
383}
384
385void EventHub::setExcludedDevices(const Vector<String8>& devices) {
386    AutoMutex _l(mLock);
387
388    mExcludedDevices = devices;
389}
390
391bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
392    AutoMutex _l(mLock);
393    Device* device = getDeviceLocked(deviceId);
394    if (device) {
395        uint8_t bitmask[sizeof_bit_array(LED_MAX + 1)];
396        memset(bitmask, 0, sizeof(bitmask));
397        if (ioctl(device->fd, EVIOCGBIT(EV_LED, sizeof(bitmask)), bitmask) >= 0) {
398            if (test_bit(led, bitmask)) {
399                return true;
400            }
401        }
402    }
403    return false;
404}
405
406void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
407    AutoMutex _l(mLock);
408    Device* device = getDeviceLocked(deviceId);
409    if (device) {
410        struct input_event ev;
411        ev.time.tv_sec = 0;
412        ev.time.tv_usec = 0;
413        ev.type = EV_LED;
414        ev.code = led;
415        ev.value = on ? 1 : 0;
416
417        ssize_t nWrite;
418        do {
419            nWrite = write(device->fd, &ev, sizeof(struct input_event));
420        } while (nWrite == -1 && errno == EINTR);
421    }
422}
423
424void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
425        Vector<VirtualKeyDefinition>& outVirtualKeys) const {
426    outVirtualKeys.clear();
427
428    AutoMutex _l(mLock);
429    Device* device = getDeviceLocked(deviceId);
430    if (device && device->virtualKeyMap) {
431        outVirtualKeys.appendVector(device->virtualKeyMap->getVirtualKeys());
432    }
433}
434
435EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
436    if (deviceId == 0) {
437        deviceId = mBuiltInKeyboardId;
438    }
439
440    size_t numDevices = mDevices.size();
441    for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < numDevices; i++) {
442        Device* device = mDevices[i];
443        if (device->id == deviceId) {
444            return device;
445        }
446    }
447    return NULL;
448}
449
450size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
451    // Note that we only allow one caller to getEvents(), so don't need
452    // to do locking here...  only when adding/removing devices.
453    assert(bufferSize >= 1);
454
455    if (!mOpened) {
456        android_atomic_acquire_store(0, &mNeedToReopenDevices);
457
458        mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
459        mOpened = true;
460        mNeedToScanDevices = true;
461    }
462
463    struct input_event readBuffer[bufferSize];
464
465    RawEvent* event = buffer;
466    size_t capacity = bufferSize;
467    for (;;) {
468        nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
469
470        // Reopen input devices if needed.
471        if (android_atomic_acquire_load(&mNeedToReopenDevices)) {
472            android_atomic_acquire_store(0, &mNeedToReopenDevices);
473
474            LOGI("Reopening all input devices due to a configuration change.");
475
476            AutoMutex _l(mLock);
477            while (mDevices.size() > 1) {
478                closeDeviceAtIndexLocked(mDevices.size() - 1);
479            }
480            mNeedToScanDevices = true;
481            break; // return to the caller before we actually rescan
482        }
483
484        // Report any devices that had last been added/removed.
485        while (mClosingDevices) {
486            Device* device = mClosingDevices;
487            LOGV("Reporting device closed: id=%d, name=%s\n",
488                 device->id, device->path.string());
489            mClosingDevices = device->next;
490            event->when = now;
491            event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
492            event->type = DEVICE_REMOVED;
493            event += 1;
494            delete device;
495            mNeedToSendFinishedDeviceScan = true;
496            if (--capacity == 0) {
497                break;
498            }
499        }
500
501        if (mNeedToScanDevices) {
502            mNeedToScanDevices = false;
503            scanDevices();
504            mNeedToSendFinishedDeviceScan = true;
505        }
506
507        while (mOpeningDevices != NULL) {
508            Device* device = mOpeningDevices;
509            LOGV("Reporting device opened: id=%d, name=%s\n",
510                 device->id, device->path.string());
511            mOpeningDevices = device->next;
512            event->when = now;
513            event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
514            event->type = DEVICE_ADDED;
515            event += 1;
516            mNeedToSendFinishedDeviceScan = true;
517            if (--capacity == 0) {
518                break;
519            }
520        }
521
522        if (mNeedToSendFinishedDeviceScan) {
523            mNeedToSendFinishedDeviceScan = false;
524            event->when = now;
525            event->type = FINISHED_DEVICE_SCAN;
526            event += 1;
527            if (--capacity == 0) {
528                break;
529            }
530        }
531
532        // Grab the next input event.
533        // mInputFdIndex is initially 1 because index 0 is used for inotify.
534        bool deviceWasRemoved = false;
535        while (mInputFdIndex < mFds.size()) {
536            const struct pollfd& pfd = mFds[mInputFdIndex];
537            if (pfd.revents & POLLIN) {
538                int32_t readSize = read(pfd.fd, readBuffer, sizeof(struct input_event) * capacity);
539                if (readSize < 0) {
540                    if (errno == ENODEV) {
541                        deviceWasRemoved = true;
542                        break;
543                    }
544                    if (errno != EAGAIN && errno != EINTR) {
545                        LOGW("could not get event (errno=%d)", errno);
546                    }
547                } else if ((readSize % sizeof(struct input_event)) != 0) {
548                    LOGE("could not get event (wrong size: %d)", readSize);
549                } else if (readSize == 0) { // eof
550                    deviceWasRemoved = true;
551                    break;
552                } else {
553                    const Device* device = mDevices[mInputFdIndex];
554                    int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
555
556                    size_t count = size_t(readSize) / sizeof(struct input_event);
557                    for (size_t i = 0; i < count; i++) {
558                        const struct input_event& iev = readBuffer[i];
559                        LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
560                                device->path.string(),
561                                (int) iev.time.tv_sec, (int) iev.time.tv_usec,
562                                iev.type, iev.code, iev.value);
563
564#ifdef HAVE_POSIX_CLOCKS
565                        // Use the time specified in the event instead of the current time
566                        // so that downstream code can get more accurate estimates of
567                        // event dispatch latency from the time the event is enqueued onto
568                        // the evdev client buffer.
569                        //
570                        // The event's timestamp fortuitously uses the same monotonic clock
571                        // time base as the rest of Android.  The kernel event device driver
572                        // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
573                        // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
574                        // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
575                        // system call that also queries ktime_get_ts().
576                        event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
577                                + nsecs_t(iev.time.tv_usec) * 1000LL;
578                        LOGV("event time %lld, now %lld", event->when, now);
579#else
580                        event->when = now;
581#endif
582                        event->deviceId = deviceId;
583                        event->type = iev.type;
584                        event->scanCode = iev.code;
585                        event->value = iev.value;
586                        event->keyCode = AKEYCODE_UNKNOWN;
587                        event->flags = 0;
588                        if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
589                            status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
590                                        &event->keyCode, &event->flags);
591                            LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
592                                    iev.code, event->keyCode, event->flags, err);
593                        }
594                        event += 1;
595                    }
596                    capacity -= count;
597                    if (capacity == 0) {
598                        break;
599                    }
600                }
601            }
602            mInputFdIndex += 1;
603        }
604
605        // Handle the case where a device has been removed but INotify has not yet noticed.
606        if (deviceWasRemoved) {
607            AutoMutex _l(mLock);
608            closeDeviceAtIndexLocked(mInputFdIndex);
609            continue; // report added or removed devices immediately
610        }
611
612#if HAVE_INOTIFY
613        // readNotify() will modify mFDs and mFDCount, so this must be done after
614        // processing all other events.
615        if(mFds[0].revents & POLLIN) {
616            readNotify(mFds[0].fd);
617            mFds.editItemAt(0).revents = 0;
618            mInputFdIndex = mFds.size();
619            continue; // report added or removed devices immediately
620        }
621#endif
622
623        // Return now if we have collected any events, otherwise poll.
624        if (event != buffer) {
625            break;
626        }
627
628        // Poll for events.  Mind the wake lock dance!
629        // We hold a wake lock at all times except during poll().  This works due to some
630        // subtle choreography.  When a device driver has pending (unread) events, it acquires
631        // a kernel wake lock.  However, once the last pending event has been read, the device
632        // driver will release the kernel wake lock.  To prevent the system from going to sleep
633        // when this happens, the EventHub holds onto its own user wake lock while the client
634        // is processing events.  Thus the system can only sleep if there are no events
635        // pending or currently being processed.
636        //
637        // The timeout is advisory only.  If the device is asleep, it will not wake just to
638        // service the timeout.
639        release_wake_lock(WAKE_LOCK_ID);
640
641        int pollResult = poll(mFds.editArray(), mFds.size(), timeoutMillis);
642
643        acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
644
645        if (pollResult == 0) {
646            break; // timed out
647        }
648        if (pollResult < 0) {
649            // Sleep after errors to avoid locking up the system.
650            // Hopefully the error is transient.
651            if (errno != EINTR) {
652                LOGW("poll failed (errno=%d)\n", errno);
653                usleep(100000);
654            }
655        } else {
656            // On an SMP system, it is possible for the framework to read input events
657            // faster than the kernel input device driver can produce a complete packet.
658            // Because poll() wakes up as soon as the first input event becomes available,
659            // the framework will often end up reading one event at a time until the
660            // packet is complete.  Instead of one call to read() returning 71 events,
661            // it could take 71 calls to read() each returning 1 event.
662            //
663            // Sleep for a short period of time after waking up from the poll() to give
664            // the kernel time to finish writing the entire packet of input events.
665            if (mNumCpus > 1) {
666                usleep(250);
667            }
668        }
669
670        // Prepare to process all of the FDs we just polled.
671        mInputFdIndex = 1;
672    }
673
674    // All done, return the number of events we read.
675    return event - buffer;
676}
677
678/*
679 * Open the platform-specific input device.
680 */
681bool EventHub::openPlatformInput(void) {
682    /*
683     * Open platform-specific input device(s).
684     */
685    int res, fd;
686
687#ifdef HAVE_INOTIFY
688    fd = inotify_init();
689    res = inotify_add_watch(fd, DEVICE_PATH, IN_DELETE | IN_CREATE);
690    if(res < 0) {
691        LOGE("could not add watch for %s, %s\n", DEVICE_PATH, strerror(errno));
692    }
693#else
694    /*
695     * The code in EventHub::getEvent assumes that mFDs[0] is an inotify fd.
696     * We allocate space for it and set it to something invalid.
697     */
698    fd = -1;
699#endif
700
701    // Reserve fd index 0 for inotify.
702    struct pollfd pollfd;
703    pollfd.fd = fd;
704    pollfd.events = POLLIN;
705    pollfd.revents = 0;
706    mFds.push(pollfd);
707    mDevices.push(NULL);
708    return true;
709}
710
711void EventHub::scanDevices() {
712    int res = scanDir(DEVICE_PATH);
713    if(res < 0) {
714        LOGE("scan dir failed for %s\n", DEVICE_PATH);
715    }
716}
717
718// ----------------------------------------------------------------------------
719
720static bool containsNonZeroByte(const uint8_t* array, uint32_t startIndex, uint32_t endIndex) {
721    const uint8_t* end = array + endIndex;
722    array += startIndex;
723    while (array != end) {
724        if (*(array++) != 0) {
725            return true;
726        }
727    }
728    return false;
729}
730
731static const int32_t GAMEPAD_KEYCODES[] = {
732        AKEYCODE_BUTTON_A, AKEYCODE_BUTTON_B, AKEYCODE_BUTTON_C,
733        AKEYCODE_BUTTON_X, AKEYCODE_BUTTON_Y, AKEYCODE_BUTTON_Z,
734        AKEYCODE_BUTTON_L1, AKEYCODE_BUTTON_R1,
735        AKEYCODE_BUTTON_L2, AKEYCODE_BUTTON_R2,
736        AKEYCODE_BUTTON_THUMBL, AKEYCODE_BUTTON_THUMBR,
737        AKEYCODE_BUTTON_START, AKEYCODE_BUTTON_SELECT, AKEYCODE_BUTTON_MODE,
738        AKEYCODE_BUTTON_1, AKEYCODE_BUTTON_2, AKEYCODE_BUTTON_3, AKEYCODE_BUTTON_4,
739        AKEYCODE_BUTTON_5, AKEYCODE_BUTTON_6, AKEYCODE_BUTTON_7, AKEYCODE_BUTTON_8,
740        AKEYCODE_BUTTON_9, AKEYCODE_BUTTON_10, AKEYCODE_BUTTON_11, AKEYCODE_BUTTON_12,
741        AKEYCODE_BUTTON_13, AKEYCODE_BUTTON_14, AKEYCODE_BUTTON_15, AKEYCODE_BUTTON_16,
742};
743
744int EventHub::openDevice(const char *devicePath) {
745    char buffer[80];
746
747    LOGV("Opening device: %s", devicePath);
748
749    AutoMutex _l(mLock);
750
751    int fd = open(devicePath, O_RDWR);
752    if(fd < 0) {
753        LOGE("could not open %s, %s\n", devicePath, strerror(errno));
754        return -1;
755    }
756
757    InputDeviceIdentifier identifier;
758
759    // Get device name.
760    if(ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
761        //fprintf(stderr, "could not get device name for %s, %s\n", devicePath, strerror(errno));
762    } else {
763        buffer[sizeof(buffer) - 1] = '\0';
764        identifier.name.setTo(buffer);
765    }
766
767    // Check to see if the device is on our excluded list
768    for (size_t i = 0; i < mExcludedDevices.size(); i++) {
769        const String8& item = mExcludedDevices.itemAt(i);
770        if (identifier.name == item) {
771            LOGI("ignoring event id %s driver %s\n", devicePath, item.string());
772            close(fd);
773            return -1;
774        }
775    }
776
777    // Get device driver version.
778    int driverVersion;
779    if(ioctl(fd, EVIOCGVERSION, &driverVersion)) {
780        LOGE("could not get driver version for %s, %s\n", devicePath, strerror(errno));
781        close(fd);
782        return -1;
783    }
784
785    // Get device identifier.
786    struct input_id inputId;
787    if(ioctl(fd, EVIOCGID, &inputId)) {
788        LOGE("could not get device input id for %s, %s\n", devicePath, strerror(errno));
789        close(fd);
790        return -1;
791    }
792    identifier.bus = inputId.bustype;
793    identifier.product = inputId.product;
794    identifier.vendor = inputId.vendor;
795    identifier.version = inputId.version;
796
797    // Get device physical location.
798    if(ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
799        //fprintf(stderr, "could not get location for %s, %s\n", devicePath, strerror(errno));
800    } else {
801        buffer[sizeof(buffer) - 1] = '\0';
802        identifier.location.setTo(buffer);
803    }
804
805    // Get device unique id.
806    if(ioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &buffer) < 1) {
807        //fprintf(stderr, "could not get idstring for %s, %s\n", devicePath, strerror(errno));
808    } else {
809        buffer[sizeof(buffer) - 1] = '\0';
810        identifier.uniqueId.setTo(buffer);
811    }
812
813    // Make file descriptor non-blocking for use with poll().
814    if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
815        LOGE("Error %d making device file descriptor non-blocking.", errno);
816        close(fd);
817        return -1;
818    }
819
820    // Allocate device.  (The device object takes ownership of the fd at this point.)
821    int32_t deviceId = mNextDeviceId++;
822    Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
823
824#if 0
825    LOGI("add device %d: %s\n", deviceId, devicePath);
826    LOGI("  bus:       %04x\n"
827         "  vendor     %04x\n"
828         "  product    %04x\n"
829         "  version    %04x\n",
830        identifier.bus, identifier.vendor, identifier.product, identifier.version);
831    LOGI("  name:      \"%s\"\n", identifier.name.string());
832    LOGI("  location:  \"%s\"\n", identifier.location.string());
833    LOGI("  unique id: \"%s\"\n", identifier.uniqueId.string());
834    LOGI("  driver:    v%d.%d.%d\n",
835        driverVersion >> 16, (driverVersion >> 8) & 0xff, driverVersion & 0xff);
836#endif
837
838    // Load the configuration file for the device.
839    loadConfiguration(device);
840
841    // Figure out the kinds of events the device reports.
842    uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];
843    memset(key_bitmask, 0, sizeof(key_bitmask));
844    ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
845
846    uint8_t abs_bitmask[sizeof_bit_array(ABS_MAX + 1)];
847    memset(abs_bitmask, 0, sizeof(abs_bitmask));
848    ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
849
850    uint8_t rel_bitmask[sizeof_bit_array(REL_MAX + 1)];
851    memset(rel_bitmask, 0, sizeof(rel_bitmask));
852    ioctl(fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask);
853
854    uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)];
855    memset(sw_bitmask, 0, sizeof(sw_bitmask));
856    ioctl(fd, EVIOCGBIT(EV_SW, sizeof(sw_bitmask)), sw_bitmask);
857
858    device->keyBitmask = new uint8_t[sizeof(key_bitmask)];
859    if (device->keyBitmask != NULL) {
860        memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));
861    } else {
862        delete device;
863        LOGE("out of memory allocating key bitmask");
864        return -1;
865    }
866
867    device->relBitmask = new uint8_t[sizeof(rel_bitmask)];
868    if (device->relBitmask != NULL) {
869        memcpy(device->relBitmask, rel_bitmask, sizeof(rel_bitmask));
870    } else {
871        delete device;
872        LOGE("out of memory allocating rel bitmask");
873        return -1;
874    }
875
876    // See if this is a keyboard.  Ignore everything in the button range except for
877    // joystick and gamepad buttons which are handled like keyboards for the most part.
878    bool haveKeyboardKeys = containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))
879            || containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),
880                    sizeof_bit_array(KEY_MAX + 1));
881    bool haveGamepadButtons = containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_MISC),
882                    sizeof_bit_array(BTN_MOUSE))
883            || containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_JOYSTICK),
884                    sizeof_bit_array(BTN_DIGI));
885    if (haveKeyboardKeys || haveGamepadButtons) {
886        device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
887    }
888
889    // See if this is a cursor device such as a trackball or mouse.
890    if (test_bit(BTN_MOUSE, key_bitmask)
891            && test_bit(REL_X, rel_bitmask)
892            && test_bit(REL_Y, rel_bitmask)) {
893        device->classes |= INPUT_DEVICE_CLASS_CURSOR;
894    }
895
896    // See if this is a touch pad.
897    // Is this a new modern multi-touch driver?
898    if (test_bit(ABS_MT_POSITION_X, abs_bitmask)
899            && test_bit(ABS_MT_POSITION_Y, abs_bitmask)) {
900        // Some joysticks such as the PS3 controller report axes that conflict
901        // with the ABS_MT range.  Try to confirm that the device really is
902        // a touch screen.
903        if (test_bit(BTN_TOUCH, key_bitmask) || !haveGamepadButtons) {
904            device->classes |= INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_TOUCH_MT;
905        }
906    // Is this an old style single-touch driver?
907    } else if (test_bit(BTN_TOUCH, key_bitmask)
908            && test_bit(ABS_X, abs_bitmask)
909            && test_bit(ABS_Y, abs_bitmask)) {
910        device->classes |= INPUT_DEVICE_CLASS_TOUCH;
911    }
912
913    // See if this device is a joystick.
914    // Ignore touchscreens because they use the same absolute axes for other purposes.
915    // Assumes that joysticks always have gamepad buttons in order to distinguish them
916    // from other devices such as accelerometers that also have absolute axes.
917    if (haveGamepadButtons
918            && !(device->classes & INPUT_DEVICE_CLASS_TOUCH)
919            && containsNonZeroByte(abs_bitmask, 0, sizeof_bit_array(ABS_MAX + 1))) {
920        device->classes |= INPUT_DEVICE_CLASS_JOYSTICK;
921    }
922
923    // figure out the switches this device reports
924    bool haveSwitches = false;
925    for (int i=0; i<EV_SW; i++) {
926        //LOGI("Device %d sw %d: has=%d", device->id, i, test_bit(i, sw_bitmask));
927        if (test_bit(i, sw_bitmask)) {
928            haveSwitches = true;
929            if (mSwitches[i] == 0) {
930                mSwitches[i] = device->id;
931            }
932        }
933    }
934    if (haveSwitches) {
935        device->classes |= INPUT_DEVICE_CLASS_SWITCH;
936    }
937
938    if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) {
939        // Load the virtual keys for the touch screen, if any.
940        // We do this now so that we can make sure to load the keymap if necessary.
941        status_t status = loadVirtualKeyMap(device);
942        if (!status) {
943            device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;
944        }
945    }
946
947    // Load the key map.
948    // We need to do this for joysticks too because the key layout may specify axes.
949    status_t keyMapStatus = NAME_NOT_FOUND;
950    if (device->classes & (INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_JOYSTICK)) {
951        // Load the keymap for the device.
952        keyMapStatus = loadKeyMap(device);
953    }
954
955    // Configure the keyboard, gamepad or virtual keyboard.
956    if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
957        // Set system properties for the keyboard.
958        setKeyboardProperties(device, false);
959
960        // Register the keyboard as a built-in keyboard if it is eligible.
961        if (!keyMapStatus
962                && mBuiltInKeyboardId == -1
963                && isEligibleBuiltInKeyboard(device->identifier,
964                        device->configuration, &device->keyMap)) {
965            mBuiltInKeyboardId = device->id;
966            setKeyboardProperties(device, true);
967        }
968
969        // 'Q' key support = cheap test of whether this is an alpha-capable kbd
970        if (hasKeycodeLocked(device, AKEYCODE_Q)) {
971            device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;
972        }
973
974        // See if this device has a DPAD.
975        if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&
976                hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&
977                hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&
978                hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&
979                hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {
980            device->classes |= INPUT_DEVICE_CLASS_DPAD;
981        }
982
983        // See if this device has a gamepad.
984        for (size_t i = 0; i < sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]); i++) {
985            if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {
986                device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;
987                break;
988            }
989        }
990    }
991
992    // If the device isn't recognized as something we handle, don't monitor it.
993    if (device->classes == 0) {
994        LOGV("Dropping device: id=%d, path='%s', name='%s'",
995                deviceId, devicePath, device->identifier.name.string());
996        delete device;
997        return -1;
998    }
999
1000    // Determine whether the device is external or internal.
1001    if (isExternalDevice(device)) {
1002        device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
1003    }
1004
1005    LOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
1006            "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s",
1007         deviceId, fd, devicePath, device->identifier.name.string(),
1008         device->classes,
1009         device->configurationFile.string(),
1010         device->keyMap.keyLayoutFile.string(),
1011         device->keyMap.keyCharacterMapFile.string(),
1012         toString(mBuiltInKeyboardId == deviceId));
1013
1014    struct pollfd pollfd;
1015    pollfd.fd = fd;
1016    pollfd.events = POLLIN;
1017    pollfd.revents = 0;
1018    mFds.push(pollfd);
1019    mDevices.push(device);
1020
1021    device->next = mOpeningDevices;
1022    mOpeningDevices = device;
1023    return 0;
1024}
1025
1026void EventHub::loadConfiguration(Device* device) {
1027    device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
1028            device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
1029    if (device->configurationFile.isEmpty()) {
1030        LOGD("No input device configuration file found for device '%s'.",
1031                device->identifier.name.string());
1032    } else {
1033        status_t status = PropertyMap::load(device->configurationFile,
1034                &device->configuration);
1035        if (status) {
1036            LOGE("Error loading input device configuration file for device '%s'.  "
1037                    "Using default configuration.",
1038                    device->identifier.name.string());
1039        }
1040    }
1041}
1042
1043status_t EventHub::loadVirtualKeyMap(Device* device) {
1044    // The virtual key map is supplied by the kernel as a system board property file.
1045    String8 path;
1046    path.append("/sys/board_properties/virtualkeys.");
1047    path.append(device->identifier.name);
1048    if (access(path.string(), R_OK)) {
1049        return NAME_NOT_FOUND;
1050    }
1051    return VirtualKeyMap::load(path, &device->virtualKeyMap);
1052}
1053
1054status_t EventHub::loadKeyMap(Device* device) {
1055    return device->keyMap.load(device->identifier, device->configuration);
1056}
1057
1058void EventHub::setKeyboardProperties(Device* device, bool builtInKeyboard) {
1059    int32_t id = builtInKeyboard ? 0 : device->id;
1060    android::setKeyboardProperties(id, device->identifier,
1061            device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
1062}
1063
1064void EventHub::clearKeyboardProperties(Device* device, bool builtInKeyboard) {
1065    int32_t id = builtInKeyboard ? 0 : device->id;
1066    android::clearKeyboardProperties(id);
1067}
1068
1069bool EventHub::isExternalDevice(Device* device) {
1070    if (device->configuration) {
1071        bool value;
1072        if (device->configuration->tryGetProperty(String8("device.internal"), value)
1073                && value) {
1074            return false;
1075        }
1076    }
1077    return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
1078}
1079
1080bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
1081    if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
1082        return false;
1083    }
1084
1085    Vector<int32_t> scanCodes;
1086    device->keyMap.keyLayoutMap->findScanCodesForKey(keycode, &scanCodes);
1087    const size_t N = scanCodes.size();
1088    for (size_t i=0; i<N && i<=KEY_MAX; i++) {
1089        int32_t sc = scanCodes.itemAt(i);
1090        if (sc >= 0 && sc <= KEY_MAX && test_bit(sc, device->keyBitmask)) {
1091            return true;
1092        }
1093    }
1094
1095    return false;
1096}
1097
1098int EventHub::closeDevice(const char *devicePath) {
1099    AutoMutex _l(mLock);
1100
1101    for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1102        Device* device = mDevices[i];
1103        if (device->path == devicePath) {
1104            return closeDeviceAtIndexLocked(i);
1105        }
1106    }
1107    LOGV("Remove device: %s not found, device may already have been removed.", devicePath);
1108    return -1;
1109}
1110
1111int EventHub::closeDeviceAtIndexLocked(int index) {
1112    Device* device = mDevices[index];
1113    LOGI("Removed device: path=%s name=%s id=%d fd=%d classes=0x%x\n",
1114         device->path.string(), device->identifier.name.string(), device->id,
1115         device->fd, device->classes);
1116
1117    for (int j=0; j<EV_SW; j++) {
1118        if (mSwitches[j] == device->id) {
1119            mSwitches[j] = 0;
1120        }
1121    }
1122
1123    if (device->id == mBuiltInKeyboardId) {
1124        LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
1125                device->path.string(), mBuiltInKeyboardId);
1126        mBuiltInKeyboardId = -1;
1127        clearKeyboardProperties(device, true);
1128    }
1129    clearKeyboardProperties(device, false);
1130
1131    mFds.removeAt(index);
1132    mDevices.removeAt(index);
1133    device->close();
1134
1135    // Unlink for opening devices list if it is present.
1136    Device* pred = NULL;
1137    bool found = false;
1138    for (Device* entry = mOpeningDevices; entry != NULL; ) {
1139        if (entry == device) {
1140            found = true;
1141            break;
1142        }
1143        pred = entry;
1144        entry = entry->next;
1145    }
1146    if (found) {
1147        // Unlink the device from the opening devices list then delete it.
1148        // We don't need to tell the client that the device was closed because
1149        // it does not even know it was opened in the first place.
1150        LOGI("Device %s was immediately closed after opening.", device->path.string());
1151        if (pred) {
1152            pred->next = device->next;
1153        } else {
1154            mOpeningDevices = device->next;
1155        }
1156        delete device;
1157    } else {
1158        // Link into closing devices list.
1159        // The device will be deleted later after we have informed the client.
1160        device->next = mClosingDevices;
1161        mClosingDevices = device;
1162    }
1163    return 0;
1164}
1165
1166int EventHub::readNotify(int nfd) {
1167#ifdef HAVE_INOTIFY
1168    int res;
1169    char devname[PATH_MAX];
1170    char *filename;
1171    char event_buf[512];
1172    int event_size;
1173    int event_pos = 0;
1174    struct inotify_event *event;
1175
1176    LOGV("EventHub::readNotify nfd: %d\n", nfd);
1177    res = read(nfd, event_buf, sizeof(event_buf));
1178    if(res < (int)sizeof(*event)) {
1179        if(errno == EINTR)
1180            return 0;
1181        LOGW("could not get event, %s\n", strerror(errno));
1182        return 1;
1183    }
1184    //printf("got %d bytes of event information\n", res);
1185
1186    strcpy(devname, DEVICE_PATH);
1187    filename = devname + strlen(devname);
1188    *filename++ = '/';
1189
1190    while(res >= (int)sizeof(*event)) {
1191        event = (struct inotify_event *)(event_buf + event_pos);
1192        //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
1193        if(event->len) {
1194            strcpy(filename, event->name);
1195            if(event->mask & IN_CREATE) {
1196                openDevice(devname);
1197            }
1198            else {
1199                closeDevice(devname);
1200            }
1201        }
1202        event_size = sizeof(*event) + event->len;
1203        res -= event_size;
1204        event_pos += event_size;
1205    }
1206#endif
1207    return 0;
1208}
1209
1210int EventHub::scanDir(const char *dirname)
1211{
1212    char devname[PATH_MAX];
1213    char *filename;
1214    DIR *dir;
1215    struct dirent *de;
1216    dir = opendir(dirname);
1217    if(dir == NULL)
1218        return -1;
1219    strcpy(devname, dirname);
1220    filename = devname + strlen(devname);
1221    *filename++ = '/';
1222    while((de = readdir(dir))) {
1223        if(de->d_name[0] == '.' &&
1224           (de->d_name[1] == '\0' ||
1225            (de->d_name[1] == '.' && de->d_name[2] == '\0')))
1226            continue;
1227        strcpy(filename, de->d_name);
1228        openDevice(devname);
1229    }
1230    closedir(dir);
1231    return 0;
1232}
1233
1234void EventHub::reopenDevices() {
1235    android_atomic_release_store(1, &mNeedToReopenDevices);
1236}
1237
1238void EventHub::dump(String8& dump) {
1239    dump.append("Event Hub State:\n");
1240
1241    { // acquire lock
1242        AutoMutex _l(mLock);
1243
1244        dump.appendFormat(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
1245
1246        dump.append(INDENT "Devices:\n");
1247
1248        for (size_t i = FIRST_ACTUAL_DEVICE_INDEX; i < mDevices.size(); i++) {
1249            const Device* device = mDevices[i];
1250            if (device) {
1251                if (mBuiltInKeyboardId == device->id) {
1252                    dump.appendFormat(INDENT2 "%d: %s (aka device 0 - built-in keyboard)\n",
1253                            device->id, device->identifier.name.string());
1254                } else {
1255                    dump.appendFormat(INDENT2 "%d: %s\n", device->id,
1256                            device->identifier.name.string());
1257                }
1258                dump.appendFormat(INDENT3 "Classes: 0x%08x\n", device->classes);
1259                dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
1260                dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
1261                dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
1262                dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
1263                        "product=0x%04x, version=0x%04x\n",
1264                        device->identifier.bus, device->identifier.vendor,
1265                        device->identifier.product, device->identifier.version);
1266                dump.appendFormat(INDENT3 "KeyLayoutFile: %s\n",
1267                        device->keyMap.keyLayoutFile.string());
1268                dump.appendFormat(INDENT3 "KeyCharacterMapFile: %s\n",
1269                        device->keyMap.keyCharacterMapFile.string());
1270                dump.appendFormat(INDENT3 "ConfigurationFile: %s\n",
1271                        device->configurationFile.string());
1272            }
1273        }
1274    } // release lock
1275}
1276
1277}; // namespace android
1278