#include "exec/types.h"
#include "exec/libraries.h"
#include "exec/memory.h"
#include "graphics/gfx.h"
#include "intuition/intuition.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"

#define BLUE 0 
#define WHITE 1 
#define BLACK 2 
#define RED 3 

#define ACTION_SCREEN_MODE 994

struct GfxBase *GfxBase; 
LONG *IntuitionBase;
LONG *DosBase;

extern struct Library *OpenLibrary();
extern struct Screen *OpenScreen();
extern struct Window *OpenWindow();

extern struct MsgPort  *NewConsole();

struct Window   *window=NULL;
struct Screen   *screen=NULL;
struct MsgPort  *task=NULL;
struct Process  *process=NULL;

LONG handle=NULL;

char buffer[80];

struct MsgPort iorp = {
    {0, 0, NT_MSGPORT, 0, 0}, 0,
    -1,                         /* initialize signal to -1 */
    0,
                                /* start with empty list */
    {&iorp.mp_MsgList.lh_Tail, 0, &iorp.mp_MsgList.lh_Head, 0, 0}
};


struct NewScreen ns = {
        0,0,
        320,200,4, /* & depth */
        BLUE,RED,
        NULL,  /* viewmodes */
        CUSTOMSCREEN,
        NULL, /* default font */
        "Window Test Program",
        NULL, /* no user gadgets */
        NULL
}; /* default bit map */

struct NewWindow nw = {
        0, 12,          /* starting position (left,top) */
        320,186,        /* width, height */
        BLUE,WHITE,    /* detailpen, blockpen */
        NULL,  /* flags for idcmp */
        SMART_REFRESH|WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|ACTIVATE, /* window gadget flags */
        NULL,           /* pointer to 1st user gadget */
        NULL,           /* pointer to user check */
        NULL,            /* no title */
        NULL,           /* pointer to window screen (add after it is open */
        NULL,           /* pointer to super bitmap */
        50,50,         /* min width, height */
        640,200,        /* max width, height */
        CUSTOMSCREEN};
/* own screen defaults */

main()
{
struct StandardPacket *packet;

if((IntuitionBase=OpenLibrary("intuition.library",0)) == NULL)cleanup(20);
if((DosBase = OpenLibrary(DOSNAME, 0)) == NULL) cleanup(20);
if((GfxBase = OpenLibrary("graphics.library", 0)) ==NULL) cleanup(20);

if((screen=OpenScreen(&ns)) == NULL)cleanup(20);
nw.Screen=screen;
if((window=OpenWindow(&nw)) == NULL)cleanup(20); 

/* set up the message port */
    if ((iorp.mp_SigBit = AllocSignal(-1)) < 0) {
kprintf("message port error\n");
        cleanup(0);
        exit(35);
    }
    iorp.mp_SigTask = (struct Task *) FindTask((char *) NULL);


/* Start up new console handler task; pass it new window */

	if(!(task = NewConsole(window)))cleanup(20);

/* Patch DOS "consoletask" location. Subsequent calls to */
/* Open("*") will refer to the new window created above  */

	process = (struct Process *)FindTask(NULL); 
	process -> pr_ConsoleTask = task;

	process->pr_WindowPtr=window; /* reset error screen */
/* Open * now refers to the new window */

	if (!(handle = Open("*", MODE_OLDFILE))) {
	   CloseConsole(task); /* open failed, kill our console task */
	   cleanup(20);
	}

/* put window into cooked mode */
	if ((packet = (struct StandardPacket *)
		AllocMem(sizeof(*packet), MEMF_PUBLIC|MEMF_CLEAR))) {
	    /* this is the console handlers packet port */
	    packet->sp_Msg.mn_Node.ln_Name = &(packet->sp_Pkt);
            packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
            packet->sp_Pkt.dp_Port = &iorp;
            packet->sp_Pkt.dp_Type = ACTION_SCREEN_MODE;
            packet->sp_Pkt.dp_Arg1 = 0;
            PutMsg(task, packet);
            WaitPort(&iorp);
            GetMsg(&iorp); /* pull message */
  	    FreeMem(packet, sizeof(*packet));
	}

	Write(handle,"\033[20h",5);	/* Xlate \n to \r\n */
	Write(handle,"This is a custom screen\n",24);
	Write(handle,"And here's my own stupid\n",25);
	Write(handle,"diskcopy message, duh.\n",23);
	Read(handle,buffer,10);
	Execute("diskcopy <* df0: to df1:",0,0);
	cleanup(0);
}


cleanup(code)
{
struct Process *process;

 if (iorp.mp_SigBit != -1) 
    FreeSignal(iorp.mp_SigBit);

process = (struct Process *)FindTask(NULL); /* reset error window */
process->pr_WindowPtr=NULL;
process -> pr_ConsoleTask = NULL;;

if(handle)Close(handle);
if(screen)CloseScreen(screen);

if(GfxBase)CloseLibrary(GfxBase);
if(DosBase)CloseLibrary(DosBase);

OpenWorkBench(); /* just in case */
if(IntuitionBase)CloseLibrary(IntuitionBase); 

exit(code);

}
