/* SetMode.c ****************************************************************
*
*	SetMode	-------	Set the current display mode the Amiga is in,
*			this means PAL and NTSC. Written for all users
*			of programs which adapt to the displayflags
*			of the Amiga, such as DPaint III, Butcher 2.0, etc.
*
*	Author -------- Dr. Peter Kittel (Commodore Germany)
*			Olaf 'Olsen' Barthel, ED Electronic Design Hannover.
*
*****************************************************************************
*
*	SetMode can be used via Workbench or CLI. For Workbench usage
*	add:
*
*		MODE=NTSC
*		or
*		MODE=PAL
*
*	to the ToolTypes. For CLI usage type
*
*		SetMode PAL
*		or
*		SetMode NTSC
*
*****************************************************************************/

#include <intuition/intuitionbase.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <graphics/gfxbase.h>
#include <stdio.h>

	/* Some macros so I don't forget the colours and numbers. */

#define BLUE 0
#define WHIT 1
#define BLAC 2
#define ORNG 3

	/* Forward declarations. */

extern struct Library		*OpenLibrary();
extern struct DiskObject	*GetDiskObject();
extern char 			*FindToolType();

	/* Some global data various routines need. */

struct IntuitionBase		*IntuitionBase;
struct GfxBase			*GfxBase;
struct Library			*IconBase;
extern struct WBStartup		*WBenchMsg;

	/* ForceNTSC() :
	 *
	 *	Forces NTSC display mode.
	 */

void
ForceNTSC()
{
	GfxBase -> DisplayFlags		= (GfxBase -> DisplayFlags) & (!PAL);
	GfxBase -> NormalDisplayRows	= 200;
}

	/* ForcePAL() :
	 *
	 *	Forces PAL display mode.
	 */

void
ForcePAL()
{
	GfxBase -> DisplayFlags		= (GfxBase -> DisplayFlags) | PAL;
	GfxBase -> NormalDisplayRows	= 256;
}

	/* Inform(Message,Dire) :
	 *
	 *	Informs the user about errors, if Dire is TRUE flashes
	 *	also the screen.
	 */

void
Inform(Message,Dire)
char *Message;
BOOL Dire;
{
	static struct TextAttr DefaultFont[2] =
	{
		{(UBYTE *)"topaz.font",8,FSF_BOLD  ,FPF_ROMFONT},
		{(UBYTE *)"topaz.font",8,FS_NORMAL ,FPF_ROMFONT}
	};

	static struct IntuiText InfoText[3] =
	{
		{ORNG,WHIT,JAM2,13, 4,(struct TextAttr *)&DefaultFont[0],(UBYTE *)"SetMode v1.0, © 1989 by ED Electronic Design",(struct IntuiText *)&InfoText[1]},
		{BLAC,WHIT,JAM2,13,14,(struct TextAttr *)&DefaultFont[1],(UBYTE *)NULL,		(struct IntuiText *)NULL},
	
		{BLUE,WHIT,JAM2, 5, 3,(struct TextAttr *)&DefaultFont[1],(UBYTE *)"Understood",	(struct IntuiText *)NULL}
	};

	if(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0))
	{
		InfoText[1] . IText = (UBYTE *)Message;

		if(Dire)
			DisplayBeep(NULL);

		AutoRequest(NULL,&InfoText[0],NULL,&InfoText[2],NULL,NULL,419,56);

		CloseLibrary(IntuitionBase);
	}
}

	/* GetWBType() :
	 *
	 *	Examines the DiskObject structure associated with the
	 *	program to find out about the mode the user wants us
	 *	to set.
	 */

long
GetWBType()
{
	char *Type;
	long IsPal;
	struct DiskObject *DiskObject;

		/* Open the icon.library. */

	if(!(IconBase = (struct Library *)OpenLibrary("icon.library",0)))
		return(TRUE);

		/* Try to load the icon. */

	if(!(DiskObject = (struct DiskObject *)GetDiskObject(WBenchMsg -> sm_ArgList -> wa_Name)))
	{
		FreeDiskObject(DiskObject);
		CloseLibrary(IconBase);

		return(TRUE);
	}

		/* Try to find the mode. */

	if(Type = (char *)FindToolType(DiskObject -> do_ToolTypes,"MODE"))
	{
		IsPal = -1;

			/* Look for the modes. */

		if(!strcmp(Type,"PAL") || !strcmp(Type,"pal"))
			IsPal = TRUE;

		if(!strcmp(Type,"NTSC") || !strcmp(Type,"ntsc"))
			IsPal = FALSE;

		FreeDiskObject(DiskObject);
		CloseLibrary(IconBase);

			/* Made a mistake? */

		if(IsPal == -1)
			Inform("Need MODE=PAL or MODE=NTSC in Tool Types!",FALSE);

		return(IsPal);
	}
}

	/* Quit(Code) :
	 *
	 *	Leaves after Gfx has been closed.
	 */

void
Quit(Code)
long Code;
{
	if(GfxBase)
		CloseLibrary(GfxBase);

	exit(Code);
}

	/* main(argc,argv) :
	 *
	 *	Main routines are always fun.
	 */

void
main(argc,argv)
long argc;
char *argv[];
{
	long IsPal;
	register long i;

		/* Open the graphics. */

	if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",LIBRARY_VERSION)))
	{
		Inform("Couldn't open graphics.library!",TRUE);
		Quit(20);
	}

		/* Try to find what we need. */

	IsPal = -1;

	if(!argc)
		IsPal = GetWBType();
	else
	{
			/* Give some information. */

		for(i = 0 ; i < strlen(argv[0]) ; i++)
			argv[0][i] = toupper(argv[0][i]);

		printf("\33[33m\33[1mSetMode\33[0m\33[31m © 1989 by ED Electronic Design Hannover\n");

		if(!strcmp(argv[1],"PAL") || !strcmp(argv[1],"pal"))
			IsPal = TRUE;

		if(!strcmp(argv[1],"NTSC") || !strcmp(argv[1],"ntsc"))
			IsPal = FALSE;

			/* Spelling mistake? */

		if(IsPal == -1)
			printf("%s: Need \33[33m%s PAL\33[31m or \33[33m%s NTSC\33[31m!\n",argv[0],argv[0],argv[0]);
	}

	switch(IsPal)
	{
			/* Adjust the modes. */

		case TRUE:	ForcePAL();
				break;

		case FALSE:	ForceNTSC();
				break;

		case -1:	Quit(20);
	}

	Quit(0);
}
