/* $VER: WBStars_main.c 2.0 (18 Dec 1996)
*/

#include <intuition/screens.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <clib/alib_protos.h>
#include <clib/macros.h>

#include "WBStars_include.h"
#include "WBStars_protos.h"
#include "WBStars_plot.h"

struct Screen		*wbench=NULL;
struct RastPort		*RPort;
struct Layer_Info	*LInfo;

long    _stack=4000;
char    *_procname="WBStars";
long    _priority=0;
long    _BackGroundIO=FALSE;

int		minx,miny,width,height;
BYTE	CXpri=0;
UBYTE	*hotkey;
char	activated=FALSE;
char	locked=FALSE;
char	version[]="$VER: WBStars 2.0ß (18 Dec 1996)\0";
int		fg_color;
int		bg_color;
int		Loop_Wait;
int		Max_Objects;
int		Max_Stick;

void	main(long argc, UBYTE **argv)
{
	UBYTE	**ttypes;

	if( OpenLibraries() )
	{
		/* read the Tooltypes */
		ttypes		= ArgArrayInit(argc, argv);
		CXpri		= (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
		hotkey		= ArgString(ttypes, "CX_POPKEY", "control alt w");
		fg_color	= ArgInt(ttypes, "FG_COLOR", 2);
		bg_color	= ArgInt(ttypes, "BG_COLOR", 0);
		Loop_Wait	= ArgInt(ttypes, "LOOP_WAIT", 3);
		Max_Objects	= ArgInt(ttypes, "MAX_OBJECTS", 1000);
		Max_Stick	= ArgInt(ttypes, "MAX_STICK", 100);
		SetTaskPri(FindTask(0),MAX(MIN(ArgInt(ttypes, "TOOLPRI", 0),127),-128));
		ArgArrayDone();

		if( InitCx() )
		{
			while( HandleCx() )
			{
				if(activated) PlotObjects();

				Delay(Loop_Wait);
			}
			RemovCx();
		}
	}
	CloseLibraries();
	if( activated )	Inactivate();
}




void	Inactivate()
{
	if(activated)
	{
		ClearObjects();
		UnlockPubScreen( NULL, wbench);
		wbench=NULL;
		activated=FALSE;
	}
}

char	Activate()		/* is called, if WBStars should activate */
{						/* returns TRUE if Workbench could be locked */

	if( activated ) Inactivate();		/* if activated twice inactivate before */
										/* locking workbench again */
	if( wbench=LockPubScreen("Workbench") )
	{
		RPort		= &(wbench->RastPort);	/* the WBench´s RPort is the */
		LInfo		= &(wbench->LayerInfo);	/* one we want to draw in */
		minx		= wbench->LeftEdge;
		miny		= wbench->TopEdge + wbench->BarHeight;
		width		= wbench->Width;
		height		= wbench->Height - wbench->BarHeight;

		activated=TRUE;
		InitObjects();
	}

	return activated;
}