
/******
 * Include vari
 ******/

#include "ProgED:sources/include/Ped.h"
#include <stdio.h>
#include <stdlib.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <rexx/errors.h>



/******
 * Prototipi funzioni
 ******/

LONG ASM SAVEDS CmdBeep( RG(a0) struct CommandData * );
void ApriMiaFinestra( void );
void ChiudiMiaFinestra( void );
void SendMessageToPED( struct APIMessage * );



/******
 * Variabili globali
 ******/

struct IntuitionBase	*IntuitionBase;
struct Window		*PEDWindow;
struct Screen		*PEDScreen;
struct MsgPort		*MyPort,
			*PEDPort;

static int tre=3;

struct ArexxExtCmds	MyCmdBeep={
TRUE,			/* TRUE=Comando esterno */
"BEEP",			/* Nome del comando */
"NUM/N,FLAG/S",		/* Template ReadArgs */
{&tre,0,},		/* Vettore di default per gli argomenti */
CmdBeep,		/* Funzione di gestione del comando */
NULL			/* Porre a NULL e non manipolare! Grazie. *****/
};

struct APIClient	MyClient={
NULL,					/* Questo campo sara' riempito dopo... */
NOTIFY_ON_SHOW_HIDE|NOTIFY_ON_KEY,	/* Dimmi quando chiudi e riapri il tuo schermo e quando l'utente batte un tasto*/
"prova cliente",			/* Nome del cliente (non utilizzato ancora) */
NULL					/* Porre a NULL e non manipolare! Grazie. *****/
};



/*****
 *
 * FUNZIONE:	void main(void)
 *
 * SCOPO:	Cerca la porta API del ProgED, si registra come cliente ed
 *		apre una finestra sullo schermo del ProgED. Aggiunge un
 *		comando interno BEEP che, se eseguito da ProgED, il
 *		solo effetto di chiamare la funzione DisplayBeep (vedi
 *		la funzione CmdBeep).
 *
 * RESTITUISCE: -
 *
 ****/

void main(void)
{
	struct APIMessage	 msg,
				*mess;
	struct IntuiMessage	*imsg;
	UBYTE			 finito;



	/***** Cerca la porta del ProgED *****/
	if (!(PEDPort=FindPort("PED_API")))
	{
		printf("Can't find ProgED API Port!\n");
		exit(0);
	}

	/***** Crea una porta che utilizzeremo per ricevere i msg dal ProgED *****/
	if (!(MyPort=CreateMsgPort()))
	{
		printf("Can't create a MsgPort!\n");
		exit(0);
	}

	/***** Riempi il campo ac_ClientPort della struttura APIClient che
		utilizzeremo per la registrazione *****/
	MyClient.ac_ClientPort=MyPort;

	/***** Apri intuition.library :-)) *****/
	if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0)))
	{
		DeleteMsgPort(MyPort);
		printf("Can't open intuition.library!\n");
		exit(0);
	}

	/***** Registriamoci come cliente presso il ProgED *****/
	msg.am_MsgType=PED_API_REGISTER;
	msg.am_MsgArg[0]=(ULONG)&MyClient;
	SendMessageToPED(&msg);

	/***** Aggiungi il comando interno BEEP *****/
	msg.am_MsgType=PED_API_ADD_INTERNAL_COMMAND;
	msg.am_MsgArg[0]=(ULONG)&MyCmdBeep;
	SendMessageToPED(&msg);

	/***** Ottiene l'indirizzo dello schermo del ProgED *****/
	msg.am_MsgType=PED_API_GET_SCREEN_ADDRESS;
	SendMessageToPED(&msg);

	/***** Se e' non nullo apri una finestra. Se e' NULL ProgED e'
		attualmente iconificato! In questo caso ci spedira' un
		messaggio per farci sapere quando aprire la finestra. *****/
	if (msg.am_RC)	ApriMiaFinestra();

	/***** Loop principale *****/
	finito=FALSE;
	while(!finito)
	{
		WaitPort(MyPort);
		while(mess=(struct APIMessage *)GetMsg(MyPort))
		{
			switch(mess->am_MsgType)
			{
				/***** ProgED sta' per terminare! Terminiamo
					anche noi :-( *****/
				case	PED_API_QUIT:
					finito=TRUE;
				break;

				/***** ProgED sta' per chiudere il suo schermo.
					Chiudiamo la nostra finestra. *****/
				case	PED_API_HIDE:
					ChiudiMiaFinestra();
				break;

				/***** ProgED ha riaperto il suo schermo.
					Riapriamo la nostra finestra. *****/
				case	PED_API_SHOW:
					ApriMiaFinestra();
				break;

				/***** L'utente ha battuto il tasto RETURN *****/
				case	PED_API_KEY:
					imsg=(struct IntuiMessage *)mess->am_MsgArg[0];
					if (imsg->Code==196)	DisplayBeep(0);
				break;
			}

			mess->am_RC=0;

			ReplyMsg((struct Message *)mess);
		}
	}

	/***** Libera tutto ed esci *****/
	DeleteMsgPort(MyPort);
	CloseLibrary((struct Library *)IntuitionBase);
	exit(0);
}



/*****
 *
 * FUNZIONE:	void ApriMiaFinestra(void)
 *
 * SCOPO:	Apre una finestra sullo schermo del ProgED.
 *
 * RESTITUISCE: -
 *
 ****/

void ApriMiaFinestra(void)
{
	PEDWindow=OpenWindowTags(NULL,
		WA_Width,		100,
		WA_Height,		80,
		WA_Title,		"Prova !!!!!!!!",
		WA_DepthGadget,		TRUE,
		WA_SizeGadget,		TRUE,
		WA_DragBar,		TRUE,
		WA_SmartRefresh,	TRUE,
		WA_PubScreenName,	"PED_SCREEN",
	TAG_DONE);
}



/*****
 *
 * FUNZIONE:	void ChiudiMiaFinestra(void)
 *
 * SCOPO:	Chiude la finestra aperta tramite ApriMiaFinestra().
 *
 * RESTITUISCE: -
 *
 ****/

void ChiudiMiaFinestra(void)
{
	if (PEDWindow)	CloseWindow(PEDWindow);
	PEDWindow=NULL;
}



/*****
 *
 * FUNZIONE:	LONG CmdBeep(struct CommandData *data)
 *
 * SCOPO:	Questa e' la funzione di gestione del comando BEEP.
 *		Come e' possibile notare gli argomenti vengono estratti
 *		dal vettore data->CommandArg[]. ProgED ha gia' provveduto
 *		a chiamare la funzione ReadArgs per noi.
 *
 *		Questo comando effettua un singolo beep se l'utente
 *		ha specificato il flag "FLAG" o "n" BEEP se l'utente
 *		utilizza il comando nella forma "BEEP NUM <n>".
 *
 * RESTITUISCE: -
 *
 ****/

LONG ASM SAVEDS CmdBeep(RG(a0) struct CommandData *data)
{
	long	num=*((LONG *)data->CommandArgs[0]),
		flag=(long)data->CommandArgs[1];
	int	i;

	if (flag)
	{
		for(i=0;i<num;i++)
		{
			DisplayBeep(NULL);
			Delay(50);
		}
	}
	else	DisplayBeep(NULL);

	return(RC_OK);
}



/*****
 *
 * FUNZIONE:	void SendMessageToPED(struct APIMessage	*msg)
 *
 * SCOPO:	Questa funzione spedisce un messaggio alla porta APi del
 *		ProgED.
 *
 * RESTITUISCE: -
 *
 ****/

void SendMessageToPED(struct APIMessage	*msg)
{
	msg->am_Message.mn_Node.ln_Succ=NULL;
	msg->am_Message.mn_Node.ln_Pred=NULL;
	msg->am_Message.mn_Node.ln_Type=NT_MESSAGE;
	msg->am_Message.mn_Node.ln_Pri=0;
	msg->am_Message.mn_Node.ln_Name=NULL;
	msg->am_Message.mn_ReplyPort=MyPort;
	msg->am_Message.mn_Length=sizeof(struct APIMessage);

	PutMsg(PEDPort,(struct Message *)msg);
	WaitPort(MyPort);
	while(GetMsg(MyPort));
}
