/* Beispielprogramm zur Ermittlung der AES-Zeichenstze (IBM und SMALL)
	16.12.93 Harald Sommerfeldt */

#include <aes.h>
#include <vdi.h>
#include <tos.h>
#include <stdio.h>

#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif

/* Wert eines Cookies holen, zurckgegeben wird TRUE bei Erfolg */

typedef struct {
	long	cookie, value;
} cookiejar;

int	get_cookie( long cookie, long *value )
{
	cookiejar *_p_cookies;
	void	*sp;

	sp = (void *)Super( NULL );
	_p_cookies = *(void **)0x5A0;		/* Zeiger auf Cookiejar holen */
	Super( sp );
	if ( _p_cookies != NULL ) {		/* wenn Cookiejar installiert ... */
												/* ... dann Cookies abklappern ... */
		for (; _p_cookies->cookie != 0L; _p_cookies++ ) {
			if ( _p_cookies->cookie == cookie ) {
												/* ... bis der gewnschte Eintrag ist */
				if ( value != NULL ) *value = _p_cookies->value;
				return TRUE;
			}
		}
	}
	return FALSE;
}


/* appl_getinfo() fr Pure-C */
AESPB aespb = { _GemParBlk.contrl, _GemParBlk.global, _GemParBlk.intin,
                _GemParBlk.intout, (int *)_GemParBlk.addrin, (int *)_GemParBlk.addrout };

int	appl_getinfo( int type, int *out1, int *out2, int *out3, int *out4 )
{
	_GemParBlk.intin[0] = type;
	_GemParBlk.contrl[0] = 130;
	_GemParBlk.contrl[1] = 1;
	_GemParBlk.contrl[2] = 5;
	_GemParBlk.contrl[3] = 0;
	_crystal( &aespb );
	*out1 = _GemParBlk.intout[1];
	*out2 = _GemParBlk.intout[2];
	*out3 = _GemParBlk.intout[3];
	*out4 = _GemParBlk.intout[4];
	return _GemParBlk.intout[0];
}

#define ap_version	_GemParBlk.global[0]	/* AES-Versionsnummer (z.B. 0x140 = 1.40) */


int	vdi_handle, fonts_loaded;

int	appl_getfontinfo( int ap_gtype, int* height, int* id, int* type )
{
	int	dummy, attrib[10];
	long	value;

	/* AES 4.0 oder Mag!X 2.0? */
	if ( ap_version >= 0x400 || (get_cookie( 'MagX', &value ) && ((short **)value)[2][24] >= 0x200) ) {
		dummy = appl_getinfo( ap_gtype, height, id, type, &dummy );

		/* Patzer von Mag!X 2.0 umgehen */
		if ( *id == -12124 ) *id = 1;

		/* ggf. GDOS-Zeichenstze laden */
		if ( dummy && vst_font( vdi_handle, *id ) != *id && !fonts_loaded && vq_gdos() ) {
			vst_load_fonts( vdi_handle, 0 );
			fonts_loaded = TRUE;
		}

		return dummy;
  }

	/* ansonsten akt. AES-Zeichensatz nehmen */
	vqt_attributes( graf_handle( &dummy, &dummy, &dummy, &dummy ), attrib );
	*id     = attrib[0];

	/* ggf. GDOS-Zeichenstze laden */
	if ( vst_font( vdi_handle, *id ) != *id && !fonts_loaded && vq_gdos() ) {
		vst_load_fonts( vdi_handle, 0 );
		fonts_loaded = TRUE;
	}

	/* beim SMALL-Font nehmen wir die kleinste mgliche Hhe,
	   beim IBM-Font die akt. beim AES eingestellte Hhe */
	if ( ap_gtype == 1 ) {
		vst_font( vdi_handle, *id );
		vst_point( vdi_handle, 1, &dummy, &dummy, &dummy, &dummy );
		vqt_attributes( vdi_handle, attrib );
	}
	*height = attrib[7];
	*type   = 0;

	return TRUE;
}


int	main( void )
{
	int	work_in[11] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 };
	int	work_out[57];
	int	height, id, speedo, dummy;
	long	l;

	appl_init();
	vdi_handle = graf_handle( &dummy, &dummy, &dummy, &dummy );
	/* AES 4.0 oder Mag!X 2.0? */
	if ( ap_version >= 0x400 || (get_cookie( 'MagX', &l ) && ((short **)l)[2][24] >= 0x200) )
		appl_getinfo( 2, &work_in[0], &dummy, &dummy, &dummy );
	else
		work_in[0] = 2 + Getrez();
	v_opnvwk( work_in, &vdi_handle, work_out );
	appl_getfontinfo( 0, &height, &id, &speedo );
	printf( "IBM: %d, %d, %d\n", height, id, speedo );
	appl_getfontinfo( 1, &height, &id, &speedo );
	printf( "SMALL: %d, %d, %d\n", height, id, speedo );
	if ( fonts_loaded ) vst_unload_fonts( vdi_handle, 0 );
	v_clsvwk( vdi_handle );
	appl_exit();
	return 0;
}
