#include "PostPre.h"
#include "Global.h"

static	UWORD penspec[] = {0, 1, 1, 2, 1, 3, 1, 0, 2, 1, 2, 1, (UWORD) ~0};
struct	ColorMap scr_colormap;
static	UWORD	screenbwcolors[4] =		/* Screen Black and white */
{
		0xaaa, 0x000, 0xfff, 0x68b			/* Standard OS 2.0 Workbench colors */
};

static	UWORD	screenrgbcolors[16] =	/* Screen RGB */
{
		0xaaa, 0x000, 0xfff, 0x68b,		/* Standard OS 2.0 Workbench colors */
		0x000, 0x000, 0x000, 0x000,		/* Black */
		0xfff, 0x0ff, 0xf0f, 0x00f,		/* White   cyan    magenta blue */
		0xff0, 0x0f0, 0xf00, 0x000 		/* Yellow  green   red     black */
};

static	UWORD	screencmykcolors[32] =	/* Screen CMYK */
{
		0xaaa, 0x000, 0xfff, 0x68b,		/* Standard OS 2.0 Workbench colors */
		0x000, 0x000, 0x000, 0x000,		/* Black */
		0xfff, 0x0ff, 0xf0f, 0x00f,		/* White   cyan    magenta blue */
		0xff0, 0x0f0, 0xf00, 0x000, 		/* Yellow  green   red     black */
		0xfff, 0x0ff, 0xf0f, 0x00f,		/* White   cyan    magenta blue */
		0xff0, 0x0f0, 0xf00, 0x000, 		/* Yellow  green   red     black */
		0x000, 0x000, 0x000, 0x000,		/* Black */
		0x000, 0x000, 0x000, 0x000			/* Black */
};

void	openscreen(void)
{
	if(custscreen == NULL)
	{
		ScrDepth = Options.PostOpts.depth + 1;
		sprintf(postpubscrname, "%s%d", PSNAME, post_count);
		custscreen = OpenScreenTags(NULL,
				SA_Left,			0,
				SA_Top,			0,
				SA_Depth,		ScrDepth,
				SA_Width,		Options.Screen.ScrWidth,
				SA_Height,		Options.Screen.ScrHeight,
				SA_Title,		(ULONG) titlewait,
				SA_Type,			PUBLICSCREEN,
				SA_DisplayID,	Options.Screen.ScrDisplayID,
				SA_Overscan,	Options.Screen.ScrOverscanType,
				SA_AutoScroll,	Options.Screen.ScrAutoScroll,
				SA_Pens,			(ULONG) penspec,
				SA_Interleaved, TRUE,
				SA_PubName,		(ULONG) postpubscrname,
				TAG_DONE);
	}
	if (custscreen == NULL)
	{
		okmsg("can't open screen");
		errorende();
		exit(20);
	}

	PubScreenStatus(custscreen, 0L);
/*	MoveScreen(custscreen, -ScrWidth, -ScrHeight);*/
	scr_colormap.Count = 1 << ScrDepth;
	if(Options.PostOpts.depth == 1) scr_colormap.ColorTable = (APTR) screenbwcolors;
	else if (Options.PostOpts.depth == 3) scr_colormap.ColorTable = (APTR) screenrgbcolors;
	else scr_colormap.ColorTable = (APTR) screencmykcolors;
	LoadRGB4(&custscreen->ViewPort, (UWORD *) scr_colormap.ColorTable,
				1 << ScrDepth);
	screen = custscreen;
}

BOOL	openpubscreen(struct PubScrContext *pscontext, char *psname)
{
	if((pscontext->pubscreen = LockPubScreen(psname)) == NULL)
	{
/*		okmsg("can't obtain public screen!");
		return(FALSE);*/
		okmsg("can't obtain public screen! falling back on workbench");
		strcpy(pubscreenname, "Workbench");
		if((pscontext->pubscreen = LockPubScreen(pubscreenname)) == NULL)
		{
			okmsg("can't obtain workbench screen!");
			return(FALSE);
		}
	}
	pscontext->ps_lock = TRUE;
	if(argwindow) GetNeededPens(pscontext);
	screen = pscontext->pubscreen;
	return(TRUE);
}

void	GetVisual(void)
{
	if(my_VisualInfo) return;
	my_VisualInfo = GetVisualInfo(screen, TAG_END);
	if (my_VisualInfo == NULL)
	{
		okmsg("can't get visual info");
		errorende();
		exit(20);
	}
}

void	FreeVisual(void)
{
	if (my_VisualInfo)
	{
		FreeVisualInfo(my_VisualInfo);
		my_VisualInfo = NULL;
	}
}