/*    Comm  global storage allocation */
/*  compiler directives to fetch the necessary header files */
#include <exec/types.h>

#include <libraries/dos.h>
#include <intuition/intuition.h>
#include <exec/exec.h>
#include <graphics/gfxbase.h>
#include <graphics/regions.h>
#include <graphics/copper.h>
#include <graphics/gels.h>
#include <devices/serial.h>
#include <devices/keymap.h>
#include <hardware/blit.h>
#include <libraries/dosextens.h>

#include <stdio.h>
#include <ctype.h>
#include "defines.h"

#define VERSION   "Comm 1.34 "
#define DATE      " 04/09/87 "


struct setup {
   UBYTE  version[41];
   UBYTE  date[12];
   UBYTE  def_dir[41];
   UBYTE  DefDir[41];
   USHORT new_colors[4];
   UBYTE  cap_name[41];
   ULONG  priority;
   UBYTE  prefbaud;
   ULONG  titlebar;
   ULONG  interlace;
   int    numbuffs;
   int    period;
   int    volume;
   int    cycles;
   int    length;
   int    keyload;
} install = {
   "Comm 1.34\0                             ",  /* version */
   " 04/09/87  ",                              /* date */
   "RAM:\0                                  ",  /* def_dir */
   "SYS:\0                                  ",  /* DefDir */
   { 0,0,0,0 },                                /* new_colors */
   "RAM:Capture.Txt\0                       ",  /* cap_name */
   1, TRUE,
   0, 0, NUMBUFS, BEEP_PERIOD, 64, BEEPCYCLES, BEEPSIZE,0
};

/*
   defines required for phone library
     library entry looks like this:
name              number           baud    comment
plink             652-0800         1200    accessed through tymnet
>                 555-1212,,,12345         MCI gateway and account number

*/

struct dir_entry {                     /* phone directory entry */
                    char name[ NAMESIZE+1 ];
                    char number[ NBRSIZE+1 ];
                    char baud[ BAUDSIZE+1 ];
                    char comment[ COMTSIZE+1 ];
                 } pdir[ MAXPHONE ] ;

/*
UBYTE  version[] = VERSION;
UBYTE  date[]    = DATE;
*/
UBYTE  alt_serv[ NBRSIZE+1 ];     /* alternate long distance service # */
UBYTE  phl_empty[] = "Phone library empty\n";
UBYTE  phonedir[ MAXFNAME ] = "\000";
UBYTE  sbuff[120];                /* sprintf work buffer */
UBYTE  buffer[BufSize];           /* ASCII capture buffer */
UBYTE  xbuffer[SECSIZ];           /* XMODEM sector buffer */
UBYTE  *diskbuff = NULL;          /* allocated XMODEM file buffer */
/*
UBYTE  def_dir[ 41 ] = "RAM:";
*/
int   altservflg  = FALSE;
int   numbufs;           /* number of disk buffers allocated */

unsigned
    fd,                  /* XMODEM file being sent/received */
    prt     = 0,         /* printer device */
    sector,              /* sector being sent/received */
    baud    = 1200,      /* current baud rate */
    cancel  = FALSE,     /* TRUE if CAN received and abort_flag is TRUE */
    abort   = FALSE,     /* TRUE if user aborts XMODEM xfers with ESC */
    timeout = FALSE,     /* TRUE if timeout during character receive */
    chopflg = TRUE,      /* TRUE if we CHOP files */
    xonflg  = FALSE,     /* does driver do XON/XOFF? */
    echoflg = FALSE;     /* TRUE to echo received characters to sender */

USHORT printon = FALSE;  /* TRUE if printing */
USHORT capton  = FALSE;  /* TRUE if capturing */
USHORT crcflag = TRUE;   /* TRUE if crc mode, FALSE for checksum */
                         /* can be changed in send mode by remote receiver */
USHORT xfrmode = TRUE;   /* same as crcflag, except only under user control */
USHORT crc;              /* 16 bit crc value */
UBYTE  checksum;

int viewflg      = FALSE; /* TRUE if XMODEM transfers are viewed on screen */
int close_window = FALSE; /* TRUE if we have to close the split window */
int debug        = FALSE; /* general debug flag */
int asciiflg     = FALSE; /* TRUE to add CR/LF pairs to XMODEM send */
int interlace    = FALSE;

short dcd        = 0;     /* state of modem Carrier Detect */
short wxflag     = FALSE;
short halfduplex = FALSE; /* TRUE if HalfDuplex mode */
UWORD dos_version = 0;

UBYTE *keymacro[ KEYMACS ];
UBYTE commkeys[ MAXFNAME ] = KEYFILE;
UBYTE *capt_status;

extern struct Gadget Strgadget;

/*   Intuition always wants to see these declarations */
struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase = NULL;

struct Screen       *commscreen = NULL;
struct NewScreen    CommScreen =
   {  0,0,640,200,2,
      0,1,
      HIRES | SPRITES,
      CUSTOMSCREEN,
      0,install.version,0,0
};

extern struct MenuItem PlibItems[];
struct Window       *rx_window  = NULL;     /* ptr to receive   window */
struct Window       *tx_window  = NULL;     /* ptr to transmit  window */
struct Window       *req_window = NULL;     /* ptr to requester window */
struct Window       *vw_window  = NULL;     /* ptr to file view window */
struct Window       *stat_window= NULL;     /* ptr to status window */
struct Window       *st_window  = NULL;

struct Gadget tx_window_close  = {
      NULL,             /* next gadget in list */
      0,0,              /* left, top edge of hit box */
      20,9,             /* width, height of hit box  */
      GADGHCOMP,        /* flags */
      RELVERIFY,        /* activation */
      BOOLGADGET,       /* gadget type */
      NULL,             /* rendering */
      NULL,             /* select render */
      NULL,             /* text for this gadget */
      0,                /* Mutual exclude */
      NULL,
      0,0               /* user fields */
      }, rx_window_close = {
         NULL,0,0,20,9,GADGHCOMP,RELVERIFY,BOOLGADGET,NULL,NULL,NULL,
         0,NULL,0,0};

struct Gadget tx_window_front = {
      NULL,             /* next gadget in list */
      620,0,            /* left, top edge of hit box */
      20,9,             /* width, height of hit box  */
      GADGHCOMP,        /* flags */
      RELVERIFY,        /* activation */
      BOOLGADGET,       /* gadget type */
      NULL,             /* rendering */
      NULL,             /* select render */
      NULL,             /* text for this gadget */
      0,                /* Mutual exclude */
      NULL,
      0,0               /* user fields */
      }, rx_window_front = {
         NULL,620,0,20,9,GADGHCOMP,RELVERIFY,BOOLGADGET,NULL,NULL,NULL,
         0,NULL,0,0};


struct Gadget tx_window_back = {
      NULL,             /* next gadget in list */
      595,0,            /* left, top edge of hit box */
      20,9,             /* width, height of hit box  */
      GADGHCOMP,        /* flags */
      RELVERIFY,        /* activation */
      BOOLGADGET,       /* gadget type */
      NULL,             /* rendering */
      NULL,             /* select render */
      NULL,             /* text for this gadget */
      0,                /* Mutual exclude */
      NULL,
      0,0               /* user fields */
      }, rx_window_back = {
         NULL,595,0,20,9,GADGHCOMP,RELVERIFY,BOOLGADGET,NULL,NULL,NULL,
         0,NULL,0,0};

#ifdef XYZ
struct Image knob;

struct PropInfo propinfo = {
   AUTOKNOB | FREEHORIZ,
   0,0,
   MAXPOT / 10,0,
   0,0,0,0,0,0
};


struct Gadget delay_prop = {
      NULL,                   /* next gadget */
      110,15,                 /* left, top */
      100,PROPSIZE-1,         /* width, height */
GADGIMAGE | GADGHNONE,        /* Flags */
      0,                      /* Activation */
      PROPGADGET,             /* type */
      (APTR)&knob,            /* render */
      NULL,                   /* select render */
      NULL,                   /* text */
      NULL,                   /* mutual exclude */
      (APTR)&propinfo,        /* special info */
      NULL,                   /* id */
      NULL                    /* user info */
};
#endif
struct IntuiMessage *NewMessage = NULL;    /* msg structure for GetMsg() */
int    split_screen = FALSE;               /* TRUE if 2 screens FALSE if 1 */

/* RX window structure */
struct NewWindow RXWindow = {
   0, 0, 640, 190,
   0, 1,
   NULL,  /* use TX window IDCMP */
   BORDERLESS | NOCAREREFRESH | SMART_REFRESH | BACKDROP,
   NULL, NULL,
   NULL,
   NULL, NULL,
   0,148,0,0,   /* I use MinHeight for Split Screen Height */
   CUSTOMSCREEN
};

/* status window structure */
struct NewWindow STWindow = {
   0, 190, 640,10,
   0, 1,
   NULL,  /* use TX window IDCMP */
   BORDERLESS | NOCAREREFRESH | SMART_REFRESH | BACKDROP,
   NULL, NULL,
   NULL,
   NULL, NULL,
   0,0,0,0,
   CUSTOMSCREEN
};

/* TX window structure */
struct NewWindow TXWindow = {
   0, 0, 640, 190,
   0, 1,
   CLOSEWINDOW | MENUPICK | RAWKEY | GADGETUP,
   BORDERLESS  | NOCAREREFRESH | SMART_REFRESH | ACTIVATE | BACKDROP,
   NULL, NULL,
   NULL,
   NULL, NULL,
   0,43,0,0,   /* I use MinHeight for Split screen height */
   CUSTOMSCREEN
};

struct NewWindow VWindow = {     /* file view window */
   10, 20, 600, 102,
   0, 1,
   NULL,  /* use TX window IDCMP */
   SMART_REFRESH | NOCAREREFRESH | WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH
   | WINDOWSIZING,
   NULL, NULL,
   (UBYTE *)"View Window",
   NULL, NULL,
   80, 30, 640, 160,
   CUSTOMSCREEN
};

struct NewWindow RQWindow = {
   0, 0, REQWIDTH+5, REQHEIGHT+13,
   0, 1,
   NULL,       /* No IDCMP, we'll use the tx_window port */
   WINDOWDRAG | NOCAREREFRESH | SMART_REFRESH | ACTIVATE,
   NULL,
   NULL,
   (UBYTE *)"INPUT WINDOW",
   NULL,
   NULL,
   0,0,0,0,
   CUSTOMSCREEN
};


extern struct MenuItem  FileItems[], SysItems[], ModeItems[], SerialIems[],
                        DebugItems[], PhoneItems[];
extern struct IntuiText FileText[], SysText[], ModeText[], SerialText[],
                        DebugText[], EolText[], BaudText[],
                        ParityText[], LengthText[], StopText[],
                        DuplexText[], AltFileText[], PhoneText[];

extern struct Menu menu[];

/* declarations for the serial stuff */

struct IOExtSer *Read_Request  = NULL;
struct IOExtSer *Write_Request = NULL;
struct IOExtSer *Echo_Request  = NULL;

UBYTE     rs_in[ RSBUFSIZ + 1 ];
UBYTE     rs_out[2];
UBYTE     rs_echo[2];

/* declarations for console device */

extern struct MsgPort  *CreatePort();
extern struct IOStdReq *CreateStdIO();

struct IOStdReq *tx_con    = NULL;
struct IOStdReq *rx_con    = NULL;
struct IOStdReq *st_con    = NULL;
struct IOStdReq *vw_con    = NULL;
struct MsgPort  *tx_con_mp = NULL;
struct MsgPort  *rx_con_mp = NULL;
struct MsgPort  *st_con_mp = NULL;
struct MsgPort  *vw_con_mp = NULL;

/* Timer device allocation */

struct timerequest TimerIO;
struct MsgPort TimerMsgPort;

/* requester allocation */

UBYTE enter_txt[]  = "ENTER ";
UBYTE cancel_txt[] = "CANCEL";
UBYTE ok_txt[]     = " OK ";

struct IntuiText  canceltext =
      {
         ORANGE,  /* front pen */
         WHITE,   /* back pen  */
         JAM2,    /* draw mode */
         0,       /* left edge */
         0,       /* top edge  */
         0,       /* text font */
       cancel_txt,/* text      */
         NULL     /* next text */
      };

struct IntuiText  Reqtext =
      {
         BLACK,WHITE,JAM2,51,-10,0,NULL,NULL
      };

struct IntuiText  Entertxt =
      {
         BLACK,WHITE,JAM2,0,-10,0,enter_txt,&Reqtext
      };

struct IntuiText  oktext =
      {
         BLACK,WHITE,JAM2,0,0,0,ok_txt,NULL
      };

struct StringInfo string =
    {
      NULL,             /* buffer for input data */
      NULL,             /* no undo buffer */
      0,MAXFNAME,       /* position and max characters in buffer */
      0,                /* position of first displayed character */
      0,0,0,0,0,0,0,0
    };

struct StringInfo Dirstr =
    {
      install.DefDir,NULL,0,40,0,0,0,0,0,0,0,0,0
    };


struct Gadget     Dirgadget =
    {
      NULL,             /* next gadget in list */
      76,38,            /* left, top edge of hit box */
      96,8,             /* width, height of hit box  */
    GADGHCOMP,          /* flags */
ENDGADGET | RELVERIFY,  /* activation */
    STRGADGET,          /* gadget type */
      NULL,             /* rendering */
      NULL,             /* select render */
      NULL,             /* text for this gadget */
      0,                /* Mutual exclude */
      (APTR)&Dirstr,    /* special info */
      0,0               /* user fields */
    };

struct Gadget     OKgadget =
    {
      NULL,16,38,32,8,GADGHCOMP,ENDGADGET | RELVERIFY,BOOLGADGET,NULL,
      NULL,&oktext,0,NULL,0,0
    };

struct Gadget     Cancelgadget =
    {
      NULL,200,38,48,8,GADGHCOMP,ENDGADGET | RELVERIFY,BOOLGADGET,NULL,
      NULL,&canceltext,0,NULL,0,0
    };

struct Gadget     Strgadget =
    {
      NULL,12,26,240,8,GADGHCOMP,ENDGADGET | RELVERIFY,STRGADGET,NULL,
      NULL,&Entertxt,0,(APTR)&string,0,0
    };

struct Process *this_process;

