#include <ctype.h>

#include "intern.h"

long ts2ol(short i1, short i2)
{
	short va[2];
		 
	va[0] = i1;
	va[1] = i2;
	return *(long *)(va);
}

void ol2ts(long l, short *i1, short *i2)
{
	short	*va;
	
	va = (short *)&l;
	*i1 = va[0];
	*i2 = va[1];
}


static void cf_background(GRECT *box, MFDB *buffer, int get)
{
	MFDB	screen;
	int	xy [8];
	GRECT	r;
	
	r = *box;
	if (rc_intersect(&gl_desk, &r))						/* nur sichtbaren Bereich */
	{
		screen.fd_addr = NULL;								/* Bildschirm */
		buffer->fd_w = r.g_w;
		buffer->fd_h = r.g_h;
		buffer->fd_wdwidth = (r.g_w + 15) >> 4; 	/* Anzahl in intS */
		buffer->fd_stand	 = FALSE;
		buffer->fd_nplanes = gl_planes;
		if (get)
			buffer->fd_addr	 = cf_malloc((long)buffer->fd_wdwidth * 2L * r.g_h * gl_planes, "cf_background", FALSE);
			
		if (buffer->fd_addr != NULL)
		{
			hide_mouse();
			if (get)
			{
				xy[0] = r.g_x;
				xy[1] = r.g_y;
				xy[2] = r.g_x + r.g_w - 1;
				xy[3] = r.g_y + r.g_h - 1;
				xy[4] = 0;
				xy[5] = 0;
				xy[6] = r.g_w-1;
				xy[7] = r.g_h-1;
				vro_cpyfm(cf_vdi_handle, S_ONLY, xy, &screen, buffer);
			}
			else
			{
				xy[0] = 0;
				xy[1] = 0;
				xy[2] = r.g_w - 1;
				xy[3] = r.g_h - 1;
				xy[4] = r.g_x;
				xy[5] = r.g_y;
				xy[6] = r.g_x + r.g_w - 1;
				xy[7] = r.g_y + r.g_h - 1;
				vro_cpyfm(cf_vdi_handle, S_ONLY, xy, buffer, &screen);
				Mfree(buffer->fd_addr);
			}
			show_mouse();
		}
		else
#ifdef __MTAES__
			form_dial(FMD_FINISH, &r, &r);
#else
			form_dial(FMD_FINISH, r.g_x, r.g_y, r.g_w, r.g_h, r.g_x, r.g_y, r.g_w, r.g_h);
#endif
	}
}

void save_background(GRECT *box, MFDB *buffer)
{
	cf_background(box, buffer, TRUE);
}

void restore_background(GRECT *box, MFDB *buffer)
{
	cf_background(box, buffer, FALSE);
}

int get_patchlev(char *id_str, char *pl)
{
	char	*p;
	int	len;
	int	ret = FALSE;
		
	len = (int)strlen(id_str);
	if (id_str[0] == '$' && id_str[len-1] == '$')
	{
		p = id_str;
		while (!isdigit(*p))
			p++;
		while (*p != '$' && !isspace(*p))
			*pl++ = *p++;
		*pl = EOS;
		ret = TRUE;
	}
	return ret;
}

#ifndef _GEMLIB_H_
/*
 * Kennt die Pure/GEM-Lib nicht.
*/
int rc_intersect(GRECT *r1, GRECT *r2)
{
	int	tx, ty, tw, th, ret;
		
	tx = max(r2->g_x, r1->g_x);
	tw = min(r2->g_x + r2->g_w, r1->g_x + r1->g_w) - tx;
	ret = (0 < tw);
	if (ret) 
	{
		ty = max (r2->g_y, r1->g_y);
		th = min (r2->g_y + r2->g_h, r1->g_y + r1->g_h) - ty;
		ret = (0 < th);
		if (ret) 
		{
			r2->g_x = tx;
			r2->g_y = ty;
			r2->g_w = tw;
			r2->g_h = th;
		}
	}
	return (ret);
}
#endif

void *cf_malloc(long size, char *who, int global)
{
	void	*r = NULL;

	if (global && getcookie("MiNT", NULL))
/*		r = (void *)Mxalloc(size, 43);*/
		r = (void *)Mxalloc(size, 0x23);
	else
		r = (void *)Malloc(size);

	if (r == NULL)
	{
		char	msg[80];

		if (gl_gem > 0)
		{
			sprintf(msg, "[3][CF-Lib (%s)|Can't allocate RAM!][Ignore|Exit]", who);
			if (form_alert(2, msg) == 2)
			{
				appl_exit();
				exit(-39);
			}
		}
		else
		{
			fprintf(stderr, "CF-Lib (%s)\nCan't allocate RAM!\n", who);
			exit(-39);
		}
	}
	return r;
}

void *malloc_global(long size)
{
	return cf_malloc(size, "malloc_global", TRUE);
}
