Init------------zygote---------system-server-------------------windosmanager ------------------------------------------------------------ UEventObserver
------------------------------------------------------------ InputDeviceRead
-------------------------------------------------------------InputDispatcher
-------------------------------------------------------------DisplayEventThr
-------------------------------------------------------------ActivityManager
EventHub :
而 事件的傳入是從EventHub開始的,EventHub是事件的抽象結(jié)構(gòu),維護著系統(tǒng)設(shè)備的運行情況,設(shè)備類型包括Keyboard、 TouchScreen、TraceBall。它在系統(tǒng)啟動的時候會通過open_device方法將系統(tǒng)提供的輸入設(shè)備都增加到這個抽象結(jié)構(gòu)中,并維護 一個所有輸入設(shè)備的文件描述符,如果輸入設(shè)備是鍵盤的話還會讀取/system/usr/keylayout/目錄下對應(yīng)鍵盤設(shè)備的映射文件,另外 getEvent方法是對EventHub中的設(shè)備文件描述符使用poll操作等侍驅(qū)動層事件的發(fā)生,如果發(fā)生的事件是鍵盤事件,則調(diào)用Map函數(shù)按照映 射文件轉(zhuǎn)換成相應(yīng)的鍵值并將掃描碼和鍵碼返回給KeyInputQueue。
KeyLayoutMap主要是讀取鍵盤映射文件并將鍵盤掃描碼和鍵碼進行轉(zhuǎn)換
frameworks/base/core/jni/server/ com_android_server_KeyInputQueue.cpp
EventHub和KeyinputQueue的JNI接口層
KeyinputQueue:
在線程InputDeviceReader中會根據(jù)事件的類型以及事件值進行判斷處理,從而確定這個事件對應(yīng)的設(shè)備狀態(tài)是否發(fā)生了改變并相應(yīng)的改變對這個設(shè)備的描述結(jié)構(gòu)InputDevice。
getEvent:在給定時間段時看是否有事件發(fā)生,如果有的話返回true否則false。
Windowmanager:
(frameworks/base/services/java/com/android/server/windowmanagerservice.java)
進 程Windowmanager會創(chuàng)建一個線程(InputDispatcherThread),在這個線程里從事件隊列中讀取發(fā)生的事件 (QueuedEvent ev = mQueue.getEvent()),并根據(jù)讀取到事件類型的不同分成三類(KEYBOARD、TOUCHSCREEN、TRACKBALL),分別進 行處理,例如鍵盤事件會調(diào)用dispatchKey((KeyEvent)ev.event, 0, 0)以將事件通過Binder發(fā)送給具有焦點的窗口應(yīng)用程序,然后調(diào)用 mQueue.recycleEvent(ev)繼續(xù)等侍鍵盤事件的發(fā)生;如果是觸摸屏事件則調(diào)用dispatchPointer(ev, (MotionEvent)ev.event, 0, 0),這里會根據(jù)事件的種類(UP、DOWN、MOVE、OUT_SIDE等)進行判斷并處理,比如Cancel或?qū)⑹录l(fā)送到具有權(quán)限的指定的窗口中 去;
Android 輸入事件流程
EventHub
EventHub對輸入設(shè)備進行了封裝。輸入設(shè)備驅(qū)動程序?qū)τ脩艨臻g應(yīng)用程序提供一些設(shè)備文件,這些設(shè)備文件放在/dev/input里面。
EventHub掃描/dev/input下所有設(shè)備文件,并打開它們。
- bool EventHub::openPlatformInput( void )
- {
- ...
- mFDCount=1;
- mFDs=(pollfd*)calloc(1, sizeof (mFDs[0]));
- mDevices=(device_t**)calloc(1, sizeof (mDevices[0]));
- mFDs[0].events=POLLIN;
- mDevices[0]=NULL;
- res=scan_dir(device_path);
- ...
- return true ;
- }
EventHub對外提供了一個函數(shù)用于從輸入設(shè)備文件中讀取數(shù)據(jù)。
- bool EventHub::getEvent(int32_t*outDeviceId,int32_t*outType,
- int32_t*outScancode,int32_t*outKeycode,uint32_t*outFlags,
- int32_t*outValue,nsecs_t*outWhen)
- {
- ...
- while (1){
- //First,reportanydevicesthathadlastbeenadded/removed.
- if (mClosingDevices!=NULL){
- device_t*device=mClosingDevices;
- LOGV( "Reportingdeviceclosed:id=0x%x,name=%s/n" ,
- device->id,device->path.string());
- mClosingDevices=device->next;
- *outDeviceId=device->id;
- if (*outDeviceId==mFirstKeyboardId)*outDeviceId=0;
- *outType=DEVICE_REMOVED;
- delete device;
- return true ;
- }
- if (mOpeningDevices!=NULL){
- device_t*device=mOpeningDevices;
- LOGV( "Reportingdeviceopened:id=0x%x,name=%s/n" ,
- device->id,device->path.string());
- mOpeningDevices=device->next;
- *outDeviceId=device->id;
- if (*outDeviceId==mFirstKeyboardId)*outDeviceId=0;
- *outType=DEVICE_ADDED;
- return true ;
- }
- release_wake_lock(WAKE_LOCK_ID);
- pollres=poll(mFDs,mFDCount,-1);
- acquire_wake_lock(PARTIAL_WAKE_LOCK,WAKE_LOCK_ID);
- if (pollres<=0){
- if (errno!=EINTR){
- LOGW( "selectfailed(errno=%d)/n" ,errno);
- usleep(100000);
- }
- continue ;
- }
- for (i=1;i<mFDCount;i++){
- if (mFDs[i].revents){
- LOGV( "reventsfor%d=0x%08x" ,i,mFDs[i].revents);
- if (mFDs[i].revents&POLLIN){
- res=read(mFDs[i].fd,&iev, sizeof (iev));
- if (res== sizeof (iev)){
- LOGV( "%sgot:t0=%d,t1=%d,type=%d,code=%d,v=%d" ,
- mDevices[i]->path.string(),
- ( int )iev.time.tv_sec,( int )iev.time.tv_usec,
- iev.type,iev.code,iev.value);
- *outDeviceId=mDevices[i]->id;
- if (*outDeviceId==mFirstKeyboardId)*outDeviceId=0;
- *outType=iev.type;
- *outScancode=iev.code;
- if (iev.type==EV_KEY){
- err=mDevices[i]->layoutMap->map(iev.code,outKeycode,outFlags);
- LOGV( "iev.code=%doutKeycode=%doutFlags=0x%08xerr=%d/n" ,
- iev.code,*outKeycode,*outFlags,err);
- if (err!=0){
- *outKeycode=0;
- *outFlags=0;
- }
- } else {
- *outKeycode=iev.code;
- }
- *outValue=iev.value;
- *outWhen=s2ns(iev.time.tv_sec)+us2ns(iev.time.tv_usec);
- return true ;
- } else {
- if (res<0){
- LOGW( "couldnotgetevent(errno=%d)" ,errno);
- } else {
- LOGE( "couldnotgetevent(wrongsize:%d)" ,res);
- }
- continue ;
- }
- }
- }
- }
- ...
- }
對于按鍵事件,調(diào)用mDevices[i]->layoutMap->map進行映射。映射實際是由 KeyLayoutMap::map完成的,KeyLayoutMap類里讀取配置文件qwerty.kl,由配置 文件 qwerty.kl 決定鍵值的映射關(guān)系。你可以通過修 改./development/emulator/keymaps/qwerty.kl來改變鍵值的映射關(guān)系。
JNI 函數(shù)
在 frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp文 件中,向 JAVA提供了函數(shù)android_server_KeyInputQueue_readEvent,用于讀 取輸入設(shè)備事件。
- static jboolean
- android_server_KeyInputQueue_readEvent(JNIEnv*env,jobjectclazz,
- jobjectevent)
- {
- gLock.lock();
- sphub=gHub;
- if (hub==NULL){
- hub= new EventHub;
- gHub=hub;
- }
- gLock.unlock();
- int32_tdeviceId;
- int32_ttype;
- int32_tscancode,keycode;
- uint32_tflags;
- int32_tvalue;
- nsecs_twhen;
- bool res=hub->getEvent(&deviceId,&type,&scancode,&keycode,
- &flags,&value,&when);
- env->SetIntField(event,gInputOffsets.mDeviceId,(jint)deviceId);
- env->SetIntField(event,gInputOffsets.mType,(jint)type);
- env->SetIntField(event,gInputOffsets.mScancode,(jint)scancode);
- env->SetIntField(event,gInputOffsets.mKeycode,(jint)keycode);
- env->SetIntField(event,gInputOffsets.mFlags,(jint)flags);
- env->SetIntField(event,gInputOffsets.mValue,value);
- env->SetLongField(event,gInputOffsets.mWhen,
- (jlong)(nanoseconds_to_milliseconds(when)));
- return res;
- }
readEvent調(diào)用hub->getEvent讀了取事件,然后轉(zhuǎn)換成JAVA的結(jié)構(gòu)。
事件中轉(zhuǎn)線程
在frameworks/base/services/java/com/android/server/KeyInputQueue.java 里創(chuàng)建了一個線程,它循環(huán)的讀取事件,然后把事件放入事件隊列里。
- ThreadmThread= new Thread( "InputDeviceReader" ){
- public void run(){
- android.os.Process.setThreadPriority(
- android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
- try {
- RawInputEventev= new RawInputEvent();
- while ( true ){
- InputDevicedi;
- readEvent(ev);
- send=preprocessEvent(di,ev);
- addLocked(di,curTime,ev.flags,...,me);
- }
- }
- };
輸入事件分發(fā)線程
在frameworks/base/services/java/com/android/server/WindowManagerService.java里創(chuàng)建了一個輸入事件分發(fā)線程,它負責把事件分發(fā)到相應(yīng)的窗口上去。
- mQueue.getEvent
- dispatchKey/dispatchPointer/dispatchTrackball
按鍵,觸摸屏流程分析
按鍵觸摸屏流程分析:
WindowManagerService類的構(gòu)造函數(shù)
WindowManagerService()
mQueue = new KeyQ();
因為 WindowManagerService.java (frameworks/base/services/java/com/android/server)中有:
private class KeyQ extends KeyInputQueue
KeyQ 是抽象類 KeyInputQueue 的實現(xiàn),所以 new KeyQ類的時候?qū)嶋H上在 KeyInputQueue 類中創(chuàng)建了
一個線程 InputDeviceReader 專門用來沖設(shè)備讀取按鍵事件,代碼:
Thread mThread = new Thread("InputDeviceReader") {
public void run()
{
在循環(huán)中調(diào)用:readEvent(ev);
...
send = preprocessEvent(di, ev);
實際調(diào)用的是 KeyQ 類的 preprocessEvent 函數(shù)
...
int keycode = rotateKeyCodeLocked(ev.keycode);
int[] map = mKeyRotationMap;
for (int i=0; i<N; i+=2)
{
if (map[i] == keyCode)
return map[i+1];
} //
addLocked(di, curTime, ev.flags,RawInputEvent.CLASS_KEYBOARD,newKeyEvent(di, di.mDownTime, curTime, down,keycode, 0, scancode,...));
QueuedEvent ev = obtainLocked(device, when, flags, classType, event);
}
}
readEvent() 實際上調(diào)用的是 com_android_server_KeyInputQueue.cpp (frameworks/base/services/jni)中的:
static jboolean android_server_KeyInputQueue_readEvent(JNIEnv* env, jobject clazz,jobject event)
bool res = hub->getEvent(&deviceId, &type, &scancode, &keycode,&flags, &value, &when);
調(diào)用的是 EventHub.cpp (frameworks/base/libs/ui)中的:
bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
int32_t* outValue, nsecs_t* outWhen)
在函數(shù)中調(diào)用了讀設(shè)備操作:res = read(mFDs[i].fd, &iev, sizeof(iev));
在構(gòu)造函數(shù) WindowManagerService()調(diào)用 new KeyQ() 以后接著調(diào)用了:
mInputThread = new InputDispatcherThread();
...
mInputThread.start();
來啟動一個線程 InputDispatcherThread
run()
process();
QueuedEvent ev = mQueue.getEvent(...)
因為WindowManagerService類中: final KeyQ mQueue;
所以實際上 InputDispatcherThread 線程實際上從 KeyQ 的事件隊列中讀取按鍵事件。
switch (ev.classType)
case RawInputEvent.CLASS_KEYBOARD:
...
dispatchKey((KeyEvent)ev.event, 0, 0);
mQueue.recycleEvent(ev);
break;
case RawInputEvent.CLASS_TOUCHSCREEN:
//Log.i(TAG, "Read next event " + ev);
dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
break;
===============================================================
KeyInputQueue.java (frameworks/base/services/java/com/android/server):
的線程 Thread mThread = new Thread("InputDeviceReader") 本地調(diào)用:
readEvent(ev);讀取按鍵。readEvent 調(diào)用的是文件:
com_android_server_KeyInputQueue.cpp (frameworks/base/services/jni)中的函數(shù):
static jboolean android_server_KeyInputQueue_readEvent(JNIEnv* env, jobject clazz,
jobject event)
android_server_KeyInputQueue_readEvent中有:
hub = new EventHub;
bool res = hub->getEvent(&deviceId, &type, &scancode, &keycode,
&flags, &value, &when);
hub->getEvent 調(diào)用的是
EventHub.cpp (frameworks/base/libs/ui) 文件中的函數(shù):
bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
int32_t* outValue, nsecs_t* outWhen)
讀取按鍵。
class RefBase::weakref_impl : public RefBase::weakref_type
在系統(tǒng)啟動后,android 會通過
static const char *device_path = "/dev/input";
bool EventHub::openPlatformInput(void)
res = scan_dir(device_path);
通過下面的函數(shù)打開設(shè)備。
int EventHub::open_device(const char *deviceName)
{
...
fd = open(deviceName, O_RDWR);
...
mFDs[mFDCount].fd = fd;
mFDs[mFDCount].events = POLLIN;
...
ioctl(mFDs[mFDCount].fd, EVIOCGNAME(sizeof(devname)-1), devname);
...
const char* root = getenv("ANDROID_ROOT");
snprintf(keylayoutFilename, sizeof(keylayoutFilename),
"%s/usr/keylayout/%s.kl", root, tmpfn);
...
device->layoutMap->load(keylayoutFilename);
...
}
打開設(shè)備的時候,如果 device->classes&CLASS_KEYBOARD 不等于 0 表明是鍵盤。
常用輸入設(shè)備的定義有:
enum {
CLASS_KEYBOARD = 0x00000001, //鍵盤
CLASS_ALPHAKEY = 0x00000002, //
CLASS_TOUCHSCREEN = 0x00000004, //觸摸屏
CLASS_TRACKBALL = 0x00000008 //軌跡球
};
打開鍵盤設(shè)備的時候通過上面的 ioctl 獲得設(shè)備名稱,命令字 EVIOCGNAME 的定義在文件:
kernel/include/linux/input.h 中。
#define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len) /* get device name */
在內(nèi)核鍵盤驅(qū)動文件 drivers/input/keyboard/pxa27x_keypad.c 中定義了設(shè)備名稱:pxa27x-keypad
static struct platform_driver pxa27x_keypad_driver = {
.probe = pxa27x_keypad_probe,
.remove = __devexit_p(pxa27x_keypad_remove),
.suspend = pxa27x_keypad_suspend,
.resume = pxa27x_keypad_resume,
.driver = {
.name = "pxa27x-keypad",
.owner = THIS_MODULE,
},
};
ANDROID_ROOT 為環(huán)境變量,在android的命令模式下通過 printenv 可以知道它為: system
所以 keylayoutFilename 為:/system/usr/keylayout/pxa27x-keypad.kl
pxa27x-keypad.kl 定義了按鍵映射,具體內(nèi)容如下:
----------------------
# NUMERIC KEYS 3x4
key 2 1
key 3 2
key 4 3
key 5 4
key 6 5
key 7 6
key 8 7
key 9 8
key 10 9
key 11 0
key 83 POUND
key 55 STAR
# FUNCTIONAL KEYS
key 231 MENU WAKE_DROPPED
key 192 BACK WAKE_DROPPED
key 193 HOME WAKE
key 107 DEL WAKE
key 102 CALL WAKE_DROPPED
key 158 ENDCALL WAKE_DROPPED
key 28 DPAD_CENTER WAKE
key 115 VOLUME_UP
key 114 VOLUME_DOWN
----------------------
如果沒有定義鍵盤映射文件,那么默認使用系統(tǒng)的 /system/usr/keylayout/qwerty.kl
可以修改 /system/usr/keylayout/qwerty.kl 文件改變Android公司的按鍵映射。
device->layoutMap->load(keylayoutFilename) 調(diào)用的是文件:
KeyLayoutMap.cpp (frameworks/base/libs/ui)中的函數(shù):
status_t KeyLayoutMap::load(const char* filename)通過解析 pxa27x-keypad.kl
把按鍵的映射關(guān)系保存在 :KeyedVector<int32_t,Key> m_keys; 中。
當獲得按鍵事件以后調(diào)用:
status_t KeyLayoutMap::map(int32_t scancode, int32_t *keycode, uint32_t *flags)
由映射關(guān)系 KeyedVector<int32_t,Key> m_keys 把掃描碼轉(zhuǎn)換成andorid上層可以識別的按鍵。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
