Blame src/bridge/ptyc_term_sgr.c

e4c2ac
/*********************************************************/
e4c2ac
/*  ptycon: a pty-console bridge                         */
1f983d
/*  Copyright (C) 2016--2017  Z. Gilboa                  */
e4c2ac
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
e4c2ac
/*********************************************************/
e4c2ac
e4c2ac
#include <psxtypes/psxtypes.h>
e4c2ac
#include <ntcon/ntcon.h>
e4c2ac
e4c2ac
#include <ptycon/ptycon.h>
e4c2ac
#include "ptycon_bridge_impl.h"
e4c2ac
#include "ptycon_driver_impl.h"
e4c2ac
e4c2ac
/* forward declarations: sgr control handlers */
e4c2ac
static ptyc_term_handler	sgr_illegal_code;
e4c2ac
static ptyc_term_handler	sgr_default;
e4c2ac
static ptyc_term_handler	sgr_set_ansi_foreground_color;
e4c2ac
static ptyc_term_handler	sgr_set_ansi_background_color;
e4c2ac
static ptyc_term_handler	sgr_set_aixterm_foreground_color;
e4c2ac
static ptyc_term_handler	sgr_set_aixterm_background_color;
e4c2ac
e4c2ac
static const uint16_t ansi_foreground_colors[10]  = {
e4c2ac
	NT_FOREGROUND_BLACK,
e4c2ac
	NT_FOREGROUND_RED,
e4c2ac
	NT_FOREGROUND_GREEN,
e4c2ac
	NT_FOREGROUND_YELLOW,
e4c2ac
	NT_FOREGROUND_BLUE,
e4c2ac
	NT_FOREGROUND_MAGENTA,
e4c2ac
	NT_FOREGROUND_CYAN,
e4c2ac
	NT_FOREGROUND_WHITE,
e4c2ac
	0,
e4c2ac
	NT_FOREGROUND_WHITE	/* default */
e4c2ac
};
e4c2ac
e4c2ac
e4c2ac
static const uint16_t ansi_background_colors[10]  = {
e4c2ac
	NT_BACKGROUND_BLACK,
e4c2ac
	NT_BACKGROUND_RED,
e4c2ac
	NT_BACKGROUND_GREEN,
e4c2ac
	NT_BACKGROUND_YELLOW,
e4c2ac
	NT_BACKGROUND_BLUE,
e4c2ac
	NT_BACKGROUND_MAGENTA,
e4c2ac
	NT_BACKGROUND_CYAN,
e4c2ac
	NT_BACKGROUND_WHITE,
e4c2ac
	0,
e4c2ac
	NT_BACKGROUND_BLACK	/* default */
e4c2ac
};
e4c2ac
e4c2ac
e4c2ac
static const uint16_t aixterm_foreground_colors[8]  = {
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_BLACK,
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_RED,
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_GREEN,
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_YELLOW,
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_BLUE,
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_MAGENTA,
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_CYAN,
e4c2ac
	NT_FOREGROUND_INTENSITY | NT_FOREGROUND_WHITE
e4c2ac
};
e4c2ac
e4c2ac
e4c2ac
static const uint16_t aixterm_background_colors[8]  = {
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_BLACK,
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_RED,
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_GREEN,
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_YELLOW,
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_BLUE,
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_MAGENTA,
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_CYAN,
e4c2ac
	NT_BACKGROUND_INTENSITY | NT_BACKGROUND_WHITE
e4c2ac
};
e4c2ac
e4c2ac
e4c2ac
ptyc_term_handler * const ptyc_sgr_handlers[PTYC_SGR_ARRAY_SIZE] = {
e4c2ac
	sgr_default,				/* 0 */		/* default */
e4c2ac
	sgr_illegal_code,			/* 1 */		/* bold    */
e4c2ac
	sgr_illegal_code,			/* 2 */
e4c2ac
	sgr_illegal_code,			/* 3 */
e4c2ac
	sgr_illegal_code,			/* 4 */		/* underlined */
e4c2ac
	sgr_illegal_code,			/* 5 */		/* blink      */
e4c2ac
	sgr_illegal_code,			/* 6 */
e4c2ac
	sgr_illegal_code,			/* 7 */		/* inverse   */
e4c2ac
	sgr_illegal_code,			/* 8 */		/* invisible */
e4c2ac
	sgr_illegal_code,			/* 9 */
e4c2ac
	sgr_illegal_code,			/* 10 */
e4c2ac
	sgr_illegal_code,			/* 11 */
e4c2ac
	sgr_illegal_code,			/* 12 */
e4c2ac
	sgr_illegal_code,			/* 13 */
e4c2ac
	sgr_illegal_code,			/* 14 */
e4c2ac
	sgr_illegal_code,			/* 15 */
e4c2ac
	sgr_illegal_code,			/* 16 */
e4c2ac
	sgr_illegal_code,			/* 17 */
e4c2ac
	sgr_illegal_code,			/* 18 */
e4c2ac
	sgr_illegal_code,			/* 19 */
e4c2ac
	sgr_illegal_code,			/* 20 */
e4c2ac
	sgr_illegal_code,			/* 21 */
e4c2ac
	sgr_illegal_code,			/* 22 */	/* normal */
e4c2ac
	sgr_illegal_code,			/* 23 */
e4c2ac
	sgr_illegal_code,			/* 24 */	/* not underlined */
e4c2ac
	sgr_illegal_code,			/* 25 */	/* steady         */
e4c2ac
	sgr_illegal_code,			/* 26 */
e4c2ac
	sgr_illegal_code,			/* 27 */	/* positive */
e4c2ac
	sgr_illegal_code,			/* 28 */	/* visible  */
e4c2ac
	sgr_illegal_code,			/* 29 */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 30 */	/* foreground: black   */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 31 */	/* foreground: red     */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 32 */	/* foreground: green   */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 33 */	/* foreground: yellow  */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 34 */	/* foreground: blue    */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 35 */	/* foreground: magenta */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 36 */	/* foreground: cyan    */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 37 */	/* foreground: white   */
e4c2ac
	sgr_illegal_code,			/* 38 */
e4c2ac
	sgr_set_ansi_foreground_color,		/* 39 */	/* foreground: default */
e4c2ac
	sgr_set_ansi_background_color,		/* 40 */	/* background: black   */
e4c2ac
	sgr_set_ansi_background_color,		/* 41 */	/* background: red     */
e4c2ac
	sgr_set_ansi_background_color,		/* 42 */	/* background: green   */
e4c2ac
	sgr_set_ansi_background_color,		/* 43 */	/* background: yellow  */
e4c2ac
	sgr_set_ansi_background_color,		/* 44 */	/* background: blue    */
e4c2ac
	sgr_set_ansi_background_color,		/* 45 */	/* background: magenta */
e4c2ac
	sgr_set_ansi_background_color,		/* 46 */	/* background: cyan    */
e4c2ac
	sgr_set_ansi_background_color,		/* 47 */	/* background: white   */
e4c2ac
	sgr_illegal_code,			/* 48 */
e4c2ac
	sgr_set_ansi_background_color,		/* 49 */	/* background: default */
e4c2ac
	sgr_illegal_code,			/* 50 */
e4c2ac
	sgr_illegal_code,			/* 51 */
e4c2ac
	sgr_illegal_code,			/* 52 */
e4c2ac
	sgr_illegal_code,			/* 53 */
e4c2ac
	sgr_illegal_code,			/* 54 */
e4c2ac
	sgr_illegal_code,			/* 55 */
e4c2ac
	sgr_illegal_code,			/* 56 */
e4c2ac
	sgr_illegal_code,			/* 57 */
e4c2ac
	sgr_illegal_code,			/* 58 */
e4c2ac
	sgr_illegal_code,			/* 59 */
e4c2ac
	sgr_illegal_code,			/* 60 */
e4c2ac
	sgr_illegal_code,			/* 61 */
e4c2ac
	sgr_illegal_code,			/* 62 */
e4c2ac
	sgr_illegal_code,			/* 63 */
e4c2ac
	sgr_illegal_code,			/* 64 */
e4c2ac
	sgr_illegal_code,			/* 65 */
e4c2ac
	sgr_illegal_code,			/* 66 */
e4c2ac
	sgr_illegal_code,			/* 67 */
e4c2ac
	sgr_illegal_code,			/* 68 */
e4c2ac
	sgr_illegal_code,			/* 69 */
e4c2ac
	sgr_illegal_code,			/* 70 */
e4c2ac
	sgr_illegal_code,			/* 71 */
e4c2ac
	sgr_illegal_code,			/* 72 */
e4c2ac
	sgr_illegal_code,			/* 73 */
e4c2ac
	sgr_illegal_code,			/* 74 */
e4c2ac
	sgr_illegal_code,			/* 75 */
e4c2ac
	sgr_illegal_code,			/* 76 */
e4c2ac
	sgr_illegal_code,			/* 77 */
e4c2ac
	sgr_illegal_code,			/* 78 */
e4c2ac
	sgr_illegal_code,			/* 79 */
e4c2ac
	sgr_illegal_code,			/* 80 */
e4c2ac
	sgr_illegal_code,			/* 81 */
e4c2ac
	sgr_illegal_code,			/* 82 */
e4c2ac
	sgr_illegal_code,			/* 83 */
e4c2ac
	sgr_illegal_code,			/* 84 */
e4c2ac
	sgr_illegal_code,			/* 85 */
e4c2ac
	sgr_illegal_code,			/* 86 */
e4c2ac
	sgr_illegal_code,			/* 87 */
e4c2ac
	sgr_illegal_code,			/* 88 */
e4c2ac
	sgr_illegal_code,			/* 89 */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 90 */	/* foreground: bright black   */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 91 */	/* foreground: bright red     */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 92 */	/* foreground: bright green   */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 93 */	/* foreground: bright yellow  */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 94 */	/* foreground: bright blue    */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 95 */	/* foreground: bright magenta */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 96 */	/* foreground: bright cyan    */
e4c2ac
	sgr_set_aixterm_foreground_color,	/* 97 */	/* foreground: bright white   */
e4c2ac
	sgr_illegal_code,			/* 98 */
e4c2ac
	sgr_illegal_code,			/* 99 */
e4c2ac
	sgr_set_aixterm_background_color,	/* 100 */	/* background: bright black   */
e4c2ac
	sgr_set_aixterm_background_color,	/* 101 */	/* background: bright red     */
e4c2ac
	sgr_set_aixterm_background_color,	/* 102 */	/* background: bright green   */
e4c2ac
	sgr_set_aixterm_background_color,	/* 103 */	/* background: bright yellow  */
e4c2ac
	sgr_set_aixterm_background_color,	/* 104 */	/* background: bright blue    */
e4c2ac
	sgr_set_aixterm_background_color,	/* 105 */	/* background: bright magenta */
e4c2ac
	sgr_set_aixterm_background_color,	/* 106 */	/* background: bright cyan    */
e4c2ac
	sgr_set_aixterm_background_color,	/* 107 */	/* background: bright white   */
e4c2ac
	sgr_illegal_code
e4c2ac
};
e4c2ac
e4c2ac
e4c2ac
static void * __fastcall sgr_illegal_code(struct ptyc_term_ctx * tctx)
e4c2ac
{
e4c2ac
	(void)tctx;
e4c2ac
	return 0;
e4c2ac
}
e4c2ac
e4c2ac
e4c2ac
static void * __fastcall sgr_default(struct ptyc_term_ctx * tctx)
e4c2ac
{
e4c2ac
	tctx->background = NT_BACKGROUND_BLACK;
e4c2ac
	tctx->foreground = NT_FOREGROUND_WHITE;
e4c2ac
e4c2ac
	ntcon->set_console_text_attribute(
e4c2ac
		tctx->hout,
e4c2ac
		tctx->background | tctx->foreground);
e4c2ac
e4c2ac
	return 0;
e4c2ac
}
e4c2ac
e4c2ac
e4c2ac
static void * __fastcall sgr_set_ansi_foreground_color(struct ptyc_term_ctx * tctx)
e4c2ac
{
e4c2ac
	int		color_idx;
e4c2ac
	const int	param_base = 30;
e4c2ac
e4c2ac
	color_idx = *tctx->ctrl_mark - param_base;
e4c2ac
	tctx->foreground = ansi_foreground_colors[color_idx];
e4c2ac
e4c2ac
	ntcon->set_console_text_attribute(
e4c2ac
		tctx->hout,
e4c2ac
		tctx->background | tctx->foreground);
e4c2ac
e4c2ac
	return 0;
e4c2ac
}
e4c2ac
e4c2ac
e4c2ac
static void * __fastcall sgr_set_ansi_background_color(struct ptyc_term_ctx * tctx)
e4c2ac
{
e4c2ac
	int		color_idx;
e4c2ac
	const int	param_base = 40;
e4c2ac
e4c2ac
	color_idx = *tctx->ctrl_mark - param_base;
e4c2ac
	tctx->background = ansi_background_colors[color_idx];
e4c2ac
e4c2ac
	ntcon->set_console_text_attribute(
e4c2ac
		tctx->hout,
e4c2ac
		tctx->background | tctx->foreground);
e4c2ac
e4c2ac
	return 0;
e4c2ac
}
e4c2ac
e4c2ac
e4c2ac
static void * __fastcall sgr_set_aixterm_foreground_color(struct ptyc_term_ctx * tctx)
e4c2ac
{
e4c2ac
	int		color_idx;
e4c2ac
	const int	param_base = 90;
e4c2ac
e4c2ac
	color_idx = *tctx->ctrl_mark - param_base;
e4c2ac
	tctx->foreground = aixterm_foreground_colors[color_idx];
e4c2ac
e4c2ac
	ntcon->set_console_text_attribute(
e4c2ac
		tctx->hout,
e4c2ac
		tctx->background | tctx->foreground);
e4c2ac
e4c2ac
	return 0;
e4c2ac
}
e4c2ac
e4c2ac
e4c2ac
static void * __fastcall sgr_set_aixterm_background_color(struct ptyc_term_ctx * tctx)
e4c2ac
{
e4c2ac
	int		color_idx;
e4c2ac
	const int	param_base = 100;
e4c2ac
e4c2ac
	color_idx = *tctx->ctrl_mark - param_base;
e4c2ac
	tctx->background = ansi_background_colors[color_idx];
e4c2ac
e4c2ac
	ntcon->set_console_text_attribute(
e4c2ac
		tctx->hout,
e4c2ac
		tctx->background | tctx->foreground);
e4c2ac
e4c2ac
	return 0;
e4c2ac
}