xref: /openbsd-src/usr.bin/tmux/tty.c (revision 3e8b1db74c103e81d872d6148750233658a8adf7)
1 /* $OpenBSD: tty.c,v 1.403 2021/08/17 11:20:13 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 #include <sys/ioctl.h>
21 
22 #include <netinet/in.h>
23 
24 #include <curses.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <resolv.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32 
33 #include "tmux.h"
34 
35 static int	tty_log_fd = -1;
36 
37 static int	tty_client_ready(struct client *);
38 
39 static void	tty_set_italics(struct tty *);
40 static int	tty_try_colour(struct tty *, int, const char *);
41 static void	tty_force_cursor_colour(struct tty *, const char *);
42 static void	tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
43 		    u_int);
44 static void	tty_cursor_pane_unless_wrap(struct tty *,
45 		    const struct tty_ctx *, u_int, u_int);
46 static void	tty_invalidate(struct tty *);
47 static void	tty_colours(struct tty *, const struct grid_cell *);
48 static void	tty_check_fg(struct tty *, struct colour_palette *,
49     		    struct grid_cell *);
50 static void	tty_check_bg(struct tty *, struct colour_palette *,
51     		    struct grid_cell *);
52 static void	tty_check_us(struct tty *, struct colour_palette *,
53     		    struct grid_cell *);
54 static void	tty_colours_fg(struct tty *, const struct grid_cell *);
55 static void	tty_colours_bg(struct tty *, const struct grid_cell *);
56 static void	tty_colours_us(struct tty *, const struct grid_cell *);
57 
58 static void	tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
59 		    u_int);
60 static void	tty_region(struct tty *, u_int, u_int);
61 static void	tty_margin_pane(struct tty *, const struct tty_ctx *);
62 static void	tty_margin(struct tty *, u_int, u_int);
63 static int	tty_large_region(struct tty *, const struct tty_ctx *);
64 static int	tty_fake_bce(const struct tty *, const struct grid_cell *,
65 		    u_int);
66 static void	tty_redraw_region(struct tty *, const struct tty_ctx *);
67 static void	tty_emulate_repeat(struct tty *, enum tty_code_code,
68 		    enum tty_code_code, u_int);
69 static void	tty_repeat_space(struct tty *, u_int);
70 static void	tty_draw_pane(struct tty *, const struct tty_ctx *, u_int);
71 static void	tty_default_attributes(struct tty *, const struct grid_cell *,
72 		    struct colour_palette *, u_int);
73 static int	tty_check_overlay(struct tty *, u_int, u_int);
74 
75 #define tty_use_margin(tty) \
76 	(tty->term->flags & TERM_DECSLRM)
77 #define tty_full_width(tty, ctx) \
78 	((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
79 
80 #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
81 #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
82 #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
83 
84 void
85 tty_create_log(void)
86 {
87 	char	name[64];
88 
89 	xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
90 
91 	tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
92 	if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
93 		fatal("fcntl failed");
94 }
95 
96 int
97 tty_init(struct tty *tty, struct client *c)
98 {
99 	if (!isatty(c->fd))
100 		return (-1);
101 
102 	memset(tty, 0, sizeof *tty);
103 	tty->client = c;
104 
105 	tty->cstyle = SCREEN_CURSOR_DEFAULT;
106 	tty->ccolour = xstrdup("");
107 
108 	if (tcgetattr(c->fd, &tty->tio) != 0)
109 		return (-1);
110 	return (0);
111 }
112 
113 void
114 tty_resize(struct tty *tty)
115 {
116 	struct client	*c = tty->client;
117 	struct winsize	 ws;
118 	u_int		 sx, sy, xpixel, ypixel;
119 
120 	if (ioctl(c->fd, TIOCGWINSZ, &ws) != -1) {
121 		sx = ws.ws_col;
122 		if (sx == 0) {
123 			sx = 80;
124 			xpixel = 0;
125 		} else
126 			xpixel = ws.ws_xpixel / sx;
127 		sy = ws.ws_row;
128 		if (sy == 0) {
129 			sy = 24;
130 			ypixel = 0;
131 		} else
132 			ypixel = ws.ws_ypixel / sy;
133 	} else {
134 		sx = 80;
135 		sy = 24;
136 		xpixel = 0;
137 		ypixel = 0;
138 	}
139 	log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy,
140 	    xpixel, ypixel);
141 	tty_set_size(tty, sx, sy, xpixel, ypixel);
142 	tty_invalidate(tty);
143 }
144 
145 void
146 tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
147 {
148 	tty->sx = sx;
149 	tty->sy = sy;
150 	tty->xpixel = xpixel;
151 	tty->ypixel = ypixel;
152 }
153 
154 static void
155 tty_read_callback(__unused int fd, __unused short events, void *data)
156 {
157 	struct tty	*tty = data;
158 	struct client	*c = tty->client;
159 	const char	*name = c->name;
160 	size_t		 size = EVBUFFER_LENGTH(tty->in);
161 	int		 nread;
162 
163 	nread = evbuffer_read(tty->in, c->fd, -1);
164 	if (nread == 0 || nread == -1) {
165 		if (nread == 0)
166 			log_debug("%s: read closed", name);
167 		else
168 			log_debug("%s: read error: %s", name, strerror(errno));
169 		event_del(&tty->event_in);
170 		server_client_lost(tty->client);
171 		return;
172 	}
173 	log_debug("%s: read %d bytes (already %zu)", name, nread, size);
174 
175 	while (tty_keys_next(tty))
176 		;
177 }
178 
179 static void
180 tty_timer_callback(__unused int fd, __unused short events, void *data)
181 {
182 	struct tty	*tty = data;
183 	struct client	*c = tty->client;
184 	struct timeval	 tv = { .tv_usec = TTY_BLOCK_INTERVAL };
185 
186 	log_debug("%s: %zu discarded", c->name, tty->discarded);
187 
188 	c->flags |= CLIENT_ALLREDRAWFLAGS;
189 	c->discarded += tty->discarded;
190 
191 	if (tty->discarded < TTY_BLOCK_STOP(tty)) {
192 		tty->flags &= ~TTY_BLOCK;
193 		tty_invalidate(tty);
194 		return;
195 	}
196 	tty->discarded = 0;
197 	evtimer_add(&tty->timer, &tv);
198 }
199 
200 static int
201 tty_block_maybe(struct tty *tty)
202 {
203 	struct client	*c = tty->client;
204 	size_t		 size = EVBUFFER_LENGTH(tty->out);
205 	struct timeval	 tv = { .tv_usec = TTY_BLOCK_INTERVAL };
206 
207 	if (size < TTY_BLOCK_START(tty))
208 		return (0);
209 
210 	if (tty->flags & TTY_BLOCK)
211 		return (1);
212 	tty->flags |= TTY_BLOCK;
213 
214 	log_debug("%s: can't keep up, %zu discarded", c->name, size);
215 
216 	evbuffer_drain(tty->out, size);
217 	c->discarded += size;
218 
219 	tty->discarded = 0;
220 	evtimer_add(&tty->timer, &tv);
221 	return (1);
222 }
223 
224 static void
225 tty_write_callback(__unused int fd, __unused short events, void *data)
226 {
227 	struct tty	*tty = data;
228 	struct client	*c = tty->client;
229 	size_t		 size = EVBUFFER_LENGTH(tty->out);
230 	int		 nwrite;
231 
232 	nwrite = evbuffer_write(tty->out, c->fd);
233 	if (nwrite == -1)
234 		return;
235 	log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
236 
237 	if (c->redraw > 0) {
238 		if ((size_t)nwrite >= c->redraw)
239 			c->redraw = 0;
240 		else
241 			c->redraw -= nwrite;
242 		log_debug("%s: waiting for redraw, %zu bytes left", c->name,
243 		    c->redraw);
244 	} else if (tty_block_maybe(tty))
245 		return;
246 
247 	if (EVBUFFER_LENGTH(tty->out) != 0)
248 		event_add(&tty->event_out, NULL);
249 }
250 
251 int
252 tty_open(struct tty *tty, char **cause)
253 {
254 	struct client	*c = tty->client;
255 
256 	tty->term = tty_term_create(tty, c->term_name, c->term_caps,
257 	    c->term_ncaps, &c->term_features, cause);
258 	if (tty->term == NULL) {
259 		tty_close(tty);
260 		return (-1);
261 	}
262 	tty->flags |= TTY_OPENED;
263 
264 	tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
265 
266 	event_set(&tty->event_in, c->fd, EV_PERSIST|EV_READ,
267 	    tty_read_callback, tty);
268 	tty->in = evbuffer_new();
269 	if (tty->in == NULL)
270 		fatal("out of memory");
271 
272 	event_set(&tty->event_out, c->fd, EV_WRITE, tty_write_callback, tty);
273 	tty->out = evbuffer_new();
274 	if (tty->out == NULL)
275 		fatal("out of memory");
276 
277 	evtimer_set(&tty->timer, tty_timer_callback, tty);
278 
279 	tty_start_tty(tty);
280 
281 	tty_keys_build(tty);
282 
283 	return (0);
284 }
285 
286 static void
287 tty_start_timer_callback(__unused int fd, __unused short events, void *data)
288 {
289 	struct tty	*tty = data;
290 	struct client	*c = tty->client;
291 
292 	log_debug("%s: start timer fired", c->name);
293 	if ((tty->flags & (TTY_HAVEDA|TTY_HAVEXDA)) == 0)
294 		tty_update_features(tty);
295 	tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
296 }
297 
298 void
299 tty_start_tty(struct tty *tty)
300 {
301 	struct client	*c = tty->client;
302 	struct termios	 tio;
303 	struct timeval	 tv = { .tv_sec = 1 };
304 
305 	setblocking(c->fd, 0);
306 	event_add(&tty->event_in, NULL);
307 
308 	memcpy(&tio, &tty->tio, sizeof tio);
309 	tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
310 	tio.c_iflag |= IGNBRK;
311 	tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
312 	tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|ECHOPRT|
313 	    ECHOKE|ISIG);
314 	tio.c_cc[VMIN] = 1;
315 	tio.c_cc[VTIME] = 0;
316 	if (tcsetattr(c->fd, TCSANOW, &tio) == 0)
317 		tcflush(c->fd, TCIOFLUSH);
318 
319 	tty_putcode(tty, TTYC_SMCUP);
320 
321 	tty_putcode(tty, TTYC_SMKX);
322 	tty_putcode(tty, TTYC_CLEAR);
323 
324 	if (tty_acs_needed(tty)) {
325 		log_debug("%s: using capabilities for ACS", c->name);
326 		tty_putcode(tty, TTYC_ENACS);
327 	} else
328 		log_debug("%s: using UTF-8 for ACS", c->name);
329 
330 	tty_putcode(tty, TTYC_CNORM);
331 	if (tty_term_has(tty->term, TTYC_KMOUS)) {
332 		tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
333 		tty_puts(tty, "\033[?1006l\033[?1005l");
334 	}
335 
336 	evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
337 	evtimer_add(&tty->start_timer, &tv);
338 
339 	tty->flags |= TTY_STARTED;
340 	tty_invalidate(tty);
341 
342 	if (*tty->ccolour != '\0')
343 		tty_force_cursor_colour(tty, "");
344 
345 	tty->mouse_drag_flag = 0;
346 	tty->mouse_drag_update = NULL;
347 	tty->mouse_drag_release = NULL;
348 }
349 
350 void
351 tty_send_requests(struct tty *tty)
352 {
353 	if (~tty->flags & TTY_STARTED)
354 		return;
355 
356 	if (tty->term->flags & TERM_VT100LIKE) {
357 		if (~tty->flags & TTY_HAVEDA)
358 			tty_puts(tty, "\033[>c");
359 		if (~tty->flags & TTY_HAVEXDA)
360 			tty_puts(tty, "\033[>q");
361 	} else
362 		tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
363 }
364 
365 void
366 tty_stop_tty(struct tty *tty)
367 {
368 	struct client	*c = tty->client;
369 	struct winsize	 ws;
370 
371 	if (!(tty->flags & TTY_STARTED))
372 		return;
373 	tty->flags &= ~TTY_STARTED;
374 
375 	evtimer_del(&tty->start_timer);
376 
377 	event_del(&tty->timer);
378 	tty->flags &= ~TTY_BLOCK;
379 
380 	event_del(&tty->event_in);
381 	event_del(&tty->event_out);
382 
383 	/*
384 	 * Be flexible about error handling and try not kill the server just
385 	 * because the fd is invalid. Things like ssh -t can easily leave us
386 	 * with a dead tty.
387 	 */
388 	if (ioctl(c->fd, TIOCGWINSZ, &ws) == -1)
389 		return;
390 	if (tcsetattr(c->fd, TCSANOW, &tty->tio) == -1)
391 		return;
392 
393 	tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
394 	if (tty_acs_needed(tty))
395 		tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
396 	tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
397 	tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
398 	tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
399 	if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
400 		if (tty_term_has(tty->term, TTYC_SE))
401 			tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
402 		else if (tty_term_has(tty->term, TTYC_SS))
403 			tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
404 	}
405 	if (tty->mode & MODE_BRACKETPASTE)
406 		tty_raw(tty, tty_term_string(tty->term, TTYC_DSBP));
407 	if (*tty->ccolour != '\0')
408 		tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
409 
410 	tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
411 	if (tty_term_has(tty->term, TTYC_KMOUS)) {
412 		tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
413 		tty_raw(tty, "\033[?1006l\033[?1005l");
414 	}
415 
416 	if (tty->term->flags & TERM_VT100LIKE)
417 		tty_raw(tty, "\033[?7727l");
418 	tty_raw(tty, tty_term_string(tty->term, TTYC_DSFCS));
419 	tty_raw(tty, tty_term_string(tty->term, TTYC_DSEKS));
420 
421 	if (tty_use_margin(tty))
422 		tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG));
423 	tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
424 
425 	setblocking(c->fd, 1);
426 }
427 
428 void
429 tty_close(struct tty *tty)
430 {
431 	if (event_initialized(&tty->key_timer))
432 		evtimer_del(&tty->key_timer);
433 	tty_stop_tty(tty);
434 
435 	if (tty->flags & TTY_OPENED) {
436 		evbuffer_free(tty->in);
437 		event_del(&tty->event_in);
438 		evbuffer_free(tty->out);
439 		event_del(&tty->event_out);
440 
441 		tty_term_free(tty->term);
442 		tty_keys_free(tty);
443 
444 		tty->flags &= ~TTY_OPENED;
445 	}
446 }
447 
448 void
449 tty_free(struct tty *tty)
450 {
451 	tty_close(tty);
452 	free(tty->ccolour);
453 }
454 
455 void
456 tty_update_features(struct tty *tty)
457 {
458 	struct client	*c = tty->client;
459 
460 	if (tty_apply_features(tty->term, c->term_features))
461 		tty_term_apply_overrides(tty->term);
462 
463 	if (tty_use_margin(tty))
464 		tty_putcode(tty, TTYC_ENMG);
465 	if (options_get_number(global_options, "extended-keys"))
466 		tty_puts(tty, tty_term_string(tty->term, TTYC_ENEKS));
467 	if (options_get_number(global_options, "focus-events"))
468 		tty_puts(tty, tty_term_string(tty->term, TTYC_ENFCS));
469 	if (tty->term->flags & TERM_VT100LIKE)
470 		tty_puts(tty, "\033[?7727h");
471 }
472 
473 void
474 tty_raw(struct tty *tty, const char *s)
475 {
476 	struct client	*c = tty->client;
477 	ssize_t		 n, slen;
478 	u_int		 i;
479 
480 	slen = strlen(s);
481 	for (i = 0; i < 5; i++) {
482 		n = write(c->fd, s, slen);
483 		if (n >= 0) {
484 			s += n;
485 			slen -= n;
486 			if (slen == 0)
487 				break;
488 		} else if (n == -1 && errno != EAGAIN)
489 			break;
490 		usleep(100);
491 	}
492 }
493 
494 void
495 tty_putcode(struct tty *tty, enum tty_code_code code)
496 {
497 	tty_puts(tty, tty_term_string(tty->term, code));
498 }
499 
500 void
501 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
502 {
503 	if (a < 0)
504 		return;
505 	tty_puts(tty, tty_term_string1(tty->term, code, a));
506 }
507 
508 void
509 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
510 {
511 	if (a < 0 || b < 0)
512 		return;
513 	tty_puts(tty, tty_term_string2(tty->term, code, a, b));
514 }
515 
516 void
517 tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
518 {
519 	if (a < 0 || b < 0 || c < 0)
520 		return;
521 	tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
522 }
523 
524 void
525 tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
526 {
527 	if (a != NULL)
528 		tty_puts(tty, tty_term_ptr1(tty->term, code, a));
529 }
530 
531 void
532 tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
533     const void *b)
534 {
535 	if (a != NULL && b != NULL)
536 		tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
537 }
538 
539 static void
540 tty_add(struct tty *tty, const char *buf, size_t len)
541 {
542 	struct client	*c = tty->client;
543 
544 	if (tty->flags & TTY_BLOCK) {
545 		tty->discarded += len;
546 		return;
547 	}
548 
549 	evbuffer_add(tty->out, buf, len);
550 	log_debug("%s: %.*s", c->name, (int)len, buf);
551 	c->written += len;
552 
553 	if (tty_log_fd != -1)
554 		write(tty_log_fd, buf, len);
555 	if (tty->flags & TTY_STARTED)
556 		event_add(&tty->event_out, NULL);
557 }
558 
559 void
560 tty_puts(struct tty *tty, const char *s)
561 {
562 	if (*s != '\0')
563 		tty_add(tty, s, strlen(s));
564 }
565 
566 void
567 tty_putc(struct tty *tty, u_char ch)
568 {
569 	const char	*acs;
570 
571 	if ((tty->term->flags & TERM_NOAM) &&
572 	    ch >= 0x20 && ch != 0x7f &&
573 	    tty->cy == tty->sy - 1 &&
574 	    tty->cx + 1 >= tty->sx)
575 		return;
576 
577 	if (tty->cell.attr & GRID_ATTR_CHARSET) {
578 		acs = tty_acs_get(tty, ch);
579 		if (acs != NULL)
580 			tty_add(tty, acs, strlen(acs));
581 		else
582 			tty_add(tty, &ch, 1);
583 	} else
584 		tty_add(tty, &ch, 1);
585 
586 	if (ch >= 0x20 && ch != 0x7f) {
587 		if (tty->cx >= tty->sx) {
588 			tty->cx = 1;
589 			if (tty->cy != tty->rlower)
590 				tty->cy++;
591 
592 			/*
593 			 * On !am terminals, force the cursor position to where
594 			 * we think it should be after a line wrap - this means
595 			 * it works on sensible terminals as well.
596 			 */
597 			if (tty->term->flags & TERM_NOAM)
598 				tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
599 		} else
600 			tty->cx++;
601 	}
602 }
603 
604 void
605 tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
606 {
607 	if ((tty->term->flags & TERM_NOAM) &&
608 	    tty->cy == tty->sy - 1 &&
609 	    tty->cx + len >= tty->sx)
610 		len = tty->sx - tty->cx - 1;
611 
612 	tty_add(tty, buf, len);
613 	if (tty->cx + width > tty->sx) {
614 		tty->cx = (tty->cx + width) - tty->sx;
615 		if (tty->cx <= tty->sx)
616 			tty->cy++;
617 		else
618 			tty->cx = tty->cy = UINT_MAX;
619 	} else
620 		tty->cx += width;
621 }
622 
623 static void
624 tty_set_italics(struct tty *tty)
625 {
626 	const char	*s;
627 
628 	if (tty_term_has(tty->term, TTYC_SITM)) {
629 		s = options_get_string(global_options, "default-terminal");
630 		if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
631 			tty_putcode(tty, TTYC_SITM);
632 			return;
633 		}
634 	}
635 	tty_putcode(tty, TTYC_SMSO);
636 }
637 
638 void
639 tty_set_title(struct tty *tty, const char *title)
640 {
641 	if (!tty_term_has(tty->term, TTYC_TSL) ||
642 	    !tty_term_has(tty->term, TTYC_FSL))
643 		return;
644 
645 	tty_putcode(tty, TTYC_TSL);
646 	tty_puts(tty, title);
647 	tty_putcode(tty, TTYC_FSL);
648 }
649 
650 static void
651 tty_force_cursor_colour(struct tty *tty, const char *ccolour)
652 {
653 	if (*ccolour == '\0')
654 		tty_putcode(tty, TTYC_CR);
655 	else
656 		tty_putcode_ptr1(tty, TTYC_CS, ccolour);
657 	free(tty->ccolour);
658 	tty->ccolour = xstrdup(ccolour);
659 }
660 
661 void
662 tty_update_mode(struct tty *tty, int mode, struct screen *s)
663 {
664 	struct client		*c = tty->client;
665 	int			 changed;
666 	enum screen_cursor_style cstyle = tty->cstyle;
667 
668 	if (tty->flags & TTY_NOCURSOR)
669 		mode &= ~MODE_CURSOR;
670 
671 	changed = mode ^ tty->mode;
672 	if (log_get_level() != 0 && changed != 0) {
673 		log_debug("%s: current mode %s", c->name,
674 		    screen_mode_to_string(tty->mode));
675 		log_debug("%s: setting mode %s", c->name,
676 		    screen_mode_to_string(mode));
677 	}
678 
679 	if (s != NULL) {
680 		if (strcmp(s->ccolour, tty->ccolour) != 0)
681 			tty_force_cursor_colour(tty, s->ccolour);
682 		cstyle = s->cstyle;
683 	}
684 	if (~mode & MODE_CURSOR) {
685 		/* Cursor now off - set as invisible. */
686 		if (changed & MODE_CURSOR)
687 			tty_putcode(tty, TTYC_CIVIS);
688 	} else if ((changed & (MODE_CURSOR|MODE_BLINKING)) ||
689 	    cstyle != tty->cstyle) {
690 		/*
691 		 * Cursor now on, blinking flag changed or style changed. Start
692 		 * by setting the cursor to normal.
693 		 */
694 		tty_putcode(tty, TTYC_CNORM);
695 		switch (cstyle) {
696 		case SCREEN_CURSOR_DEFAULT:
697 			/*
698 			 * If the old style wasn't default, then reset it to
699 			 * default.
700 			 */
701 			if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
702 				if (tty_term_has(tty->term, TTYC_SE))
703 					tty_putcode(tty, TTYC_SE);
704 				else
705 					tty_putcode1(tty, TTYC_SS, 0);
706 			}
707 
708 			/* Set the cursor as very visible if necessary. */
709 			if (mode & MODE_BLINKING)
710 				tty_putcode(tty, TTYC_CVVIS);
711 			break;
712 		case SCREEN_CURSOR_BLOCK:
713 			/*
714 			 * Set style to either block blinking (1) or steady (2)
715 			 * if supported, otherwise just check the blinking
716 			 * flag.
717 			 */
718 			if (tty_term_has(tty->term, TTYC_SS)) {
719 				if (mode & MODE_BLINKING)
720 					tty_putcode1(tty, TTYC_SS, 1);
721 				else
722 					tty_putcode1(tty, TTYC_SS, 2);
723 			} else if (mode & MODE_BLINKING)
724 				tty_putcode(tty, TTYC_CVVIS);
725 			break;
726 		case SCREEN_CURSOR_UNDERLINE:
727 			/*
728 			 * Set style to either underline blinking (3) or steady
729 			 * (4) if supported, otherwise just check the blinking
730 			 * flag.
731 			 */
732 			if (tty_term_has(tty->term, TTYC_SS)) {
733 				if (mode & MODE_BLINKING)
734 					tty_putcode1(tty, TTYC_SS, 3);
735 				else
736 					tty_putcode1(tty, TTYC_SS, 4);
737 			} else if (mode & MODE_BLINKING)
738 				tty_putcode(tty, TTYC_CVVIS);
739 			break;
740 		case SCREEN_CURSOR_BAR:
741 			/*
742 			 * Set style to either bar blinking (5) or steady (6)
743 			 * if supported, otherwise just check the blinking
744 			 * flag.
745 			 */
746 			if (tty_term_has(tty->term, TTYC_SS)) {
747 				if (mode & MODE_BLINKING)
748 					tty_putcode1(tty, TTYC_SS, 5);
749 				else
750 					tty_putcode1(tty, TTYC_SS, 6);
751 			} else if (mode & MODE_BLINKING)
752 				tty_putcode(tty, TTYC_CVVIS);
753 			break;
754 		}
755 		tty->cstyle = cstyle;
756 	}
757 
758 	if ((changed & ALL_MOUSE_MODES) &&
759 	    tty_term_has(tty->term, TTYC_KMOUS)) {
760 		/*
761 		 * If the mouse modes have changed, clear any that are set and
762 		 * apply again. There are differences in how terminals track
763 		 * the various bits.
764 		 */
765 		if (tty->mode & MODE_MOUSE_SGR)
766 			tty_puts(tty, "\033[?1006l");
767 		if (tty->mode & MODE_MOUSE_STANDARD)
768 			tty_puts(tty, "\033[?1000l");
769 		if (tty->mode & MODE_MOUSE_BUTTON)
770 			tty_puts(tty, "\033[?1002l");
771 		if (tty->mode & MODE_MOUSE_ALL)
772 			tty_puts(tty, "\033[?1003l");
773 		if (mode & ALL_MOUSE_MODES)
774 			tty_puts(tty, "\033[?1006h");
775 		if (mode & MODE_MOUSE_STANDARD)
776 			tty_puts(tty, "\033[?1000h");
777 		if (mode & MODE_MOUSE_BUTTON)
778 			tty_puts(tty, "\033[?1002h");
779 		if (mode & MODE_MOUSE_ALL)
780 			tty_puts(tty, "\033[?1003h");
781 	}
782 	if (changed & MODE_BRACKETPASTE) {
783 		if (mode & MODE_BRACKETPASTE)
784 			tty_putcode(tty, TTYC_ENBP);
785 		else
786 			tty_putcode(tty, TTYC_DSBP);
787 	}
788 	tty->mode = mode;
789 }
790 
791 static void
792 tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
793     enum tty_code_code code1, u_int n)
794 {
795 	if (tty_term_has(tty->term, code))
796 		tty_putcode1(tty, code, n);
797 	else {
798 		while (n-- > 0)
799 			tty_putcode(tty, code1);
800 	}
801 }
802 
803 static void
804 tty_repeat_space(struct tty *tty, u_int n)
805 {
806 	static char s[500];
807 
808 	if (*s != ' ')
809 		memset(s, ' ', sizeof s);
810 
811 	while (n > sizeof s) {
812 		tty_putn(tty, s, sizeof s, sizeof s);
813 		n -= sizeof s;
814 	}
815 	if (n != 0)
816 		tty_putn(tty, s, n, n);
817 }
818 
819 /* Is this window bigger than the terminal? */
820 int
821 tty_window_bigger(struct tty *tty)
822 {
823 	struct client	*c = tty->client;
824 	struct window	*w = c->session->curw->window;
825 
826 	return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
827 }
828 
829 /* What offset should this window be drawn at? */
830 int
831 tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
832 {
833 	*ox = tty->oox;
834 	*oy = tty->ooy;
835 	*sx = tty->osx;
836 	*sy = tty->osy;
837 
838 	return (tty->oflag);
839 }
840 
841 /* What offset should this window be drawn at? */
842 static int
843 tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
844 {
845 	struct client		*c = tty->client;
846 	struct window		*w = c->session->curw->window;
847 	struct window_pane	*wp = server_client_get_pane(c);
848 	u_int			 cx, cy, lines;
849 
850 	lines = status_line_size(c);
851 
852 	if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
853 		*ox = 0;
854 		*oy = 0;
855 		*sx = w->sx;
856 		*sy = w->sy;
857 
858 		c->pan_window = NULL;
859 		return (0);
860 	}
861 
862 	*sx = tty->sx;
863 	*sy = tty->sy - lines;
864 
865 	if (c->pan_window == w) {
866 		if (*sx >= w->sx)
867 			c->pan_ox = 0;
868 		else if (c->pan_ox + *sx > w->sx)
869 			c->pan_ox = w->sx - *sx;
870 		*ox = c->pan_ox;
871 		if (*sy >= w->sy)
872 			c->pan_oy = 0;
873 		else if (c->pan_oy + *sy > w->sy)
874 			c->pan_oy = w->sy - *sy;
875 		*oy = c->pan_oy;
876 		return (1);
877 	}
878 
879 	if (~wp->screen->mode & MODE_CURSOR) {
880 		*ox = 0;
881 		*oy = 0;
882 	} else {
883 		cx = wp->xoff + wp->screen->cx;
884 		cy = wp->yoff + wp->screen->cy;
885 
886 		if (cx < *sx)
887 			*ox = 0;
888 		else if (cx > w->sx - *sx)
889 			*ox = w->sx - *sx;
890 		else
891 			*ox = cx - *sx / 2;
892 
893 		if (cy < *sy)
894 			*oy = 0;
895 		else if (cy > w->sy - *sy)
896 			*oy = w->sy - *sy;
897 		else
898 			*oy = cy - *sy / 2;
899 	}
900 
901 	c->pan_window = NULL;
902 	return (1);
903 }
904 
905 /* Update stored offsets for a window and redraw if necessary. */
906 void
907 tty_update_window_offset(struct window *w)
908 {
909 	struct client	*c;
910 
911 	TAILQ_FOREACH(c, &clients, entry) {
912 		if (c->session != NULL && c->session->curw->window == w)
913 			tty_update_client_offset(c);
914 	}
915 }
916 
917 /* Update stored offsets for a client and redraw if necessary. */
918 void
919 tty_update_client_offset(struct client *c)
920 {
921 	u_int	ox, oy, sx, sy;
922 
923 	if (~c->flags & CLIENT_TERMINAL)
924 		return;
925 
926 	c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
927 	if (ox == c->tty.oox &&
928 	    oy == c->tty.ooy &&
929 	    sx == c->tty.osx &&
930 	    sy == c->tty.osy)
931 		return;
932 
933 	log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
934 	    __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
935 	    ox, oy, sx, sy);
936 
937 	c->tty.oox = ox;
938 	c->tty.ooy = oy;
939 	c->tty.osx = sx;
940 	c->tty.osy = sy;
941 
942 	c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
943 }
944 
945 /*
946  * Is the region large enough to be worth redrawing once later rather than
947  * probably several times now? Currently yes if it is more than 50% of the
948  * pane.
949  */
950 static int
951 tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
952 {
953 	return (ctx->orlower - ctx->orupper >= ctx->sy / 2);
954 }
955 
956 /*
957  * Return if BCE is needed but the terminal doesn't have it - it'll need to be
958  * emulated.
959  */
960 static int
961 tty_fake_bce(const struct tty *tty, const struct grid_cell *gc, u_int bg)
962 {
963 	if (tty_term_flag(tty->term, TTYC_BCE))
964 		return (0);
965 	if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc->bg))
966 		return (1);
967 	return (0);
968 }
969 
970 /*
971  * Redraw scroll region using data from screen (already updated). Used when
972  * CSR not supported, or window is a pane that doesn't take up the full
973  * width of the terminal.
974  */
975 static void
976 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
977 {
978 	struct client		*c = tty->client;
979 	u_int			 i;
980 
981 	/*
982 	 * If region is large, schedule a redraw. In most cases this is likely
983 	 * to be followed by some more scrolling.
984 	 */
985 	if (tty_large_region(tty, ctx)) {
986 		log_debug("%s: %s large redraw", __func__, c->name);
987 		ctx->redraw_cb(ctx);
988 		return;
989 	}
990 
991 	for (i = ctx->orupper; i <= ctx->orlower; i++)
992 		tty_draw_pane(tty, ctx, i);
993 }
994 
995 /* Is this position visible in the pane? */
996 static int
997 tty_is_visible(__unused struct tty *tty, const struct tty_ctx *ctx, u_int px,
998     u_int py, u_int nx, u_int ny)
999 {
1000 	u_int	xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1001 
1002 	if (!ctx->bigger)
1003 		return (1);
1004 
1005 	if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx ||
1006 	    yoff + ny <= ctx->woy || yoff >= ctx->woy + ctx->wsy)
1007 		return (0);
1008 	return (1);
1009 }
1010 
1011 /* Clamp line position to visible part of pane. */
1012 static int
1013 tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1014     u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
1015 {
1016 	u_int	xoff = ctx->rxoff + px;
1017 
1018 	if (!tty_is_visible(tty, ctx, px, py, nx, 1))
1019 		return (0);
1020 	*ry = ctx->yoff + py - ctx->woy;
1021 
1022 	if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1023 		/* All visible. */
1024 		*i = 0;
1025 		*x = ctx->xoff + px - ctx->wox;
1026 		*rx = nx;
1027 	} else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1028 		/* Both left and right not visible. */
1029 		*i = ctx->wox;
1030 		*x = 0;
1031 		*rx = ctx->wsx;
1032 	} else if (xoff < ctx->wox) {
1033 		/* Left not visible. */
1034 		*i = ctx->wox - (ctx->xoff + px);
1035 		*x = 0;
1036 		*rx = nx - *i;
1037 	} else {
1038 		/* Right not visible. */
1039 		*i = 0;
1040 		*x = (ctx->xoff + px) - ctx->wox;
1041 		*rx = ctx->wsx - *x;
1042 	}
1043 	if (*rx > nx)
1044 		fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1045 
1046 	return (1);
1047 }
1048 
1049 /* Clear a line. */
1050 static void
1051 tty_clear_line(struct tty *tty, const struct grid_cell *defaults, u_int py,
1052     u_int px, u_int nx, u_int bg)
1053 {
1054 	struct client	*c = tty->client;
1055 	u_int		 i;
1056 
1057 	log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1058 
1059 	/* Nothing to clear. */
1060 	if (nx == 0)
1061 		return;
1062 
1063 	/* If genuine BCE is available, can try escape sequences. */
1064 	if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1065 		/* Off the end of the line, use EL if available. */
1066 		if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
1067 			tty_cursor(tty, px, py);
1068 			tty_putcode(tty, TTYC_EL);
1069 			return;
1070 		}
1071 
1072 		/* At the start of the line. Use EL1. */
1073 		if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1074 			tty_cursor(tty, px + nx - 1, py);
1075 			tty_putcode(tty, TTYC_EL1);
1076 			return;
1077 		}
1078 
1079 		/* Section of line. Use ECH if possible. */
1080 		if (tty_term_has(tty->term, TTYC_ECH)) {
1081 			tty_cursor(tty, px, py);
1082 			tty_putcode1(tty, TTYC_ECH, nx);
1083 			return;
1084 		}
1085 	}
1086 
1087 	/*
1088 	 * Couldn't use an escape sequence, use spaces. Clear only the visible
1089 	 * bit if there is an overlay.
1090 	 */
1091 	for (i = 0; i < nx; i++) {
1092 		if (!tty_check_overlay(tty, px + i, py))
1093 			break;
1094 	}
1095 	tty_cursor(tty, px, py);
1096 	tty_repeat_space(tty, i);
1097 	for (; i < nx; i++) {
1098 		if (tty_check_overlay(tty, px + i, py))
1099 			break;
1100 	}
1101 	tty_cursor(tty, px + i, py);
1102 	tty_repeat_space(tty, nx - i);
1103 }
1104 
1105 /* Clear a line, adjusting to visible part of pane. */
1106 static void
1107 tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1108     u_int px, u_int nx, u_int bg)
1109 {
1110 	struct client	*c = tty->client;
1111 	u_int		 i, x, rx, ry;
1112 
1113 	log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1114 
1115 	if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1116 		tty_clear_line(tty, &ctx->defaults, ry, x, rx, bg);
1117 }
1118 
1119 /* Clamp area position to visible part of pane. */
1120 static int
1121 tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1122     u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1123     u_int *ry)
1124 {
1125 	u_int	xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1126 
1127 	if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1128 		return (0);
1129 
1130 	if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1131 		/* All visible. */
1132 		*i = 0;
1133 		*x = ctx->xoff + px - ctx->wox;
1134 		*rx = nx;
1135 	} else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1136 		/* Both left and right not visible. */
1137 		*i = ctx->wox;
1138 		*x = 0;
1139 		*rx = ctx->wsx;
1140 	} else if (xoff < ctx->wox) {
1141 		/* Left not visible. */
1142 		*i = ctx->wox - (ctx->xoff + px);
1143 		*x = 0;
1144 		*rx = nx - *i;
1145 	} else {
1146 		/* Right not visible. */
1147 		*i = 0;
1148 		*x = (ctx->xoff + px) - ctx->wox;
1149 		*rx = ctx->wsx - *x;
1150 	}
1151 	if (*rx > nx)
1152 		fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1153 
1154 	if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) {
1155 		/* All visible. */
1156 		*j = 0;
1157 		*y = ctx->yoff + py - ctx->woy;
1158 		*ry = ny;
1159 	} else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) {
1160 		/* Both top and bottom not visible. */
1161 		*j = ctx->woy;
1162 		*y = 0;
1163 		*ry = ctx->wsy;
1164 	} else if (yoff < ctx->woy) {
1165 		/* Top not visible. */
1166 		*j = ctx->woy - (ctx->yoff + py);
1167 		*y = 0;
1168 		*ry = ny - *j;
1169 	} else {
1170 		/* Bottom not visible. */
1171 		*j = 0;
1172 		*y = (ctx->yoff + py) - ctx->woy;
1173 		*ry = ctx->wsy - *y;
1174 	}
1175 	if (*ry > ny)
1176 		fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1177 
1178 	return (1);
1179 }
1180 
1181 /* Clear an area, adjusting to visible part of pane. */
1182 static void
1183 tty_clear_area(struct tty *tty, const struct grid_cell *defaults, u_int py,
1184     u_int ny, u_int px, u_int nx, u_int bg)
1185 {
1186 	struct client	*c = tty->client;
1187 	u_int		 yy;
1188 	char		 tmp[64];
1189 
1190 	log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1191 
1192 	/* Nothing to clear. */
1193 	if (nx == 0 || ny == 0)
1194 		return;
1195 
1196 	/* If genuine BCE is available, can try escape sequences. */
1197 	if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1198 		/* Use ED if clearing off the bottom of the terminal. */
1199 		if (px == 0 &&
1200 		    px + nx >= tty->sx &&
1201 		    py + ny >= tty->sy &&
1202 		    tty_term_has(tty->term, TTYC_ED)) {
1203 			tty_cursor(tty, 0, py);
1204 			tty_putcode(tty, TTYC_ED);
1205 			return;
1206 		}
1207 
1208 		/*
1209 		 * On VT420 compatible terminals we can use DECFRA if the
1210 		 * background colour isn't default (because it doesn't work
1211 		 * after SGR 0).
1212 		 */
1213 		if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1214 			xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1215 			    py + 1, px + 1, py + ny, px + nx);
1216 			tty_puts(tty, tmp);
1217 			return;
1218 		}
1219 
1220 		/* Full lines can be scrolled away to clear them. */
1221 		if (px == 0 &&
1222 		    px + nx >= tty->sx &&
1223 		    ny > 2 &&
1224 		    tty_term_has(tty->term, TTYC_CSR) &&
1225 		    tty_term_has(tty->term, TTYC_INDN)) {
1226 			tty_region(tty, py, py + ny - 1);
1227 			tty_margin_off(tty);
1228 			tty_putcode1(tty, TTYC_INDN, ny);
1229 			return;
1230 		}
1231 
1232 		/*
1233 		 * If margins are supported, can just scroll the area off to
1234 		 * clear it.
1235 		 */
1236 		if (nx > 2 &&
1237 		    ny > 2 &&
1238 		    tty_term_has(tty->term, TTYC_CSR) &&
1239 		    tty_use_margin(tty) &&
1240 		    tty_term_has(tty->term, TTYC_INDN)) {
1241 			tty_region(tty, py, py + ny - 1);
1242 			tty_margin(tty, px, px + nx - 1);
1243 			tty_putcode1(tty, TTYC_INDN, ny);
1244 			return;
1245 		}
1246 	}
1247 
1248 	/* Couldn't use an escape sequence, loop over the lines. */
1249 	for (yy = py; yy < py + ny; yy++)
1250 		tty_clear_line(tty, defaults, yy, px, nx, bg);
1251 }
1252 
1253 /* Clear an area in a pane. */
1254 static void
1255 tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1256     u_int ny, u_int px, u_int nx, u_int bg)
1257 {
1258 	u_int	i, j, x, y, rx, ry;
1259 
1260 	if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1261 		tty_clear_area(tty, &ctx->defaults, y, ry, x, rx, bg);
1262 }
1263 
1264 static void
1265 tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1266 {
1267 	struct screen	*s = ctx->s;
1268 	u_int		 nx = ctx->sx, i, x, rx, ry;
1269 
1270 	log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1271 
1272 	if (!ctx->bigger) {
1273 		tty_draw_line(tty, s, 0, py, nx, ctx->xoff, ctx->yoff + py,
1274 		    &ctx->defaults, ctx->palette);
1275 		return;
1276 	}
1277 	if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry)) {
1278 		tty_draw_line(tty, s, i, py, rx, x, ry, &ctx->defaults,
1279 		    ctx->palette);
1280 	}
1281 }
1282 
1283 static const struct grid_cell *
1284 tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1285 {
1286 	static struct grid_cell	new;
1287 	int			c;
1288 
1289 	/* Characters less than 0x7f are always fine, no matter what. */
1290 	if (gc->data.size == 1 && *gc->data.data < 0x7f)
1291 		return (gc);
1292 
1293 	/* UTF-8 terminal and a UTF-8 character - fine. */
1294 	if (tty->client->flags & CLIENT_UTF8)
1295 		return (gc);
1296 	memcpy(&new, gc, sizeof new);
1297 
1298 	/* See if this can be mapped to an ACS character. */
1299 	c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size);
1300 	if (c != -1) {
1301 		utf8_set(&new.data, c);
1302 		new.attr |= GRID_ATTR_CHARSET;
1303 		return (&new);
1304 	}
1305 
1306 	/* Replace by the right number of underscores. */
1307 	new.data.size = gc->data.width;
1308 	if (new.data.size > UTF8_SIZE)
1309 		new.data.size = UTF8_SIZE;
1310 	memset(new.data.data, '_', new.data.size);
1311 	return (&new);
1312 }
1313 
1314 static int
1315 tty_check_overlay(struct tty *tty, u_int px, u_int py)
1316 {
1317 	struct client	*c = tty->client;
1318 
1319 	if (c->overlay_check == NULL)
1320 		return (1);
1321 	return (c->overlay_check(c, c->overlay_data, px, py));
1322 }
1323 
1324 void
1325 tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
1326     u_int atx, u_int aty, const struct grid_cell *defaults,
1327     struct colour_palette *palette)
1328 {
1329 	struct grid		*gd = s->grid;
1330 	struct grid_cell	 gc, last;
1331 	const struct grid_cell	*gcp;
1332 	struct grid_line	*gl;
1333 	struct client		*c = tty->client;
1334 	u_int			 i, j, ux, sx, width, hidden;
1335 	int			 flags, cleared = 0, wrapped = 0;
1336 	char			 buf[512];
1337 	size_t			 len;
1338 	u_int			 cellsize;
1339 
1340 	log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1341 	    px, py, nx, atx, aty);
1342 	log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg,
1343 	    defaults->bg);
1344 
1345 	/*
1346 	 * py is the line in the screen to draw.
1347 	 * px is the start x and nx is the width to draw.
1348 	 * atx,aty is the line on the terminal to draw it.
1349 	 */
1350 
1351 	flags = (tty->flags & TTY_NOCURSOR);
1352 	tty->flags |= TTY_NOCURSOR;
1353 	tty_update_mode(tty, tty->mode, s);
1354 
1355 	tty_region_off(tty);
1356 	tty_margin_off(tty);
1357 
1358 	/*
1359 	 * Clamp the width to cellsize - note this is not cellused, because
1360 	 * there may be empty background cells after it (from BCE).
1361 	 */
1362 	sx = screen_size_x(s);
1363 	if (nx > sx)
1364 		nx = sx;
1365 	cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1366 	if (sx > cellsize)
1367 		sx = cellsize;
1368 	if (sx > tty->sx)
1369 		sx = tty->sx;
1370 	if (sx > nx)
1371 		sx = nx;
1372 	ux = 0;
1373 
1374 	if (py == 0)
1375 		gl = NULL;
1376 	else
1377 		gl = grid_get_line(gd, gd->hsize + py - 1);
1378 	if (gl == NULL ||
1379 	    (~gl->flags & GRID_LINE_WRAPPED) ||
1380 	    atx != 0 ||
1381 	    tty->cx < tty->sx ||
1382 	    nx < tty->sx) {
1383 		if (nx < tty->sx &&
1384 		    atx == 0 &&
1385 		    px + sx != nx &&
1386 		    tty_term_has(tty->term, TTYC_EL1) &&
1387 		    !tty_fake_bce(tty, defaults, 8) &&
1388 		    c->overlay_check == NULL) {
1389 			tty_default_attributes(tty, defaults, palette, 8);
1390 			tty_cursor(tty, nx - 1, aty);
1391 			tty_putcode(tty, TTYC_EL1);
1392 			cleared = 1;
1393 		}
1394 	} else {
1395 		log_debug("%s: wrapped line %u", __func__, aty);
1396 		wrapped = 1;
1397 	}
1398 
1399 	memcpy(&last, &grid_default_cell, sizeof last);
1400 	len = 0;
1401 	width = 0;
1402 
1403 	for (i = 0; i < sx; i++) {
1404 		grid_view_get_cell(gd, px + i, py, &gc);
1405 		gcp = tty_check_codeset(tty, &gc);
1406 		if (len != 0 &&
1407 		    (!tty_check_overlay(tty, atx + ux + width, aty) ||
1408 		    (gcp->attr & GRID_ATTR_CHARSET) ||
1409 		    gcp->flags != last.flags ||
1410 		    gcp->attr != last.attr ||
1411 		    gcp->fg != last.fg ||
1412 		    gcp->bg != last.bg ||
1413 		    gcp->us != last.us ||
1414 		    ux + width + gcp->data.width > nx ||
1415 		    (sizeof buf) - len < gcp->data.size)) {
1416 			tty_attributes(tty, &last, defaults, palette);
1417 			if (last.flags & GRID_FLAG_CLEARED) {
1418 				log_debug("%s: %zu cleared", __func__, len);
1419 				tty_clear_line(tty, defaults, aty, atx + ux,
1420 				    width, last.bg);
1421 			} else {
1422 				if (!wrapped || atx != 0 || ux != 0)
1423 					tty_cursor(tty, atx + ux, aty);
1424 				tty_putn(tty, buf, len, width);
1425 			}
1426 			ux += width;
1427 
1428 			len = 0;
1429 			width = 0;
1430 			wrapped = 0;
1431 		}
1432 
1433 		if (gcp->flags & GRID_FLAG_SELECTED)
1434 			screen_select_cell(s, &last, gcp);
1435 		else
1436 			memcpy(&last, gcp, sizeof last);
1437 
1438 		hidden = 0;
1439 		for (j = 0; j < gcp->data.width; j++) {
1440 			if (!tty_check_overlay(tty, atx + ux + j, aty))
1441 				hidden++;
1442 		}
1443 		if (hidden != 0 && hidden == gcp->data.width) {
1444 			if (~gcp->flags & GRID_FLAG_PADDING)
1445 				ux += gcp->data.width;
1446 		} else if (hidden != 0 || ux + gcp->data.width > nx) {
1447 			if (~gcp->flags & GRID_FLAG_PADDING) {
1448 				tty_attributes(tty, &last, defaults, palette);
1449 				tty_cursor(tty, atx + ux, aty);
1450 				for (j = 0; j < gcp->data.width; j++) {
1451 					if (ux > nx)
1452 						break;
1453 					if (tty_check_overlay(tty, atx + ux,
1454 					    aty))
1455 						tty_putc(tty, ' ');
1456 					else {
1457 						tty_cursor(tty, atx + ux + 1,
1458 						    aty);
1459 					}
1460 					ux++;
1461 				}
1462 			}
1463 		} else if (gcp->attr & GRID_ATTR_CHARSET) {
1464 			tty_attributes(tty, &last, defaults, palette);
1465 			tty_cursor(tty, atx + ux, aty);
1466 			for (j = 0; j < gcp->data.size; j++)
1467 				tty_putc(tty, gcp->data.data[j]);
1468 			ux += gcp->data.width;
1469 		} else if (~gcp->flags & GRID_FLAG_PADDING) {
1470 			memcpy(buf + len, gcp->data.data, gcp->data.size);
1471 			len += gcp->data.size;
1472 			width += gcp->data.width;
1473 		}
1474 	}
1475 	if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1476 		tty_attributes(tty, &last, defaults, palette);
1477 		if (last.flags & GRID_FLAG_CLEARED) {
1478 			log_debug("%s: %zu cleared (end)", __func__, len);
1479 			tty_clear_line(tty, defaults, aty, atx + ux, width,
1480 			    last.bg);
1481 		} else {
1482 			if (!wrapped || atx != 0 || ux != 0)
1483 				tty_cursor(tty, atx + ux, aty);
1484 			tty_putn(tty, buf, len, width);
1485 		}
1486 		ux += width;
1487 	}
1488 
1489 	if (!cleared && ux < nx) {
1490 		log_debug("%s: %u to end of line (%zu cleared)", __func__,
1491 		    nx - ux, len);
1492 		tty_default_attributes(tty, defaults, palette, 8);
1493 		tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1494 	}
1495 
1496 	tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1497 	tty_update_mode(tty, tty->mode, s);
1498 }
1499 
1500 void
1501 tty_sync_start(struct tty *tty)
1502 {
1503 	if (tty->flags & TTY_BLOCK)
1504 		return;
1505 	if (tty->flags & TTY_SYNCING)
1506 		return;
1507 	tty->flags |= TTY_SYNCING;
1508 
1509 	if (tty_term_has(tty->term, TTYC_SYNC)) {
1510 		log_debug("%s sync start", tty->client->name);
1511 		tty_putcode1(tty, TTYC_SYNC, 1);
1512 	}
1513 }
1514 
1515 void
1516 tty_sync_end(struct tty *tty)
1517 {
1518 	if (tty->flags & TTY_BLOCK)
1519 		return;
1520 	if (~tty->flags & TTY_SYNCING)
1521 		return;
1522 	tty->flags &= ~TTY_SYNCING;
1523 
1524 	if (tty_term_has(tty->term, TTYC_SYNC)) {
1525  		log_debug("%s sync end", tty->client->name);
1526 		tty_putcode1(tty, TTYC_SYNC, 2);
1527 	}
1528 }
1529 
1530 static int
1531 tty_client_ready(struct client *c)
1532 {
1533 	if (c->session == NULL || c->tty.term == NULL)
1534 		return (0);
1535 	if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1536 		return (0);
1537 	if (c->tty.flags & TTY_FREEZE)
1538 		return (0);
1539 	return (1);
1540 }
1541 
1542 void
1543 tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1544     struct tty_ctx *ctx)
1545 {
1546 	struct client	*c;
1547 	int		 state;
1548 
1549 	if (ctx->set_client_cb == NULL)
1550 		return;
1551 	TAILQ_FOREACH(c, &clients, entry) {
1552 		if (!tty_client_ready(c))
1553 			continue;
1554 		state = ctx->set_client_cb(ctx, c);
1555 		if (state == -1)
1556 			break;
1557 		if (state == 0)
1558 			continue;
1559 		cmdfn(&c->tty, ctx);
1560 	}
1561 }
1562 
1563 void
1564 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1565 {
1566 	struct client	*c = tty->client;
1567 
1568 	if (ctx->bigger ||
1569 	    !tty_full_width(tty, ctx) ||
1570 	    tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1571 	    (!tty_term_has(tty->term, TTYC_ICH) &&
1572 	    !tty_term_has(tty->term, TTYC_ICH1)) ||
1573 	    c->overlay_check != NULL) {
1574 		tty_draw_pane(tty, ctx, ctx->ocy);
1575 		return;
1576 	}
1577 
1578 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1579 
1580 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1581 
1582 	tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1583 }
1584 
1585 void
1586 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1587 {
1588 	struct client	*c = tty->client;
1589 
1590 	if (ctx->bigger ||
1591 	    !tty_full_width(tty, ctx) ||
1592 	    tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1593 	    (!tty_term_has(tty->term, TTYC_DCH) &&
1594 	    !tty_term_has(tty->term, TTYC_DCH1)) ||
1595 	    c->overlay_check != NULL) {
1596 		tty_draw_pane(tty, ctx, ctx->ocy);
1597 		return;
1598 	}
1599 
1600 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1601 
1602 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1603 
1604 	tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1605 }
1606 
1607 void
1608 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1609 {
1610 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1611 
1612 	tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1613 }
1614 
1615 void
1616 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1617 {
1618 	struct client	*c = tty->client;
1619 
1620 	if (ctx->bigger ||
1621 	    !tty_full_width(tty, ctx) ||
1622 	    tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1623 	    !tty_term_has(tty->term, TTYC_CSR) ||
1624 	    !tty_term_has(tty->term, TTYC_IL1) ||
1625 	    ctx->sx == 1 ||
1626 	    ctx->sy == 1 ||
1627 	    c->overlay_check != NULL) {
1628 		tty_redraw_region(tty, ctx);
1629 		return;
1630 	}
1631 
1632 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1633 
1634 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1635 	tty_margin_off(tty);
1636 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1637 
1638 	tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1639 	tty->cx = tty->cy = UINT_MAX;
1640 }
1641 
1642 void
1643 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1644 {
1645 	struct client	*c = tty->client;
1646 
1647 	if (ctx->bigger ||
1648 	    !tty_full_width(tty, ctx) ||
1649 	    tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1650 	    !tty_term_has(tty->term, TTYC_CSR) ||
1651 	    !tty_term_has(tty->term, TTYC_DL1) ||
1652 	    ctx->sx == 1 ||
1653 	    ctx->sy == 1 ||
1654 	    c->overlay_check != NULL) {
1655 		tty_redraw_region(tty, ctx);
1656 		return;
1657 	}
1658 
1659 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1660 
1661 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1662 	tty_margin_off(tty);
1663 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1664 
1665 	tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1666 	tty->cx = tty->cy = UINT_MAX;
1667 }
1668 
1669 void
1670 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1671 {
1672 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1673 
1674 	tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1675 }
1676 
1677 void
1678 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1679 {
1680 	u_int	nx = ctx->sx - ctx->ocx;
1681 
1682 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1683 
1684 	tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1685 }
1686 
1687 void
1688 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1689 {
1690 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1691 
1692 	tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1693 }
1694 
1695 void
1696 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1697 {
1698 	struct client	*c = tty->client;
1699 
1700 	if (ctx->ocy != ctx->orupper)
1701 		return;
1702 
1703 	if (ctx->bigger ||
1704 	    (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1705 	    tty_fake_bce(tty, &ctx->defaults, 8) ||
1706 	    !tty_term_has(tty->term, TTYC_CSR) ||
1707 	    (!tty_term_has(tty->term, TTYC_RI) &&
1708 	    !tty_term_has(tty->term, TTYC_RIN)) ||
1709 	    ctx->sx == 1 ||
1710 	    ctx->sy == 1 ||
1711 	    c->overlay_check != NULL) {
1712 		tty_redraw_region(tty, ctx);
1713 		return;
1714 	}
1715 
1716 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1717 
1718 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1719 	tty_margin_pane(tty, ctx);
1720 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1721 
1722 	if (tty_term_has(tty->term, TTYC_RI))
1723 		tty_putcode(tty, TTYC_RI);
1724 	else
1725 		tty_putcode1(tty, TTYC_RIN, 1);
1726 }
1727 
1728 void
1729 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1730 {
1731 	struct client	*c = tty->client;
1732 
1733 	if (ctx->ocy != ctx->orlower)
1734 		return;
1735 
1736 	if (ctx->bigger ||
1737 	    (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1738 	    tty_fake_bce(tty, &ctx->defaults, 8) ||
1739 	    !tty_term_has(tty->term, TTYC_CSR) ||
1740 	    ctx->sx == 1 ||
1741 	    ctx->sy == 1 ||
1742 	    c->overlay_check != NULL) {
1743 		tty_redraw_region(tty, ctx);
1744 		return;
1745 	}
1746 
1747 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1748 
1749 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1750 	tty_margin_pane(tty, ctx);
1751 
1752 	/*
1753 	 * If we want to wrap a pane while using margins, the cursor needs to
1754 	 * be exactly on the right of the region. If the cursor is entirely off
1755 	 * the edge - move it back to the right. Some terminals are funny about
1756 	 * this and insert extra spaces, so only use the right if margins are
1757 	 * enabled.
1758 	 */
1759 	if (ctx->xoff + ctx->ocx > tty->rright) {
1760 		if (!tty_use_margin(tty))
1761 			tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1762 		else
1763 			tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1764 	} else
1765 		tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1766 
1767 	tty_putc(tty, '\n');
1768 }
1769 
1770 void
1771 tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1772 {
1773 	struct client	*c = tty->client;
1774 	u_int		 i;
1775 
1776 	if (ctx->bigger ||
1777 	    (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1778 	    tty_fake_bce(tty, &ctx->defaults, 8) ||
1779 	    !tty_term_has(tty->term, TTYC_CSR) ||
1780 	    ctx->sx == 1 ||
1781 	    ctx->sy == 1 ||
1782 	    c->overlay_check != NULL) {
1783 		tty_redraw_region(tty, ctx);
1784 		return;
1785 	}
1786 
1787 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1788 
1789 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1790 	tty_margin_pane(tty, ctx);
1791 
1792 	if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1793 		if (!tty_use_margin(tty))
1794 			tty_cursor(tty, 0, tty->rlower);
1795 		else
1796 			tty_cursor(tty, tty->rright, tty->rlower);
1797 		for (i = 0; i < ctx->num; i++)
1798 			tty_putc(tty, '\n');
1799 	} else {
1800 		if (tty->cy == UINT_MAX)
1801 			tty_cursor(tty, 0, 0);
1802 		else
1803 			tty_cursor(tty, 0, tty->cy);
1804 		tty_putcode1(tty, TTYC_INDN, ctx->num);
1805 	}
1806 }
1807 
1808 void
1809 tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
1810 {
1811 	u_int		 i;
1812 	struct client	*c = tty->client;
1813 
1814 	if (ctx->bigger ||
1815 	    (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1816 	    tty_fake_bce(tty, &ctx->defaults, 8) ||
1817 	    !tty_term_has(tty->term, TTYC_CSR) ||
1818 	    (!tty_term_has(tty->term, TTYC_RI) &&
1819 	    !tty_term_has(tty->term, TTYC_RIN)) ||
1820 	    ctx->sx == 1 ||
1821 	    ctx->sy == 1 ||
1822 	    c->overlay_check != NULL) {
1823 		tty_redraw_region(tty, ctx);
1824 		return;
1825 	}
1826 
1827 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1828 
1829 	tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1830 	tty_margin_pane(tty, ctx);
1831 	tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1832 
1833 	if (tty_term_has(tty->term, TTYC_RIN))
1834 		tty_putcode1(tty, TTYC_RIN, ctx->num);
1835 	else {
1836 		for (i = 0; i < ctx->num; i++)
1837 			tty_putcode(tty, TTYC_RI);
1838 	}
1839 }
1840 
1841 void
1842 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1843 {
1844 	u_int	px, py, nx, ny;
1845 
1846 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1847 
1848 	tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1849 	tty_margin_off(tty);
1850 
1851 	px = 0;
1852 	nx = ctx->sx;
1853 	py = ctx->ocy + 1;
1854 	ny = ctx->sy - ctx->ocy - 1;
1855 
1856 	tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1857 
1858 	px = ctx->ocx;
1859 	nx = ctx->sx - ctx->ocx;
1860 	py = ctx->ocy;
1861 
1862 	tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1863 }
1864 
1865 void
1866 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1867 {
1868 	u_int	px, py, nx, ny;
1869 
1870 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1871 
1872 	tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1873 	tty_margin_off(tty);
1874 
1875 	px = 0;
1876 	nx = ctx->sx;
1877 	py = 0;
1878 	ny = ctx->ocy;
1879 
1880 	tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1881 
1882 	px = 0;
1883 	nx = ctx->ocx + 1;
1884 	py = ctx->ocy;
1885 
1886 	tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1887 }
1888 
1889 void
1890 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1891 {
1892 	u_int	px, py, nx, ny;
1893 
1894 	tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1895 
1896 	tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1897 	tty_margin_off(tty);
1898 
1899 	px = 0;
1900 	nx = ctx->sx;
1901 	py = 0;
1902 	ny = ctx->sy;
1903 
1904 	tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1905 }
1906 
1907 void
1908 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1909 {
1910 	u_int	i, j;
1911 
1912 	if (ctx->bigger) {
1913 		ctx->redraw_cb(ctx);
1914 		return;
1915 	}
1916 
1917 	tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette);
1918 
1919 	tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1920 	tty_margin_off(tty);
1921 
1922 	for (j = 0; j < ctx->sy; j++) {
1923 		tty_cursor_pane(tty, ctx, 0, j);
1924 		for (i = 0; i < ctx->sx; i++)
1925 			tty_putc(tty, 'E');
1926 	}
1927 }
1928 
1929 void
1930 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1931 {
1932 	const struct grid_cell	*gcp = ctx->cell;
1933 	struct screen		*s = ctx->s;
1934 	u_int			 i, px, py;
1935 
1936 	px = ctx->xoff + ctx->ocx - ctx->wox;
1937 	py = ctx->yoff + ctx->ocy - ctx->woy;
1938 	if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
1939 	    (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
1940 		return;
1941 
1942 	/* Handle partially obstructed wide characters. */
1943 	if (gcp->data.width > 1) {
1944 		for (i = 0; i < gcp->data.width; i++) {
1945 			if (!tty_check_overlay(tty, px + i, py)) {
1946 				tty_draw_line(tty, s, s->cx, s->cy,
1947 				    gcp->data.width, px, py, &ctx->defaults,
1948 				    ctx->palette);
1949 				return;
1950 			}
1951 		}
1952 	}
1953 
1954 	if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
1955 	    ctx->ocy == ctx->orlower &&
1956 	    tty_full_width(tty, ctx))
1957 		tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1958 
1959 	tty_margin_off(tty);
1960 	tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1961 
1962 	tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette);
1963 }
1964 
1965 void
1966 tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1967 {
1968 	u_int	i, hide = 0;
1969 
1970 	if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
1971 		return;
1972 
1973 	if (ctx->bigger &&
1974 	    (ctx->xoff + ctx->ocx < ctx->wox ||
1975 	    ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
1976 		if (!ctx->wrapped ||
1977 		    !tty_full_width(tty, ctx) ||
1978 		    (tty->term->flags & TERM_NOAM) ||
1979 		    ctx->xoff + ctx->ocx != 0 ||
1980 		    ctx->yoff + ctx->ocy != tty->cy + 1 ||
1981 		    tty->cx < tty->sx ||
1982 		    tty->cy == tty->rlower)
1983 			tty_draw_pane(tty, ctx, ctx->ocy);
1984 		else
1985 			ctx->redraw_cb(ctx);
1986 		return;
1987 	}
1988 
1989 	tty_margin_off(tty);
1990 	tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1991 
1992 	tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette);
1993 	for (i = 0; i < ctx->num; i++) {
1994 		if (!tty_check_overlay(tty, tty->cx + i, tty->cy))
1995 			break;
1996 	}
1997 	tty_putn(tty, ctx->ptr, i, i);
1998 	for (; i < ctx->num; i++) {
1999 		if (tty_check_overlay(tty, tty->cx + hide, tty->cy))
2000 			break;
2001 		hide++;
2002 	}
2003 	tty_cursor(tty, tty->cx + hide, tty->cy);
2004 	tty_putn(tty, (char *)ctx->ptr + i, ctx->num - i, ctx->num - i);
2005 }
2006 
2007 void
2008 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
2009 {
2010 	tty_set_selection(tty, ctx->ptr, ctx->num);
2011 }
2012 
2013 void
2014 tty_set_selection(struct tty *tty, const char *buf, size_t len)
2015 {
2016 	char	*encoded;
2017 	size_t	 size;
2018 
2019 	if (~tty->flags & TTY_STARTED)
2020 		return;
2021 	if (!tty_term_has(tty->term, TTYC_MS))
2022 		return;
2023 
2024 	size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
2025 	encoded = xmalloc(size);
2026 
2027 	b64_ntop(buf, len, encoded, size);
2028 	tty_putcode_ptr2(tty, TTYC_MS, "", encoded);
2029 	tty->client->redraw = EVBUFFER_LENGTH(tty->out);
2030 
2031 	free(encoded);
2032 }
2033 
2034 void
2035 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
2036 {
2037 	tty_add(tty, ctx->ptr, ctx->num);
2038 	tty_invalidate(tty);
2039 }
2040 
2041 void
2042 tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
2043 {
2044 	if (ctx->num == 0x11) {
2045 		/*
2046 		 * This is an overlay and a command that moves the cursor so
2047 		 * start synchronized updates.
2048 		 */
2049 		tty_sync_start(tty);
2050 	} else if (~ctx->num & 0x10) {
2051 		/*
2052 		 * This is a pane. If there is an overlay, always start;
2053 		 * otherwise, only if requested.
2054 		 */
2055 		if (ctx->num || tty->client->overlay_draw != NULL)
2056 			tty_sync_start(tty);
2057 	}
2058 }
2059 
2060 void
2061 tty_cell(struct tty *tty, const struct grid_cell *gc,
2062     const struct grid_cell *defaults, struct colour_palette *palette)
2063 {
2064 	const struct grid_cell	*gcp;
2065 
2066 	/* Skip last character if terminal is stupid. */
2067 	if ((tty->term->flags & TERM_NOAM) &&
2068 	    tty->cy == tty->sy - 1 &&
2069 	    tty->cx == tty->sx - 1)
2070 		return;
2071 
2072 	/* If this is a padding character, do nothing. */
2073 	if (gc->flags & GRID_FLAG_PADDING)
2074 		return;
2075 
2076 	/* Check the output codeset and apply attributes. */
2077 	gcp = tty_check_codeset(tty, gc);
2078 	tty_attributes(tty, gcp, defaults, palette);
2079 
2080 	/* If it is a single character, write with putc to handle ACS. */
2081 	if (gcp->data.size == 1) {
2082 		tty_attributes(tty, gcp, defaults, palette);
2083 		if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
2084 			return;
2085 		tty_putc(tty, *gcp->data.data);
2086 		return;
2087 	}
2088 
2089 	/* Write the data. */
2090 	tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
2091 }
2092 
2093 void
2094 tty_reset(struct tty *tty)
2095 {
2096 	struct grid_cell	*gc = &tty->cell;
2097 
2098 	if (!grid_cells_equal(gc, &grid_default_cell)) {
2099 		if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2100 			tty_putcode(tty, TTYC_RMACS);
2101 		tty_putcode(tty, TTYC_SGR0);
2102 		memcpy(gc, &grid_default_cell, sizeof *gc);
2103 	}
2104 	memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2105 }
2106 
2107 static void
2108 tty_invalidate(struct tty *tty)
2109 {
2110 	memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2111 	memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2112 
2113 	tty->cx = tty->cy = UINT_MAX;
2114 	tty->rupper = tty->rleft = UINT_MAX;
2115 	tty->rlower = tty->rright = UINT_MAX;
2116 
2117 	if (tty->flags & TTY_STARTED) {
2118 		if (tty_use_margin(tty))
2119 			tty_putcode(tty, TTYC_ENMG);
2120 		tty_putcode(tty, TTYC_SGR0);
2121 
2122 		tty->mode = ALL_MODES;
2123 		tty_update_mode(tty, MODE_CURSOR, NULL);
2124 
2125 		tty_cursor(tty, 0, 0);
2126 		tty_region_off(tty);
2127 		tty_margin_off(tty);
2128 	} else
2129 		tty->mode = MODE_CURSOR;
2130 }
2131 
2132 /* Turn off margin. */
2133 void
2134 tty_region_off(struct tty *tty)
2135 {
2136 	tty_region(tty, 0, tty->sy - 1);
2137 }
2138 
2139 /* Set region inside pane. */
2140 static void
2141 tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2142     u_int rlower)
2143 {
2144 	tty_region(tty, ctx->yoff + rupper - ctx->woy,
2145 	    ctx->yoff + rlower - ctx->woy);
2146 }
2147 
2148 /* Set region at absolute position. */
2149 static void
2150 tty_region(struct tty *tty, u_int rupper, u_int rlower)
2151 {
2152 	if (tty->rlower == rlower && tty->rupper == rupper)
2153 		return;
2154 	if (!tty_term_has(tty->term, TTYC_CSR))
2155 		return;
2156 
2157 	tty->rupper = rupper;
2158 	tty->rlower = rlower;
2159 
2160 	/*
2161 	 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2162 	 * 0,0 if it is beyond the last column (they do not reset their wrap
2163 	 * flag so further output causes a line feed). As a workaround, do an
2164 	 * explicit move to 0 first.
2165 	 */
2166 	if (tty->cx >= tty->sx) {
2167 		if (tty->cy == UINT_MAX)
2168 			tty_cursor(tty, 0, 0);
2169 		else
2170 			tty_cursor(tty, 0, tty->cy);
2171 	}
2172 
2173 	tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2174 	tty->cx = tty->cy = UINT_MAX;
2175 }
2176 
2177 /* Turn off margin. */
2178 void
2179 tty_margin_off(struct tty *tty)
2180 {
2181 	tty_margin(tty, 0, tty->sx - 1);
2182 }
2183 
2184 /* Set margin inside pane. */
2185 static void
2186 tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2187 {
2188 	tty_margin(tty, ctx->xoff - ctx->wox,
2189 	    ctx->xoff + ctx->sx - 1 - ctx->wox);
2190 }
2191 
2192 /* Set margin at absolute position. */
2193 static void
2194 tty_margin(struct tty *tty, u_int rleft, u_int rright)
2195 {
2196 	if (!tty_use_margin(tty))
2197 		return;
2198 	if (tty->rleft == rleft && tty->rright == rright)
2199 		return;
2200 
2201 	tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2202 
2203 	tty->rleft = rleft;
2204 	tty->rright = rright;
2205 
2206 	if (rleft == 0 && rright == tty->sx - 1)
2207 		tty_putcode(tty, TTYC_CLMG);
2208 	else
2209 		tty_putcode2(tty, TTYC_CMG, rleft, rright);
2210 	tty->cx = tty->cy = UINT_MAX;
2211 }
2212 
2213 /*
2214  * Move the cursor, unless it would wrap itself when the next character is
2215  * printed.
2216  */
2217 static void
2218 tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2219     u_int cx, u_int cy)
2220 {
2221 	if (!ctx->wrapped ||
2222 	    !tty_full_width(tty, ctx) ||
2223 	    (tty->term->flags & TERM_NOAM) ||
2224 	    ctx->xoff + cx != 0 ||
2225 	    ctx->yoff + cy != tty->cy + 1 ||
2226 	    tty->cx < tty->sx ||
2227 	    tty->cy == tty->rlower)
2228 		tty_cursor_pane(tty, ctx, cx, cy);
2229 	else
2230 		log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
2231 }
2232 
2233 /* Move cursor inside pane. */
2234 static void
2235 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2236 {
2237 	tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
2238 }
2239 
2240 /* Move cursor to absolute position. */
2241 void
2242 tty_cursor(struct tty *tty, u_int cx, u_int cy)
2243 {
2244 	struct tty_term	*term = tty->term;
2245 	u_int		 thisx, thisy;
2246 	int		 change;
2247 
2248 	if (tty->flags & TTY_BLOCK)
2249 		return;
2250 
2251 	if (cx > tty->sx - 1)
2252 		cx = tty->sx - 1;
2253 
2254 	thisx = tty->cx;
2255 	thisy = tty->cy;
2256 
2257 	/* No change. */
2258 	if (cx == thisx && cy == thisy)
2259 		return;
2260 
2261 	/* Very end of the line, just use absolute movement. */
2262 	if (thisx > tty->sx - 1)
2263 		goto absolute;
2264 
2265 	/* Move to home position (0, 0). */
2266 	if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2267 		tty_putcode(tty, TTYC_HOME);
2268 		goto out;
2269 	}
2270 
2271 	/* Zero on the next line. */
2272 	if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2273 	    (!tty_use_margin(tty) || tty->rleft == 0)) {
2274 		tty_putc(tty, '\r');
2275 		tty_putc(tty, '\n');
2276 		goto out;
2277 	}
2278 
2279 	/* Moving column or row. */
2280 	if (cy == thisy) {
2281 		/*
2282 		 * Moving column only, row staying the same.
2283 		 */
2284 
2285 		/* To left edge. */
2286 		if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
2287 			tty_putc(tty, '\r');
2288 			goto out;
2289 		}
2290 
2291 		/* One to the left. */
2292 		if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2293 			tty_putcode(tty, TTYC_CUB1);
2294 			goto out;
2295 		}
2296 
2297 		/* One to the right. */
2298 		if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2299 			tty_putcode(tty, TTYC_CUF1);
2300 			goto out;
2301 		}
2302 
2303 		/* Calculate difference. */
2304 		change = thisx - cx;	/* +ve left, -ve right */
2305 
2306 		/*
2307 		 * Use HPA if change is larger than absolute, otherwise move
2308 		 * the cursor with CUB/CUF.
2309 		 */
2310 		if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
2311 			tty_putcode1(tty, TTYC_HPA, cx);
2312 			goto out;
2313 		} else if (change > 0 &&
2314 		    tty_term_has(term, TTYC_CUB) &&
2315 		    !tty_use_margin(tty)) {
2316 			if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2317 				tty_putcode(tty, TTYC_CUB1);
2318 				tty_putcode(tty, TTYC_CUB1);
2319 				goto out;
2320 			}
2321 			tty_putcode1(tty, TTYC_CUB, change);
2322 			goto out;
2323 		} else if (change < 0 &&
2324 		    tty_term_has(term, TTYC_CUF) &&
2325 		    !tty_use_margin(tty)) {
2326 			tty_putcode1(tty, TTYC_CUF, -change);
2327 			goto out;
2328 		}
2329 	} else if (cx == thisx) {
2330 		/*
2331 		 * Moving row only, column staying the same.
2332 		 */
2333 
2334 		/* One above. */
2335 		if (thisy != tty->rupper &&
2336 		    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2337 			tty_putcode(tty, TTYC_CUU1);
2338 			goto out;
2339 		}
2340 
2341 		/* One below. */
2342 		if (thisy != tty->rlower &&
2343 		    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2344 			tty_putcode(tty, TTYC_CUD1);
2345 			goto out;
2346 		}
2347 
2348 		/* Calculate difference. */
2349 		change = thisy - cy;	/* +ve up, -ve down */
2350 
2351 		/*
2352 		 * Try to use VPA if change is larger than absolute or if this
2353 		 * change would cross the scroll region, otherwise use CUU/CUD.
2354 		 */
2355 		if ((u_int) abs(change) > cy ||
2356 		    (change < 0 && cy - change > tty->rlower) ||
2357 		    (change > 0 && cy - change < tty->rupper)) {
2358 			    if (tty_term_has(term, TTYC_VPA)) {
2359 				    tty_putcode1(tty, TTYC_VPA, cy);
2360 				    goto out;
2361 			    }
2362 		} else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2363 			tty_putcode1(tty, TTYC_CUU, change);
2364 			goto out;
2365 		} else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2366 			tty_putcode1(tty, TTYC_CUD, -change);
2367 			goto out;
2368 		}
2369 	}
2370 
2371 absolute:
2372 	/* Absolute movement. */
2373 	tty_putcode2(tty, TTYC_CUP, cy, cx);
2374 
2375 out:
2376 	tty->cx = cx;
2377 	tty->cy = cy;
2378 }
2379 
2380 void
2381 tty_attributes(struct tty *tty, const struct grid_cell *gc,
2382     const struct grid_cell *defaults, struct colour_palette *palette)
2383 {
2384 	struct grid_cell	*tc = &tty->cell, gc2;
2385 	int			 changed;
2386 
2387 	/* Copy cell and update default colours. */
2388 	memcpy(&gc2, gc, sizeof gc2);
2389 	if (~gc->flags & GRID_FLAG_NOPALETTE) {
2390 		if (gc2.fg == 8)
2391 			gc2.fg = defaults->fg;
2392 		if (gc2.bg == 8)
2393 			gc2.bg = defaults->bg;
2394 	}
2395 
2396 	/* Ignore cell if it is the same as the last one. */
2397 	if (gc2.attr == tty->last_cell.attr &&
2398 	    gc2.fg == tty->last_cell.fg &&
2399 	    gc2.bg == tty->last_cell.bg &&
2400 	    gc2.us == tty->last_cell.us)
2401 		return;
2402 
2403 	/*
2404 	 * If no setab, try to use the reverse attribute as a best-effort for a
2405 	 * non-default background. This is a bit of a hack but it doesn't do
2406 	 * any serious harm and makes a couple of applications happier.
2407 	 */
2408 	if (!tty_term_has(tty->term, TTYC_SETAB)) {
2409 		if (gc2.attr & GRID_ATTR_REVERSE) {
2410 			if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
2411 				gc2.attr &= ~GRID_ATTR_REVERSE;
2412 		} else {
2413 			if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
2414 				gc2.attr |= GRID_ATTR_REVERSE;
2415 		}
2416 	}
2417 
2418 	/* Fix up the colours if necessary. */
2419 	tty_check_fg(tty, palette, &gc2);
2420 	tty_check_bg(tty, palette, &gc2);
2421 	tty_check_us(tty, palette, &gc2);
2422 
2423 	/*
2424 	 * If any bits are being cleared or the underline colour is now default,
2425 	 * reset everything.
2426 	 */
2427 	if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
2428 		tty_reset(tty);
2429 
2430 	/*
2431 	 * Set the colours. This may call tty_reset() (so it comes next) and
2432 	 * may add to (NOT remove) the desired attributes.
2433 	 */
2434 	tty_colours(tty, &gc2);
2435 
2436 	/* Filter out attribute bits already set. */
2437 	changed = gc2.attr & ~tc->attr;
2438 	tc->attr = gc2.attr;
2439 
2440 	/* Set the attributes. */
2441 	if (changed & GRID_ATTR_BRIGHT)
2442 		tty_putcode(tty, TTYC_BOLD);
2443 	if (changed & GRID_ATTR_DIM)
2444 		tty_putcode(tty, TTYC_DIM);
2445 	if (changed & GRID_ATTR_ITALICS)
2446 		tty_set_italics(tty);
2447 	if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2448 		if ((changed & GRID_ATTR_UNDERSCORE) ||
2449 		    !tty_term_has(tty->term, TTYC_SMULX))
2450 			tty_putcode(tty, TTYC_SMUL);
2451 		else if (changed & GRID_ATTR_UNDERSCORE_2)
2452 			tty_putcode1(tty, TTYC_SMULX, 2);
2453 		else if (changed & GRID_ATTR_UNDERSCORE_3)
2454 			tty_putcode1(tty, TTYC_SMULX, 3);
2455 		else if (changed & GRID_ATTR_UNDERSCORE_4)
2456 			tty_putcode1(tty, TTYC_SMULX, 4);
2457 		else if (changed & GRID_ATTR_UNDERSCORE_5)
2458 			tty_putcode1(tty, TTYC_SMULX, 5);
2459 	}
2460 	if (changed & GRID_ATTR_BLINK)
2461 		tty_putcode(tty, TTYC_BLINK);
2462 	if (changed & GRID_ATTR_REVERSE) {
2463 		if (tty_term_has(tty->term, TTYC_REV))
2464 			tty_putcode(tty, TTYC_REV);
2465 		else if (tty_term_has(tty->term, TTYC_SMSO))
2466 			tty_putcode(tty, TTYC_SMSO);
2467 	}
2468 	if (changed & GRID_ATTR_HIDDEN)
2469 		tty_putcode(tty, TTYC_INVIS);
2470 	if (changed & GRID_ATTR_STRIKETHROUGH)
2471 		tty_putcode(tty, TTYC_SMXX);
2472 	if (changed & GRID_ATTR_OVERLINE)
2473 		tty_putcode(tty, TTYC_SMOL);
2474 	if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2475 		tty_putcode(tty, TTYC_SMACS);
2476 
2477 	memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
2478 }
2479 
2480 static void
2481 tty_colours(struct tty *tty, const struct grid_cell *gc)
2482 {
2483 	struct grid_cell	*tc = &tty->cell;
2484 	int			 have_ax;
2485 
2486 	/* No changes? Nothing is necessary. */
2487 	if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
2488 		return;
2489 
2490 	/*
2491 	 * Is either the default colour? This is handled specially because the
2492 	 * best solution might be to reset both colours to default, in which
2493 	 * case if only one is default need to fall onward to set the other
2494 	 * colour.
2495 	 */
2496 	if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
2497 		/*
2498 		 * If don't have AX but do have op, send sgr0 (op can't
2499 		 * actually be used because it is sometimes the same as sgr0
2500 		 * and sometimes isn't). This resets both colours to default.
2501 		 *
2502 		 * Otherwise, try to set the default colour only as needed.
2503 		 */
2504 		have_ax = tty_term_flag(tty->term, TTYC_AX);
2505 		if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2506 			tty_reset(tty);
2507 		else {
2508 			if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
2509 				if (have_ax)
2510 					tty_puts(tty, "\033[39m");
2511 				else if (tc->fg != 7)
2512 					tty_putcode1(tty, TTYC_SETAF, 7);
2513 				tc->fg = gc->fg;
2514 			}
2515 			if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
2516 				if (have_ax)
2517 					tty_puts(tty, "\033[49m");
2518 				else if (tc->bg != 0)
2519 					tty_putcode1(tty, TTYC_SETAB, 0);
2520 				tc->bg = gc->bg;
2521 			}
2522 		}
2523 	}
2524 
2525 	/* Set the foreground colour. */
2526 	if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
2527 		tty_colours_fg(tty, gc);
2528 
2529 	/*
2530 	 * Set the background colour. This must come after the foreground as
2531 	 * tty_colour_fg() can call tty_reset().
2532 	 */
2533 	if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
2534 		tty_colours_bg(tty, gc);
2535 
2536 	/* Set the underscore colour. */
2537 	if (gc->us != tc->us)
2538 		tty_colours_us(tty, gc);
2539 }
2540 
2541 static void
2542 tty_check_fg(struct tty *tty, struct colour_palette *palette,
2543     struct grid_cell *gc)
2544 {
2545 	u_char	r, g, b;
2546 	u_int	colours;
2547 	int	c;
2548 
2549 	/*
2550 	 * Perform substitution if this pane has a palette. If the bright
2551 	 * attribute is set, use the bright entry in the palette by changing to
2552 	 * the aixterm colour.
2553 	 */
2554 	if (~gc->flags & GRID_FLAG_NOPALETTE) {
2555 		c = gc->fg;
2556 		if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
2557 			c += 90;
2558 		if ((c = colour_palette_get(palette, c)) != -1)
2559 			gc->fg = c;
2560 	}
2561 
2562 	/* Is this a 24-bit colour? */
2563 	if (gc->fg & COLOUR_FLAG_RGB) {
2564 		/* Not a 24-bit terminal? Translate to 256-colour palette. */
2565 		if (tty->term->flags & TERM_RGBCOLOURS)
2566 			return;
2567 		colour_split_rgb(gc->fg, &r, &g, &b);
2568 		gc->fg = colour_find_rgb(r, g, b);
2569 	}
2570 
2571 	/* How many colours does this terminal have? */
2572 	if (tty->term->flags & TERM_256COLOURS)
2573 		colours = 256;
2574 	else
2575 		colours = tty_term_number(tty->term, TTYC_COLORS);
2576 
2577 	/* Is this a 256-colour colour? */
2578 	if (gc->fg & COLOUR_FLAG_256) {
2579 		/* And not a 256 colour mode? */
2580 		if (colours < 256) {
2581 			gc->fg = colour_256to16(gc->fg);
2582 			if (gc->fg & 8) {
2583 				gc->fg &= 7;
2584 				if (colours >= 16)
2585 					gc->fg += 90;
2586 			}
2587 		}
2588 		return;
2589 	}
2590 
2591 	/* Is this an aixterm colour? */
2592 	if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2593 		gc->fg -= 90;
2594 		gc->attr |= GRID_ATTR_BRIGHT;
2595 	}
2596 }
2597 
2598 static void
2599 tty_check_bg(struct tty *tty, struct colour_palette *palette,
2600     struct grid_cell *gc)
2601 {
2602 	u_char	r, g, b;
2603 	u_int	colours;
2604 	int	c;
2605 
2606 	/* Perform substitution if this pane has a palette. */
2607 	if (~gc->flags & GRID_FLAG_NOPALETTE) {
2608 		if ((c = colour_palette_get(palette, gc->bg)) != -1)
2609 			gc->bg = c;
2610 	}
2611 
2612 	/* Is this a 24-bit colour? */
2613 	if (gc->bg & COLOUR_FLAG_RGB) {
2614 		/* Not a 24-bit terminal? Translate to 256-colour palette. */
2615 		if (tty->term->flags & TERM_RGBCOLOURS)
2616 			return;
2617 		colour_split_rgb(gc->bg, &r, &g, &b);
2618 		gc->bg = colour_find_rgb(r, g, b);
2619 	}
2620 
2621 	/* How many colours does this terminal have? */
2622 	if (tty->term->flags & TERM_256COLOURS)
2623 		colours = 256;
2624 	else
2625 		colours = tty_term_number(tty->term, TTYC_COLORS);
2626 
2627 	/* Is this a 256-colour colour? */
2628 	if (gc->bg & COLOUR_FLAG_256) {
2629 		/*
2630 		 * And not a 256 colour mode? Translate to 16-colour
2631 		 * palette. Bold background doesn't exist portably, so just
2632 		 * discard the bold bit if set.
2633 		 */
2634 		if (colours < 256) {
2635 			gc->bg = colour_256to16(gc->bg);
2636 			if (gc->bg & 8) {
2637 				gc->bg &= 7;
2638 				if (colours >= 16)
2639 					gc->bg += 90;
2640 			}
2641 		}
2642 		return;
2643 	}
2644 
2645 	/* Is this an aixterm colour? */
2646 	if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
2647 		gc->bg -= 90;
2648 }
2649 
2650 static void
2651 tty_check_us(__unused struct tty *tty, struct colour_palette *palette,
2652     struct grid_cell *gc)
2653 {
2654 	int	c;
2655 
2656 	/* Perform substitution if this pane has a palette. */
2657 	if (~gc->flags & GRID_FLAG_NOPALETTE) {
2658 		if ((c = colour_palette_get(palette, gc->us)) != -1)
2659 			gc->us = c;
2660 	}
2661 
2662 	/* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2663 	if (gc->us & COLOUR_FLAG_256)
2664 		gc->us = colour_256toRGB (gc->us);
2665 }
2666 
2667 static void
2668 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
2669 {
2670 	struct grid_cell	*tc = &tty->cell;
2671 	char			 s[32];
2672 
2673 	/* Is this a 24-bit or 256-colour colour? */
2674 	if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
2675 		if (tty_try_colour(tty, gc->fg, "38") == 0)
2676 			goto save;
2677 		/* Should not get here, already converted in tty_check_fg. */
2678 		return;
2679 	}
2680 
2681 	/* Is this an aixterm bright colour? */
2682 	if (gc->fg >= 90 && gc->fg <= 97) {
2683 		if (tty->term->flags & TERM_256COLOURS) {
2684 			xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2685 			tty_puts(tty, s);
2686 		} else
2687 			tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
2688 		goto save;
2689 	}
2690 
2691 	/* Otherwise set the foreground colour. */
2692 	tty_putcode1(tty, TTYC_SETAF, gc->fg);
2693 
2694 save:
2695 	/* Save the new values in the terminal current cell. */
2696 	tc->fg = gc->fg;
2697 }
2698 
2699 static void
2700 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
2701 {
2702 	struct grid_cell	*tc = &tty->cell;
2703 	char			 s[32];
2704 
2705 	/* Is this a 24-bit or 256-colour colour? */
2706 	if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
2707 		if (tty_try_colour(tty, gc->bg, "48") == 0)
2708 			goto save;
2709 		/* Should not get here, already converted in tty_check_bg. */
2710 		return;
2711 	}
2712 
2713 	/* Is this an aixterm bright colour? */
2714 	if (gc->bg >= 90 && gc->bg <= 97) {
2715 		if (tty->term->flags & TERM_256COLOURS) {
2716 			xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2717 			tty_puts(tty, s);
2718 		} else
2719 			tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
2720 		goto save;
2721 	}
2722 
2723 	/* Otherwise set the background colour. */
2724 	tty_putcode1(tty, TTYC_SETAB, gc->bg);
2725 
2726 save:
2727 	/* Save the new values in the terminal current cell. */
2728 	tc->bg = gc->bg;
2729 }
2730 
2731 static void
2732 tty_colours_us(struct tty *tty, const struct grid_cell *gc)
2733 {
2734 	struct grid_cell	*tc = &tty->cell;
2735 	u_int			 c;
2736 	u_char			 r, g, b;
2737 
2738 	/* Clear underline colour. */
2739 	if (gc->us == 0) {
2740 		tty_putcode(tty, TTYC_OL);
2741 		goto save;
2742 	}
2743 
2744 	/* Must be an RGB colour - this should never happen. */
2745 	if (~gc->us & COLOUR_FLAG_RGB)
2746 		return;
2747 
2748 	/*
2749 	 * Setulc and setal follows the ncurses(3) one argument "direct colour"
2750 	 * capability format. Calculate the colour value.
2751 	 */
2752 	colour_split_rgb(gc->us, &r, &g, &b);
2753 	c = (65536 * r) + (256 * g) + b;
2754 
2755 	/*
2756 	 * Write the colour. Only use setal if the RGB flag is set because the
2757 	 * non-RGB version may be wrong.
2758 	 */
2759 	if (tty_term_has(tty->term, TTYC_SETULC))
2760 		tty_putcode1(tty, TTYC_SETULC, c);
2761 	else if (tty_term_has(tty->term, TTYC_SETAL) &&
2762 	    tty_term_has(tty->term, TTYC_RGB))
2763 		tty_putcode1(tty, TTYC_SETAL, c);
2764 
2765 save:
2766 	/* Save the new values in the terminal current cell. */
2767 	tc->us = gc->us;
2768 }
2769 
2770 static int
2771 tty_try_colour(struct tty *tty, int colour, const char *type)
2772 {
2773 	u_char	r, g, b;
2774 
2775 	if (colour & COLOUR_FLAG_256) {
2776 		if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
2777 			tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2778 		else if (tty_term_has(tty->term, TTYC_SETAB))
2779 			tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2780 		return (0);
2781 	}
2782 
2783 	if (colour & COLOUR_FLAG_RGB) {
2784 		colour_split_rgb(colour & 0xffffff, &r, &g, &b);
2785 		if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
2786 			tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
2787 		else if (tty_term_has(tty->term, TTYC_SETRGBB))
2788 			tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
2789 		return (0);
2790 	}
2791 
2792 	return (-1);
2793 }
2794 
2795 static void
2796 tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
2797 {
2798 	memcpy(gc, &grid_default_cell, sizeof *gc);
2799 	gc->fg = wp->palette.fg;
2800 	gc->bg = wp->palette.bg;
2801 }
2802 
2803 void
2804 tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
2805 {
2806 	struct options		*oo = wp->options;
2807 	struct format_tree	*ft;
2808 
2809 	memcpy(gc, &grid_default_cell, sizeof *gc);
2810 
2811 	if (wp->flags & PANE_STYLECHANGED) {
2812 		log_debug("%%%u: style changed", wp->id);
2813 		wp->flags &= ~PANE_STYLECHANGED;
2814 
2815 		ft = format_create(NULL, NULL, FORMAT_PANE|wp->id,
2816 		    FORMAT_NOJOBS);
2817 		format_defaults(ft, NULL, NULL, NULL, wp);
2818 		tty_window_default_style(&wp->cached_active_gc, wp);
2819 		style_add(&wp->cached_active_gc, oo, "window-active-style", ft);
2820 		tty_window_default_style(&wp->cached_gc, wp);
2821 		style_add(&wp->cached_gc, oo, "window-style", ft);
2822 		format_free(ft);
2823 	}
2824 
2825 	if (gc->fg == 8) {
2826 		if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
2827 			gc->fg = wp->cached_active_gc.fg;
2828 		else
2829 			gc->fg = wp->cached_gc.fg;
2830 	}
2831 
2832 	if (gc->bg == 8) {
2833 		if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
2834 			gc->bg = wp->cached_active_gc.bg;
2835 		else
2836 			gc->bg = wp->cached_gc.bg;
2837 	}
2838 }
2839 
2840 static void
2841 tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
2842     struct colour_palette *palette, u_int bg)
2843 {
2844 	struct grid_cell	gc;
2845 
2846 	memcpy(&gc, &grid_default_cell, sizeof gc);
2847 	gc.bg = bg;
2848 	tty_attributes(tty, &gc, defaults, palette);
2849 }
2850