Blob Blame History Raw
/*********************************************************/
/*  ptycon: a pty-console bridge                         */
/*  Copyright (C) 2016--2017  Z. Gilboa                  */
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
/*********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntcon/ntcon.h>

#include <ptycon/ptycon.h>
#include "ptycon_bridge_impl.h"
#include "ptycon_driver_impl.h"

/* forward declarations: sgr control handlers */
static ptyc_term_handler	sgr_illegal_code;
static ptyc_term_handler	sgr_default;
static ptyc_term_handler	sgr_set_ansi_foreground_color;
static ptyc_term_handler	sgr_set_ansi_background_color;
static ptyc_term_handler	sgr_set_aixterm_foreground_color;
static ptyc_term_handler	sgr_set_aixterm_background_color;

static const uint16_t ansi_foreground_colors[10]  = {
	NT_FOREGROUND_BLACK,
	NT_FOREGROUND_RED,
	NT_FOREGROUND_GREEN,
	NT_FOREGROUND_YELLOW,
	NT_FOREGROUND_BLUE,
	NT_FOREGROUND_MAGENTA,
	NT_FOREGROUND_CYAN,
	NT_FOREGROUND_WHITE,
	0,
	NT_FOREGROUND_WHITE	/* default */
};


static const uint16_t ansi_background_colors[10]  = {
	NT_BACKGROUND_BLACK,
	NT_BACKGROUND_RED,
	NT_BACKGROUND_GREEN,
	NT_BACKGROUND_YELLOW,
	NT_BACKGROUND_BLUE,
	NT_BACKGROUND_MAGENTA,
	NT_BACKGROUND_CYAN,
	NT_BACKGROUND_WHITE,
	0,
	NT_BACKGROUND_BLACK	/* default */
};


static const uint16_t aixterm_foreground_colors[8]  = {
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_BLACK,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_RED,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_GREEN,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_YELLOW,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_BLUE,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_MAGENTA,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_CYAN,
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_WHITE
};


static const uint16_t aixterm_background_colors[8]  = {
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_BLACK,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_RED,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_GREEN,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_YELLOW,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_BLUE,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_MAGENTA,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_CYAN,
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_WHITE
};


ptyc_term_handler * const ptyc_sgr_handlers[PTYC_SGR_ARRAY_SIZE] = {
	sgr_default,				/* 0 */		/* default */
	sgr_illegal_code,			/* 1 */		/* bold    */
	sgr_illegal_code,			/* 2 */
	sgr_illegal_code,			/* 3 */
	sgr_illegal_code,			/* 4 */		/* underlined */
	sgr_illegal_code,			/* 5 */		/* blink      */
	sgr_illegal_code,			/* 6 */
	sgr_illegal_code,			/* 7 */		/* inverse   */
	sgr_illegal_code,			/* 8 */		/* invisible */
	sgr_illegal_code,			/* 9 */
	sgr_illegal_code,			/* 10 */
	sgr_illegal_code,			/* 11 */
	sgr_illegal_code,			/* 12 */
	sgr_illegal_code,			/* 13 */
	sgr_illegal_code,			/* 14 */
	sgr_illegal_code,			/* 15 */
	sgr_illegal_code,			/* 16 */
	sgr_illegal_code,			/* 17 */
	sgr_illegal_code,			/* 18 */
	sgr_illegal_code,			/* 19 */
	sgr_illegal_code,			/* 20 */
	sgr_illegal_code,			/* 21 */
	sgr_illegal_code,			/* 22 */	/* normal */
	sgr_illegal_code,			/* 23 */
	sgr_illegal_code,			/* 24 */	/* not underlined */
	sgr_illegal_code,			/* 25 */	/* steady         */
	sgr_illegal_code,			/* 26 */
	sgr_illegal_code,			/* 27 */	/* positive */
	sgr_illegal_code,			/* 28 */	/* visible  */
	sgr_illegal_code,			/* 29 */
	sgr_set_ansi_foreground_color,		/* 30 */	/* foreground: black   */
	sgr_set_ansi_foreground_color,		/* 31 */	/* foreground: red     */
	sgr_set_ansi_foreground_color,		/* 32 */	/* foreground: green   */
	sgr_set_ansi_foreground_color,		/* 33 */	/* foreground: yellow  */
	sgr_set_ansi_foreground_color,		/* 34 */	/* foreground: blue    */
	sgr_set_ansi_foreground_color,		/* 35 */	/* foreground: magenta */
	sgr_set_ansi_foreground_color,		/* 36 */	/* foreground: cyan    */
	sgr_set_ansi_foreground_color,		/* 37 */	/* foreground: white   */
	sgr_illegal_code,			/* 38 */
	sgr_set_ansi_foreground_color,		/* 39 */	/* foreground: default */
	sgr_set_ansi_background_color,		/* 40 */	/* background: black   */
	sgr_set_ansi_background_color,		/* 41 */	/* background: red     */
	sgr_set_ansi_background_color,		/* 42 */	/* background: green   */
	sgr_set_ansi_background_color,		/* 43 */	/* background: yellow  */
	sgr_set_ansi_background_color,		/* 44 */	/* background: blue    */
	sgr_set_ansi_background_color,		/* 45 */	/* background: magenta */
	sgr_set_ansi_background_color,		/* 46 */	/* background: cyan    */
	sgr_set_ansi_background_color,		/* 47 */	/* background: white   */
	sgr_illegal_code,			/* 48 */
	sgr_set_ansi_background_color,		/* 49 */	/* background: default */
	sgr_illegal_code,			/* 50 */
	sgr_illegal_code,			/* 51 */
	sgr_illegal_code,			/* 52 */
	sgr_illegal_code,			/* 53 */
	sgr_illegal_code,			/* 54 */
	sgr_illegal_code,			/* 55 */
	sgr_illegal_code,			/* 56 */
	sgr_illegal_code,			/* 57 */
	sgr_illegal_code,			/* 58 */
	sgr_illegal_code,			/* 59 */
	sgr_illegal_code,			/* 60 */
	sgr_illegal_code,			/* 61 */
	sgr_illegal_code,			/* 62 */
	sgr_illegal_code,			/* 63 */
	sgr_illegal_code,			/* 64 */
	sgr_illegal_code,			/* 65 */
	sgr_illegal_code,			/* 66 */
	sgr_illegal_code,			/* 67 */
	sgr_illegal_code,			/* 68 */
	sgr_illegal_code,			/* 69 */
	sgr_illegal_code,			/* 70 */
	sgr_illegal_code,			/* 71 */
	sgr_illegal_code,			/* 72 */
	sgr_illegal_code,			/* 73 */
	sgr_illegal_code,			/* 74 */
	sgr_illegal_code,			/* 75 */
	sgr_illegal_code,			/* 76 */
	sgr_illegal_code,			/* 77 */
	sgr_illegal_code,			/* 78 */
	sgr_illegal_code,			/* 79 */
	sgr_illegal_code,			/* 80 */
	sgr_illegal_code,			/* 81 */
	sgr_illegal_code,			/* 82 */
	sgr_illegal_code,			/* 83 */
	sgr_illegal_code,			/* 84 */
	sgr_illegal_code,			/* 85 */
	sgr_illegal_code,			/* 86 */
	sgr_illegal_code,			/* 87 */
	sgr_illegal_code,			/* 88 */
	sgr_illegal_code,			/* 89 */
	sgr_set_aixterm_foreground_color,	/* 90 */	/* foreground: bright black   */
	sgr_set_aixterm_foreground_color,	/* 91 */	/* foreground: bright red     */
	sgr_set_aixterm_foreground_color,	/* 92 */	/* foreground: bright green   */
	sgr_set_aixterm_foreground_color,	/* 93 */	/* foreground: bright yellow  */
	sgr_set_aixterm_foreground_color,	/* 94 */	/* foreground: bright blue    */
	sgr_set_aixterm_foreground_color,	/* 95 */	/* foreground: bright magenta */
	sgr_set_aixterm_foreground_color,	/* 96 */	/* foreground: bright cyan    */
	sgr_set_aixterm_foreground_color,	/* 97 */	/* foreground: bright white   */
	sgr_illegal_code,			/* 98 */
	sgr_illegal_code,			/* 99 */
	sgr_set_aixterm_background_color,	/* 100 */	/* background: bright black   */
	sgr_set_aixterm_background_color,	/* 101 */	/* background: bright red     */
	sgr_set_aixterm_background_color,	/* 102 */	/* background: bright green   */
	sgr_set_aixterm_background_color,	/* 103 */	/* background: bright yellow  */
	sgr_set_aixterm_background_color,	/* 104 */	/* background: bright blue    */
	sgr_set_aixterm_background_color,	/* 105 */	/* background: bright magenta */
	sgr_set_aixterm_background_color,	/* 106 */	/* background: bright cyan    */
	sgr_set_aixterm_background_color,	/* 107 */	/* background: bright white   */
	sgr_illegal_code
};


static void * __fastcall sgr_illegal_code(struct ptyc_term_ctx * tctx)
{
	(void)tctx;
	return 0;
}


static void * __fastcall sgr_default(struct ptyc_term_ctx * tctx)
{
	tctx->background = NT_BACKGROUND_BLACK;
	tctx->foreground = NT_FOREGROUND_WHITE;

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_ansi_foreground_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 30;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->foreground = ansi_foreground_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_ansi_background_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 40;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->background = ansi_background_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_aixterm_foreground_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 90;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->foreground = aixterm_foreground_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}


static void * __fastcall sgr_set_aixterm_background_color(struct ptyc_term_ctx * tctx)
{
	int		color_idx;
	const int	param_base = 100;

	color_idx = *tctx->ctrl_mark - param_base;
	tctx->background = ansi_background_colors[color_idx];

	ntcon->set_console_text_attribute(
		tctx->hout,
		tctx->background | tctx->foreground);

	return 0;
}