Linux程式設計入門-gpm(轉)

subid發表於2007-08-17
Linux程式設計入門-gpm(轉)[@more@] gpm是Linuxconsole下的滑鼠驅動程式,它主要提供文字模式下的滑鼠事件處理。Linux下文字介面的滑鼠幾乎都是用gpm來處理。

  gpm的檔案在gpm原始碼的doc目錄中,詳細的說明可叄考該目錄中的檔案gpmprogrammingguide,此處只提供給您KickStart的一些技巧及一些叄考說明。

  特別注意到以下的範例,需在console下執行,不可在XWindow的Terminal下執行。

  範例:gpm_mouse.c

  gpm原始碼中有一個mev.c的程式,主要用來測試滑鼠狀態。事實上,mev.c是個很好的範例,本範例便是取自mev.c,經過簡化修改而來。

#include

#include

#include

#include


voidmain(intargc,char**argv)

{

fd_setreadset;

Gpm_Eventevent;

Gpm_Connectconn;


conn.eventMask=~0;

conn.defaultMask=~GPM_HARD;

conn.maxMod=0;

conn.minMod=0;


if(Gpm_Open(&conn,0)==-1){

printf("Cannotopenmouseconnectionn");

exit(1);

}


while(1){


FD_ZERO(&readset);

FD_SET(gpm_fd,&readset);

select(gpm_fd+1,&readset,0,0,0);


if(FD_ISSET(gpm_fd,&readset)){

if(Gpm_GetEvent(&event)>0){

printf("mouse:event0x%02X,at%2i%2i(delta%2i%2i),"

"button%i,modifiers0x%02Xrn",

event.type,

event.x,event.y,

event.dx,event.dy,

event.buttons,

event.modifiers

);

}

}

}


while(Gpm_Close());


}


編譯

gcc-ogpm_mousegpm_mouse.c-lgpm

檢驗結果


mouse:event0x01,at151(delta-2-1),button0,modifiers0x00

mouse:event0x01,at141(delta-10),button0,modifiers0x00

mouse:event0x01,at131(delta-10),button0,modifiers0x00


資料結構

typedefstructGpm_Connect{

unsignedshorteventMask,defaultMask;

unsignedshortminMod,maxMod;

intpid;

intvc;

}Gpm_Connect;


enumGpm_Etype{

GPM_MOVE=1,

GPM_DRAG=2,/*exactlyoneofthebareonesisactiveatatime

*/

GPM_DOWN=4,

GPM_UP=8,


GPM_SINGLE=16,/*atmostoneinthreeisset*/

GPM_DOUBLE=32,

GPM_TRIPLE=64,/*WARNING:Idependonthevalues*/


GPM_MFLAG=128,/*motionduringclick?*/

GPM_HARD=256,/*ifsetinthedefaultMask,forcean

alreadyusedeventtopassovertoanotherhandler*/


GPM_ENTER=512,/*enterevent,userinRoi's*/

GPM_LEAVE=1024/*leaveevent,usedinRoi's*/

};


typedefstructGpm_Event{

unsignedcharbuttons,modifiers;/*trytobeamultipleof4*/

unsignedshortvc;

shortdx,dy,x,y;

enumGpm_Etypetype;

intclicks;

enumGpm_Marginmargin;

}Gpm_Event;


typedefintGpm_Handler(Gpm_Event*event,void*clientdata);


函式宣告


intGpm_Open(Gpm_Connect*CONN,intFLAGS);

intGpm_Close(void);

intGpm_GetEvent(Gpm_Event*EVENT);

intGpm_Getc(FILE*fp);

#defineGpm_Getchar()Gpm_Getc(stdin)

intGpm_Wgetch();

#defineGpm_Getch()(Gpm_Wgetch(NULL))

intGpm_Repeat(intmillisecs);

intGpm_DrawPointer(intX,intY,intFD);

intGPM_DRAWPOINTER(Gpm_Event*EPTR;)

intGpm_FitValuesM(int*X,int*Y,intMARGIN);

intGpm_FitValues(X,Y);

Gpm_FitEvent(EPTR);

char*Gpm_GetLibVersion(int*where);

char*Gpm_GetServerVersion(int*where);

intGpm_GetSnapshot(Gpm_Event*ePtr);

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10796304/viewspace-963391/,如需轉載,請註明出處,否則將追究法律責任。

相關文章