100 likes | 213 Views
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
E N D
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 • Requires that the gpm daemon is running • Type ‘info gpm’ to see official information • Also an online article by Pradeep Padala
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)
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
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
Useful fields in Gpm_Event Gpm_Event *evt; evt->type == 1: // indicates a mouse-move evt->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)
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();
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
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
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?