/* $VER: WBStars_main.c 2.0b4 (29 May 1997)
*/

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

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

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

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.0b4 (29 May 1997)\0";
int	bg_color;
int	fg_color;
int	Loop_Wait;
int	Max_Objects;
int	Max_Stick;
int	Check_Stick;
int	V_Flutter;
char	Remember;
char	Force;

void	main(long argc, UBYTE **argv)
{
	UBYTE	**ttypes;
	struct EasyStruct	fail1={sizeof(struct EasyStruct),0,"WBStars","failed to open libraries"," Shit ! "};

	if( OpenLibraries() )
	{
		/* read the Tooltypes */
		ttypes		= ArgArrayInit(argc, argv);
		CXpri		= (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
		hotkey		= ArgString(ttypes, "CX_POPKEY", "control alt w");
		bg_color	= ArgInt(ttypes, "BG_COLOR", -1);
		Loop_Wait	= ArgInt(ttypes, "LOOP_WAIT", 3);
		Max_Objects	= ArgInt(ttypes, "MAX_OBJECTS", 1000);
		Max_Stick	= ArgInt(ttypes, "MAX_STICK", 100);
		V_Flutter	= MAX(MIN(ArgInt(ttypes, "V_FLUTTER", 20),100),0);
		Remember	= (char)(0!=strcmpi(ArgString(ttypes, "REMEMBER", "NO"),"NO\0"));
		Force		= (char)(0!=strcmpi(ArgString(ttypes, "FORCE", "NO"),"NO\0"));
		Check_Stick	= ArgInt(ttypes, "CHECK_STICK", 5);
		SetTaskPri(FindTask(0),MAX(MIN(ArgInt(ttypes, "TOOLPRI", 0),127),-128));
		ArgArrayDone();

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

				Delay(Loop_Wait);
			}
			RemovCx();
		}
	}
	else
	{
		if( IntuitionBase ) EasyRequestArgs(NULL,&fail1,0,NULL);
	}
	CloseLibraries();
	if( activated )	Inactivate();
}




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

char	Activate()		/* is called, if WBStars should activate */
{				/* returns TRUE if Workbench could be locked */
	short	i;
	short	testcolor[256];
	short	h;
	double	dist;
	double	mindist=-1.0;
	ULONG	color;
	short	ncolors;
	short	r,g,b;

	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 + 1;
		width		= wbench->Width;
		height		= wbench->Height - wbench->BarHeight - 1;
		cm		= wbench->ViewPort.ColorMap;
		ncolors		= (short)(1<<(RPort->BitMap->Depth));

		if( bg_color<0 )
		{
			for(i=0;i<(short)(1<<(RPort->BitMap->Depth));i++)
			{
				testcolor[i]=0;
			}
			for(i=0;i<width;i++)
			{
				testcolor[ReadPixel(RPort,i+minx,miny)]++;
			}
			bg_color=0;
			h=testcolor[0];
			for(i=1;i<(short)(1<<(RPort->BitMap->Depth));i++)
			{
				if(testcolor[i]>h)
				{
					bg_color=i;
					h=testcolor[i];
				}
			}
		}
		if(OS3)
		{
			fg_color=(int)ObtainBestPenA(cm,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,NULL);
		}
		if(!OS3 || fg_color==-1)
		{
			for(i=0;i<ncolors;i++)
			{
				color=GetRGB4(cm,(long)i);
				r=(color>>8)&15;
				g=(color>>4)&15;
				b=color&15;

				if( (i!=bg_color) && (((dist=pow((double)(15-r),2.0)+pow((double)(15-g),2.0)+pow((double)(15-b),2.0))<mindist)||(mindist<0.0)) )
				{
					mindist=dist;
					fg_color=i;
				}
			}
		}

		activated=TRUE;
		InitObjects();
	}

	return activated;
}