/* ---------------------------------------------------------------------
   Fred P. for J2K Library
   LGPL Licensed.
   http://j2k.sourceforge.net/
   Initially 'ANSI-PRINT'

   Project      : CLIB
   Function     : VT-100 screen functions
   Module       : VT100
   File         : VT100.C
   Created      : 26-02-2003
   Modified     : 09-09-2005
   --------------------------------------------------------------------- */

/* ---------------------------------------------------------------------
   Log

   1.3 - 09-09-2005 fflush (stdout) added
   1.2 - 16-12-2004 'vt100 prefix added to avoid confusion.
   1.1 - 21-09-2004 Types trimming
   1.0 - 26-02-2003 Initial version

   --------------------------------------------------------------------- */

#ifdef __cplusplus
#error This source file is not C++ but rather C. Please use a C-compiler
#endif

#include "ed/inc/vt100.h"

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

/* macros ============================================================== */
#define VER "1.3"
#define ID "VT-100"

/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private variables =================================================== */

static short Ansi2QBColorTable[] =
{
   0,                           /* Black  -> Black  */
   4,                           /* Blue   -> Red    */
   2,                           /* Green  -> Green  */
   6,                           /* Cyan   -> Yellow */
   1,                           /* Red    -> Blue   */
   5,                           /* Purple -> Purple */
   3,                           /* Brown  -> Cyan   */
   7,                           /* White  -> White  */
};

/* private functions =================================================== */
/* internal public functions =========================================== */
/* public variables ==================================================== */

const char *colorNameTable[] =
{
   "Black",
   "Dark Blue",
   "Dark Green",
   "Dark Cyan",
   "Dark Red",
   "Dark Purple",
   "Brown",
   "Dark White",
   "Gray",
   "Bright Blue",
   "Bright Green",
   "Bright Cyan",
   "Bright Red",
   "Bright Purple",
   "Bright Yellow",
   "Bright White",
};

/* entry points ======================================================== */

/* ---------------------------------------------------------------------
   vt100_sid ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
char const *vt100_sid (void)
{
   return ID;
}

/* ---------------------------------------------------------------------
   vt100_sver ()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
char const *vt100_sver (void)
{
   return VER;
}

/* ---------------------------------------------------------------------
   locate()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void vt100_locate (uint r, uint c)
{
   printf ("\033[%u;%uH", (uint) r, (uint) c);
   fflush (stdout);
}

/* ---------------------------------------------------------------------
   screen()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void vt100_screen (vt100_VideoMode_e mode)
{
   printf ("\033[=%u", (uint) mode);
   fflush (stdout);
}

/* ---------------------------------------------------------------------
   screenNoLine()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void vt100_screenNoLine (vt100_VideoMode_e mode)
{
   printf ("\033[=%ul", (uint) mode);
   fflush (stdout);
}

/* ---------------------------------------------------------------------
   center()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void vt100_center (uint row, const char *text)
{
   uint col = 41 - (strlen (text) / 2);
   vt100_locate (row, col);
   printf ("%s", text);
   fflush (stdout);
}

/* ---------------------------------------------------------------------
   color()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void vt100_color (vt100_Color_e fg, vt100_Color_e bg, vt100_TextAttr_e txtAttr)
{
   uint f = (uint) fg + 30;
   uint b = (uint) bg + 40;
   uint a = (uint) txtAttr;

   printf ("\033[%u;%u;%um", (uint) a, (uint) f, (uint) b);
   fflush (stdout);
}

/* ---------------------------------------------------------------------
   qbColor()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void vt100_qbColor (uint fg, vt100_Color_e bg)
{
   uint a = 0;
   if (fg > 7 && fg <= 15)
   {
      a = 1;
      fg -= 8;
   }
   if (fg > 15 && fg <= 22)
   {
      a = 5;
      fg -= 16;
   }
   if (fg > 22 && fg <= 30)
   {
      a = 6;
      fg -= 24;
   }

   {
      uint qb_fg = Ansi2QBColorTable[fg];
      uint qb_bg = Ansi2QBColorTable[bg];
      uint f = (qb_fg) + 30;
      uint b = (qb_bg) + 40;

      printf ("\033[%u;%u;%um", (uint) a, (uint) f, (uint) b);
      fflush (stdout);
   }
}

/* ---------------------------------------------------------------------
   cls()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
void vt100_cls (vt100_Color_e bg)
{
   vt100_qbColor (7, bg);
   printf ("\033[2J");
   fflush (stdout);
}
