/*
 * $Header: DH0:src/omti/dist/src/RCS/OmtiDisplay_Timer.c,v 1.1 92/11/25 02:05:45 Barnard Exp $
 *
 */

/*
 * This program simulates the Drive-LEDs for systems which have the
 * drive leds not on the front panel.
 *
 * Sorry, I just inserted this comment-lines, the rest is commented in
 * german.
 *
 * This program was written under AmigaOS 1.3. I don't know, whether it will
 * run under AmigaOS 2.04 and above.
 *
 * The TAB-Size use is 4.
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>


#include "include/omti.h"
#include "include/omtistruct.h"

#define	UPDATES_PER_SECOND		10		/* Wieoft wird Display erneuert */

struct	Window	*Window;

struct	NewWindow	NewWindowDef	=
{
	500,0,
	140,30,
	-1,-1,
	CLOSEWINDOW,
	WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | SMART_REFRESH | NOCAREREFRESH,
	NULL, NULL,
	"Display",
	NULL,
	NULL,
	0, 0, 0, 0,
	WBENCHSCREEN
};

SHORT BData[][2]	 =
{
	 0, 0,
	64, 0,
	64, 8,
	 0, 8,
	 0, 0,
};

struct Border	LEDBorder	 =
{
	 0, 0,
	 1, 0, JAM1,
	 5,
	 &BData,
	 NULL
};

struct	MsgPort			*TimerPort		 = NULL;
struct	timerequest		*TimerRequest	 = NULL;

struct	IntuitionBase	*IntuitionBase;
struct	GfxBase			*GfxBase;

void SendTimeRequest()
{
	TimerRequest->tr_node.io_Command				 = TR_ADDREQUEST;
	TimerRequest->tr_node.io_Message.mn_ReplyPort	 = TimerPort;
	TimerRequest->tr_time.tv_secs					 = 0;
	TimerRequest->tr_time.tv_micro					 = (1000000L / UPDATES_PER_SECOND);
	SendIO(TimerRequest);
}

BOOL OpenAll()
{
	if(NULL == (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
	{
		printf("OmtiDisplay: No Intuition\n");
		return(TRUE);
	}
	if(NULL == (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
	{
		printf("OmtiDisplay: No Gfx\n");
		return(TRUE);
	}
	if(NULL == (Window = (struct Window *)OpenWindow(&NewWindowDef)))
	{
		printf("OmtiDisplay: No Window\n");
		return(TRUE);
	}
	if(NULL == (TimerPort = (struct MsgPort *)CreatePort("Omti_Dev_Timer",0)))
	{
		printf("Kein Message-Port\n");
		return(TRUE);
	}
	if(NULL == (TimerRequest = (struct timerequest *)CreateExtIO(TimerPort,sizeof(struct timerequest))))
	{
		printf("Kein IORequest\n");
		return(TRUE);
	}
	if(OpenDevice(TIMERNAME,UNIT_VBLANK,TimerRequest,0))
	{
		printf("OmtiDisplay: Kein Timer-Device\n");
		return(TRUE);
	}
	return(FALSE);
}

void CloseAll()
{
	if(TimerRequest)	CloseDevice(TimerRequest);
	if(TimerRequest)	DeleteExtIO(TimerRequest);
	if(TimerPort)		DeletePort(TimerPort);
	if(Window)			CloseWindow(Window);
	if(IntuitionBase)	CloseLibrary(IntuitionBase);
	if(GfxBase)			CloseLibrary(GfxBase);
}

main()
{
BOOL					 EndeFlag	 = FALSE;
struct	IntuiMessage	*Message	 = NULL;
ULONG					 Class;
ULONG					 UserBit,TimerBit;
ULONG					 Signal;

UBYTE					 LEDStat[2];
UBYTE					 NewLEDStat[2];
ULONG					 DisplayLED[2];

register	COUNT		 i;
struct	IOStdReq		*OmtiRequest[2];
struct	MsgPort			*OmtiMsgPort;
UWORD					*FlagPointer[2];

	if(OpenAll())
	{
		CloseAll();
		exit(RETURN_FAIL);
	}

	LEDStat[0] = LEDStat[1] = 0;

	for(i=0;i<2;i++)
	{
		OmtiRequest[i] = CreateExtIO(OmtiMsgPort,sizeof(struct IOStdReq));
		if(!(DisplayLED[i]	 = OpenDevice(OD_NAME,i,OmtiRequest[i],0)))
		{
			FlagPointer[i] = &(((struct divechar *)(OmtiRequest[i]->io_Unit))->dc_flags);
		}
	}

	SetAPen(Window->RPort,1);
	Move(Window->RPort,14,27);
	Text(Window->RPort,"Unit 0  Unit 1",14);
	DrawBorder(Window->RPort,&LEDBorder, 4,11);
	DrawBorder(Window->RPort,&LEDBorder,71,11);


	SendTimeRequest();

	UserBit	 = (1L << Window->UserPort->mp_SigBit);
	TimerBit	 = (1L << TimerPort->mp_SigBit);
	while(!EndeFlag)
	{
		Signal = Wait(UserBit | TimerBit);
		if(Signal & UserBit)
			while(Message = (struct IntuiMessage *)(GetMsg(Window->UserPort)))
			{
				if(Message->Class = CLOSEWINDOW)
				{
					EndeFlag = TRUE;
					AbortIO(TimerRequest);
					WaitIO(TimerRequest);
				}
				ReplyMsg(Message);
			}
		if(Signal & TimerBit)
			while(Message = (struct timerequest *)(GetMsg(TimerPort)))
			{
				for(i=0;i<2;i++)
				{
					NewLEDStat[i] = (*FlagPointer[i] & DCF_MOTOR) >> DCB_MOTOR;
					if(NewLEDStat[i] != LEDStat[i])
					{
						if(NewLEDStat[i])
							SetAPen(Window->RPort,2);
						else
							SetAPen(Window->RPort,0);
						RectFill(Window->RPort,6+67*i,13,66+67*i,17);
					}
					LEDStat[i] = NewLEDStat[i];
				}
				SendTimeRequest();
			}
	}
	for(i=0;i<2;i++)
	{
		if(!DisplayLED[i])
			CloseDevice(OmtiRequest[i]);
		DeleteExtIO(OmtiRequest[i]);
	}
	CloseAll();
}

