// Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator
// From input file "ncrt.p"


#include <p2c/p2c.h>


#include <p2c/exithandler.h>

#define NCRT_G
#include "/software/include/p2c/ncrt.h"


#include "/software/include/p2c/keyconst.h"



// globals for KeyPressed & ReadKey
static char _pre_char,_pre_filled,_pre_scanned,_pre_esc;
static int  _pre_int;
unsigned char Letters_Map[26] = {
 fmAltAKey, fmAltBKey, fmAltCKey, fmAltDKey, fmAltEKey, fmAltFKey, fmAltGKey, fmAltHKey, 
 fmAltIKey, fmAltJKey, fmAltKKey, fmAltLKey, fmAltMKey, fmAltNKey, fmAltOKey, fmAltPKey, 
 fmAltQKey, fmAltRKey, fmAltSKey, fmAltTKey, fmAltUKey, fmAltVKey, fmAltWKey, fmAltXKey,	
 fmAltYKey, fmAltZKey 
 };

// global for ExitHandler
ExitProcType NCrtSaveExit;


uchar EGA_Colors[16] = {
  0, 1, 2, 3, 4, 5, 20, 7, 56, 57, 58, 59, 60, 61, 62, 63
};


void _Set_Window(void)
{
  int x,y,w,h;

  delwin(stdscr);
  x=(WindMin & 0xff);
  y=(WindMin >> 8);
  w=(WindMax & 0xff) + 1 - (WindMin & 0xff);
  h=(WindMax >> 8)  +1 - (WindMin >> 8);
  stdscr=newwin(y,x,h,w);
}


void Window(int x1, int y1, int x2, int y2)
{
  WindMin = (x1 & 0xff) | ((y1 & 0xff) << 8);
  WindMax = (x2 & 0xff) | ((y2 & 0xff) << 8);

  _Set_Window();
}


void ClrScr(void)
{
  clear();
}


void ClrEol(void)
{
  clrtoeol();
}


void ClrEof(void)
{
  clrtobot();
}


void Sound(unsigned short Hz)
{
}


void NoSound(void)
{
}


void Delay(unsigned short ms)
{
  usleep(1000*ms);
}




char _process_key(int c)
{
   char h;
 
   if ((-31 <= c) && (c <= -6))	// ALT + Key pressed
      {
       _pre_filled=TRUE;
       _pre_char=Letters_Map[c+31];
       h=0;
      }
     else
   if ((225 <= c) && (c <= 250))
      {   
       _pre_filled=TRUE;
       _pre_char=Letters_Map[c-225];
       h=0;
      }   
     else switch(c)
        {
                case  KEY_DOWN: { _pre_filled=TRUE; _pre_char=fmDownKey; h=0; }
         break; case    KEY_UP: { _pre_filled=TRUE; _pre_char=fmUpKey; h=0; }      
         break; case  KEY_LEFT: { _pre_filled=TRUE; _pre_char=fmLeftKey; h=0; }      
         break; case KEY_RIGHT: { _pre_filled=TRUE; _pre_char=fmRightKey; h=0; }      

         break; case  KEY_F(1): { _pre_filled=TRUE; _pre_char=fmF1Key; h=0; }      
         break; case  KEY_F(2): { _pre_filled=TRUE; _pre_char=fmF2Key; h=0; }      
         break; case  KEY_F(3): { _pre_filled=TRUE; _pre_char=fmF3Key; h=0; }      
         break; case  KEY_F(4): { _pre_filled=TRUE; _pre_char=fmF4Key; h=0; }      
         break; case  KEY_F(5): { _pre_filled=TRUE; _pre_char=fmF5Key; h=0; }      
         break; case  KEY_F(6): { _pre_filled=TRUE; _pre_char=fmF6Key; h=0; }      
         break; case  KEY_F(7): { _pre_filled=TRUE; _pre_char=fmF7Key; h=0; }      
         break; case  KEY_F(8): { _pre_filled=TRUE; _pre_char=fmF8Key; h=0; }      
         break; case  KEY_F(9): { _pre_filled=TRUE; _pre_char=fmF9Key; h=0; }      
         break; case KEY_F(10): { _pre_filled=TRUE; _pre_char=fmF10Key; h=0; }      
         break; case KEY_F(11): { _pre_filled=TRUE; _pre_char=fmF11Key; h=0; }      
         break; case KEY_F(12): { _pre_filled=TRUE; _pre_char=fmF12Key; h=0; }      

         break; case  KEY_BACKSPACE: { h=8; }      

         break; case  KEY_HOME: { _pre_filled=TRUE; _pre_char=fmPos1Key; h=0; }      
         break; case   KEY_END: { _pre_filled=TRUE; _pre_char=fmEndKey; h=0; }      
         break; case    KEY_DC: { _pre_filled=TRUE; _pre_char=fmDelKey; h=0; }      
         break; case    KEY_IC: { _pre_filled=TRUE; _pre_char=fmInsKey; h=0; }      
         break; case KEY_NPAGE: { _pre_filled=TRUE; _pre_char=fmPgDnKey; h=0; }      
         break; case KEY_PPAGE: { _pre_filled=TRUE; _pre_char=fmPgUpKey; h=0; }      
         break; case       127: { _pre_filled=TRUE; _pre_char=fmDelKey; h=0; }      
         
         break;        default: h=c;
        } 
        
#ifdef DEBUG
   fprintf(stderr,"_process_key_return(%d)\n",h);
#endif  
  return h;      
}


char ReadKey(void)
{
  char c;


  if ((_pre_filled == TRUE) || (_pre_scanned == TRUE)) 
     {
      if (_pre_scanned == TRUE)
         {
          _pre_scanned=FALSE;
          c=_process_key(_pre_int);
#ifdef DEBUG
          fprintf(stderr,"ReadKey_return(%d)\n",c);
#endif
          return c;
         } 

      _pre_filled=FALSE;
#ifdef DEBUG
     fprintf(stderr,"ReadKey_return(%d)\n",_pre_char);
#endif
      return _pre_char;
     } 
   else 
     { 
      do { 
          usleep(20000);
         } while (!KeyPressed());

      _pre_scanned=FALSE;
      c=_process_key(_pre_int);
#ifdef DEBUG
     fprintf(stderr,"ReadKey_return(%d)\n",c);
#endif
      return c;
     }
}


boolean KeyPressed(void)
{
   int c;
   
   if ((_pre_scanned == TRUE) || (_pre_filled == TRUE))
      return TRUE;
   
   c=getch();
   if (c != ERR)
      {
       _pre_scanned=TRUE;
       _pre_int=c;

#ifdef WITH_CONSOLE_SUPPORT
       if ((_pre_esc==FALSE) && (_pre_int==27))
          {
           _pre_scanned=FALSE;
           _pre_esc=TRUE;
           return FALSE;
          } 
          
       if (_pre_esc==TRUE)  
          {
           if (('a'<=c) && (c<='z'))
              {
               _pre_int=(c-128);
               _pre_scanned=TRUE;
               _pre_esc=FALSE;
               return TRUE;
              }
            else 
              {
               _pre_filled=TRUE;
               _pre_char=27;
               _pre_esc=FALSE;
              }
          }        
#endif
                 
#ifdef DEBUG       
       fprintf(stderr,"keypressed_return(TRUE+%d)\n",_pre_int);
#endif
       return TRUE;
      }
   
   return FALSE;
}


void GotoXY(int x, int y)
{
  move(y-1,x-1);
}


unsigned short WhereX(void)
{
  return stdscr->_curx;
}


unsigned short WhereY(void)
{
  return stdscr->_cury;
}




unsigned long int ct_attr(unsigned char TA)
{
  int b,c,f,a;

  b=(TA & 0x70) >> 1;
  f=(TA & 0x07);
  c = f | b;
  cur_color=c;

  a=COLOR_PAIR(c); 
  switch (TA & 0x88)
      {
              case 0x00: cur_attr=0;	return a;			 		
       break; case 0x08: if (TA == 0x08) 
                            {
                             cur_attr=A_DIM;
                             return cur_attr;
                            } 
                          else
                            {
                             cur_attr=A_BOLD; 
                             return (a | A_BOLD);         		
                            } 
       break; case 0x80: cur_attr=A_BLINK;	return (a | A_BLINK);          		
       break; case 0x88: if (TA == 0x88)
                            {
                             cur_attr=A_DIM|A_BLINK;
                             return cur_attr;
                            }
                          else   
                            {
                             cur_attr=A_BOLD|A_BLINK; 
                             return (a | A_BOLD | A_BLINK);	
                            } 
       break; default: return 0; 
    }
}


void _Set_Attribute(void)
{
   attr_set(ct_attr(TextAttr));
   wbkgdset(stdscr,ct_attr(TextAttr)|' '); 
}


void TextColor(uchar b)
{
  TextAttr = (TextAttr & 0xf0) | (b & 0x0f);
//  attr_set(ct_attr(TextAttr));
  _Set_Attribute();
}


void TextBackGround(uchar b)
{
  TextAttr = (TextAttr & 0x0f) | ((b & 0x0f) << 4);
//  attr_set(ct_attr(TextAttr));
  _Set_Attribute();
}



static int map_EGA_color(int i, int s)
{
  uchar j = (EGA_Colors[i] >> s) & 1;
  uchar k = (EGA_Colors[i] >> (s + 2)) & 2;

  return ((i + k) * 21);
}




void _CRT_Init(void)
{
  int i,j,r,g,b;
  char bitrev[8] = { 0,4,2,6,1,5,3,7 };

  NCrtSaveExit.proc = ExitProc.proc;
  NCrtSaveExit.link = ExitProc.link;
  
  ExitProc.proc = _CRT_Done;
  ExitProc.link = (long int)&NCrtSaveExit; 

  initscr();
  start_color();
  cbreak();

  _pre_filled=FALSE;
  _pre_scanned=FALSE;
  _pre_esc=FALSE;

  if (has_colors())
   {
    if (can_change_color())
       {
        fprintf(stderr,"\b YES -- can change RGB-Values\b\n");
        init_color(bitrev[3],0,0x40,0x80);
       }
    
      for (int i=0;i<8;i++)
        {
         r=map_EGA_color(i,2);
         g=map_EGA_color(i,1);
         b=map_EGA_color(i,0);

         init_color(i,r,g,b);
        }
           
        
/*
    for (int j=0;j<8;j++)
      for (int i=0;i<16;i++)
        {
         init_pair((j << 4)+i,i,j);
        }
*/
    if (COLOR_PAIRS==64)
      for (i=0;i<8;i++)
        for (j=0;j<8;j++)
//          if ((j==0) && (i==0))
//              {}
//            else
              init_pair((i*8)+j,bitrev[j],bitrev[i]);
   }

   
//  cbreak();
  noecho();
  nonl();
  intrflush(stdscr,FALSE);
  keypad(stdscr,TRUE);
  nodelay(stdscr,TRUE);
}


void _CRT_Done(void)
{
  endwin();

#ifdef DEBUG
  fprintf(stderr,"NCRT.Done\n");
#endif  
}




void _NCRT_init(void)
{
  static int _was_initialized = 0;
  if (_was_initialized++)
    return;
}
// p2c: Note: Remember to call _NCRT_init() in main program [215]



// End.
