1. TouchScreen功能在Android4.0下不工作
原來在Android2.3.5下能正常工作的TouchScreen功能,移植到Android 4.0就不能正常工作了。憑直覺,Android4.0肯定有鬼。真是不看不知道,一看嚇一跳。在Android 4.0中,Event Input地位提高了,你看看,在Adroid2.3.5中,它在frameworks/base/libs/ui之下,在Android4.0中,它在frameworks/base/services/input之下,看到?jīng)]有,它有了自己的地位,就像在Kernel中一樣,有自己門戶了。
再看看代碼,變化也太大了,當(dāng)然TouchScreen不能工作,首先自然會(huì)看接口部分代碼。首先看它是如何打開設(shè)備的,查看函數(shù)EventHub::openDeviceLocked,看看其代碼,大部分還是很熟悉的,但仔細(xì)一看多了一個(gè)下面的東東:
ioctl(fd, EVIOCGPROP(sizeof(device->propBitmask)), device->propBitmask);
由于升級(jí)到Android4.0時(shí),Kernel還是2.6.35,并沒有進(jìn)行升級(jí)。既然需要EVIOCGPROP,就就看看evdev.c中的ioctl函數(shù)是否支持此功能。一看不支持,再看看Kernel3.0.8<這個(gè)Kernel版本與Android4.0是一伙的>,我的乖乖,它已經(jīng)支持了此功能,詳見evdev.c中函數(shù)evdev_do_ioctl,這個(gè)寫得2.6.35中的友好多了,分別處理:固定長(zhǎng)度命令、單個(gè)可變長(zhǎng)度命令和多個(gè)可變長(zhǎng)度命令。
對(duì)于為什么我的TouchScreen在Android4.0不工作,答案顯而易見,我用的Kernel版本不對(duì),當(dāng)然移植到Android4.0對(duì)應(yīng)的Kernel(Kernel3.0.8)時(shí),TouchScreen驅(qū)動(dòng)本身也需要修改,因?yàn)閕nput_dev變化也比較大,比如增加了propbit字段,以供處理上面的ioctl時(shí)使用。
2. Android 4.0如何管理各種驅(qū)動(dòng)設(shè)備
正是由于遇到上面的問題,才促使自己對(duì)Event Input進(jìn)行深入了解。因?yàn)轵唑腰c(diǎn)水不是小弟的性格。
這個(gè)年代干啥都有什么經(jīng)理,小弟之類的。比如去飯店吃飯,吃到小強(qiáng)了,總是會(huì)大吼一聲,經(jīng)理,過來看看,然后談打折或賠償?shù)膯栴}。可見經(jīng)理是不可缺少的,要不然我們找誰來維權(quán)啊!
前面談到的EventHub,這個(gè)一看就是一個(gè)做實(shí)事的,肯定不是領(lǐng)導(dǎo),哪它的領(lǐng)導(dǎo)是誰呢? 哪我們就從以下幾方面來分析此問題:
1)每個(gè)功能模塊是怎么產(chǎn)生的?
2)讀取設(shè)備輸入流程?
3)事件分發(fā)流程?
3. 各個(gè)功能模塊是怎么產(chǎn)生的?
先介紹一下每個(gè)模塊的工作職責(zé):EventHub, InputReader, InputManager...
3.1 模塊功能
3.1.1 EventHub
它是系統(tǒng)中所有事件的中央處理站。它管理所有系統(tǒng)中可以識(shí)別的輸入設(shè)備的輸入事件,此外,當(dāng)設(shè)備增加或刪除時(shí),EventHub將產(chǎn)生相應(yīng)的輸入事件給系統(tǒng)。
EventHub通過getEvents函數(shù),給系統(tǒng)提供一個(gè)輸入事件流。它也支持查詢輸入設(shè)備當(dāng)前的狀態(tài)(如哪些鍵當(dāng)前被按下)。而且EventHub還跟蹤每個(gè)輸入調(diào)入的能力,比如輸入設(shè)備的類別,輸入設(shè)備支持哪些按鍵。
3.1.2 InputReader
InputReader從EventHub中讀取原始事件數(shù)據(jù)(RawEvent),并由各個(gè)InputMapper處理之后輸入對(duì)應(yīng)的input listener.
InputReader擁有一個(gè)InputMapper集合。它做的大部分工作在InputReader線程中完成,但是InputReader可以接受任意線程的查詢。為了可管理性,InputReader使用一個(gè)簡(jiǎn)單的Mutex來保護(hù)它的狀態(tài)。
InputReader擁有一個(gè)EventHub對(duì)象,但這個(gè)對(duì)象不是它創(chuàng)建的,而是在創(chuàng)建InputReader時(shí)作為參數(shù)傳入的。
3.1.3 InputDispatcher
InputDispatcher負(fù)責(zé)把事件分發(fā)給輸入目標(biāo),其中的一些功能(如識(shí)別輸入目標(biāo))由獨(dú)立的policy對(duì)象控制。
3.1.4 InputManager
InputManager是系統(tǒng)事件處理的核心,它雖然不做具體的事,但管理工作還是要做的,比如接受我們客戶的投訴和索賠要求,或者老板的出所筒。
InputManager使用兩個(gè)線程:
1)InputReaderThread叫做"InputReader"線程,它負(fù)責(zé)讀取并預(yù)處理RawEvent,applies policy并且把消息送入DispatcherThead管理的隊(duì)列中。
2)InputDispatcherThread叫做"InputDispatcher"線程,它在隊(duì)列上等待新的輸入事件,并且異步地把這些事件分發(fā)給應(yīng)用程序。
InputReaderThread類與InputDispatcherThread類不共享內(nèi)部狀態(tài),所有的通信都是單向的,從InputReaderThread到InputDispatcherThread。兩個(gè)類可以通過InputDispatchPolicy進(jìn)行交互。
InputManager類從不與Java交互,而InputDispatchPolicy負(fù)責(zé)執(zhí)行所有與系統(tǒng)的外部交互,包括調(diào)用DVM業(yè)務(wù)。
3.2 創(chuàng)建流程
1)在android_server_InputManager_nativeInit中創(chuàng)建NativeInputManager對(duì)象,并保存到gNativeInputManager中;
2)在創(chuàng)建NativeInputManager對(duì)象時(shí),它會(huì)創(chuàng)建EventHub對(duì)象<且創(chuàng)建是其成員mNeedToScanDevices的值為true>,然后把剛創(chuàng)建的EventHub對(duì)象作為參數(shù)創(chuàng)建InputManager對(duì)象;
3)在創(chuàng)建InputManager對(duì)象時(shí),創(chuàng)建InputReader對(duì)象,然后把它作為參數(shù)創(chuàng)建InputReaderThread;創(chuàng)建InputDispatcher對(duì)象,然后把它作為參數(shù)創(chuàng)建InputDispatcherThread對(duì)象;(注:以上兩個(gè)線程對(duì)象都有自己的threadLoop函數(shù),它將在Thread::_threadLoop中被調(diào)用,這個(gè)Thread::_threadLoop是線程入口函數(shù),線程在Thread::run中被真正地創(chuàng)建)
4.1)創(chuàng)建InputReader對(duì)象
4.1.1)把EventHub、readerPolicy<實(shí)質(zhì)為NativeInputManager對(duì)象>和創(chuàng)建的InputDispatcher對(duì)象作為參數(shù)創(chuàng)建InputReader對(duì)象:mReader = new InputReader(eventHub, readerPolicy, mDispatcher);
4.1.2)在創(chuàng)建InputReader時(shí), 保存EventHub對(duì)象到mEventHub中,并創(chuàng)建QueuedInputListener對(duì)象并保存在mQueuedListener中
4.2)創(chuàng)建InputDispatcher對(duì)象
4.2.1)把傳入的參數(shù)dispatcherPolicy<實(shí)質(zhì)為NativeInputManager對(duì)象>作為參數(shù)創(chuàng)建InputDispatcher對(duì)象:mDispatcher = new InputDispatcher(dispatcherPolicy);
4.2.1)在創(chuàng)建InputDispatcher時(shí),創(chuàng)建了一個(gè)looper對(duì)象:mLooper = new Looper(false);
3.3 啟動(dòng)流程
1)在android_server_InputManager_nativeStart中調(diào)用InputManager::start,代碼如下:
result = gNativeInputManager->getInputManager()->start();
2)在InputManager::start中,調(diào)用mDispatcherThread->run和mReaderThread->run,代碼如下:
result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
3)在上面的Thread::run中,調(diào)用createThreadEtc函數(shù),并以Thread::_threadLoop作為入口函數(shù),以上面的mDispatcherThread或mReaderThread作為userdata創(chuàng)建線程
4)至此InputReader線程和InputDispatcher線程都已經(jīng)工作,詳細(xì)信息見Thread::_threadLoop,在此函數(shù)中它將調(diào)用mDispatcherThread或mReaderThread的threadLoop函數(shù)來做真正的事
5.1)mReaderThread->threadLoop
bool InputReaderThread::threadLoop() {
mReader->loopOnce();
return true;
}
5.2)mDispatcherThread->threadLoop
bool InputDispatcherThread::threadLoop() {
mDispatcher->dispatchOnce();
return true;
}
3.4 EventInput對(duì)象關(guān)系圖

4. 設(shè)備操作流程
從EventHub::getEvents讀取的事件數(shù)據(jù)結(jié)構(gòu)如下:
- struct RawEvent {
- nsecs_t when; //事件發(fā)生的時(shí)間
- int32_t deviceId; //產(chǎn)生此事件的設(shè)備,比如發(fā)送FINISHED_DEVICE_SCAN,不需要填此項(xiàng)
- int32_t type; //事件類型(如:DEVICE_ADDED,DEVICE_REMOVED,FINISHED_DEVICE_SCAN)
- int32_t scanCode;
- int32_t keyCode;
- int32_t value;
- uint32_t flags;
- };
讀取事件時(shí)的調(diào)用流程為:
Thread::_threadLoop->
InputReaderThread::threadLoop->
InputReader::loopOnce->
EventHub::getEvents->
4.1 打開設(shè)備
在EventHub::getEvents中,當(dāng)mNeedToScanDevices為true時(shí)<當(dāng)創(chuàng)建EventHub對(duì)象時(shí),它就為true>,它將從/dev/input目錄下查找所有設(shè)備,并進(jìn)行打開,獲取其相關(guān)屬性,最后加入mDevices列表中。
EventHub::scanDevicesLocked->
EventHub::scanDirLocked("/dev/input")->
EventHub::openDeviceLocked
4.1.1 打開事件輸入設(shè)備
打開事件輸入設(shè)備,在用戶態(tài)調(diào)用open,則在kernel態(tài)中調(diào)用evdev_open函數(shù),evdev_open處理流程如下:
1)首先從參數(shù)inode中獲取在evdev_table中的索引,從而獲取對(duì)應(yīng)的evdev對(duì)象
2)創(chuàng)建evdev_client對(duì)象,創(chuàng)建此對(duì)象時(shí)同時(shí)為其buffer成員分配對(duì)應(yīng)的內(nèi)存
3)把新創(chuàng)建evdev_client對(duì)象添加到client_list鏈表中
4)把client保存在file的private_data中
5)調(diào)用evdev_open_device->input_open_device->input_dev.open函數(shù)打開設(shè)備。
4.2 讀取輸入事件
要說EventHub::getEvents如何獲取輸入事件,不得不先說說它的幾個(gè)相關(guān)的成員變量:
1)mPendingEventCount:調(diào)用epoll_wait時(shí)的返回值,當(dāng)然如果沒有事件,則其值為0;
2)mPendingEventIndex:當(dāng)前需要處理的事件索引
3)mEpollFd:epoll實(shí)例,在EventHub::EventHub中初始化此例,所有輸入事件通過epoll_wait來獲取,每一個(gè)事件的數(shù)據(jù)結(jié)構(gòu)為:struct epoll_event,為了搞明白如何讀取輸入事件的原理,不得不對(duì)epoll相關(guān)的東東搞個(gè)清清楚楚,明明白白,見epoll kernel實(shí)現(xiàn)原理。注:epoll_event只表明某個(gè)設(shè)備上有事件,并不包含事件內(nèi)容,具體事件內(nèi)容需要通過read來讀取。
struct epoll_event定義如下:
- typedef union epoll_data
- {
- void *ptr;
- int fd;
- unsigned int u32;
- unsigned long long u64;
- } epoll_data_t;
-
- struct epoll_event
- {
- unsigned int events;
- epoll_data_t data;
- };
每個(gè)設(shè)備被創(chuàng)建(在函數(shù)EventHub::openDeviceLocked中)時(shí),都會(huì)向epoll注冊(cè),代碼如下:
- // Register with epoll.
- 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)) {
- LOGE("Could not add device fd to epoll instance. errno=%d", errno);
- delete device;
- return -1;
- }
4.2.1 查看設(shè)備上是否有事件
在調(diào)用epoll_wait(mEpollFd, mPendingEventItems, EPOLL_MAX_EVENTS, timeoutMillis)之后,讀到的epoll_event事件保存在mPendingEventItems,總共的事件數(shù)保存在mPendingEventCount,當(dāng)然,在調(diào)用epoll_event之前,mPendingEventIndex被清0,直正的事件處理在下面的代碼中。
- // Grab the next input event.
- bool deviceChanged = false;
- while (mPendingEventIndex < mPendingEventCount) {
- const struct epoll_event& eventItem = mPendingEventItems[mPendingEventIndex++];
- if (eventItem.data.u32 == EPOLL_ID_INOTIFY) {
- if (eventItem.events & EPOLLIN) {
- mPendingINotify = true;
- } else {
- LOGW("Received unexpected epoll event 0x%08x for INotify.", eventItem.events);
- }
- continue;
- }
-
- if (eventItem.data.u32 == EPOLL_ID_WAKE) {
- if (eventItem.events & EPOLLIN) {
- LOGV("awoken after wake()");
- awoken = true;
- char buffer[16];
- ssize_t nRead;
- do {
- nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
- } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
- } else {
- LOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
- eventItem.events);
- }
- continue;
- }
-
- ssize_t deviceIndex = mDevices.indexOfKey(eventItem.data.u32);
- if (deviceIndex < 0) {
- LOGW("Received unexpected epoll event 0x%08x for unknown device id %d.",
- eventItem.events, eventItem.data.u32);
- continue;
- }
-
- Device* device = mDevices.valueAt(deviceIndex);
- if (eventItem.events & EPOLLIN) {
- int32_t readSize = read(device->fd, readBuffer,
- sizeof(struct input_event) * capacity);
- if (readSize == 0 || (readSize < 0 && errno == ENODEV)) {
- // Device was removed before INotify noticed.
- LOGW("could not get event, removed? (fd: %d size: %d bufferSize: %d capacity: %d errno: %d)\n",
- device->fd, readSize, bufferSize, capacity, errno);
- deviceChanged = true;
- closeDeviceLocked(device);
- } else if (readSize < 0) {
- if (errno != EAGAIN && errno != EINTR) {
- LOGW("could not get event (errno=%d)", errno);
- }
- } else if ((readSize % sizeof(struct input_event)) != 0) {
- LOGE("could not get event (wrong size: %d)", readSize);
- } else {
- int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;
-
- size_t count = size_t(readSize) / sizeof(struct input_event);
- for (size_t i = 0; i < count; i++) {
- const struct input_event& iev = readBuffer[i];
- LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, value=%d",
- device->path.string(),
- (int) iev.time.tv_sec, (int) iev.time.tv_usec,
- iev.type, iev.code, iev.value);
-
- #ifdef HAVE_POSIX_CLOCKS
- // Use the time specified in the event instead of the current time
- // so that downstream code can get more accurate estimates of
- // event dispatch latency from the time the event is enqueued onto
- // the evdev client buffer.
- //
- // The event's timestamp fortuitously uses the same monotonic clock
- // time base as the rest of Android. The kernel event device driver
- // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
- // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
- // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
- // system call that also queries ktime_get_ts().
- event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
- + nsecs_t(iev.time.tv_usec) * 1000LL;
- LOGV("event time %lld, now %lld", event->when, now);
- #else
- event->when = now;
- #endif
- event->deviceId = deviceId;
- event->type = iev.type;
- event->scanCode = iev.code;
- event->value = iev.value;
- event->keyCode = AKEYCODE_UNKNOWN;
- event->flags = 0;
- if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
- status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
- &event->keyCode, &event->flags);
- LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
- iev.code, event->keyCode, event->flags, err);
- }
- event += 1;
- }
- capacity -= count;
- if (capacity == 0) {
- // The result buffer is full. Reset the pending event index
- // so we will try to read the device again on the next iteration.
- mPendingEventIndex -= 1;
- break;
- }
- }
- } else {
- LOGW("Received unexpected epoll event 0x%08x for device %s.",
- eventItem.events, device->identifier.name.string());
- }
- }
4.2.2 讀取設(shè)備上真正的事件
epoll_wait只是告訴我們Device已經(jīng)有事件了,讓我們?nèi)プx,真正讀取設(shè)備輸入事件的代碼如上,其流程如下:
1)根據(jù)eventItem.data.u32獲取設(shè)備索引,從而獲取對(duì)應(yīng)的Device
2)從device->fd中讀取input_event事件。read(device->fd, readBuffer, sizeof(struct input_event) * capacity);這些input_event是由各個(gè)注冊(cè)的input_device報(bào)告給input子系統(tǒng)的。具體讀入流程參見Input Core和evdev基本知識(shí) - Kernel3.0.8
至此,事件已經(jīng)讀取到用戶態(tài),哪我們就看看EventHub怎么處理這些事件了。
4.3 處理輸入事件
在4.2中,首先通過epoll_wait查看哪些設(shè)備有事件,然后通過read從有事件的設(shè)備中讀取事件,現(xiàn)在事件已經(jīng)讀取到用戶態(tài),且數(shù)據(jù)結(jié)構(gòu)為input_event,保存在EventHub::getEvents的readBuffer中。下面就看看這些事件下一步的東家是誰?
1)首先把input_event的信息填入RawEvent中,其相關(guān)代碼如下:
- #ifdef HAVE_POSIX_CLOCKS
- // Use the time specified in the event instead of the current time
- // so that downstream code can get more accurate estimates of
- // event dispatch latency from the time the event is enqueued onto
- // the evdev client buffer.
- //
- // The event's timestamp fortuitously uses the same monotonic clock
- // time base as the rest of Android. The kernel event device driver
- // (drivers/input/evdev.c) obtains timestamps using ktime_get_ts().
- // The systemTime(SYSTEM_TIME_MONOTONIC) function we use everywhere
- // calls clock_gettime(CLOCK_MONOTONIC) which is implemented as a
- // system call that also queries ktime_get_ts().
- event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
- + nsecs_t(iev.time.tv_usec) * 1000LL;
- LOGV("event time %lld, now %lld", event->when, now);
- #else
- event->when = now;
- #endif
- event->deviceId = deviceId;
- event->type = iev.type;
- event->scanCode = iev.code;
- event->value = iev.value;
- event->keyCode = AKEYCODE_UNKNOWN;
- event->flags = 0;
- if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
- status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
- &event->keyCode, &event->flags);
- LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
- iev.code, event->keyCode, event->flags, err);
- }
2)如果是input_event的類型為EV_KEY,則需要調(diào)用device->keyMap.keyLayoutMap->mapKey函數(shù)把iput_event.code映射為RawEvent.keyCode。相關(guān)數(shù)據(jù)結(jié)構(gòu)關(guān)系如下圖所示:

至此,EventHub::getEvents讀取事件的任務(wù)已經(jīng)完成,下面看看這些RawEvent的命運(yùn)如何呢?
4.3.1 InputReader::loopOnce如何處理RawEvent?
為此,先溫習(xí)一下讀取事件時(shí)的調(diào)用流程為:
Thread::_threadLoop->
InputReaderThread::threadLoop->
InputReader::loopOnce->
EventHub::getEvents->
在InputReader::loopOnce中,當(dāng)調(diào)用EventHub->getEvents獲取到RawEvent之后,調(diào)用InputReader::processEventsLocked來處理這些事件,然后調(diào)用mQueuedListener->flush()把這些隊(duì)列中的事件發(fā)送到Listener。
4.3.1.1 InputReader::processEventsLocked
在InputReader::processEventsLocked主要分兩步處理:
1)處理來自于事件驅(qū)動(dòng)設(shè)備的事件(processEventsForDeviceLocked)
2)處理設(shè)備增加、刪除和修改事件
按照程序執(zhí)行流程,應(yīng)該是先有設(shè)備,然后才會(huì)有設(shè)備事件,所以先分析設(shè)備增加。 其代碼如下:
- <span style="font-size:10px;">void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count)
- {
- for (const RawEvent* rawEvent = rawEvents; count;) {
- int32_t type = rawEvent->type;
- size_t batchSize = 1;
-
- //處理來自于事件驅(qū)動(dòng)設(shè)備的事件
- 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;
- }
- //處理來自于同一個(gè)事件驅(qū)動(dòng)設(shè)備的1個(gè)或多個(gè)事件
- processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
- }
- else
- {
- //處理增加或刪除事件驅(qū)動(dòng)設(shè)備的事件,在EventHub::getEvents中產(chǎn)生,
- //不是由事件驅(qū)動(dòng)設(shè)備產(chǎn)生的。
- 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:
- LOG_ASSERT(false); // can't happen
- break;
- }
- }
- count -= batchSize;
- rawEvent += batchSize;
- }
- }</span>
4.3.1.1.1 設(shè)備增加事件處理 addDeviceLocked
它處理其中的EventHubInterface::DEVICE_ADDED, EventHubInterface:: DEVICE_REMOVED和EventHubInterface::FINISHED_DEVICE_SCAN事件,即與Device相關(guān)的事件,這些事件是在EventHub::getEvents中產(chǎn)生的,并不是Kernel態(tài)的事件輸入設(shè)備產(chǎn)生的。
下面分析它如何處理EventHubInterface::DEVICE_ADDED事件。查看其它代碼,它是調(diào)用InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId)來處理此事件。
在InputReader::addDeviceLocked中的調(diào)用流程:
1)先根據(jù)mContext, deviceId, name, classes創(chuàng)建一個(gè)InputDevice對(duì)象,它用于表示單個(gè)輸入設(shè)備的狀態(tài)。其中的classes為對(duì)應(yīng)Device的classes成員,它用于表示設(shè)備類型,其定義如下:
- /*
- * Input device classes.
- */
- enum {
- /* The input device is a keyboard or has buttons. */
- INPUT_DEVICE_CLASS_KEYBOARD = 0x00000001,
-
- /* The input device is an alpha-numeric keyboard (not just a dial pad). */
- INPUT_DEVICE_CLASS_ALPHAKEY = 0x00000002,
-
- /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
- INPUT_DEVICE_CLASS_TOUCH = 0x00000004,
-
- /* The input device is a cursor device such as a trackball or mouse. */
- INPUT_DEVICE_CLASS_CURSOR = 0x00000008,
-
- /* The input device is a multi-touch touchscreen. */
- INPUT_DEVICE_CLASS_TOUCH_MT = 0x00000010,
-
- /* The input device is a directional pad (implies keyboard, has DPAD keys). */
- INPUT_DEVICE_CLASS_DPAD = 0x00000020,
-
- /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
- INPUT_DEVICE_CLASS_GAMEPAD = 0x00000040,
-
- /* The input device has switches. */
- INPUT_DEVICE_CLASS_SWITCH = 0x00000080,
-
- /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
- INPUT_DEVICE_CLASS_JOYSTICK = 0x00000100,
-
- /* The input device is external (not built-in). */
- INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000,
- }
創(chuàng)建InputDevice對(duì)象之后, 對(duì)于多點(diǎn)觸摸設(shè)備(class為INPUT_DEVICE_CLASS_TOUCH_MT),創(chuàng)建MultiTouchInputMapper對(duì)象并增加到InputDevice的mMappers向量列表中。
對(duì)于單點(diǎn)觸摸設(shè)備(class為INPUT_DEVICE_CLASS_TOUCH),創(chuàng)建SingleTouchInputMapper對(duì)象并增加到InputDevice的mMappers向量列表中。相關(guān)代碼如下:
- InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
- const String8& name, uint32_t classes) {
- InputDevice* device = new InputDevice(&mContext, deviceId, name, classes);
-
- ....
-
- if (keyboardSource != 0) {
- device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
- }
-
- // Cursor-like devices.
- if (classes & INPUT_DEVICE_CLASS_CURSOR) {
- device->addMapper(new CursorInputMapper(device));
- }
-
- // Touchscreens and touchpad devices.
- if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
- device->addMapper(new MultiTouchInputMapper(device));
- } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
- device->addMapper(new SingleTouchInputMapper(device));
- }
-
- // Joystick-like devices.
- if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
- device->addMapper(new JoystickInputMapper(device));
- }
-
- return device;
- }
總之,它調(diào)用createDeviceLocked創(chuàng)建一個(gè)InputDevice設(shè)備,并根據(jù)class類別創(chuàng)建對(duì)應(yīng)的事件轉(zhuǎn)換器(InputMapper),然后把這些新那建的InputMapper增加到InputDevice::mMappers中。InputMapper關(guān)系如下圖所示:

2)調(diào)用InputDevice::configure配置此InputDevice
3)調(diào)用InputDevice::reset重置此InputDevice
4)把新建的InputDevice增加到InputReader::mDevices中。
InputReader::processEventsLocked設(shè)備增加、刪除處理總結(jié):
它負(fù)責(zé)處理Device 增加、刪除事件。增加事件的流程為:為一個(gè)新增的Device創(chuàng)建一個(gè)InputDevice,并增加到InputReader::mDevices中;根據(jù)新增加設(shè)備的class類別,創(chuàng)建對(duì)應(yīng)的消息轉(zhuǎn)換器(InputMapper),然后此消息轉(zhuǎn)換器加入InputDevice::mMappers中。消息轉(zhuǎn)換器負(fù)責(zé)把讀取的RawEvent轉(zhuǎn)換成特定的事件,以供應(yīng)用程序使用。
EventHub與InputReader各自管理功能:
1)EventHub管理一堆Device,每一個(gè)Device與Kernel中一個(gè)事件輸入設(shè)備對(duì)應(yīng)
2)InputReader管理一堆InputDevice,每一個(gè)InputDevice與EventHub中的Device對(duì)應(yīng)
3)InputDevice管理一些與之相關(guān)的InputMapper,每一個(gè)InputMapper與一個(gè)特定的應(yīng)用事件相對(duì)應(yīng),如:SingleTouchInputMapper。
4.3.1.1.2 事件驅(qū)動(dòng)設(shè)備事件處理processEventsForDeviceLocked
下面的分析處理以單點(diǎn)觸摸為例,對(duì)于單點(diǎn)觸摸Touch Down時(shí),它將報(bào)告以下事件:
代碼:
input_report_abs(myInputDev, ABS_X, event->x);
input_report_abs(myInputDev, ABS_Y, event->y);
產(chǎn)生的事件:*type, code, value
EV_ABS,ABS_X,event->x
EV_ABS,ABS_Y,event->y
代碼:
input_report_key(myInputDev, BTN_TOUCH, 1);
產(chǎn)生的事件:*type, code, value
EV_KEY, BTN_TOUCH, 1
代碼:
input_sync(myInputDev);
它調(diào)用input_event(dev, EV_SYN, SYN_REPORT, 0);
產(chǎn)生的事件:*type, code, value
EV_SYN, SYN_REPORT, 0
它負(fù)責(zé)處理來自于同一個(gè)設(shè)備且在mEventBuffer中連續(xù)的多個(gè)事件,其函數(shù)原型如下:
- void InputReader::processEventsForDeviceLocked(int32_t deviceId,
- const RawEvent* rawEvents, size_t count) {
- ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
- if (deviceIndex < 0) {
- LOGW("Discarding event for unknown deviceId %d.", deviceId);
- return;
- }
-
- InputDevice* device = mDevices.valueAt(deviceIndex);
- if (device->isIgnored()) {
- //LOGD("Discarding event for ignored deviceId %d.", deviceId);
- return;
- }
-
- device->process(rawEvents, count);
- }
它其實(shí)很簡(jiǎn)單,根據(jù)輸入的deviceId找到對(duì)應(yīng)的InputDevice,然后調(diào)用InputDevice::process以對(duì)設(shè)備輸入事件進(jìn)行處理。InputDevice::process主要源碼如下:
- void InputDevice::process(const RawEvent* rawEvents, size_t count) {
- // Process all of the events in order for each mapper.
- // We cannot simply ask each mapper to process them in bulk because mappers may
- // have side-effects that must be interleaved. For example, joystick movement events and
- // gamepad button presses are handled by different mappers but they should be dispatched
- // in the order received.
-
- size_t numMappers = mMappers.size();
- for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++)
- {
- for (size_t i = 0; i < numMappers; i++) {
- InputMapper* mapper = mMappers[i];
- mapper->process(rawEvent);
- }
- }
- }
從上面的代碼中可以看出,在InputDevice::process中,對(duì)于傳入的每一個(gè)RawEvent,依次調(diào)用InputDevice中的每一個(gè)InputMapper來進(jìn)行處理。前面提到過,InputDevice包含一組處理對(duì)應(yīng)設(shè)備事件InputMapper,現(xiàn)在這些InputMapper開始干活了。
下面以處理一個(gè)單點(diǎn)觸摸事件設(shè)備的事件為例,進(jìn)行分析,其它的處理流程類似。對(duì)于mapper->process需要查看InputReader::createDeviceLocked中創(chuàng)建的具體的InputMapper的process函數(shù)。下面就看看SingleTouchInputMapper的process是如何處理的,其代碼如下:
- void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
- TouchInputMapper::process(rawEvent);
-
- mSingleTouchMotionAccumulator.process(rawEvent);
- }
1)TouchInputMapper::process
由此可見,它將首先調(diào)用TouchInputMaaper::process處理此事件,其處理代碼如下:
- void TouchInputMapper::process(const RawEvent* rawEvent) {
- mCursorButtonAccumulator.process(rawEvent);
- mCursorScrollAccumulator.process(rawEvent);
- mTouchButtonAccumulator.process(rawEvent);
-
- if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
- sync(rawEvent->when);
- }
- }
1.1) mCursorButtonAccumulator.process(rawEvent)
記錄mouse或touch pad按鍵狀態(tài),記錄rawEvent->type為EV_KEY,且rawEvent->scanCode為BTN_LEFT、BTN_RIGHT、BTN_MIDDLE、BTN_BACK、BTN_SIDE、BTN_FORWARD、BTN_EXTRA、BTN_TASK的事件。
1.2) mCursorScrollAccumulator.process(rawEvent)
記錄cursor scrolling motions,記錄rawEvent->type為EV_REL,且rawEvent->scanCode為REL_WHEEL、REL_HWHEEL的事件。
1.3) mTouchButtonAccumulator.process(rawEvent)
記錄touch, stylus and tool buttons狀態(tài),記錄rawEvent->type為EV_KEY,且rawEvent->scanCode為BTN_TOUCH、BTN_STYLUS、BTN_STYLUS2、BTN_TOOL_FINGER、BTN_TOOL_PEN、BTN_TOOL_RUBBER、BTN_TOOL_BRUSH、BTN_TOOL_PENCIL、BTN_TOOL_AIRBRUSH、BTN_TOOL_MOUSE、BTN_TOOL_LENS、BTN_TOOL_DOUBLETAP、BTN_TOOL_TRIPLETAP、BTN_TOOL_QUADTAP的事件。
看到了吧,我們的BTN_TOUCH在這兒被處理了,且其value被保存在mBtnTouch成員變量中。
1.4) sync(rawEvent->when)
處理EV_SYN:SYN_REPORT,我們的EV_SYN就在這兒被處理了,當(dāng)然它是Touch Down時(shí),所發(fā)事件的最后一個(gè)事件。這兒才是處理的重點(diǎn)。
TouchInputMapper::sync將調(diào)用SingleTouchInputMapper::syncTouch函數(shù)。
a)SingleTouchInputMapper::syncTouch
把mCurrentRawPointerData中的ABS_X和ABS_Y的值保存在TouchInputMapper::mCurrentRawPointerData->pointers中。
單點(diǎn)觸摸的syncTouch一次處理一個(gè)RawEvent,在pointers中只有一個(gè)值;而多點(diǎn)觸摸的syncTouch一次處理多個(gè)RawEvent,在pointers中有多個(gè)值,最多16個(gè)。
b)TouchInputMapper::cookPointerData
根據(jù)TouchInputMapper::mCurrentRawPointerData->pointers中的數(shù)據(jù),通過計(jì)算,最后生成TouchInputMapper::mCurrentCookedPointerData.pointerCoords,mCurrentCookedPointerData.pointerProperties和mCurrentCookedPointerData.idToIndex的數(shù)據(jù)。把Raw進(jìn)行cook,之后生成了cooked數(shù)據(jù)。
c)TouchInputMapper::dispatchHoverExit
d)TouchInputMapper::dispatchTouches
d.a)它調(diào)用dispatchMotion
d.b)在dispatchMotion中,根據(jù)cooked數(shù)據(jù)創(chuàng)建NotifyMotionArg對(duì)象,它描述了一個(gè)移動(dòng)事件
d.c)調(diào)用TouchInputMapper::getListener()->notifyMotion(&args)
TouchInputMapper::getListener()調(diào)用mContext->getListener(),此mContext為InputReader::mContext,所以其getListener()返回的則為InputReader::mQueuedListener,則最后調(diào)用QueuedInputListener::notifyMotion
補(bǔ)充1) InputReader::mContext在構(gòu)造時(shí)用自己的指針初始化了mContext,從而mContext::mReader則為此InputReader實(shí)例。
補(bǔ)充2) 在InputReader::createDeviceLocked中創(chuàng)建InputDevice時(shí),把自己的mContext作為參數(shù)傳入,從而把它保存在InputDevice::mContext中;在創(chuàng)建InputMapper時(shí),以InputDevice作為參數(shù),且InputMapper把它保存在mDevice中,然后從把InputDevice中的mContext也保存在InputMapper的mContext中。
d.d)把傳遞過來的NotifyMotionArg參數(shù)復(fù)制一份,然后加入QueuedInputListener::mArgsQueue例表中。
e)TouchInputMapper::dispatchHoverEnterAndMove
2)mSingleTouchMotionAccumulator.process
記錄ABS相關(guān)的值,記錄rawEvent->type為EV_ABS,且rawEvent->scanCode為ABS_X、ABS_Y、ABS_PRESSURE、ABS_TOOL_WIDTH、ABS_DISTANCE、ABS_TILT_X、ABS_TILT_Y的事件。我們發(fā)的ABS_X和ABS_Y在這兒被處理了。
事件處理相關(guān)數(shù)據(jù)結(jié)構(gòu)如下圖所示:

4.3.1.2 InputReader::mQueuedListener->flush()
先溫習(xí)一下,至此的消息結(jié)構(gòu)變化流程:

processEventsLocked已經(jīng)把來自于事件設(shè)備的事件煮熟之后放入到各種NotifyArgs(如NotifyMotionArgs)之中,然后把這些各種NotifyArgs加入InputReader::mQueuedListener::mArgsQueue鏈表中。本Flush函數(shù)就是要把mArgsQueue中的所有NotifyArgs進(jìn)行處理。為描述方便,先看看其代碼:
- void QueuedInputListener::flush() {
- size_t count = mArgsQueue.size();
- for (size_t i = 0; i < count; i++) {
- NotifyArgs* args = mArgsQueue[i];
- args->notify(mInnerListener);
- delete args;
- }
- mArgsQueue.clear();
- }
看到了吧,確實(shí)很簡(jiǎn)單,調(diào)用鏈表中每個(gè)NotifyArgs的notify函數(shù),且有一個(gè)有意思的參數(shù) mInnerListener,這個(gè)參數(shù)在前面多次提到過,它是在創(chuàng)建mQueuedListener時(shí)提供的,它其實(shí)就是InputManager中的mDispatcher,前面一直在InputReader中打轉(zhuǎn)轉(zhuǎn),現(xiàn)在終于看到InputDispatcher登場(chǎng)了,說明事件很快就可以謝幕了。
再向下看一下吧,這么多類NotifyArgs,為描述方便,下面以NotifyMotionArgs為例,其代碼為:
- void NotifyMotionArgs::notify(const sp<InputListenerInterface>& listener) const {
- listener->notifyMotion(this);
- }
下面就看看InputDispatcher(mDispatcher)的notifyMotion函數(shù)做了些什么。這個(gè)InputDispatcher::notifyMotion(const NotifyMotionArgs* args)可就不簡(jiǎn)單了。
在InputDispatcher::notifyMotion中,
1)根據(jù)NotifyMotionArgs提供的信息,構(gòu)造一個(gè)MotionEvent,再調(diào)用mPolicy->filterInputEvent看是否需要丟棄此事件,如果需要丟棄則馬上返加。其中mPolicy為NativeInputManager實(shí)例,在構(gòu)造InputDispatcher時(shí)提供的參數(shù)。
2)對(duì)于AMOTION_EVENT_ACTION_UP或AMOTION_EVENT_ACTION_DOWN事件,則直接根據(jù)NotifyMotionArgs提供的信息,構(gòu)造一個(gè)MotionEntry。
3)調(diào)用InputDispatcher::enqueueInboundEventLocked把新構(gòu)造的MotionEntry添加到InputDispatcher::mInboundQueue中,并返回是否需要喚醒mLooper<向pipe中寫入數(shù)據(jù)>的標(biāo)識(shí)。
以上操作都是在InputReader線程中完成的,現(xiàn)在應(yīng)該InputDispatcher線程開始工作了。
4. 4 分發(fā)輸入事件
InputDispatcherThread主循環(huán)如下:
Thread::_threadLoop->
InputDispatcherThread::threadLoop->
mDispatcher->dispatchOnce(InputDispatcher::dispatchOnce)->
dispatchOnceInnerLocked then
mLooper->pollOnce
下面先看看簡(jiǎn)單的mLooper->pollOnce
4.4.1 mLooper->pollOnce
其功能為等待超時(shí)或被pipe喚醒(InputReader線程調(diào)用InputDispatcher::notifyMotion時(shí), InputDispatcher::notifyMotion根據(jù)情況調(diào)用mLooper->wake)。
其調(diào)用流程如下:
mLooper->pollOnce(int timeoutMillis)->
Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData)->
4.4.2 dispatchOnceInnerLocked
1)從mInboundQueue從中依次取出EventEntry<MotionEntry的基類>,
2)調(diào)用InputDispatcher::dispatchMotionLocked處理此MotionEntry
3)調(diào)用InputDispatcher::dispatchEventToCurrentInputTargetsLocked
對(duì)于InputDispatcher::mCurrentInputTargets中的每一個(gè)InputTarget,并獲取對(duì)應(yīng)的Connection,調(diào)用InputDispatcher::prepareDispatchCycleLocked,
其相關(guān)代碼如下:
- <span style="font-size:10px;"> for (size_t i = 0; i < mCurrentInputTargets.size(); i++) {
- const InputTarget& inputTarget = mCurrentInputTargets.itemAt(i);
-
- ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel);
- if (connectionIndex >= 0) {
- sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex);
- prepareDispatchCycleLocked(currentTime, connection, eventEntry, & inputTarget,
- resumeWithAppendedMotionSample);
- } else {
- #if DEBUG_FOCUS
- LOGD("Dropping event delivery to target with channel '%s' because it "
- "is no longer registered with the input dispatcher.",
- inputTarget.inputChannel->getName().string());
- #endif
- }
- }</span>
4)InputDispatcher::prepareDispatchCycleLocked
4.1)調(diào)用enqueueDispatchEntryLocked創(chuàng)建DispatchEntry對(duì)象,并把它增加到Connection::outboundQueue隊(duì)列中。
4.2)調(diào)用activateConnectionLocked把當(dāng)前Connection增加到InputDispatcher::mActiveConnections鏈表中
4.3)調(diào)用InputDispatcher::startDispatchCycleLocked,接著它調(diào)用Connection::inputPublisher.publishMotionEvent來發(fā)布事件到ashmem buffer中,調(diào)用Connection::inputPublisher.sendDispatchSignal發(fā)送一個(gè)dispatch信號(hào)到InputConsumer通知它有一個(gè)新的消息到了,快來消費(fèi)吧! 關(guān)于消費(fèi)者如何注冊(cè)和如何消息的流程在下一個(gè)專題中再寫。本文到此結(jié)束!!!