From dcab540839f46aa3dbe919929e4b999c713bee5d Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 24 Sep 2018 01:25:30 -0700 Subject: [PATCH] GUACAMOLE-630: Persist semantics of default foreground/background with dedicated palette pseudo-indexes. --- src/terminal/color-scheme.c | 5 +++++ src/terminal/display.c | 33 +++++++++++++++++++++++++++------ src/terminal/palette.c | 4 ++++ src/terminal/terminal/palette.h | 14 ++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/terminal/color-scheme.c b/src/terminal/color-scheme.c index 2799aac9..f4300196 100644 --- a/src/terminal/color-scheme.c +++ b/src/terminal/color-scheme.c @@ -247,5 +247,10 @@ void guac_terminal_parse_color_scheme(guac_client* client, (const guac_terminal_color(*)[256]) palette, color_target)) return; /* Parsing failed. */ } + + /* Persist pseudo-index for foreground/background colors */ + foreground->palette_index = GUAC_TERMINAL_COLOR_FOREGROUND; + background->palette_index = GUAC_TERMINAL_COLOR_BACKGROUND; + } diff --git a/src/terminal/display.c b/src/terminal/display.c index 480e8398..1c803b41 100644 --- a/src/terminal/display.c +++ b/src/terminal/display.c @@ -78,7 +78,12 @@ int __guac_terminal_set_colors(guac_terminal_display* display, } display->glyph_foreground = *foreground; + guac_terminal_display_lookup_color(display, + foreground->palette_index, &display->glyph_foreground); + display->glyph_background = *background; + guac_terminal_display_lookup_color(display, + background->palette_index, &display->glyph_background); /* Modify color if half-bright (low intensity) */ if (attributes->half_bright && !attributes->bold) { @@ -315,6 +320,18 @@ int guac_terminal_display_assign_color(guac_terminal_display* display, int guac_terminal_display_lookup_color(guac_terminal_display* display, int index, guac_terminal_color* color) { + /* Use default foreground if foreground pseudo-index is given */ + if (index == GUAC_TERMINAL_COLOR_FOREGROUND) { + *color = display->default_foreground; + return 0; + } + + /* Use default background if background pseudo-index is given */ + if (index == GUAC_TERMINAL_COLOR_BACKGROUND) { + *color = display->default_background; + return 0; + } + /* Lookup fails if out-of-bounds */ if (index < 0 || index > 255) return 1; @@ -658,11 +675,15 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) { int rect_width, rect_height; /* Color of the rectangle to draw */ - const guac_terminal_color* color; + guac_terminal_color color; if (current->character.attributes.reverse != current->character.attributes.cursor) - color = ¤t->character.attributes.foreground; + color = current->character.attributes.foreground; else - color = ¤t->character.attributes.background; + color = current->character.attributes.background; + + /* Rely only on palette index if defined */ + guac_terminal_display_lookup_color(display, + color.palette_index, &color); /* Current row within a subrect */ guac_terminal_operation* rect_current_row; @@ -685,7 +706,7 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) { /* If not identical operation, stop */ if (rect_current->type != GUAC_CHAR_SET || guac_terminal_has_glyph(rect_current->character.value) - || guac_terminal_colorcmp(joining_color, color) != 0) + || guac_terminal_colorcmp(joining_color, &color) != 0) break; /* Next column */ @@ -730,7 +751,7 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) { /* Mark clear operations as NOP */ if (rect_current->type == GUAC_CHAR_SET && !guac_terminal_has_glyph(rect_current->character.value) - && guac_terminal_colorcmp(joining_color, color) == 0) + && guac_terminal_colorcmp(joining_color, &color) == 0) rect_current->type = GUAC_CHAR_NOP; /* Next column */ @@ -750,7 +771,7 @@ void __guac_terminal_display_flush_clear(guac_terminal_display* display) { row * display->char_height, rect_width * display->char_width, rect_height * display->char_height, - color->red, color->green, color->blue, + color.red, color.green, color.blue, 0xFF); } /* end if clear operation */ diff --git a/src/terminal/palette.c b/src/terminal/palette.c index cdbe4156..a6b2d5ae 100644 --- a/src/terminal/palette.c +++ b/src/terminal/palette.c @@ -289,6 +289,10 @@ const guac_terminal_color GUAC_TERMINAL_INITIAL_PALETTE[256] = { int guac_terminal_colorcmp(const guac_terminal_color* a, const guac_terminal_color* b) { + /* Compare palette index alone if not unknown */ + if (a->palette_index != -1 && b->palette_index != -1) + return a->palette_index - b->palette_index; + /* Consider red component highest order ... */ if (a->red != b->red) return a->red - b->red; diff --git a/src/terminal/terminal/palette.h b/src/terminal/terminal/palette.h index 7f467258..b1524b5a 100644 --- a/src/terminal/terminal/palette.h +++ b/src/terminal/terminal/palette.h @@ -24,6 +24,20 @@ #include +/** + * The pseudo-index of the color set as the the default foreground color for + * the terminal. Regardless of what changes are made to the palette, this index + * will always return the current default foreground color. + */ +#define GUAC_TERMINAL_COLOR_FOREGROUND -2 + +/** + * The pseudo-index of the color set as the the default background color for + * the terminal. Regardless of what changes are made to the palette, this index + * will always return the current default background color. + */ +#define GUAC_TERMINAL_COLOR_BACKGROUND -3 + /** * The index of black within the terminal color palette. */