首页 文章 精选 留言 我的

精选列表

搜索[android things],共10000篇文章
优秀的个人博客,低调大师

Android应用程序键盘(Keyboard)消息处理机制分析(14)

3.InputManager分发键盘消息给应用程序的过程分析 在分析InputManager分发键盘消息给应用程序的过程之前,我们先假设现在没有键盘事件发生,因此,InputManager中的InputReader正在睡眠等待键盘事件的发生,而InputManager中的InputDispatcher正在等待InputReader从睡眠中醒过来并且唤醒它,而应用程序也正在消息循环中等待InputDispatcher从睡眠中醒过来并且唤醒它。这时候,用户按下键盘中的一个键,于是,一系列唤醒的事件就依次发生了,一直到应用程序中正在显示的Activity得到通知,有键盘事件发生了。我们先来看这个过程的序列图,然后再详细分析每一个步骤: Step 1. InputReader.pollOnce Step 2. EventHub.getEvent 这两个函数分别定义在frameworks/base/libs/ui/InputReader.cpp和frameworks/base/libs/ui/EventHub.cpp文件中,前面我们在分析InputManager的启动过程的Step 17和Step 18时,已经看到过这两个函数了。InputReaderThread线程会不民地循环调用InputReader.pollOnce函数来读入键盘事件,而实际的键盘事件读入操作是由EventHub.getEvent函数来进行的。如果当前没有键盘事件发生,InputReaderThread线程就会睡眠在EventHub.getEvent函数上,而当键盘事件发生后,就会把这个事件封装成一个RawEvent对象,然后返回到pollOnce函数中,执行process函数进一步处理: voidInputReader::loopOnce(){ RawEventrawEvent; mEventHub->getEvent(&rawEvent); ...... process(&rawEvent); } Step 3. InputReader.process 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidInputReader::process(constRawEvent*rawEvent){ switch(rawEvent->type){ caseEventHubInterface::DEVICE_ADDED: addDevice(rawEvent->deviceId); break; caseEventHubInterface::DEVICE_REMOVED: removeDevice(rawEvent->deviceId); break; caseEventHubInterface::FINISHED_DEVICE_SCAN: handleConfigurationChanged(rawEvent->when); break; default: consumeEvent(rawEvent); break; } } 当键盘事件发生时,rawEvent->type的值为EV_KEY,这是一个宏定义,具体可以参考bionic/libc/kernel/common/linux/input.h文件: #defineEV_KEY0x01 因此,接下来会调用consumeEvent函数进一步处理。 Step 4.InputReader.consumeEvent 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidInputReader::consumeEvent(constRawEvent*rawEvent){ int32_tdeviceId=rawEvent->deviceId; {//acquiredeviceregistryreaderlock RWLock::AutoRLock_rl(mDeviceRegistryLock); ssize_tdeviceIndex=mDevices.indexOfKey(deviceId); if(deviceIndex<0){ LOGW("DiscardingeventforunknowndeviceId%d.",deviceId); return; } InputDevice*device=mDevices.valueAt(deviceIndex); if(device->isIgnored()){ //LOGD("DiscardingeventforignoreddeviceId%d.",deviceId); return; } device->process(rawEvent); }//releasedeviceregistryreaderlock } 首先从rawEvent中取得触发键盘事件设备对象device,然后调用它的process函数进行处理。 Step 5. InputDevice.process 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidInputDevice::process(constRawEvent*rawEvent){ size_tnumMappers=mMappers.size(); for(size_ti=0;i<numMappers;i++){ InputMapper*mapper=mMappers[i]; mapper->process(rawEvent); } } 这里的mMapper成员变量保存了一系列输入设备事件处理象,例如负责处理键盘事件的KeyboardKeyMapper对象、负责处理轨迹球事件的TrackballInputMapper对象以及负责处理触摸屏事件的TouchInputMapper对象, 它们是在InputReader类的成员函数createDevice中创建的。这里查询每一个InputMapper对象是否要对当前发生的事件进行处理。由于发生的是键盘事件,真正会对该事件进行处理的只有KeyboardKeyMapper对象。 Step 6.KeyboardInputMapper.process 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidKeyboardInputMapper::process(constRawEvent*rawEvent){ switch(rawEvent->type){ caseEV_KEY:{ int32_tscanCode=rawEvent->scanCode; if(isKeyboardOrGamepadKey(scanCode)){ processKey(rawEvent->when,rawEvent->value!=0,rawEvent->keyCode,scanCode, rawEvent->flags); } break; } } } 这个函数首先会检查一下键盘扫描码是否正确,如果正确的话,就会调用processKey函数进一步处理。 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966632,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android应用程序键盘(Keyboard)消息处理机制分析(15)

Step 7.KeyboardInputMapper.processKey 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidKeyboardInputMapper::processKey(nsecs_twhen,booldown,int32_tkeyCode, int32_tscanCode,uint32_tpolicyFlags){ int32_tnewMetaState; nsecs_tdownTime; boolmetaStateChanged=false; {//acquirelock AutoMutex_l(mLock); if(down){ //Rotatekeycodesaccordingtoorientationifneeded. //Note:getDisplayInfoisnon-reentrantsowecancontinueholdingthelock. if(mAssociatedDisplayId>=0){ int32_torientation; if(!getPolicy()->getDisplayInfo(mAssociatedDisplayId,NULL,NULL,&orientation)){ return; } keyCode=rotateKeyCode(keyCode,orientation); } //Addkeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyrepeat,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; }else{ //keydown if((policyFlags&POLICY_FLAG_VIRTUAL) &&mContext->shouldDropVirtualKey(when,getDevice(),keyCode,scanCode)){ return; } mLocked.keyDowns.push(); KeyDown&keyDown=mLocked.keyDowns.editTop(); keyDown.keyCode=keyCode; keyDown.scanCode=scanCode; } mLocked.downTime=when; }else{ //Removekeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyup,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; mLocked.keyDowns.removeAt(size_t(keyDownIndex)); }else{ //keywasnotactuallydown LOGI("Droppingkeyupfromdevice%sbecausethekeywasnotdown." "keyCode=%d,scanCode=%d", getDeviceName().string(),keyCode,scanCode); return; } } int32_toldMetaState=mLocked.metaState; newMetaState=updateMetaState(keyCode,down,oldMetaState); if(oldMetaState!=newMetaState){ mLocked.metaState=newMetaState; metaStateChanged=true; } downTime=mLocked.downTime; }//releaselock if(metaStateChanged){ getContext()->updateGlobalMetaState(); } getDispatcher()->notifyKey(when,getDeviceId(),AINPUT_SOURCE_KEYBOARD,policyFlags, down?AKEY_EVENT_ACTION_DOWN:AKEY_EVENT_ACTION_UP, AKEY_EVENT_FLAG_FROM_SYSTEM,keyCode,scanCode,newMetaState,downTime); } 这个函数首先对对按键作一些处理,例如,当某一个DPAD键被按下时,根据当时屏幕方向的不同,它所表示的意义也不同,因此,这里需要根据当时屏幕的方向来调整键盘码: //Rotatekeycodesaccordingtoorientationifneeded. //Note:getDisplayInfoisnon-reentrantsowecancontinueholdingthelock. if(mAssociatedDisplayId>=0){ int32_torientation; if(!getPolicy()->getDisplayInfo(mAssociatedDisplayId,NULL,NULL,&orientation)){ return; } keyCode=rotateKeyCode(keyCode,orientation); } 如果这个键是一直按着不放的,不管屏幕的方向如何,必须保证后面的键盘码和前面的一样: //Addkeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyrepeat,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; }else{ //keydown if((policyFlags&POLICY_FLAG_VIRTUAL) &&mContext->shouldDropVirtualKey(when,getDevice(),keyCode,scanCode)){ return; } mLocked.keyDowns.push(); KeyDown&keyDown=mLocked.keyDowns.editTop(); keyDown.keyCode=keyCode; keyDown.scanCode=scanCode; } 如果是第一次按下某个键,还必须把它保存在mLocked.keyDowns里面,就是为了处理上面讲的当这个键盘一直按着不放的时候屏幕方向发生改变的情况。 如果是松开键盘上的某个键,就把它从mLocked.keyDowns里面删除: //Removekeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyup,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; mLocked.keyDowns.removeAt(size_t(keyDownIndex)); }else{ //keywasnotactuallydown LOGI("Droppingkeyupfromdevice%sbecausethekeywasnotdown." "keyCode=%d,scanCode=%d", getDeviceName().string(),keyCode,scanCode); return; } 当然,对键盘事件的这些处理不是本文的重点,本文的重点是分析从键盘事件到当前激活的Activity窗口接收到这个键盘消息的过程。 最后,KeyboardInputMappger函数通知InputDispatcher,有键盘事件发生了: getDispatcher()->notifyKey(when,getDeviceId(),AINPUT_SOURCE_KEYBOARD,policyFlags, down?AKEY_EVENT_ACTION_DOWN:AKEY_EVENT_ACTION_UP, AKEY_EVENT_FLAG_FROM_SYSTEM,keyCode,scanCode,newMetaState,downTime); 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966633,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android应用程序键盘(Keyboard)消息处理机制分析(4)

Step 18. EventHub.getEvent 这个函数定义在frameworks/base/libs/ui/EventHub.cpp文件中: boolEventHub::getEvent(RawEvent*outEvent) { outEvent->deviceId=0; outEvent->type=0; outEvent->scanCode=0; outEvent->keyCode=0; outEvent->flags=0; outEvent->value=0; outEvent->when=0; //NotethatweonlyallowonecallertogetEvent(),sodon'tneed //todolockinghere...onlywhenadding/removingdevices. if(!mOpened){ mError=openPlatformInput()?NO_ERROR:UNKNOWN_ERROR; mOpened=true; mNeedToSendFinishedDeviceScan=true; } for(;;){ //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; if(device->id==mFirstKeyboardId){ outEvent->deviceId=0; }else{ outEvent->deviceId=device->id; } outEvent->type=DEVICE_REMOVED; outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); deletedevice; mNeedToSendFinishedDeviceScan=true; returntrue; } if(mOpeningDevices!=NULL){ device_t*device=mOpeningDevices; LOGV("Reportingdeviceopened:id=0x%x,name=%s\n", device->id,device->path.string()); mOpeningDevices=device->next; if(device->id==mFirstKeyboardId){ outEvent->deviceId=0; }else{ outEvent->deviceId=device->id; } outEvent->type=DEVICE_ADDED; outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); mNeedToSendFinishedDeviceScan=true; returntrue; } if(mNeedToSendFinishedDeviceScan){ mNeedToSendFinishedDeviceScan=false; outEvent->type=FINISHED_DEVICE_SCAN; outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); returntrue; } //Grabthenextinputevent. for(;;){ //Consumebufferedinputevents,ifany. if(mInputBufferIndex<mInputBufferCount){ conststructinput_event&iev=mInputBufferData[mInputBufferIndex++]; constdevice_t*device=mDevices[mInputDeviceIndex]; LOGV("%sgot:t0=%d,t1=%d,type=%d,code=%d,v=%d",device->path.string(), (int)iev.time.tv_sec,(int)iev.time.tv_usec,iev.type,iev.code,iev.value); if(device->id==mFirstKeyboardId){ outEvent->deviceId=0; }else{ outEvent->deviceId=device->id; } outEvent->type=iev.type; outEvent->scanCode=iev.code; if(iev.type==EV_KEY){ status_terr=device->layoutMap->map(iev.code, &outEvent->keyCode,&outEvent->flags); LOGV("iev.code=%dkeyCode=%dflags=0x%08xerr=%d\n", iev.code,outEvent->keyCode,outEvent->flags,err); if(err!=0){ outEvent->keyCode=AKEYCODE_UNKNOWN; outEvent->flags=0; } }else{ outEvent->keyCode=iev.code; } outEvent->value=iev.value; //Useaneventtimestampinthesametimebaseas //java.lang.System.nanoTime()andandroid.os.SystemClock.uptimeMillis() //asexpectedbytherestofthesystem. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); returntrue; } //Finishreadingalleventsfromdevicesidentifiedinpreviouspoll(). //ThiscodeassumesthatmInputDeviceIndexisinitially0andthatthe //reventsmemberofpollfdisinitializedto0whenthedeviceisfirstadded. //SincemFDs[0]isusedforinotify,weprocessregulareventsstartingatindex1. mInputDeviceIndex+=1; if(mInputDeviceIndex>=mFDCount){ break; } conststructpollfd&pfd=mFDs[mInputDeviceIndex]; if(pfd.revents&POLLIN){ int32_treadSize=read(pfd.fd,mInputBufferData, sizeof(structinput_event)*INPUT_BUFFER_SIZE); if(readSize<0){ if(errno!=EAGAIN&&errno!=EINTR){ LOGW("couldnotgetevent(errno=%d)",errno); } }elseif((readSize%sizeof(structinput_event))!=0){ LOGE("couldnotgetevent(wrongsize:%d)",readSize); }else{ mInputBufferCount=readSize/sizeof(structinput_event); mInputBufferIndex=0; } } } ...... mInputDeviceIndex=0; //Pollforevents.Mindthewakelockdance! //Weholdawakelockatalltimesexceptduringpoll().Thisworksduetosome //subtlechoreography.Whenadevicedriverhaspending(unread)events,itacquires //akernelwakelock.However,oncethelastpendingeventhasbeenread,thedevice //driverwillreleasethekernelwakelock.Topreventthesystemfromgoingtosleep //whenthishappens,theEventHubholdsontoitsownuserwakelockwhiletheclient //isprocessingevents.Thusthesystemcanonlysleepiftherearenoevents //pendingorcurrentlybeingprocessed. release_wake_lock(WAKE_LOCK_ID); intpollResult=poll(mFDs,mFDCount,-1); acquire_wake_lock(PARTIAL_WAKE_LOCK,WAKE_LOCK_ID); if(pollResult<=0){ if(errno!=EINTR){ LOGW("pollfailed(errno=%d)\n",errno); usleep(100000); } } } } 这个函数比较长,我们一步一步来分析。 首先,如果是第一次进入到这个函数中时,成员变量mOpened的值为false,于是就会调用openPlatformInput函数来打开系统输入设备,在本文中,我们主要讨论的输入设备就是键盘了。打开了这些输入设备文件后,就可以对这些输入设备进行是监控了。如果不是第一次进入到这个函数,那么就会分析当前有没有输入事件发生,如果有,就返回这个事件,否则就会进入等待状态,等待下一次输入事件的发生。在我们这个场景中,就是等待下一次键盘事件的发生了。 我们先分析openPlatformInput函数的实现,然后回过头来分析这个getEvent函数的具体的实现。 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966613,如需转载请自行联系原作者

优秀的个人博客,低调大师

Unity 5.3 安装完没有Android(安卓)或IOS Module(模块)?

文章目录[点击展开](?)[+] Unity5.3 模块独立 Unity5.3把各个模块分开来了,主程序安装包更轻巧,在官网下载的话,能下载到Unity安装程序,Unity编辑器等一些资源package,其它的模块可以通过Unity安装程序进行下载 下载存档链接:http://unity3d.com/cn/get-unity/download/archive 模块文件 下面是我首次下载的文件 只有web player平台? 安装UnitySetup64-5.3.1f1之后,我发现在安装目录没有Document文档, 打开Unity也会发现只有Web Player平台,其它平台提示模块没有下载,那怎么办呢? 解决方法 1、运行UnityDownloadAssistant(下载助手),此助手可以下载Unity的各个模块,如下图所示,根据需要选择相应的模块 2、设置下载文件的存放,确认Unity的安装目录 ,继续下一步。 下载的模块文件 3、下载成功后,模块会自动安装(日后发给同事他们可以双击安装) 查看已安装模块 4、在Unity的模块管理面板可以看到已安装的模块。 本文转自赵青青博客园博客,原文链接:http://www.cnblogs.com/zhaoqingqing/p/5061732.html,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码