Mouse Programming with libgpm

A description of how to work with gpm for event handling, menus and more.
Global Variables

Let's look at some global variables that are essential in any program using gpm libraries. I have used some of them in the above examples, so let me explain some important ones here. The explanations also contain tricks to make some programs work.

  • gpm_fd: This is the file descriptor used to connect to the server. Though we may never need to use it directly, this is useful in few occasions. The gpm_fd will be -2 if we run the programs in an xterm. It is useful to diagnose whether the program is running in an xterm or on the console. Another use is to select it and wait for input. We can fire select call on gpm_fd to see when the data becomes available and then use Gpm_GetEvent() to get events.

  • gpm_zerobased: This variable can be set to 1 to force gpm to return zero-based coordinates of the mouse. It is off(0) by default.

  • gpm_visiblepointer: This variable can be set to 1 to force the pointer to be visible all the time. The pointer becomes invisible when keyboard focus is on. Especially when curses are initialized, the pointer becomes invisible. So we need to force gpm to show the pointer by setting this variable to 1.

  • gpm_hflag & gpm_morekeys: gpm_hflag is used to decide if the key code generated is from the mouse or not. It is useful when generating key codes for mouse events. gpm_morekeys is useful when we want to generate more than one key code for the event. When this variable is set, the mouse handler is invoked again for the event.

  • gpm_roi: This is the linked list of the regions in the stack; it's a pointer to the top. With this variable we can do lot of weird things any time, any where.

  • gmp_current_roi: This is the pointer to the region that received the last event. I usually use this for debugging purposes. While interested in regions on only the top of the stack, I always want to know where the mouse pointer is wandering. This variable comes to my rescue for such applications.


I would like to thank my friend Ravi Parimi for his help in editing this document. I am grateful to him for his constant encouragement and motivation.

Pradeep Padala is a software engineer for Hughes Software Systems. He has a degree in Computer Science from MNREC, Allahabad, India.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Mouse programming

ajayan's picture

really good stuff ,
thanks to author , i got this at right time

Re: Mouse Programming with libgpm

Anonymous's picture

I tried the sample progam but the #includes on

the website did not appear on my netscape.

So I used the obvious includes and it worked

just fine. Here are the includes you need

without angle brackets that this web site can't

digest. (Webmaster, please work on accepting

plain text in comments, we're not all MS junkies






However, after exiting, the sample program, I

could not cut and paste as before to or from that

one xterm window. All other windows, xterm or

GUI, were unaffected. I don't know what this

means but I know it works in Xfree86 using Gnome.

I assume that means it works as well on the CRT

80x24 screen as well.

This might be something to follow up on for

anybody writing sam and smit type console programs

for linux administration. I know I hate using the

GUI with pictures, where you don't know if the

little birdie icon means add or delete. I like

the text based GUI that spells it out so you know

what you get when you press return, or maybe, now,

if you click on the highlighted text.

Re: Mouse Programming with libgpm

Anonymous's picture

Which sample program you had problems? I checked all of them on plain console and I have no problems


Re: Mouse Programming with libgpm

Anonymous's picture

This is that code.... after exiting .... just see whether cut copy paste with mouse works ... on the same xterm where u executed this program .


int my_handler(Gpm_Event *event, void *data)
{ printf("Event Type : %d at x=%d y=%d
", event->type, event->x, event->y);
return 0;

int main()
{ Gpm_Connect conn;
int c;

conn.eventMask = ~0; /* Want to know about all the events */
conn.defaultMask = 0; /* don't handle anything by default */
conn.minMod = 0; /* want everything */
conn.maxMod = ~0; /* all modifiers included */

if(Gpm_Open(&conn, 0) == -1)
printf("Cannot connect to mouse server

gpm_handler = my_handler;
while((c = Gpm_Getc(stdin)) != EOF)
printf("%c", c);
return 0;

How to capture special keys like F1 F2 etc...

suneel's picture

Gpm_Getc does not appear to be returning F1 F2... etc other keystrokes. Do u know how to capture those apart fromthis mouse and regular key strokes.