Make send-keys without any arguments send the key it is bound to (if
[tmux-openbsd.git] / tty.c
blobe4800ec283f019bb4fdf96e712e38dea0554ae04
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <netinet/in.h>
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>
33 #include "tmux.h"
35 static int tty_log_fd = -1;
37 static int tty_client_ready(struct client *);
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 *);
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);
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)
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)
84 void
85 tty_create_log(void)
87 char name[64];
89 xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
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");
96 int
97 tty_init(struct tty *tty, struct client *c)
99 if (!isatty(c->fd))
100 return (-1);
102 memset(tty, 0, sizeof *tty);
103 tty->client = c;
105 tty->cstyle = SCREEN_CURSOR_DEFAULT;
106 tty->ccolour = xstrdup("");
108 if (tcgetattr(c->fd, &tty->tio) != 0)
109 return (-1);
110 return (0);
113 void
114 tty_resize(struct tty *tty)
116 struct client *c = tty->client;
117 struct winsize ws;
118 u_int sx, sy, xpixel, ypixel;
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;
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);
145 void
146 tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
148 tty->sx = sx;
149 tty->sy = sy;
150 tty->xpixel = xpixel;
151 tty->ypixel = ypixel;
154 static void
155 tty_read_callback(__unused int fd, __unused short events, void *data)
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;
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;
173 log_debug("%s: read %d bytes (already %zu)", name, nread, size);
175 while (tty_keys_next(tty))
179 static void
180 tty_timer_callback(__unused int fd, __unused short events, void *data)
182 struct tty *tty = data;
183 struct client *c = tty->client;
184 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
186 log_debug("%s: %zu discarded", c->name, tty->discarded);
188 c->flags |= CLIENT_ALLREDRAWFLAGS;
189 c->discarded += tty->discarded;
191 if (tty->discarded < TTY_BLOCK_STOP(tty)) {
192 tty->flags &= ~TTY_BLOCK;
193 tty_invalidate(tty);
194 return;
196 tty->discarded = 0;
197 evtimer_add(&tty->timer, &tv);
200 static int
201 tty_block_maybe(struct tty *tty)
203 struct client *c = tty->client;
204 size_t size = EVBUFFER_LENGTH(tty->out);
205 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
207 if (size < TTY_BLOCK_START(tty))
208 return (0);
210 if (tty->flags & TTY_BLOCK)
211 return (1);
212 tty->flags |= TTY_BLOCK;
214 log_debug("%s: can't keep up, %zu discarded", c->name, size);
216 evbuffer_drain(tty->out, size);
217 c->discarded += size;
219 tty->discarded = 0;
220 evtimer_add(&tty->timer, &tv);
221 return (1);
224 static void
225 tty_write_callback(__unused int fd, __unused short events, void *data)
227 struct tty *tty = data;
228 struct client *c = tty->client;
229 size_t size = EVBUFFER_LENGTH(tty->out);
230 int nwrite;
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);
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;
247 if (EVBUFFER_LENGTH(tty->out) != 0)
248 event_add(&tty->event_out, NULL);
252 tty_open(struct tty *tty, char **cause)
254 struct client *c = tty->client;
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);
262 tty->flags |= TTY_OPENED;
264 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
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");
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");
277 evtimer_set(&tty->timer, tty_timer_callback, tty);
279 tty_start_tty(tty);
281 tty_keys_build(tty);
283 return (0);
286 static void
287 tty_start_timer_callback(__unused int fd, __unused short events, void *data)
289 struct tty *tty = data;
290 struct client *c = tty->client;
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);
298 void
299 tty_start_tty(struct tty *tty)
301 struct client *c = tty->client;
302 struct termios tio;
303 struct timeval tv = { .tv_sec = 1 };
305 setblocking(c->fd, 0);
306 event_add(&tty->event_in, NULL);
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, TCOFLUSH);
319 tty_putcode(tty, TTYC_SMCUP);
321 tty_putcode(tty, TTYC_SMKX);
322 tty_putcode(tty, TTYC_CLEAR);
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);
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");
336 evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
337 evtimer_add(&tty->start_timer, &tv);
339 tty->flags |= TTY_STARTED;
340 tty_invalidate(tty);
342 if (*tty->ccolour != '\0')
343 tty_force_cursor_colour(tty, "");
345 tty->mouse_drag_flag = 0;
346 tty->mouse_drag_update = NULL;
347 tty->mouse_drag_release = NULL;
350 void
351 tty_send_requests(struct tty *tty)
353 if (~tty->flags & TTY_STARTED)
354 return;
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);
365 void
366 tty_stop_tty(struct tty *tty)
368 struct client *c = tty->client;
369 struct winsize ws;
371 if (!(tty->flags & TTY_STARTED))
372 return;
373 tty->flags &= ~TTY_STARTED;
375 evtimer_del(&tty->start_timer);
377 event_del(&tty->timer);
378 tty->flags &= ~TTY_BLOCK;
380 event_del(&tty->event_in);
381 event_del(&tty->event_out);
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.
388 if (ioctl(c->fd, TIOCGWINSZ, &ws) == -1)
389 return;
390 if (tcsetattr(c->fd, TCSANOW, &tty->tio) == -1)
391 return;
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));
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));
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");
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));
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));
425 setblocking(c->fd, 1);
428 void
429 tty_close(struct tty *tty)
431 if (event_initialized(&tty->key_timer))
432 evtimer_del(&tty->key_timer);
433 tty_stop_tty(tty);
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);
441 tty_term_free(tty->term);
442 tty_keys_free(tty);
444 tty->flags &= ~TTY_OPENED;
448 void
449 tty_free(struct tty *tty)
451 tty_close(tty);
452 free(tty->ccolour);
455 void
456 tty_update_features(struct tty *tty)
458 struct client *c = tty->client;
460 if (tty_apply_features(tty->term, c->term_features))
461 tty_term_apply_overrides(tty->term);
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");
473 void
474 tty_raw(struct tty *tty, const char *s)
476 struct client *c = tty->client;
477 ssize_t n, slen;
478 u_int i;
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);
494 void
495 tty_putcode(struct tty *tty, enum tty_code_code code)
497 tty_puts(tty, tty_term_string(tty->term, code));
500 void
501 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
503 if (a < 0)
504 return;
505 tty_puts(tty, tty_term_string1(tty->term, code, a));
508 void
509 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
511 if (a < 0 || b < 0)
512 return;
513 tty_puts(tty, tty_term_string2(tty->term, code, a, b));
516 void
517 tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
519 if (a < 0 || b < 0 || c < 0)
520 return;
521 tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
524 void
525 tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
527 if (a != NULL)
528 tty_puts(tty, tty_term_ptr1(tty->term, code, a));
531 void
532 tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
533 const void *b)
535 if (a != NULL && b != NULL)
536 tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
539 static void
540 tty_add(struct tty *tty, const char *buf, size_t len)
542 struct client *c = tty->client;
544 if (tty->flags & TTY_BLOCK) {
545 tty->discarded += len;
546 return;
549 evbuffer_add(tty->out, buf, len);
550 log_debug("%s: %.*s", c->name, (int)len, buf);
551 c->written += len;
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);
559 void
560 tty_puts(struct tty *tty, const char *s)
562 if (*s != '\0')
563 tty_add(tty, s, strlen(s));
566 void
567 tty_putc(struct tty *tty, u_char ch)
569 const char *acs;
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;
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);
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++;
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.
597 if (tty->term->flags & TERM_NOAM)
598 tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
599 } else
600 tty->cx++;
604 void
605 tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
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;
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;
623 static void
624 tty_set_italics(struct tty *tty)
626 const char *s;
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;
635 tty_putcode(tty, TTYC_SMSO);
638 void
639 tty_set_title(struct tty *tty, const char *title)
641 if (!tty_term_has(tty->term, TTYC_TSL) ||
642 !tty_term_has(tty->term, TTYC_FSL))
643 return;
645 tty_putcode(tty, TTYC_TSL);
646 tty_puts(tty, title);
647 tty_putcode(tty, TTYC_FSL);
650 static void
651 tty_force_cursor_colour(struct tty *tty, const char *ccolour)
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);
661 static void
662 tty_update_cursor(struct tty *tty, int mode, int changed, struct screen *s)
664 enum screen_cursor_style cstyle;
666 /* Set cursor colour if changed. */
667 if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
668 tty_force_cursor_colour(tty, s->ccolour);
670 /* If cursor is off, set as invisible. */
671 if (~mode & MODE_CURSOR) {
672 if (changed & MODE_CURSOR)
673 tty_putcode(tty, TTYC_CIVIS);
674 return;
677 /* Check if blinking or very visible flag changed or style changed. */
678 if (s == NULL)
679 cstyle = tty->cstyle;
680 else
681 cstyle = s->cstyle;
682 if ((changed & CURSOR_MODES) == 0 && cstyle == tty->cstyle)
683 return;
686 * Set cursor style. If an explicit style has been set with DECSCUSR,
687 * set it if supported, otherwise send cvvis for blinking styles.
689 * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
690 * if either the blinking or very visible flags are set.
692 tty_putcode(tty, TTYC_CNORM);
693 switch (cstyle) {
694 case SCREEN_CURSOR_DEFAULT:
695 if (tty_term_has(tty->term, TTYC_SE))
696 tty_putcode(tty, TTYC_SE);
697 else
698 tty_putcode1(tty, TTYC_SS, 0);
699 if (mode & (MODE_CURSOR_BLINKING|MODE_CURSOR_VERY_VISIBLE))
700 tty_putcode(tty, TTYC_CVVIS);
701 break;
702 case SCREEN_CURSOR_BLOCK:
703 if (tty_term_has(tty->term, TTYC_SS)) {
704 if (mode & MODE_CURSOR_BLINKING)
705 tty_putcode1(tty, TTYC_SS, 1);
706 else
707 tty_putcode1(tty, TTYC_SS, 2);
708 } else if (mode & MODE_CURSOR_BLINKING)
709 tty_putcode(tty, TTYC_CVVIS);
710 break;
711 case SCREEN_CURSOR_UNDERLINE:
712 if (tty_term_has(tty->term, TTYC_SS)) {
713 if (mode & MODE_CURSOR_BLINKING)
714 tty_putcode1(tty, TTYC_SS, 3);
715 else
716 tty_putcode1(tty, TTYC_SS, 4);
717 } else if (mode & MODE_CURSOR_BLINKING)
718 tty_putcode(tty, TTYC_CVVIS);
719 break;
720 case SCREEN_CURSOR_BAR:
721 if (tty_term_has(tty->term, TTYC_SS)) {
722 if (mode & MODE_CURSOR_BLINKING)
723 tty_putcode1(tty, TTYC_SS, 5);
724 else
725 tty_putcode1(tty, TTYC_SS, 6);
726 } else if (mode & MODE_CURSOR_BLINKING)
727 tty_putcode(tty, TTYC_CVVIS);
728 break;
730 tty->cstyle = cstyle;
733 void
734 tty_update_mode(struct tty *tty, int mode, struct screen *s)
736 struct client *c = tty->client;
737 int changed;
739 if (tty->flags & TTY_NOCURSOR)
740 mode &= ~MODE_CURSOR;
742 changed = mode ^ tty->mode;
743 if (log_get_level() != 0 && changed != 0) {
744 log_debug("%s: current mode %s", c->name,
745 screen_mode_to_string(tty->mode));
746 log_debug("%s: setting mode %s", c->name,
747 screen_mode_to_string(mode));
750 tty_update_cursor(tty, mode, changed, s);
751 if ((changed & ALL_MOUSE_MODES) &&
752 tty_term_has(tty->term, TTYC_KMOUS)) {
754 * If the mouse modes have changed, clear any that are set and
755 * apply again. There are differences in how terminals track
756 * the various bits.
758 if (tty->mode & MODE_MOUSE_SGR)
759 tty_puts(tty, "\033[?1006l");
760 if (tty->mode & MODE_MOUSE_STANDARD)
761 tty_puts(tty, "\033[?1000l");
762 if (tty->mode & MODE_MOUSE_BUTTON)
763 tty_puts(tty, "\033[?1002l");
764 if (tty->mode & MODE_MOUSE_ALL)
765 tty_puts(tty, "\033[?1003l");
766 if (mode & ALL_MOUSE_MODES)
767 tty_puts(tty, "\033[?1006h");
768 if (mode & MODE_MOUSE_STANDARD)
769 tty_puts(tty, "\033[?1000h");
770 if (mode & MODE_MOUSE_BUTTON)
771 tty_puts(tty, "\033[?1002h");
772 if (mode & MODE_MOUSE_ALL)
773 tty_puts(tty, "\033[?1003h");
775 if (changed & MODE_BRACKETPASTE) {
776 if (mode & MODE_BRACKETPASTE)
777 tty_putcode(tty, TTYC_ENBP);
778 else
779 tty_putcode(tty, TTYC_DSBP);
781 tty->mode = mode;
784 static void
785 tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
786 enum tty_code_code code1, u_int n)
788 if (tty_term_has(tty->term, code))
789 tty_putcode1(tty, code, n);
790 else {
791 while (n-- > 0)
792 tty_putcode(tty, code1);
796 static void
797 tty_repeat_space(struct tty *tty, u_int n)
799 static char s[500];
801 if (*s != ' ')
802 memset(s, ' ', sizeof s);
804 while (n > sizeof s) {
805 tty_putn(tty, s, sizeof s, sizeof s);
806 n -= sizeof s;
808 if (n != 0)
809 tty_putn(tty, s, n, n);
812 /* Is this window bigger than the terminal? */
814 tty_window_bigger(struct tty *tty)
816 struct client *c = tty->client;
817 struct window *w = c->session->curw->window;
819 return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
822 /* What offset should this window be drawn at? */
824 tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
826 *ox = tty->oox;
827 *oy = tty->ooy;
828 *sx = tty->osx;
829 *sy = tty->osy;
831 return (tty->oflag);
834 /* What offset should this window be drawn at? */
835 static int
836 tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
838 struct client *c = tty->client;
839 struct window *w = c->session->curw->window;
840 struct window_pane *wp = server_client_get_pane(c);
841 u_int cx, cy, lines;
843 lines = status_line_size(c);
845 if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
846 *ox = 0;
847 *oy = 0;
848 *sx = w->sx;
849 *sy = w->sy;
851 c->pan_window = NULL;
852 return (0);
855 *sx = tty->sx;
856 *sy = tty->sy - lines;
858 if (c->pan_window == w) {
859 if (*sx >= w->sx)
860 c->pan_ox = 0;
861 else if (c->pan_ox + *sx > w->sx)
862 c->pan_ox = w->sx - *sx;
863 *ox = c->pan_ox;
864 if (*sy >= w->sy)
865 c->pan_oy = 0;
866 else if (c->pan_oy + *sy > w->sy)
867 c->pan_oy = w->sy - *sy;
868 *oy = c->pan_oy;
869 return (1);
872 if (~wp->screen->mode & MODE_CURSOR) {
873 *ox = 0;
874 *oy = 0;
875 } else {
876 cx = wp->xoff + wp->screen->cx;
877 cy = wp->yoff + wp->screen->cy;
879 if (cx < *sx)
880 *ox = 0;
881 else if (cx > w->sx - *sx)
882 *ox = w->sx - *sx;
883 else
884 *ox = cx - *sx / 2;
886 if (cy < *sy)
887 *oy = 0;
888 else if (cy > w->sy - *sy)
889 *oy = w->sy - *sy;
890 else
891 *oy = cy - *sy / 2;
894 c->pan_window = NULL;
895 return (1);
898 /* Update stored offsets for a window and redraw if necessary. */
899 void
900 tty_update_window_offset(struct window *w)
902 struct client *c;
904 TAILQ_FOREACH(c, &clients, entry) {
905 if (c->session != NULL && c->session->curw->window == w)
906 tty_update_client_offset(c);
910 /* Update stored offsets for a client and redraw if necessary. */
911 void
912 tty_update_client_offset(struct client *c)
914 u_int ox, oy, sx, sy;
916 if (~c->flags & CLIENT_TERMINAL)
917 return;
919 c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
920 if (ox == c->tty.oox &&
921 oy == c->tty.ooy &&
922 sx == c->tty.osx &&
923 sy == c->tty.osy)
924 return;
926 log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
927 __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
928 ox, oy, sx, sy);
930 c->tty.oox = ox;
931 c->tty.ooy = oy;
932 c->tty.osx = sx;
933 c->tty.osy = sy;
935 c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
939 * Is the region large enough to be worth redrawing once later rather than
940 * probably several times now? Currently yes if it is more than 50% of the
941 * pane.
943 static int
944 tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
946 return (ctx->orlower - ctx->orupper >= ctx->sy / 2);
950 * Return if BCE is needed but the terminal doesn't have it - it'll need to be
951 * emulated.
953 static int
954 tty_fake_bce(const struct tty *tty, const struct grid_cell *gc, u_int bg)
956 if (tty_term_flag(tty->term, TTYC_BCE))
957 return (0);
958 if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc->bg))
959 return (1);
960 return (0);
964 * Redraw scroll region using data from screen (already updated). Used when
965 * CSR not supported, or window is a pane that doesn't take up the full
966 * width of the terminal.
968 static void
969 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
971 struct client *c = tty->client;
972 u_int i;
975 * If region is large, schedule a redraw. In most cases this is likely
976 * to be followed by some more scrolling.
978 if (tty_large_region(tty, ctx)) {
979 log_debug("%s: %s large redraw", __func__, c->name);
980 ctx->redraw_cb(ctx);
981 return;
984 for (i = ctx->orupper; i <= ctx->orlower; i++)
985 tty_draw_pane(tty, ctx, i);
988 /* Is this position visible in the pane? */
989 static int
990 tty_is_visible(__unused struct tty *tty, const struct tty_ctx *ctx, u_int px,
991 u_int py, u_int nx, u_int ny)
993 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
995 if (!ctx->bigger)
996 return (1);
998 if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx ||
999 yoff + ny <= ctx->woy || yoff >= ctx->woy + ctx->wsy)
1000 return (0);
1001 return (1);
1004 /* Clamp line position to visible part of pane. */
1005 static int
1006 tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1007 u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
1009 u_int xoff = ctx->rxoff + px;
1011 if (!tty_is_visible(tty, ctx, px, py, nx, 1))
1012 return (0);
1013 *ry = ctx->yoff + py - ctx->woy;
1015 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1016 /* All visible. */
1017 *i = 0;
1018 *x = ctx->xoff + px - ctx->wox;
1019 *rx = nx;
1020 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1021 /* Both left and right not visible. */
1022 *i = ctx->wox;
1023 *x = 0;
1024 *rx = ctx->wsx;
1025 } else if (xoff < ctx->wox) {
1026 /* Left not visible. */
1027 *i = ctx->wox - (ctx->xoff + px);
1028 *x = 0;
1029 *rx = nx - *i;
1030 } else {
1031 /* Right not visible. */
1032 *i = 0;
1033 *x = (ctx->xoff + px) - ctx->wox;
1034 *rx = ctx->wsx - *x;
1036 if (*rx > nx)
1037 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1039 return (1);
1042 /* Clear a line. */
1043 static void
1044 tty_clear_line(struct tty *tty, const struct grid_cell *defaults, u_int py,
1045 u_int px, u_int nx, u_int bg)
1047 struct client *c = tty->client;
1048 u_int i;
1050 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1052 /* Nothing to clear. */
1053 if (nx == 0)
1054 return;
1056 /* If genuine BCE is available, can try escape sequences. */
1057 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1058 /* Off the end of the line, use EL if available. */
1059 if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
1060 tty_cursor(tty, px, py);
1061 tty_putcode(tty, TTYC_EL);
1062 return;
1065 /* At the start of the line. Use EL1. */
1066 if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1067 tty_cursor(tty, px + nx - 1, py);
1068 tty_putcode(tty, TTYC_EL1);
1069 return;
1072 /* Section of line. Use ECH if possible. */
1073 if (tty_term_has(tty->term, TTYC_ECH)) {
1074 tty_cursor(tty, px, py);
1075 tty_putcode1(tty, TTYC_ECH, nx);
1076 return;
1081 * Couldn't use an escape sequence, use spaces. Clear only the visible
1082 * bit if there is an overlay.
1084 for (i = 0; i < nx; i++) {
1085 if (!tty_check_overlay(tty, px + i, py))
1086 break;
1088 tty_cursor(tty, px, py);
1089 tty_repeat_space(tty, i);
1090 for (; i < nx; i++) {
1091 if (tty_check_overlay(tty, px + i, py))
1092 break;
1094 tty_cursor(tty, px + i, py);
1095 tty_repeat_space(tty, nx - i);
1098 /* Clear a line, adjusting to visible part of pane. */
1099 static void
1100 tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1101 u_int px, u_int nx, u_int bg)
1103 struct client *c = tty->client;
1104 u_int i, x, rx, ry;
1106 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1108 if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1109 tty_clear_line(tty, &ctx->defaults, ry, x, rx, bg);
1112 /* Clamp area position to visible part of pane. */
1113 static int
1114 tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1115 u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1116 u_int *ry)
1118 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1120 if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1121 return (0);
1123 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1124 /* All visible. */
1125 *i = 0;
1126 *x = ctx->xoff + px - ctx->wox;
1127 *rx = nx;
1128 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1129 /* Both left and right not visible. */
1130 *i = ctx->wox;
1131 *x = 0;
1132 *rx = ctx->wsx;
1133 } else if (xoff < ctx->wox) {
1134 /* Left not visible. */
1135 *i = ctx->wox - (ctx->xoff + px);
1136 *x = 0;
1137 *rx = nx - *i;
1138 } else {
1139 /* Right not visible. */
1140 *i = 0;
1141 *x = (ctx->xoff + px) - ctx->wox;
1142 *rx = ctx->wsx - *x;
1144 if (*rx > nx)
1145 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1147 if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) {
1148 /* All visible. */
1149 *j = 0;
1150 *y = ctx->yoff + py - ctx->woy;
1151 *ry = ny;
1152 } else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) {
1153 /* Both top and bottom not visible. */
1154 *j = ctx->woy;
1155 *y = 0;
1156 *ry = ctx->wsy;
1157 } else if (yoff < ctx->woy) {
1158 /* Top not visible. */
1159 *j = ctx->woy - (ctx->yoff + py);
1160 *y = 0;
1161 *ry = ny - *j;
1162 } else {
1163 /* Bottom not visible. */
1164 *j = 0;
1165 *y = (ctx->yoff + py) - ctx->woy;
1166 *ry = ctx->wsy - *y;
1168 if (*ry > ny)
1169 fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1171 return (1);
1174 /* Clear an area, adjusting to visible part of pane. */
1175 static void
1176 tty_clear_area(struct tty *tty, const struct grid_cell *defaults, u_int py,
1177 u_int ny, u_int px, u_int nx, u_int bg)
1179 struct client *c = tty->client;
1180 u_int yy;
1181 char tmp[64];
1183 log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1185 /* Nothing to clear. */
1186 if (nx == 0 || ny == 0)
1187 return;
1189 /* If genuine BCE is available, can try escape sequences. */
1190 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1191 /* Use ED if clearing off the bottom of the terminal. */
1192 if (px == 0 &&
1193 px + nx >= tty->sx &&
1194 py + ny >= tty->sy &&
1195 tty_term_has(tty->term, TTYC_ED)) {
1196 tty_cursor(tty, 0, py);
1197 tty_putcode(tty, TTYC_ED);
1198 return;
1202 * On VT420 compatible terminals we can use DECFRA if the
1203 * background colour isn't default (because it doesn't work
1204 * after SGR 0).
1206 if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1207 xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1208 py + 1, px + 1, py + ny, px + nx);
1209 tty_puts(tty, tmp);
1210 return;
1213 /* Full lines can be scrolled away to clear them. */
1214 if (px == 0 &&
1215 px + nx >= tty->sx &&
1216 ny > 2 &&
1217 tty_term_has(tty->term, TTYC_CSR) &&
1218 tty_term_has(tty->term, TTYC_INDN)) {
1219 tty_region(tty, py, py + ny - 1);
1220 tty_margin_off(tty);
1221 tty_putcode1(tty, TTYC_INDN, ny);
1222 return;
1226 * If margins are supported, can just scroll the area off to
1227 * clear it.
1229 if (nx > 2 &&
1230 ny > 2 &&
1231 tty_term_has(tty->term, TTYC_CSR) &&
1232 tty_use_margin(tty) &&
1233 tty_term_has(tty->term, TTYC_INDN)) {
1234 tty_region(tty, py, py + ny - 1);
1235 tty_margin(tty, px, px + nx - 1);
1236 tty_putcode1(tty, TTYC_INDN, ny);
1237 return;
1241 /* Couldn't use an escape sequence, loop over the lines. */
1242 for (yy = py; yy < py + ny; yy++)
1243 tty_clear_line(tty, defaults, yy, px, nx, bg);
1246 /* Clear an area in a pane. */
1247 static void
1248 tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1249 u_int ny, u_int px, u_int nx, u_int bg)
1251 u_int i, j, x, y, rx, ry;
1253 if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1254 tty_clear_area(tty, &ctx->defaults, y, ry, x, rx, bg);
1257 static void
1258 tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1260 struct screen *s = ctx->s;
1261 u_int nx = ctx->sx, i, x, rx, ry;
1263 log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1265 if (!ctx->bigger) {
1266 tty_draw_line(tty, s, 0, py, nx, ctx->xoff, ctx->yoff + py,
1267 &ctx->defaults, ctx->palette);
1268 return;
1270 if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry)) {
1271 tty_draw_line(tty, s, i, py, rx, x, ry, &ctx->defaults,
1272 ctx->palette);
1276 static const struct grid_cell *
1277 tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1279 static struct grid_cell new;
1280 int c;
1282 /* Characters less than 0x7f are always fine, no matter what. */
1283 if (gc->data.size == 1 && *gc->data.data < 0x7f)
1284 return (gc);
1286 /* UTF-8 terminal and a UTF-8 character - fine. */
1287 if (tty->client->flags & CLIENT_UTF8)
1288 return (gc);
1289 memcpy(&new, gc, sizeof new);
1291 /* See if this can be mapped to an ACS character. */
1292 c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size);
1293 if (c != -1) {
1294 utf8_set(&new.data, c);
1295 new.attr |= GRID_ATTR_CHARSET;
1296 return (&new);
1299 /* Replace by the right number of underscores. */
1300 new.data.size = gc->data.width;
1301 if (new.data.size > UTF8_SIZE)
1302 new.data.size = UTF8_SIZE;
1303 memset(new.data.data, '_', new.data.size);
1304 return (&new);
1307 static int
1308 tty_check_overlay(struct tty *tty, u_int px, u_int py)
1310 struct client *c = tty->client;
1312 if (c->overlay_check == NULL)
1313 return (1);
1314 return (c->overlay_check(c, c->overlay_data, px, py));
1317 void
1318 tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
1319 u_int atx, u_int aty, const struct grid_cell *defaults,
1320 struct colour_palette *palette)
1322 struct grid *gd = s->grid;
1323 struct grid_cell gc, last;
1324 const struct grid_cell *gcp;
1325 struct grid_line *gl;
1326 struct client *c = tty->client;
1327 u_int i, j, ux, sx, width, hidden;
1328 int flags, cleared = 0, wrapped = 0;
1329 char buf[512];
1330 size_t len;
1331 u_int cellsize;
1333 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1334 px, py, nx, atx, aty);
1335 log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg,
1336 defaults->bg);
1339 * py is the line in the screen to draw.
1340 * px is the start x and nx is the width to draw.
1341 * atx,aty is the line on the terminal to draw it.
1344 flags = (tty->flags & TTY_NOCURSOR);
1345 tty->flags |= TTY_NOCURSOR;
1346 tty_update_mode(tty, tty->mode, s);
1348 tty_region_off(tty);
1349 tty_margin_off(tty);
1352 * Clamp the width to cellsize - note this is not cellused, because
1353 * there may be empty background cells after it (from BCE).
1355 sx = screen_size_x(s);
1356 if (nx > sx)
1357 nx = sx;
1358 cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1359 if (sx > cellsize)
1360 sx = cellsize;
1361 if (sx > tty->sx)
1362 sx = tty->sx;
1363 if (sx > nx)
1364 sx = nx;
1365 ux = 0;
1367 if (py == 0)
1368 gl = NULL;
1369 else
1370 gl = grid_get_line(gd, gd->hsize + py - 1);
1371 if (gl == NULL ||
1372 (~gl->flags & GRID_LINE_WRAPPED) ||
1373 atx != 0 ||
1374 tty->cx < tty->sx ||
1375 nx < tty->sx) {
1376 if (nx < tty->sx &&
1377 atx == 0 &&
1378 px + sx != nx &&
1379 tty_term_has(tty->term, TTYC_EL1) &&
1380 !tty_fake_bce(tty, defaults, 8) &&
1381 c->overlay_check == NULL) {
1382 tty_default_attributes(tty, defaults, palette, 8);
1383 tty_cursor(tty, nx - 1, aty);
1384 tty_putcode(tty, TTYC_EL1);
1385 cleared = 1;
1387 } else {
1388 log_debug("%s: wrapped line %u", __func__, aty);
1389 wrapped = 1;
1392 memcpy(&last, &grid_default_cell, sizeof last);
1393 len = 0;
1394 width = 0;
1396 for (i = 0; i < sx; i++) {
1397 grid_view_get_cell(gd, px + i, py, &gc);
1398 gcp = tty_check_codeset(tty, &gc);
1399 if (len != 0 &&
1400 (!tty_check_overlay(tty, atx + ux + width, aty) ||
1401 (gcp->attr & GRID_ATTR_CHARSET) ||
1402 gcp->flags != last.flags ||
1403 gcp->attr != last.attr ||
1404 gcp->fg != last.fg ||
1405 gcp->bg != last.bg ||
1406 gcp->us != last.us ||
1407 ux + width + gcp->data.width > nx ||
1408 (sizeof buf) - len < gcp->data.size)) {
1409 tty_attributes(tty, &last, defaults, palette);
1410 if (last.flags & GRID_FLAG_CLEARED) {
1411 log_debug("%s: %zu cleared", __func__, len);
1412 tty_clear_line(tty, defaults, aty, atx + ux,
1413 width, last.bg);
1414 } else {
1415 if (!wrapped || atx != 0 || ux != 0)
1416 tty_cursor(tty, atx + ux, aty);
1417 tty_putn(tty, buf, len, width);
1419 ux += width;
1421 len = 0;
1422 width = 0;
1423 wrapped = 0;
1426 if (gcp->flags & GRID_FLAG_SELECTED)
1427 screen_select_cell(s, &last, gcp);
1428 else
1429 memcpy(&last, gcp, sizeof last);
1431 hidden = 0;
1432 for (j = 0; j < gcp->data.width; j++) {
1433 if (!tty_check_overlay(tty, atx + ux + j, aty))
1434 hidden++;
1436 if (hidden != 0 && hidden == gcp->data.width) {
1437 if (~gcp->flags & GRID_FLAG_PADDING)
1438 ux += gcp->data.width;
1439 } else if (hidden != 0 || ux + gcp->data.width > nx) {
1440 if (~gcp->flags & GRID_FLAG_PADDING) {
1441 tty_attributes(tty, &last, defaults, palette);
1442 tty_cursor(tty, atx + ux, aty);
1443 for (j = 0; j < gcp->data.width; j++) {
1444 if (ux > nx)
1445 break;
1446 if (tty_check_overlay(tty, atx + ux,
1447 aty))
1448 tty_putc(tty, ' ');
1449 else {
1450 tty_cursor(tty, atx + ux + 1,
1451 aty);
1453 ux++;
1456 } else if (gcp->attr & GRID_ATTR_CHARSET) {
1457 tty_attributes(tty, &last, defaults, palette);
1458 tty_cursor(tty, atx + ux, aty);
1459 for (j = 0; j < gcp->data.size; j++)
1460 tty_putc(tty, gcp->data.data[j]);
1461 ux += gcp->data.width;
1462 } else if (~gcp->flags & GRID_FLAG_PADDING) {
1463 memcpy(buf + len, gcp->data.data, gcp->data.size);
1464 len += gcp->data.size;
1465 width += gcp->data.width;
1468 if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1469 tty_attributes(tty, &last, defaults, palette);
1470 if (last.flags & GRID_FLAG_CLEARED) {
1471 log_debug("%s: %zu cleared (end)", __func__, len);
1472 tty_clear_line(tty, defaults, aty, atx + ux, width,
1473 last.bg);
1474 } else {
1475 if (!wrapped || atx != 0 || ux != 0)
1476 tty_cursor(tty, atx + ux, aty);
1477 tty_putn(tty, buf, len, width);
1479 ux += width;
1482 if (!cleared && ux < nx) {
1483 log_debug("%s: %u to end of line (%zu cleared)", __func__,
1484 nx - ux, len);
1485 tty_default_attributes(tty, defaults, palette, 8);
1486 tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1489 tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1490 tty_update_mode(tty, tty->mode, s);
1493 void
1494 tty_sync_start(struct tty *tty)
1496 if (tty->flags & TTY_BLOCK)
1497 return;
1498 if (tty->flags & TTY_SYNCING)
1499 return;
1500 tty->flags |= TTY_SYNCING;
1502 if (tty_term_has(tty->term, TTYC_SYNC)) {
1503 log_debug("%s sync start", tty->client->name);
1504 tty_putcode1(tty, TTYC_SYNC, 1);
1508 void
1509 tty_sync_end(struct tty *tty)
1511 if (tty->flags & TTY_BLOCK)
1512 return;
1513 if (~tty->flags & TTY_SYNCING)
1514 return;
1515 tty->flags &= ~TTY_SYNCING;
1517 if (tty_term_has(tty->term, TTYC_SYNC)) {
1518 log_debug("%s sync end", tty->client->name);
1519 tty_putcode1(tty, TTYC_SYNC, 2);
1523 static int
1524 tty_client_ready(struct client *c)
1526 if (c->session == NULL || c->tty.term == NULL)
1527 return (0);
1528 if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1529 return (0);
1530 if (c->tty.flags & TTY_FREEZE)
1531 return (0);
1532 return (1);
1535 void
1536 tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1537 struct tty_ctx *ctx)
1539 struct client *c;
1540 int state;
1542 if (ctx->set_client_cb == NULL)
1543 return;
1544 TAILQ_FOREACH(c, &clients, entry) {
1545 if (!tty_client_ready(c))
1546 continue;
1547 state = ctx->set_client_cb(ctx, c);
1548 if (state == -1)
1549 break;
1550 if (state == 0)
1551 continue;
1552 cmdfn(&c->tty, ctx);
1556 void
1557 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1559 struct client *c = tty->client;
1561 if (ctx->bigger ||
1562 !tty_full_width(tty, ctx) ||
1563 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1564 (!tty_term_has(tty->term, TTYC_ICH) &&
1565 !tty_term_has(tty->term, TTYC_ICH1)) ||
1566 c->overlay_check != NULL) {
1567 tty_draw_pane(tty, ctx, ctx->ocy);
1568 return;
1571 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1573 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1575 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1578 void
1579 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1581 struct client *c = tty->client;
1583 if (ctx->bigger ||
1584 !tty_full_width(tty, ctx) ||
1585 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1586 (!tty_term_has(tty->term, TTYC_DCH) &&
1587 !tty_term_has(tty->term, TTYC_DCH1)) ||
1588 c->overlay_check != NULL) {
1589 tty_draw_pane(tty, ctx, ctx->ocy);
1590 return;
1593 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1595 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1597 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1600 void
1601 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1603 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1605 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1608 void
1609 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1611 struct client *c = tty->client;
1613 if (ctx->bigger ||
1614 !tty_full_width(tty, ctx) ||
1615 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1616 !tty_term_has(tty->term, TTYC_CSR) ||
1617 !tty_term_has(tty->term, TTYC_IL1) ||
1618 ctx->sx == 1 ||
1619 ctx->sy == 1 ||
1620 c->overlay_check != NULL) {
1621 tty_redraw_region(tty, ctx);
1622 return;
1625 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1627 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1628 tty_margin_off(tty);
1629 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1631 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1632 tty->cx = tty->cy = UINT_MAX;
1635 void
1636 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1638 struct client *c = tty->client;
1640 if (ctx->bigger ||
1641 !tty_full_width(tty, ctx) ||
1642 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1643 !tty_term_has(tty->term, TTYC_CSR) ||
1644 !tty_term_has(tty->term, TTYC_DL1) ||
1645 ctx->sx == 1 ||
1646 ctx->sy == 1 ||
1647 c->overlay_check != NULL) {
1648 tty_redraw_region(tty, ctx);
1649 return;
1652 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1654 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1655 tty_margin_off(tty);
1656 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1658 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1659 tty->cx = tty->cy = UINT_MAX;
1662 void
1663 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1665 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1667 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1670 void
1671 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1673 u_int nx = ctx->sx - ctx->ocx;
1675 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1677 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1680 void
1681 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1683 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1685 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1688 void
1689 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1691 struct client *c = tty->client;
1693 if (ctx->ocy != ctx->orupper)
1694 return;
1696 if (ctx->bigger ||
1697 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1698 tty_fake_bce(tty, &ctx->defaults, 8) ||
1699 !tty_term_has(tty->term, TTYC_CSR) ||
1700 (!tty_term_has(tty->term, TTYC_RI) &&
1701 !tty_term_has(tty->term, TTYC_RIN)) ||
1702 ctx->sx == 1 ||
1703 ctx->sy == 1 ||
1704 c->overlay_check != NULL) {
1705 tty_redraw_region(tty, ctx);
1706 return;
1709 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1711 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1712 tty_margin_pane(tty, ctx);
1713 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1715 if (tty_term_has(tty->term, TTYC_RI))
1716 tty_putcode(tty, TTYC_RI);
1717 else
1718 tty_putcode1(tty, TTYC_RIN, 1);
1721 void
1722 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1724 struct client *c = tty->client;
1726 if (ctx->ocy != ctx->orlower)
1727 return;
1729 if (ctx->bigger ||
1730 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1731 tty_fake_bce(tty, &ctx->defaults, 8) ||
1732 !tty_term_has(tty->term, TTYC_CSR) ||
1733 ctx->sx == 1 ||
1734 ctx->sy == 1 ||
1735 c->overlay_check != NULL) {
1736 tty_redraw_region(tty, ctx);
1737 return;
1740 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1742 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1743 tty_margin_pane(tty, ctx);
1746 * If we want to wrap a pane while using margins, the cursor needs to
1747 * be exactly on the right of the region. If the cursor is entirely off
1748 * the edge - move it back to the right. Some terminals are funny about
1749 * this and insert extra spaces, so only use the right if margins are
1750 * enabled.
1752 if (ctx->xoff + ctx->ocx > tty->rright) {
1753 if (!tty_use_margin(tty))
1754 tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1755 else
1756 tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1757 } else
1758 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1760 tty_putc(tty, '\n');
1763 void
1764 tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1766 struct client *c = tty->client;
1767 u_int i;
1769 if (ctx->bigger ||
1770 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1771 tty_fake_bce(tty, &ctx->defaults, 8) ||
1772 !tty_term_has(tty->term, TTYC_CSR) ||
1773 ctx->sx == 1 ||
1774 ctx->sy == 1 ||
1775 c->overlay_check != NULL) {
1776 tty_redraw_region(tty, ctx);
1777 return;
1780 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1782 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1783 tty_margin_pane(tty, ctx);
1785 if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1786 if (!tty_use_margin(tty))
1787 tty_cursor(tty, 0, tty->rlower);
1788 else
1789 tty_cursor(tty, tty->rright, tty->rlower);
1790 for (i = 0; i < ctx->num; i++)
1791 tty_putc(tty, '\n');
1792 } else {
1793 if (tty->cy == UINT_MAX)
1794 tty_cursor(tty, 0, 0);
1795 else
1796 tty_cursor(tty, 0, tty->cy);
1797 tty_putcode1(tty, TTYC_INDN, ctx->num);
1801 void
1802 tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
1804 u_int i;
1805 struct client *c = tty->client;
1807 if (ctx->bigger ||
1808 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1809 tty_fake_bce(tty, &ctx->defaults, 8) ||
1810 !tty_term_has(tty->term, TTYC_CSR) ||
1811 (!tty_term_has(tty->term, TTYC_RI) &&
1812 !tty_term_has(tty->term, TTYC_RIN)) ||
1813 ctx->sx == 1 ||
1814 ctx->sy == 1 ||
1815 c->overlay_check != NULL) {
1816 tty_redraw_region(tty, ctx);
1817 return;
1820 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1822 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1823 tty_margin_pane(tty, ctx);
1824 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1826 if (tty_term_has(tty->term, TTYC_RIN))
1827 tty_putcode1(tty, TTYC_RIN, ctx->num);
1828 else {
1829 for (i = 0; i < ctx->num; i++)
1830 tty_putcode(tty, TTYC_RI);
1834 void
1835 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1837 u_int px, py, nx, ny;
1839 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1841 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1842 tty_margin_off(tty);
1844 px = 0;
1845 nx = ctx->sx;
1846 py = ctx->ocy + 1;
1847 ny = ctx->sy - ctx->ocy - 1;
1849 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1851 px = ctx->ocx;
1852 nx = ctx->sx - ctx->ocx;
1853 py = ctx->ocy;
1855 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1858 void
1859 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1861 u_int px, py, nx, ny;
1863 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1865 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1866 tty_margin_off(tty);
1868 px = 0;
1869 nx = ctx->sx;
1870 py = 0;
1871 ny = ctx->ocy;
1873 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1875 px = 0;
1876 nx = ctx->ocx + 1;
1877 py = ctx->ocy;
1879 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1882 void
1883 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1885 u_int px, py, nx, ny;
1887 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1889 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1890 tty_margin_off(tty);
1892 px = 0;
1893 nx = ctx->sx;
1894 py = 0;
1895 ny = ctx->sy;
1897 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1900 void
1901 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1903 u_int i, j;
1905 if (ctx->bigger) {
1906 ctx->redraw_cb(ctx);
1907 return;
1910 tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette);
1912 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1913 tty_margin_off(tty);
1915 for (j = 0; j < ctx->sy; j++) {
1916 tty_cursor_pane(tty, ctx, 0, j);
1917 for (i = 0; i < ctx->sx; i++)
1918 tty_putc(tty, 'E');
1922 void
1923 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1925 const struct grid_cell *gcp = ctx->cell;
1926 struct screen *s = ctx->s;
1927 u_int i, px, py;
1929 px = ctx->xoff + ctx->ocx - ctx->wox;
1930 py = ctx->yoff + ctx->ocy - ctx->woy;
1931 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
1932 (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
1933 return;
1935 /* Handle partially obstructed wide characters. */
1936 if (gcp->data.width > 1) {
1937 for (i = 0; i < gcp->data.width; i++) {
1938 if (!tty_check_overlay(tty, px + i, py)) {
1939 tty_draw_line(tty, s, s->cx, s->cy,
1940 gcp->data.width, px, py, &ctx->defaults,
1941 ctx->palette);
1942 return;
1947 if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
1948 ctx->ocy == ctx->orlower &&
1949 tty_full_width(tty, ctx))
1950 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1952 tty_margin_off(tty);
1953 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1955 tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette);
1958 void
1959 tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
1961 u_int i, hide = 0;
1963 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
1964 return;
1966 if (ctx->bigger &&
1967 (ctx->xoff + ctx->ocx < ctx->wox ||
1968 ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
1969 if (!ctx->wrapped ||
1970 !tty_full_width(tty, ctx) ||
1971 (tty->term->flags & TERM_NOAM) ||
1972 ctx->xoff + ctx->ocx != 0 ||
1973 ctx->yoff + ctx->ocy != tty->cy + 1 ||
1974 tty->cx < tty->sx ||
1975 tty->cy == tty->rlower)
1976 tty_draw_pane(tty, ctx, ctx->ocy);
1977 else
1978 ctx->redraw_cb(ctx);
1979 return;
1982 tty_margin_off(tty);
1983 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1985 tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette);
1986 for (i = 0; i < ctx->num; i++) {
1987 if (!tty_check_overlay(tty, tty->cx + i, tty->cy))
1988 break;
1990 tty_putn(tty, ctx->ptr, i, i);
1991 for (; i < ctx->num; i++) {
1992 if (tty_check_overlay(tty, tty->cx + hide, tty->cy))
1993 break;
1994 hide++;
1996 tty_cursor(tty, tty->cx + hide, tty->cy);
1997 tty_putn(tty, (char *)ctx->ptr + i, ctx->num - i, ctx->num - i);
2000 void
2001 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
2003 tty_set_selection(tty, ctx->ptr, ctx->num);
2006 void
2007 tty_set_selection(struct tty *tty, const char *buf, size_t len)
2009 char *encoded;
2010 size_t size;
2012 if (~tty->flags & TTY_STARTED)
2013 return;
2014 if (!tty_term_has(tty->term, TTYC_MS))
2015 return;
2017 size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
2018 encoded = xmalloc(size);
2020 b64_ntop(buf, len, encoded, size);
2021 tty_putcode_ptr2(tty, TTYC_MS, "", encoded);
2022 tty->client->redraw = EVBUFFER_LENGTH(tty->out);
2024 free(encoded);
2027 void
2028 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
2030 tty_add(tty, ctx->ptr, ctx->num);
2031 tty_invalidate(tty);
2034 void
2035 tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
2037 if (ctx->num == 0x11) {
2039 * This is an overlay and a command that moves the cursor so
2040 * start synchronized updates.
2042 tty_sync_start(tty);
2043 } else if (~ctx->num & 0x10) {
2045 * This is a pane. If there is an overlay, always start;
2046 * otherwise, only if requested.
2048 if (ctx->num || tty->client->overlay_draw != NULL)
2049 tty_sync_start(tty);
2053 void
2054 tty_cell(struct tty *tty, const struct grid_cell *gc,
2055 const struct grid_cell *defaults, struct colour_palette *palette)
2057 const struct grid_cell *gcp;
2059 /* Skip last character if terminal is stupid. */
2060 if ((tty->term->flags & TERM_NOAM) &&
2061 tty->cy == tty->sy - 1 &&
2062 tty->cx == tty->sx - 1)
2063 return;
2065 /* If this is a padding character, do nothing. */
2066 if (gc->flags & GRID_FLAG_PADDING)
2067 return;
2069 /* Check the output codeset and apply attributes. */
2070 gcp = tty_check_codeset(tty, gc);
2071 tty_attributes(tty, gcp, defaults, palette);
2073 /* If it is a single character, write with putc to handle ACS. */
2074 if (gcp->data.size == 1) {
2075 tty_attributes(tty, gcp, defaults, palette);
2076 if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
2077 return;
2078 tty_putc(tty, *gcp->data.data);
2079 return;
2082 /* Write the data. */
2083 tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
2086 void
2087 tty_reset(struct tty *tty)
2089 struct grid_cell *gc = &tty->cell;
2091 if (!grid_cells_equal(gc, &grid_default_cell)) {
2092 if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2093 tty_putcode(tty, TTYC_RMACS);
2094 tty_putcode(tty, TTYC_SGR0);
2095 memcpy(gc, &grid_default_cell, sizeof *gc);
2097 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2100 static void
2101 tty_invalidate(struct tty *tty)
2103 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2104 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2106 tty->cx = tty->cy = UINT_MAX;
2107 tty->rupper = tty->rleft = UINT_MAX;
2108 tty->rlower = tty->rright = UINT_MAX;
2110 if (tty->flags & TTY_STARTED) {
2111 if (tty_use_margin(tty))
2112 tty_putcode(tty, TTYC_ENMG);
2113 tty_putcode(tty, TTYC_SGR0);
2115 tty->mode = ALL_MODES;
2116 tty_update_mode(tty, MODE_CURSOR, NULL);
2118 tty_cursor(tty, 0, 0);
2119 tty_region_off(tty);
2120 tty_margin_off(tty);
2121 } else
2122 tty->mode = MODE_CURSOR;
2125 /* Turn off margin. */
2126 void
2127 tty_region_off(struct tty *tty)
2129 tty_region(tty, 0, tty->sy - 1);
2132 /* Set region inside pane. */
2133 static void
2134 tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2135 u_int rlower)
2137 tty_region(tty, ctx->yoff + rupper - ctx->woy,
2138 ctx->yoff + rlower - ctx->woy);
2141 /* Set region at absolute position. */
2142 static void
2143 tty_region(struct tty *tty, u_int rupper, u_int rlower)
2145 if (tty->rlower == rlower && tty->rupper == rupper)
2146 return;
2147 if (!tty_term_has(tty->term, TTYC_CSR))
2148 return;
2150 tty->rupper = rupper;
2151 tty->rlower = rlower;
2154 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2155 * 0,0 if it is beyond the last column (they do not reset their wrap
2156 * flag so further output causes a line feed). As a workaround, do an
2157 * explicit move to 0 first.
2159 if (tty->cx >= tty->sx) {
2160 if (tty->cy == UINT_MAX)
2161 tty_cursor(tty, 0, 0);
2162 else
2163 tty_cursor(tty, 0, tty->cy);
2166 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2167 tty->cx = tty->cy = UINT_MAX;
2170 /* Turn off margin. */
2171 void
2172 tty_margin_off(struct tty *tty)
2174 tty_margin(tty, 0, tty->sx - 1);
2177 /* Set margin inside pane. */
2178 static void
2179 tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2181 tty_margin(tty, ctx->xoff - ctx->wox,
2182 ctx->xoff + ctx->sx - 1 - ctx->wox);
2185 /* Set margin at absolute position. */
2186 static void
2187 tty_margin(struct tty *tty, u_int rleft, u_int rright)
2189 if (!tty_use_margin(tty))
2190 return;
2191 if (tty->rleft == rleft && tty->rright == rright)
2192 return;
2194 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2196 tty->rleft = rleft;
2197 tty->rright = rright;
2199 if (rleft == 0 && rright == tty->sx - 1)
2200 tty_putcode(tty, TTYC_CLMG);
2201 else
2202 tty_putcode2(tty, TTYC_CMG, rleft, rright);
2203 tty->cx = tty->cy = UINT_MAX;
2207 * Move the cursor, unless it would wrap itself when the next character is
2208 * printed.
2210 static void
2211 tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2212 u_int cx, u_int cy)
2214 if (!ctx->wrapped ||
2215 !tty_full_width(tty, ctx) ||
2216 (tty->term->flags & TERM_NOAM) ||
2217 ctx->xoff + cx != 0 ||
2218 ctx->yoff + cy != tty->cy + 1 ||
2219 tty->cx < tty->sx ||
2220 tty->cy == tty->rlower)
2221 tty_cursor_pane(tty, ctx, cx, cy);
2222 else
2223 log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
2226 /* Move cursor inside pane. */
2227 static void
2228 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2230 tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
2233 /* Move cursor to absolute position. */
2234 void
2235 tty_cursor(struct tty *tty, u_int cx, u_int cy)
2237 struct tty_term *term = tty->term;
2238 u_int thisx, thisy;
2239 int change;
2241 if (tty->flags & TTY_BLOCK)
2242 return;
2244 if (cx > tty->sx - 1)
2245 cx = tty->sx - 1;
2247 thisx = tty->cx;
2248 thisy = tty->cy;
2250 /* No change. */
2251 if (cx == thisx && cy == thisy)
2252 return;
2254 /* Very end of the line, just use absolute movement. */
2255 if (thisx > tty->sx - 1)
2256 goto absolute;
2258 /* Move to home position (0, 0). */
2259 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2260 tty_putcode(tty, TTYC_HOME);
2261 goto out;
2264 /* Zero on the next line. */
2265 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2266 (!tty_use_margin(tty) || tty->rleft == 0)) {
2267 tty_putc(tty, '\r');
2268 tty_putc(tty, '\n');
2269 goto out;
2272 /* Moving column or row. */
2273 if (cy == thisy) {
2275 * Moving column only, row staying the same.
2278 /* To left edge. */
2279 if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
2280 tty_putc(tty, '\r');
2281 goto out;
2284 /* One to the left. */
2285 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2286 tty_putcode(tty, TTYC_CUB1);
2287 goto out;
2290 /* One to the right. */
2291 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2292 tty_putcode(tty, TTYC_CUF1);
2293 goto out;
2296 /* Calculate difference. */
2297 change = thisx - cx; /* +ve left, -ve right */
2300 * Use HPA if change is larger than absolute, otherwise move
2301 * the cursor with CUB/CUF.
2303 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
2304 tty_putcode1(tty, TTYC_HPA, cx);
2305 goto out;
2306 } else if (change > 0 &&
2307 tty_term_has(term, TTYC_CUB) &&
2308 !tty_use_margin(tty)) {
2309 if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2310 tty_putcode(tty, TTYC_CUB1);
2311 tty_putcode(tty, TTYC_CUB1);
2312 goto out;
2314 tty_putcode1(tty, TTYC_CUB, change);
2315 goto out;
2316 } else if (change < 0 &&
2317 tty_term_has(term, TTYC_CUF) &&
2318 !tty_use_margin(tty)) {
2319 tty_putcode1(tty, TTYC_CUF, -change);
2320 goto out;
2322 } else if (cx == thisx) {
2324 * Moving row only, column staying the same.
2327 /* One above. */
2328 if (thisy != tty->rupper &&
2329 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2330 tty_putcode(tty, TTYC_CUU1);
2331 goto out;
2334 /* One below. */
2335 if (thisy != tty->rlower &&
2336 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2337 tty_putcode(tty, TTYC_CUD1);
2338 goto out;
2341 /* Calculate difference. */
2342 change = thisy - cy; /* +ve up, -ve down */
2345 * Try to use VPA if change is larger than absolute or if this
2346 * change would cross the scroll region, otherwise use CUU/CUD.
2348 if ((u_int) abs(change) > cy ||
2349 (change < 0 && cy - change > tty->rlower) ||
2350 (change > 0 && cy - change < tty->rupper)) {
2351 if (tty_term_has(term, TTYC_VPA)) {
2352 tty_putcode1(tty, TTYC_VPA, cy);
2353 goto out;
2355 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2356 tty_putcode1(tty, TTYC_CUU, change);
2357 goto out;
2358 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2359 tty_putcode1(tty, TTYC_CUD, -change);
2360 goto out;
2364 absolute:
2365 /* Absolute movement. */
2366 tty_putcode2(tty, TTYC_CUP, cy, cx);
2368 out:
2369 tty->cx = cx;
2370 tty->cy = cy;
2373 void
2374 tty_attributes(struct tty *tty, const struct grid_cell *gc,
2375 const struct grid_cell *defaults, struct colour_palette *palette)
2377 struct grid_cell *tc = &tty->cell, gc2;
2378 int changed;
2380 /* Copy cell and update default colours. */
2381 memcpy(&gc2, gc, sizeof gc2);
2382 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2383 if (gc2.fg == 8)
2384 gc2.fg = defaults->fg;
2385 if (gc2.bg == 8)
2386 gc2.bg = defaults->bg;
2389 /* Ignore cell if it is the same as the last one. */
2390 if (gc2.attr == tty->last_cell.attr &&
2391 gc2.fg == tty->last_cell.fg &&
2392 gc2.bg == tty->last_cell.bg &&
2393 gc2.us == tty->last_cell.us)
2394 return;
2397 * If no setab, try to use the reverse attribute as a best-effort for a
2398 * non-default background. This is a bit of a hack but it doesn't do
2399 * any serious harm and makes a couple of applications happier.
2401 if (!tty_term_has(tty->term, TTYC_SETAB)) {
2402 if (gc2.attr & GRID_ATTR_REVERSE) {
2403 if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
2404 gc2.attr &= ~GRID_ATTR_REVERSE;
2405 } else {
2406 if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
2407 gc2.attr |= GRID_ATTR_REVERSE;
2411 /* Fix up the colours if necessary. */
2412 tty_check_fg(tty, palette, &gc2);
2413 tty_check_bg(tty, palette, &gc2);
2414 tty_check_us(tty, palette, &gc2);
2417 * If any bits are being cleared or the underline colour is now default,
2418 * reset everything.
2420 if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
2421 tty_reset(tty);
2424 * Set the colours. This may call tty_reset() (so it comes next) and
2425 * may add to (NOT remove) the desired attributes.
2427 tty_colours(tty, &gc2);
2429 /* Filter out attribute bits already set. */
2430 changed = gc2.attr & ~tc->attr;
2431 tc->attr = gc2.attr;
2433 /* Set the attributes. */
2434 if (changed & GRID_ATTR_BRIGHT)
2435 tty_putcode(tty, TTYC_BOLD);
2436 if (changed & GRID_ATTR_DIM)
2437 tty_putcode(tty, TTYC_DIM);
2438 if (changed & GRID_ATTR_ITALICS)
2439 tty_set_italics(tty);
2440 if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2441 if ((changed & GRID_ATTR_UNDERSCORE) ||
2442 !tty_term_has(tty->term, TTYC_SMULX))
2443 tty_putcode(tty, TTYC_SMUL);
2444 else if (changed & GRID_ATTR_UNDERSCORE_2)
2445 tty_putcode1(tty, TTYC_SMULX, 2);
2446 else if (changed & GRID_ATTR_UNDERSCORE_3)
2447 tty_putcode1(tty, TTYC_SMULX, 3);
2448 else if (changed & GRID_ATTR_UNDERSCORE_4)
2449 tty_putcode1(tty, TTYC_SMULX, 4);
2450 else if (changed & GRID_ATTR_UNDERSCORE_5)
2451 tty_putcode1(tty, TTYC_SMULX, 5);
2453 if (changed & GRID_ATTR_BLINK)
2454 tty_putcode(tty, TTYC_BLINK);
2455 if (changed & GRID_ATTR_REVERSE) {
2456 if (tty_term_has(tty->term, TTYC_REV))
2457 tty_putcode(tty, TTYC_REV);
2458 else if (tty_term_has(tty->term, TTYC_SMSO))
2459 tty_putcode(tty, TTYC_SMSO);
2461 if (changed & GRID_ATTR_HIDDEN)
2462 tty_putcode(tty, TTYC_INVIS);
2463 if (changed & GRID_ATTR_STRIKETHROUGH)
2464 tty_putcode(tty, TTYC_SMXX);
2465 if (changed & GRID_ATTR_OVERLINE)
2466 tty_putcode(tty, TTYC_SMOL);
2467 if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2468 tty_putcode(tty, TTYC_SMACS);
2470 memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
2473 static void
2474 tty_colours(struct tty *tty, const struct grid_cell *gc)
2476 struct grid_cell *tc = &tty->cell;
2477 int have_ax;
2479 /* No changes? Nothing is necessary. */
2480 if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
2481 return;
2484 * Is either the default colour? This is handled specially because the
2485 * best solution might be to reset both colours to default, in which
2486 * case if only one is default need to fall onward to set the other
2487 * colour.
2489 if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
2491 * If don't have AX but do have op, send sgr0 (op can't
2492 * actually be used because it is sometimes the same as sgr0
2493 * and sometimes isn't). This resets both colours to default.
2495 * Otherwise, try to set the default colour only as needed.
2497 have_ax = tty_term_flag(tty->term, TTYC_AX);
2498 if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2499 tty_reset(tty);
2500 else {
2501 if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
2502 if (have_ax)
2503 tty_puts(tty, "\033[39m");
2504 else if (tc->fg != 7)
2505 tty_putcode1(tty, TTYC_SETAF, 7);
2506 tc->fg = gc->fg;
2508 if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
2509 if (have_ax)
2510 tty_puts(tty, "\033[49m");
2511 else if (tc->bg != 0)
2512 tty_putcode1(tty, TTYC_SETAB, 0);
2513 tc->bg = gc->bg;
2518 /* Set the foreground colour. */
2519 if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
2520 tty_colours_fg(tty, gc);
2523 * Set the background colour. This must come after the foreground as
2524 * tty_colour_fg() can call tty_reset().
2526 if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
2527 tty_colours_bg(tty, gc);
2529 /* Set the underscore colour. */
2530 if (gc->us != tc->us)
2531 tty_colours_us(tty, gc);
2534 static void
2535 tty_check_fg(struct tty *tty, struct colour_palette *palette,
2536 struct grid_cell *gc)
2538 u_char r, g, b;
2539 u_int colours;
2540 int c;
2543 * Perform substitution if this pane has a palette. If the bright
2544 * attribute is set, use the bright entry in the palette by changing to
2545 * the aixterm colour.
2547 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2548 c = gc->fg;
2549 if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
2550 c += 90;
2551 if ((c = colour_palette_get(palette, c)) != -1)
2552 gc->fg = c;
2555 /* Is this a 24-bit colour? */
2556 if (gc->fg & COLOUR_FLAG_RGB) {
2557 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2558 if (tty->term->flags & TERM_RGBCOLOURS)
2559 return;
2560 colour_split_rgb(gc->fg, &r, &g, &b);
2561 gc->fg = colour_find_rgb(r, g, b);
2564 /* How many colours does this terminal have? */
2565 if (tty->term->flags & TERM_256COLOURS)
2566 colours = 256;
2567 else
2568 colours = tty_term_number(tty->term, TTYC_COLORS);
2570 /* Is this a 256-colour colour? */
2571 if (gc->fg & COLOUR_FLAG_256) {
2572 /* And not a 256 colour mode? */
2573 if (colours < 256) {
2574 gc->fg = colour_256to16(gc->fg);
2575 if (gc->fg & 8) {
2576 gc->fg &= 7;
2577 if (colours >= 16)
2578 gc->fg += 90;
2581 return;
2584 /* Is this an aixterm colour? */
2585 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2586 gc->fg -= 90;
2587 gc->attr |= GRID_ATTR_BRIGHT;
2591 static void
2592 tty_check_bg(struct tty *tty, struct colour_palette *palette,
2593 struct grid_cell *gc)
2595 u_char r, g, b;
2596 u_int colours;
2597 int c;
2599 /* Perform substitution if this pane has a palette. */
2600 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2601 if ((c = colour_palette_get(palette, gc->bg)) != -1)
2602 gc->bg = c;
2605 /* Is this a 24-bit colour? */
2606 if (gc->bg & COLOUR_FLAG_RGB) {
2607 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2608 if (tty->term->flags & TERM_RGBCOLOURS)
2609 return;
2610 colour_split_rgb(gc->bg, &r, &g, &b);
2611 gc->bg = colour_find_rgb(r, g, b);
2614 /* How many colours does this terminal have? */
2615 if (tty->term->flags & TERM_256COLOURS)
2616 colours = 256;
2617 else
2618 colours = tty_term_number(tty->term, TTYC_COLORS);
2620 /* Is this a 256-colour colour? */
2621 if (gc->bg & COLOUR_FLAG_256) {
2623 * And not a 256 colour mode? Translate to 16-colour
2624 * palette. Bold background doesn't exist portably, so just
2625 * discard the bold bit if set.
2627 if (colours < 256) {
2628 gc->bg = colour_256to16(gc->bg);
2629 if (gc->bg & 8) {
2630 gc->bg &= 7;
2631 if (colours >= 16)
2632 gc->bg += 90;
2635 return;
2638 /* Is this an aixterm colour? */
2639 if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
2640 gc->bg -= 90;
2643 static void
2644 tty_check_us(__unused struct tty *tty, struct colour_palette *palette,
2645 struct grid_cell *gc)
2647 int c;
2649 /* Perform substitution if this pane has a palette. */
2650 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2651 if ((c = colour_palette_get(palette, gc->us)) != -1)
2652 gc->us = c;
2655 /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2656 if (gc->us & COLOUR_FLAG_256)
2657 gc->us = colour_256toRGB (gc->us);
2660 static void
2661 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
2663 struct grid_cell *tc = &tty->cell;
2664 char s[32];
2666 /* Is this a 24-bit or 256-colour colour? */
2667 if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
2668 if (tty_try_colour(tty, gc->fg, "38") == 0)
2669 goto save;
2670 /* Should not get here, already converted in tty_check_fg. */
2671 return;
2674 /* Is this an aixterm bright colour? */
2675 if (gc->fg >= 90 && gc->fg <= 97) {
2676 if (tty->term->flags & TERM_256COLOURS) {
2677 xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2678 tty_puts(tty, s);
2679 } else
2680 tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
2681 goto save;
2684 /* Otherwise set the foreground colour. */
2685 tty_putcode1(tty, TTYC_SETAF, gc->fg);
2687 save:
2688 /* Save the new values in the terminal current cell. */
2689 tc->fg = gc->fg;
2692 static void
2693 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
2695 struct grid_cell *tc = &tty->cell;
2696 char s[32];
2698 /* Is this a 24-bit or 256-colour colour? */
2699 if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
2700 if (tty_try_colour(tty, gc->bg, "48") == 0)
2701 goto save;
2702 /* Should not get here, already converted in tty_check_bg. */
2703 return;
2706 /* Is this an aixterm bright colour? */
2707 if (gc->bg >= 90 && gc->bg <= 97) {
2708 if (tty->term->flags & TERM_256COLOURS) {
2709 xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2710 tty_puts(tty, s);
2711 } else
2712 tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
2713 goto save;
2716 /* Otherwise set the background colour. */
2717 tty_putcode1(tty, TTYC_SETAB, gc->bg);
2719 save:
2720 /* Save the new values in the terminal current cell. */
2721 tc->bg = gc->bg;
2724 static void
2725 tty_colours_us(struct tty *tty, const struct grid_cell *gc)
2727 struct grid_cell *tc = &tty->cell;
2728 u_int c;
2729 u_char r, g, b;
2731 /* Clear underline colour. */
2732 if (gc->us == 0) {
2733 tty_putcode(tty, TTYC_OL);
2734 goto save;
2737 /* Must be an RGB colour - this should never happen. */
2738 if (~gc->us & COLOUR_FLAG_RGB)
2739 return;
2742 * Setulc and setal follows the ncurses(3) one argument "direct colour"
2743 * capability format. Calculate the colour value.
2745 colour_split_rgb(gc->us, &r, &g, &b);
2746 c = (65536 * r) + (256 * g) + b;
2749 * Write the colour. Only use setal if the RGB flag is set because the
2750 * non-RGB version may be wrong.
2752 if (tty_term_has(tty->term, TTYC_SETULC))
2753 tty_putcode1(tty, TTYC_SETULC, c);
2754 else if (tty_term_has(tty->term, TTYC_SETAL) &&
2755 tty_term_has(tty->term, TTYC_RGB))
2756 tty_putcode1(tty, TTYC_SETAL, c);
2758 save:
2759 /* Save the new values in the terminal current cell. */
2760 tc->us = gc->us;
2763 static int
2764 tty_try_colour(struct tty *tty, int colour, const char *type)
2766 u_char r, g, b;
2768 if (colour & COLOUR_FLAG_256) {
2769 if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
2770 tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2771 else if (tty_term_has(tty->term, TTYC_SETAB))
2772 tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2773 return (0);
2776 if (colour & COLOUR_FLAG_RGB) {
2777 colour_split_rgb(colour & 0xffffff, &r, &g, &b);
2778 if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
2779 tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
2780 else if (tty_term_has(tty->term, TTYC_SETRGBB))
2781 tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
2782 return (0);
2785 return (-1);
2788 static void
2789 tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
2791 memcpy(gc, &grid_default_cell, sizeof *gc);
2792 gc->fg = wp->palette.fg;
2793 gc->bg = wp->palette.bg;
2796 void
2797 tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
2799 struct options *oo = wp->options;
2800 struct format_tree *ft;
2802 memcpy(gc, &grid_default_cell, sizeof *gc);
2804 if (wp->flags & PANE_STYLECHANGED) {
2805 log_debug("%%%u: style changed", wp->id);
2806 wp->flags &= ~PANE_STYLECHANGED;
2808 ft = format_create(NULL, NULL, FORMAT_PANE|wp->id,
2809 FORMAT_NOJOBS);
2810 format_defaults(ft, NULL, NULL, NULL, wp);
2811 tty_window_default_style(&wp->cached_active_gc, wp);
2812 style_add(&wp->cached_active_gc, oo, "window-active-style", ft);
2813 tty_window_default_style(&wp->cached_gc, wp);
2814 style_add(&wp->cached_gc, oo, "window-style", ft);
2815 format_free(ft);
2818 if (gc->fg == 8) {
2819 if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
2820 gc->fg = wp->cached_active_gc.fg;
2821 else
2822 gc->fg = wp->cached_gc.fg;
2825 if (gc->bg == 8) {
2826 if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
2827 gc->bg = wp->cached_active_gc.bg;
2828 else
2829 gc->bg = wp->cached_gc.bg;
2833 static void
2834 tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
2835 struct colour_palette *palette, u_int bg)
2837 struct grid_cell gc;
2839 memcpy(&gc, &grid_default_cell, sizeof gc);
2840 gc.bg = bg;
2841 tty_attributes(tty, &gc, defaults, palette);