/*
**	EmulationDebug.c
**
**	Terminal emulation debugging support code
**
**	Copyright © 1990-1996 by Olaf `Olsen' Barthel
**		All Rights Reserved
**
**	:ts=4
*/

#ifndef _GLOBAL_H
#include "Global.h"
#endif

STATIC struct Screen	*DebugScreen;
STATIC struct Window	*DebugWindow;
STATIC struct RastPort	*DebugRPort;

VOID
DebugExit()
{
	if(DebugWindow)
	{
		CloseWindow(DebugWindow);

		DebugWindow = NULL;
	}

	if(DebugScreen)
	{
		CloseScreen(DebugScreen);

		DebugScreen = NULL;
	}
}

BOOL
DebugInit()
{
	if(DebugScreen)
		return(TRUE);
	else
	{
		ULONG			 DisplayID = DEFAULT_MONITOR_ID | HIRESLACE_KEY;
		struct Screen	*PubScreen;

		if(PubScreen = LockPubScreen(NULL))
		{
			DisplayID = GetVPModeID(&PubScreen -> ViewPort);

			UnlockPubScreen(NULL,PubScreen);
		}

		if(DebugScreen = OpenScreenTags(NULL,
			SA_Depth,		1,
			SA_Overscan,	OSCAN_TEXT,
			SA_DisplayID,	DisplayID,
			SA_Font,		&DefaultFont,
			SA_Title,		"term Debug Screen",
			SA_Behind,		TRUE,
		TAG_DONE))
		{
			if(DebugWindow = OpenWindowTags(NULL,
				WA_CustomScreen,	DebugScreen,
				WA_Top,				DebugScreen -> BarHeight + 1,
				WA_Left,			0,
				WA_Width,			DebugScreen -> Width,
				WA_Height,			DebugScreen -> Height - (DebugScreen -> BarHeight + 1),
				WA_RMBTrap,			TRUE,
				WA_Backdrop,		TRUE,
				WA_Borderless,		TRUE,
			TAG_DONE))
			{
				DebugRPort = DebugWindow -> RPort;

				return(TRUE);
			}

			CloseScreen(DebugScreen);

			DebugScreen = NULL;
		}

		return(FALSE);
	}
}

VOID
DebugShowScrollInfo(VOID)
{
	UBYTE	LocalBuffer[256];
	LONG	i,Left;

	for(i = 0 ; i < RasterHeight ; i++)
	{
		if(i == ScrollLineFirst)
			SPrintf(LocalBuffer,"+%3ld %02lx",i,ScrollLines[i] . ColourMask);
		else
		{
			if(i == ScrollLineLast)
				SPrintf(LocalBuffer,"-%3ld %02lx",i,ScrollLines[i] . ColourMask);
			else
				SPrintf(LocalBuffer," %3ld %02lx",i,ScrollLines[i] . ColourMask);
		}

		if(i == ScrollLineFirst || i == ScrollLineLast)
			SetABPenDrMd(DebugRPort,0,1,JAM2);
		else
			SetABPenDrMd(DebugRPort,1,0,JAM2);

		Move(DebugRPort,0,DebugRPort -> TxBaseline + i * DebugRPort -> TxHeight);
		Text(DebugRPort,LocalBuffer,strlen(LocalBuffer));

		Left = DebugRPort -> cp_x;

		SetABPenDrMd(DebugRPort,0,0,JAM1);
		RectFill(DebugRPort,Left,i * DebugRPort -> TxHeight,DebugWindow -> Width - 1,(i + 1) * DebugRPort -> TxHeight - 1);

		if(ScrollLines[i] . Width)
		{
			ULONG Ghosting = 0x44441111;

			SetAPen(DebugRPort,1);
			SetAfPt(DebugRPort,(UWORD *)&Ghosting,1);

			RectFill(DebugRPort,Left + ScrollLines[i] . Left * 3,i * DebugRPort -> TxHeight,Left + ScrollLines[i] . Right * 3 - 1,(i + 1) * DebugRPort -> TxHeight - 1);

			SetAfPt(DebugRPort,NULL,0);
		}
	}
}
