/*
**	Cx.c
**
**	Copyright (C) 1994,95,96,97,98 Bernardo Innocenti
**
**	Commodity support functions
*/


#include <exec/memory.h>
#include <libraries/commodities.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/commodities.h>

#include "XModulePriv.h"
#include "Gui.h"


#define CX_POPKEY	'POP!'


XDEF BYTE CxPri = 0;
XDEF UBYTE CxPopKey[32] = "control alt x";
XDEF BOOL CxPopup = TRUE;
XDEF ULONG CxSig = 0;

XDEF struct MsgPort *CxPort = NULL;
XDEF CxObj *MyBroker = NULL;


static struct NewBroker MyNewBroker =
{
	NB_VERSION,
	PRGNAME,
	Version+6,
	"Module Processing Utility",
	NBU_DUPLICATE,
	COF_SHOW_HIDE,
	0,
	NULL,
	0
};



GLOBALCALL void HandleCx (void)
{
	CxMsg	*cxm;
	ULONG	 type;
	LONG	 id;

	while (cxm = (CxMsg *) GetMsg (CxPort))
	{
		type = CxMsgType (cxm);
		id = CxMsgID (cxm);

		switch (type)
		{
			case CXM_IEVENT:
				if (id == CX_POPKEY) DeIconify();
				break;

			case CXM_COMMAND:
				switch (id)
				{
					case CXCMD_DISABLE:
						ActivateCxObj (MyBroker, FALSE);
						break;

					case CXCMD_ENABLE:
						ActivateCxObj (MyBroker, TRUE);
						break;

					case CXCMD_APPEAR:
						DeIconify();
						break;

					case CXCMD_DISAPPEAR:
						CloseDownScreen();
						break;

					case CXCMD_KILL:
						Quit = TRUE;
						GuiSwitches.AskExit = FALSE;
						break;

					default:
						break;

				}	/* End Switch (id) */

			default:
				break;

		}	/* End Switch (type) */

		ReplyMsg ((struct Message *) cxm);

	}	/* End While (GetMsg()) */
}



GLOBALCALL LONG SetupCx (void)
{
	CxObj *filter, *sender;

	if (!CxPopKey[0]) return RETURN_FAIL;

	if (!(CxBase = OpenLibrary ("commodities.library", 37L)))
		return RETURN_FAIL;

	if (!(CxPort = CreateMsgPort ()))
	{
		CleanupCx();
		return ERROR_NO_FREE_STORE;
	}

	CxSig = 1 << CxPort->mp_SigBit;
	Signals |= CxSig;

	MyNewBroker.nb_Pri = CxPri;
	MyNewBroker.nb_Port = CxPort;

	if (!(MyBroker = CxBroker (&MyNewBroker, NULL)))
	{
		CleanupCx();
		return RETURN_FAIL;
	}

	/* Create PopKey Filter/Sender */

	if (filter = CxFilter (CxPopKey))
	{
		if (CxObjError (filter) & COERR_BADFILTER)
			ShowMessage (MSG_BAD_HOTKEY);

		AttachCxObj (MyBroker, filter);

		if (sender = CxSender (CxPort, CX_POPKEY))
			AttachCxObj (filter, sender);
	}

	ActivateCxObj (MyBroker, TRUE);

	return RETURN_OK;
}



GLOBALCALL void CleanupCx (void)
{
	if (CxBase)
	{
		if (MyBroker)
			{ DeleteCxObjAll (MyBroker); MyBroker = NULL; }

		if (CxPort)
		{
			KillMsgPort (CxPort);
			CxPort = NULL;
			Signals &= ~CxSig;
			CxSig = 0;
		}

		CloseLibrary (CxBase); CxBase = NULL;
	}
}

