From b2443aa2f98c1a1fa5d53d4e79a3e7fd221cc365 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 16 Apr 2020 13:35:24 +0000 Subject: [PATCH] Add support for the iTerm2 sychronized updates escape sequence which drastically reduces flickering. --- screen-redraw.c | 6 ++++++ screen-write.c | 11 ++++++++++- tmux.1 | 2 ++ tmux.h | 6 ++++++ tty-keys.c | 2 +- tty-term.c | 5 +++++ tty.c | 26 ++++++++++++++++++++++++++ 7 files changed, 56 insertions(+), 2 deletions(-) diff --git a/screen-redraw.c b/screen-redraw.c index 211f7f79..c510fb68 100644 --- a/screen-redraw.c +++ b/screen-redraw.c @@ -435,6 +435,7 @@ screen_redraw_screen(struct client *c) flags = screen_redraw_update(c, c->flags); screen_redraw_set_context(c, &ctx); + tty_sync_start(&c->tty); if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) { if (ctx.pane_status != PANE_STATUS_OFF) @@ -448,7 +449,9 @@ screen_redraw_screen(struct client *c) screen_redraw_draw_status(&ctx); if (c->overlay_draw != NULL && (flags & CLIENT_REDRAWOVERLAY)) c->overlay_draw(c, &ctx); + tty_reset(&c->tty); + tty_sync_end(&c->tty); } /* Redraw a single pane. */ @@ -461,9 +464,12 @@ screen_redraw_pane(struct client *c, struct window_pane *wp) return; screen_redraw_set_context(c, &ctx); + tty_sync_start(&c->tty); screen_redraw_draw_pane(&ctx, wp); + tty_reset(&c->tty); + tty_sync_end(&c->tty); } /* Draw a border cell. */ diff --git a/screen-write.c b/screen-write.c index b2f9e223..3e7fa66b 100644 --- a/screen-write.c +++ b/screen-write.c @@ -100,7 +100,8 @@ void screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp, struct screen *s) { - u_int y; + struct tty_ctx ttyctx; + u_int y; memset(ctx, 0, sizeof *ctx); @@ -129,18 +130,26 @@ screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp, screen_size_y(ctx->s)); } } + + screen_write_initctx(ctx, &ttyctx); + tty_write(tty_cmd_syncstart, &ttyctx); } /* Finish writing. */ void screen_write_stop(struct screen_write_ctx *ctx) { + struct tty_ctx ttyctx; + screen_write_collect_end(ctx); screen_write_collect_flush(ctx, 0); log_debug("%s: %u cells (%u written, %u skipped)", __func__, ctx->cells, ctx->written, ctx->skipped); + screen_write_initctx(ctx, &ttyctx); + tty_write(tty_cmd_syncend, &ttyctx); + free(ctx->item); free(ctx->list); /* flush will have emptied */ } diff --git a/tmux.1 b/tmux.1 index 81a5c613..fd022c9e 100644 --- a/tmux.1 +++ b/tmux.1 @@ -5511,6 +5511,8 @@ $ printf '\e033[4 q' If .Em Se is not set, \&Ss with argument 0 will be used to reset the cursor style instead. +.It Em \&Sync +Show that the terminal supports synchronized updates. .It Em \&Tc Indicate that the terminal supports the .Ql direct colour diff --git a/tmux.h b/tmux.h index 98d78a63..87d38ef3 100644 --- a/tmux.h +++ b/tmux.h @@ -452,6 +452,7 @@ enum tty_code_code { TTYC_SMUL, TTYC_SMXX, TTYC_SS, + TTYC_SYNC, TTYC_TC, TTYC_TSL, TTYC_U8, @@ -1181,6 +1182,7 @@ struct tty_term { #define TERM_DECSLRM 0x4 #define TERM_DECFRA 0x8 #define TERM_RGBCOLOURS 0x10 +#define TERM_SYNC 0x20 int flags; LIST_ENTRY(tty_term) entry; @@ -1949,6 +1951,8 @@ void tty_set_title(struct tty *, const char *); void tty_update_mode(struct tty *, int, struct screen *); void tty_draw_line(struct tty *, struct window_pane *, struct screen *, u_int, u_int, u_int, u_int, u_int); +void tty_sync_start(struct tty *); +void tty_sync_end(struct tty *); int tty_open(struct tty *, char **); void tty_close(struct tty *); void tty_free(struct tty *); @@ -1976,6 +1980,8 @@ void tty_cmd_scrolldown(struct tty *, const struct tty_ctx *); void tty_cmd_reverseindex(struct tty *, const struct tty_ctx *); void tty_cmd_setselection(struct tty *, const struct tty_ctx *); void tty_cmd_rawstring(struct tty *, const struct tty_ctx *); +void tty_cmd_syncstart(struct tty *, const struct tty_ctx *); +void tty_cmd_syncend(struct tty *, const struct tty_ctx *); /* tty-term.c */ extern struct tty_terms tty_terms; diff --git a/tty-keys.c b/tty-keys.c index 064f2172..1fcc74b8 100644 --- a/tty-keys.c +++ b/tty-keys.c @@ -1117,7 +1117,7 @@ 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|TERM_256COLOURS|TERM_RGBCOLOURS); + flags |= (TERM_DECSLRM|TERM_256COLOURS|TERM_RGBCOLOURS|TERM_SYNC); if (strncmp(tmp, "TMUX ", 5) == 0) flags |= (TERM_256COLOURS|TERM_RGBCOLOURS); log_debug("%s: received DSR %.*s", c->name, (int)*size, buf); diff --git a/tty-term.c b/tty-term.c index ef7d7905..d6faa6ff 100644 --- a/tty-term.c +++ b/tty-term.c @@ -260,6 +260,7 @@ static const struct tty_term_code_entry tty_term_codes[] = { [TTYC_SMUL] = { TTYCODE_STRING, "smul" }, [TTYC_SMXX] = { TTYCODE_STRING, "smxx" }, [TTYC_SS] = { TTYCODE_STRING, "Ss" }, + [TTYC_SYNC] = { TTYCODE_FLAG, "Sync" }, [TTYC_TC] = { TTYCODE_FLAG, "Tc" }, [TTYC_TSL] = { TTYCODE_STRING, "tsl" }, [TTYC_U8] = { TTYCODE_NUMBER, "U8" }, @@ -532,6 +533,10 @@ tty_term_find(char *name, int fd, char **cause) tty_term_has(term, TTYC_SETRGBB))) term->flags |= TERM_RGBCOLOURS; + /* Set flag if terminal has synchronized updates. */ + if (tty_term_flag(term, TTYC_SYNC)) + term->flags |= TERM_SYNC; + /* * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1). diff --git a/tty.c b/tty.c index a7607cba..95c16fb8 100644 --- a/tty.c +++ b/tty.c @@ -1426,6 +1426,20 @@ tty_draw_line(struct tty *tty, struct window_pane *wp, struct screen *s, tty_update_mode(tty, tty->mode, s); } +void +tty_sync_start(struct tty *tty) +{ + if ((tty->term->flags|tty->term_flags) & TERM_SYNC) + tty_puts(tty, "\033P=1s\033\\"); +} + +void +tty_sync_end(struct tty *tty) +{ + if ((tty->term->flags|tty->term_flags) & TERM_SYNC) + tty_puts(tty, "\033P=2s\033\\"); +} + static int tty_client_ready(struct client *c, struct window_pane *wp) { @@ -1919,6 +1933,18 @@ tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx) tty_invalidate(tty); } +void +tty_cmd_syncstart(struct tty *tty, __unused const struct tty_ctx *ctx) +{ + tty_sync_start(tty); +} + +void +tty_cmd_syncend(struct tty *tty, __unused const struct tty_ctx *ctx) +{ + tty_sync_end(tty); +} + static void tty_cell(struct tty *tty, const struct grid_cell *gc, struct window_pane *wp) {