xref: /openbsd-src/usr.bin/tmux/screen-redraw.c (revision c020cf82e0cc147236f01a8dca7052034cf9d30d)
1 /* $OpenBSD: screen-redraw.c,v 1.79 2020/06/23 14:10:43 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 static void	screen_redraw_draw_borders(struct screen_redraw_ctx *);
27 static void	screen_redraw_draw_panes(struct screen_redraw_ctx *);
28 static void	screen_redraw_draw_status(struct screen_redraw_ctx *);
29 static void	screen_redraw_draw_pane(struct screen_redraw_ctx *,
30 		    struct window_pane *);
31 static void	screen_redraw_set_context(struct client *,
32 		    struct screen_redraw_ctx *);
33 
34 #define CELL_INSIDE 0
35 #define CELL_LEFTRIGHT 1
36 #define CELL_TOPBOTTOM 2
37 #define CELL_TOPLEFT 3
38 #define CELL_TOPRIGHT 4
39 #define CELL_BOTTOMLEFT 5
40 #define CELL_BOTTOMRIGHT 6
41 #define CELL_TOPJOIN 7
42 #define CELL_BOTTOMJOIN 8
43 #define CELL_LEFTJOIN 9
44 #define CELL_RIGHTJOIN 10
45 #define CELL_JOIN 11
46 #define CELL_OUTSIDE 12
47 
48 #define CELL_BORDERS " xqlkmjwvtun~"
49 
50 static const struct utf8_data screen_redraw_double_borders[] = {
51 	{ "", 0, 0, 0 },
52 	{ "\342\225\221", 0, 3, 1 }, /* U+2551 */
53 	{ "\342\225\220", 0, 3, 1 }, /* U+2550 */
54 	{ "\342\225\224", 0, 3, 1 }, /* U+2554 */
55 	{ "\342\225\227", 0, 3, 1 }, /* U+2557 */
56 	{ "\342\225\232", 0, 3, 1 }, /* U+255A */
57 	{ "\342\225\235", 0, 3, 1 }, /* U+255D */
58 	{ "\342\225\246", 0, 3, 1 }, /* U+2566 */
59 	{ "\342\225\251", 0, 3, 1 }, /* U+2569 */
60 	{ "\342\225\240", 0, 3, 1 }, /* U+2560 */
61 	{ "\342\225\243", 0, 3, 1 }, /* U+2563 */
62 	{ "\342\225\254", 0, 3, 1 }, /* U+256C */
63 	{ "\302\267",     0, 2, 1 }  /* U+00B7 */
64 };
65 
66 static const struct utf8_data screen_redraw_heavy_borders[] = {
67 	{ "", 0, 0, 0 },
68 	{ "\342\224\203", 0, 3, 1 }, /* U+2503 */
69 	{ "\342\224\201", 0, 3, 1 }, /* U+2501 */
70 	{ "\342\224\223", 0, 3, 1 }, /* U+2513 */
71 	{ "\342\224\217", 0, 3, 1 }, /* U+250F */
72 	{ "\342\224\227", 0, 3, 1 }, /* U+2517 */
73 	{ "\342\224\233", 0, 3, 1 }, /* U+251B */
74 	{ "\342\224\263", 0, 3, 1 }, /* U+2533 */
75 	{ "\342\224\273", 0, 3, 1 }, /* U+253B */
76 	{ "\342\224\243", 0, 3, 1 }, /* U+2523 */
77 	{ "\342\224\253", 0, 3, 1 }, /* U+252B */
78 	{ "\342\225\213", 0, 3, 1 }, /* U+254B */
79 	{ "\302\267",     0, 2, 1 }  /* U+00B7 */
80 };
81 
82 enum screen_redraw_border_type {
83 	SCREEN_REDRAW_OUTSIDE,
84 	SCREEN_REDRAW_INSIDE,
85 	SCREEN_REDRAW_BORDER
86 };
87 
88 /* Get cell border character. */
89 static void
90 screen_redraw_border_set(struct window_pane *wp, int pane_lines, int cell_type,
91     struct grid_cell *gc)
92 {
93 	u_int	idx;
94 
95 	switch (pane_lines) {
96 	case PANE_LINES_NUMBER:
97 		if (cell_type == CELL_OUTSIDE) {
98 			gc->attr |= GRID_ATTR_CHARSET;
99 			utf8_set(&gc->data, CELL_BORDERS[CELL_OUTSIDE]);
100 			break;
101 		}
102 		gc->attr &= ~GRID_ATTR_CHARSET;
103 		if (wp != NULL && window_pane_index(wp, &idx) == 0)
104 			utf8_set(&gc->data, '0' + (idx % 10));
105 		else
106 			utf8_set(&gc->data, '*');
107 		break;
108 	case PANE_LINES_DOUBLE:
109 		gc->attr &= ~GRID_ATTR_CHARSET;
110 		utf8_copy(&gc->data, &screen_redraw_double_borders[cell_type]);
111 		break;
112 	case PANE_LINES_HEAVY:
113 		gc->attr &= ~GRID_ATTR_CHARSET;
114 		utf8_copy(&gc->data, &screen_redraw_heavy_borders[cell_type]);
115 		break;
116 	case PANE_LINES_SIMPLE:
117 		gc->attr &= ~GRID_ATTR_CHARSET;
118 		utf8_set(&gc->data, " |-+++++++++."[cell_type]);
119 		break;
120 	default:
121 		gc->attr |= GRID_ATTR_CHARSET;
122 		utf8_set(&gc->data, CELL_BORDERS[cell_type]);
123 		break;
124 	}
125 }
126 
127 /* Return if window has only two panes. */
128 static int
129 screen_redraw_two_panes(struct window *w, int direction)
130 {
131 	struct window_pane	*wp;
132 
133 	wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
134 	if (wp == NULL)
135 		return (0); /* one pane */
136 	if (TAILQ_NEXT(wp, entry) != NULL)
137 		return (0); /* more than two panes */
138 	if (direction == 0 && wp->xoff == 0)
139 		return (0);
140 	if (direction == 1 && wp->yoff == 0)
141 		return (0);
142 	return (1);
143 }
144 
145 /* Check if cell is on the border of a pane. */
146 static enum screen_redraw_border_type
147 screen_redraw_pane_border(struct window_pane *wp, u_int px, u_int py,
148     int pane_status)
149 {
150 	u_int	ex = wp->xoff + wp->sx, ey = wp->yoff + wp->sy;
151 
152 	/* Inside pane. */
153 	if (px >= wp->xoff && px < ex && py >= wp->yoff && py < ey)
154 		return (SCREEN_REDRAW_INSIDE);
155 
156 	/* Left/right borders. */
157 	if (pane_status == PANE_STATUS_OFF) {
158 		if (screen_redraw_two_panes(wp->window, 0)) {
159 			if (wp->xoff == 0 && px == wp->sx && py <= wp->sy / 2)
160 				return (SCREEN_REDRAW_BORDER);
161 			if (wp->xoff != 0 &&
162 			    px == wp->xoff - 1 &&
163 			    py > wp->sy / 2)
164 				return (SCREEN_REDRAW_BORDER);
165 		} else {
166 			if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
167 				if (wp->xoff != 0 && px == wp->xoff - 1)
168 					return (SCREEN_REDRAW_BORDER);
169 				if (px == ex)
170 					return (SCREEN_REDRAW_BORDER);
171 			}
172 		}
173 	} else {
174 		if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
175 			if (wp->xoff != 0 && px == wp->xoff - 1)
176 				return (SCREEN_REDRAW_BORDER);
177 			if (px == ex)
178 				return (SCREEN_REDRAW_BORDER);
179 		}
180 	}
181 
182 	/* Top/bottom borders. */
183 	if (pane_status == PANE_STATUS_OFF) {
184 		if (screen_redraw_two_panes(wp->window, 1)) {
185 			if (wp->yoff == 0 && py == wp->sy && px <= wp->sx / 2)
186 				return (SCREEN_REDRAW_BORDER);
187 			if (wp->yoff != 0 &&
188 			    py == wp->yoff - 1 &&
189 			    px > wp->sx / 2)
190 				return (SCREEN_REDRAW_BORDER);
191 		} else {
192 			if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
193 				if (wp->yoff != 0 && py == wp->yoff - 1)
194 					return (SCREEN_REDRAW_BORDER);
195 				if (py == ey)
196 					return (SCREEN_REDRAW_BORDER);
197 			}
198 		}
199 	} else if (pane_status == PANE_STATUS_TOP) {
200 		if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
201 			if (wp->yoff != 0 && py == wp->yoff - 1)
202 				return (SCREEN_REDRAW_BORDER);
203 		}
204 	} else {
205 		if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
206 			if (py == ey)
207 				return (SCREEN_REDRAW_BORDER);
208 		}
209 	}
210 
211 	/* Outside pane. */
212 	return (SCREEN_REDRAW_OUTSIDE);
213 }
214 
215 /* Check if a cell is on a border. */
216 static int
217 screen_redraw_cell_border(struct client *c, u_int px, u_int py, int pane_status)
218 {
219 	struct window		*w = c->session->curw->window;
220 	struct window_pane	*wp;
221 
222 	/* Outside the window? */
223 	if (px > w->sx || py > w->sy)
224 		return (0);
225 
226 	/* On the window border? */
227 	if (px == w->sx || py == w->sy)
228 		return (1);
229 
230 	/* Check all the panes. */
231 	TAILQ_FOREACH(wp, &w->panes, entry) {
232 		if (!window_pane_visible(wp))
233 			continue;
234 		switch (screen_redraw_pane_border(wp, px, py, pane_status)) {
235 		case SCREEN_REDRAW_INSIDE:
236 			return (0);
237 		case SCREEN_REDRAW_BORDER:
238 			return (1);
239 		case SCREEN_REDRAW_OUTSIDE:
240 			break;
241 		}
242 	}
243 
244 	return (0);
245 }
246 
247 /* Work out type of border cell from surrounding cells. */
248 static int
249 screen_redraw_type_of_cell(struct client *c, u_int px, u_int py,
250     int pane_status)
251 {
252 	struct window	*w = c->session->curw->window;
253 	u_int		 sx = w->sx, sy = w->sy;
254 	int		 borders = 0;
255 
256 	/* Is this outside the window? */
257 	if (px >= sx || py >= sy)
258 		return (CELL_OUTSIDE);
259 
260 	/*
261 	 * Construct a bitmask of whether the cells to the left (bit 4), right,
262 	 * top, and bottom (bit 1) of this cell are borders.
263 	 */
264 	if (px == 0 || screen_redraw_cell_border(c, px - 1, py, pane_status))
265 		borders |= 8;
266 	if (px <= sx && screen_redraw_cell_border(c, px + 1, py, pane_status))
267 		borders |= 4;
268 	if (pane_status == PANE_STATUS_TOP) {
269 		if (py != 0 &&
270 		    screen_redraw_cell_border(c, px, py - 1, pane_status))
271 			borders |= 2;
272 		if (screen_redraw_cell_border(c, px, py + 1, pane_status))
273 			borders |= 1;
274 	} else {
275 		if (py == 0 ||
276 		    screen_redraw_cell_border(c, px, py - 1, pane_status))
277 			borders |= 2;
278 		if (py != sy - 1 &&
279 		    screen_redraw_cell_border(c, px, py + 1, pane_status))
280 			borders |= 1;
281 	}
282 
283 	/*
284 	 * Figure out what kind of border this cell is. Only one bit set
285 	 * doesn't make sense (can't have a border cell with no others
286 	 * connected).
287 	 */
288 	switch (borders) {
289 	case 15:	/* 1111, left right top bottom */
290 		return (CELL_JOIN);
291 	case 14:	/* 1110, left right top */
292 		return (CELL_BOTTOMJOIN);
293 	case 13:	/* 1101, left right bottom */
294 		return (CELL_TOPJOIN);
295 	case 12:	/* 1100, left right */
296 		return (CELL_TOPBOTTOM);
297 	case 11:	/* 1011, left top bottom */
298 		return (CELL_RIGHTJOIN);
299 	case 10:	/* 1010, left top */
300 		return (CELL_BOTTOMRIGHT);
301 	case 9:		/* 1001, left bottom */
302 		return (CELL_TOPRIGHT);
303 	case 7:		/* 0111, right top bottom */
304 		return (CELL_LEFTJOIN);
305 	case 6:		/* 0110, right top */
306 		return (CELL_BOTTOMLEFT);
307 	case 5:		/* 0101, right bottom */
308 		return (CELL_TOPLEFT);
309 	case 3:		/* 0011, top bottom */
310 		return (CELL_LEFTRIGHT);
311 	}
312 	return (CELL_OUTSIDE);
313 }
314 
315 /* Check if cell inside a pane. */
316 static int
317 screen_redraw_check_cell(struct client *c, u_int px, u_int py, int pane_status,
318     struct window_pane **wpp)
319 {
320 	struct window		*w = c->session->curw->window;
321 	struct window_pane	*wp, *active;
322 	int			 border;
323 	u_int			 right, line;
324 
325 	*wpp = NULL;
326 
327 	if (px >= w->sx || py >= w->sy)
328 		return (CELL_OUTSIDE);
329 	if (px == w->sx || py == w->sy) /* window border */
330 		return (screen_redraw_type_of_cell(c, px, py, pane_status));
331 
332 	if (pane_status != PANE_STATUS_OFF) {
333 		active = wp = server_client_get_pane(c);
334 		do {
335 			if (!window_pane_visible(wp))
336 				goto next1;
337 
338 			if (pane_status == PANE_STATUS_TOP)
339 				line = wp->yoff - 1;
340 			else
341 				line = wp->yoff + wp->sy;
342 			right = wp->xoff + 2 + wp->status_size - 1;
343 
344 			if (py == line && px >= wp->xoff + 2 && px <= right)
345 				return (CELL_INSIDE);
346 
347 		next1:
348 			wp = TAILQ_NEXT(wp, entry);
349 			if (wp == NULL)
350 				wp = TAILQ_FIRST(&w->panes);
351 		} while (wp != active);
352 	}
353 
354 	active = wp = server_client_get_pane(c);
355 	do {
356 		if (!window_pane_visible(wp))
357 			goto next2;
358 		*wpp = wp;
359 
360 		/*
361 		 * If definitely inside, return. If not on border, skip.
362 		 * Otherwise work out the cell.
363 		 */
364 		border = screen_redraw_pane_border(wp, px, py, pane_status);
365 		if (border == SCREEN_REDRAW_INSIDE)
366 			return (CELL_INSIDE);
367 		if (border == SCREEN_REDRAW_OUTSIDE)
368 			goto next2;
369 		return (screen_redraw_type_of_cell(c, px, py, pane_status));
370 
371 	next2:
372 		wp = TAILQ_NEXT(wp, entry);
373 		if (wp == NULL)
374 			wp = TAILQ_FIRST(&w->panes);
375 	} while (wp != active);
376 
377 	return (CELL_OUTSIDE);
378 }
379 
380 /* Check if the border of a particular pane. */
381 static int
382 screen_redraw_check_is(u_int px, u_int py, int pane_status,
383     struct window_pane *wp)
384 {
385 	enum screen_redraw_border_type	border;
386 
387 	border = screen_redraw_pane_border(wp, px, py, pane_status);
388 	if (border == SCREEN_REDRAW_BORDER)
389 		return (1);
390 	return (0);
391 }
392 
393 /* Update pane status. */
394 static int
395 screen_redraw_make_pane_status(struct client *c, struct window_pane *wp,
396     struct screen_redraw_ctx *rctx, int pane_lines)
397 {
398 	struct window		*w = wp->window;
399 	struct grid_cell	 gc;
400 	const char		*fmt;
401 	struct format_tree	*ft;
402 	char			*expanded;
403 	int			 pane_status = rctx->pane_status;
404 	u_int			 width, i, cell_type, top, px, py;
405 	struct screen_write_ctx	 ctx;
406 	struct screen		 old;
407 
408 	ft = format_create(c, NULL, FORMAT_PANE|wp->id, FORMAT_STATUS);
409 	format_defaults(ft, c, c->session, c->session->curw, wp);
410 
411 	if (wp == server_client_get_pane(c))
412 		style_apply(&gc, w->options, "pane-active-border-style", ft);
413 	else
414 		style_apply(&gc, w->options, "pane-border-style", ft);
415 	fmt = options_get_string(w->options, "pane-border-format");
416 
417 	expanded = format_expand_time(ft, fmt);
418 	if (wp->sx < 4)
419 		wp->status_size = width = 0;
420 	else
421 		wp->status_size = width = wp->sx - 4;
422 
423 	memcpy(&old, &wp->status_screen, sizeof old);
424 	screen_init(&wp->status_screen, width, 1, 0);
425 	wp->status_screen.mode = 0;
426 
427 	screen_write_start(&ctx, &wp->status_screen);
428 
429 	if (rctx->statustop)
430 		top = rctx->statuslines;
431 	else
432 		top = 0;
433 	for (i = 0; i < width; i++) {
434 		px = wp->xoff + 2 + i;
435 		if (rctx->pane_status == PANE_STATUS_TOP)
436 			py = top + wp->yoff - 1;
437 		else
438 			py = top + wp->yoff + wp->sy;
439 		cell_type = screen_redraw_type_of_cell(c, px, py, pane_status);
440 		screen_redraw_border_set(wp, pane_lines, cell_type, &gc);
441 		screen_write_cell(&ctx, &gc);
442 	}
443 	gc.attr &= ~GRID_ATTR_CHARSET;
444 
445 	screen_write_cursormove(&ctx, 0, 0, 0);
446 	format_draw(&ctx, &gc, width, expanded, NULL);
447 	screen_write_stop(&ctx);
448 
449 	free(expanded);
450 	format_free(ft);
451 
452 	if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
453 		screen_free(&old);
454 		return (0);
455 	}
456 	screen_free(&old);
457 	return (1);
458 }
459 
460 /* Draw pane status. */
461 static void
462 screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
463 {
464 	struct client		*c = ctx->c;
465 	struct window		*w = c->session->curw->window;
466 	struct tty		*tty = &c->tty;
467 	struct window_pane	*wp;
468 	struct screen		*s;
469 	u_int			 i, x, width, xoff, yoff, size;
470 
471 	log_debug("%s: %s @%u", __func__, c->name, w->id);
472 
473 	TAILQ_FOREACH(wp, &w->panes, entry) {
474 		if (!window_pane_visible(wp))
475 			continue;
476 		s = &wp->status_screen;
477 
478 		size = wp->status_size;
479 		if (ctx->pane_status == PANE_STATUS_TOP)
480 			yoff = wp->yoff - 1;
481 		else
482 			yoff = wp->yoff + wp->sy;
483 		xoff = wp->xoff + 2;
484 
485 		if (xoff + size <= ctx->ox ||
486 		    xoff >= ctx->ox + ctx->sx ||
487 		    yoff < ctx->oy ||
488 		    yoff >= ctx->oy + ctx->sy)
489 			continue;
490 
491 		if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
492 			/* All visible. */
493 			i = 0;
494 			x = xoff - ctx->ox;
495 			width = size;
496 		} else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
497 			/* Both left and right not visible. */
498 			i = ctx->ox;
499 			x = 0;
500 			width = ctx->sx;
501 		} else if (xoff < ctx->ox) {
502 			/* Left not visible. */
503 			i = ctx->ox - xoff;
504 			x = 0;
505 			width = size - i;
506 		} else {
507 			/* Right not visible. */
508 			i = 0;
509 			x = xoff - ctx->ox;
510 			width = size - x;
511 		}
512 
513 		if (ctx->statustop)
514 			yoff += ctx->statuslines;
515 		tty_draw_line(tty, s, i, 0, width, x, yoff - ctx->oy,
516 		    &grid_default_cell, NULL);
517 	}
518 	tty_cursor(tty, 0, 0);
519 }
520 
521 /* Update status line and change flags if unchanged. */
522 static int
523 screen_redraw_update(struct client *c, int flags)
524 {
525 	struct window		*w = c->session->curw->window;
526 	struct window_pane	*wp;
527 	struct options		*wo = w->options;
528 	int			 redraw, lines;
529 	struct screen_redraw_ctx ctx;
530 
531 	if (c->message_string != NULL)
532 		redraw = status_message_redraw(c);
533 	else if (c->prompt_string != NULL)
534 		redraw = status_prompt_redraw(c);
535 	else
536 		redraw = status_redraw(c);
537 	if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
538 		flags &= ~CLIENT_REDRAWSTATUS;
539 
540 	if (c->overlay_draw != NULL)
541 		flags |= CLIENT_REDRAWOVERLAY;
542 
543 	if (options_get_number(wo, "pane-border-status") != PANE_STATUS_OFF) {
544 		screen_redraw_set_context(c, &ctx);
545 		lines = options_get_number(wo, "pane-border-lines");
546 		redraw = 0;
547 		TAILQ_FOREACH(wp, &w->panes, entry) {
548 			if (screen_redraw_make_pane_status(c, wp, &ctx, lines))
549 				redraw = 1;
550 		}
551 		if (redraw)
552 			flags |= CLIENT_REDRAWBORDERS;
553 	}
554 	return (flags);
555 }
556 
557 /* Set up redraw context. */
558 static void
559 screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
560 {
561 	struct session	*s = c->session;
562 	struct options	*oo = s->options;
563 	struct window	*w = s->curw->window;
564 	struct options	*wo = w->options;
565 	u_int		 lines;
566 
567 	memset(ctx, 0, sizeof *ctx);
568 	ctx->c = c;
569 
570 	lines = status_line_size(c);
571 	if (c->message_string != NULL || c->prompt_string != NULL)
572 		lines = (lines == 0) ? 1 : lines;
573 	if (lines != 0 && options_get_number(oo, "status-position") == 0)
574 		ctx->statustop = 1;
575 	ctx->statuslines = lines;
576 
577 	ctx->pane_status = options_get_number(wo, "pane-border-status");
578 	ctx->pane_lines = options_get_number(wo, "pane-border-lines");
579 
580 	tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
581 
582 	log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
583 	    w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->statuslines,
584 	    ctx->statustop);
585 }
586 
587 /* Redraw entire screen. */
588 void
589 screen_redraw_screen(struct client *c)
590 {
591 	struct screen_redraw_ctx	ctx;
592 	int				flags;
593 
594 	if (c->flags & CLIENT_SUSPENDED)
595 		return;
596 
597 	flags = screen_redraw_update(c, c->flags);
598 	if ((flags & CLIENT_ALLREDRAWFLAGS) == 0)
599 		return;
600 
601 	screen_redraw_set_context(c, &ctx);
602 	tty_update_mode(&c->tty, c->tty.mode, NULL);
603 	tty_sync_start(&c->tty);
604 
605 	if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
606 		log_debug("%s: redrawing borders", c->name);
607 		if (ctx.pane_status != PANE_STATUS_OFF)
608 			screen_redraw_draw_pane_status(&ctx);
609 		screen_redraw_draw_borders(&ctx);
610 	}
611 	if (flags & CLIENT_REDRAWWINDOW) {
612 		log_debug("%s: redrawing panes", c->name);
613 		screen_redraw_draw_panes(&ctx);
614 	}
615 	if (ctx.statuslines != 0 &&
616 	    (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS))) {
617 		log_debug("%s: redrawing status", c->name);
618 		screen_redraw_draw_status(&ctx);
619 	}
620 	if (c->overlay_draw != NULL && (flags & CLIENT_REDRAWOVERLAY)) {
621 		log_debug("%s: redrawing overlay", c->name);
622 		c->overlay_draw(c, &ctx);
623 	}
624 
625 	tty_reset(&c->tty);
626 }
627 
628 /* Redraw a single pane. */
629 void
630 screen_redraw_pane(struct client *c, struct window_pane *wp)
631 {
632 	struct screen_redraw_ctx	 ctx;
633 
634 	if (c->overlay_draw != NULL || !window_pane_visible(wp))
635 		return;
636 
637 	screen_redraw_set_context(c, &ctx);
638 	tty_update_mode(&c->tty, c->tty.mode, NULL);
639 	tty_sync_start(&c->tty);
640 
641 	screen_redraw_draw_pane(&ctx, wp);
642 
643 	tty_reset(&c->tty);
644 }
645 
646 /* Get border cell style. */
647 static const struct grid_cell *
648 screen_redraw_draw_borders_style(struct screen_redraw_ctx *ctx, u_int x,
649     u_int y, struct window_pane *wp)
650 {
651 	struct client		*c = ctx->c;
652 	struct session		*s = c->session;
653 	struct window		*w = s->curw->window;
654 	struct window_pane	*active = server_client_get_pane(c);
655 	struct options		*oo = w->options;
656 	struct format_tree	*ft;
657 
658 	if (wp->border_gc_set)
659 		return (&wp->border_gc);
660 	wp->border_gc_set = 1;
661 
662 	ft = format_create_defaults(NULL, c, s, s->curw, wp);
663 	if (screen_redraw_check_is(x, y, ctx->pane_status, active))
664 		style_apply(&wp->border_gc, oo, "pane-active-border-style", ft);
665 	else
666 		style_apply(&wp->border_gc, oo, "pane-border-style", ft);
667 	format_free(ft);
668 
669 	return (&wp->border_gc);
670 }
671 
672 /* Draw a border cell. */
673 static void
674 screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j)
675 {
676 	struct client		*c = ctx->c;
677 	struct session		*s = c->session;
678 	struct tty		*tty = &c->tty;
679 	struct window_pane	*wp;
680 	u_int			 cell_type, x = ctx->ox + i, y = ctx->oy + j;
681 	int			 pane_status = ctx->pane_status;
682 	struct grid_cell	 gc;
683 	const struct grid_cell	*tmp;
684 
685 	if (c->overlay_check != NULL && !c->overlay_check(c, x, y))
686 		return;
687 
688 	cell_type = screen_redraw_check_cell(c, x, y, pane_status, &wp);
689 	if (cell_type == CELL_INSIDE)
690 		return;
691 
692 	if (wp == NULL)
693 		memcpy(&gc, &grid_default_cell, sizeof gc);
694 	else {
695 		tmp = screen_redraw_draw_borders_style(ctx, x, y, wp);
696 		if (tmp == NULL)
697 			return;
698 		memcpy(&gc, tmp, sizeof gc);
699 
700 		if (server_is_marked(s, s->curw, marked_pane.wp) &&
701 		    screen_redraw_check_is(x, y, pane_status, marked_pane.wp))
702 			gc.attr ^= GRID_ATTR_REVERSE;
703 	}
704 	screen_redraw_border_set(wp, ctx->pane_lines, cell_type, &gc);
705 
706 	if (ctx->statustop)
707 		tty_cursor(tty, i, ctx->statuslines + j);
708 	else
709 		tty_cursor(tty, i, j);
710 	tty_cell(tty, &gc, &grid_default_cell, NULL);
711 }
712 
713 /* Draw the borders. */
714 static void
715 screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
716 {
717 	struct client		*c = ctx->c;
718 	struct session		*s = c->session;
719 	struct window		*w = s->curw->window;
720 	struct window_pane	*wp;
721 	u_int		 	 i, j;
722 
723 	log_debug("%s: %s @%u", __func__, c->name, w->id);
724 
725 	TAILQ_FOREACH(wp, &w->panes, entry)
726 		wp->border_gc_set = 0;
727 
728 	for (j = 0; j < c->tty.sy - ctx->statuslines; j++) {
729 		for (i = 0; i < c->tty.sx; i++)
730 			screen_redraw_draw_borders_cell(ctx, i, j);
731 	}
732 }
733 
734 /* Draw the panes. */
735 static void
736 screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
737 {
738 	struct client		*c = ctx->c;
739 	struct window		*w = c->session->curw->window;
740 	struct window_pane	*wp;
741 
742 	log_debug("%s: %s @%u", __func__, c->name, w->id);
743 
744 	TAILQ_FOREACH(wp, &w->panes, entry) {
745 		if (window_pane_visible(wp))
746 			screen_redraw_draw_pane(ctx, wp);
747 	}
748 }
749 
750 /* Draw the status line. */
751 static void
752 screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
753 {
754 	struct client	*c = ctx->c;
755 	struct window	*w = c->session->curw->window;
756 	struct tty	*tty = &c->tty;
757 	struct screen	*s = c->status.active;
758 	u_int		 i, y;
759 
760 	log_debug("%s: %s @%u", __func__, c->name, w->id);
761 
762 	if (ctx->statustop)
763 		y = 0;
764 	else
765 		y = c->tty.sy - ctx->statuslines;
766 	for (i = 0; i < ctx->statuslines; i++) {
767 		tty_draw_line(tty, s, 0, i, UINT_MAX, 0, y + i,
768 		    &grid_default_cell, NULL);
769 	}
770 }
771 
772 /* Draw one pane. */
773 static void
774 screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
775 {
776 	struct client	*c = ctx->c;
777 	struct window	*w = c->session->curw->window;
778 	struct tty	*tty = &c->tty;
779 	struct screen	*s;
780 	struct grid_cell defaults;
781 	u_int		 i, j, top, x, y, width;
782 
783 	log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
784 
785 	if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
786 		return;
787 	if (ctx->statustop)
788 		top = ctx->statuslines;
789 	else
790 		top = 0;
791 
792 	s = wp->screen;
793 	for (j = 0; j < wp->sy; j++) {
794 		if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
795 			continue;
796 		y = top + wp->yoff + j - ctx->oy;
797 
798 		if (wp->xoff >= ctx->ox &&
799 		    wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
800 			/* All visible. */
801 			i = 0;
802 			x = wp->xoff - ctx->ox;
803 			width = wp->sx;
804 		} else if (wp->xoff < ctx->ox &&
805 		    wp->xoff + wp->sx > ctx->ox + ctx->sx) {
806 			/* Both left and right not visible. */
807 			i = ctx->ox;
808 			x = 0;
809 			width = ctx->sx;
810 		} else if (wp->xoff < ctx->ox) {
811 			/* Left not visible. */
812 			i = ctx->ox - wp->xoff;
813 			x = 0;
814 			width = wp->sx - i;
815 		} else {
816 			/* Right not visible. */
817 			i = 0;
818 			x = wp->xoff - ctx->ox;
819 			width = ctx->sx - x;
820 		}
821 		log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
822 		    __func__, c->name, wp->id, i, j, x, y, width);
823 
824 		tty_default_colours(&defaults, wp);
825 		tty_draw_line(tty, s, i, j, width, x, y, &defaults,
826 		    wp->palette);
827 	}
828 }
829