From a6129e99749d2bbc8b4a991c7b5d09300aa55f39 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 28 Jan 2020 11:39:51 +0000 Subject: [PATCH] If we can identify the terminal as iTerm2 or as tmux, we can be sure they support 256 and RGB colours, so set those flags too. --- tmux.h | 1 + tty-keys.c | 4 +++- tty-term.c | 28 +++++++++------------------- tty.c | 27 +++++++++++++++------------ 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/tmux.h b/tmux.h index 24e4277f..3ec89019 100644 --- a/tmux.h +++ b/tmux.h @@ -1171,6 +1171,7 @@ struct tty_term { #define TERM_NOXENL 0x2 #define TERM_DECSLRM 0x4 #define TERM_DECFRA 0x8 +#define TERM_RGBCOLOURS 0x10 int flags; LIST_ENTRY(tty_term) entry; diff --git a/tty-keys.c b/tty-keys.c index 987fd1b0..064f2172 100644 --- a/tty-keys.c +++ b/tty-keys.c @@ -1117,7 +1117,9 @@ tty_keys_device_status_report(struct tty *tty, const char *buf, size_t len, /* Set terminal flags. */ if (strncmp(tmp, "ITERM2 ", 7) == 0) - flags |= TERM_DECSLRM; + flags |= (TERM_DECSLRM|TERM_256COLOURS|TERM_RGBCOLOURS); + if (strncmp(tmp, "TMUX ", 5) == 0) + flags |= (TERM_256COLOURS|TERM_RGBCOLOURS); log_debug("%s: received DSR %.*s", c->name, (int)*size, buf); tty_set_flags(tty, flags); diff --git a/tty-term.c b/tty-term.c index 164ab2f1..ef7d7905 100644 --- a/tty-term.c +++ b/tty-term.c @@ -522,11 +522,16 @@ tty_term_find(char *name, int fd, char **cause) goto error; } - /* Figure out if we have 256 colours (or more). */ - if (tty_term_number(term, TTYC_COLORS) >= 256 || - tty_term_has(term, TTYC_RGB)) + /* Set flag if terminal has 256 colours. */ + if (tty_term_number(term, TTYC_COLORS) >= 256) term->flags |= TERM_256COLOURS; + /* Set flag if terminal has RGB colours. */ + if ((tty_term_flag(term, TTYC_TC) || tty_term_has(term, TTYC_RGB)) || + (tty_term_has(term, TTYC_SETRGBF) && + tty_term_has(term, TTYC_SETRGBB))) + term->flags |= TERM_RGBCOLOURS; + /* * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1). @@ -561,22 +566,7 @@ tty_term_find(char *name, int fd, char **cause) code->type = TTYCODE_STRING; } - /* - * On terminals with RGB colour (Tc or RGB), fill in setrgbf and - * setrgbb if they are missing. - */ - if ((tty_term_flag(term, TTYC_TC) || tty_term_flag(term, TTYC_RGB)) && - !tty_term_has(term, TTYC_SETRGBF) && - !tty_term_has(term, TTYC_SETRGBB)) { - code = &term->codes[TTYC_SETRGBF]; - code->value.string = xstrdup("\033[38;2;%p1%d;%p2%d;%p3%dm"); - code->type = TTYCODE_STRING; - code = &term->codes[TTYC_SETRGBB]; - code->value.string = xstrdup("\033[48;2;%p1%d;%p2%d;%p3%dm"); - code->type = TTYCODE_STRING; - } - - /* Log it. */ + /* Log the capabilities. */ for (i = 0; i < tty_term_ncodes(); i++) log_debug("%s%s", name, tty_term_describe(term, i)); diff --git a/tty.c b/tty.c index 3d58f05b..54c3be30 100644 --- a/tty.c +++ b/tty.c @@ -2386,11 +2386,10 @@ tty_check_fg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc) /* Is this a 24-bit colour? */ if (gc->fg & COLOUR_FLAG_RGB) { /* Not a 24-bit terminal? Translate to 256-colour palette. */ - if (!tty_term_has(tty->term, TTYC_SETRGBF)) { - colour_split_rgb(gc->fg, &r, &g, &b); - gc->fg = colour_find_rgb(r, g, b); - } else + if ((tty->term->flags|tty->term_flags) & TERM_RGBCOLOURS) return; + colour_split_rgb(gc->fg, &r, &g, &b); + gc->fg = colour_find_rgb(r, g, b); } /* How many colours does this terminal have? */ @@ -2436,11 +2435,10 @@ tty_check_bg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc) /* Is this a 24-bit colour? */ if (gc->bg & COLOUR_FLAG_RGB) { /* Not a 24-bit terminal? Translate to 256-colour palette. */ - if (!tty_term_has(tty->term, TTYC_SETRGBB)) { - colour_split_rgb(gc->bg, &r, &g, &b); - gc->bg = colour_find_rgb(r, g, b); - } else + if ((tty->term->flags|tty->term_flags) & TERM_RGBCOLOURS) return; + colour_split_rgb(gc->bg, &r, &g, &b); + gc->bg = colour_find_rgb(r, g, b); } /* How many colours does this terminal have? */ @@ -2617,15 +2615,14 @@ tty_try_colour(struct tty *tty, int colour, const char *type) } if (colour & COLOUR_FLAG_RGB) { + colour_split_rgb(colour & 0xffffff, &r, &g, &b); if (*type == '3') { if (!tty_term_has(tty->term, TTYC_SETRGBF)) - return (-1); - colour_split_rgb(colour & 0xffffff, &r, &g, &b); + goto fallback_rgb; tty_putcode3(tty, TTYC_SETRGBF, r, g, b); } else { if (!tty_term_has(tty->term, TTYC_SETRGBB)) - return (-1); - colour_split_rgb(colour & 0xffffff, &r, &g, &b); + goto fallback_rgb; tty_putcode3(tty, TTYC_SETRGBB, r, g, b); } return (0); @@ -2638,6 +2635,12 @@ fallback_256: log_debug("%s: 256 colour fallback: %s", tty->client->name, s); tty_puts(tty, s); return (0); + +fallback_rgb: + xsnprintf(s, sizeof s, "\033[%s;2;%d;%d;%dm", type, r, g, b); + log_debug("%s: RGB colour fallback: %s", tty->client->name, s); + tty_puts(tty, s); + return (0); } static void