#include <exec/types.h>
#include <exec/io.h>
#include <intuition/intuition.h>

#define EOF -1
#define GRAPHICS_REV   29
#define INTUITION_REV  29

void myexit();

extern struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

static struct IOStdReq conrequest;
struct Window *unixwindow;

static struct NewWindow NewWindow = {
   0,1,640,199, -1,-1,   /* left, top, width, height, detail, block */
   CLOSEWINDOW | VANILLAKEY,
   WINDOWDEPTH | WINDOWCLOSE | ACTIVATE,
   NULL, NULL, "Cwindow Test Bed - For that true C feeling",
   NULL, NULL, 640,199,640,199, WBENCHSCREEN };

/* initialize the window so we can read and write to it */
void mysetup()
{
   if (unixwindow == NULL)
      {
      if ( (IntuitionBase = (struct IntuitionBase *)
         OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
         _exit(1);

      if ( (GfxBase = (struct GfxBase *)
         OpenLibrary("graphics.library",GRAPHICS_REV)) == NULL)
         _exit(2);

      if ( (unixwindow = (struct Window *)OpenWindow(&NewWindow)) == NULL)
         _exit(3);

      conrequest.io_Data = (APTR)unixwindow;
      conrequest.io_Length = sizeof(*unixwindow);
      if (OpenDevice("console.device",0, &conrequest, 0) != 0)
         myexit(6);
      }
}

int mygetc() /* read a character from our special window */
{
   int c;
   struct IntuiMessage *Message, *GetMsg();
         
   if (unixwindow == NULL) mysetup();

   /* wait until something happens in the window */
   while( (Message = GetMsg(unixwindow->UserPort)) == NULL)
      Wait( 1 << unixwindow->UserPort->mp_SigBit );

   switch(Message->Class)
     {
     case CLOSEWINDOW:    c = EOF;
                          break;
     case VANILLAKEY:     if ( (c = Message->Code) == 0x1c)
                                  c = EOF;
                          else if (c == 0x0D)
                               c = '\n';
                          break;
     default:             c = EOF;
                          break;
     }
   ReplyMsg(Message);
   return(c);
}

void myputc(c)  /* write a single character to our window */
char c;
{
   if (unixwindow == NULL) mysetup();

   conrequest.io_Command = CMD_WRITE;
   conrequest.io_Data = (APTR)&c;
   conrequest.io_Length = 1;
   DoIO(&conrequest);
}

void myputs(str)  /* write a string to our window */
char *str;
{
   if (unixwindow == NULL) mysetup();

   conrequest.io_Command = CMD_WRITE;
   conrequest.io_Data = (APTR)str;
   conrequest.io_Length = strlen(str);
   DoIO(&conrequest);
   myputc('\n');     /* don't forget the newline for the string */
}
      
/* write a formatted string to our window */
void myprintf(str,v1,v2,v3,v4,v5,v6,v7,v8,v9)
char *str;
long v1,v2,v3,v4,v5,v6,v7,v8,v9;
{
   char buff[256];
         
   if (unixwindow == NULL) mysetup();

   conrequest.io_Length = sprintf(buff,str,v1,v2,v3,v4,v5,v6,v7,v8,v9);
   conrequest.io_Command = CMD_WRITE;
   conrequest.io_Data = (APTR)buff;
   DoIO(&conrequest);
}

/* close up everything and leave the program */
void myexit(code)
int code;
{
   myputs("Closing everything down");
   if (unixwindow != NULL)
      {
      CloseDevice(&conrequest);
      CloseWindow(unixwindow);
      CloseLibrary(GfxBase);
      CloseLibrary(IntuitionBase);
      }
   _exit(code);
}
