#include <ctype.h>
#include <stddef.h>
#include "intern.h"

/*
 * Der MagX-Cookie
 */

typedef struct
{
	char		*in_dos;		 	  				/* Adresse der DOS- Semaphore */
	int		*dos_time; 						/* Adresse der DOS- Zeit */
	int		*dos_date; 						/* Adresse des DOS- Datums */
	long		res1;
	long		res2;
	long		res3;
	void		*act_pd;		 	  				/* Laufendes Programm */
	long		res4;
	int		res5;
	void		*res6;
	void		*res7;							/* interne DOS- Speicherliste */
	void		(*resv_intmem)(void);	 	/* DOS- Speicher erweitern */
	long		(*etv_critic)(void);			/* etv_critic des GEMDOS */
	char*		((*err_to_str)(char e)); 	/* Umrechnung Code->Klartext */
	long		res8;
	long		res9;
	long		res10;
} DOSVARS;


typedef struct
{
	long 	magic;								/* muž $87654321 sein */
	void 	*membot;		   					/* Ende der AES- Variablen	*/
	void 	*aes_start;		   				/* Startadresse */
	long 	magic2;			 					/* ist 'MAGX' */
	long 	date; 								/* Erstelldatum ttmmjjjj */
	void 	(*chgres)(int res, int txt);	/* Aufl”sung „ndern */
	long 	(**shel_vector)(void);  		/* residentes Desktop */
	char 	*aes_bootdrv; 		 				/* von hieraus wurde gebootet	*/
	int	*vdi_device;						/* vom AES benutzter VDI-Treiber */
	void	*reservd1;
	void 	*reservd2;
	void 	*reservd3;
	int 	version;		   					/* z.B. $0201 ist V2.1 */
	int	release;		   					/* 0=alpha..3=release */
} AESVARS;


typedef struct
{
	long		config_status;
	DOSVARS	*dosvars;
	AESVARS	*aesvars;
	void		*res1;
	void		*hddrv_functions;
	long		status_bits;
} MAGX_COOKIE;


int get_magx_version(void)
{
	long			v;
	MAGX_COOKIE	*magxCook;
	int			version;
	
	if (getcookie("MagX", &v))
	{
		magxCook = (MAGX_COOKIE *)v;
		version = magxCook->aesvars->version;
	}
	else
		version = 0;
	return version;
}


int get_magx_obj(OBJECT	*tree, int obj)
{
	short		us, typ, state, flags, ret;
	
	flags = tree[obj].ob_flags;
	state = tree[obj].ob_state & 0x00ff;
	us = (tree[obj].ob_state & 0xff00) >> 8;

	typ = tree[obj].ob_type & 0x00ff;
	if (typ == G_USERDEF)
		typ = (tree[obj].ob_type & 0xff00) >> 8;

	if (state & WHITEBAK)
	{
		ret = MX_UNKNOWN;
		switch (typ)
		{
			case G_STRING :
				if (us == 0xff)				/* komplett unterstrichen */
					ret = MX_UNDERLINE;
				else if ((us & 0xf0) == 0)	/* String mit Shortcut */
					ret = MX_SCSTRING;
				break;

			case G_BUTTON :
				if (us == 0xfe)				/* Gruppenrahmen */
				{
					if (state & CHECKED)
						ret = MX_GROUPBOX2;	/* - kleine Schrift */
					else
						ret = MX_GROUPBOX;	/* - normal */
				}
				else if (us & 0x80)			/* bit15 -> MagiC-Knopf */
				{
					if (flags & RBUTTON)		/* Radio */
					{
						if (us == 0xff)
							ret = MX_RADIO;	/* ohne */
						else
							ret = MX_SCRADIO;	/* mit Shortcut */
					}						
					else							/* Kreuz */
					{
						if (us == 0xff)
							ret = MX_CHECK;	/* ohne */
						else
							ret = MX_SCCHECK;	/* mit ShortCut */
					}
				}
				else if (flags & EXIT) 		/* Exit-Knopf */
					ret = MX_SCEXIT;
				break;
		}
	}
	else if ((typ == G_FTEXT) && (flags & FL3DBAK) && 
				(tree[obj].ob_spec.tedinfo->te_thickness == -2))
		ret = MX_EDIT3D;
	else
		ret = MX_NOTXOBJ;
	return ret;	
}


int get_magx_shortcut(OBJECT *tree, int obj, char *c)
{
	int	pos = -1, us, mtyp;
	char	*p;

	if (c != NULL)
		*c = '\0';
	mtyp = get_magx_obj(tree, obj);
	
	if ((mtyp == MX_SCRADIO) || (mtyp == MX_SCCHECK) || 
	    (mtyp == MX_SCEXIT) || (mtyp == MX_SCSTRING))
	{
		if ((tree[obj].ob_type & 0x00ff) == G_USERDEF)
			p = (char *)tree[obj].ob_spec.userblk->ub_parm;
		else
			p = tree[obj].ob_spec.free_string;
		us = (tree[obj].ob_state & 0xff00) >> 8;
		if (us != 0xFF)
		{
			pos = us & 0xf;
			if (c != NULL)
				*c = p[pos];
		}
	}
	return pos;
}
