/** DoRev Header ** Do not edit! **
*
* Name             :  cxhand.c
* Copyright        :  Free Software
* Creation date    :  12-May-93
* Translator       :  SAS/C 6.2
* Compiler opts.   :  See Makefile
*
* Date       Rev  Author               Comment
* ---------  ---  -------------------  ----------------------------------------
* 12-May-93    0  Michael D. Bayne     Commodities handling for Blanker
*
*** DoRev End **/

#include <exec/types.h>
#include <dos/dos.h>
#include <libraries/commodities.h>
#include <devices/inputevent.h>

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

#include "defs.h"

LONG openMainWindow( void );
VOID CloseBlankerWindow( void );
VOID CxBFunc( CxMsg *CxMessage, CxObj *CxObject );

extern	struct	NewBroker nbBroker;
extern	struct	Task  *bTask, *Task;
extern	struct	Screen *bScr;
extern	struct	bPrefObject bPrefs;
	struct	MsgPort *bPort;
	CxObj	*bBroker, *pHotKey, *bHotKey, *objectList;
	ULONG	cxSigFlag, cxBlankFlag, timeCount = 0;

void __interrupt __saveds CxBFunc( CxMsg *CxMessage, CxObj *CxObject )
{
	struct InputEvent *Event = ( struct InputEvent * )CxMsgData( CxMessage );

	if( Event->ie_Class == IECLASS_TIMER ) {
		if( !bTask ) {
			if( ++timeCount >= bPrefs.bTimeout ) {
				Signal( Task, cxBlankFlag );
				timeCount = 0;
			}
		}
	} else {
		if( bTask ) {
			Signal( bTask, SIGBREAKF_CTRL_C );
			bTask = 0L;
		}
		timeCount = 0;
	}
}

LONG handleCxMess( void )
{
	ULONG msgid, msgtype;
	CxMsg *msg;

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

		switch( msgtype ) {
		case CXM_IEVENT:
			switch( msgid ) {
			case EVT_CX_POPUP:
				openMainWindow();
				break;
			case EVT_CX_BLANK:
				Signal( Task, cxBlankFlag );
				break;
			}
			break;
		case CXM_COMMAND:
		switch( msgid ) {
			case CXCMD_DISABLE:
				ActivateCxObj( bBroker, 0l );
				break;
			case CXCMD_ENABLE:
				ActivateCxObj( bBroker, 1l );
				break;
			case CXCMD_KILL:
				return( QUIT );
				break;
			case CXCMD_APPEAR:
			case CXCMD_UNIQUE:
				openMainWindow();
				break;
			case CXCMD_DISAPPEAR:
				CloseBlankerWindow();
				break;
			default:
				break;
			}
		default:
			break;
		}
	}
	return( OK );
}

void ShutdownCX( void )
{
	CxMsg *msg;

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

LONG UpdateCX( void )
{
	ActivateCxObj( bBroker, 0l );

	DeleteCxObj( objectList );
	DeleteCxObj( pHotKey );
	DeleteCxObj( bHotKey );

	if( objectList = CxCustom( CxBFunc, 0L )) AttachCxObj( bBroker, objectList );
	if( pHotKey = HotKey( bPrefs.pKey, bPort, EVT_CX_POPUP )) AttachCxObj( bBroker, pHotKey );
	if( bHotKey = HotKey( bPrefs.bKey, bPort, EVT_CX_BLANK )) AttachCxObj( bBroker, bHotKey );

	if(!( CxObjError( bBroker ))) ActivateCxObj( bBroker, 1l );
	else {
		ShutdownCX();
		return( QUIT );
	}
	return( OK );
}

LONG SetupCX( void )
{
	ShutdownCX();

	if( bPort = CreateMsgPort()) {
		nbBroker.nb_Port = bPort;
		cxSigFlag = 1L << bPort->mp_SigBit;
		if( bBroker = CxBroker( &nbBroker, NULL )) {
			objectList = CxCustom( CxBFunc, NULL );
			AttachCxObj( bBroker, objectList );
			if( pHotKey = HotKey( bPrefs.pKey, bPort, EVT_CX_POPUP )) AttachCxObj( bBroker, pHotKey );
			if( bHotKey = HotKey( bPrefs.bKey, bPort, EVT_CX_BLANK )) AttachCxObj( bBroker, bHotKey );
			if(!( CxObjError( bBroker ))) {
				ActivateCxObj( bBroker, 1l );
				return( OK );
			} else {
				DeleteCxObjAll( bBroker );
				bBroker = 0l;
			}
		}
		DeletePort( bPort );
		bPort = 0l;
	}
	return( QUIT );
}
