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