关闭

关闭

关闭

封号提示

内容

首页 linux_uinput.pdf

linux_uinput.pdf

linux_uinput.pdf

上传者: 小Daniel_Dai 2012-05-18 评分 0 0 0 0 0 0 暂无简介 简介 举报

简介:本文档为《linux_uinputpdf》,可适用于IT/计算机领域,主题内容包含DashboardJanuaryIssueMehulPatelUsinguinputdriverinLinuxxtosenduserinputDas符等。

DashboardJanuaryIssueMehulPatelUsinguinputdriverinLinuxxtosenduserinputDashboardJanuaryIssueUsinguinputdriverinLinuxxtosenduserinputIntroduction:TheLinuxxprovidesa“uinput”driver,whichhelpsuserstoinjectdatatotheLinuxkernelThisisveryusefulwhilewritingapplicationstointerfacecustomizedinputdeviceslikewirelessjoystick,keyboardetcThedriverusesdevuinputdevicetosenddatatokernelspacewhichinturnsenddatatoXwindowsORactiveshellThisfeaturecanbeusedtoperformautomatedshellscriptswhichinvolvegraphicaluserinputsUinputisconfiguredasaloadablemoduleinmostoftheLinuxkernelsYoucanloaduinputdriverbygivingthefollowingcommands$modprobeuinput$lsmodThe“lsmod”commandlistsallthedriversloadedintheLinuxsystemYoushouldsee“uinput”driverinthelistThenextstepistodevelopasampleapplicationThisapplicationwillsendtheuserkeysequencetokernelwhichisinturnsenttoXWindowsorshellOpeninganinputdevice:Opentheinputdeviceuinpfd=open("devuinput",OWRONLY|ONDELAY)if(uinpfd==){printf("Unabletoopendevuinputn")return}Afteropeningdeviceyouhavetoconfiguretheuinputdeviceparameters(mouse,keyboard,etc)usingtheioctl()functionsuchas:ioctl(outfd,UISETEVBIT,EVKEY)ioctl(outfd,UISETEVBIT,EVREP)TheEVKEYandEVREPinformtheuinputdriverastheeventisakeyboardeventandthekeyvaluecontainsthekeyrepetitionpropertyDashboardJanuaryIssueSendingeventstokernel:Alltheeventscomingfromtheuserprogramwillbecarriedtothekernelspacethroughthestructure"structinputevent"definedinkernels"usrincludelinuxinputh"Akeyboardcanbegeneratedusingfollowingpieceofcode:eventtype=EVKEYeventcode=KEYENTEReventvalue=write(uinpfd,event,sizeof(event))TheaboveexamplewillsendENTERkeytokernelThiskeyeventinturnwillbesenttouserspaceapplicationAllthekeydefinitionsaredefinedin“usrincludelinuxinputh”fileYoucanusethefollowingsamplecodetotestLinuxuinputinterfaceuinputc#include<stringh>#include<stdioh>#include<systypesh>#include<sysstath>#include<fcntlh>#include<linuxinputh>#include<linuxuinputh>#include<stdioh>#include<systimeh>#include<systypesh>#include<unistdh>*Globals*staticintuinpfd=structuinputuserdevuinpuInputdevicestructurestructinputeventeventInputdevicestructure*Setuptheuinputdevice*intsetupuinputdevice(){Temporaryvariableinti=Opentheinputdeviceuinpfd=open("devuinput",OWRONLY|ONDELAY)if(uinpfd==){DashboardJanuaryIssueprintf("Unabletoopendevuinputn")return}memset(uinp,,sizeof(uinp))IntializetheuInputdevicetostrncpy(uinpname,"PolyVisionTouchScreen",UINPUTMAXNAMESIZE)uinpidversion=uinpidbustype=BUSUSBSetuptheuinputdeviceioctl(uinpfd,UISETEVBIT,EVKEY)ioctl(uinpfd,UISETEVBIT,EVREL)ioctl(uinpfd,UISETRELBIT,RELX)ioctl(uinpfd,UISETRELBIT,RELY)for(i=i<i){ioctl(uinpfd,UISETKEYBIT,i)}ioctl(uinpfd,UISETKEYBIT,BTNMOUSE)ioctl(uinpfd,UISETKEYBIT,BTNTOUCH)ioctl(uinpfd,UISETKEYBIT,BTNMOUSE)ioctl(uinpfd,UISETKEYBIT,BTNLEFT)ioctl(uinpfd,UISETKEYBIT,BTNMIDDLE)ioctl(uinpfd,UISETKEYBIT,BTNRIGHT)ioctl(uinpfd,UISETKEYBIT,BTNFORWARD)ioctl(uinpfd,UISETKEYBIT,BTNBACK)*Createinputdeviceintoinputsubsystem*write(uinpfd,uinp,sizeof(uinp))if(ioctl(uinpfd,UIDEVCREATE)){printf("UnabletocreateUINPUTdevice")return}return}voidsendclickevents(){Movepointerto(,)locationmemset(event,,sizeof(event))gettimeofday(eventtime,)eventtype=EVRELeventcode=RELXDashboardJanuaryIssueeventvalue=write(uinpfd,event,sizeof(event))eventtype=EVRELeventcode=RELYeventvalue=write(uinpfd,event,sizeof(event))eventtype=EVSYNeventcode=SYNREPORTeventvalue=write(uinpfd,event,sizeof(event))ReportBUTTONCLICKPRESSeventmemset(event,,sizeof(event))gettimeofday(eventtime,)eventtype=EVKEYeventcode=BTNLEFTeventvalue=write(uinpfd,event,sizeof(event))eventtype=EVSYNeventcode=SYNREPORTeventvalue=write(uinpfd,event,sizeof(event))ReportBUTTONCLICKRELEASEeventmemset(event,,sizeof(event))gettimeofday(eventtime,)eventtype=EVKEYeventcode=BTNLEFTeventvalue=write(uinpfd,event,sizeof(event))eventtype=EVSYNeventcode=SYNREPORTeventvalue=write(uinpfd,event,sizeof(event))}voidsendabutton(){ReportBUTTONCLICKPRESSeventmemset(event,,sizeof(event))gettimeofday(eventtime,)eventtype=EVKEYeventcode=KEYADashboardJanuaryIssueeventvalue=write(uinpfd,event,sizeof(event))eventtype=EVSYNeventcode=SYNREPORTeventvalue=write(uinpfd,event,sizeof(event))ReportBUTTONCLICKRELEASEeventmemset(event,,sizeof(event))gettimeofday(eventtime,)eventtype=EVKEYeventcode=KEYAeventvalue=write(uinpfd,event,sizeof(event))eventtype=EVSYNeventcode=SYNREPORTeventvalue=write(uinpfd,event,sizeof(event))}*ThisfunctionwillopentheuInputdevicePleasemakesurethatyouhaveinsertedtheuinputkointokernel*intmain(){Returnanerrorifdevicenotfoundif(setupuinputdevice()<){printf("Unabletofinduinputdevicen")return}sendabutton()Senda"A"keysendclickevents()Sendmouseevent*Destroytheinputdevice*ioctl(uinpfd,UIDEVDESTROY)*ClosetheUINPUTdevice*close(uinpfd)}

用户评论(0)

0/200

精彩专题

上传我的资料

每篇奖励 +2积分

资料评价:

/6
1下载券 下载 加入VIP, 送下载券

意见
反馈

立即扫码关注

爱问共享资料微信公众号

返回
顶部