/*HUMBIRD v2.0 by C&P SOFTWARE************************************************
 *				        				     *
 *		This code is copywright 1988 by C&P SOFTWARE and is          *
 *	put in the public domain.  All or any part of this code is           *
 *	freely distributable and can be used in other programs with the      *
 *	agreement that this noticed must remain intact.			     *
 *									     *
 *****************************************************************************
 *			      DATA DECLARATIONS				     *
 *****************************************************************************/

#include "exec/types.h"
#include "intuition/intuition.h"
#include "intuition/intuitionBase.h"
#include "libraries/dos.h"

#define ADOS 	"AmigaDOS"
#define SYSPTS	i=0; i<POINTERSIZE; i++ 
#define PREFSIZE  232L

char   *Copyright = "Copyright 1988 by C&P SOFTWARE";
struct IntuitionBase *IntuitionBase,*OpenLibrary();
struct GfxBase *GfxBase;
struct Screen scren, *CusScrens;
struct Window *Wdw;
struct ViewPort *WVP,*ViewPortAddress();
struct   Preferences	OurPrefs;

UWORD  PSprite_Data[4][44] = {
	{  /* Humbird wings up */
	0,0,
	0x0000,0x0000,0x0000,0x0000,0x0001,0x0000,
	0x0006,0x0000,0x1C0E,0x0004,0x363C,0x0010,0xF3F8,0x0C20,
	0x17FB,0x1818,0x07FE,0x0400,0x03F8,0x0300,0x00F0,0x00F0,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xFFFB,0x00B0,
	0x0E00,0x0777,
	0,0
	},
	{  /* Humbird wings 1/4 */
	0,0,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x1C1F,0x0010,0x363C,0x0024,0xF3F0,0x0C90,
	0x17F8,0x1870,0x07FF,0x0400,0x03F8,0x0200,0x00F0,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xFFFB,0x00B0,
	0x0E00,0x0777,
	0,0,
	},
	{  /* Humbird wings 1/2 */
	0,0,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x1C00,0x0000,0x3600,0x0000,0xF3E0,0x0C00,
	0x17F8,0x18C0,0x07FF,0x0460,0x03FC,0x0230,0x00FF,0x0008,
	0x0003,0x0002,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xFFFB,0x00B0,
	0x0E00,0x0777,
	0,0
	},
	{  /* Humbird wings down */
	0,0,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x1C00,0x0000,0x3600,0x0000,0xF3E0,0x0C00,
	0x17FB,0x1980,0x07FE,0x0480,0x03F8,0x0260,0x00F8,0x0010,
	0x000C,0x0008,0x0006,0x0004,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xFFFB,0x00B0,
	0x0E00,0x0777,
	0,0
	},
};

UWORD  Blank_Data[44] = {  /* Humbird gone */
	0,0,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
	0x0000,0x0000,
	0,0
	};

USHORT PointerData[POINTERSIZE];
int    i, ok = 0;
short  mx,my;
ULONG lv;

_main() /* No parameters, so why not save some space */
{
char *strcmp();
char WTitle[9];

	SetTaskPri(FindTask(NULL),-2L); /* not to interfere with DOS */
	strcpy(WTitle,ADOS);

	/********************** Standard Library set up **********************/
	IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L);
	if(!IntuitionBase) cleanup();
	GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L);
	if(!GfxBase) cleanup();

	/***************** Magic to get first screen pointer *****************/
	ok = GetScreenData(&scren,sizeof(struct Screen),WBENCHSCREEN,NULL);
	if (!ok) cleanup();

	/**** Get and save system prefs so we can restore them upon exit *****/
	GetPrefs (&OurPrefs, PREFSIZE );
	for(SYSPTS)
		PointerData[i] = OurPrefs.PointerMatrix[i];

	/***** Loop thru all system windows to find the AmigaDOS window ******/
	Wdw = scren.FirstWindow;
	while (Wdw)
		{
		if (strcmp(Wdw->Title ,WTitle)  == 0)
			break;
		Wdw = Wdw->NextWindow;
	}
	if(!Wdw) cleanup(); /* No AmigaDOS window */

	/*********************** Set up the colors ***************************/
	WVP = (struct ViewPort *)ViewPortAddress(Wdw);
	SetColors();

	/********************** Get the  mouse x/y ***************************/
	mx = Wdw -> MouseX;
	my = Wdw -> MouseY;

	/******************** Initial humbird display ************************/
	lv = LockIBase(0L);
	SetPointer(IntuitionBase->ActiveWindow,&PSprite_Data[0],18,16,0,-6);
	UnlockIBase(lv);
	setp();
}

setp()
{
int x, mm = 0;
short tmx, tmy, off;
ULONG Seconds, Micros, OldSecs, OldMics;

	do{	/* CurrentTime bug ??? */
		CurrentTime(&OldSecs,&OldMics); /* Get initial time */
	}while(OldSecs<=0 || OldMics<=0);
	while(1){  /* FOREVER */
		for(x=0;x<=3;){
			tmx = Wdw -> MouseX;
			tmy = Wdw -> MouseY;
			if((tmx != mx) || (tmy != my)){ /* Mouse moved... */
				x = x + 1;
				mx = tmx; my = tmy;
				mm = 1; off = 0;  /* set move & mouseoff */
				do{  /* reset time */
					CurrentTime(&OldSecs,&OldMics);
				}while(OldSecs<=0 || OldMics<=0);
			}
			else{ /* No mouse movement */
				do{ /* Get time to compare with old time */
					CurrentTime(&Seconds,&Micros);
				}while(Seconds<=0 || Micros<=0);
				if(((Seconds - 10) > OldSecs) && (!off)){
					lv = LockIBase(0L); /* time > 10 secs and mouse is on */
					SetPointer(IntuitionBase->ActiveWindow,&Blank_Data,18,16,0,-6);
					UnlockIBase(lv);
					SetColors();
					off = 1; /* mouse is off */
				}	
			}
			if((x >= 0) && (x <= 3) && (mm)){ /* mouse moved = change pointer. */
				lv = LockIBase(0L);
				SetPointer(IntuitionBase->ActiveWindow,&PSprite_Data[x],18,16,0,-6);
				UnlockIBase(lv);
				mm = 0;
			}
			Chk_Abort(); /* Was the program aborted? */
		}

	}
}

cleanup()
{

	/************* Close all our stuff and reset the pointer *************/
	ok = 0;
	ok = GetScreenData(&scren,sizeof(struct Screen),WBENCHSCREEN,NULL);
	if (ok){
		Wdw = scren.FirstWindow;
		while (Wdw)
			{
			ClearPointer(Wdw);
			Wdw = Wdw->NextWindow;
		}
		CusScrens = scren.NextScreen;
		while(CusScrens){
			Wdw = CusScrens->FirstWindow;
			while (Wdw)
				{
				ClearPointer(Wdw);
				Wdw = Wdw->NextWindow;
			}
			CusScrens = CusScrens->NextScreen;
		}
	}
	if(IntuitionBase) CloseLibrary(IntuitionBase);
	if(GfxBase) CloseLibrary(GfxBase);
	exit(0);
}

Chk_Abort()
{
	if (SetSignal(0L, (ULONG) (SIGBREAKF_CTRL_C)) &
	    (SIGBREAKF_CTRL_C)) {
		resetptr(PointerData);
		cleanup();
	}
}

resetptr(data) /* reset system colors */
USHORT data[];
{
   for(SYSPTS)
	OurPrefs.PointerMatrix[i] = (data) ? data[i] : NULL;
   SetPrefs(&OurPrefs,PREFSIZE);
}
   
SetColors()
{
	SetRGB4(WVP,17,0,8,2);
	SetRGB4(WVP,18,11,0,0);
	SetRGB4(WVP,19,9,11,0);
}
