/*
****************************************************************************

      Console Device ToolKit Subroutines.
      Standard Include File.

      Console.h.
      Version 1.1.

      Paris E. Bingham Jr.
      Copyright © 1986 - 1990  All Rights Reserved.


      History:

    Version      Date      Comments
    -------    --------    -----------------------------------------------
      1.0      10/01/89    PEB - Created.
      1.1      09/10/90    PEB - Add support for Manx C V5.0.
               09/22/90    PEB - Add support for SAS C V5.10.

****************************************************************************
*/

#ifndef  DT_CONSOLE_H
#define  DT_CONSOLE_H

/*
      Compiler Definitions
*/

#ifdef   SAS
#define  ANSI_PROTO        1
#endif

#ifdef   MANXV3
#undef   ANSI_PROTO
#undef   NO_PRAGMAS
#define  NO_PRAGMAS        1
#define  MANX              1
#endif

#ifdef   MANXV5
#define  ANSI_PROTO        1
#define  MANX              1
#endif

/*
      Include Files
*/

#include <exec/types.h>
#include <exec/exec.h>
#include <exec/execbase.h>
#include <devices/console.h>
#include <devices/keymap.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

#ifdef   SAS
#include <proto/exec.h>
#include <proto/intuition.h>
#endif

#ifdef   MANX
#ifndef  NO_PRAGMAS
#include <pragmas.h>
#else
#include <functions.h>
#endif
#endif

/*
      Definitions
*/

/*
****************************************************************************
      DTConsole - Console Control Structure
****************************************************************************
*/

struct   DTConsole   {
   ULONG             co_flags;               /* Control flags */
   struct   Window   *co_window;             /* Console window */
   struct   MsgPort  *co_wport;              /* Write reply port */
   struct   IOStdReq *co_wreq;               /* Write request block */
   struct   MsgPort  *co_rport;              /* Read reply port */
   struct   IOStdReq *co_rreq;               /* Read request block */
   LONG              co_error;               /* Error on request */
};

/*  Flag bits  */
#define  DTCOB_GOTWINDOW   0                 /* Got window */
#define  DTCOF_GOTWINDOW   (1 << DTCOB_GOTWINDOW)

#define  DTCOB_GOTWPORT    1                 /* Got write reply port */
#define  DTCOF_GOTWPORT    (1 << DTCOB_GOTWPORT)

#define  DTCOB_GOTWREQB    2                 /* Got write request block */
#define  DTCOF_GOTWREQB    (1 << DTCOB_GOTWREQB)

#define  DTCOB_GOTRPORT    3                 /* Got read reply port */
#define  DTCOF_GOTRPORT    (1 << DTCOB_GOTRPORT)

#define  DTCOB_GOTRREQB    4                 /* Got read request block */
#define  DTCOF_GOTRREQB    (1 << DTCOB_GOTRREQB)

#define  DTCOB_GOTDEVICE   5                 /* Got device */
#define  DTCOF_GOTDEVICE   (1 << DTCOB_GOTDEVICE)

#define  DTCOB_READQUEUED  6                 /* Read queued */
#define  DTCOF_READQUEUED  (1 << DTCOB_READQUEUED)

#define  DTCOB_INIT        7                 /* All done */
#define  DTCOF_INIT        (1 << DTCOB_INIT)

#define  DTCOB_COPY        8                 /* Is copy */
#define  DTCOF_COPY        (1 << DTCOB_COPY)

/*  Typedef for User */
typedef  struct   DTConsole   *DTConsole_t;


/*
****************************************************************************
      Error Codes
****************************************************************************
*/

#define  DTCOE_NO_ERROR       0L             /* No error */
#define  DTCOE_NO_STRUCTURE   1L             /* No control structure */
#define  DTCOE_NO_WINDOW      2L             /* No control window */
#define  DTCOE_NO_WPORT       3L             /* Write reply port not alloc. */
#define  DTCOE_NO_WREQB       4L             /* Write req. not alloc. */
#define  DTCOE_NO_RPORT       5L             /* Read reply port not alloc. */
#define  DTCOE_NO_RREQB       6L             /* Read req. not alloc. */
#define  DTCOE_NO_DEVICE      7L             /* Device not opened */
#define  DTCOE_NO_INIT        8L             /* Control not initialized */
#define  DTCOE_NO_DATA        9L             /* No pointer to data */
#define  DTCOE_NO_LENGTH      10L            /* No data length */
#define  DTCOE_ALREADY_QUEUED 11L            /* Read already queued */
#define  DTCOE_NO_READ        12L            /* No read queued */


/*
****************************************************************************
      Useful Macros
****************************************************************************
*/

#define  DTConsoleFree(c)        { DTConsoleDestroy(c); c = NULL; }
#define  DTConsoleReadLength(c)  ((c)->co_rreq->io_Actual)
#define  DTConsoleWriteLength(c) ((c)->co_wreq->io_Actual)

#ifndef  DTBuildStdRequest
#define  DTBuildStdRequest(r,c,f,o,d,l) \
   { (r)->io_Command = (UWORD) c; \
     (r)->io_Flags = (UBYTE) f; \
     (r)->io_Offset = (ULONG) o; \
     (r)->io_Data = (APTR) d; \
     (r)->io_Length = (ULONG) l; }
#endif

/*
      Parameters:

         a - DTConsole
         b - string of parameters
*/

/***  ANSI Standard Sequences  ***/

#define  DTConsoleBACKSPACE(a)      DTConsoleWriteChar(a,(ULONG)0x08)
#define  DTConsoleBS(a)             DTConsoleWriteChar(a,(ULONG)0x08)

#define  DTConsoleTAB(a)            DTConsoleWriteChar(a,(ULONG)0x09)
#define  DTConsoleHT(a)             DTConsoleWriteChar(a,(ULONG)0x09)

#define  DTConsoleLINEFEED(a)       DTConsoleWriteChar(a,(ULONG)0x0A)
#define  DTConsoleLF(a)             DTConsoleWriteChar(a,(ULONG)0x0A)

#define  DTConsoleVERTICALTAB(a)    DTConsoleWriteChar(a,(ULONG)0x0B)
#define  DTConsoleVT(a)             DTConsoleWriteChar(a,(ULONG)0x0B)

#define  DTConsoleFORMFEED(a)       DTConsoleWriteChar(a,(ULONG)0x0C)
#define  DTConsoleFF(a)             DTConsoleWriteChar(a,(ULONG)0x0C)
#define  DTConsoleCLEARSCREEN(a)    DTConsoleWriteChar(a,(ULONG)0x0C)

#define  DTConsoleCARRIAGERETURN(a) DTConsoleWriteChar(a,(ULONG)0x0D)
#define  DTConsoleCR(a)             DTConsoleWriteChar(a,(ULONG)0x0D)

#define  DTConsoleSHIFTOUT(a)       DTConsoleWriteChar(a,(ULONG)0x0E)

#define  DTConsoleSHIFTIN(a)        DTConsoleWriteChar(a,(ULONG)0x0F)

#define  DTConsoleRESET(a)          DTConsoleWriteANSI(a,(ULONG)0x1b,"","c")

#define  DTConsoleCURSUP(a,b)       DTConsoleWriteANSI(a,(ULONG)0x9b,b,"A")
#define  DTConsoleCURSDOWN(a,b)     DTConsoleWriteANSI(a,(ULONG)0x9b,b,"B")
#define  DTConsoleCURSLEFT(a,b)     DTConsoleWriteANSI(a,(ULONG)0x9b,b,"D")
#define  DTConsoleCURSRIGHT(a,b)    DTConsoleWriteANSI(a,(ULONG)0x9b,b,"C")
#define  DTConsoleCURSNEXTLINE(a,b) DTConsoleWriteANSI(a,(ULONG)0x9b,b,"E")
#define  DTConsoleCURSPREVLINE(a,b) DTConsoleWriteANSI(a,(ULONG)0x9b,b,"F")
#define  DTConsoleCURSPOS(a,b)      DTConsoleWriteANSI(a,(ULONG)0x9b,b,"H")
#define  DTConsoleCURSHOME(a)       DTConsoleWriteANSI(a,(ULONG)0x9b,"1;1","H")

#define  DTConsoleINSCHAR(a,b)      DTConsoleWriteANSI(a,(ULONG)0x9b,b,"@")
#define  DTConsoleDELCHAR(a,b)      DTConsoleWriteANSI(a,(ULONG)0x9b,b,"P")

#define  DTConsoleINSLINE(a,b)      DTConsoleWriteANSI(a,(ULONG)0x9b,b,"L")
#define  DTConsoleDELLINE(a,b)      DTConsoleWriteANSI(a,(ULONG)0x9b,b,"M")

#define  DTConsoleSCROLLUP(a,b)     DTConsoleWriteANSI(a,(ULONG)0x9b,b,"S")
#define  DTConsoleSCROLLDOWN(a,b)   DTConsoleWriteANSI(a,(ULONG)0x9b,b,"T")

#define  DTConsoleERASEEOD(a)       DTConsoleWriteANSI(a,(ULONG)0x9b,"","J")
#define  DTConsoleERASEEOL(a)       DTConsoleWriteANSI(a,(ULONG)0x9b,"","K")

#define  DTConsoleSETMODE(a)        DTConsoleWriteANSI(a,(ULONG)0x9b,"20","h")
#define  DTConsoleRESETMODE(a)      DTConsoleWriteANSI(a,(ULONG)0x9b,"20","l")

#define  DTConsoleDSR(a)            DTConsoleWriteANSI(a,(ULONG)0x9b,"","n")

#define  DTConsoleSGR(a,b)          DTConsoleWriteANSI(a,(ULONG)0x9b,b,"m")


/***  Amiga Specific Sequences  ***/

#define  DTConsoleSETPAGELEN(a,b)   DTConsoleWriteANSI(a,(ULONG)0x9b,b,"t")
#define  DTConsoleSETLINELEN(a,b)   DTConsoleWriteANSI(a,(ULONG)0x9b,b,"u")
#define  DTConsoleSETLEFT(a,b)      DTConsoleWriteANSI(a,(ULONG)0x9b,b,"x")
#define  DTConsoleSETTOP(a,b)       DTConsoleWriteANSI(a,(ULONG)0x9b,b,"y")

#define  DTConsoleSETRAWEVENT(a,b)  DTConsoleWriteANSI(a,(ULONG)0x9b,b,"{")
#define  DTConsoleRESETRAWEVENT(a,b) DTConsoleWriteANSI(a,(ULONG)0x9b,b,"}")

#define  DTConsoleSETCURSOR(a,b)    DTConsoleWriteANSI(a,(ULONG)0x9b,b,"p")
#define  DTConsoleCURSORON(a)       DTConsoleSETCURSOR(a," ")
#define  DTConsoleCURSOROFF(a)      DTConsoleSETCURSOR(a,"0 ")

#define  DTConsoleWSR(a,b)          DTConsoleWriteANSI(a,(ULONG)0x9b,b,"q")

/*
****************************************************************************
      End of Console.h
****************************************************************************
*/

#endif

