/*--------------------------------+
 | MOUSE.C                        |
 | Useful mouse functions for VGA |
 | by Alex Matulich               |
 | Unicorn Research Corporation   |
 +--------------------------------*/

/* Compile this and link with any program.  Should work with just about
   any compiler, perhaps with some syntax modification for
   int86() and int86s() */

#include "mouse.h"

/* crosshair with a blank space in the center */
static unsigned short mousecursormask[32] = {
/*	16 words screen mask, 16 words cursor mask
   each word defines the sixteen pixels of a row, low bit rightmost */
   0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
   0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
   0,0,0x80,0x80,0x80,0x80,0x80,0,0x3e3e,
   0x00,0x80,0x80,0x80,0x80,0x80,0
   };

static union REGS reg;    /* normal x and h registers */
static union REGSS regs;  /* includes ES register */

/*-------------------------------------------------------------------------*/

/* check availability of mouse driver, reset it if it's there, or
   return 0 if the driver isn't installed. */
int resetmouse()
{
reg.x.ax = 0;
int86(0x33, &reg, &reg);
return reg.x.ax;  /* FFFF if installed, 0 otherwise */
/* bx = FFFF for 2 buttons, 0 for other than 2, 3 for Mouse Systems mouse */
}

/*-------------------------------------------------------------------------*/

/* set up mouse cursor images and other things -- call after resetmouse() */
void setupmouse()
{
/* set up the text cursor */
reg.x.ax = 0x0a;
reg.x.bx = 1;  /* 0=software cursor, 1=hardware cursor */
reg.x.cx = 0;  /* start scan line (hardware), or screen mask (software) */
reg.x.dx = 15; /* end scan line (hardware), or cursor mask (software) */
/* when the software cursor is selected, the char/attribute data at the
   current screen position is ANDed with the screen mask and then XORed
   with the cursor mask. */
int86(0x33, &reg, &reg);

/* set up the graphics cursor */
regs.x.ax = 9;
regs.x.bx = regs.x.cx = 8;  /* column & row of cursor hot spot (-16 to 16) */
regs.x.es = FP_SEG(mousecursormask);
regs.x.dx = FP_OFF(mousecursormask);
int86s(0x33, &regs, &regs);

/* let the mouse travel over the full VGA screen */
reg.x.ax = 7;     /* define horizontal cursor range */
reg.x.cx = 0;     /* minimum column */
reg.x.dx = 639;   /* maximum column */
int86(0x33, &reg, &reg);
reg.x.ax = 8;     /* define vertical cursor range */
reg.x.cx = 0;     /* minimum row */
reg.x.dx = 479;   /* maximum row */
int86(0x33, &reg, &reg);
}

/*-------------------------------------------------------------------------*/

/* return row and column where a designated mouse button was pressed */
int mouse_activity(int button, int *col, int *row)
{
reg.x.ax = 5;       /* return button press data (6 = button release data) */
reg.x.bx = button;  /* button to test (0=left, 1=right, 2=middle) */
int86(0x33, &reg, &reg);
if (reg.x.bx) {    /* number of times button was pressed since last call */
   *col = reg.x.cx;
   *row = reg.x.dx;
   }
return reg.x.bx;
}

/*-------------------------------------------------------------------------*/

/* get position and button status of mouse */
int mouse_status(int *col, int *row)
{
reg.x.ax = 3;
int86(0x33, &reg, &reg);
*col = reg.x.cx;
*row = reg.x.dx;
return reg.x.bx;  /* button status (bit0=left, bit1=right, bit2=middle) */
}

/*-------------------------------------------------------------------------*/

/* make the mouse cursor visible */
void showmouse()
{
reg.x.ax = 1;
int86(0x33, &reg, &reg);
}

/* make the mouse cursor invisible (undo repeated calls with showmouse()) */
void hidemouse()
{
reg.x.ax = 2;
int86(0x33, &reg, &reg);
}
