Commodities

From Amiga Development Wiki
Jump to: navigation, search

It is recommended to every Amiga application to register with commodities.library to allow easy control of your application through Exchange program. Note that MUI registers your application automatically and you only have to do this when using other GUI framework.

See sample program below how to register your application.

#include <proto/commodities.h>
#include <proto/dos.h>
#include <proto/exec.h>
 
static struct NewBroker MyBroker =
{
	NB_VERSION,
	"Broker example",
	"Exchange example program",
	"This is an example how to add Exchange support to Amiga applications.",
	NBU_NOTIFY,
	COF_SHOW_HIDE,
	0,
	NULL,
	0
};
 
int main()
{
	struct MsgPort *mp = CreateMsgPort();
 
	if (mp)
	{
		CxObj *broker;
 
		MyBroker.nb_Port = mp;
		broker = CxBroker(&MyBroker, NULL);
 
		if (broker)
		{
			CxMsg *msg;
			BOOL running = TRUE;
 
			ActivateCxObj(broker, 1);
 
			while (running)
			{
				WaitPort(mp);
 
				while ((msg = (APTR)GetMsg(mp)))
				{
					ULONG id = CxMsgID(msg);
					ULONG type = CxMsgType(msg);
 
					ReplyMsg((struct Message *)msg);
 
					if (type == CXM_COMMAND)
					{
						switch (id)
						{
							case CXCMD_DISABLE:
							case CXCMD_ENABLE:
								PutStr("Disable or enable requested.\n");
								break;
 
							case CXCMD_KILL:
								running = FALSE;
								PutStr("Quit requested.\n");
								break;
 
							case CXCMD_APPEAR:
								PutStr("Uniconify/unhide requested.\n");
								break;
 
							case CXCMD_DISAPPEAR:
								PutStr("Iconify/hide requested.\n");
								break;
						}
					}
				}
			}
 
			DeleteCxObjAll(broker);
 
			while ((msg = (APTR)GetMsg(mp)))
				ReplyMsg((struct Message *)msg);
		}
 
		DeleteMsgPort(mp);
	}
 
	return 0;
}

Input events

Commodities can define global hotkeys.

#include <proto/commodities.h>
#include <proto/dos.h>
#include <proto/exec.h>
 
#define HOTKEY_HELP 1
 
static struct NewBroker MyBroker =
{
	NB_VERSION,
	"Broker example",
	"Exchange example program",
	"This is an example how to add Exchange support to Amiga applications.",
	NBU_NOTIFY,
	COF_SHOW_HIDE,
	0,
	NULL,
	0
};
 
static CONST TEXT hotkey_definition[] = "help";
 
static BOOL AttachHotkey(CxObj *broker, CONST_STRPTR hotkey, struct MsgPort *port)
{
	BOOL rc = FALSE;
	CxObj *filter = CxFilter(hotkey);
 
	if (filter)
	{
		/* Attach hotkey object to broker and initialized hotkey detection.
		 */
		AttachCxObj(broker, filter);
		AttachCxObj(filter, CxSender(port, HOTKEY_HELP));
		AttachCxObj(filter, CxTranslate(NULL));
 
		if (CxObjError(filter) == 0)
			rc = TRUE;
	}
 
	return rc;
}
 
int main()
{
	struct MsgPort *mp = CreateMsgPort();
 
	if (mp)
	{
		CxObj *broker;
 
		MyBroker.nb_Port = mp;
		broker = CxBroker(&MyBroker, NULL);
 
		if (broker)
		{
			CxMsg *msg;
 
			if (AttachHotkey(broker, hotkey_definition, mp))
			{
				BOOL running = TRUE;
 
				ActivateCxObj(broker, 1);
 
				while (running)
				{
					WaitPort(mp);
 
					while ((msg = (APTR)GetMsg(mp)))
					{
						ULONG id = CxMsgID(msg);
						ULONG type = CxMsgType(msg);
 
						ReplyMsg((struct Message *)msg);
 
						if (type == CXM_COMMAND)
						{
							switch (id)
							{
								case CXCMD_UNIQUE:
									PutStr("Another instance of this test program was launched.\n");
									break;
 
								case CXCMD_DISABLE:
								case CXCMD_ENABLE:
									PutStr("Disable or enable requested.\n");
									break;
 
								case CXCMD_KILL:
									running = FALSE;
									PutStr("Quit requested.\n");
									break;
 
								case CXCMD_APPEAR:
									PutStr("Uniconify/unhide requested.\n");
									break;
 
								case CXCMD_DISAPPEAR:
									PutStr("Iconify/hide requested.\n");
									break;
							}
						}
						else if (type == CXM_IEVENT)
						{
							switch (id)
							{
								case HOTKEY_HELP:
									PutStr("Help key pressed.\n");
									break;
							}
						}
					}
				}
			}
 
			DeleteCxObjAll(broker);
 
			while ((msg = (APTR)GetMsg(mp)))
				ReplyMsg((struct Message *)msg);
		}
 
		DeleteMsgPort(mp);
	}
 
	return 0;
}
Input classDescription
diskremovedMassstorage removal event
diskinsertedMassstorage insertion event
event
newprefsSystem settings changed event
pointerposPointer position
rawkeyKeyboard events
rawmouseMouse events
timerTimer events

Events can use following qualifiers:

QualifierDescription
shiftLeft or right shift key
lshiftLeft shift key only
rshiftRight shift key only
capslockCaps lock on
caps_lockCaps lock on
capsCaps lock on or any shift key is pressed
controlLeft or right control key
altLeft or right alt key
laltLeft alt key only
raltRight alt key only
lcommandLeft command key (Amiga, Apple or Windows key)
rcommandRight command key (Amiga, Apple or Windows key)
numericpadNumeric pad
repeat
relativemouse
Rawmouse eventDescription
mouse_leftpressLeft mouse button pressed
mouse_middlepressMiddle mouse button pressed
mouse_rightpressRight mouse button pressed
mouse_fourthpressRight mouse button pressed

Commodities can use normal ASCII range keys or define special keys defined below:

Rawkey eventDescription
backspace
break
comma
cursor_downCursor key down
cursor_leftCursor key left
cursor_rightCursor key right
cursor_upCursor key up
del
delete
downCursor key down
end
enter
esc
f1
f2
f3
f4
f5
f6
f7
f8
f9
f10
f11
f12
helpOn Windows keyboards this is 'menu' key
home
insert
leftCursor key left
numlock
page_down
page_up
prtscrPrint screen key. Not available on Amiga keyboards.
return
rightCursor right key
scrlockScroll lock key. Not available on Amiga keyboards.
space
tab
upCursor key up
media1
media2
media3
media4
media5
media_stop
media_play
media_prev
media_next
media_rewind
media_forward
wheel_upMouse wheel up
wheel_downMouse wheel down
wheel_leftMouse wheel left
wheel_rightMouse wheel right