/*
 *	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/ports.h>
#include <exec/memory.h>

#include <dos/dos.h>

#include <clib/exec_protos.h>
#include <clib/alib_protos.h>

#include "defs.h"

VOID blank( VOID );
VOID defaults( struct bMessage * );
VOID prefs( LONG );
LONG winSig( VOID );

__far	extern	ULONG	RangeSeed;
struct	MsgPort		*modPort, *sendPort;
struct	bMessage	*bMsg;
struct	Task		**Task;
	ULONG		Depth, Mode, Type;
	UBYTE		*prefData;

VOID main( VOID )
{
	LONG retval = CONTINUE, sigs;
	struct bMessage *modMsg;

	if(( sendPort = FindPort( "BlankerCC" ))&&( modPort = CreatePort(0,0))&&
		( modMsg = AllocMem( sizeof( struct bMessage ), MEMF_PUBLIC|MEMF_CLEAR ))) {

		modMsg->bm_Mess.mn_ReplyPort = modPort;
		modMsg->bm_Mess.mn_Length = sizeof( struct bMessage );
		modMsg->bm_Task = FindTask( 0L );
		Forbid();
		CopyMem( "Blanker Task", modMsg->bm_Task->tc_Node.ln_Name, 13 );
		Permit();
		defaults( modMsg );
		PutMsg( sendPort, ( struct Message * )modMsg );

		RangeSeed = ((ULONG)modMsg ^ (ULONG)sendPort );

		while( retval ) {
			sigs = Wait( SIG_PORT | winSig() | SIGBREAKF_CTRL_C );

			if( sigs & winSig() ) prefs( IDCMP );
			if( sigs & SIGBREAKF_CTRL_C ) retval = QUIT;
			if(( sigs & SIG_PORT )||( bMsg )) {
				while( bMsg = ( struct bMessage * )GetMsg( modPort )) {
					Depth = bMsg->bm_Dep;
					Mode = bMsg->bm_Mod;
					prefData = bMsg->bm_Data;
					Type = bMsg->bm_Type;
					Task = (( struct Task ** )bMsg->bm_Task );
					ReplyMsg(( struct Message * )bMsg );
					bMsg = 0L;

					switch( Type ) {
					case BM_DOBLANK:
						if( SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C ) SetSignal( 0L, SIGBREAKF_CTRL_C );
						else blank();
						break;
					case BM_DOPREFS:
						if(!( winSig() )) prefs( STARTUP );
						break;
					case BM_DOQUIT:
						if( winSig() ) prefs( KILL );
						retval = QUIT;
						break;
					}
				}
			}
		}
	}
	if( modPort ) DeletePort( modPort );
}
