Linux程序设计入门 - gpm gpm是Linux console下的滑鼠驱动程序,它主要提供文字模式下的滑鼠事件处 理。Linux下文字界面的滑鼠几乎都是用gpm来处理。 gpm的文件在gpm原始码的doc目录中,详细的说明可叁考该目录中的文件gpm programming guide,此处只提供给您KickStart的一些技巧及一些叁考说明。 特别注意到以下的范例,需在console下执行,不可在X Window的 Terminal下 执行。 范例 : gpm_mouse.c gpm原始码中有一个mev.c的程序 ,主要用来测试滑鼠状态。事实上,mev.c是个 很好的范例,本范例便是取自mev.c,经过简化修改而来。 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <gpm.h> void main(int argc,char **argv) { fd_set readset; Gpm_Event event; Gpm_Connect conn; conn.eventMask = ~0; conn.defaultMask = ~GPM_HARD; conn.maxMod = 0; conn.minMod = 0; if (Gpm_Open(&conn,0)==-1) { printf("Can not open mouse connection "); 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: event 0x%02X, at %2i %2i (delta %2i %2i)," "button %i, modifiers 0x%02X ", event.type, event.x,event.y, event.dx,event.dy, event.buttons, event.modifiers ); } } } while (Gpm_Close()); } 编译 gcc -o gpm_mouse gpm_mouse.c -lgpm 检验结果 mouse: event 0x01, at 15 1 (delta -2 -1),button 0, modifiers 0x00 mouse: event 0x01, at 14 1 (delta -1 0),button 0, modifiers 0x00 mouse: event 0x01, at 13 1 (delta -1 0),button 0, modifiers 0x00 资料结构 typedef struct Gpm_Connect { unsigned short eventMask, defaultMask; unsigned short minMod, maxMod; int pid; int vc; } Gpm_Connect; enum Gpm_Etype { GPM_MOVE=1, GPM_DRAG=2, /* exactly one of the bare ones is active at a time */ GPM_DOWN=4, GPM_UP= 8, GPM_SINGLE=16, /* at most one in three is set */ GPM_DOUBLE=32, GPM_TRIPLE=64, /* WARNING: I depend on the values */ GPM_MFLAG=128, /* motion during click? */ GPM_HARD=256, /* if set in the defaultMask, force an already used event to pass over to another handler */ GPM_ENTER=512, /* enter event, user in Roi's */ GPM_LEAVE=1024 /* leave event, used in Roi's */ }; typedef struct Gpm_Event { unsigned char buttons, modifiers; /* try to be a multiple of 4 */ unsigned short vc; short dx, dy, x, y; enum Gpm_Etype type; int clicks; enum Gpm_Margin margin; } Gpm_Event; typedef int Gpm_Handler(Gpm_Event *event, void *clientdata); 函数宣告 int Gpm_Open(Gpm_Connect * CONN, int FLAGS); int Gpm_Close(void); int Gpm_GetEvent(Gpm_Event * EVENT); int Gpm_Getc(FILE * fp); #define Gpm_Getchar() Gpm_Getc(stdin) int Gpm_Wgetch(); #define Gpm_Getch() (Gpm_Wgetch(NULL)) int Gpm_Repeat (int millisecs); int Gpm_DrawPointer (int X, int Y, int FD); int GPM_DRAWPOINTER (Gpm_Event *EPTR;) int Gpm_FitValuesM (int *X, int *Y, int MARGIN); int Gpm_FitValues (X,Y); Gpm_FitEvent (EPTR); char *Gpm_GetLibVersion (int *where); char *Gpm_GetServerVersion (int *where); int Gpm_GetSnapshot (Gpm_Event *ePtr); OK STATION, Webmaster, Brian Lin