/**********************************************************************
 * Terminal I/O for the CBBS program.
 *
 * Pete VE5VA
 **********************************************************************/
#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <devices/serial.h>
#include <stdio.h>
#include <ctype.h>
#define INTUITION_REV 1L



struct IntuitionBase *IntuitionBase = 0L;

extern struct IOExtSer *Read_Request;
extern struct IOExtSer *Write_Request;

long readbit,windbit,waitbits;
struct TextAttr TextFont = {
   (UBYTE *)"topaz.font",
   8,0,0
};

char *title = "AMIGA CBBS V6.1c (VE5VA)";


struct IOStdReq *iop = 0L;
/* my window structure */
struct NewWindow NewWindow = {
   0,
   0,
   640,
   200,
   0,
   1,
/* IDCMP Flags */
   0
   |  RAWKEY
   |  MOUSEBUTTONS
 /*|  MENUPICK*/
 /*|  NEWSIZE */
   |  CLOSEWINDOW
   ,
/* Flags */
   0
   |  BORDERLESS
   |  ACTIVATE
   |  RMBTRAP
 /*|  WINDOWDRAG*/
   |  WINDOWCLOSE
   |  WINDOWDEPTH
 /*|  WINDOWSIZING*/
   ,
   NULL,
   NULL,
   NULL,
   NULL,
   NULL,
   0,0,
   640,200,
   WBENCHSCREEN
};


struct Window *mywindow = 0L;             /* ptr to applications window */
struct RastPort *rp;
struct ViewPort *vp;
extern struct Window *OpenWindow();
extern char *OpenLibrary();
extern long OpenDevice();
short mouse = 0;               /* If mouse set then open mouse too */
extern struct IOStdReq  *CreateStdIO();
extern struct MsgPort *CreatePort();
struct MsgPort *crp;

extern   long      IoErr();
extern   char      *Open();
extern   long      Read(),Write();
extern   void      Close();
#define NEW      1006L
#define AMG_MAXBUF   1024
static unsigned char scrn_tmp[AMG_MAXBUF+1];
static long      scrn_tmp_p = 0L;
short od = 0;     /* device is open so it can be closed */
openterm()
{
   long i;

   short id;
   register unsigned short *p,*q;
   register int j,k;
   IntuitionBase = (struct IntuitionBase *)
                    OpenLibrary("intuition.library", INTUITION_REV);
   if( IntuitionBase == NULL ) {
      puts("can't open intuition\n");
      return(1);
   }
   if(( mywindow = OpenWindow(&NewWindow) ) == NULL) {
      printf("cant open window\n");
      return(1);
   }
   rp = mywindow->RPort;
   vp = &mywindow->WScreen->ViewPort;
   crp = CreatePort("con.read",0L);
   if(crp == 0L) {
      printf("can't create console read port\n");
      cleanup();
      exit();
   }
   iop = CreateStdIO(crp);
   if(iop == 0L) {
      printf("can't create stdio read port\n");
      cleanup();
      exit();
   }

   iop->io_Data = (APTR)mywindow;
   iop->io_Length = (long)sizeof(*mywindow);
   if(OpenDevice("console.device",0L,iop,0L)) {
      i = IoErr();
      printf("OpenDevice failed %ld\n",i);
      return(1);
   }
   od = 1;
   windbit = (1L << mywindow->WindowPort->mp_SigBit);
   waitbits |= windbit;
   cursor_off();
   window_title(0,0);
   return(0);
}

closeterm()
{
   if(od)CloseDevice(iop);
   if(crp)DeletePort(crp);
   if(iop)DeleteStdIO(iop);
   if(mywindow)CloseWindow(mywindow);
   if(IntuitionBase)CloseLibrary(IntuitionBase);
}


ttputc(c)
unsigned char c;
{
   if(c > 0177)return;
   if(c == 7) {   /* beep char ... sound the beep and let it flash screen */
      beep();
   }
   /* Remove a return char. The original IBM code outchar()
      will not send us a return char anyway but some of my stuff might.
   */
   if(c == '\r')return;
   scrn_tmp[scrn_tmp_p++] = c;
   amg_flush();

}
ttputs(s)
unsigned char   *s;
{
   while (*s) {
      if(*s > 0177) {
         s++;
         continue;
      }
      if(*s == 7) {   /* beep char */
         beep();
      }
      /* Remove a return char. The original IBM code
         outstr() will not send us a return char anyway but some of
         my stuff might.
      */
      if(*s == '\r') return;
      scrn_tmp[scrn_tmp_p++] = *s++;
   }
   amg_flush();
}

ttflush()
{
    amg_flush();

}
amg_flush()
{
   if(scrn_tmp_p) {
      iop->io_Data = (APTR) scrn_tmp;
      iop->io_Length = scrn_tmp_p;
      iop->io_Command = CMD_WRITE;
      DoIO(iop);
   }
   scrn_tmp_p = 0;
}
cleanup()
{
   if(Read_Request) {
      AbortIO(Read_Request);
      CloseDevice(Read_Request);
      DeletePort(Read_Request->IOSer.io_Message.mn_ReplyPort);
      FreeMem(Read_Request,(long)sizeof(*Read_Request));
   }
   if(Write_Request) {
      CloseDevice(Write_Request);
      DeletePort(Write_Request->IOSer.io_Message.mn_ReplyPort);
      FreeMem(Write_Request,(long)sizeof(*Write_Request));
   }
   closeterm();
}
cursor_off()
{
   ttputs("\033[\060\040p");
}
cursor_on()
{
   ttputs("\033[\040p");
}
char titlestr[80];
window_title(i,j)
short i,j;
{
   int k,l;
   if((k = i) < 0)k = 0;
   if((l = j) < 0)l = 0;
   sprintf(titlestr,"%s   %4d/%d",title,k,l);
   SetWindowTitles(mywindow,titlestr,-1L);
}
