/****************************************************************************
MOUSE CONTROL FUNCTIONS
Copyright (c) Gary Wood All Rights Reserved
Not for Commercial or Business Use with Permission of Gary Wood,
5401 Lakeview Road, North Little Rock, Arkansas  72116

Use the /DOS2 option to set the MSC 6.0 compiler to use OS/2 calls

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

#ifndef OS2
#include <dos.h>
#include <bios.h>
#endif
#define MLCHDRS 1
#include <mouse.h>

#define ON    1
#define OFF   0
#define BW40  0
#define CO40  1
#define BW80  2
#define CO80  3
#define MONO  7

static int MOUSE_V_MODE = 3;	/* Video Mode of Monitor */

/****************************************************************************/
/*  Initialize Mouse				 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_open(row,col,mask)					    */
/*	   row = integer initial row position of cursor 		    */
/*	   col = integer initial column position of cursor		    */
/*	   mask = what functions of mouse to support (see OS/2 Technical    */
/*		  Reference for MouSetEventMask()			    */
/*									    */
/*  Returns:  OS/2 = handle (0xff = not functional)			    */
/*									    */
/****************************************************************************/
int _cdecl mouse_open(row,col,mask)
int row,col;
unsigned int mask;
{
#ifdef OS2
VIOMODEINFO ModData;

	MOUSE_HANDLE = 0xff;

	ModData.cb = sizeof(VIOMODEINFO);
	VioGetMode((VIOMODEINFO far *) &ModData,0);
	if (ModData.col == 40)
		if (ModData.fbType & 0x01)
			MOUSE_V_MODE = CO40;
		else
			MOUSE_V_MODE = BW40;
	else
	if (ModData.col == 80)
		if (ModData.fbType & 0x01)
			MOUSE_V_MODE = CO80;
		else
			MOUSE_V_MODE = BW80;
	else
		MOUSE_V_MODE = CO80;
	MouOpen(0L, (unsigned far *) &MOUSE_HANDLE);
	if (MOUSE_HANDLE != 0xff) {
		MOUSE_POS.row = row;		     /* Second Row	*/
		MOUSE_POS.col = col;		     /* Column position */
		MouSetPtrPos((PTRLOC far *) &MOUSE_POS, MOUSE_HANDLE);
		MOUSE_EVENT_MASK = mask;	     /* Set mask	*/
		MouSetEventMask((unsigned far *) &MOUSE_EVENT_MASK, MOUSE_HANDLE);
		MOUSE_READ_TYPE = MOU_NOWAIT;	       /* no wait */
	}
#else
union REGS xregs;

	MOUSE_HANDLE = 0xff;
	if (_dos_getvect(0x33)) {
		xregs.x.ax = 0x0F00;
		int86(0x10,&xregs,&xregs);
		MOUSE_V_MODE = xregs.x.ax & 0xFF;
		xregs.x.ax = 0x00;
		int86(0x33,&xregs,&xregs);
		if (xregs.x.ax == -1) {
			xregs.x.ax = 0x04;
			xregs.x.cx = col * 8;
			xregs.x.dx = row * 8;
			int86(0x33,&xregs,&xregs);
			xregs.x.ax = 0x000a;
			xregs.x.bx = 0x0000;
			xregs.x.cx = 0x77ff;
			xregs.x.dx = 0x7700;
			int86(0x33,&xregs,&xregs); /* set shape */
			MOUSE_HANDLE = 0x00;
		}
	}
#endif
	return(MOUSE_HANDLE);

}
/****************************************************************************/
/*  WAIT FOR KEYSTROKE				 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_wait_ki()						    */
/*									    */
/*  Returns:  0 = Mouse button not pressed before keystroke		    */
/*	      0x04 = Button 1 Pressed					    */
/*	      0x10 = Button 2 Pressed					    */
/*									    */
/****************************************************************************/
int _cdecl mouse_wait_ki()
{
int fs;

	if (MOUSE_HANDLE != 0xff)
		while (!mouse_ki_chk())
			if ((fs = mouse_read(ON)))
				if (MOUSE_EVENT_BUFFER.fs & MOUSE_BN1_DOWN ||
				    MOUSE_EVENT_BUFFER.fs & MOUSE_BN2_DOWN)
					return(fs);
	return(OFF);
}

/****************************************************************************/
/*  READ  MOUSE 				 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_read(flag)						    */
/*	   flag = Wait for mouse press when = 1 			    */
/*									    */
/*  Will set the information in structure MOUEVENTINFO to reflect	    */
/*  the button press, row, and column location of cursor. (see mouse.h)     */
/*									    */
/*  Returns:  0x00 = No Buttons Pressed 				    */
/*	      0x04 = Button 1 Pressed					    */
/*	      0x10 = Button 2 Pressed					    */
/*									    */
/****************************************************************************/
int _cdecl mouse_read(flag)
int flag;
{
#ifdef OS2
MOUEVENTINFO work_event_buffer;   /* Mouse event buffer */
#else
union REGS xregs;
#endif

	if (MOUSE_HANDLE == 0xff) return(-1);

#ifdef OS2
	MouReadEventQue((MOUEVENTINFO far *) &work_event_buffer,
	   (unsigned far *) &MOUSE_READ_TYPE,MOUSE_HANDLE);
	if (work_event_buffer.time != 0L) {
		MOUSE_NULL = OFF;
		MOUSE_EVENT_BUFFER.fs = work_event_buffer.fs;
		MOUSE_EVENT_BUFFER.col = work_event_buffer.col;
		MOUSE_EVENT_BUFFER.row = work_event_buffer.row;
	} else
		MOUSE_NULL = ON;
#else
	MOUSE_EVENT_BUFFER.fs = 0;
	xregs.x.ax = 0x03;
	int86(0x33,&xregs,&xregs);
	if (xregs.x.bx) {
		MOUSE_NULL = OFF;
		if (xregs.x.bx & 0x0001)	/* Button 1 */
			MOUSE_EVENT_BUFFER.fs = MOUSE_EVENT_BUFFER.fs | MOUSE_BN1_DOWN;
		if (xregs.x.bx & 0x0002)	/* Button 2 */
			MOUSE_EVENT_BUFFER.fs = MOUSE_EVENT_BUFFER.fs | MOUSE_BN2_DOWN;
		if (flag == ON)
			while (xregs.x.bx) {
				xregs.x.ax = 0x03;
				int86(0x33,&xregs,&xregs);
			}
	} else
		MOUSE_NULL = ON;

	switch(MOUSE_V_MODE) {
		case(0):
		case(1):
			MOUSE_EVENT_BUFFER.row = xregs.x.dx / 8;
			MOUSE_EVENT_BUFFER.col = xregs.x.cx / 16;
			break;
		case(2):
		case(3):
		case(7):
			MOUSE_EVENT_BUFFER.row = xregs.x.dx / 8;
			MOUSE_EVENT_BUFFER.col = xregs.x.cx / 8;
	}
#endif

	return(MOUSE_EVENT_BUFFER.fs);
}

/****************************************************************************/
/*  RETURN RELEASE COUNTER			 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_release(button)					    */
/*	   button = button to check 0 = button 1 , 1 = button 2 	    */
/*									    */
/*  Used in conjunction with mouse_read(0) function to know when the	    */
/*  button is released. 						    */
/*									    */
/*  Returns:  0x00 = No Buttons Pressed 				    */
/*	      0x01 = Button 1 Pressed					    */
/*	      0x02 = Button 2 Pressed					    */
/*									    */
/****************************************************************************/
int _cdecl mouse_release(button)
int button;
{
int rc;
#ifndef OS2
union REGS xregs;
#endif

	rc = OFF;
	if (MOUSE_NULL == OFF) {
		MOUSE_NULL = ON;
		switch (button) {
			case 0: if (MOUSE_EVENT_BUFFER.fs & MOUSE_BN1_DOWN)
					rc = 1;
				break;
			case 1: if (MOUSE_EVENT_BUFFER.fs & MOUSE_BN2_DOWN)
					rc = 2;
		}
	}
#ifndef OS2
	if (rc) {
		xregs.x.ax = 0x03;
		int86(0x33,&xregs,&xregs);
		while (xregs.x.bx) {
			xregs.x.ax = 0x03;
			int86(0x33,&xregs,&xregs);
		}
	}
#endif
	return(rc);
}
/****************************************************************************/
/*  MOUSE POSITION					DOS VERSION ONLY    */
/*									    */
/*  Call:  mouse_position(row,col)					    */
/*	   row = integer initial row position of cursor 		    */
/*	   col = integer initial column position of cursor		    */
/*									    */
/****************************************************************************/
void _cdecl mouse_position(row,col)
int row,col;
{
#ifndef OS2
union REGS xregs,oregs;

	if (MOUSE_HANDLE != 0xff) {
		switch(MOUSE_V_MODE) {
			case(0):
			case(1):
				xregs.x.dx = row * 8;
				xregs.x.cx = col * 16;
				break;
			case(2):
			case(3):
			case(7):
				xregs.x.dx = row * 8;
				xregs.x.cx = col * 8;
		}
		xregs.x.ax = 0x04;
		int86(0x33,&xregs,&oregs);
	}
#endif
}

/****************************************************************************/
/*  CLOSE MOUSE 				 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_close()						    */
/*									    */
/****************************************************************************/
void _cdecl mouse_close()
{
#ifdef OS2
	if (MOUSE_HANDLE != 0xff)
		MouClose(MOUSE_HANDLE);
#else
union REGS xregs;

	if (MOUSE_HANDLE != 0xff) {
		xregs.x.ax = 0x02;
		int86(0x33,&xregs,&xregs);
	}
#endif
	MOUSE_HANDLE = 0xff;
}

/****************************************************************************/
/*  SHOW MOUSE					 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_show() 						    */
/*									    */
/*  Always hide the mouse with the mouse_hide() function before changing    */
/*  the screen.  Then use the mouse_show() function to redisplay the	    */
/*  cursor.								    */
/*									    */
/****************************************************************************/
void _cdecl mouse_show()
{
#ifndef OS2
union REGS xregs;

	if (MOUSE_HANDLE != 0xff) {
		xregs.x.ax = 0x0001;
		int86(0x33,&xregs,&xregs); /* show cursor */
	}
#else
	if (MOUSE_HANDLE != 0xff)
		MouDrawPtr(MOUSE_HANDLE);
#endif
}

/****************************************************************************/
/*  HIDE MOUSE					 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_hide() 						    */
/*									    */
/*  Always hide the mouse with the mouse_hide() function before changing    */
/*  the screen.  Then use the mouse_show() function to redisplay the	    */
/*  cursor.								    */
/*									    */
/****************************************************************************/
void _cdecl mouse_hide()
{
#ifndef OS2
union REGS xregs;

	if (MOUSE_HANDLE != 0xff) {
		xregs.x.ax = 0x0002;
		int86(0x33,&xregs,&xregs); /* show cursor */
	}
#else
NOPTRRECT rptr;
VIOMODEINFO ModData;

	if (MOUSE_HANDLE != 0xff) {
		ModData.cb = sizeof(VIOMODEINFO);
		VioGetMode((VIOMODEINFO far *) &ModData,0);
		rptr.row = rptr.col = 0;
		rptr.cRow = ModData.row-1;
		rptr.cCol = ModData.col-1;
		MouRemovePtr((NOPTRRECT far *) &rptr,MOUSE_HANDLE);
	}
#endif
}

/****************************************************************************/
/*  CHECK KEYSTOKE AVAILABLE			 OS/2 & DOS VERSION	    */
/*									    */
/*  Call:  mouse_ki_chk()						    */
/*									    */
/*  Returns: 1 = Keystroke available					    */
/*	     0 = Waiting for Keystroke to be entered			    */
/*									    */
/****************************************************************************/
int _cdecl mouse_ki_chk()
{
#ifdef OS2
KBDKEYINFO CharData;

	KbdPeek((char far *) &CharData,0);
	if (CharData.fbStatus & 0x40)
		return(1);
	return(0);
#else
    if (_bios_keybrd(_KEYBRD_READY))	/* check buffer 		      */
	    return(1);			/*return 0 if no code in buffer       */
    return(0);
#endif
}
