10
On using the mouse A brief introduction to LIBGPM: the General Purpose Mouse programming interface

On using the mouse

Embed Size (px)

DESCRIPTION

On using the mouse. A brief introduction to LIBGPM: the General Purpose Mouse programming interface. The Linux “gpm” package. It’s a mouse server for the Linux console It “hides” details about mouse hardware Intended for use with text-based programs But we can use it with graphics programs - PowerPoint PPT Presentation

Citation preview

Page 1: On using the mouse

On using the mouse

A brief introduction to LIBGPM: the General Purpose Mouse

programming interface

Page 2: On using the mouse

The Linux “gpm” package

• It’s a mouse server for the Linux console

• It “hides” details about mouse hardware

• Intended for use with text-based programs

• But we can use it with graphics programs

• Requires that the gpm daemon is running

• Type ‘info gpm’ to see official information

• Also an online article by Pradeep Padala

Page 3: On using the mouse

Programming steps

• Your client application must establish a connection with the gpm server-daemon

• You need a header-file: #include <gpm.h>• You declare an important data-structure:

Gpm_Connect conn;• You will need to initialize its four fields• Then you call Gpm_Open( &conn, 0 );• Returns -1 if unsuccessful (otherwise 0)

Page 4: On using the mouse

Fields to be initialized

conn.eventMask = ~0;// events of interest

conn.defaultMask = 0;// to handle for you

conn.minMod = 0; // lowest modifier

conn.maxMod = ~0; // highest modifer

Page 5: On using the mouse

Responding to mouse activity

• You create your own ‘handler’ function for those mouse events that you wish to act upon

• Prototype of the handler-function is: int my_handler( Gpm_Event *evt,

void *my_data );• To install the handler, use this assignment:

gpm_handler = my_handler;• Whenever the mouse is moved, or its buttons

are pressed or released, your function executes

Page 6: On using the mouse

Useful fields in Gpm_Event

Gpm_Event*evt;evt->type == 1: // indicates a mouse-moveevt->x, evt->y: // current mouse ‘hot-spot’evt->dx, evt->dy: // changes in position(+/-)

NOTE: Remember that GPM was developed

for text-based applications, so the hot-spot coordinates are character-cell locations (not graphics-pixel locations)

Page 7: On using the mouse

A typical program loop

This loop allows normal keyboard input to continue being processed (e.g., echoed, buffered) while any mouse activities are processed by your handler (or else by a default handler supplied by the daemon)

int c;

While ( ( c = Gpm_Getc( stdin ) ) != EOF );

Gpm_Close();

Page 8: On using the mouse

A simple text-mode demo

• Pradeep Padala has published a short C program that illustrates ‘barebones’ usage of the gpm package (from Linux Journal)

• We have adapted his code for C++

• Our demo is called ‘trymouse.cpp’

• It’s compiled like this:

$ g++ trymouse.cpp –lgpm –o trymouse

Page 9: On using the mouse

A simple graphics demo

• We have created a minimal graphics demo

• It shows how you could use the mouse to move a ‘slider’ object (e.g.,in Pong game)

• It’s called ‘gpmslide.cpp’

• You compile it like this:$ g++ gpmslide.cpp –lgpm –o

gpmslide

Page 10: On using the mouse

In-class exercises

• Compile and run the ‘trymouse.cpp’ demo

• Compile and run the ‘gpmslide.cpp’ demo

• Look at the C++ source-code in the demos

• Can you replace the keyboard-based user controls (in your earlier pong animation) with mouse-based user-controls?

• Can you incorporate mouse-based control into your 3D wire-frame model animation?