/*
 * 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 <libraries/commodities.h>
#include <devices/inputevent.h>

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/utility_protos.h>
#include <clib/commodities_protos.h>
#include <clib/alib_protos.h>

#include <stdio.h>

#include "TaskE_rev.h"
#include "defs.h"

#define KILLKEY "ctrl alt k"

	struct	MsgPort *tPort;
	CxObj	*tBroker, *killKey;
extern	ULONG	killMode;
extern	struct	IntuitionBase *IBase;
	struct	NewBroker ntBroker = {
		NB_VERSION,
		"Task Exchange", VERS,
		"Task manipulation software",
		0, 0, 0, 0, 0
	};
    
ULONG handleCxMess( void )
{
	struct Window *deadWnd;
	struct Screen *deadScr;
	ULONG msgid, msgtype, lock;
	CxMsg *msg;

	while( msg = ( CxMsg * )GetMsg( tPort )) {
		msgid = CxMsgID( msg );
		msgtype = CxMsgType( msg );
		ReplyMsg(( struct Message * )msg );

		switch( msgtype ) {
		case CXM_IEVENT:
			switch( msgid ) {
			case EVT_CX_KILL:
				switch( killMode ) {
				case WINDOW:
					lock = LockIBase( 0 );
					if( IBase->FirstScreen->FirstWindow ) for( deadWnd = IBase->FirstScreen->FirstWindow;
						!(( deadWnd->Flags )&( WFLG_WINDOWACTIVE )); deadWnd = deadWnd->NextWindow );
					UnlockIBase( lock );
					if( deadWnd ) CloseWindow( deadWnd );
					deadWnd = 0L;
					killMode = NONE;
					break;
				case SCREEN:
					lock = LockIBase( 0 );
					deadScr = IBase->FirstScreen;
					UnlockIBase( lock );
					for( deadWnd = deadScr->FirstWindow; deadScr->FirstWindow;
						deadWnd = deadScr->FirstWindow ) {
						CloseWindow( deadWnd );
					}
					deadWnd = 0L;
					CloseScreen( deadScr );
					deadScr = 0L;
					killMode = NONE;
					break;
				}
				break;
			}
			break;
		case CXM_COMMAND:
			switch( msgid ) {
			case CXCMD_DISABLE:
				ActivateCxObj( tBroker, 0l );
				break;
			case CXCMD_ENABLE:
				ActivateCxObj( tBroker, 1l );
				break;
			case CXCMD_KILL:
				return( 0 );
				break;
			default:
				break;
			}
		default:
			break;
		}
	}
	return( 1 );
}

void ShutdownCX( void )
{
	CxMsg *msg;

	if( tPort ) {
		if( tBroker ) DeleteCxObjAll( tBroker );
		tBroker = 0l;
		while( msg = ( CxMsg * )GetMsg( tPort )) ReplyMsg(( struct Message * )msg );
		DeletePort( tPort );
		tPort = 0l;
	}
}

ULONG SetupCX( void )
{
	ULONG	cxSigFlag;

	if( tPort = CreateMsgPort()) {
		ntBroker.nb_Port = tPort;
		cxSigFlag = 1L << tPort->mp_SigBit;
		if( tBroker = CxBroker( &ntBroker, NULL )) {
			if( killKey = HotKey( KILLKEY, tPort, EVT_CX_KILL )) AttachCxObj( tBroker, killKey );
			if(!( CxObjError( tBroker ))) {
				ActivateCxObj( tBroker, 1l );
				return( cxSigFlag );
			} else {
				DeleteCxObjAll( tBroker );
				tBroker = 0l;
			}
		}
		DeletePort( tPort );
		tPort = 0l;
	}
	return( 0 );
}
