Android应用程序键盘(Keyboard)消息处理机制分析(6)
16lz
2021-01-24
Step 21.EventHub.openDevice
这个函数定义在frameworks/base/libs/ui/EventHub.cpp文件中:
这个函数定义在frameworks/base/libs/ui/EventHub.cpp文件中:
- intEventHub::openDevice(constchar*deviceName){
- intversion;
- intfd;
- structpollfd*new_mFDs;
- device_t**new_devices;
- char**new_device_names;
- charname[80];
- charlocation[80];
- charidstr[80];
- structinput_idid;
- LOGV("Openingdevice:%s",deviceName);
- AutoMutex_l(mLock);
- fd=open(deviceName,O_RDWR);
- if(fd<0){
- LOGE("couldnotopen%s,%s\n",deviceName,strerror(errno));
- return-1;
- }
- ......
- intdevid=0;
- while(devid<mNumDevicesById){
- if(mDevicesById[devid].device==NULL){
- break;
- }
- devid++;
- }
- ......
- mDevicesById[devid].seq=(mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
- if(mDevicesById[devid].seq==0){
- mDevicesById[devid].seq=1<<SEQ_SHIFT;
- }
- new_mFDs=(pollfd*)realloc(mFDs,sizeof(mFDs[0])*(mFDCount+1));
- new_devices=(device_t**)realloc(mDevices,sizeof(mDevices[0])*(mFDCount+1));
- if(new_mFDs==NULL||new_devices==NULL){
- LOGE("outofmemory");
- return-1;
- }
- mFDs=new_mFDs;
- mDevices=new_devices;
- ......
- device_t*device=newdevice_t(devid|mDevicesById[devid].seq,deviceName,name);
- if(device==NULL){
- LOGE("outofmemory");
- return-1;
- }
- device->fd=fd;
- mFDs[mFDCount].fd=fd;
- mFDs[mFDCount].events=POLLIN;
- mFDs[mFDCount].revents=0;
- //Figureoutthekindsofeventsthedevicereports.
- uint8_tkey_bitmask[sizeof_bit_array(KEY_MAX+1)];
- memset(key_bitmask,0,sizeof(key_bitmask));
- LOGV("Gettingkeys...");
- if(ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(key_bitmask)),key_bitmask)>=0){
- //Seeifthisisakeyboard.Ignoreeverythinginthebuttonrangeexceptfor
- //gamepadswhicharealsoconsideredkeyboards.
- if(containsNonZeroByte(key_bitmask,0,sizeof_bit_array(BTN_MISC))
- ||containsNonZeroByte(key_bitmask,sizeof_bit_array(BTN_GAMEPAD),
- sizeof_bit_array(BTN_DIGI))
- ||containsNonZeroByte(key_bitmask,sizeof_bit_array(KEY_OK),
- sizeof_bit_array(KEY_MAX+1))){
- device->classes|=INPUT_DEVICE_CLASS_KEYBOARD;
- device->keyBitmask=newuint8_t[sizeof(key_bitmask)];
- if(device->keyBitmask!=NULL){
- memcpy(device->keyBitmask,key_bitmask,sizeof(key_bitmask));
- }else{
- deletedevice;
- LOGE("outofmemoryallocatingkeybitmask");
- return-1;
- }
- }
- }
- ......
- if((device->classes&INPUT_DEVICE_CLASS_KEYBOARD)!=0){
- chartmpfn[sizeof(name)];
- charkeylayoutFilename[300];
- //amoredescriptivename
- device->name=name;
- //replaceallthespaceswithunderscores
- strcpy(tmpfn,name);
- for(char*p=strchr(tmpfn,'');p&&*p;p=strchr(tmpfn,''))
- *p='_';
- //findthe.klfileweneedforthisdevice
- constchar*root=getenv("ANDROID_ROOT");
- snprintf(keylayoutFilename,sizeof(keylayoutFilename),
- "%s/usr/keylayout/%s.kl",root,tmpfn);
- booldefaultKeymap=false;
- if(access(keylayoutFilename,R_OK)){
- snprintf(keylayoutFilename,sizeof(keylayoutFilename),
- "%s/usr/keylayout/%s",root,"qwerty.kl");
- defaultKeymap=true;
- }
- status_tstatus=device->layoutMap->load(keylayoutFilename);
- if(status){
- LOGE("Error%dloadingkeylayout.",status);
- }
- //telltheworldaboutthedevname(thedescriptivename)
- if(!mHaveFirstKeyboard&&!defaultKeymap&&strstr(name,"-keypad")){
- //thebuilt-inkeyboardhasawell-knowndeviceIDof0,
- //thisdevicebetternotgoaway.
- mHaveFirstKeyboard=true;
- mFirstKeyboardId=device->id;
- property_set("hw.keyboards.0.devname",name);
- }else{
- //ensuremFirstKeyboardIdissetto-something-.
- if(mFirstKeyboardId==0){
- mFirstKeyboardId=device->id;
- }
- }
- charpropName[100];
- sprintf(propName,"hw.keyboards.%u.devname",device->id);
- property_set(propName,name);
- //'Q'keysupport=cheaptestofwhetherthisisanalpha-capablekbd
- if(hasKeycodeLocked(device,AKEYCODE_Q)){
- device->classes|=INPUT_DEVICE_CLASS_ALPHAKEY;
- }
- //SeeifthisdevicehasaDPAD.
- if(hasKeycodeLocked(device,AKEYCODE_DPAD_UP)&&
- hasKeycodeLocked(device,AKEYCODE_DPAD_DOWN)&&
- hasKeycodeLocked(device,AKEYCODE_DPAD_LEFT)&&
- hasKeycodeLocked(device,AKEYCODE_DPAD_RIGHT)&&
- hasKeycodeLocked(device,AKEYCODE_DPAD_CENTER)){
- device->classes|=INPUT_DEVICE_CLASS_DPAD;
- }
- //Seeifthisdevicehasagamepad.
- for(size_ti=0;i<sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]);i++){
- if(hasKeycodeLocked(device,GAMEPAD_KEYCODES[i])){
- device->classes|=INPUT_DEVICE_CLASS_GAMEPAD;
- break;
- }
- }
- LOGI("Newkeyboard:device->id=0x%xdevname='%s'propName='%s'keylayout='%s'\n",
- device->id,name,propName,keylayoutFilename);
- }
- ......
- mDevicesById[devid].device=device;
- device->next=mOpeningDevices;
- mOpeningDevices=device;
- mDevices[mFDCount]=device;
- mFDCount++;
- return0;
- }
更多相关文章
- 学习深入理解android第一章
- android 文件下载到SD卡中
- java android 删除文件和文件夹的函数
- 安装Android(安卓)SDK
- Android自定义Dialog去除黑色边框
- Android中Log机制
- Android设计开发要必用的Color.xml文件整理
- [置顶] android 中使用回调函数
- Android中java.lang.ClassNotFoundException: ***.**** in loade