From e4c2ac9de9b7ac8a4885361588566219ffe6e587 Mon Sep 17 00:00:00 2001 From: midipix Date: Mon, 11 Jul 2016 05:10:11 -0400 Subject: bridge: added initial sgr logic. --- project/common.mk | 1 + src/bridge/ptyc_term_csi.c | 3 +- src/bridge/ptyc_term_sgr.c | 268 ++++++++++++++++++++++++++++++++++++++ src/internal/ptycon_bridge_impl.h | 2 + 4 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 src/bridge/ptyc_term_sgr.c diff --git a/project/common.mk b/project/common.mk index cf47564..bbd9cf6 100644 --- a/project/common.mk +++ b/project/common.mk @@ -1,6 +1,7 @@ COMMON_SRCS = \ src/bridge/ptyc_term_csi.c \ src/bridge/ptyc_term_esi.c \ + src/bridge/ptyc_term_sgr.c \ src/console/ptyc_console_alloc.c \ src/console/ptyc_console_ctrl.c \ src/console/ptyc_console_poller.c \ diff --git a/src/bridge/ptyc_term_csi.c b/src/bridge/ptyc_term_csi.c index c856c35..fe302bd 100644 --- a/src/bridge/ptyc_term_csi.c +++ b/src/bridge/ptyc_term_csi.c @@ -200,8 +200,7 @@ static void * __fastcall csi_action_invoke( static void * __fastcall csi_action_select_sgr(struct ptyc_term_ctx * tctx) { - /* todo: pass ptyc_sgr_handlers */ - return csi_action_invoke(tctx,0,PTYC_SGR_ARRAY_SIZE); + return csi_action_invoke(tctx,ptyc_sgr_handlers,PTYC_SGR_ARRAY_SIZE); } static void * __fastcall csi_action_process_params(struct ptyc_term_ctx * tctx) diff --git a/src/bridge/ptyc_term_sgr.c b/src/bridge/ptyc_term_sgr.c new file mode 100644 index 0000000..3151718 --- /dev/null +++ b/src/bridge/ptyc_term_sgr.c @@ -0,0 +1,268 @@ +/*********************************************************/ +/* ptycon: a pty-console bridge */ +/* Copyright (C) 2016 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.PTYCON. */ +/*********************************************************/ + +#include +#include + +#include +#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; +} diff --git a/src/internal/ptycon_bridge_impl.h b/src/internal/ptycon_bridge_impl.h index b70497e..a88b774 100644 --- a/src/internal/ptycon_bridge_impl.h +++ b/src/internal/ptycon_bridge_impl.h @@ -44,6 +44,8 @@ struct ptyc_term_ctx { ptyc_term_handler * ctrl_handler; ptyc_term_handler * const * ctrl_handlers[PTYC_CTRL_STATE_CAP]; ptyc_term_handler * ctrl_command; + uint16_t foreground; + uint16_t background; struct ptyc_term_data data; }; -- cgit v1.2.3