// $Id: cio_linux.h,v 1.13 2008/05/10 01:17:37 lpoulsen Exp $
// Copyright (C) 2001 Wi_LAN Technologies, Inc
//
// $Log: cio_linux.h,v $
// Revision 1.13  2008/05/10 01:17:37  lpoulsen
// Making econ -x filename work (not yet complete)
//
// Revision 1.12  2005/06/24 18:17:04  lpoulsen
// Make sure this module works on OpenBSD as well as Linux.
//
// Revision 1.11  2002/05/31 23:18:48  mourlouk
// Yet another revision - make the function keys to work (linux console - they were already working through telnet)
//
// Revision 1.7  2002/05/21 19:52:27  lpoulsen
// Fix for PSR int236: Econsole uses 100% CPU
//     Code inspection confirmed that the thread that reads from the
//     console terminal has set the TTY to always return immediately
//     from a read. After changing the TTY mode to return only if
//     a character was received or 200 ms passed, CPU usage has
//     dropped to the expected minimal amount.
// Also some readability cleanup.
//
// Revision 1.6  2001/11/13 01:23:33  lpoulsen
// Remove compiler warnings
//
// Revision 1.5  2001/11/13 00:38:51  mourlouk
// ....................
//
// Revision 1.4  2001/11/12 23:06:48  mourlouk
// making happy compiler (II)
//
// Revision 1.3  2001/11/07 00:02:41  mourlouk
// Fixed a small bug on Linux client - now if an error occurs, and return to Linux environment, everything is normal
//
// Revision 1.2  2001/10/30 01:07:45  lpoulsen
// Add "module ID" statement, audit trail generator and copyright line
// Remove "win_or_linux.h" and use a -D in Linux makefile instead.
//
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>

char getchvalue;
struct termios save;

int kbhit(void);
char getch(void);
void initscr(void);
void endwin(void);

void 
endwin()
{
    tcsetattr(0, TCSANOW, &save);
}

void 
initscr()
{
    struct termios term;
    if (tcgetattr(0, &term) == -1)
    {
        fprintf(stderr, "standard input not a tty\n");
        exit(1);
    }
    save = term;

    term.c_lflag &= ~ICANON;
    term.c_lflag &= ~ECHO;

    term.c_cc[VMIN] = 0;
    term.c_cc[VTIME] = 2;

    tcsetattr(0, TCSANOW, &term);
}

int 
kbhit()
{
    int ifkeyin;

    ifkeyin = read(0, &getchvalue, 1);
    return ifkeyin;
}

// custom getch() implementation
int 
a_getch()
{
    char ch1, ch2;

    while (1)
    {
        if (kbhit())
            break;
    }

    // check for backspace
    if (getchvalue == 127)
        return 8;

    // now check for special keys - F1,F2,F3
    if (getchvalue != 27)
        return getchvalue;

    while (1)
    {
        if (kbhit())
            break;
    }

    ch1 = getchvalue;
    if (ch1 == 27)
        return ESC_KEY;

    while (1)
    {
        if (kbhit())
            break;
    }
    ch2 = getchvalue;

    if ((ch1 != 79) && (ch1 != 91))
        return UNKNOWN_KEY;

    switch (ch2)
    {
    case 65:
        return ctrl('P');       // up arrow

    case 66:
        return ctrl('N');       // down arrow

    case 67:
        return ctrl('F');       // right arrow

    case 68:
        return ctrl('B');       // left arrow

    case 80:
        return F1_KEY;
    case 81:
        return F2_KEY;
    case 82:
        return F3_KEY;
    case 83:
        return F4_KEY;
    case 51:
        return 4;               //DEL

    case 91:                   // Function Keys from console - not telnet

        while (1)
        {
            if (kbhit())
                break;
        }
        switch (getchvalue)
        {
        case 65:
            return F1_KEY;
        case 66:
            return F2_KEY;
        case 67:
            return F3_KEY;
        case 68:
            return F4_KEY;
        }
    default:
        ;
        return UNKNOWN_KEY;
    }
}

void
setNormalTextBackground()
{
}

void
setReverseTextBackground()
{
}

void 
initCIOSettings()
{
    initscr();
}

void 
restoreCIOSettings()
{
    endwin();
}

char
ctrl(char control_key)
{
    return control_key & 0x1F;
}

void 
moveCursor(int xDis, int yDis)
{
    printf("moveCursor: NOT IMPLEMENTED YET\n");
}

void 
clear_screen()
{
    printf("clear_screen: NOT IMPLEMENTED YET\n");
}

void
e_print(char c)
{
    if (showOutputToScreen)
    {
        putchar(c);
        fflush(stdout);
        if (howToPrint == PRINT_FILE)
            fprintf(console_out_file, "%c", c);

    }
    else if (howToPrint == PRINT_FILE)
    {
        fprintf(console_out_file, "%c", c);
    }

}
