Pointing Devices

Device drivers for pointing devices are supplied by the hardware manufacturer and are loaded with a DEVICE statement in the CONFIG.SYS file. Although the hardware characteristics of the available pointing devices differ greatly, nearly all of their drivers present the same software interface to application programs: the Int 33H protocol used by the Microsoft Mouse driver. Version 6 of the Microsoft Mouse driver (which was current as this was written) offers the following functions:

Function Meaning

00H Reset mouse and get status.

01H Show mouse pointer.

02H Hide mouse pointer.

03H Get button status and pointer position.

04H Set pointer position.

05H Get button-press information.

06H Get button-release information.

07H Set horizontal limits for pointer.

08H Set vertical limits for pointer.

09H Set graphics pointer type.

0AH Set text pointer type.

0BH Read mouse-motion counters.

0CH Install interrupt handler for mouse events.

0DH Turn on light pen emulation.

0EH Turn off light pen emulation.

0FH Set mickeys to pixel ratio.

10H Set pointer exclusion area.

13H Set double-speed threshold.

14H Swap mouse-event interrupt routines.

15H Get buffer size for mouse-driver state.

16H Save mouse-driver state.

17H Restore mouse-driver state.

18H Install alternate handler for mouse events.

19H Get address of alternate handler.

1AH Set mouse sensitivity.

1BH Get mouse sensitivity.

1CH Set mouse interrupt rate.

1DH Select display page for pointer.

1EH Get display page for pointer.

1FH Disable mouse driver.

20H Enable mouse driver.

21H Reset mouse driver.

22H Set language for mouse-driver messages.

23H Get language number.

24H Get driver version, mouse type, and IRQ number.

Although this list of mouse functions may appear intimidating, the average application will only need a few of them.

A program first calls Int 33H Function 00H to initialize the mouse driver for the current display mode and to check its status. At this point, the mouse is "alive" and the application can obtain its state and position; however, the pointer does not become visible until the process calls Int 33H Function 01H.

The program can then call Int 33H Functions 03H, 05H, and 06H to monitor the mouse position and the status of the mouse buttons. Alternatively, the program can register an interrupt handler for mouse events, using Int 33H Function 0CH. This latter technique eliminates the need to poll the mouse driver; the driver will notify the program by calling the interrupt handler whenever the mouse is moved or a button is pressed or released.

When the application is finished with the mouse, it can call Int 33H Function 02H to hide the mouse pointer. If the program has registered an interrupt handler for mouse events, it should disable further calls to the handler by resetting the mouse driver again with Int 33H Function 00H.

For a complete description of the mouse-driver functions, see Section III of this book, "IBM ROM BIOS and Mouse Functions Reference." Figure 5-3 shows a small demonstration program that polls the mouse continually, to display its position and status.

/*

Simple Demo of Int 33H Mouse Driver

Ó 1988 Ray Duncan

Compile with: CL MOUDEMO.C

*/

#include <stdio.h>

#include <dos.h>

union REGS regs;

void cls(void); /* function prototypes */

void gotoxy(int, int);

main(int argc, char *argv[])

{

int x,y,buttons; /* some scratch variables */

/* for the mouse state */

regs.x.ax = 0; /* reset mouse driver */

int86(0x33, &regs, &regs); /* and check status */

if(regs.x.ax == 0) /* exit if no mouse */

{ printf("\nMouse not available\n");

exit(1);

}

cls(); /* clear the screen */

gotoxy(45,0); /* and show help info */

puts("Press Both Mouse Buttons To Exit");

regs.x.ax = 1; /* display mouse cursor */

int86(0x33, &regs, &regs);

do {

regs.x.ax = 3; /* get mouse position */

int86(0x33, &regs, &regs); /* and button status */

buttons = regs.x.bx & 3;

x = regs.x.cx;

y = regs.x.dx;

gotoxy(0,0); /* display mouse position */

printf("X = %3d Y = %3d", x, y);

} while(buttons != 3); /* exit if both buttons down */

regs.x.ax = 2; /* hide mouse cursor */

int86(0x33, &regs, &regs);

cls(); /* display message and exit */

gotoxy(0,0);

puts("Have a Mice Day!");

}

/*

Clear the screen

*/

void cls(void)

{

regs.x.ax = 0x0600; /* ROM BIOS video driver */

regs.h.bh = 7; /* int 10h function 06h */

regs.x.cx = 0; /* initializes a window */

regs.h.dh = 24;

regs.h.dl = 79;

int86(0x10, &regs, &regs);

}

/*

Position cursor to (x,y)

*/

void gotoxy(int x, int y)

{

regs.h.dl = x; /* ROM BIOS video driver */

regs.h.dh = y; /* int 10h function 02h */

regs.h.bh = 0; /* positions the cursor */

regs.h.ah = 2;

int86(0x10, &regs, &regs);

}

Figure 5-3. MOUDEMO.C: A simple Microsoft C program that polls the mouse and continually displays the coordinates of the mouse pointer in the upper left corner of the screen. The program uses the ROM BIOS video driver, which is discussed in Chapter 6, to clear the screen and position the text cursor.