/************************************************************************
 *	TITLE:			JAZPAT.C				*
 *	PROJECT:		JAGUAR/JAZ				*
 *	FUNCTION:		MAIN JAZ MODULE				*
 *	PROGRAMMER:		Hans-Martin Krober			*
 *	DATE:			5/95					*
 *									*
 *              COPYRIGHT 1993,1994 Atari U.S. Corporation              *
 *          UNATHORIZED REPRODUCTION, ADAPTATION, DISTRIBUTION,         *
 *          PERFORMANCE OR DISPLAY OF THIS COMPUTER PROGRAM OR          *
 *        THE ASSOCIATED AUDIOVISUAL WORK IS STRICTLY PROHIBITED.       *
 *                            ALL RIGHTS RESERVED.                      *
 *									*
 ************************************************************************/

#include <stdarg.h>
#include "portab.h"
#include "slibc.h"
#include "glib.h"
#include "gemlib.h"
#include "jazobj.h"
#include "jazsyn.h"
#include "ptchptch.h"
#include "dos.h"

/*
 * 	EXTERNAL VARIABLES
 */

EXTERN OBJECT	jaztree[];
EXTERN WORD	scrn_w, scrn_h;
EXTERN BYTE	filebuf[];
EXTERN ULONG	*_xvalue;
EXTERN LONG	patchtab[];			/* buffer for patches */

/*
 *	Prototypes
 */
WORD	build_pedit(VOID);
WORD	find_keyword(BYTE*);
WORD	pe_title(OBJECT *, WORD, BYTE *);
WORD	pe_line(OBJECT *, WORD, BYTE *);
WORD	pe_packedwave(OBJECT *, WORD, BYTE *);
WORD	pe_wave(OBJECT *, WORD, BYTE *);
WORD	pe_int(OBJECT *, WORD, BYTE *);
WORD	pe_float(OBJECT *, WORD, BYTE *);
WORD	pe_env(OBJECT *, WORD, BYTE *);

/*
 *	GLOBAL VARIABLES
 */

/*
 *	Here we store up the object screens for patch editing
 */

T_PEDIT	peditdat[64];

/*
 *	This is a table which contains the keywords for the patch layout file
 */

T_KEYW keyword_tab[] = {
	{ "END", NULL },
	{ "title", pe_title },
	{ "line", pe_line },
	{ "packedwave", pe_packedwave }, 
	{ "waveform", pe_wave }, 
	{ "int", pe_int },
	{ "float", pe_float },
	{ "env", pe_env },
	{ "", NULL }
};

/*
 *	Waveform stuff
 */

#define WAVETAB_NITEMS	12
#define WAVETAB_MAXLEN	6


BYTE	*wavestrtab[] = {
	"TRI",
	"SINE",
	"AMSINE",
	"SINE12",
	"CHIRP",
	"NTRI",
	"DELTA",
	"NOISE",
	"USER 1",
	"USER 2",
	"USER 3",
	"USER 4",
#if 0
	"USER 5"
#endif
};


#define PKDWAVETAB_NITEMS	3
#define PKDWAVETAB_MAXLEN	8

BYTE	*pkdwavestrtab[] = {
	"SAWTOOTH",
	"SQUARE",
	"NOISE"
};

#if 0
LONG   wave_table[] = {
			ROM_TRI,
			ROM_SINE,
			ROM_AMSINE,
			ROM_12W,
			ROM_CHIRP16,
			ROM_NTRI,
			ROM_DELTA,
			ROM_NOISE,
			ROM_SINE,	/* 5 user-defined waves*/
			ROM_SINE,	/* Go HERE!!!          */	
			ROM_SINE,	
			ROM_SINE,
#if 0	
			ROM_SINE	
#endif
		      };

LONG  DSPEnvTable[] = { 		/* 5 DSP envelope addresses */
			( LONG )(DRAM + OFF_ENVS + 0L ),
			( LONG )(DRAM + OFF_ENVS + 128L ),
			( LONG )(DRAM + OFF_ENVS + 256L ),
			( LONG )(DRAM + OFF_ENVS + 384L ),
			( LONG )(DRAM + OFF_ENVS + 512L ),
			( LONG )(DRAM + OFF_ENVS + 640L ),
			( LONG )(DRAM + OFF_ENVS + 768L ),
			( LONG )(DRAM + OFF_ENVS + 896L ),
		      };


LONG   UWAVE1[128];	/* User-Defined Waveform Storage areas during LOAD WaveForms */
LONG   UWAVE2[128];	/* Each are 512 bytes long				     */
LONG   UWAVE3[128];
LONG   UWAVE4[128];
LONG   UWAVE5[128];

LONG   *UWAVE_TABLE[] = {
			  UWAVE1,
			  UWAVE2,
			  UWAVE3,
			  UWAVE4,
#if 0
			  UWAVE5,
#endif
		        };

LONG nwave;
#endif


/*
 *	FUNCTIONS
 */
	WORD
build_pedit()
{
	BYTE		buf[256];
	REG WORD	i = 0;
	REG T_PEDIT	*p;
	WORD		indescr, inquote;
	WORD		n;
	WORD		obj;
	REG BYTE	*s, *t;
	MFILE 		*fp;
	WORD		more = FALSE;
	WORD		keyword;
	WORD		parent;
	BYTE		str[64];


	dos_read("playout.cnf");		


	if ((fp = mfopen(filebuf, "ra")) == NULL)
		return -1;

	if (mfgets(buf, 256, fp) == NULL)
		return -1;
	if (strstr(buf, "ATPLAYOUT") == NULL) {
		form_alert(FRM_ALERT, "[Not a valid JAZ layout.][ OK ]");
		return -1;
	}
	indescr = FALSE;
	while (mfgets(buf, 256, fp) != NULL) {

		s = buf;
		for (; *s && (*s == ' ') && (*s == '\t'); )
			s++;
		if (!*s)
			continue;

		if (*s == '#' || *s == ';' || *s == '*')
			continue;
		inquote = 0;
		for (t = s; *t; t++)
			if (*t == '\"')
				inquote ^= 1;
			
			if (!inquote && (*t == '#' || *t == ';' || *t == '*')) {
				*t = '\0';
				break;
			}
		
		if ((n = sscanf(s, "MDTAB_%s:", &str)) == 1) {
			p = &peditdat[i++];
			strcpy(p->name, "MDTAB_");
			strcat(p->name, str);
			indescr = TRUE;
			obj = ROOT;
			objc_xadd(p->tree, NIL, obj++, 
				G_BOX, -1, -1, 
				0, 0, scrn_w, scrn_h,
				NONE, NORMAL,
				0);	
		}
		if ((keyword = find_keyword(s)) == -1) {
			continue;
		}
		if (keyword == 0) {
			objc_xadd(p->tree, ROOT, parent = obj++, 
				G_XBAR, -1, -1, 
				4, 183, 372, 15,
				IS3DOBJ|JOYPAD|SELECTABLE, NORMAL,
/*				T_OB_DATTACH, ROOT, 0,*/
				T_BA_NUM, 2,
				T_BA_FIRST, 1,
				0);
			objc_xadd(p->tree, parent, obj++, 
				G_XBUTTON, -1, -1, 
				0, 2, 80, 10,
				ABUTTON|IS3DOBJ|SELECTABLE, NORMAL,
				T_BT_TEXT, " OK ",
				T_BT_BGCOLOR, 0,
				T_BT_TXTCOLOR, 1,
				T_BT_FONTID, 1,
				T_BT_SMCHAR, '1',
				T_OB_LATTACH, parent, 10,
				T_OB_UATTACH, parent, 0,
				/*T_BT_CALLBK, pstab_handler,*/
				0);
			objc_xadd(p->tree, parent, obj++, 
				G_XBUTTON, -1, -1, 
				0, 2, 80, 10,
				ABUTTON|IS3DOBJ|SELECTABLE, NORMAL,
				T_BT_TEXT, " Cancel ",
				T_BT_BGCOLOR, 0,
				T_BT_TXTCOLOR, 1,
				T_BT_FONTID, 1,
				T_BT_SMCHAR, '2',
				T_OB_RATTACH, parent, 10,
				T_OB_UATTACH, parent, 0,
				/*T_BT_CALLBK, pstab_handler,*/
				0);

			indescr = FALSE;
			p++;
			continue;
		}
		if (!indescr) {
			form_alert(FRM_ALERT, "[Statement outside|patch layout.][ OK ]");
			continue;
		}
		keyword_tab[keyword].parsefct(p->tree, obj++, s);
	}

	mfclose(fp);
}


	WORD
find_keyword(str)
	BYTE *str;
{
	REG WORD i;
	REG T_KEYW *p;

	p = keyword_tab;
	for (i = 0; p->name[0]; p++, i++)
		if (strstr(str, p->name) != NULL)
			return i;
	return -1;
}

	WORD
pe_title(tree, obj, s)
	OBJECT *tree;
	WORD obj;
	REG BYTE *s;
{
	REG BYTE *t;
	BYTE buf[32];

	for (s, t = buf; *s && (*s != '\"'); s++);
	if (*s) 
		for (s++; *s && (*s != '\"'); )
			*t++ = *s++;
 	*t = '\0';

	objc_xadd(tree, ROOT, obj, 
		G_XBOXTEXT, -1, -1, 
		0, 0, 0, 9,
		IS3DOBJ|NONE, NORMAL,
		T_OB_LATTACH, ROOT, 4,		
		T_OB_RATTACH, ROOT, 4,		
		T_OB_UATTACH, ROOT, 0,
		T_BX_TEXT, buf,
		T_BX_FONTID, FNT_MEDIUM,
		T_BX_ALIGN, TE_CNTR,
		0);
	return 0;
}

GRECT line_xy[] = {
	{4, 14, 126, 6 },
	{130, 14, 124, 6 },
	{254, 14, 124, 6 },
	{4, 56, 126, 6 },
	{130, 56, 124, 6 },
	{254, 56, 124, 6 },
	{4, 98, 126, 6 },
	{130, 98, 124, 6 },
	{254, 98, 124, 6 },
	{4, 140, 126, 6 },
	{130, 140, 124, 6 },
	{254, 140, 124, 6 }
};

GRECT box_xy[] = {
	{4, 22, 120, 32 },
	{130, 22, 120, 32 },
	{254, 22, 120, 32 },
	{4, 64, 120, 32 },
	{130, 64, 120, 32 },
	{254, 64, 120, 32 },
	{4, 106, 120, 32 },
	{130, 106, 10, 32 },
	{254, 106, 120, 32 },
	{4, 148, 120, 32 },
	{130, 148, 120, 32 },
	{254, 148, 120, 32}
	
};

	WORD
pe_line(tree, obj, s)
	OBJECT *tree;
	WORD obj;
	REG BYTE *s;
{
	WORD x, y, w;
	REG WORD i, l;
	REG BYTE *t;
	BYTE *str;
	BYTE buf[64];
	WORD aslot, bslot;

	if (sscanf(s, "line, %d, %d", &aslot, &bslot) != 2)
		return -1;

	if ((str = strchr(s, '\"')) == NULL)
		str = "----";
		
	x = line_xy[aslot].g_x;
	y = line_xy[aslot].g_y;

	w =  line_xy[bslot].g_x;
	w += line_xy[bslot].g_w;

	l = strlen(str) - 1;	/* don't count " */

	l = ((w / FNT_SHEIGHT - l) / 2) - 1;	
	for (i = 0, s = buf ; i < l; i++)
		*s++ = '-';
	*s++ = ' ';
	for (t = str+1; *t && (*t != '\"'); )
		*s++ = *t++;
	*s++ = ' ';
#if 1
	if ((l % 2) == 0)
		l += 1;
#endif
	for (i = 0 ; i < l; i++)
		*s++ = '-';
	*s = '\0';

	objc_xadd(tree, ROOT, obj, 
		G_XTEXT, -1, -1, 
		x, y, w, 6,
		NONE, NORMAL,
		T_BX_TEXT, buf,
		T_BX_FONTID, FNT_SMALL,
		T_BX_ALIGN, TE_LEFT,
		0);

	return 0;
}

	WORD
pe_packedwave(tree, obj, s)
	OBJECT *tree;
	WORD obj;
	BYTE *s;
{
	REG BYTE *t, *u;
	WORD x, y, w, h;
	BYTE name[32];
	WORD slot;
	WORD offset;
	WORD hilo;

	if ((t = strchr(s, '\"')) == NULL)
		strcpy(name, "  ");
	else {
		for (u = name, t += 1; *t && (*t != '\"'); ) {
			*u++ = *t++;
		}
		*u = '\0';
	}
	if (sscanf(s, "packedwave, %d, %d, %d", &hilo, &slot, &offset) != 3) {
		return -1;
	}

	x = box_xy[slot].g_x;
	y = box_xy[slot].g_y;
	w = box_xy[slot].g_w;
	h = box_xy[slot].g_h;

	objc_xadd(tree, ROOT, obj,
		G_XTXTSLIDER, -1, -1,
		x, y, w, h,
		JOYPAD|IS3DOBJ|SELECTABLE, NORMAL,
		T_TXT_SLNAME, name,
		T_TXT_FONTID, FNT_MEDIUM,
		T_TXT_TABLE, pkdwavestrtab,
		T_TXT_MAXITEMS, PKDWAVETAB_NITEMS,
		T_TXT_MAXLEN, PKDWAVETAB_MAXLEN,
		0);

}

	WORD
pe_env(tree, obj, s)
	OBJECT *tree;
	WORD obj;
	BYTE *s;
{
	return 0;
}
	WORD
pe_wave(tree, obj, s)
	OBJECT *tree;
	WORD obj;
	BYTE *s;
{
	REG BYTE *t, *u;
	WORD x, y, w, h;
	BYTE name[32];
	WORD slot;
	WORD offset;

	if ((t = strchr(s, '\"')) == NULL)
		strcpy(name, "  ");
	else {
		for (u = name, t += 1; *t && (*t != '\"'); ) {
			*u++ = *t++;
		}
		*u = '\0';
	}
	if (sscanf(s, "waveform, %d, %d",&slot, &offset) != 2) {
		return -1;
	}

	x = box_xy[slot].g_x;
	y = box_xy[slot].g_y;
	w = box_xy[slot].g_w;
	h = box_xy[slot].g_h;

	objc_xadd(tree, ROOT, obj,
		G_XTXTSLIDER, -1, -1,
		x, y, w, h,
		JOYPAD|IS3DOBJ|SELECTABLE, NORMAL,
		T_TXT_SLNAME, name,
		T_TXT_FONTID, FNT_MEDIUM,
		T_TXT_TABLE, wavestrtab,
		T_TXT_MAXITEMS, WAVETAB_NITEMS,
		T_TXT_MAXLEN, WAVETAB_MAXLEN,
		0);

	return 0;
}

	WORD
pe_int(tree, obj, s)
	OBJECT *tree;
	WORD obj;
	BYTE *s;
{
	REG BYTE *t, *u;
	WORD x, y, w, h;
	BYTE name[32];
	ULONG max, xv;
	WORD slot;
	WORD offset;
	WORD len;

	if ((t = strchr(s, '\"')) == NULL)
		strcpy(name, "  ");
	else {
		for (u = name, t += 1; *t && (*t != '\"'); ) {
			*u++ = *t++;
		}
		*u = '\0';
	}
	if (sscanf(s, "int, $%lx, %d, %d", &max, &slot, &offset) != 3) {
	    if (sscanf(s, "int, %ld, %d, %d", &max, &slot, &offset) != 3) {	
		return -1;
	    }
	}

	x = box_xy[slot].g_x;
	y = box_xy[slot].g_y;
	w = box_xy[slot].g_w;
	h = box_xy[slot].g_h;

	for (len = 0, xv = max; xv > 0L; len++)
		xv = xv / 10L;
	
	objc_xadd(tree, ROOT, obj,
		G_XINTSLIDER, -1, -1,
		x, y, w, h,
		JOYPAD|IS3DOBJ|SELECTABLE, NORMAL,
		T_INT_SLNAME, name,
		T_INT_FONTID, FNT_MEDIUM,
		T_INT_INTSPACES, len,
		T_INT_MAXINT, max,
		0);
	return 0;
}

	WORD
pe_float(tree, obj, s)
	OBJECT *tree;
	WORD obj;
	BYTE *s;
{
	REG BYTE *t, *u;
	WORD x, y, w, h;
	BYTE name[32];
	ULONG maxi, maxf, xv;
	WORD slot;
	WORD offset;
	WORD leni, lenf;

	if ((t = strchr(s, '\"')) == NULL)
		strcpy(name, "  ");
	else {
		for (u = name, t += 1; *t && (*t != '\"'); ) {
			*u++ = *t++;
		}
		*u = '\0';
	}
	if (sscanf(s, "float, $%lx.%lx, %d, %d", &maxi, &maxf, &slot, &offset) != 4) {
	    if (sscanf(s, "float, %ld.%ld, %d, %d", &maxi, &maxf, &slot, &offset) != 4) {	
		return -1;
	    }
	}

	x = box_xy[slot].g_x;
	y = box_xy[slot].g_y;
	w = box_xy[slot].g_w;
	h = box_xy[slot].g_h;

	for (leni = 0, xv = maxi; xv > 0L; leni++)
		xv = xv / 10L;
	for (lenf = 0, xv = maxf; xv > 0L; lenf++)
		xv = xv / 10L;
	
	objc_xadd(tree, ROOT, obj,
		G_XFLTSLIDER, -1, -1,
		x, y, w, h,
		JOYPAD|IS3DOBJ|SELECTABLE, NORMAL,
		T_FLT_SLNAME, name,
		T_FLT_FONTID, FNT_MEDIUM,
		T_FLT_INTSPACES, leni,
		T_FLT_FRACSPACES, lenf,
		T_FLT_MAXINT, maxi,
		T_FLT_MAXFRAC, maxf,
		0);
	return 0;
}

#if 0
/* Initialize the UserDefined Waves to ROM_SINE */
void
InitWTable( void )
{
    LONG i;
    LONG j;
    ULONG *sptr,*dptr;

    nwave = 1L;
    
    wave_table[8] = user_wave[0];
    wave_table[9] = user_wave[1];
    wave_table[10] = user_wave[2];
    wave_table[11] = user_wave[3];
#if 0
    wave_table[12] = user_wave[4];
#endif
    for( i = 0; i < 4 /* 5 */; i++ ) {

	sptr = ( LONG *)ROM_SINE;
        dptr = ( LONG *)user_wave[i];
        
        for( j = 0L; j < 128L; j++ ) {
	   *dptr++ = *sptr++;
        }
    }
}


#endif

