Getting started with Linux programming-gpm

Source: Internet
Author: User
For more information about Linux programming and kernel, see gpm. Getting started with Linux programming-gpm

Gpm is a mouse driver in the Linux console. It mainly provides mouse events in text mode.

. In Linux, almost all the mouse on the text interface is processed using gpm.


The gpm file is in the gpm original code doc directory. For detailed instructions, refer to the file gpm in this directory.

Programming guide, which only provides some KickStart skills and references.


Note that the following example must be executed on the console and cannot be executed under Terminal of X Window.

Run.



Example: gpm_mouse.c


The original gpm code contains a program named "PEM. c", which is mainly used to test the mouse state. In fact

A good example. This example is taken from the "PEM. c" and simplified and modified.


# Include

# Include

# Include

# Include


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 \ n ");

Exit (1 );

}


While (1 ){


FD_ZERO (& readset );

FD_SET (gpm_fd, & readset );

Select (gpm_fd + 1, & readset, 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 \ r \ n ",

Event. type,

Event. x, event. y,

Event. dx, event. dy,

Event. buttons,

Event. modifiers

);

}

}

}


While (Gpm_Close ());


}


Compile


Gcc-o gpm_mouse gpm_mouse.c-lgpm


Inspection result


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



Data Structure


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

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 );


Function Declaration


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 );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.