/*
**    GrabKEY
**
**        © 1996 by Timo C. Nentwig
**        All Rights Reserved !
**
**        Tcn@techbase.in-berlin.de
**
**
*/

/// #include

#include "gk_GST.h"
#include "gk_Protos.h"

///
/// proto

static BOOL              AttachFilter    (const STRPTR HotKey, const ULONG Event);
proto  BOOL __regargs    HandleCx        (CxMsg *Message);
proto  VOID              QuitCx          (VOID);
proto  BOOL              SetupCx         (VOID);

///

struct    MsgPort         *CxPort;
CxObj                     *Broker;

/// AttachFilter ()

	/*
	 *    FUNCTION    Attach a filter.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     AttachFilter ("LALT b", HOTKEY);
	 *
	 */


static BOOL
AttachFilter (const STRPTR HotKey, const ULONG Event)
{

	CxObj   *Filter;
	CxObj   *Sender;
	CxObj   *Translator;

	if (Filter = CxFilter (HotKey))
	{

		AttachCxObj (Broker, Filter);

	   if (Sender = CxSender (CxPort, Event))
	   {

		   AttachCxObj (Filter, Sender);

		   if (Translator = CxTranslate (NULL))
		   {

			   AttachCxObj (Filter, Translator);

			   if ( ! (CxObjError (Filter)))
			   {

				   return (TRUE);

			   }

		   }

	   }

	}

	return (FALSE);

}

///
/// HandleCx ()

	/*
	 *    FUNCTION    Handle commodity events.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     HandleCx (Msg);
	 *
	 */


BOOL __regargs
HandleCx (CxMsg *Message)
{

	ULONG    MsgID   = CxMsgID   (Message),
			 MsgType = CxMsgType (Message);

	ReplyMsg ((struct Message *) Message);

	switch (MsgType)
	{

			// Program events

		case CXM_IEVENT:

			switch (MsgID)
			{

				case CX_POPKEY:            CreateGUI();
										   break;

				case SAVE_WINDOW_KEY:      Grab (WINDOW, TO_FILE);
										   break;

				case SAVE_SCREEN_KEY:      Grab (SCREEN, TO_FILE);
										   break;

				case CLIP_WINDOW_KEY:      Grab (WINDOW, TO_CLIPBOARD);
										   break;

				case CLIP_SCREEN_KEY:      Grab (SCREEN, TO_CLIPBOARD);
										   break;

				case PRINT_WINDOW_KEY:     Grab (WINDOW, TO_PRINTER);
										   break;

				case PRINT_SCREEN_KEY:     Grab (SCREEN, TO_PRINTER);
										   break;

			}

			break;

			// Commodity events

		case CXM_COMMAND:

			switch (MsgID)
			{
				case CXCMD_DISABLE:

					ActivateCxObj (Broker, FALSE);
					break;

				case CXCMD_ENABLE:

					ActivateCxObj (Broker, TRUE);
					break;

				case CXCMD_APPEAR:
				case CXCMD_UNIQUE:

					if (Window)
					{

						LT_ShowWindow (Handle, TRUE);

					}
					else
					{


						CreateGUI();

					}
					break;

				case CXCMD_DISAPPEAR:

					RemoveGUI();
					break;

				case CXCMD_KILL:

					return (TRUE);
					break;

			}

	}

	return (FALSE);

}

///
/// QuitCx ()

	/*
	 *    FUNCTION    Shut down program.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     QuitCx ();
	 *
	 */


VOID
QuitCx (VOID)
{

	if (Broker)
	{

		DeleteCxObjAll (Broker);
		Broker = NULL;

	}

	if (CxPort)
	{

		struct    Message   *Message;

		RemPort (CxPort);

		while (Message = GetMsg (CxPort))
		{

			ReplyMsg (Message);

		}

		DeleteMsgPort (CxPort);

		CxPort = NULL;

	}

}

///
/// SetupCx ()

	/*
	 *    FUNCTION    Setup commoditiy stuff.
	 *
	 *    NOTE
	 *
	 *    EXAMPLE     SetupCx ();
	 *
	 */


BOOL
SetupCx (VOID)
{

	QuitCx();

	if (CxPort = CreateMsgPort())
	{

		struct    NewBroker    NewBroker;

		NewBroker . nb_Version         = NB_VERSION;
		NewBroker . nb_Name            = PRG_TITLE;
		NewBroker . nb_Title           = PRG_TITLE " " PRG_VERSION " © " PRG_YEAR " by " PRG_AUTHOR;
		NewBroker . nb_Descr           = LocaleString (MSG_CX_INFO);
		NewBroker . nb_Unique          = NBU_UNIQUE | NBU_NOTIFY;
		NewBroker . nb_Flags           = COF_SHOW_HIDE;
		NewBroker . nb_Pri             = 0;
		NewBroker . nb_Port            = NULL;
		NewBroker . nb_ReservedChannel = 0;

		CxPort -> mp_Node . ln_Name = NewBroker . nb_Name;

		AddPort (CxPort);

		NewBroker . nb_Port = CxPort;
		NewBroker . nb_Pri  = Set -> Cx . Priority;

		if (Broker = CxBroker (&NewBroker, NULL))
		{

			if (AttachFilter (Set -> Key . Window . Save,  SAVE_WINDOW_KEY)  &&
				AttachFilter (Set -> Key . Screen . Save,  SAVE_SCREEN_KEY)  &&
				AttachFilter (Set -> Key . Window . Clip,  CLIP_WINDOW_KEY)  &&
				AttachFilter (Set -> Key . Screen . Clip,  CLIP_SCREEN_KEY)  &&
				AttachFilter (Set -> Key . Window . Print, PRINT_WINDOW_KEY) &&
				AttachFilter (Set -> Key . Screen . Print, PRINT_SCREEN_KEY) &&
				AttachFilter (Set -> Cx . PopKey,          CX_POPKEY))
				{

					ActivateCxObj (Broker,TRUE);
					return (TRUE);

				}

		}

	}

	return (FALSE);

}

///

