/*
 * $Header: DH0:src/omti/dist/src/RCS/OmtiDisplay_Vector.c,v 1.1 92/11/25 02:06:01 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.
 *
 * This program should use the 'Motor On/Off' vector in the device, but I
 * was never able to bring it to life. If I think about it today, there
 * may only a 'geta4()' missing, but while I wrote this program waaaay back
 * in 88 / 89, I didn't know anything about this. So, if you want to check 
 * out, you're welcome.
 */

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

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

struct	IntuitionBase	*IntuitionBase;
struct	GfxBase			*GfxBase;

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
};

UBYTE					 LEDStat[2];
UBYTE					 NewLEDStat[2];
ULONG					 DisplayLED[2];
struct	divechar		*DivePointer[2];

void Led0Control()
{
#asm
	movem.l	d0-d7/a0-a6,-(a7)
#endasm
	geta4();
	NewLEDStat[0] = ((DivePointer[0]->dc_flags) & DCF_MOTOR);
	if(NewLEDStat[0] != LEDStat[0])
	{
		Forbid();
		Disable();
		if(NewLEDStat[0])
			SetAPen(Window->RPort,2);
		else
			SetAPen(Window->RPort,0);
		RectFill(Window->RPort,6+67*0,13,66+67*0,17);
		Enable();
		Permit();
	}
	LEDStat[0] = NewLEDStat[0];
#asm
	movem.l	(a7)+,d0-d7/a0-a6
#endasm
}

void Led1Control()
{
#asm
	movem.l	d0-d7/a0-a6,-(a7)
#endasm
	geta4();
	NewLEDStat[1]	 = ((DivePointer[1]->dc_flags) & DCF_MOTOR);
	if(NewLEDStat[1]	 != LEDStat[1])
	{
		Forbid();
		Disable();
		if(NewLEDStat[1])
			SetAPen(Window->RPort,2);
		else
			SetAPen(Window->RPort,0);
		RectFill(Window->RPort,6+67*1,13,66+67*1,17);
		Enable();
		Permit();
	}
	LEDStat[1]	 = NewLEDStat[1];
#asm
	movem.l	(a7)+,d0-d7/a0-a6
#endasm
}


main()
{
BOOL					 EndeFlag	 = FALSE;
struct	IntuiMessage	*Message	 = NULL;
ULONG					 Class;
ULONG					 UserBit;
ULONG					 Signal;
ULONG					 Display[2];
void					*LedControl[2];

register	COUNT		 i;

struct	IOStdReq		 OmtiRequest[2];

	if(NULL == (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
	{
		printf("OmtiDisplay: No Intuition\n");
		exit(RETURN_FAIL);
	}

	if(NULL == (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
	{
		printf("OmtiDisplay: No Gfx\n");
		CloseLibrary(IntuitionBase);
		exit(RETURN_FAIL);
	}

	if((NULL == (Window = (struct Window *)OpenWindow(&NewWindowDef))))
	{
		printf("OmtiDisplay: No Window\n");
		CloseLibrary(IntuitionBase);
		CloseLibrary(GfxBase);
		exit(RETURN_FAIL);
	}

/* Window-Setup */

	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);


	LEDStat[0] = LEDStat[1] = 0;
	LedControl[0]	 = Led0Control;
	LedControl[1]	 = Led1Control;

/* Ueber die Unit die Drivechar holen und die Routinen verankern */

	for(i=0;i<2;i++)
	{
		OmtiRequest[i].io_Message.ln_Type	 = NT_MESSAGE;
		OmtiRequest[i].io_Message.mn_Length	 = sizeof(struct IOStdReq);
		OmtiRequest[i].io_Message.mn_ReplyPort	 = NULL;
		if(!(Display[i] = OpenDevice(OD_NAME,i,&OmtiRequest[i],NULL)))
		{
			DivePointer[i] = (struct divechar *)(OmtiRequest[i].io_Unit);
			DivePointer[i]->dc_vector = LedControl[i];
			printf("Vektor: %d: %lx\n",i,LedControl[i]);
		}
	}

/* Jetzt auf CloseWindow warten */

	UserBit	 = (1L << Window->UserPort->mp_SigBit);
	while(!EndeFlag)
	{
		Signal = Wait(UserBit);
		if(Signal & UserBit)
			while(Message = (struct IntuiMessage *)(GetMsg(Window->UserPort)))
			{
				if(Message->Class = CLOSEWINDOW)	EndeFlag = TRUE;
				ReplyMsg(Message);
			}
	}

/* Die Routinen wieder ausklinken */

	for(i=0;i<2;i++)
		if(!Display[i])
		{
			DivePointer[i]->dc_vector = NULL; /* Leds wieder aus */
			CloseDevice(OmtiRequest[i]);
		}

/* Fenster zumachen */

	CloseLibrary(IntuitionBase);
	CloseLibrary(GfxBase);
	CloseWindow(Window);
}

