/* test.c */

/*
** Author: Tim Holloway
** Date written: 01/87
**
** Description: This program tests the Desk Accessory Control Program (DACP)
** as well as provides a skeleton for programs that want to hook into DACP.
**
** Use:
**   RUN DACP
**   RUN TEST
**
**  Press Left-Amiga shift key + 'A' key.  you should get a pop-up (as opp-
**  osed to pull-down) menu of accessories.  This program registers as:
*/
#define ACCESSORY_ID "A DACP Test Accessory"
/*
**  Select it via the associated fundtion key or the mouse.  TEST should
**  print that it has been Signal'ed.  It will then wait for a keypress,
**  unless you compiled/linked it under Lattice C versions 3.03 and earlier
**  And for all I know, later.  In the case of Lattice, TEST will logout
**  and terminate immediately.  Otherwise, it will wait for the keypress
**  like it was TOLD to do.
**
**  Distribution: This program, DACP itself, and the accompanying docu-
**  mentation may be freely distributed, but not sold.  Please keep all
**  the pieces together.
*/

#include <stdio.h>
#include <exec/types.h>
#include <exec/exec.h>
#include <exec/ports.h>

#include "dacp.h"

DACPmsg log_dacp =
{
   {
      {NULL, NULL, NT_MESSAGE, 0, ACCESSORY_ID },	/* Node */
      NULL, sizeof(DACPmsg)				/* Message */
   },
   0L, LOGIN_DACP				/* user stuff */
};

main()
{
	extern struct MsgPort *CreatePort(char *, int), *FindPort(char *);
	struct MsgPort *mp, *mp1;
	LONGBITS DACPsignal = 0;
	int DACPSigNum;

	printf ("Starting accessory test\n");

	if ( (mp1 = CreatePort(NULL, 0)) == NULL)
	{
		printf ("Couldn't create reply port\n");
		exit(20);
	}
printf ("Reply port is at %lx\n", mp1);

	log_dacp.msg.mn_ReplyPort = mp1;

	DACPSigNum = AllocSignal(-1);
	log_dacp.sigbits =
		DACPsignal =  1L << DACPSigNum;
printf ("allocate signal #%lx\n", log_dacp.sigbits);
/* SHOULD TEST TO SEE IF WE GOT SIGNAL HERE... */
	log_dacp.log_status = LOGIN_DACP;


/******* CAUTION - turning multitasking OFF *****************/
	Forbid();		
	mp = FindPort(LOGPORTNAME);
printf ("Message port is at %lx\n", mp);
	if (mp == NULL)
	{
/* in a "real" program, this would simply mean that DACP could not select
   me.  In this test, I would just hang,. since if DACP doesn't, nobody
   else will.
*/
		Permit();
		printf ("Couldn't find the DACP message port\n");
		exit(20);
	}
	else
	{
printf ("Go put message\n");
		PutMsg(mp, &log_dacp);
		Permit();		/* Restore multitasking */
printf ("Waiting for reply\n");
		WaitPort(mp1);
printf ("GOT REPLY");
	}

/*	while (1) */
	{
		register int i;

		i = Wait(DACPsignal);
		printf ("RECIEVED SIGNAL %lx\n", i);
	}

	printf ("Tell me when to shut down: ");
	(void) getchar();

	printf ("Away I go!\n");
	if (DACPsignal != 0)	/* i.e. logged-in */
	{
		log_dacp.log_status = LOGOUT_DACP;
		PutMsg(mp, &log_dacp);
printf ("Awaiting acknowledgement\n");
		WaitPort(mp1);
printf ("DISCONNECTED FROM DACP\n");
		FreeSignal(DACPSigNum);
	}
		DeletePort(mp1);
}
