/*
**	termBeta.c
**
**	Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
**		All Rights Reserved
*/

#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <exec/execbase.h>
#include <devices/timer.h>
#include <dos/dos.h>

#include <pragmas/intuition_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/exec_pragmas.h>

#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>

#include <string.h>

#define FRAGMENT 20

extern struct ExecBase *SysBase;
extern struct Screen *Screen;

STATIC struct Task *BetaTask,*MasterTask;

STATIC STRPTR BetaMessage[] =
{
	"`term' 3.4.2",
	"",
	"This is a beta-test release",
	"",
	"Send reports on",
	"MungWall/Enforcer hits",
	"and bugs to",
	"",
	"olsen@sourcery.han.de",
	"",
	"",
	"This screen will",
	"be closed in 5 seconds"
};

STATIC VOID __saveds
BetaServer(VOID)
{
	struct MsgPort *TimePort;

	if(TimePort = CreateMsgPort())
	{
		struct timerequest *TimeRequest;

		if(TimeRequest = (struct timerequest *)CreateIORequest(TimePort,sizeof(struct timerequest)))
		{
			if(!OpenDevice(TIMERNAME,UNIT_VBLANK,TimeRequest,NULL))
			{
				struct IntuitionBase *IntuitionBase;

				if(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",37))
				{
					struct GfxBase *GfxBase;

					if(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",37))
					{
						STATIC struct TextAttr DefaultText = { "topaz.font", 9, FS_NORMAL, FPF_ROMFONT | FPF_DESIGNED };
						STATIC struct ColorSpec Colors[3] = { {0,0,0,0}, {1,0,0,0}, {-1} };

						struct Screen *BetaScreen;

						if(BetaScreen = OpenScreenTags(NULL,
							SA_Depth,	1,
							SA_Font,	&DefaultText,
							SA_Quiet,	TRUE,
							SA_DisplayID,	LORES_KEY,
							SA_Overscan,	OSCAN_TEXT,
							SA_Colors,	Colors,
							SA_Draggable,	FALSE,
							SA_Exclusive,	TRUE,
						TAG_DONE))
						{
							struct RastPort *RPort = &BetaScreen -> RastPort;
							struct TextFont	*Font = RPort -> Font;
							LONG i,y,Len,Val = 16,Count = 0;
							ULONG Signals;
							BOOL Stopped = FALSE;

							y = (BetaScreen -> Height - (sizeof(BetaMessage) / sizeof(STRPTR)) * Font -> tf_YSize) / 2 + Font -> tf_Baseline;

							for(i = 0 ; i < (sizeof(BetaMessage) / sizeof(STRPTR)) ; i++)
							{
								if(BetaMessage[i])
								{
									Len = strlen(BetaMessage[i]);

									Move(RPort,(BetaScreen -> Width - TextLength(RPort,BetaMessage[i],Len)) / 2,y);
									Text(RPort,BetaMessage[i],Len);
								}

								y += Font -> tf_YSize;
							}

							TimeRequest -> tr_node . io_Command	= TR_ADDREQUEST;
							TimeRequest -> tr_time . tv_secs	= 0;
							TimeRequest -> tr_time . tv_micro	= 1000000 / FRAGMENT;

							SendIO(TimeRequest);

							Signal(MasterTask,SIGF_SINGLE);

							i = -1;

							FOREVER
							{
								Signals = Wait((1 << TimePort -> mp_SigBit) | SIGBREAKF_CTRL_C);

								if(Signals & SIGBREAKF_CTRL_C)
									Stopped = TRUE;

								if(Signals & (1 << TimePort -> mp_SigBit))
								{
									Val += i;

									if(Val == 7)
										i = 1;

									if(Val == 15)
										i = -1;

									SetRGB4(&BetaScreen -> ViewPort,1,Val,Val,Val);

									WaitIO(TimeRequest);

									TimeRequest -> tr_node . io_Command	= TR_ADDREQUEST;
									TimeRequest -> tr_time . tv_secs	= 0;
									TimeRequest -> tr_time . tv_micro	= 1000000 / FRAGMENT;

									SendIO(TimeRequest);

									Count++;

									if(Screen)
									{
										ULONG IntuiLock;
										BOOL Move;

										IntuiLock = LockIBase(NULL);

										Move = (IntuitionBase -> FirstScreen == Screen);

										UnlockIBase(IntuiLock);

										if(Move)
											ScreenToFront(BetaScreen);
									}
								}

								if(Stopped && Count >= 5 * FRAGMENT)
								{
									if(!CheckIO(TimeRequest))
										AbortIO(TimeRequest);

									WaitIO(TimeRequest);

									break;
								}
							}

							CloseScreen(BetaScreen);
						}

						CloseLibrary(GfxBase);
					}

					CloseLibrary(IntuitionBase);
				}

				CloseDevice(TimeRequest);
			}

			DeleteIORequest(TimeRequest);
		}

		DeleteMsgPort(TimePort);
	}

	Forbid();

	Signal(MasterTask,SIGF_SINGLE);

	BetaTask = NULL;
}

VOID
StartBetaTask(VOID)
{
	MasterTask = SysBase -> ThisTask;

	Forbid();

	if(BetaTask = CreateTask("term Beta task",15,BetaServer,8096))
	{
		SetSignal(0,SIGF_SINGLE);

		Wait(SIGF_SINGLE);
	}

	Permit();
}

VOID
StopBetaTask(VOID)
{
	Forbid();

	MasterTask = SysBase -> ThisTask;

	if(BetaTask)
	{
		SetSignal(0,SIGF_SINGLE);

		Signal(BetaTask,SIGBREAKF_CTRL_C);

		Wait(SIGF_SINGLE);
	}

	Permit();
}
