/*****************************************************************************


				MOUSE.C

		This file contains mouse routines for menus.


*****************************************************************************/
	
#include <dos.h>
#include <std.h>
#include <stdlib.h>
#include <stdio.h>
#include "mouse.h"

#define MOUSEINT	51


int mousebuttons = (-1);

int mousestat()
 {
 union REGS regs, r2;

 regs.x.ax = 0;
 int86(MOUSEINT, &regs, &regs);
 r2.x.ax = 33;
 r2.x.bx = 0;
 int86(MOUSEINT, &r2, &r2);
 mousebuttons = regs.x.ax ? (int)regs.x.bx : 1;
 return regs.x.ax ? 0 : (-1);
}

BOOL mousecur(on)
 BOOL on;
{
 static BOOL last = FALSE;
 union REGS regs;
 BOOL tmp;

 if(on != last)	{
	regs.x.ax = on ? 1 : 2;
	int86(MOUSEINT, &regs, &regs);
 	}
 tmp = last;
 last = on;
 return tmp;
}

PT mousepos()
{
 union REGS regs;
 PT xy;

 regs.x.ax = 3;
 int86(MOUSEINT, &regs, &regs);
 xy.x = regs.x.cx;
 xy.y = regs.x.dx;

 return xy;
}

PT mousecpos()
{
 PT p, r;

 p = mousepos();
 r = mouseres();

 p.x /= r.x; p.y /= r.y;

 return p;
}

void mousecput(pt)
 PT pt;
{
 PT r;
 union REGS regs;

 r = mouseres();
 regs.x.ax = 4;
 regs.x.cx = pt.x*r.x + r.x/2;
 regs.x.dx = pt.y*r.y + r.y/2;
 int86(MOUSEINT, &regs, &regs);
}


static PT cursorsize = {8, 8};

PT mouseclick(enum whichbutton n)
{
 union REGS regs;
 PT xy;

 regs.x.ax = 5;
 regs.x.bx = n;
 int86(MOUSEINT, &regs, &regs);

 if(regs.x.bx == 0)	{
 	xy.x = -cursorsize.x;
	xy.y = -cursorsize.y;
	return xy;
	}

 xy.x = regs.x.cx;
 xy.y = regs.x.dx;

 return xy;
 }


PT mousecclick(enum whichbutton n)
{
 PT p, r;

 p = mouseclick(n);
 r = mouseres();

 p.x /= r.x; p.y /= r.y;

 return p;
}

void mousectype(hardware, start, stop)
 BOOL hardware;
 int start, stop;
{
 union REGS regs;

 regs.x.ax = 10;
 regs.x.bx = hardware ? 1 : 0;
 regs.x.cx = start;
 regs.x.dx = stop;
 int86(MOUSEINT, &regs, &regs);

 cursorsize.x = cursorsize.y = 8;
}

PT mouseres() { return cursorsize; }


void mousereset(mode)
 int mode;
{
 if ((mode > 6) && (mode != 0x40))	{
	 printf("Mouse does not recognize graphics mode");
	 exit(1);
	 }

 if (mode == 0x40)	{
	 cursorsize.y = 16;
	 mousergn(0, 640, 0, 400);
	 }
 else	{
	 cursorsize.y = 8;
	 mousergn(0, 640, 0, 200);
	 }
}

static struct mousergn_s region = {0, 0, 0, 0};

/* sets region xmin<=x<xmax, ymin<=y<ymax */
void mousergn(int xmin, int xmax, int ymin, int ymax)
{
 union REGS regs;

 regs.x.ax = 8;
 regs.x.cx = region.ymin = ymin;
 regs.x.dx = (region.ymax = ymax)-1;
 int86(MOUSEINT, &regs, &regs);
 regs.x.ax = 7;
 regs.x.cx = region.xmin = xmin;
 regs.x.dx = (region.xmax = xmax)-1;
 int86(MOUSEINT, &regs, &regs);
}

#ifdef c_plusplus
void mousergn(struct mousergn_s a)
{
mousergn(a.xmin, a.xmax, a.ymin, a.ymax);
}

struct mousergn_s mousergn() { return region;}
#endif

struct mousergn_s mousearea() {return region;}
