#include "intern.h"

char __Ident_cflib[] = "$PatchLevel: CF library: "CFLIB_PATCHLEVEL" $";

/* intern global */
int	cf_vdi_handle;
int	cf_font_anz;
int	cf_msg_cb = FALSE;
int	cf_magxPC = FALSE;

/* intern lokal */
static int		hidden;
static int	rsc_load = FALSE;

void	init_app(char *rsc)
{
	int	d;
	long	l;
	int	work_out[57];
	char	s[128];
	char	path[256];

	debug("started\n");
	gl_apid = appl_init();
	gl_phys_handle = graf_handle(&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);
	wind_get_grect(0, WF_WORKXYWH, &gl_desk);
	graf_mouse(ARROW, 0x0L);			
	hidden = 0;
#ifdef __MINT__
	gl_gem = aes_global[0];
#else
	gl_gem = _GemParBlk.global[0];
#endif

	if (getcookie("MiNT", &l))
		gl_mint = (int)l;
	gl_magx = get_magx_version();
	cf_magxPC = getcookie("MgPC", NULL);
	if (getcookie("nAES", &l))
		gl_naes = *(unsigned short *)l;
	if (getcookie("NVDI", &l))
		gl_nvdi = *(unsigned short *)l;

	/* Systemzeichenstze bestimmen */
	if (appl_xgetinfo(0, &sys_big_height, &sys_big_id, &d, &d) == 0)
	{
		sys_big_id = 1;
		if ((Getrez() == 0) || (Getrez() == 1))	/* ST-Low/Mid */
			sys_big_height = 6;
		else
			sys_big_height = 13;
	}
	if (appl_xgetinfo(1, &sys_sml_height, &sys_sml_id, &d, &d) == 0)
	{
		sys_sml_id = 1;
		sys_sml_height = 4;
	}

	cf_vdi_handle = open_vwork(work_out);
	cf_font_anz = work_out[10];
	vq_extnd(cf_vdi_handle, TRUE, work_out);
	gl_planes = work_out[4];

	/* Aus den Pixel-Hhen Point-Hhen ermitteln */
	sys_sml_pts = height2pts(cf_vdi_handle, sys_sml_id, sys_sml_height);
	sys_big_pts = height2pts(cf_vdi_handle, sys_big_id, sys_big_height);

	gl_gdos	= (vq_vgdos() != -2);
	if (gl_gdos)
		cf_font_anz += vst_load_fonts(cf_vdi_handle, 0);

	vst_font(cf_vdi_handle, sys_big_id);
	vst_point(cf_vdi_handle, sys_big_pts, &sys_wchar, &sys_hchar, &sys_wbox, &sys_hbox);

	/*
	 * Startverzeichnis ermitteln. 
	 * Unter N.AES (Minix) kann es ungltige Pfade geben, daher der extra 
	 * path_exitst().
	 */
	shel_read(path, s);
	split_filename(path, gl_appdir, NULL);
	if (!path_exists(gl_appdir))
		get_path(gl_appdir, 0);

	init_userdef();

	if (rsc && *rsc != EOS)
	{
		rsc_load = (rsrc_load(rsc) != 0);
		if (!rsc_load)
		{
			form_alert(1, "[3][RSC nicht gefunden!|RSC not found!][Exit]");
			exit_app(-1);
		}
	}
}

void	exit_gem(void)
{
	if (gl_gdos)
		vst_unload_fonts(cf_vdi_handle, 0);
	v_clsvwk(cf_vdi_handle);
	term_userdef();
	if (rsc_load)
		rsrc_free();
	appl_exit();
}


void	exit_app(int ret)
{
	exit_gem();
	debug("finish with %d\n", ret);
	exit(ret);
}

void	hide_mouse(void)
{
	if (!hidden)
		graf_mouse(M_OFF, NULL);
	hidden++;
}


void	show_mouse(void)
{
	if (hidden > 0)
	{
		hidden--;
		if (!hidden)
			graf_mouse (M_ON, NULL);
	}
}


int hide_mouse_if_needed(GRECT *rect)
{
	if (!hidden)
	{
		GRECT	mouse;
		
		mouse.g_w = 63;
		mouse.g_h = 31;
#ifdef __MTAES__
	{
		EVNTDATA	ev;
		graf_mkstate(&ev);
		mouse.g_x = ev.x - (mouse.g_w / 2);
		mouse.g_y = ev.y - (mouse.g_h / 2);
	}	
#else
	{
		int	mx, my, d;
		graf_mkstate(&mx, &my, &d, &d);
		mouse.g_x = mx - (mouse.g_w / 2);
		mouse.g_y = my - (mouse.g_h / 2);
	}
#endif
	
		if (rc_intersect(rect, &mouse))
		{
			hide_mouse();
			return TRUE;
		}
	}
	return FALSE;
}


int appl_xgetinfo(int type, int *out1, int *out2, int *out3, int *out4)
{
	int	has_agi = FALSE;

#ifdef __MINT__
	has_agi = (	(gl_ap_version == 0x399 && getcookie("MagX", NULL)) ||
					(gl_ap_version >= 0x400) ||
					(appl_find("?AGI\0\0\0\0") == 0));
#else
	has_agi = (	(_GemParBlk.global[0] == 0x399 && getcookie("MagX", NULL)) ||
					(_GemParBlk.global[0] >= 0x400) ||
					(appl_find("?AGI\0\0\0\0") == 0));
#endif
	if (has_agi)
		return (appl_getinfo (type, out1, out2, out3, out4));
	else 
		return 0;
}
