Mouse Programming in C/C++
Till now we have seen only static output on the black window in C/C++ without any peripheral device interaction(like mouse). Here static means that interaction with the output screen through the mouse to run a dynamic event or task. The goal will be to make the pointer of the mouse visible on our output screen, through which it can see any new event when the mouse is clicked on the same output window.
Requirement: Turbo C++ IDE or DOS BOX
The idea is to tell a mouse to do anything on the output screen. In actual the communication with the mouse directly is not possible but through the driver provided. The idea is to use interrupts to get access to this driver. Each device provided by a computer has a unique port which is a hexadecimal value that is designed to be machine-independent enhancing the portability of the program. Mouse has port 0X33 attached to it. Use of address registers is also required to access these port. These are basically UNION of type REGS defined in “dos.h“. Use two registers to communicate to a device driver one for input and one for output and send value to device driver through the input register and receive information in it embedded in the output register.
Now there are two ways to display mouse pointer on C/C++ screen. First is the non-graphic mode and the second is Graphic mode, Here we use graphic mode. To switch our output window in Graphic mode steps are listed below:
Enable Graphic mode: For enabling the graphics mode use initgraph() function which is used to initialize the graphics mode. This function is present in “graphics.h“ header file.
Syntax of initgraph():
void initgraph(int *gdriver, int *gmode, char *pathtodriver);
- gdriver: It is an integer that specifies which graphics driver is to be used. Use DETECT it means compiler auto select the suitable driver according to requirement.
- gmode: It is also an integer that specifies the initial graphics mode. when gdriver = DETECT In this case, initgraph() sets gmode to the highest resolution available for the detected driver.
- pathtodriver: It denotes the directory path where initgraph must look for graphic drivers.
Prerequisite for Mouse Programming:
AX Register: The various mouse functions can be access using different values of AX input Register and passing those values to mouse port using an interrupt. Functions are listed below in the table:
Here AX, BX, CX, and DX are members of UNION REGS.
int86() function: The int86() is a C library function that facilitates access to bare bone DOS and BIOS service interrupts. It is a wrapper over inline assembly interrupt call. It takes CPU register values with object to a structure where member variables are equivalent to CPU registers. It takes three arguments.
// Declaration syntax
int int86(int intno, union REGS* inregs, union REGS* outregs);
// intno – Interrupt vector /service number
// inregs – Input argument registers as REGS
// outregs – Output argument registers as REGS
Use Cases of Mouse Programming:
There are various use cases of the Mouse Programming that are listed below:
- Detecting Mouse
- Display mouse pointer in graphics mode
- Hide pointer
- Determine Current position
- Identified which mouse button is clicked
- Restrict mouse pointer
- Free hand drawing (Use of all function)
Below is the program to check if a mouse driver is loaded or not: