/********************************************************************
 *																	*
 *	GEM eXtended Accessory protocol (communications protocol)		*
 *	Released by Atari Corp, modified by Ken Hollis					*
 *																	*
 *	Copyright (c) Atari Corp, Bitgate Software, and Clever Bits 1994*
 *	All rights reserved.											*
 *																	*
 *	This is the standard protocol for sending text, image, and		*
 *	metafile information to accessories.  Use at your own risk,		*
 *	since we've not tested them fully.								*
 *																	*
 ********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tos.h>
#include "winlib.h"

LOCAL int appl_id, menu_id, msg[8], ToAccApp = 0;
LOCAL long tmp_count = 0;

#define	XNAME "WinLIB Test 0.64\0XDSC\01xWinLIB Custom Test\0\0"

LOCAL char *XACCName;

GLOBAL void XACC_Send(int sendto, int msg0, int msg3, char *msg4, int msg6, int msg7)
{
	int     msg[8];
  
	msg[0]			 = msg0;
	msg[1]			 = Ap_ID;
	msg[2]			 = 0;
	msg[3]			 = msg3;
	*(char**)&msg[4] = msg4;
	msg[6]			 = msg6;
	msg[7]			 = msg7;

	appl_write(sendto, 16, msg);
}

GLOBAL void XACC_Send_Id(int sendto)
{
	XACC_Send(sendto, ACC_ID, 0x0103, XACCName, menu_id, -1);
}

GLOBAL void XACC_Send_Ack(int sendto, int answer)
{
	XACC_Send(sendto, ACC_ACK, answer, NULL, -1, -1);
}

GLOBAL void XACC_Send_AccClose(void)
{
	if (!_app && !(_GemParBlk.global[0] >= 0x0400 && _GemParBlk.global[1] == -1))
		XACC_SendId(ToAccApp);
}

GLOBAL void XACC_SendStartup(void)
{
	int     next, type, id;
	char    name[10], *buf;
	long	*MiNTCOOKIE;

	get_cookie('MiNT', &MiNTCOOKIE);  

	if (AES_VERSION >= 0x0400 && MiNTCOOKIE != NULL) {
		buf = Mxalloc(sizeof(XNAME), 0x0022);

		if (buf != NULL)
			memcpy(buf, XACCName, sizeof(XNAME));

		XACCName = buf;
		next = 0;

		while (appl_search(next, name, &type, &id)) {
			if (type & 0x06)
				XACC_SendId(id);
			next = 1;
		}
	} else {
		if (!_app)
			XACC_SendAccClose();
	}
}

GLOBAL void getfile(char **input, long *length)
{
	char	path[220];
	char	file[20], *s;
	int     ret, ex, fh;
	
	*input = NULL;
	*length = 0;
	path[0] = '\0';
	file[0] = '\0';

	ret = fsel_input(path, file, &ex);

	if (ret == 0 || ex == 0)
		return;
	
	s = strrchr(path, '\\');

	if (s == NULL)
		return;
	
	strcpy(s+1, file);
	fh = (int) Fopen(path, FO_READ);

	if (fh < 0)
		return;
	
	*length = Fseek(0L, fh, SEEK_END);

	Fseek(0L, fh, SEEK_SET);

	*input = malloc(*length + 10L);

	if (*input == NULL) {
		Fclose(fh);
		return;
	}
	
	memset(*input, '\0', *length + 10L);
	Fread(fh, *length, *input);

	Fclose(fh);
}

GLOBAL int XACC_Wait(int *msg, int wait)
{
	int		event, dummy;
  
	msg[0] = 0;
	event = evnt_multi(MU_MESAG|MU_TIMER, 0, 0, 0,
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						msg, wait, 0, &dummy, &dummy,
						&dummy, &dummy, &dummy, &dummy);
	if (event & MU_MESAG) {
		if (msg[0] == 0x0500 && msg[3] == 1)
			return TRUE;
		if (msg[0] == AC_OPEN)
			return FALSE;
		if (msg[0] == AC_CLOSE)
			return FALSE;
		if (msg[0] == ACC_TEXT)
			return FALSE;
		if (msg[0] == ACC_IMG)
			return FALSE;
		if (msg[0] == ACC_META)
			return FALSE;
	}
	if (!(event & MU_MESAG))
		tmp_count++;

	return FALSE;
}
