/* Main-Header File inserted by GenCodeC */
/* Libraries */
#include <libraries/mui.h>
#include <libraries/gadtools.h> /* for BARLABEL in MenuItem */

/* Prototypes */
#ifdef __GNUC__
#include <proto/muimaster.h>
#include <proto/exec.h>
#include <proto/alib.h>
#include <proto/dos.h>
#else
#include <clib/muimaster_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <pragmas/muimaster_pragmas.h>
#endif /* __GNUC__ */

/*  Ansi  */
#include <stdlib.h>
#include <stdio.h>

/* Increase stack size */
LONG __stack=8192;
/* GenCodeC header end */

/* Include generated by GenCodeC */
#include "ZeladorGUI.h"

/* includes inseridos por mim..	*/
#include "Zelador_Nucleo.c"
#include <exec/exec.h>
#include <devices/timer.h>

/* variáveis inseridas por mim..	*/
struct timerequest * MeuTimerVBLANK;	/* usado pelo timer vblank	*/
struct MsgPort * MeuTimerVBLANKmp;
FILE * ArqLogEFR;			/* arquivos de LOG	*/
FILE * ArqLogRR;
FILE * ArqLogGeral;

/* Declarations for libraries (inserted by GenCodeC) */
struct Library * IntuitionBase;
struct Library * MUIMasterBase;

/* Init() function */
void init( void )
{
	if (!(IntuitionBase = OpenLibrary("intuition.library",37)))
	{
		printf("Can't Open Intuition Library\n");
		exit(20);
	}
	if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
	{
		printf("Can't Open MUIMaster Library\n");
		CloseLibrary(IntuitionBase);
		exit(20);
	}

	MeuTimerVBLANKmp = CreateMsgPort ();
	if (!(MeuTimerVBLANK = CreateIORequest (MeuTimerVBLANKmp, sizeof(timerequest))))
	{
		printf("Can't Create IORequest\n");
		DeleteMsgPort (MeuTimerVBLANKmp);
		CloseLibrary(MUIMasterBase);
		CloseLibrary(IntuitionBase);
		exit(20);
	}

	if (OpenDevice ("timer.device", UNIT_VBLANK, &MeuTimerVBLANK->tr_node, 0))
	{
		printf("Can't Open Timer Device\n");
		DeleteIORequest (MeuTimerVBLANK);
		DeleteMsgPort (MeuTimerVBLANKmp);
		CloseLibrary(MUIMasterBase);
		CloseLibrary(IntuitionBase);
		exit(20);	
	}

	if (InicializaREXX())
	{
		printf("Can't Open ARexx Communication\n");
		CloseDevice (&MeuTimerVBLANK->tr_node);
		DeleteIORequest (MeuTimerVBLANK);
		DeleteMsgPort (MeuTimerVBLANKmp);
		CloseLibrary(MUIMasterBase);
		CloseLibrary(IntuitionBase);
		exit(20);		
	}

	ArqLogEFR=fopen("logs/EFR.log","a");
	ArqLogRR=fopen("logs/RR.log", "a");
	ArqLogGeral=fopen("logs/general.log", "a");	
}
/* GenCodeC init() end */

/* End() function */
void end( void )
{
	if (ArqLogEFR)
		fclose(ArqLogEFR);

	if (ArqLogRR)
		fclose(ArqLogRR);

	if (ArqLogGeral)
		fclose(ArqLogGeral);

	FechaREXX ();
	AbortIO (&MeuTimerVBLANK->tr_node);
	WaitIO (&MeuTimerVBLANK->tr_node);
	CloseDevice (&MeuTimerVBLANK->tr_node);
	DeleteIORequest (MeuTimerVBLANK);
	DeleteMsgPort (MeuTimerVBLANKmp);
	CloseLibrary(MUIMasterBase);
	CloseLibrary(IntuitionBase);
	
	exit(0);
}
/* GenCodeC end() end */

/* variáveis GLOBAIS inseridas por mim..	*/
struct ObjApp * MeuApp = NULL;	/* Object */

/* Main Function inserted by GenCodeC */
int main(int argc,char **argv)
{
	BOOL	running = TRUE;

	/* Program initialisation : generated by GenCodeC */
	init();

	/* Create Object : generated by GenCodeC */
	if (!(MeuApp = CreateApp()))
	{
		printf("Can't Create App\n");
		end();
	}

	ProcessamentoCentral ();

	DisposeApp(MeuApp);
	end();
}
