/* ------------------------------------------------------------------------ */
/*                                sstmouse.c                                */
/*                         low-level mouse routines                         */
/*                                                                          */
/*      CopyRight (C) 1991,1992  Steven Lutrov.   All rights reserved.      */
/* ------------------------------------------------------------------------ */
#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>

#include "sstvid.h"
#include "sstmou.h"

/* ------------------------------------------------------------------------ */
/*                               timer macros                               */
/* ------------------------------------------------------------------------ */
#define timedout(timer)          (timer==0)
#define settimer(timer, secs)    timer=(secs)*182/10+1
#define timerdisabled(timer)     (timer == -1)
#define disabletimer(timer)      timer = -1
#define timerrunning(timer)      (timer > 0)
#define countdown(timer)         --timer

#define TIMER                  8
#define DOUBLETICKS            5


int sstmoused = 0;
int swidth = SCREENWIDTH;

/* ------------------------------------------------------------------------ */
/*                        local function prototypes                         */
/* ------------------------------------------------------------------------ */
static void near mouse     (int m1,int m2,int m3,int m4);
static void (interrupt far *oldtimer)(void);  /* set interrupt handler */


/* ------------------------------------------------------------------------ */
/*                              local variables                             */
/* ------------------------------------------------------------------------ */
static int msclicktimer = -1;                 /* set to clear timer */


/* ------------------------------------------------------------------------ */
/*            main mouse interrupt routine used by other functions          */
/* ------------------------------------------------------------------------ */
static void near mouse(int m1,int m2,int m3,int m4)
{
    _DX = m4;
    _CX = m3;
    _BX = m2;
    _AX = m1;
    geninterrupt(MOUSE);
}

/* ------------------------------------------------------------------------ */
/*                             reset the mouse                              */
/* ------------------------------------------------------------------------ */
void msreset(void)
{
    mouse(0,0,0,0);
}

/* ------------------------------------------------------------------------ */
/*                 test to see if the mouse driver is installed             */
/* ------------------------------------------------------------------------ */
int msinstalled(void)
{
    unsigned char far *ms;

    ms = MK_FP(peek(0, MOUSE*4+2), peek(0, MOUSE*4));
    return  (swidth <= 80 && ms != NULL && *ms != 0xCF);
}

/* ------------------------------------------------------------------------ */
/*                  return true if mouse buttons are pressed                */
/* ------------------------------------------------------------------------ */
int mspressed(void)
{
    if (msinstalled())
        mouse(3,0,0,0);
    return _BX & 3;
}

/* ------------------------------------------------------------------------ */
/*                          return mouse coordinates                        */
/* ------------------------------------------------------------------------ */
void msgetpos(int *x, int *y)
{
    if (msinstalled())    {
        mouse(3,0,0,0);
	*x = _CX / 8;
	*y = _DX / 8;
		if (swidth == 40)
			*x /= 2;
    }
}

/* ------------------------------------------------------------------------ */
/*                         position the mouse cursor                        */
/* ------------------------------------------------------------------------ */
void mssetpos(int x, int y)
{
    if (msinstalled())	{
		if (swidth == 40)
			x *= 2;
        mouse(4,0,x*8,y*8);
	}
}

/* ------------------------------------------------------------------------ */
/*                          display the mouse cursor                        */
/* ------------------------------------------------------------------------ */
void msshow(void)
{
    if (msinstalled())
        mouse(1,0,0,0);
}

/* ------------------------------------------------------------------------ */
/*                           hide the mouse cursor                          */
/* ------------------------------------------------------------------------ */
void mshide(void)
{
    if (msinstalled())
        mouse(2,0,0,0);
}

/* ------------------------------------------------------------------------ */
/*               return true if a mouse button has been released            */
/* ------------------------------------------------------------------------ */
int msreleased(void)
{
    if (msinstalled())
        mouse(6,0,0,0);
    return _BX;
}

/* ------------------------------------------------------------------------ */
/*                          set mouse travel limits                         */
/* ------------------------------------------------------------------------ */
void mssettravel(int minx, int maxx, int miny, int maxy)
{
    if (msinstalled())	{
		if (swidth == 40)	{
			minx *= 2;
			maxx *= 2;
		}
		mouse(7, 0, minx*8, maxx*8);
		mouse(8, 0, miny*8, maxy*8);
	}
}

/* ------------------------------------------------------------------------ */
/*                       timer interrupt service routine                    */
/* ------------------------------------------------------------------------ */
static void interrupt far newtimer(void)
{
    if (timerrunning(msclicktimer))
	countdown(msclicktimer);
    oldtimer();
}

/* ------------------------------------------------------------------------ */
/*                   test if left button was double pressed                 */
/* ------------------------------------------------------------------------ */
int msdleftpressed(void)

{
    if (oldtimer == NULL)    {
	oldtimer = getvect(TIMER);
	setvect(TIMER, newtimer);
    }
    if (msreleased())
	  msclicktimer = DOUBLETICKS;
    if (msleftpressed())    {
	    if (timerrunning(msclicktimer))    {
		disabletimer(msclicktimer);
		return (1);
	      }
	}
 return(0);
}
/* ------------------------------------------------------------------------ */
/*                   test if right button was double pressed                */
/* ------------------------------------------------------------------------ */
int msdrightpressed(void)

{
    if (oldtimer == NULL)    {
	oldtimer = getvect(TIMER);
	setvect(TIMER, newtimer);
    }
    if (msreleased())
	  msclicktimer = DOUBLETICKS;
    if (msrightpressed())    {
	    if (timerrunning(msclicktimer))    {
		disabletimer(msclicktimer);
		return (1);
	      }
	}
 return(0);
}



