xref: /openbsd-src/usr.bin/tmux/screen-redraw.c (revision fcde59b201a29a2b4570b00b71e7aa25d61cb5c1)
1 /* $OpenBSD: screen-redraw.c,v 1.82 2020/12/07 09:46:58 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 if (pane_status == PANE_STATUS_BOTTOM) {
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 	} else {
282 		if (py == 0 ||
283 		    screen_redraw_cell_border(c, px, py - 1, pane_status))
284 			borders |= 2;
285 		if (screen_redraw_cell_border(c, px, py + 1, pane_status))
286 			borders |= 1;
287 	}
288 
289 	/*
290 	 * Figure out what kind of border this cell is. Only one bit set
291 	 * doesn't make sense (can't have a border cell with no others
292 	 * connected).
293 	 */
294 	switch (borders) {
295 	case 15:	/* 1111, left right top bottom */
296 		return (CELL_JOIN);
297 	case 14:	/* 1110, left right top */
298 		return (CELL_BOTTOMJOIN);
299 	case 13:	/* 1101, left right bottom */
300 		return (CELL_TOPJOIN);
301 	case 12:	/* 1100, left right */
302 		return (CELL_TOPBOTTOM);
303 	case 11:	/* 1011, left top bottom */
304 		return (CELL_RIGHTJOIN);
305 	case 10:	/* 1010, left top */
306 		return (CELL_BOTTOMRIGHT);
307 	case 9:		/* 1001, left bottom */
308 		return (CELL_TOPRIGHT);
309 	case 7:		/* 0111, right top bottom */
310 		return (CELL_LEFTJOIN);
311 	case 6:		/* 0110, right top */
312 		return (CELL_BOTTOMLEFT);
313 	case 5:		/* 0101, right bottom */
314 		return (CELL_TOPLEFT);
315 	case 3:		/* 0011, top bottom */
316 		return (CELL_LEFTRIGHT);
317 	}
318 	return (CELL_OUTSIDE);
319 }
320 
321 /* Check if cell inside a pane. */
322 static int
323 screen_redraw_check_cell(struct client *c, u_int px, u_int py, int pane_status,
324     struct window_pane **wpp)
325 {
326 	struct window		*w = c->session->curw->window;
327 	struct window_pane	*wp, *active;
328 	int			 border;
329 	u_int			 right, line;
330 
331 	*wpp = NULL;
332 
333 	if (px > w->sx || py > w->sy)
334 		return (CELL_OUTSIDE);
335 	if (px == w->sx || py == w->sy) /* window border */
336 		return (screen_redraw_type_of_cell(c, px, py, pane_status));
337 
338 	if (pane_status != PANE_STATUS_OFF) {
339 		active = wp = server_client_get_pane(c);
340 		do {
341 			if (!window_pane_visible(wp))
342 				goto next1;
343 
344 			if (pane_status == PANE_STATUS_TOP)
345 				line = wp->yoff - 1;
346 			else
347 				line = wp->yoff + wp->sy;
348 			right = wp->xoff + 2 + wp->status_size - 1;
349 
350 			if (py == line && px >= wp->xoff + 2 && px <= right)
351 				return (CELL_INSIDE);
352 
353 		next1:
354 			wp = TAILQ_NEXT(wp, entry);
355 			if (wp == NULL)
356 				wp = TAILQ_FIRST(&w->panes);
357 		} while (wp != active);
358 	}
359 
360 	active = wp = server_client_get_pane(c);
361 	do {
362 		if (!window_pane_visible(wp))
363 			goto next2;
364 		*wpp = wp;
365 
366 		/*
367 		 * If definitely inside, return. If not on border, skip.
368 		 * Otherwise work out the cell.
369 		 */
370 		border = screen_redraw_pane_border(wp, px, py, pane_status);
371 		if (border == SCREEN_REDRAW_INSIDE)
372 			return (CELL_INSIDE);
373 		if (border == SCREEN_REDRAW_OUTSIDE)
374 			goto next2;
375 		return (screen_redraw_type_of_cell(c, px, py, pane_status));
376 
377 	next2:
378 		wp = TAILQ_NEXT(wp, entry);
379 		if (wp == NULL)
380 			wp = TAILQ_FIRST(&w->panes);
381 	} while (wp != active);
382 
383 	return (CELL_OUTSIDE);
384 }
385 
386 /* Check if the border of a particular pane. */
387 static int
388 screen_redraw_check_is(u_int px, u_int py, int pane_status,
389     struct window_pane *wp)
390 {
391 	enum screen_redraw_border_type	border;
392 
393 	border = screen_redraw_pane_border(wp, px, py, pane_status);
394 	if (border == SCREEN_REDRAW_BORDER)
395 		return (1);
396 	return (0);
397 }
398 
399 /* Update pane status. */
400 static int
401 screen_redraw_make_pane_status(struct client *c, struct window_pane *wp,
402     struct screen_redraw_ctx *rctx, int pane_lines)
403 {
404 	struct window		*w = wp->window;
405 	struct grid_cell	 gc;
406 	const char		*fmt;
407 	struct format_tree	*ft;
408 	char			*expanded;
409 	int			 pane_status = rctx->pane_status;
410 	u_int			 width, i, cell_type, px, py;
411 	struct screen_write_ctx	 ctx;
412 	struct screen		 old;
413 
414 	ft = format_create(c, NULL, FORMAT_PANE|wp->id, FORMAT_STATUS);
415 	format_defaults(ft, c, c->session, c->session->curw, wp);
416 
417 	if (wp == server_client_get_pane(c))
418 		style_apply(&gc, w->options, "pane-active-border-style", ft);
419 	else
420 		style_apply(&gc, w->options, "pane-border-style", ft);
421 	fmt = options_get_string(w->options, "pane-border-format");
422 
423 	expanded = format_expand_time(ft, fmt);
424 	if (wp->sx < 4)
425 		wp->status_size = width = 0;
426 	else
427 		wp->status_size = width = wp->sx - 4;
428 
429 	memcpy(&old, &wp->status_screen, sizeof old);
430 	screen_init(&wp->status_screen, width, 1, 0);
431 	wp->status_screen.mode = 0;
432 
433 	screen_write_start(&ctx, &wp->status_screen);
434 
435 	for (i = 0; i < width; i++) {
436 		px = wp->xoff + 2 + i;
437 		if (rctx->pane_status == PANE_STATUS_TOP)
438 			py = wp->yoff - 1;
439 		else
440 			py = wp->yoff + wp->sy;
441 		cell_type = screen_redraw_type_of_cell(c, px, py, pane_status);
442 		screen_redraw_border_set(wp, pane_lines, cell_type, &gc);
443 		screen_write_cell(&ctx, &gc);
444 	}
445 	gc.attr &= ~GRID_ATTR_CHARSET;
446 
447 	screen_write_cursormove(&ctx, 0, 0, 0);
448 	format_draw(&ctx, &gc, width, expanded, NULL);
449 	screen_write_stop(&ctx);
450 
451 	free(expanded);
452 	format_free(ft);
453 
454 	if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
455 		screen_free(&old);
456 		return (0);
457 	}
458 	screen_free(&old);
459 	return (1);
460 }
461 
462 /* Draw pane status. */
463 static void
464 screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
465 {
466 	struct client		*c = ctx->c;
467 	struct window		*w = c->session->curw->window;
468 	struct tty		*tty = &c->tty;
469 	struct window_pane	*wp;
470 	struct screen		*s;
471 	u_int			 i, x, width, xoff, yoff, size;
472 
473 	log_debug("%s: %s @%u", __func__, c->name, w->id);
474 
475 	TAILQ_FOREACH(wp, &w->panes, entry) {
476 		if (!window_pane_visible(wp))
477 			continue;
478 		s = &wp->status_screen;
479 
480 		size = wp->status_size;
481 		if (ctx->pane_status == PANE_STATUS_TOP)
482 			yoff = wp->yoff - 1;
483 		else
484 			yoff = wp->yoff + wp->sy;
485 		xoff = wp->xoff + 2;
486 
487 		if (xoff + size <= ctx->ox ||
488 		    xoff >= ctx->ox + ctx->sx ||
489 		    yoff < ctx->oy ||
490 		    yoff >= ctx->oy + ctx->sy)
491 			continue;
492 
493 		if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
494 			/* All visible. */
495 			i = 0;
496 			x = xoff - ctx->ox;
497 			width = size;
498 		} else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
499 			/* Both left and right not visible. */
500 			i = ctx->ox;
501 			x = 0;
502 			width = ctx->sx;
503 		} else if (xoff < ctx->ox) {
504 			/* Left not visible. */
505 			i = ctx->ox - xoff;
506 			x = 0;
507 			width = size - i;
508 		} else {
509 			/* Right not visible. */
510 			i = 0;
511 			x = xoff - ctx->ox;
512 			width = size - x;
513 		}
514 
515 		if (ctx->statustop)
516 			yoff += ctx->statuslines;
517 		tty_draw_line(tty, s, i, 0, width, x, yoff - ctx->oy,
518 		    &grid_default_cell, NULL);
519 	}
520 	tty_cursor(tty, 0, 0);
521 }
522 
523 /* Update status line and change flags if unchanged. */
524 static int
525 screen_redraw_update(struct client *c, int flags)
526 {
527 	struct window		*w = c->session->curw->window;
528 	struct window_pane	*wp;
529 	struct options		*wo = w->options;
530 	int			 redraw, lines;
531 	struct screen_redraw_ctx ctx;
532 
533 	if (c->message_string != NULL)
534 		redraw = status_message_redraw(c);
535 	else if (c->prompt_string != NULL)
536 		redraw = status_prompt_redraw(c);
537 	else
538 		redraw = status_redraw(c);
539 	if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
540 		flags &= ~CLIENT_REDRAWSTATUS;
541 
542 	if (c->overlay_draw != NULL)
543 		flags |= CLIENT_REDRAWOVERLAY;
544 
545 	if (options_get_number(wo, "pane-border-status") != PANE_STATUS_OFF) {
546 		screen_redraw_set_context(c, &ctx);
547 		lines = options_get_number(wo, "pane-border-lines");
548 		redraw = 0;
549 		TAILQ_FOREACH(wp, &w->panes, entry) {
550 			if (screen_redraw_make_pane_status(c, wp, &ctx, lines))
551 				redraw = 1;
552 		}
553 		if (redraw)
554 			flags |= CLIENT_REDRAWBORDERS;
555 	}
556 	return (flags);
557 }
558 
559 /* Set up redraw context. */
560 static void
561 screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
562 {
563 	struct session	*s = c->session;
564 	struct options	*oo = s->options;
565 	struct window	*w = s->curw->window;
566 	struct options	*wo = w->options;
567 	u_int		 lines;
568 
569 	memset(ctx, 0, sizeof *ctx);
570 	ctx->c = c;
571 
572 	lines = status_line_size(c);
573 	if (c->message_string != NULL || c->prompt_string != NULL)
574 		lines = (lines == 0) ? 1 : lines;
575 	if (lines != 0 && options_get_number(oo, "status-position") == 0)
576 		ctx->statustop = 1;
577 	ctx->statuslines = lines;
578 
579 	ctx->pane_status = options_get_number(wo, "pane-border-status");
580 	ctx->pane_lines = options_get_number(wo, "pane-border-lines");
581 
582 	tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
583 
584 	log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
585 	    w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->statuslines,
586 	    ctx->statustop);
587 }
588 
589 /* Redraw entire screen. */
590 void
591 screen_redraw_screen(struct client *c)
592 {
593 	struct screen_redraw_ctx	ctx;
594 	int				flags;
595 
596 	if (c->flags & CLIENT_SUSPENDED)
597 		return;
598 
599 	flags = screen_redraw_update(c, c->flags);
600 	if ((flags & CLIENT_ALLREDRAWFLAGS) == 0)
601 		return;
602 
603 	screen_redraw_set_context(c, &ctx);
604 	tty_update_mode(&c->tty, c->tty.mode, NULL);
605 	tty_sync_start(&c->tty);
606 
607 	if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
608 		log_debug("%s: redrawing borders", c->name);
609 		if (ctx.pane_status != PANE_STATUS_OFF)
610 			screen_redraw_draw_pane_status(&ctx);
611 		screen_redraw_draw_borders(&ctx);
612 	}
613 	if (flags & CLIENT_REDRAWWINDOW) {
614 		log_debug("%s: redrawing panes", c->name);
615 		screen_redraw_draw_panes(&ctx);
616 	}
617 	if (ctx.statuslines != 0 &&
618 	    (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS))) {
619 		log_debug("%s: redrawing status", c->name);
620 		screen_redraw_draw_status(&ctx);
621 	}
622 	if (c->overlay_draw != NULL && (flags & CLIENT_REDRAWOVERLAY)) {
623 		log_debug("%s: redrawing overlay", c->name);
624 		c->overlay_draw(c, &ctx);
625 	}
626 
627 	tty_reset(&c->tty);
628 }
629 
630 /* Redraw a single pane. */
631 void
632 screen_redraw_pane(struct client *c, struct window_pane *wp)
633 {
634 	struct screen_redraw_ctx	 ctx;
635 
636 	if (c->overlay_draw != NULL || !window_pane_visible(wp))
637 		return;
638 
639 	screen_redraw_set_context(c, &ctx);
640 	tty_update_mode(&c->tty, c->tty.mode, NULL);
641 	tty_sync_start(&c->tty);
642 
643 	screen_redraw_draw_pane(&ctx, wp);
644 
645 	tty_reset(&c->tty);
646 }
647 
648 /* Get border cell style. */
649 static const struct grid_cell *
650 screen_redraw_draw_borders_style(struct screen_redraw_ctx *ctx, u_int x,
651     u_int y, struct window_pane *wp)
652 {
653 	struct client		*c = ctx->c;
654 	struct session		*s = c->session;
655 	struct window		*w = s->curw->window;
656 	struct window_pane	*active = server_client_get_pane(c);
657 	struct options		*oo = w->options;
658 	struct format_tree	*ft;
659 
660 	if (wp->border_gc_set)
661 		return (&wp->border_gc);
662 	wp->border_gc_set = 1;
663 
664 	ft = format_create_defaults(NULL, c, s, s->curw, wp);
665 	if (screen_redraw_check_is(x, y, ctx->pane_status, active))
666 		style_apply(&wp->border_gc, oo, "pane-active-border-style", ft);
667 	else
668 		style_apply(&wp->border_gc, oo, "pane-border-style", ft);
669 	format_free(ft);
670 
671 	return (&wp->border_gc);
672 }
673 
674 /* Draw a border cell. */
675 static void
676 screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j)
677 {
678 	struct client		*c = ctx->c;
679 	struct session		*s = c->session;
680 	struct tty		*tty = &c->tty;
681 	struct window_pane	*wp;
682 	u_int			 cell_type, x = ctx->ox + i, y = ctx->oy + j;
683 	int			 pane_status = ctx->pane_status;
684 	struct grid_cell	 gc;
685 	const struct grid_cell	*tmp;
686 
687 	if (c->overlay_check != NULL && !c->overlay_check(c, x, y))
688 		return;
689 
690 	cell_type = screen_redraw_check_cell(c, x, y, pane_status, &wp);
691 	if (cell_type == CELL_INSIDE)
692 		return;
693 
694 	if (wp == NULL)
695 		memcpy(&gc, &grid_default_cell, sizeof gc);
696 	else {
697 		tmp = screen_redraw_draw_borders_style(ctx, x, y, wp);
698 		if (tmp == NULL)
699 			return;
700 		memcpy(&gc, tmp, sizeof gc);
701 
702 		if (server_is_marked(s, s->curw, marked_pane.wp) &&
703 		    screen_redraw_check_is(x, y, pane_status, marked_pane.wp))
704 			gc.attr ^= GRID_ATTR_REVERSE;
705 	}
706 	screen_redraw_border_set(wp, ctx->pane_lines, cell_type, &gc);
707 
708 	if (ctx->statustop)
709 		tty_cursor(tty, i, ctx->statuslines + j);
710 	else
711 		tty_cursor(tty, i, j);
712 	tty_cell(tty, &gc, &grid_default_cell, NULL);
713 }
714 
715 /* Draw the borders. */
716 static void
717 screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
718 {
719 	struct client		*c = ctx->c;
720 	struct session		*s = c->session;
721 	struct window		*w = s->curw->window;
722 	struct window_pane	*wp;
723 	u_int		 	 i, j;
724 
725 	log_debug("%s: %s @%u", __func__, c->name, w->id);
726 
727 	TAILQ_FOREACH(wp, &w->panes, entry)
728 		wp->border_gc_set = 0;
729 
730 	for (j = 0; j < c->tty.sy - ctx->statuslines; j++) {
731 		for (i = 0; i < c->tty.sx; i++)
732 			screen_redraw_draw_borders_cell(ctx, i, j);
733 	}
734 }
735 
736 /* Draw the panes. */
737 static void
738 screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
739 {
740 	struct client		*c = ctx->c;
741 	struct window		*w = c->session->curw->window;
742 	struct window_pane	*wp;
743 
744 	log_debug("%s: %s @%u", __func__, c->name, w->id);
745 
746 	TAILQ_FOREACH(wp, &w->panes, entry) {
747 		if (window_pane_visible(wp))
748 			screen_redraw_draw_pane(ctx, wp);
749 	}
750 }
751 
752 /* Draw the status line. */
753 static void
754 screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
755 {
756 	struct client	*c = ctx->c;
757 	struct window	*w = c->session->curw->window;
758 	struct tty	*tty = &c->tty;
759 	struct screen	*s = c->status.active;
760 	u_int		 i, y;
761 
762 	log_debug("%s: %s @%u", __func__, c->name, w->id);
763 
764 	if (ctx->statustop)
765 		y = 0;
766 	else
767 		y = c->tty.sy - ctx->statuslines;
768 	for (i = 0; i < ctx->statuslines; i++) {
769 		tty_draw_line(tty, s, 0, i, UINT_MAX, 0, y + i,
770 		    &grid_default_cell, NULL);
771 	}
772 }
773 
774 /* Draw one pane. */
775 static void
776 screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
777 {
778 	struct client	*c = ctx->c;
779 	struct window	*w = c->session->curw->window;
780 	struct tty	*tty = &c->tty;
781 	struct screen	*s;
782 	struct grid_cell defaults;
783 	u_int		 i, j, top, x, y, width;
784 
785 	log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
786 
787 	if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
788 		return;
789 	if (ctx->statustop)
790 		top = ctx->statuslines;
791 	else
792 		top = 0;
793 
794 	s = wp->screen;
795 	for (j = 0; j < wp->sy; j++) {
796 		if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
797 			continue;
798 		y = top + wp->yoff + j - ctx->oy;
799 
800 		if (wp->xoff >= ctx->ox &&
801 		    wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
802 			/* All visible. */
803 			i = 0;
804 			x = wp->xoff - ctx->ox;
805 			width = wp->sx;
806 		} else if (wp->xoff < ctx->ox &&
807 		    wp->xoff + wp->sx > ctx->ox + ctx->sx) {
808 			/* Both left and right not visible. */
809 			i = ctx->ox;
810 			x = 0;
811 			width = ctx->sx;
812 		} else if (wp->xoff < ctx->ox) {
813 			/* Left not visible. */
814 			i = ctx->ox - wp->xoff;
815 			x = 0;
816 			width = wp->sx - i;
817 		} else {
818 			/* Right not visible. */
819 			i = 0;
820 			x = wp->xoff - ctx->ox;
821 			width = ctx->sx - x;
822 		}
823 		log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
824 		    __func__, c->name, wp->id, i, j, x, y, width);
825 
826 		tty_default_colours(&defaults, wp);
827 		tty_draw_line(tty, s, i, j, width, x, y, &defaults,
828 		    wp->palette);
829 	}
830 }
831