#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/commodities.h>
#include <libraries/gadtools.h>
#include <dos/dos.h>
#include <workbench/startup.h>
#include <stdio.h>
#include <string.h>

#include <proto/exec.h>
#include <proto/commodities.h>
#include <proto/gadtools.h>
#include <proto/dos.h>
#include <proto/icon.h>
#include <clib/alib_protos.h>

#include "/include/server.h"

extern BOOL PopUp, CommodityActive, RandomClient, StayCool;
extern struct Window *window;

extern BOOL PopUpWindow( void );
extern void ShutWindow( void );


 /******************
 *                 *
 * GENERAL UTILITY *
 *                 *
 ******************/


void ClearPendingMessages( struct MsgPort *mport )
{
struct Message *msg;

while ( msg = GetMsg( mport ) )
	ReplyMsg( msg );
}


 /******************
 *                 *
 * COMMODITY STUFF *
 *                 *
 ******************/

#define EVT_HOTKEY 1L

struct Library *CxBase, *IconBase;
struct MsgPort *broker_mp;
CxObj *broker, *filter, *sender, *translate;

ULONG CXSignal;

struct NewBroker newbroker = {
	NB_VERSION,
	PROGRAMNAME,
	PROGRAMNAME" "PROGRAMVERSION" © 1995 by Stefano Reksten of 3AM",
	"Simple server for screen blankers",
	NBU_UNIQUE | NBU_NOTIFY,
	COF_SHOW_HIDE, 0, 0, 0 };


UBYTE brokerPri = 0;
char hotkey[40] = "lalt b";

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

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

	switch(msgtype)
		{
		case CXM_IEVENT:
			if ( msgid == EVT_HOTKEY )
				PopUpWindow();
			break;

		case CXM_COMMAND:
			switch(msgid)
			{
			case CXCMD_DISABLE:
				ActivateCxObj(broker, 0L);
				CommodityActive = FALSE;
				break;
			case CXCMD_ENABLE:
				ActivateCxObj(broker, 1L);
				CommodityActive = TRUE;
				break;
			case CXCMD_KILL:
				StayCool = FALSE;
				break;
			case CXCMD_UNIQUE:
				PopUpWindow();
				break;
			case CXCMD_APPEAR:
				PopUpWindow();
				break;
			case CXCMD_DISAPPEAR:
				ShutWindow();
				break;
			}
			break;

		default:
			break;
		}
	}
}
