/*
 *	Copyright (c) 1993 Michael D. Bayne.
 *	All rights reserved.
 *
 *	Please see the documentation accompanying the distribution for distribution and disclaimer information.
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/ports.h>

#include <dos/dos.h>
#include <dos/dostags.h>

#include <intuition/intuition.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>
#include <clib/intuition_protos.h>
#include <clib/utility_protos.h>
#include <clib/graphics_protos.h>
#include <clib/reqtools_protos.h>

#include "Blanker.h"
#include "defs.h"

#include "protos/main.h"
#include "protos/cxhand.h"
#include "protos/winhand.h"
#include "protos/prefs.h"

struct	Task	*bTask = 0L, *tmpTask = 0L;

UBYTE	*msgs[] = { "BM_DOBLANK", "BM_DOPREFS", "BM_DOQUIT" };

VOID DefaultBlanker( VOID )
{
	struct	Screen	*Scr;
	UWORD   CTable[] = { 0x0222, 0x0AAA, 0x0AAA };

	Scr = OpenScreenTags( 0l, SA_DisplayID, bPrefs->bp_Mode, SA_Depth, bPrefs->bp_Depth, SA_Quiet, TRUE,
		SA_Overscan, OSCAN_STANDARD, SA_Behind, TRUE, TAG_DONE );
	LoadRGB4( &(Scr->ViewPort), CTable, 2 );
	ScreenToFront( Scr );

	Wait( SIGBREAKF_CTRL_C );
	SetSignal( 0L, cxBlankFlag );

	CloseScreen( Scr );
}

LONG sendModMsg( ULONG Type, struct MsgPort *modulePort )
{
	struct BlankerMessage *bMsg;

#ifdef STDIO
	printf( "Sending message: %s (%d).\n", msgs[Type-11], Type );
#endif
	if( modulePort ) {
		if(!( bMsg = AllocMem( sizeof( struct BlankerMessage ), MEMF_CLEAR|MEMF_PUBLIC ))) return( 1 );

		bMsg->bm_Mess.mn_ReplyPort = blankerPort;
		bMsg->bm_Mess.mn_Length = sizeof( struct BlankerMessage );
		bMsg->bm_Task = ( struct Task * )( &bTask );
		bMsg->bm_Type = Type;
		bMsg->bm_Mod  = bPrefs->bp_Mode;
		bMsg->bm_Dep  = bPrefs->bp_Depth;
		bMsg->bm_Data = bPrefs->bp_Data;
#ifdef STDIO
		printf( "Putting message to port.\n" );
#endif
		PutMsg( modulePort, ( struct Message * )( bMsg ));
		WaitPort( blankerPort );
		FreeMem( GetMsg( blankerPort ), sizeof( struct BlankerMessage ));
#ifdef STDIO
		printf( "Received reply from module.\n" );
#endif
		switch( Type ) {
		case BM_DOBLANK:
			bTask = tmpTask;
			break;
		case BM_DOQUIT:
			modulePort = 0L;
			break;
		default:
			break;
		}
	} else if( Type == BM_DOBLANK ) {
		bTask = Task;
		DefaultBlanker();
	}

	return( 0 );
}

VOID loadModule( ULONG loadPrefs )
{
	BPTR	checkExists;
	struct BlankerMessage *initMsg;

	sendModMsg( BM_DOQUIT, modulePort );

	if( checkExists = Open( doPath( bPrefs->bp_Name, bPrefs->bp_Dir ), MODE_OLDFILE ) ) {
		Close( checkExists );
#ifdef STDIO
		printf( "Module %s exists, loading...\n", bPrefs->bp_Name );
#endif
		SystemTags( doPath( bPrefs->bp_Name, bPrefs->bp_Dir ), NP_FreeSeglist, TRUE, SYS_Asynch, TRUE,
			SYS_Input, Open( "NIL:", MODE_OLDFILE ), SYS_Output, Open( "NIL:", MODE_OLDFILE ),
			TAG_END );

		WaitPort( blankerPort );
#ifdef STDIO
		printf( "Received modDidInit message.\n" );
#endif
		if( initMsg = ( struct BlankerMessage * )GetMsg( blankerPort )) {
			modulePort = ( struct MsgPort * )initMsg->bm_Mess.mn_ReplyPort;
			tmpTask = initMsg->bm_Task;

			if( initMsg->bm_Dep ) {
				bPrefs->bp_Depth = initMsg->bm_Dep;
				bPrefs->bp_Flags &= ~BS_ASKDEP;
			} else bPrefs->bp_Flags |= BS_ASKDEP;

			if( initMsg->bm_Mod != INVALID_ID ) {
				bPrefs->bp_Mode = initMsg->bm_Mod;
	      			bPrefs->bp_Flags &= ~BS_ASKSCR;
			} else bPrefs->bp_Flags |= BS_ASKSCR;

			if( loadPrefs ) {
				bPrefs->bp_Flags &= ~BS_NOPRFS;
				if( initMsg->bm_Data ) CopyMem( initMsg->bm_Data, bPrefs->bp_Data, 512 );
				else bPrefs->bp_Flags |= BS_NOPRFS;
			}
			CopyMem( initMsg->bm_Info, modInfo, 64 );
			bPrefs->bp_Flags |= BS_VALMOD;

			FreeMem( initMsg, sizeof( struct BlankerMessage ));
		}
	} else {
		CopyMem( "No module loaded.", modInfo, 18 );
		bPrefs->bp_Flags |= BS_ASKDEP;
		bPrefs->bp_Flags |= BS_ASKSCR;
		bPrefs->bp_Flags &= ~BS_VALMOD;
		bPrefs->bp_Flags |= BS_NOPRFS;
	}

	if( BlankerWnd && loadPrefs ) {
		setPrefs( bPrefs, GD_MODINFO );
		setPrefs( bPrefs, GD_MDISPLAY );
	}
}
