diff --git a/input-keys.c b/input-keys.c index 2f5cfafc..eb5dae88 100644 --- a/input-keys.c +++ b/input-keys.c @@ -313,12 +313,6 @@ static struct input_key_entry input_key_defaults[] = { { .key = KEYC_DC|KEYC_BUILD_MODIFIERS, .data = "\033[3;_~" }, - { .key = KEYC_REPORT_DARK_THEME, - .data = "\033[?997;1n" - }, - { .key = KEYC_REPORT_LIGHT_THEME, - .data = "\033[?997;2n" - }, }; static const key_code input_key_modifiers[] = { 0, diff --git a/input.c b/input.c index 1c7095a6..db798e05 100644 --- a/input.c +++ b/input.c @@ -1902,6 +1902,8 @@ input_csi_dispatch_rm_private(struct input_ctx *ictx) break; case 2031: screen_write_mode_clear(sctx, MODE_THEME_UPDATES); + if (ictx->wp != NULL) + ictx->wp->flags &= ~PANE_THEMECHANGED; break; case 2026: /* synchronized output */ screen_write_stop_sync(ictx->wp); @@ -2005,6 +2007,10 @@ input_csi_dispatch_sm_private(struct input_ctx *ictx) break; case 2031: screen_write_mode_set(sctx, MODE_THEME_UPDATES); + if (ictx->wp != NULL) { + ictx->wp->last_theme = window_pane_get_theme(ictx->wp); + ictx->wp->flags &= ~PANE_THEMECHANGED; + } break; case 2026: /* synchronized output */ screen_write_start_sync(ictx->wp); @@ -3434,9 +3440,11 @@ input_report_current_theme(struct input_ctx *ictx) { struct window_pane *wp = ictx->wp; - if (wp == NULL) - return; - switch (window_pane_get_theme(wp)) { + if (wp != NULL) { + wp->last_theme = window_pane_get_theme(wp); + wp->flags &= ~PANE_THEMECHANGED; + + switch (wp->last_theme) { case THEME_DARK: log_debug("%s: %%%u dark theme", __func__, wp->id); input_reply(ictx, 0, "\033[?997;1n"); @@ -3448,5 +3456,6 @@ input_report_current_theme(struct input_ctx *ictx) case THEME_UNKNOWN: log_debug("%s: %%%u unknown theme", __func__, wp->id); break; + } } } diff --git a/tmux.h b/tmux.h index e3447e54..bcd24f1f 100644 --- a/tmux.h +++ b/tmux.h @@ -1167,6 +1167,16 @@ struct window_pane_resize { }; TAILQ_HEAD(window_pane_resizes, window_pane_resize); +/* + * Client theme, this is worked out from the background colour if not reported + * by terminal. + */ +enum client_theme { + THEME_UNKNOWN, + THEME_LIGHT, + THEME_DARK +}; + /* Child window structure. */ struct window_pane { u_int id; @@ -1230,6 +1240,7 @@ struct window_pane { struct grid_cell cached_gc; struct grid_cell cached_active_gc; struct colour_palette palette; + enum client_theme last_theme; int pipe_fd; struct bufferevent *pipe_event; @@ -1911,16 +1922,6 @@ struct overlay_ranges { u_int nx[OVERLAY_MAX_RANGES]; }; -/* - * Client theme, this is worked out from the background colour if not reported - * by terminal. - */ -enum client_theme { - THEME_UNKNOWN, - THEME_LIGHT, - THEME_DARK -}; - /* Client connection. */ typedef int (*prompt_input_cb)(struct client *, void *, const char *, int); typedef void (*prompt_free_cb)(void *); diff --git a/tty-keys.c b/tty-keys.c index c59e6a68..2fa05146 100644 --- a/tty-keys.c +++ b/tty-keys.c @@ -1750,7 +1750,9 @@ tty_keys_palette(struct tty *tty, const char *buf, size_t len, size_t *size) /* Copy the rest up to \033\ or \007. */ start = (endptr - buf) + 1; - for (i = start; i < len && i - start < sizeof tmp; i++) { + for (i = start; i - start < sizeof tmp; i++) { + if (i == len) + return (1); if (buf[i - 1] == '\033' && buf[i] == '\\') break; if (buf[i] == '\007') diff --git a/window.c b/window.c index 2d3ef383..e9ec6501 100644 --- a/window.c +++ b/window.c @@ -933,7 +933,7 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) wp = xcalloc(1, sizeof *wp); wp->window = w; wp->options = options_create(w->options); - wp->flags = (PANE_STYLECHANGED|PANE_THEMECHANGED); + wp->flags = PANE_STYLECHANGED; wp->id = next_window_pane_id++; RB_INSERT(window_pane_tree, &all_window_panes, wp); @@ -1937,24 +1937,32 @@ window_pane_get_theme(struct window_pane *wp) void window_pane_send_theme_update(struct window_pane *wp) { + enum client_theme theme; + if (wp == NULL || window_pane_exited(wp)) return; if (~wp->flags & PANE_THEMECHANGED) return; if (~wp->screen->mode & MODE_THEME_UPDATES) return; - switch (window_pane_get_theme(wp)) { + + theme = window_pane_get_theme(wp); + if (theme == wp->last_theme) + return; + wp->last_theme = theme; + wp->flags &= ~PANE_THEMECHANGED; + + switch (theme) { case THEME_LIGHT: log_debug("%s: %%%u light theme", __func__, wp->id); - input_key_pane(wp, KEYC_REPORT_LIGHT_THEME, NULL); + bufferevent_write(wp->event, "\033[?997;2n", 9); break; case THEME_DARK: log_debug("%s: %%%u dark theme", __func__, wp->id); - input_key_pane(wp, KEYC_REPORT_DARK_THEME, NULL); + bufferevent_write(wp->event, "\033[?997;1n", 9); break; case THEME_UNKNOWN: log_debug("%s: %%%u unknown theme", __func__, wp->id); break; } - wp->flags &= ~PANE_THEMECHANGED; }