/*-- AutoRev header do NOT edit!
*
*   Program         :   Console.c
*   Copyright       :   Copyright © 1991-92 Jaba Development
*   Author          :   Jan van den Baard
*   Creation Date   :   04-Apr-92
*   Current version :   2.0
*   Translator      :   Dice v2.06.40
*
*   REVISION HISTORY
*
*   Date          Version         Comment
*   ---------     -------         ------------------------------------------
*   04-Apr-92     2.0             Console routine (rewrite)
*
*-- REV_END --*/

#include "View.h"

Prototype long OpenConsole( void );
Prototype void CloseConsole( void );
Prototype void ConvertKeyTab( void );
Prototype void Inform( UBYTE * );
Prototype void Display( struct Line * );

extern struct Window        *vwWindow;

static struct MsgPort       *CPort         = NULL;
struct IOStdReq             *CMsg          = NULL;
APTR                         ConsoleDevice = NULL;

UBYTE                        KeyTable[ 64 ];
UBYTE                        SKeyTable[ 64 ];

UBYTE                        MoveStr[]  = { CSI, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x48, 0x00 };
UBYTE                        ClearStr[] = { 0x0c, 0x00 };
UBYTE                        ScrUpStr[] = { CSI, 0x53, 0x00 };
UBYTE                        ScrDnStr[] = { CSI, 0x54, 0x00 };
UBYTE                        StdStr[]   = { ESC, 0x63, CSI, 0x30, 0x20, 0x70, CSI, 0x00, 0x38, 0x79, 0x00 };
UBYTE                        NoScrStr[] = { CSI, 0x3e, 0x31, 0x6c };
UBYTE                       *ResStr     = "\033[0m";
UBYTE                       *FFStr      =  "\033[7mF\033[0m";

long OpenConsole( void )
{
    if ( CPort = CreateMsgPort()) {
        if ( CMsg = ( struct IOStdReq * )CreateIORequest( CPort, (long)sizeof( struct IOStdReq ))) {

            CMsg->io_Data   = (APTR) vwWindow;
            CMsg->io_Length = (LONG) sizeof( struct Window );

            if ( ! OpenDevice( "console.device", NULL, ( struct IORequest * )CMsg, NULL )) {
                ConsoleDevice = CMsg->io_Device;
                ConvertKeyTab();
                Inform( StdStr );
                return( TRUE );
            }
        }
    }

    CloseConsole();
    return( FALSE );
}

void CloseConsole( void )
{
    if ( ConsoleDevice ) {
        CloseDevice(( struct IORequest * )CMsg );
        ConsoleDevice = NULL;
    }

    if ( CMsg ) {
        DeleteIORequest(( struct IORequest * )CMsg );
        CMsg = NULL;
    }

    if ( CPort ) {
        DeleteMsgPort( CPort );
        CPort = NULL;
    }
}

void ConvertKeyTab( void )
{
    struct InputEvent   ievent;
    UWORD               i;

    setmem( &ievent, sizeof( struct InputEvent ), NULL );

    ievent.ie_Class = IECLASS_RAWKEY;

    for ( i = 0; i < 64; i++ ) {
        ievent.ie_Code = i;
        RawKeyConvert( &ievent, &KeyTable[ i ], 1, NULL );
    }

    ievent.ie_Qualifier = IEQUALIFIER_LSHIFT;

    for ( i = 0; i < 64; i++ ) {
        ievent.ie_Code = i;
        RawKeyConvert( &ievent, &SKeyTable[ i ], 1, NULL );
    }
}

void Inform( UBYTE *text )
{
    CMsg->io_Command = CMD_WRITE;
    CMsg->io_Data    = (APTR) text;
    CMsg->io_Length  = (LONG) strlen( text );

    DoIO(( struct IORequest * )CMsg );
}

void Display( struct Line *line )
{
    Inform( ResStr );
    Inform( NoScrStr );

    CMsg->io_Command = CMD_WRITE;
    CMsg->io_Data    = (APTR) line->Text;
    CMsg->io_Length  = (LONG) line->Size - 1;

    if ( line->Text[ 0 ] == FF ) {
        Inform( FFStr );
        CMsg->io_Data   = (APTR) &line->Text[ 1 ];
        CMsg->io_Length = (LONG) line->Size - 2;
    }

    DoIO(( struct IORequest * )CMsg );
}
