在前文 Linux/Android——Input系统之frameworks层InputManagerService (六) 这里介绍了Android层input服务的启动,其中启动了一个读取来自底层event事件的线程.
而在Linux/Android——input系统之 kernel层 与 frameworks层交互 (五) 有分析到是通过一个event%d的设备文件交互的,也就是说android层是通过读取event%d来获取event的,这个工作就是InputReader
撰写不易,转载需注明出处:http://blog.csdn.net/jscese/article/details/42739197
getEvents:
这个是运行在inputread线程里面的,上篇有介绍到,字面意思就是获取事件的,实现在/frameworks/base/services/input/EventHub.cpp中,函数很长,列出几个重要的:
- size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
-
- ...
-
- struct input_event readBuffer[bufferSize];
-
- RawEvent* event = buffer;
-
- ...
-
- if (mNeedToScanDevices) {
- mNeedToScanDevices = false;
- scanDevicesLocked();
- mNeedToSendFinishedDeviceScan = true;
- }
-
- while (mOpeningDevices != NULL) {
- Device* device = mOpeningDevices;
- ALOGV("Reporting device opened: id=%d, name=%s\n",
- device->id, device->path.string());
- mOpeningDevices = device->next;
- event->when = now;
- event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
- event->type = DEVICE_ADDED;
- event += 1;
- mNeedToSendFinishedDeviceScan = true;
- if (--capacity == 0) {
- break;
- }
- }
-
- ...
-
- while (mPendingEventIndex < mPendingEventCount) {
- const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
-
- Device* device = mDevices.valueAt(deviceIndex) ;
- if (eventItem.events & EPOLLIN) {
- int32_t readSize = read(device->fd, readBuffer,
- sizeof(struct input_event) * capacity);
-
- ...
-
- for (size_t i = 0; i < count; i++) {
- const struct input_event& iev = readBuffer[i];
-
- event->deviceId = deviceId;
- event->type = iev.type;
- event->code = iev.code;
- event->value = iev.value;
- event += 1;
-
- }
-
- ...
- int pollResult = epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis);
- ...
- }
-
-
- return event - buffer;
-
- }
scanDevicesLocked:
这个往下就是打开一个input 设备文件,配置并抽象为一个android层这边的input device:
- void EventHub::scanDevicesLocked() {
- status_t res = scanDirLocked(DEVICE_PATH);
- if(res < 0) {
- ALOGE("scan dir failed for %s\n", DEVICE_PATH);
- }
- if (mDevices.indexOfKey(VIRTUAL_KEYBOARD_ID) < 0) {
- createVirtualKeyboardLocked();
- }
- }
继续往下看会依次读取目录下的文件,并调用进openDeviceLocked打开,这个函数也比较长,关注几个地方先:
- status_t EventHub::openDeviceLocked(const charchar *devicePath) {
- char buffer[80];
-
- ALOGV("Opening device: %s", devicePath);
-
- int fd = open(devicePath, O_RDWR | O_CLOEXEC);
- if(fd < 0) {
- ALOGE("could not open %s, %s\n", devicePath, strerror(errno));
- return -1;
- }
-
- ...
-
-
- int32_t deviceId = mNextDeviceId++;
- Device* device = new Device(fd, deviceId, String8(devicePath), identifier);
-
- ...
-
-
- loadConfigurationLocked(device);
-
- ...
-
-
- struct epoll_event eventItem;
- memset(&eventItem, 0, sizeof(eventItem));
- eventItem.events = EPOLLIN;
- eventItem.data.u32 = deviceId;
- if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
- ALOGE("Could not add device fd to epoll instance. errno=%d", errno);
- delete device;
- return -1;
- }
-
- ...
-
- addDeviceLocked(device);
-
- }
这个里面有很多原始打印,想深入理解的可以放开调试看看,这里不多做介绍,可以看下 加载配置那个函数loadConfigurationLocked:
- void EventHub::loadConfigurationLocked(Device* device) {
- device->configurationFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
- device->identifier, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
- if (device->configurationFile.isEmpty()) {
- ALOGD("No input device configuration file found for device '%s'.",
- device->identifier.name.string());
- } else {
- status_t status = PropertyMap::load(device->configurationFile,
- &device->configuration);
- if (status) {
- ALOGE("Error loading input device configuration file for device '%s'. "
- "Using default configuration.",
- device->identifier.name.string());
- }
- }
- }
接下来看怎么找对应config文件的,这里调用到了/frameworks/base/libs/androidfw/InputDevice.cpp中的:
- String8 getInputDeviceConfigurationFilePathByDeviceIdentifier(
- const InputDeviceIdentifier& deviceIdentifier,
- InputDeviceConfigurationFileType type) {
- if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
- if (deviceIdentifier.version != 0) {
-
- String8 versionPath(getInputDeviceConfigurationFilePathByName(
- String8::format("Vendor_%04x_Product_%04x_Version_%04x",
- deviceIdentifier.vendor, deviceIdentifier.product,
- deviceIdentifier.version),
- type));
- if (!versionPath.isEmpty()) {
- return versionPath;
- }
- }
-
-
- String8 productPath(getInputDeviceConfigurationFilePathByName(
- String8::format("Vendor_%04x_Product_%04x",
- deviceIdentifier.vendor, deviceIdentifier.product),
- type));
- if (!productPath.isEmpty()) {
- return productPath;
- }
- }
-
-
- return getInputDeviceConfigurationFilePathByName(deviceIdentifier.name, type);
- }
所以我们对一些输出设备需要把它的配置文件编译进系统,而且命名一般都是
Vendor_%04x_Product_%04x.XXX
类型,这里就明白啦!
到这里getEvents应该差不多了,细节部分就需要另行细读代码了,当上面read到事件数组返回之后接下来就是初步处理了!
processEventsLocked:
上面getEvents返回了mEventBuffer之后,做初步的处理:
- void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
-
-
- for (const RawEvent* rawEvent = rawEvents; count;) { 遍历获取到的event数组
- int32_t type = rawEvent->type;
- size_t batchSize = 1;
-
-
-
- if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
- int32_t deviceId = rawEvent->deviceId;
- while (batchSize < count) {
-
- if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
- || rawEvent[batchSize].deviceId != deviceId ) {
- break;
- }
- batchSize += 1;
- }
-
- ALOGW("BatchSize: %d Count: %d", batchSize, count);
-
- processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
- } else {
- switch (rawEvent->type) {
- case EventHubInterface::DEVICE_ADDED:
- addDeviceLocked(rawEvent->when, rawEvent->deviceId);
- break;
- case EventHubInterface::DEVICE_REMOVED:
- removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
- break;
- case EventHubInterface::FINISHED_DEVICE_SCAN:
- handleConfigurationChangedLocked(rawEvent->when);
- break;
- default:
- ALOG_ASSERT(false);
- break;
- }
- }
- count -= batchSize;
- rawEvent += batchSize;
- }
- }
传进来的参数为 前面getEvents一定时间内获取到的event数组以及个数,原型定义在/frameworks/base/services/input/InputReader.h中:
-
- static const int EVENT_BUFFER_SIZE = 256;
- RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
-
-
-
- struct RawEvent {
- nsecs_t when;
- int32_t deviceId;
- int32_t type;
- int32_t code;
- int32_t value;
- };
这里可以看到 processEventsLocked 只是对get到的event做一个初步的分发处理,先看添加的事件类型.
可以看到在InputReader里面又来了一次addDeviceLocked ,这个要更最上面getEvents中往下打开设备时addDeviceLocked 区分开来,不要给绕晕了哟~
- void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
- ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
- if (deviceIndex >= 0) {
- ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
- return;
- }
-
- InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
- uint32_t classes = mEventHub->getDeviceClasses(deviceId);
-
- ALOGW("jscese display in addDeviceLocked classes == 0x%x \n",classes);
- InputDevice* device = createDeviceLocked(deviceId, identifier, classes);
- device->configure(when, &mConfig, 0);
- device->reset(when);
-
- if (device->isIgnored()) {
- ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
- identifier.name.string());
- } else {
- ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
- identifier.name.string(), device->getSources());
- }
-
- mDevices.add(deviceId, device);
- bumpGenerationLocked();
- }
看处理平常事件时接下来的处理:
- void InputReader::processEventsForDeviceLocked(int32_t deviceId,
- const RawEvent* rawEvents, size_t count) {
- ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
- if (deviceIndex < 0) {
- ALOGW("Discarding event for unknown deviceId %d.", deviceId);
- return;
- }
-
- InputDevice* device = mDevices.valueAt(deviceIndex);
- if (device->isIgnored()) {
-
- return;
- }
-
- device->process(rawEvents, count);
- }
继续看InputDevice的process:
- void InputDevice::process(const RawEvent* rawEvents, size_t count) {
-
-
-
-
-
- size_t numMappers = mMappers.size();
- for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
- #if DEBUG_RAW_EVENTS
- ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld",
- rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
- rawEvent->when);
- #endif
-
- if (mDropUntilNextSync) {
- if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
- mDropUntilNextSync = false;
- #if DEBUG_RAW_EVENTS
- ALOGD("Recovered from input event buffer overrun.");
- #endif
- } else {
- #if DEBUG_RAW_EVENTS
- ALOGD("Dropped input event while waiting for next input sync.");
- #endif
- }
- } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
- ALOGI("Detected input event buffer overrun for device %s.", getName().string());
- mDropUntilNextSync = true;
- reset(rawEvent->when);
- } else {
- for (size_t i = 0; i < numMappers; i++) {
- InputMapper* mapper = mMappers[i];
- mapper->process(rawEvent);
- }
- }
- }
- }
多次提到classes 设备类型,看下定义,在EventHub.h中:
-
-
-
- enum {
-
- INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
-
-
- INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
-
-
- INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
-
-
- INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
-
-
- INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
-
-
- INPUT_DEVICE_CLASS_DPAD = 0x00000020,
-
-
- INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
-
-
- INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
-
-
- INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
-
-
- INPUT_DEVICE_CLASS_VIBRATOR = 0x00000200,
-
-
- INPUT_DEVICE_CLASS_VIRTUAL = 0x40000000,
-
-
- INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
- };
那么接下来需要处理的就是 定义的InputReader中的各个InputMap类了。
总结:
这里牵扯到了EventHub InputReader Device InputDevice InputMap
把关系理一下:
EventHub 中 会创建多个Device ,这个是根据/dev/input下面的设备文件 一一对应的,也就是kernel中注册的input设备.
InputReader 中会create多个 InputDevice ,这个是由Device中的参数为基础创建的,所以与EventHub中的Device 一一对应.
InputDevice 在创建的时候会根据 classes 来添加需要的InputMap,每一个不同的InputMap 在InputReader中定义,有很多种处理方式.
大体就是这么回事,这里关于InputReader部分就先分析到这里,再往下InputMap中的事件分发处理,后续分析~