Skip to content

Instantly share code, notes, and snippets.

@junegunn
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junegunn/29cb19057a57433f407d to your computer and use it in GitHub Desktop.
Save junegunn/29cb19057a57433f407d to your computer and use it in GitHub Desktop.
tmux-24.diff
diff --git a/colour.c b/colour.c
index b5efd6f..8098f83 100644
--- a/colour.c
+++ b/colour.c
@@ -29,12 +29,6 @@
* of the 256 colour palette.
*/
-/* An RGB colour. */
-struct colour_rgb {
- u_char r;
- u_char g;
- u_char b;
-};
/* 256 colour RGB table, generated on first use. */
struct colour_rgb *colour_rgb_256;
diff --git a/input.c b/input.c
index de11f62..310e65c 100644
--- a/input.c
+++ b/input.c
@@ -453,8 +453,7 @@ const struct input_transition input_state_csi_enter_table[] = {
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
{ 0x30, 0x39, input_parameter, &input_state_csi_parameter },
- { 0x3a, 0x3a, NULL, &input_state_csi_ignore },
- { 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
+ { 0x3a, 0x3b, input_parameter, &input_state_csi_parameter },
{ 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
@@ -471,8 +470,7 @@ const struct input_transition input_state_csi_parameter_table[] = {
{ 0x1c, 0x1f, input_c0_dispatch, NULL },
{ 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
{ 0x30, 0x39, input_parameter, NULL },
- { 0x3a, 0x3a, NULL, &input_state_csi_ignore },
- { 0x3b, 0x3b, input_parameter, NULL },
+ { 0x3a, 0x3b, input_parameter, NULL },
{ 0x3c, 0x3f, NULL, &input_state_csi_ignore },
{ 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
{ 0x7f, 0xff, NULL, NULL },
@@ -836,7 +834,7 @@ input_split(struct input_ctx *ictx)
return (0);
ptr = ictx->param_buf;
- while ((out = strsep(&ptr, ";")) != NULL) {
+ while ((out = strsep(&ptr, ":;")) != NULL) {
if (*out == '\0')
n = -1;
else {
@@ -1558,7 +1556,27 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
if (n == 38 || n == 48) {
i++;
- if (input_get(ictx, i, 0, -1) != 5)
+ m=input_get(ictx, i, 0, -1);
+ if (m == 2){ // 24bit?
+ u_char r, g, b;
+ r = input_get(ictx, i+1, 0, -1);
+ g = input_get(ictx, i+2, 0, -1);
+ b = input_get(ictx, i+3, 0, -1);
+ struct colour_rgb rgb = {.r=r, .g=g, .b=b};
+ if (n == 38){
+ gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags |= GRID_FLAG_FG24;
+ gc->fg_rgb = rgb;
+ } else if (n == 48){
+ gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags |= GRID_FLAG_BG24;
+ gc->bg_rgb = rgb;
+ }
+ i += 3;
+ continue;
+ }
+
+ if (m != 5)
continue;
i++;
@@ -1566,18 +1584,22 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
if (m == -1) {
if (n == 38) {
gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = 8;
} else if (n == 48) {
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = 8;
}
} else {
if (n == 38) {
gc->flags |= GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = m;
} else if (n == 48) {
gc->flags |= GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = m;
}
}
@@ -1634,10 +1656,12 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 36:
case 37:
gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = n - 30;
break;
case 39:
gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = 8;
break;
case 40:
@@ -1649,10 +1673,12 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 46:
case 47:
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = n - 40;
break;
case 49:
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = 8;
break;
case 90:
@@ -1675,6 +1701,7 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 106:
case 107:
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = n - 10;
break;
}
diff --git a/tmux.h b/tmux.h
index e296ac7..b56346c 100644
--- a/tmux.h
+++ b/tmux.h
@@ -631,10 +631,19 @@ struct utf8_data {
#define GRID_FLAG_FG256 0x1
#define GRID_FLAG_BG256 0x2
#define GRID_FLAG_PADDING 0x4
+#define GRID_FLAG_FG24 0x8
+#define GRID_FLAG_BG24 0x10
/* Grid line flags. */
#define GRID_LINE_WRAPPED 0x1
+/* An RGB colour. */
+struct colour_rgb {
+ u_char r;
+ u_char g;
+ u_char b;
+};
+
/* Grid cell data. */
struct grid_cell {
u_char attr;
@@ -644,6 +653,8 @@ struct grid_cell {
u_char xstate; /* top 4 bits width, bottom 4 bits size */
u_char xdata[UTF8_SIZE];
+ struct colour_rgb fg_rgb;
+ struct colour_rgb bg_rgb;
} __packed;
/* Grid line. */
diff --git a/tty.c b/tty.c
index 1bb8981..d95e6c1 100644
--- a/tty.c
+++ b/tty.c
@@ -35,6 +35,7 @@ void tty_read_callback(struct bufferevent *, void *);
void tty_error_callback(struct bufferevent *, short, void *);
int tty_try_256(struct tty *, u_char, const char *);
+int tty_try_24(struct tty *, struct colour_rgb, const char *);
void tty_colours(struct tty *, const struct grid_cell *);
void tty_check_fg(struct tty *, struct grid_cell *);
@@ -1381,14 +1382,23 @@ tty_attributes(struct tty *tty, const struct grid_cell *gc)
void
tty_colours(struct tty *tty, const struct grid_cell *gc)
-{
+{
struct grid_cell *tc = &tty->cell;
u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
int have_ax, fg_default, bg_default;
/* No changes? Nothing is necessary. */
if (fg == tc->fg && bg == tc->bg &&
- ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
+ tc->fg_rgb.r == gc->fg_rgb.r &&
+ tc->fg_rgb.g == gc->fg_rgb.g &&
+ tc->fg_rgb.b == gc->fg_rgb.b &&
+
+ tc->bg_rgb.r == gc->bg_rgb.r &&
+ tc->bg_rgb.g == gc->bg_rgb.g &&
+ tc->bg_rgb.b == gc->bg_rgb.b &&
+ ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256|GRID_FLAG_FG24|GRID_FLAG_BG24)) == 0
+
+ )
return;
/*
@@ -1397,8 +1407,8 @@ tty_colours(struct tty *tty, const struct grid_cell *gc)
* case if only one is default need to fall onward to set the other
* colour.
*/
- fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
- bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
+ fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256) && !(flags & GRID_FLAG_FG24));
+ bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256) && !(flags & GRID_FLAG_BG24));
if (fg_default || bg_default) {
/*
* If don't have AX but do have op, send sgr0 (op can't
@@ -1412,39 +1422,49 @@ tty_colours(struct tty *tty, const struct grid_cell *gc)
tty_reset(tty);
else {
if (fg_default &&
- (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
+ (tc->fg != 8 || tc->flags & GRID_FLAG_FG256 || tc->flags & GRID_FLAG_FG24)) {
if (have_ax)
tty_puts(tty, "\033[39m");
else if (tc->fg != 7 ||
- tc->flags & GRID_FLAG_FG256)
+ tc->flags & GRID_FLAG_FG256 ||
+ tc->flags & GRID_FLAG_FG24)
tty_putcode1(tty, TTYC_SETAF, 7);
tc->fg = 8;
tc->flags &= ~GRID_FLAG_FG256;
+ tc->flags &= ~GRID_FLAG_FG24;
}
if (bg_default &&
- (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
+ (tc->bg != 8 || tc->flags & GRID_FLAG_BG256 || tc->flags & GRID_FLAG_BG24)) {
if (have_ax)
tty_puts(tty, "\033[49m");
else if (tc->bg != 0 ||
- tc->flags & GRID_FLAG_BG256)
+ tc->flags & GRID_FLAG_BG256 ||
+ tc->flags & GRID_FLAG_BG24)
tty_putcode1(tty, TTYC_SETAB, 0);
tc->bg = 8;
tc->flags &= ~GRID_FLAG_BG256;
+ tc->flags &= ~GRID_FLAG_BG24;
}
}
}
/* Set the foreground colour. */
- if (!fg_default && (fg != tc->fg ||
- ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
+ if (!fg_default && (fg != tc->fg || ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) ||
+ (
+ ( tc->fg_rgb.r!=gc->fg_rgb.r || tc->fg_rgb.g!=gc->fg_rgb.g || tc->fg_rgb.b!=gc->fg_rgb.b ) ||
+ ((flags & GRID_FLAG_FG24) != (tc->flags & GRID_FLAG_FG24))
+ )))
tty_colours_fg(tty, gc);
/*
* Set the background colour. This must come after the foreground as
* tty_colour_fg() can call tty_reset().
*/
- if (!bg_default && (bg != tc->bg ||
- ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
+ if (!bg_default && (bg != tc->bg || ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) ||
+ (
+ ( tc->bg_rgb.r!=gc->bg_rgb.r || tc->bg_rgb.g!=gc->bg_rgb.g || tc->bg_rgb.b!=gc->bg_rgb.b ) ||
+ ((flags & GRID_FLAG_BG24) != (tc->flags & GRID_FLAG_BG24))
+ )))
tty_colours_bg(tty, gc);
}
@@ -1454,7 +1474,7 @@ tty_check_fg(struct tty *tty, struct grid_cell *gc)
u_int colours;
/* Is this a 256-colour colour? */
- if (gc->flags & GRID_FLAG_FG256) {
+ if (gc->flags & GRID_FLAG_FG256 && !(gc->flags & GRID_FLAG_BG24)) {
/* And not a 256 colour mode? */
if (!(tty->term->flags & TERM_256COLOURS) &&
!(tty->term_flags & TERM_256COLOURS)) {
@@ -1483,7 +1503,7 @@ tty_check_bg(struct tty *tty, struct grid_cell *gc)
u_int colours;
/* Is this a 256-colour colour? */
- if (gc->flags & GRID_FLAG_BG256) {
+ if (gc->flags & GRID_FLAG_BG256 && !(gc->flags & GRID_FLAG_BG24)) {
/*
* And not a 256 colour mode? Translate to 16-colour
* palette. Bold background doesn't exist portably, so just
@@ -1512,15 +1532,29 @@ void
tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
{
struct grid_cell *tc = &tty->cell;
+ struct colour_rgb rgb= gc->fg_rgb;
u_char fg = gc->fg;
char s[32];
+ tc->flags &= ~GRID_FLAG_FG256;
+ tc->flags &= ~GRID_FLAG_FG24;
+
+ /* Is this a 24-colour colour? */
+ if (gc->flags & GRID_FLAG_FG24) {
+//log_debug("trying to output 24bit fg");
+ if (tty_try_24(tty, rgb, "38") == 0){
+ tc->fg_rgb = rgb;
+ tc->flags |= gc->flags & GRID_FLAG_FG24;
+ }
+ return;
+ }
+
/* Is this a 256-colour colour? */
if (gc->flags & GRID_FLAG_FG256) {
- /* Try as 256 colours. */
- if (tty_try_256(tty, fg, "38") == 0)
- goto save_fg;
- /* Else already handled by tty_check_fg. */
+ if (tty_try_256(tty, fg, "38") == 0){
+ tc->fg = fg;
+ tc->flags |= gc->flags & GRID_FLAG_FG256;
+ }
return;
}
@@ -1528,32 +1562,41 @@ tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
if (fg >= 90 && fg <= 97) {
xsnprintf(s, sizeof s, "\033[%dm", fg);
tty_puts(tty, s);
- goto save_fg;
+ tc->fg = fg;
+ return;
}
/* Otherwise set the foreground colour. */
tty_putcode1(tty, TTYC_SETAF, fg);
-
-save_fg:
- /* Save the new values in the terminal current cell. */
tc->fg = fg;
- tc->flags &= ~GRID_FLAG_FG256;
- tc->flags |= gc->flags & GRID_FLAG_FG256;
}
void
tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
{
struct grid_cell *tc = &tty->cell;
+ struct colour_rgb rgb= gc->bg_rgb;
u_char bg = gc->bg;
char s[32];
+ tc->flags &= ~GRID_FLAG_BG256;
+ tc->flags &= ~GRID_FLAG_BG24;
+
+ /* Is this a 24-colour colour? */
+ if (gc->flags & GRID_FLAG_BG24) {
+ if (tty_try_24(tty, rgb, "48") == 0){
+ tc->bg_rgb = rgb;
+ tc->flags |= gc->flags & GRID_FLAG_BG24;
+ }
+ return;
+ }
+
/* Is this a 256-colour colour? */
if (gc->flags & GRID_FLAG_BG256) {
- /* Try as 256 colours. */
- if (tty_try_256(tty, bg, "48") == 0)
- goto save_bg;
- /* Else already handled by tty_check_bg. */
+ if (tty_try_256(tty, bg, "48") == 0){
+ tc->bg = bg;
+ tc->flags |= gc->flags & GRID_FLAG_BG256;
+ }
return;
}
@@ -1563,20 +1606,16 @@ tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
tty_puts(tty, s);
- goto save_bg;
+ tc->bg = bg;
}
bg -= 90;
+ return;
/* no such thing as a bold background */
}
/* Otherwise set the background colour. */
tty_putcode1(tty, TTYC_SETAB, bg);
-
-save_bg:
- /* Save the new values in the terminal current cell. */
tc->bg = bg;
- tc->flags &= ~GRID_FLAG_BG256;
- tc->flags |= gc->flags & GRID_FLAG_BG256;
}
int
@@ -1609,6 +1648,23 @@ tty_try_256(struct tty *tty, u_char colour, const char *type)
return (-1);
}
+
+int
+tty_try_24(struct tty *tty, struct colour_rgb rgb, const char *type)
+{
+ char s[32];
+
+ //if (!(tty->term->flags & TERM_256COLOURS) &&
+ // !(tty->term_flags & TERM_256COLOURS))
+ // return (-1);
+
+ //xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
+ xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type, rgb.r, rgb.g, rgb.b);
+//log_debug("24bit output: %s",s);
+ tty_puts(tty, s);
+ return (0);
+}
+
void
tty_bell(struct tty *tty)
{
@junegunn
Copy link
Author

junegunn commented Aug 1, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment