/*
 * Copyright (c) 1993 Michael D. Bayne.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software must display the following
 *    acknowledgement:
 *
 *       This product includes software developed by Michael D. Bayne.
 *
 * 4. My name may not be used to endorse or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY MICHAEL D. BAYNE ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL MICHAEL D. BAYNE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#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 );

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 );
		WaitPort( modPort );
		GetMsg( modPort );
		FreeMem( modMsg, sizeof( struct bMessage ));

		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 = ( 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 );

				switch( Type ) {
				case BM_DOBLANK:
					blank();
					break;
				case BM_DOPREFS:
					prefs( STARTUP );
					break;
				case BM_DOQUIT:
					if( winSig() ) prefs( KILL );
					retval = QUIT;
					break;
				}
			}
		}
	}
	if( modPort ) DeletePort( modPort );
}
