Android中input_event的分析_第1页
Android中input_event的分析_第2页
Android中input_event的分析_第3页
Android中input_event的分析_第4页
Android中input_event的分析_第5页
免费预览已结束,剩余5页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Android的InputEvent子系统的来龙去脉。Android系统里面有很多小工具,运行这些工具,我们对它们有一个感性的认识,进而阅读和分析这些小工具源代码,再顺藤摸瓜,就可以把整个子系统的来龙去脉弄清楚。1 .运行toolbox的getevent工具。#getevent-helpgetevent-helpUsage:getevent-t-n-sswitchmask-S-vmask-p-q-ccount-rdevice- t:showtimestamps- n:don'tprintnewlines- s:printswitchstatesforgivenbits- S:print

2、allswitchstates- v:verbositymask(errs=1,dev=2,name=4,info=8,vers=16,pos.events=32)- p:showpossibleevents(errs,dev,name,pos.events)- q:quiet(clearverbositymask)- c:printgivennumberofeventsthenexit- r:printrateeventsarereceived#getevent-c20getevent-c20adddevice1:/dev/input/event4name:"sensor-inpu

3、t"adddevice2:/dev/input/event3name:"88pm860x_hook"adddevice3:/dev/input/event2name:"88pm860x_on"adddevice4:/dev/input/event1name:"88pm860x-touch"adddevice5:/dev/input/event0name:"pxa27x-keypad"/dev/input/event0:0001006600000001/dev/input/event0:0000000000

4、000000/dev/input/event0:0001006600000000/dev/input/event0:0000000000000000/dev/input/event1:0003000000000c48/dev/input/event1:0003000100000751/dev/input/event1:0001014a00000001/dev/input/event1:0000000000000000/dev/input/event1:0003000000000c67/dev/input/event1:00030001000006f9/dev/input/event1:0000

5、000000000000/dev/input/event1:0003000000000c9e/dev/input/event1:000300010000069e/dev/input/event1:0000000000000000/dev/input/event1:0003000000000cc4/dev/input/event1:0003000100000620/dev/input/event1:0000000000000000/dev/input/event1:0003000000000ce8/dev/input/event1:00030001000005ba/dev/input/event

6、1:0000000000000000运行这个工具,然后按键或者滑动触摸屏,会看到程序会实时打印evento从上面的输出来看,系统有5个input子系统。它们分别是adddevice1:/dev/input/event4name:"sensor-input"#Sensorinput子系统adddevice2:/dev/input/event3name:"88pm860x_hook"# 耳机Hook键子系统。可支持接电话挂电话的耳机上面有一个按键,对应的就是这个input子系统。adddevice3:/dev/input/event2name:"8

7、8pm860x_on"# 开机键input子系统adddevice4:/dev/input/event1name:"88pm860x-touch"# TouchScreeninput子系统adddevice5:/dev/input/event0name:"pxa27x-keypad"# 按键子系统,包括Home/Menu/Back等按键。可以尝试多种event,实际感觉一下出来的log。2 .阅读getevent的代码。代码为./core/toolbox/getevent.c从代码中,我们知道,程序在while(1)的一个死循环里,不断地在读取

8、(select操作)/dev/input下面的文件,检查是否Kernel往里面更新内容,如果有内容更新,就把它打印出来。并且从代码中,我们还知道,任何一个event都有三种属性,type,code,value.while(1)pollres=poll(ufds,nfds,-1);/printf("poll%d,returned%dn",nfds,pollres);if(ufds0.revents&POLLIN)read_notify(device_path,ufds0.fd,print_flags);for(i=1;i<nfds;i+)if(ufdsi.rev

9、ents)if(ufdsi.revents&POLLIN)res=read(ufdsi.fd,&event,sizeof(event);if(res<(int)sizeof(event)fprintf(stderr,"couldnotgeteventn");return1;if(get_time)printf("%ld-%ld:",event.time.tv_sec,event.time.tv_usec);if(print_device)printf("%s:",device_namesi);printf(&qu

10、ot;%04x%04x%08x”,event.type,event.code,event.value);if(sync_rate&&event.type=0&&event.code=0)int64_tnow=event.time.tv_sec*1000000LL+event.time.tv_usec;if(last_sync_time)printf("rate%lld",1000000LL/(now-last_sync_time);last_sync_time=now;printf("%s",newline);if(eve

11、nt_count&&-event_count=0)return0;3 .问题来了,AndroidFramework是否也是一样的原理呢?猜测应该是一样的才对,不然这个工具就没有调试的价值了。我们来阅读和分析framework中inputevent的相关代码。我们从Kernel层往上看,先看看Framework中,直接操纵/dev/input设备的代码。在.frameworks/base/libs/ui/EventHub.cpp中,我们看到跟getevent工具类似的代码。boolEventHub:getEvent(int32_t*outDeviceId,int32_t*outT

12、ype,int32_t*outScancode,int32_t*outKeycode,uint32_t*outFlags,int32_t*outValue,nsecs_t*outWhen)while(1)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);usle

13、ep(100000);continue;/mFDs0isusedforinotify,soprocessregulareventsstartingatmFDs1for(i=1;i<mFDCount;i+)if(mFDsi.revents)LOGV("reventsfor%d=0x%08x",i,mFDsi.revents);if(mFDsi.revents&POLLIN)res=read(mFDsi.fd,&iev,sizeof(iev);if(res=sizeof(iev)LOGV("%sgot:t0=%d,t1=%d,type=%d,co

14、de=%d,v=%d",mDevicesi->path.string(),4 .那么framework中那个模块再调用EventHub呢,接着往下查。在framework目录中,输入下面的命令查找#find.-name"*.cpp"|grep-vEventHub|xargsgrepEventHub./base/services/jni/com_android_server_KeyInputQueue.cpp:#include<ui/EventHub.h>./base/services/jni/com_android_server_KeyInput

15、Queue.cpp:staticsp<EventHub>gHub;sp<EventHub>hub=gHub;hub=newEventHub;sp<EventHub>hub=gHub;hub=newEventHub;./base/services/jni/com_android_server_KeyInputQueue.cpp:./base/services/jni/com_android_server_KeyInputQueue.cpp:./base/services/jni/com_android_server_KeyInputQueue.cpp:./ba

16、se/services/jni/com_android_server_KeyInputQueue.cpp:5 .从查找结果中得知,在jni文件com_android_server_KeyInputQueue.cpp文件中有对EventHub进行调用。打开并阅读com_android_server_KeyInputQueue.cpp文件得知,在下面的函数中调用了EventHub的getEvent函数staticjbooleanandroid_server_KeyInputQueue_readEvent(JNIEnv*env,jobjectclazz,jobjectevent)gLock.lock

17、();sp<EventHub>hub=gHub;if(hub=NULL)hub=newEventHub;gHub=hub;gLock.unlock();int32_tdeviceId;int32_ttype;int32_tscancode,keycode;uint32_tflags;int32_tvalue;nsecs_twhen;boolres=hub->getEvent(&deviceId,&type,&scancode,&keycode,&flags,&value,&when);env->SetIntFiel

18、d(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-

19、>SetIntField(event,gInputOffsets.mValue,value);env->SetLongField(event,gInputOffsets.mWhen,(jlong)(nanoseconds_to_milliseconds(when);returnres;6 .根据jni的调用规则,在本文件中查找对于的java函数。staticJNINativeMethodgInputMethods口=/*name,signature,funcPtr*/"readEvent","(Landroid/view/RawInputEvent;)

20、Z",(void*)android_server_KeyInputQueue_readEvent,7 .接着顺藤摸瓜,找到对应的java文件,base/services/java/com/android/server/KeyInputQueue.javaprivatestaticnativebooleanreadEvent(RawInputEventoutEvent);在一个线程中会调用readEvent函数。ThreadmThread=newThread("InputDeviceReader")publicvoidrun()if(DEBUG)Slog.v(TAG

21、,"InputDeviceReader.run()");android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);RawInputEventev=newRawInputEvent();while(true)tryInputDevicedi;/block,doesn'treleasethemonitorreadEvent(ev);booleansend=false;booleanconfigChanged=false;if(false)Slog.i(T

22、AG,"Inputevent:dev=0x"+Integer.toHexString(ev.deviceId)+"type=0x"+Integer.toHexString(ev.type)+"scancode="+ev.scancode+"keycode="+ev.keycode+"value="+ev.value);8 .那是谁启动这个线程呢?查找mThread变量,得知在KeyInputQueue的构造函数中会启动这个线程。KeyInputQueue(Contextcontext,Hapti

23、cFeedbackCallbackhapticFeedbackCallback)if(MEASURE_LATENCY)lt=newLatencyTimer(100,1000);Resourcesr=context.getResources();BAD_TOUCH_HACKr.getBoolean(ernal.R.bool.config_filterTouchEvents);JUMPY_TOUCH_HACKr.getBoolean(ernal.R.bool.config_filterJumpyTouchEvents);mHapticFe

24、edbackCallback=hapticFeedbackCallback;readExcludedDevices();PowerManagerpm=(PowerManager)context.getSystemService(Context.POWER_SERVICE);mWakeLock=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"KeyInputQueue");mWakeLock.setReferenceCounted(false);mFirst=newQueuedEvent();mLast=newQueuedEven

25、t();mFirst.next=mLast;mThread.start();9 .那这个KeyInputQueue是在哪里被实例化呢?而且查看KeyInputQueue类的声明,得知它是一个abstractclass.publicabstractclassKeyInputQueue()说明它肯定会被某个类继承.接着查找。/frameworks$find.-name"*.java"|grep-vKeyInputQueue|xargsgrepKeyInputQueue./policies/base/phone/com/android/internal/policy/impl/K

26、eyguardViewMediator.java:*linkcom.android.server.KeyInputQueue'sandlinkandroid.view.WindowManager's./base/services/java/com/android/server/PowerManagerService.java:&&!"KeyInputQueue".equals(tag)implementsreturnreturnreturnprivateclassKeyQimplements./base/services/java/com/a

27、ndroid/server/WindowManagerService.java:importcom.android.server.KeyInputQueue.QueuedEvent;./base/services/java/com/android/server/WindowManagerService.java:Watchdog.Monitor,KeyInputQueue.HapticFeedbackCallback./base/services/java/com/android/server/WindowManagerService.java:KeyInputQueue.getSwitchS

28、tate(sw);./base/services/java/com/android/server/WindowManagerService.java:KeyInputQueue.getSwitchState(devid,sw);./base/services/java/com/android/server/WindowManagerService.java:KeyInputQueue.hasKeys(keycodes,keyExists);./base/services/java/com/android/server/WindowManagerService.java:extendsKeyInputQueue./base/services/java/com/android/server/WindowManagerService.java:KeyInputQueue.FilterCallback./base/services/java/com/android/server/InputDevice.java:/ForusebyKeyInputQueueforifkeepingtrackofthe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论