Use format_draw for command prompt prefix to allow styles, GitHub issue
[tmux-openbsd.git] / tty.c
blob8410a64c5b62bc942980a6314b9187b7394375f6
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 *, int);
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);
74 static void tty_check_overlay_range(struct tty *, u_int, u_int, u_int,
75 struct overlay_ranges *);
77 #define tty_use_margin(tty) \
78 (tty->term->flags & TERM_DECSLRM)
79 #define tty_full_width(tty, ctx) \
80 ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
82 #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
83 #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
84 #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
86 void
87 tty_create_log(void)
89 char name[64];
91 xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
93 tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
94 if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
95 fatal("fcntl failed");
98 int
99 tty_init(struct tty *tty, struct client *c)
101 if (!isatty(c->fd))
102 return (-1);
104 memset(tty, 0, sizeof *tty);
105 tty->client = c;
107 tty->cstyle = SCREEN_CURSOR_DEFAULT;
108 tty->ccolour = -1;
110 if (tcgetattr(c->fd, &tty->tio) != 0)
111 return (-1);
112 return (0);
115 void
116 tty_resize(struct tty *tty)
118 struct client *c = tty->client;
119 struct winsize ws;
120 u_int sx, sy, xpixel, ypixel;
122 if (ioctl(c->fd, TIOCGWINSZ, &ws) != -1) {
123 sx = ws.ws_col;
124 if (sx == 0) {
125 sx = 80;
126 xpixel = 0;
127 } else
128 xpixel = ws.ws_xpixel / sx;
129 sy = ws.ws_row;
130 if (sy == 0) {
131 sy = 24;
132 ypixel = 0;
133 } else
134 ypixel = ws.ws_ypixel / sy;
135 } else {
136 sx = 80;
137 sy = 24;
138 xpixel = 0;
139 ypixel = 0;
141 log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy,
142 xpixel, ypixel);
143 tty_set_size(tty, sx, sy, xpixel, ypixel);
144 tty_invalidate(tty);
147 void
148 tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
150 tty->sx = sx;
151 tty->sy = sy;
152 tty->xpixel = xpixel;
153 tty->ypixel = ypixel;
156 static void
157 tty_read_callback(__unused int fd, __unused short events, void *data)
159 struct tty *tty = data;
160 struct client *c = tty->client;
161 const char *name = c->name;
162 size_t size = EVBUFFER_LENGTH(tty->in);
163 int nread;
165 nread = evbuffer_read(tty->in, c->fd, -1);
166 if (nread == 0 || nread == -1) {
167 if (nread == 0)
168 log_debug("%s: read closed", name);
169 else
170 log_debug("%s: read error: %s", name, strerror(errno));
171 event_del(&tty->event_in);
172 server_client_lost(tty->client);
173 return;
175 log_debug("%s: read %d bytes (already %zu)", name, nread, size);
177 while (tty_keys_next(tty))
181 static void
182 tty_timer_callback(__unused int fd, __unused short events, void *data)
184 struct tty *tty = data;
185 struct client *c = tty->client;
186 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
188 log_debug("%s: %zu discarded", c->name, tty->discarded);
190 c->flags |= CLIENT_ALLREDRAWFLAGS;
191 c->discarded += tty->discarded;
193 if (tty->discarded < TTY_BLOCK_STOP(tty)) {
194 tty->flags &= ~TTY_BLOCK;
195 tty_invalidate(tty);
196 return;
198 tty->discarded = 0;
199 evtimer_add(&tty->timer, &tv);
202 static int
203 tty_block_maybe(struct tty *tty)
205 struct client *c = tty->client;
206 size_t size = EVBUFFER_LENGTH(tty->out);
207 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
209 if (size == 0)
210 tty->flags &= ~TTY_NOBLOCK;
211 else if (tty->flags & TTY_NOBLOCK)
212 return (0);
214 if (size < TTY_BLOCK_START(tty))
215 return (0);
217 if (tty->flags & TTY_BLOCK)
218 return (1);
219 tty->flags |= TTY_BLOCK;
221 log_debug("%s: can't keep up, %zu discarded", c->name, size);
223 evbuffer_drain(tty->out, size);
224 c->discarded += size;
226 tty->discarded = 0;
227 evtimer_add(&tty->timer, &tv);
228 return (1);
231 static void
232 tty_write_callback(__unused int fd, __unused short events, void *data)
234 struct tty *tty = data;
235 struct client *c = tty->client;
236 size_t size = EVBUFFER_LENGTH(tty->out);
237 int nwrite;
239 nwrite = evbuffer_write(tty->out, c->fd);
240 if (nwrite == -1)
241 return;
242 log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
244 if (c->redraw > 0) {
245 if ((size_t)nwrite >= c->redraw)
246 c->redraw = 0;
247 else
248 c->redraw -= nwrite;
249 log_debug("%s: waiting for redraw, %zu bytes left", c->name,
250 c->redraw);
251 } else if (tty_block_maybe(tty))
252 return;
254 if (EVBUFFER_LENGTH(tty->out) != 0)
255 event_add(&tty->event_out, NULL);
259 tty_open(struct tty *tty, char **cause)
261 struct client *c = tty->client;
263 tty->term = tty_term_create(tty, c->term_name, c->term_caps,
264 c->term_ncaps, &c->term_features, cause);
265 if (tty->term == NULL) {
266 tty_close(tty);
267 return (-1);
269 tty->flags |= TTY_OPENED;
271 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
273 event_set(&tty->event_in, c->fd, EV_PERSIST|EV_READ,
274 tty_read_callback, tty);
275 tty->in = evbuffer_new();
276 if (tty->in == NULL)
277 fatal("out of memory");
279 event_set(&tty->event_out, c->fd, EV_WRITE, tty_write_callback, tty);
280 tty->out = evbuffer_new();
281 if (tty->out == NULL)
282 fatal("out of memory");
284 evtimer_set(&tty->timer, tty_timer_callback, tty);
286 tty_start_tty(tty);
288 tty_keys_build(tty);
290 return (0);
293 static void
294 tty_start_timer_callback(__unused int fd, __unused short events, void *data)
296 struct tty *tty = data;
297 struct client *c = tty->client;
299 log_debug("%s: start timer fired", c->name);
300 if ((tty->flags & (TTY_HAVEDA|TTY_HAVEXDA)) == 0)
301 tty_update_features(tty);
302 tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
305 void
306 tty_start_tty(struct tty *tty)
308 struct client *c = tty->client;
309 struct termios tio;
310 struct timeval tv = { .tv_sec = 3 };
312 setblocking(c->fd, 0);
313 event_add(&tty->event_in, NULL);
315 memcpy(&tio, &tty->tio, sizeof tio);
316 tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
317 tio.c_iflag |= IGNBRK;
318 tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
319 tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|ECHOPRT|
320 ECHOKE|ISIG);
321 tio.c_cc[VMIN] = 1;
322 tio.c_cc[VTIME] = 0;
323 if (tcsetattr(c->fd, TCSANOW, &tio) == 0)
324 tcflush(c->fd, TCOFLUSH);
326 tty_putcode(tty, TTYC_SMCUP);
328 tty_putcode(tty, TTYC_SMKX);
329 tty_putcode(tty, TTYC_CLEAR);
331 if (tty_acs_needed(tty)) {
332 log_debug("%s: using capabilities for ACS", c->name);
333 tty_putcode(tty, TTYC_ENACS);
334 } else
335 log_debug("%s: using UTF-8 for ACS", c->name);
337 tty_putcode(tty, TTYC_CNORM);
338 if (tty_term_has(tty->term, TTYC_KMOUS)) {
339 tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
340 tty_puts(tty, "\033[?1006l\033[?1005l");
343 evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
344 evtimer_add(&tty->start_timer, &tv);
346 tty->flags |= TTY_STARTED;
347 tty_invalidate(tty);
349 if (tty->ccolour != -1)
350 tty_force_cursor_colour(tty, -1);
352 tty->mouse_drag_flag = 0;
353 tty->mouse_drag_update = NULL;
354 tty->mouse_drag_release = NULL;
357 void
358 tty_send_requests(struct tty *tty)
360 if (~tty->flags & TTY_STARTED)
361 return;
363 if (tty->term->flags & TERM_VT100LIKE) {
364 if (~tty->flags & TTY_HAVEDA)
365 tty_puts(tty, "\033[>c");
366 if (~tty->flags & TTY_HAVEXDA)
367 tty_puts(tty, "\033[>q");
368 } else
369 tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
372 void
373 tty_stop_tty(struct tty *tty)
375 struct client *c = tty->client;
376 struct winsize ws;
378 if (!(tty->flags & TTY_STARTED))
379 return;
380 tty->flags &= ~TTY_STARTED;
382 evtimer_del(&tty->start_timer);
384 event_del(&tty->timer);
385 tty->flags &= ~TTY_BLOCK;
387 event_del(&tty->event_in);
388 event_del(&tty->event_out);
391 * Be flexible about error handling and try not kill the server just
392 * because the fd is invalid. Things like ssh -t can easily leave us
393 * with a dead tty.
395 if (ioctl(c->fd, TIOCGWINSZ, &ws) == -1)
396 return;
397 if (tcsetattr(c->fd, TCSANOW, &tty->tio) == -1)
398 return;
400 tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
401 if (tty_acs_needed(tty))
402 tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
403 tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
404 tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
405 tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
406 if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
407 if (tty_term_has(tty->term, TTYC_SE))
408 tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
409 else if (tty_term_has(tty->term, TTYC_SS))
410 tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
412 if (tty->mode & MODE_BRACKETPASTE)
413 tty_raw(tty, tty_term_string(tty->term, TTYC_DSBP));
414 if (tty->ccolour != -1)
415 tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
417 tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
418 if (tty_term_has(tty->term, TTYC_KMOUS)) {
419 tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
420 tty_raw(tty, "\033[?1006l\033[?1005l");
423 if (tty->term->flags & TERM_VT100LIKE)
424 tty_raw(tty, "\033[?7727l");
425 tty_raw(tty, tty_term_string(tty->term, TTYC_DSFCS));
426 tty_raw(tty, tty_term_string(tty->term, TTYC_DSEKS));
428 if (tty_use_margin(tty))
429 tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG));
430 tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
432 setblocking(c->fd, 1);
435 void
436 tty_close(struct tty *tty)
438 if (event_initialized(&tty->key_timer))
439 evtimer_del(&tty->key_timer);
440 tty_stop_tty(tty);
442 if (tty->flags & TTY_OPENED) {
443 evbuffer_free(tty->in);
444 event_del(&tty->event_in);
445 evbuffer_free(tty->out);
446 event_del(&tty->event_out);
448 tty_term_free(tty->term);
449 tty_keys_free(tty);
451 tty->flags &= ~TTY_OPENED;
455 void
456 tty_free(struct tty *tty)
458 tty_close(tty);
461 void
462 tty_update_features(struct tty *tty)
464 struct client *c = tty->client;
466 if (tty_apply_features(tty->term, c->term_features))
467 tty_term_apply_overrides(tty->term);
469 if (tty_use_margin(tty))
470 tty_putcode(tty, TTYC_ENMG);
471 if (options_get_number(global_options, "extended-keys"))
472 tty_puts(tty, tty_term_string(tty->term, TTYC_ENEKS));
473 if (options_get_number(global_options, "focus-events"))
474 tty_puts(tty, tty_term_string(tty->term, TTYC_ENFCS));
475 if (tty->term->flags & TERM_VT100LIKE)
476 tty_puts(tty, "\033[?7727h");
479 void
480 tty_raw(struct tty *tty, const char *s)
482 struct client *c = tty->client;
483 ssize_t n, slen;
484 u_int i;
486 slen = strlen(s);
487 for (i = 0; i < 5; i++) {
488 n = write(c->fd, s, slen);
489 if (n >= 0) {
490 s += n;
491 slen -= n;
492 if (slen == 0)
493 break;
494 } else if (n == -1 && errno != EAGAIN)
495 break;
496 usleep(100);
500 void
501 tty_putcode(struct tty *tty, enum tty_code_code code)
503 tty_puts(tty, tty_term_string(tty->term, code));
506 void
507 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
509 if (a < 0)
510 return;
511 tty_puts(tty, tty_term_string1(tty->term, code, a));
514 void
515 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
517 if (a < 0 || b < 0)
518 return;
519 tty_puts(tty, tty_term_string2(tty->term, code, a, b));
522 void
523 tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
525 if (a < 0 || b < 0 || c < 0)
526 return;
527 tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
530 void
531 tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
533 if (a != NULL)
534 tty_puts(tty, tty_term_ptr1(tty->term, code, a));
537 void
538 tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
539 const void *b)
541 if (a != NULL && b != NULL)
542 tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
545 static void
546 tty_add(struct tty *tty, const char *buf, size_t len)
548 struct client *c = tty->client;
550 if (tty->flags & TTY_BLOCK) {
551 tty->discarded += len;
552 return;
555 evbuffer_add(tty->out, buf, len);
556 log_debug("%s: %.*s", c->name, (int)len, buf);
557 c->written += len;
559 if (tty_log_fd != -1)
560 write(tty_log_fd, buf, len);
561 if (tty->flags & TTY_STARTED)
562 event_add(&tty->event_out, NULL);
565 void
566 tty_puts(struct tty *tty, const char *s)
568 if (*s != '\0')
569 tty_add(tty, s, strlen(s));
572 void
573 tty_putc(struct tty *tty, u_char ch)
575 const char *acs;
577 if ((tty->term->flags & TERM_NOAM) &&
578 ch >= 0x20 && ch != 0x7f &&
579 tty->cy == tty->sy - 1 &&
580 tty->cx + 1 >= tty->sx)
581 return;
583 if (tty->cell.attr & GRID_ATTR_CHARSET) {
584 acs = tty_acs_get(tty, ch);
585 if (acs != NULL)
586 tty_add(tty, acs, strlen(acs));
587 else
588 tty_add(tty, &ch, 1);
589 } else
590 tty_add(tty, &ch, 1);
592 if (ch >= 0x20 && ch != 0x7f) {
593 if (tty->cx >= tty->sx) {
594 tty->cx = 1;
595 if (tty->cy != tty->rlower)
596 tty->cy++;
599 * On !am terminals, force the cursor position to where
600 * we think it should be after a line wrap - this means
601 * it works on sensible terminals as well.
603 if (tty->term->flags & TERM_NOAM)
604 tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
605 } else
606 tty->cx++;
610 void
611 tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
613 if ((tty->term->flags & TERM_NOAM) &&
614 tty->cy == tty->sy - 1 &&
615 tty->cx + len >= tty->sx)
616 len = tty->sx - tty->cx - 1;
618 tty_add(tty, buf, len);
619 if (tty->cx + width > tty->sx) {
620 tty->cx = (tty->cx + width) - tty->sx;
621 if (tty->cx <= tty->sx)
622 tty->cy++;
623 else
624 tty->cx = tty->cy = UINT_MAX;
625 } else
626 tty->cx += width;
629 static void
630 tty_set_italics(struct tty *tty)
632 const char *s;
634 if (tty_term_has(tty->term, TTYC_SITM)) {
635 s = options_get_string(global_options, "default-terminal");
636 if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
637 tty_putcode(tty, TTYC_SITM);
638 return;
641 tty_putcode(tty, TTYC_SMSO);
644 void
645 tty_set_title(struct tty *tty, const char *title)
647 if (!tty_term_has(tty->term, TTYC_TSL) ||
648 !tty_term_has(tty->term, TTYC_FSL))
649 return;
651 tty_putcode(tty, TTYC_TSL);
652 tty_puts(tty, title);
653 tty_putcode(tty, TTYC_FSL);
656 static void
657 tty_force_cursor_colour(struct tty *tty, int c)
659 u_char r, g, b;
660 char s[13] = "";
662 if (c != -1)
663 c = colour_force_rgb(c);
664 if (c == tty->ccolour)
665 return;
666 if (c == -1)
667 tty_putcode(tty, TTYC_CR);
668 else {
669 colour_split_rgb(c, &r, &g, &b);
670 xsnprintf(s, sizeof s, "rgb:%02hhx/%02hhx/%02hhx", r, g, b);
671 tty_putcode_ptr1(tty, TTYC_CS, s);
673 tty->ccolour = c;
676 static int
677 tty_update_cursor(struct tty *tty, int mode, struct screen *s)
679 enum screen_cursor_style cstyle;
680 int ccolour, changed, cmode = mode;
682 /* Set cursor colour if changed. */
683 if (s != NULL) {
684 ccolour = s->ccolour;
685 if (s->ccolour == -1)
686 ccolour = s->default_ccolour;
687 tty_force_cursor_colour(tty, ccolour);
690 /* If cursor is off, set as invisible. */
691 if (~cmode & MODE_CURSOR) {
692 if (tty->mode & MODE_CURSOR)
693 tty_putcode(tty, TTYC_CIVIS);
694 return (cmode);
697 /* Check if blinking or very visible flag changed or style changed. */
698 if (s == NULL)
699 cstyle = tty->cstyle;
700 else {
701 cstyle = s->cstyle;
702 if (cstyle == SCREEN_CURSOR_DEFAULT) {
703 if (~cmode & MODE_CURSOR_BLINKING_SET) {
704 if (s->default_mode & MODE_CURSOR_BLINKING)
705 cmode |= MODE_CURSOR_BLINKING;
706 else
707 cmode &= ~MODE_CURSOR_BLINKING;
709 cstyle = s->default_cstyle;
713 /* If nothing changed, do nothing. */
714 changed = cmode ^ tty->mode;
715 if ((changed & CURSOR_MODES) == 0 && cstyle == tty->cstyle)
716 return (cmode);
719 * Set cursor style. If an explicit style has been set with DECSCUSR,
720 * set it if supported, otherwise send cvvis for blinking styles.
722 * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
723 * if either the blinking or very visible flags are set.
725 tty_putcode(tty, TTYC_CNORM);
726 switch (cstyle) {
727 case SCREEN_CURSOR_DEFAULT:
728 if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
729 if (tty_term_has(tty->term, TTYC_SE))
730 tty_putcode(tty, TTYC_SE);
731 else
732 tty_putcode1(tty, TTYC_SS, 0);
734 if (cmode & (MODE_CURSOR_BLINKING|MODE_CURSOR_VERY_VISIBLE))
735 tty_putcode(tty, TTYC_CVVIS);
736 break;
737 case SCREEN_CURSOR_BLOCK:
738 if (tty_term_has(tty->term, TTYC_SS)) {
739 if (cmode & MODE_CURSOR_BLINKING)
740 tty_putcode1(tty, TTYC_SS, 1);
741 else
742 tty_putcode1(tty, TTYC_SS, 2);
743 } else if (cmode & MODE_CURSOR_BLINKING)
744 tty_putcode(tty, TTYC_CVVIS);
745 break;
746 case SCREEN_CURSOR_UNDERLINE:
747 if (tty_term_has(tty->term, TTYC_SS)) {
748 if (cmode & MODE_CURSOR_BLINKING)
749 tty_putcode1(tty, TTYC_SS, 3);
750 else
751 tty_putcode1(tty, TTYC_SS, 4);
752 } else if (cmode & MODE_CURSOR_BLINKING)
753 tty_putcode(tty, TTYC_CVVIS);
754 break;
755 case SCREEN_CURSOR_BAR:
756 if (tty_term_has(tty->term, TTYC_SS)) {
757 if (cmode & MODE_CURSOR_BLINKING)
758 tty_putcode1(tty, TTYC_SS, 5);
759 else
760 tty_putcode1(tty, TTYC_SS, 6);
761 } else if (cmode & MODE_CURSOR_BLINKING)
762 tty_putcode(tty, TTYC_CVVIS);
763 break;
765 tty->cstyle = cstyle;
766 return (cmode);
769 void
770 tty_update_mode(struct tty *tty, int mode, struct screen *s)
772 struct tty_term *term = tty->term;
773 struct client *c = tty->client;
774 int changed;
776 if (tty->flags & TTY_NOCURSOR)
777 mode &= ~MODE_CURSOR;
779 if (tty_update_cursor(tty, mode, s) & MODE_CURSOR_BLINKING)
780 mode |= MODE_CURSOR_BLINKING;
781 else
782 mode &= ~MODE_CURSOR_BLINKING;
784 changed = mode ^ tty->mode;
785 if (log_get_level() != 0 && changed != 0) {
786 log_debug("%s: current mode %s", c->name,
787 screen_mode_to_string(tty->mode));
788 log_debug("%s: setting mode %s", c->name,
789 screen_mode_to_string(mode));
792 if ((changed & ALL_MOUSE_MODES) && tty_term_has(term, TTYC_KMOUS)) {
794 * If the mouse modes have changed, clear then all and apply
795 * again. There are differences in how terminals track the
796 * various bits.
798 tty_puts(tty, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l");
799 if (mode & ALL_MOUSE_MODES)
800 tty_puts(tty, "\033[?1006h");
801 if (mode & MODE_MOUSE_ALL)
802 tty_puts(tty, "\033[?1000h\033[?1002h\033[?1003h");
803 if (mode & MODE_MOUSE_BUTTON)
804 tty_puts(tty, "\033[?1000h\033[?1002h");
805 else if (mode & MODE_MOUSE_STANDARD)
806 tty_puts(tty, "\033[?1000h");
808 if (changed & MODE_BRACKETPASTE) {
809 if (mode & MODE_BRACKETPASTE)
810 tty_putcode(tty, TTYC_ENBP);
811 else
812 tty_putcode(tty, TTYC_DSBP);
814 tty->mode = mode;
817 static void
818 tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
819 enum tty_code_code code1, u_int n)
821 if (tty_term_has(tty->term, code))
822 tty_putcode1(tty, code, n);
823 else {
824 while (n-- > 0)
825 tty_putcode(tty, code1);
829 static void
830 tty_repeat_space(struct tty *tty, u_int n)
832 static char s[500];
834 if (*s != ' ')
835 memset(s, ' ', sizeof s);
837 while (n > sizeof s) {
838 tty_putn(tty, s, sizeof s, sizeof s);
839 n -= sizeof s;
841 if (n != 0)
842 tty_putn(tty, s, n, n);
845 /* Is this window bigger than the terminal? */
847 tty_window_bigger(struct tty *tty)
849 struct client *c = tty->client;
850 struct window *w = c->session->curw->window;
852 return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
855 /* What offset should this window be drawn at? */
857 tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
859 *ox = tty->oox;
860 *oy = tty->ooy;
861 *sx = tty->osx;
862 *sy = tty->osy;
864 return (tty->oflag);
867 /* What offset should this window be drawn at? */
868 static int
869 tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
871 struct client *c = tty->client;
872 struct window *w = c->session->curw->window;
873 struct window_pane *wp = server_client_get_pane(c);
874 u_int cx, cy, lines;
876 lines = status_line_size(c);
878 if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
879 *ox = 0;
880 *oy = 0;
881 *sx = w->sx;
882 *sy = w->sy;
884 c->pan_window = NULL;
885 return (0);
888 *sx = tty->sx;
889 *sy = tty->sy - lines;
891 if (c->pan_window == w) {
892 if (*sx >= w->sx)
893 c->pan_ox = 0;
894 else if (c->pan_ox + *sx > w->sx)
895 c->pan_ox = w->sx - *sx;
896 *ox = c->pan_ox;
897 if (*sy >= w->sy)
898 c->pan_oy = 0;
899 else if (c->pan_oy + *sy > w->sy)
900 c->pan_oy = w->sy - *sy;
901 *oy = c->pan_oy;
902 return (1);
905 if (~wp->screen->mode & MODE_CURSOR) {
906 *ox = 0;
907 *oy = 0;
908 } else {
909 cx = wp->xoff + wp->screen->cx;
910 cy = wp->yoff + wp->screen->cy;
912 if (cx < *sx)
913 *ox = 0;
914 else if (cx > w->sx - *sx)
915 *ox = w->sx - *sx;
916 else
917 *ox = cx - *sx / 2;
919 if (cy < *sy)
920 *oy = 0;
921 else if (cy > w->sy - *sy)
922 *oy = w->sy - *sy;
923 else
924 *oy = cy - *sy / 2;
927 c->pan_window = NULL;
928 return (1);
931 /* Update stored offsets for a window and redraw if necessary. */
932 void
933 tty_update_window_offset(struct window *w)
935 struct client *c;
937 TAILQ_FOREACH(c, &clients, entry) {
938 if (c->session != NULL &&
939 c->session->curw != NULL &&
940 c->session->curw->window == w)
941 tty_update_client_offset(c);
945 /* Update stored offsets for a client and redraw if necessary. */
946 void
947 tty_update_client_offset(struct client *c)
949 u_int ox, oy, sx, sy;
951 if (~c->flags & CLIENT_TERMINAL)
952 return;
954 c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
955 if (ox == c->tty.oox &&
956 oy == c->tty.ooy &&
957 sx == c->tty.osx &&
958 sy == c->tty.osy)
959 return;
961 log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
962 __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
963 ox, oy, sx, sy);
965 c->tty.oox = ox;
966 c->tty.ooy = oy;
967 c->tty.osx = sx;
968 c->tty.osy = sy;
970 c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
974 * Is the region large enough to be worth redrawing once later rather than
975 * probably several times now? Currently yes if it is more than 50% of the
976 * pane.
978 static int
979 tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
981 return (ctx->orlower - ctx->orupper >= ctx->sy / 2);
985 * Return if BCE is needed but the terminal doesn't have it - it'll need to be
986 * emulated.
988 static int
989 tty_fake_bce(const struct tty *tty, const struct grid_cell *gc, u_int bg)
991 if (tty_term_flag(tty->term, TTYC_BCE))
992 return (0);
993 if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc->bg))
994 return (1);
995 return (0);
999 * Redraw scroll region using data from screen (already updated). Used when
1000 * CSR not supported, or window is a pane that doesn't take up the full
1001 * width of the terminal.
1003 static void
1004 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1006 struct client *c = tty->client;
1007 u_int i;
1010 * If region is large, schedule a redraw. In most cases this is likely
1011 * to be followed by some more scrolling.
1013 if (tty_large_region(tty, ctx)) {
1014 log_debug("%s: %s large redraw", __func__, c->name);
1015 ctx->redraw_cb(ctx);
1016 return;
1019 for (i = ctx->orupper; i <= ctx->orlower; i++)
1020 tty_draw_pane(tty, ctx, i);
1023 /* Is this position visible in the pane? */
1024 static int
1025 tty_is_visible(__unused struct tty *tty, const struct tty_ctx *ctx, u_int px,
1026 u_int py, u_int nx, u_int ny)
1028 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1030 if (!ctx->bigger)
1031 return (1);
1033 if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx ||
1034 yoff + ny <= ctx->woy || yoff >= ctx->woy + ctx->wsy)
1035 return (0);
1036 return (1);
1039 /* Clamp line position to visible part of pane. */
1040 static int
1041 tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1042 u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
1044 u_int xoff = ctx->rxoff + px;
1046 if (!tty_is_visible(tty, ctx, px, py, nx, 1))
1047 return (0);
1048 *ry = ctx->yoff + py - ctx->woy;
1050 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1051 /* All visible. */
1052 *i = 0;
1053 *x = ctx->xoff + px - ctx->wox;
1054 *rx = nx;
1055 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1056 /* Both left and right not visible. */
1057 *i = ctx->wox;
1058 *x = 0;
1059 *rx = ctx->wsx;
1060 } else if (xoff < ctx->wox) {
1061 /* Left not visible. */
1062 *i = ctx->wox - (ctx->xoff + px);
1063 *x = 0;
1064 *rx = nx - *i;
1065 } else {
1066 /* Right not visible. */
1067 *i = 0;
1068 *x = (ctx->xoff + px) - ctx->wox;
1069 *rx = ctx->wsx - *x;
1071 if (*rx > nx)
1072 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1074 return (1);
1077 /* Clear a line. */
1078 static void
1079 tty_clear_line(struct tty *tty, const struct grid_cell *defaults, u_int py,
1080 u_int px, u_int nx, u_int bg)
1082 struct client *c = tty->client;
1083 struct overlay_ranges r;
1084 u_int i;
1086 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1088 /* Nothing to clear. */
1089 if (nx == 0)
1090 return;
1092 /* If genuine BCE is available, can try escape sequences. */
1093 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1094 /* Off the end of the line, use EL if available. */
1095 if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
1096 tty_cursor(tty, px, py);
1097 tty_putcode(tty, TTYC_EL);
1098 return;
1101 /* At the start of the line. Use EL1. */
1102 if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1103 tty_cursor(tty, px + nx - 1, py);
1104 tty_putcode(tty, TTYC_EL1);
1105 return;
1108 /* Section of line. Use ECH if possible. */
1109 if (tty_term_has(tty->term, TTYC_ECH)) {
1110 tty_cursor(tty, px, py);
1111 tty_putcode1(tty, TTYC_ECH, nx);
1112 return;
1117 * Couldn't use an escape sequence, use spaces. Clear only the visible
1118 * bit if there is an overlay.
1120 tty_check_overlay_range(tty, px, py, nx, &r);
1121 for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
1122 if (r.nx[i] == 0)
1123 continue;
1124 tty_cursor(tty, r.px[i], py);
1125 tty_repeat_space(tty, r.nx[i]);
1129 /* Clear a line, adjusting to visible part of pane. */
1130 static void
1131 tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1132 u_int px, u_int nx, u_int bg)
1134 struct client *c = tty->client;
1135 u_int i, x, rx, ry;
1137 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1139 if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1140 tty_clear_line(tty, &ctx->defaults, ry, x, rx, bg);
1143 /* Clamp area position to visible part of pane. */
1144 static int
1145 tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1146 u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1147 u_int *ry)
1149 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1151 if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1152 return (0);
1154 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1155 /* All visible. */
1156 *i = 0;
1157 *x = ctx->xoff + px - ctx->wox;
1158 *rx = nx;
1159 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1160 /* Both left and right not visible. */
1161 *i = ctx->wox;
1162 *x = 0;
1163 *rx = ctx->wsx;
1164 } else if (xoff < ctx->wox) {
1165 /* Left not visible. */
1166 *i = ctx->wox - (ctx->xoff + px);
1167 *x = 0;
1168 *rx = nx - *i;
1169 } else {
1170 /* Right not visible. */
1171 *i = 0;
1172 *x = (ctx->xoff + px) - ctx->wox;
1173 *rx = ctx->wsx - *x;
1175 if (*rx > nx)
1176 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1178 if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) {
1179 /* All visible. */
1180 *j = 0;
1181 *y = ctx->yoff + py - ctx->woy;
1182 *ry = ny;
1183 } else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) {
1184 /* Both top and bottom not visible. */
1185 *j = ctx->woy;
1186 *y = 0;
1187 *ry = ctx->wsy;
1188 } else if (yoff < ctx->woy) {
1189 /* Top not visible. */
1190 *j = ctx->woy - (ctx->yoff + py);
1191 *y = 0;
1192 *ry = ny - *j;
1193 } else {
1194 /* Bottom not visible. */
1195 *j = 0;
1196 *y = (ctx->yoff + py) - ctx->woy;
1197 *ry = ctx->wsy - *y;
1199 if (*ry > ny)
1200 fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1202 return (1);
1205 /* Clear an area, adjusting to visible part of pane. */
1206 static void
1207 tty_clear_area(struct tty *tty, const struct grid_cell *defaults, u_int py,
1208 u_int ny, u_int px, u_int nx, u_int bg)
1210 struct client *c = tty->client;
1211 u_int yy;
1212 char tmp[64];
1214 log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1216 /* Nothing to clear. */
1217 if (nx == 0 || ny == 0)
1218 return;
1220 /* If genuine BCE is available, can try escape sequences. */
1221 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1222 /* Use ED if clearing off the bottom of the terminal. */
1223 if (px == 0 &&
1224 px + nx >= tty->sx &&
1225 py + ny >= tty->sy &&
1226 tty_term_has(tty->term, TTYC_ED)) {
1227 tty_cursor(tty, 0, py);
1228 tty_putcode(tty, TTYC_ED);
1229 return;
1233 * On VT420 compatible terminals we can use DECFRA if the
1234 * background colour isn't default (because it doesn't work
1235 * after SGR 0).
1237 if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1238 xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1239 py + 1, px + 1, py + ny, px + nx);
1240 tty_puts(tty, tmp);
1241 return;
1244 /* Full lines can be scrolled away to clear them. */
1245 if (px == 0 &&
1246 px + nx >= tty->sx &&
1247 ny > 2 &&
1248 tty_term_has(tty->term, TTYC_CSR) &&
1249 tty_term_has(tty->term, TTYC_INDN)) {
1250 tty_region(tty, py, py + ny - 1);
1251 tty_margin_off(tty);
1252 tty_putcode1(tty, TTYC_INDN, ny);
1253 return;
1257 * If margins are supported, can just scroll the area off to
1258 * clear it.
1260 if (nx > 2 &&
1261 ny > 2 &&
1262 tty_term_has(tty->term, TTYC_CSR) &&
1263 tty_use_margin(tty) &&
1264 tty_term_has(tty->term, TTYC_INDN)) {
1265 tty_region(tty, py, py + ny - 1);
1266 tty_margin(tty, px, px + nx - 1);
1267 tty_putcode1(tty, TTYC_INDN, ny);
1268 return;
1272 /* Couldn't use an escape sequence, loop over the lines. */
1273 for (yy = py; yy < py + ny; yy++)
1274 tty_clear_line(tty, defaults, yy, px, nx, bg);
1277 /* Clear an area in a pane. */
1278 static void
1279 tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1280 u_int ny, u_int px, u_int nx, u_int bg)
1282 u_int i, j, x, y, rx, ry;
1284 if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1285 tty_clear_area(tty, &ctx->defaults, y, ry, x, rx, bg);
1288 static void
1289 tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1291 struct screen *s = ctx->s;
1292 u_int nx = ctx->sx, i, x, rx, ry;
1294 log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1296 if (!ctx->bigger) {
1297 tty_draw_line(tty, s, 0, py, nx, ctx->xoff, ctx->yoff + py,
1298 &ctx->defaults, ctx->palette);
1299 return;
1301 if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry)) {
1302 tty_draw_line(tty, s, i, py, rx, x, ry, &ctx->defaults,
1303 ctx->palette);
1307 static const struct grid_cell *
1308 tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1310 static struct grid_cell new;
1311 int c;
1313 /* Characters less than 0x7f are always fine, no matter what. */
1314 if (gc->data.size == 1 && *gc->data.data < 0x7f)
1315 return (gc);
1317 /* UTF-8 terminal and a UTF-8 character - fine. */
1318 if (tty->client->flags & CLIENT_UTF8)
1319 return (gc);
1320 memcpy(&new, gc, sizeof new);
1322 /* See if this can be mapped to an ACS character. */
1323 c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size);
1324 if (c != -1) {
1325 utf8_set(&new.data, c);
1326 new.attr |= GRID_ATTR_CHARSET;
1327 return (&new);
1330 /* Replace by the right number of underscores. */
1331 new.data.size = gc->data.width;
1332 if (new.data.size > UTF8_SIZE)
1333 new.data.size = UTF8_SIZE;
1334 memset(new.data.data, '_', new.data.size);
1335 return (&new);
1339 * Check if a single character is obstructed by the overlay and return a
1340 * boolean.
1342 static int
1343 tty_check_overlay(struct tty *tty, u_int px, u_int py)
1345 struct overlay_ranges r;
1348 * A unit width range will always return nx[2] == 0 from a check, even
1349 * with multiple overlays, so it's sufficient to check just the first
1350 * two entries.
1352 tty_check_overlay_range(tty, px, py, 1, &r);
1353 if (r.nx[0] + r.nx[1] == 0)
1354 return (0);
1355 return (1);
1358 /* Return parts of the input range which are visible. */
1359 static void
1360 tty_check_overlay_range(struct tty *tty, u_int px, u_int py, u_int nx,
1361 struct overlay_ranges *r)
1363 struct client *c = tty->client;
1365 if (c->overlay_check == NULL) {
1366 r->px[0] = px;
1367 r->nx[0] = nx;
1368 r->px[1] = 0;
1369 r->nx[1] = 0;
1370 r->px[2] = 0;
1371 r->nx[2] = 0;
1372 return;
1375 c->overlay_check(c, c->overlay_data, px, py, nx, r);
1378 void
1379 tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
1380 u_int atx, u_int aty, const struct grid_cell *defaults,
1381 struct colour_palette *palette)
1383 struct grid *gd = s->grid;
1384 struct grid_cell gc, last;
1385 const struct grid_cell *gcp;
1386 struct grid_line *gl;
1387 struct client *c = tty->client;
1388 struct overlay_ranges r;
1389 u_int i, j, ux, sx, width, hidden, eux, nxx;
1390 u_int cellsize;
1391 int flags, cleared = 0, wrapped = 0;
1392 char buf[512];
1393 size_t len;
1395 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1396 px, py, nx, atx, aty);
1397 log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg,
1398 defaults->bg);
1401 * py is the line in the screen to draw.
1402 * px is the start x and nx is the width to draw.
1403 * atx,aty is the line on the terminal to draw it.
1406 flags = (tty->flags & TTY_NOCURSOR);
1407 tty->flags |= TTY_NOCURSOR;
1408 tty_update_mode(tty, tty->mode, s);
1410 tty_region_off(tty);
1411 tty_margin_off(tty);
1414 * Clamp the width to cellsize - note this is not cellused, because
1415 * there may be empty background cells after it (from BCE).
1417 sx = screen_size_x(s);
1418 if (nx > sx)
1419 nx = sx;
1420 cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1421 if (sx > cellsize)
1422 sx = cellsize;
1423 if (sx > tty->sx)
1424 sx = tty->sx;
1425 if (sx > nx)
1426 sx = nx;
1427 ux = 0;
1429 if (py == 0)
1430 gl = NULL;
1431 else
1432 gl = grid_get_line(gd, gd->hsize + py - 1);
1433 if (gl == NULL ||
1434 (~gl->flags & GRID_LINE_WRAPPED) ||
1435 atx != 0 ||
1436 tty->cx < tty->sx ||
1437 nx < tty->sx) {
1438 if (nx < tty->sx &&
1439 atx == 0 &&
1440 px + sx != nx &&
1441 tty_term_has(tty->term, TTYC_EL1) &&
1442 !tty_fake_bce(tty, defaults, 8) &&
1443 c->overlay_check == NULL) {
1444 tty_default_attributes(tty, defaults, palette, 8);
1445 tty_cursor(tty, nx - 1, aty);
1446 tty_putcode(tty, TTYC_EL1);
1447 cleared = 1;
1449 } else {
1450 log_debug("%s: wrapped line %u", __func__, aty);
1451 wrapped = 1;
1454 memcpy(&last, &grid_default_cell, sizeof last);
1455 len = 0;
1456 width = 0;
1458 for (i = 0; i < sx; i++) {
1459 grid_view_get_cell(gd, px + i, py, &gc);
1460 gcp = tty_check_codeset(tty, &gc);
1461 if (len != 0 &&
1462 (!tty_check_overlay(tty, atx + ux + width, aty) ||
1463 (gcp->attr & GRID_ATTR_CHARSET) ||
1464 gcp->flags != last.flags ||
1465 gcp->attr != last.attr ||
1466 gcp->fg != last.fg ||
1467 gcp->bg != last.bg ||
1468 gcp->us != last.us ||
1469 ux + width + gcp->data.width > nx ||
1470 (sizeof buf) - len < gcp->data.size)) {
1471 tty_attributes(tty, &last, defaults, palette);
1472 if (last.flags & GRID_FLAG_CLEARED) {
1473 log_debug("%s: %zu cleared", __func__, len);
1474 tty_clear_line(tty, defaults, aty, atx + ux,
1475 width, last.bg);
1476 } else {
1477 if (!wrapped || atx != 0 || ux != 0)
1478 tty_cursor(tty, atx + ux, aty);
1479 tty_putn(tty, buf, len, width);
1481 ux += width;
1483 len = 0;
1484 width = 0;
1485 wrapped = 0;
1488 if (gcp->flags & GRID_FLAG_SELECTED)
1489 screen_select_cell(s, &last, gcp);
1490 else
1491 memcpy(&last, gcp, sizeof last);
1493 tty_check_overlay_range(tty, atx + ux, aty, gcp->data.width,
1494 &r);
1495 hidden = 0;
1496 for (j = 0; j < OVERLAY_MAX_RANGES; j++)
1497 hidden += r.nx[j];
1498 hidden = gcp->data.width - hidden;
1499 if (hidden != 0 && hidden == gcp->data.width) {
1500 if (~gcp->flags & GRID_FLAG_PADDING)
1501 ux += gcp->data.width;
1502 } else if (hidden != 0 || ux + gcp->data.width > nx) {
1503 if (~gcp->flags & GRID_FLAG_PADDING) {
1504 tty_attributes(tty, &last, defaults, palette);
1505 for (j = 0; j < OVERLAY_MAX_RANGES; j++) {
1506 if (r.nx[j] == 0)
1507 continue;
1508 /* Effective width drawn so far. */
1509 eux = r.px[j] - atx;
1510 if (eux < nx) {
1511 tty_cursor(tty, r.px[j], aty);
1512 nxx = nx - eux;
1513 if (r.nx[j] > nxx)
1514 r.nx[j] = nxx;
1515 tty_repeat_space(tty, r.nx[j]);
1516 ux = eux + r.nx[j];
1520 } else if (gcp->attr & GRID_ATTR_CHARSET) {
1521 tty_attributes(tty, &last, defaults, palette);
1522 tty_cursor(tty, atx + ux, aty);
1523 for (j = 0; j < gcp->data.size; j++)
1524 tty_putc(tty, gcp->data.data[j]);
1525 ux += gcp->data.width;
1526 } else if (~gcp->flags & GRID_FLAG_PADDING) {
1527 memcpy(buf + len, gcp->data.data, gcp->data.size);
1528 len += gcp->data.size;
1529 width += gcp->data.width;
1532 if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1533 tty_attributes(tty, &last, defaults, palette);
1534 if (last.flags & GRID_FLAG_CLEARED) {
1535 log_debug("%s: %zu cleared (end)", __func__, len);
1536 tty_clear_line(tty, defaults, aty, atx + ux, width,
1537 last.bg);
1538 } else {
1539 if (!wrapped || atx != 0 || ux != 0)
1540 tty_cursor(tty, atx + ux, aty);
1541 tty_putn(tty, buf, len, width);
1543 ux += width;
1546 if (!cleared && ux < nx) {
1547 log_debug("%s: %u to end of line (%zu cleared)", __func__,
1548 nx - ux, len);
1549 tty_default_attributes(tty, defaults, palette, 8);
1550 tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1553 tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1554 tty_update_mode(tty, tty->mode, s);
1557 void
1558 tty_sync_start(struct tty *tty)
1560 if (tty->flags & TTY_BLOCK)
1561 return;
1562 if (tty->flags & TTY_SYNCING)
1563 return;
1564 tty->flags |= TTY_SYNCING;
1566 if (tty_term_has(tty->term, TTYC_SYNC)) {
1567 log_debug("%s sync start", tty->client->name);
1568 tty_putcode1(tty, TTYC_SYNC, 1);
1572 void
1573 tty_sync_end(struct tty *tty)
1575 if (tty->flags & TTY_BLOCK)
1576 return;
1577 if (~tty->flags & TTY_SYNCING)
1578 return;
1579 tty->flags &= ~TTY_SYNCING;
1581 if (tty_term_has(tty->term, TTYC_SYNC)) {
1582 log_debug("%s sync end", tty->client->name);
1583 tty_putcode1(tty, TTYC_SYNC, 2);
1587 static int
1588 tty_client_ready(struct client *c)
1590 if (c->session == NULL || c->tty.term == NULL)
1591 return (0);
1592 if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1593 return (0);
1594 if (c->tty.flags & TTY_FREEZE)
1595 return (0);
1596 return (1);
1599 void
1600 tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1601 struct tty_ctx *ctx)
1603 struct client *c;
1604 int state;
1606 if (ctx->set_client_cb == NULL)
1607 return;
1608 TAILQ_FOREACH(c, &clients, entry) {
1609 if (!tty_client_ready(c))
1610 continue;
1611 state = ctx->set_client_cb(ctx, c);
1612 if (state == -1)
1613 break;
1614 if (state == 0)
1615 continue;
1616 cmdfn(&c->tty, ctx);
1620 void
1621 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1623 struct client *c = tty->client;
1625 if (ctx->bigger ||
1626 !tty_full_width(tty, ctx) ||
1627 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1628 (!tty_term_has(tty->term, TTYC_ICH) &&
1629 !tty_term_has(tty->term, TTYC_ICH1)) ||
1630 c->overlay_check != NULL) {
1631 tty_draw_pane(tty, ctx, ctx->ocy);
1632 return;
1635 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1637 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1639 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1642 void
1643 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1645 struct client *c = tty->client;
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_DCH) &&
1651 !tty_term_has(tty->term, TTYC_DCH1)) ||
1652 c->overlay_check != NULL) {
1653 tty_draw_pane(tty, ctx, ctx->ocy);
1654 return;
1657 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1659 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1661 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1664 void
1665 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1667 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1669 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1672 void
1673 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1675 struct client *c = tty->client;
1677 if (ctx->bigger ||
1678 !tty_full_width(tty, ctx) ||
1679 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1680 !tty_term_has(tty->term, TTYC_CSR) ||
1681 !tty_term_has(tty->term, TTYC_IL1) ||
1682 ctx->sx == 1 ||
1683 ctx->sy == 1 ||
1684 c->overlay_check != NULL) {
1685 tty_redraw_region(tty, ctx);
1686 return;
1689 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1691 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1692 tty_margin_off(tty);
1693 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1695 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1696 tty->cx = tty->cy = UINT_MAX;
1699 void
1700 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1702 struct client *c = tty->client;
1704 if (ctx->bigger ||
1705 !tty_full_width(tty, ctx) ||
1706 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1707 !tty_term_has(tty->term, TTYC_CSR) ||
1708 !tty_term_has(tty->term, TTYC_DL1) ||
1709 ctx->sx == 1 ||
1710 ctx->sy == 1 ||
1711 c->overlay_check != NULL) {
1712 tty_redraw_region(tty, ctx);
1713 return;
1716 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1718 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1719 tty_margin_off(tty);
1720 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1722 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1723 tty->cx = tty->cy = UINT_MAX;
1726 void
1727 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1729 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1731 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1734 void
1735 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1737 u_int nx = ctx->sx - ctx->ocx;
1739 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1741 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1744 void
1745 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1747 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1749 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1752 void
1753 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1755 struct client *c = tty->client;
1757 if (ctx->ocy != ctx->orupper)
1758 return;
1760 if (ctx->bigger ||
1761 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1762 tty_fake_bce(tty, &ctx->defaults, 8) ||
1763 !tty_term_has(tty->term, TTYC_CSR) ||
1764 (!tty_term_has(tty->term, TTYC_RI) &&
1765 !tty_term_has(tty->term, TTYC_RIN)) ||
1766 ctx->sx == 1 ||
1767 ctx->sy == 1 ||
1768 c->overlay_check != NULL) {
1769 tty_redraw_region(tty, ctx);
1770 return;
1773 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1775 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1776 tty_margin_pane(tty, ctx);
1777 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1779 if (tty_term_has(tty->term, TTYC_RI))
1780 tty_putcode(tty, TTYC_RI);
1781 else
1782 tty_putcode1(tty, TTYC_RIN, 1);
1785 void
1786 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1788 struct client *c = tty->client;
1790 if (ctx->ocy != ctx->orlower)
1791 return;
1793 if (ctx->bigger ||
1794 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1795 tty_fake_bce(tty, &ctx->defaults, 8) ||
1796 !tty_term_has(tty->term, TTYC_CSR) ||
1797 ctx->sx == 1 ||
1798 ctx->sy == 1 ||
1799 c->overlay_check != NULL) {
1800 tty_redraw_region(tty, ctx);
1801 return;
1804 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1806 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1807 tty_margin_pane(tty, ctx);
1810 * If we want to wrap a pane while using margins, the cursor needs to
1811 * be exactly on the right of the region. If the cursor is entirely off
1812 * the edge - move it back to the right. Some terminals are funny about
1813 * this and insert extra spaces, so only use the right if margins are
1814 * enabled.
1816 if (ctx->xoff + ctx->ocx > tty->rright) {
1817 if (!tty_use_margin(tty))
1818 tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1819 else
1820 tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1821 } else
1822 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1824 tty_putc(tty, '\n');
1827 void
1828 tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1830 struct client *c = tty->client;
1831 u_int i;
1833 if (ctx->bigger ||
1834 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1835 tty_fake_bce(tty, &ctx->defaults, 8) ||
1836 !tty_term_has(tty->term, TTYC_CSR) ||
1837 ctx->sx == 1 ||
1838 ctx->sy == 1 ||
1839 c->overlay_check != NULL) {
1840 tty_redraw_region(tty, ctx);
1841 return;
1844 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1846 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1847 tty_margin_pane(tty, ctx);
1849 if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1850 if (!tty_use_margin(tty))
1851 tty_cursor(tty, 0, tty->rlower);
1852 else
1853 tty_cursor(tty, tty->rright, tty->rlower);
1854 for (i = 0; i < ctx->num; i++)
1855 tty_putc(tty, '\n');
1856 } else {
1857 if (tty->cy == UINT_MAX)
1858 tty_cursor(tty, 0, 0);
1859 else
1860 tty_cursor(tty, 0, tty->cy);
1861 tty_putcode1(tty, TTYC_INDN, ctx->num);
1865 void
1866 tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
1868 u_int i;
1869 struct client *c = tty->client;
1871 if (ctx->bigger ||
1872 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1873 tty_fake_bce(tty, &ctx->defaults, 8) ||
1874 !tty_term_has(tty->term, TTYC_CSR) ||
1875 (!tty_term_has(tty->term, TTYC_RI) &&
1876 !tty_term_has(tty->term, TTYC_RIN)) ||
1877 ctx->sx == 1 ||
1878 ctx->sy == 1 ||
1879 c->overlay_check != NULL) {
1880 tty_redraw_region(tty, ctx);
1881 return;
1884 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1886 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1887 tty_margin_pane(tty, ctx);
1888 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1890 if (tty_term_has(tty->term, TTYC_RIN))
1891 tty_putcode1(tty, TTYC_RIN, ctx->num);
1892 else {
1893 for (i = 0; i < ctx->num; i++)
1894 tty_putcode(tty, TTYC_RI);
1898 void
1899 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1901 u_int px, py, nx, ny;
1903 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1905 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1906 tty_margin_off(tty);
1908 px = 0;
1909 nx = ctx->sx;
1910 py = ctx->ocy + 1;
1911 ny = ctx->sy - ctx->ocy - 1;
1913 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1915 px = ctx->ocx;
1916 nx = ctx->sx - ctx->ocx;
1917 py = ctx->ocy;
1919 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1922 void
1923 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1925 u_int px, py, nx, ny;
1927 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1929 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1930 tty_margin_off(tty);
1932 px = 0;
1933 nx = ctx->sx;
1934 py = 0;
1935 ny = ctx->ocy;
1937 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1939 px = 0;
1940 nx = ctx->ocx + 1;
1941 py = ctx->ocy;
1943 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1946 void
1947 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1949 u_int px, py, nx, ny;
1951 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1953 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1954 tty_margin_off(tty);
1956 px = 0;
1957 nx = ctx->sx;
1958 py = 0;
1959 ny = ctx->sy;
1961 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1964 void
1965 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1967 u_int i, j;
1969 if (ctx->bigger) {
1970 ctx->redraw_cb(ctx);
1971 return;
1974 tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette);
1976 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1977 tty_margin_off(tty);
1979 for (j = 0; j < ctx->sy; j++) {
1980 tty_cursor_pane(tty, ctx, 0, j);
1981 for (i = 0; i < ctx->sx; i++)
1982 tty_putc(tty, 'E');
1986 void
1987 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1989 const struct grid_cell *gcp = ctx->cell;
1990 struct screen *s = ctx->s;
1991 struct overlay_ranges r;
1992 u_int px, py, i, vis = 0;
1994 px = ctx->xoff + ctx->ocx - ctx->wox;
1995 py = ctx->yoff + ctx->ocy - ctx->woy;
1996 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
1997 (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
1998 return;
2000 /* Handle partially obstructed wide characters. */
2001 if (gcp->data.width > 1) {
2002 tty_check_overlay_range(tty, px, py, gcp->data.width, &r);
2003 for (i = 0; i < OVERLAY_MAX_RANGES; i++)
2004 vis += r.nx[i];
2005 if (vis < gcp->data.width) {
2006 tty_draw_line(tty, s, s->cx, s->cy, gcp->data.width,
2007 px, py, &ctx->defaults, ctx->palette);
2008 return;
2012 if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
2013 ctx->ocy == ctx->orlower &&
2014 tty_full_width(tty, ctx))
2015 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
2017 tty_margin_off(tty);
2018 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2020 tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette);
2023 void
2024 tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
2026 struct overlay_ranges r;
2027 u_int i, px, py, cx;
2028 char *cp = ctx->ptr;
2030 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
2031 return;
2033 if (ctx->bigger &&
2034 (ctx->xoff + ctx->ocx < ctx->wox ||
2035 ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
2036 if (!ctx->wrapped ||
2037 !tty_full_width(tty, ctx) ||
2038 (tty->term->flags & TERM_NOAM) ||
2039 ctx->xoff + ctx->ocx != 0 ||
2040 ctx->yoff + ctx->ocy != tty->cy + 1 ||
2041 tty->cx < tty->sx ||
2042 tty->cy == tty->rlower)
2043 tty_draw_pane(tty, ctx, ctx->ocy);
2044 else
2045 ctx->redraw_cb(ctx);
2046 return;
2049 tty_margin_off(tty);
2050 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2051 tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette);
2053 /* Get tty position from pane position for overlay check. */
2054 px = ctx->xoff + ctx->ocx - ctx->wox;
2055 py = ctx->yoff + ctx->ocy - ctx->woy;
2057 tty_check_overlay_range(tty, px, py, ctx->num, &r);
2058 for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
2059 if (r.nx[i] == 0)
2060 continue;
2061 /* Convert back to pane position for printing. */
2062 cx = r.px[i] - ctx->xoff + ctx->wox;
2063 tty_cursor_pane_unless_wrap(tty, ctx, cx, ctx->ocy);
2064 tty_putn(tty, cp + r.px[i] - px, r.nx[i], r.nx[i]);
2068 void
2069 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
2071 tty_set_selection(tty, ctx->ptr, ctx->num);
2074 void
2075 tty_set_selection(struct tty *tty, const char *buf, size_t len)
2077 char *encoded;
2078 size_t size;
2080 if (~tty->flags & TTY_STARTED)
2081 return;
2082 if (!tty_term_has(tty->term, TTYC_MS))
2083 return;
2085 size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
2086 encoded = xmalloc(size);
2088 b64_ntop(buf, len, encoded, size);
2089 tty->flags |= TTY_NOBLOCK;
2090 tty_putcode_ptr2(tty, TTYC_MS, "", encoded);
2092 free(encoded);
2095 void
2096 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
2098 tty->flags |= TTY_NOBLOCK;
2099 tty_add(tty, ctx->ptr, ctx->num);
2100 tty_invalidate(tty);
2103 void
2104 tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
2106 if (ctx->num == 0x11) {
2108 * This is an overlay and a command that moves the cursor so
2109 * start synchronized updates.
2111 tty_sync_start(tty);
2112 } else if (~ctx->num & 0x10) {
2114 * This is a pane. If there is an overlay, always start;
2115 * otherwise, only if requested.
2117 if (ctx->num || tty->client->overlay_draw != NULL)
2118 tty_sync_start(tty);
2122 void
2123 tty_cell(struct tty *tty, const struct grid_cell *gc,
2124 const struct grid_cell *defaults, struct colour_palette *palette)
2126 const struct grid_cell *gcp;
2128 /* Skip last character if terminal is stupid. */
2129 if ((tty->term->flags & TERM_NOAM) &&
2130 tty->cy == tty->sy - 1 &&
2131 tty->cx == tty->sx - 1)
2132 return;
2134 /* If this is a padding character, do nothing. */
2135 if (gc->flags & GRID_FLAG_PADDING)
2136 return;
2138 /* Check the output codeset and apply attributes. */
2139 gcp = tty_check_codeset(tty, gc);
2140 tty_attributes(tty, gcp, defaults, palette);
2142 /* If it is a single character, write with putc to handle ACS. */
2143 if (gcp->data.size == 1) {
2144 tty_attributes(tty, gcp, defaults, palette);
2145 if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
2146 return;
2147 tty_putc(tty, *gcp->data.data);
2148 return;
2151 /* Write the data. */
2152 tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
2155 void
2156 tty_reset(struct tty *tty)
2158 struct grid_cell *gc = &tty->cell;
2160 if (!grid_cells_equal(gc, &grid_default_cell)) {
2161 if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2162 tty_putcode(tty, TTYC_RMACS);
2163 tty_putcode(tty, TTYC_SGR0);
2164 memcpy(gc, &grid_default_cell, sizeof *gc);
2166 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2169 static void
2170 tty_invalidate(struct tty *tty)
2172 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2173 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2175 tty->cx = tty->cy = UINT_MAX;
2176 tty->rupper = tty->rleft = UINT_MAX;
2177 tty->rlower = tty->rright = UINT_MAX;
2179 if (tty->flags & TTY_STARTED) {
2180 if (tty_use_margin(tty))
2181 tty_putcode(tty, TTYC_ENMG);
2182 tty_putcode(tty, TTYC_SGR0);
2184 tty->mode = ALL_MODES;
2185 tty_update_mode(tty, MODE_CURSOR, NULL);
2187 tty_cursor(tty, 0, 0);
2188 tty_region_off(tty);
2189 tty_margin_off(tty);
2190 } else
2191 tty->mode = MODE_CURSOR;
2194 /* Turn off margin. */
2195 void
2196 tty_region_off(struct tty *tty)
2198 tty_region(tty, 0, tty->sy - 1);
2201 /* Set region inside pane. */
2202 static void
2203 tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2204 u_int rlower)
2206 tty_region(tty, ctx->yoff + rupper - ctx->woy,
2207 ctx->yoff + rlower - ctx->woy);
2210 /* Set region at absolute position. */
2211 static void
2212 tty_region(struct tty *tty, u_int rupper, u_int rlower)
2214 if (tty->rlower == rlower && tty->rupper == rupper)
2215 return;
2216 if (!tty_term_has(tty->term, TTYC_CSR))
2217 return;
2219 tty->rupper = rupper;
2220 tty->rlower = rlower;
2223 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2224 * 0,0 if it is beyond the last column (they do not reset their wrap
2225 * flag so further output causes a line feed). As a workaround, do an
2226 * explicit move to 0 first.
2228 if (tty->cx >= tty->sx) {
2229 if (tty->cy == UINT_MAX)
2230 tty_cursor(tty, 0, 0);
2231 else
2232 tty_cursor(tty, 0, tty->cy);
2235 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2236 tty->cx = tty->cy = UINT_MAX;
2239 /* Turn off margin. */
2240 void
2241 tty_margin_off(struct tty *tty)
2243 tty_margin(tty, 0, tty->sx - 1);
2246 /* Set margin inside pane. */
2247 static void
2248 tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2250 tty_margin(tty, ctx->xoff - ctx->wox,
2251 ctx->xoff + ctx->sx - 1 - ctx->wox);
2254 /* Set margin at absolute position. */
2255 static void
2256 tty_margin(struct tty *tty, u_int rleft, u_int rright)
2258 if (!tty_use_margin(tty))
2259 return;
2260 if (tty->rleft == rleft && tty->rright == rright)
2261 return;
2263 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2265 tty->rleft = rleft;
2266 tty->rright = rright;
2268 if (rleft == 0 && rright == tty->sx - 1)
2269 tty_putcode(tty, TTYC_CLMG);
2270 else
2271 tty_putcode2(tty, TTYC_CMG, rleft, rright);
2272 tty->cx = tty->cy = UINT_MAX;
2276 * Move the cursor, unless it would wrap itself when the next character is
2277 * printed.
2279 static void
2280 tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2281 u_int cx, u_int cy)
2283 if (!ctx->wrapped ||
2284 !tty_full_width(tty, ctx) ||
2285 (tty->term->flags & TERM_NOAM) ||
2286 ctx->xoff + cx != 0 ||
2287 ctx->yoff + cy != tty->cy + 1 ||
2288 tty->cx < tty->sx ||
2289 tty->cy == tty->rlower)
2290 tty_cursor_pane(tty, ctx, cx, cy);
2291 else
2292 log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
2295 /* Move cursor inside pane. */
2296 static void
2297 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2299 tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
2302 /* Move cursor to absolute position. */
2303 void
2304 tty_cursor(struct tty *tty, u_int cx, u_int cy)
2306 struct tty_term *term = tty->term;
2307 u_int thisx, thisy;
2308 int change;
2310 if (tty->flags & TTY_BLOCK)
2311 return;
2313 thisx = tty->cx;
2314 thisy = tty->cy;
2317 * If in the automargin space, and want to be there, do not move.
2318 * Otherwise, force the cursor to be in range (and complain).
2320 if (cx == thisx && cy == thisy && cx == tty->sx)
2321 return;
2322 if (cx > tty->sx - 1) {
2323 log_debug("%s: x too big %u > %u", __func__, cx, tty->sx - 1);
2324 cx = tty->sx - 1;
2327 /* No change. */
2328 if (cx == thisx && cy == thisy)
2329 return;
2331 /* Currently at the very end of the line - use absolute movement. */
2332 if (thisx > tty->sx - 1)
2333 goto absolute;
2335 /* Move to home position (0, 0). */
2336 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2337 tty_putcode(tty, TTYC_HOME);
2338 goto out;
2341 /* Zero on the next line. */
2342 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2343 (!tty_use_margin(tty) || tty->rleft == 0)) {
2344 tty_putc(tty, '\r');
2345 tty_putc(tty, '\n');
2346 goto out;
2349 /* Moving column or row. */
2350 if (cy == thisy) {
2352 * Moving column only, row staying the same.
2355 /* To left edge. */
2356 if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
2357 tty_putc(tty, '\r');
2358 goto out;
2361 /* One to the left. */
2362 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2363 tty_putcode(tty, TTYC_CUB1);
2364 goto out;
2367 /* One to the right. */
2368 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2369 tty_putcode(tty, TTYC_CUF1);
2370 goto out;
2373 /* Calculate difference. */
2374 change = thisx - cx; /* +ve left, -ve right */
2377 * Use HPA if change is larger than absolute, otherwise move
2378 * the cursor with CUB/CUF.
2380 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
2381 tty_putcode1(tty, TTYC_HPA, cx);
2382 goto out;
2383 } else if (change > 0 &&
2384 tty_term_has(term, TTYC_CUB) &&
2385 !tty_use_margin(tty)) {
2386 if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2387 tty_putcode(tty, TTYC_CUB1);
2388 tty_putcode(tty, TTYC_CUB1);
2389 goto out;
2391 tty_putcode1(tty, TTYC_CUB, change);
2392 goto out;
2393 } else if (change < 0 &&
2394 tty_term_has(term, TTYC_CUF) &&
2395 !tty_use_margin(tty)) {
2396 tty_putcode1(tty, TTYC_CUF, -change);
2397 goto out;
2399 } else if (cx == thisx) {
2401 * Moving row only, column staying the same.
2404 /* One above. */
2405 if (thisy != tty->rupper &&
2406 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2407 tty_putcode(tty, TTYC_CUU1);
2408 goto out;
2411 /* One below. */
2412 if (thisy != tty->rlower &&
2413 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2414 tty_putcode(tty, TTYC_CUD1);
2415 goto out;
2418 /* Calculate difference. */
2419 change = thisy - cy; /* +ve up, -ve down */
2422 * Try to use VPA if change is larger than absolute or if this
2423 * change would cross the scroll region, otherwise use CUU/CUD.
2425 if ((u_int) abs(change) > cy ||
2426 (change < 0 && cy - change > tty->rlower) ||
2427 (change > 0 && cy - change < tty->rupper)) {
2428 if (tty_term_has(term, TTYC_VPA)) {
2429 tty_putcode1(tty, TTYC_VPA, cy);
2430 goto out;
2432 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2433 tty_putcode1(tty, TTYC_CUU, change);
2434 goto out;
2435 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2436 tty_putcode1(tty, TTYC_CUD, -change);
2437 goto out;
2441 absolute:
2442 /* Absolute movement. */
2443 tty_putcode2(tty, TTYC_CUP, cy, cx);
2445 out:
2446 tty->cx = cx;
2447 tty->cy = cy;
2450 void
2451 tty_attributes(struct tty *tty, const struct grid_cell *gc,
2452 const struct grid_cell *defaults, struct colour_palette *palette)
2454 struct grid_cell *tc = &tty->cell, gc2;
2455 int changed;
2457 /* Copy cell and update default colours. */
2458 memcpy(&gc2, gc, sizeof gc2);
2459 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2460 if (gc2.fg == 8)
2461 gc2.fg = defaults->fg;
2462 if (gc2.bg == 8)
2463 gc2.bg = defaults->bg;
2466 /* Ignore cell if it is the same as the last one. */
2467 if (gc2.attr == tty->last_cell.attr &&
2468 gc2.fg == tty->last_cell.fg &&
2469 gc2.bg == tty->last_cell.bg &&
2470 gc2.us == tty->last_cell.us)
2471 return;
2474 * If no setab, try to use the reverse attribute as a best-effort for a
2475 * non-default background. This is a bit of a hack but it doesn't do
2476 * any serious harm and makes a couple of applications happier.
2478 if (!tty_term_has(tty->term, TTYC_SETAB)) {
2479 if (gc2.attr & GRID_ATTR_REVERSE) {
2480 if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
2481 gc2.attr &= ~GRID_ATTR_REVERSE;
2482 } else {
2483 if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
2484 gc2.attr |= GRID_ATTR_REVERSE;
2488 /* Fix up the colours if necessary. */
2489 tty_check_fg(tty, palette, &gc2);
2490 tty_check_bg(tty, palette, &gc2);
2491 tty_check_us(tty, palette, &gc2);
2494 * If any bits are being cleared or the underline colour is now default,
2495 * reset everything.
2497 if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
2498 tty_reset(tty);
2501 * Set the colours. This may call tty_reset() (so it comes next) and
2502 * may add to (NOT remove) the desired attributes.
2504 tty_colours(tty, &gc2);
2506 /* Filter out attribute bits already set. */
2507 changed = gc2.attr & ~tc->attr;
2508 tc->attr = gc2.attr;
2510 /* Set the attributes. */
2511 if (changed & GRID_ATTR_BRIGHT)
2512 tty_putcode(tty, TTYC_BOLD);
2513 if (changed & GRID_ATTR_DIM)
2514 tty_putcode(tty, TTYC_DIM);
2515 if (changed & GRID_ATTR_ITALICS)
2516 tty_set_italics(tty);
2517 if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2518 if ((changed & GRID_ATTR_UNDERSCORE) ||
2519 !tty_term_has(tty->term, TTYC_SMULX))
2520 tty_putcode(tty, TTYC_SMUL);
2521 else if (changed & GRID_ATTR_UNDERSCORE_2)
2522 tty_putcode1(tty, TTYC_SMULX, 2);
2523 else if (changed & GRID_ATTR_UNDERSCORE_3)
2524 tty_putcode1(tty, TTYC_SMULX, 3);
2525 else if (changed & GRID_ATTR_UNDERSCORE_4)
2526 tty_putcode1(tty, TTYC_SMULX, 4);
2527 else if (changed & GRID_ATTR_UNDERSCORE_5)
2528 tty_putcode1(tty, TTYC_SMULX, 5);
2530 if (changed & GRID_ATTR_BLINK)
2531 tty_putcode(tty, TTYC_BLINK);
2532 if (changed & GRID_ATTR_REVERSE) {
2533 if (tty_term_has(tty->term, TTYC_REV))
2534 tty_putcode(tty, TTYC_REV);
2535 else if (tty_term_has(tty->term, TTYC_SMSO))
2536 tty_putcode(tty, TTYC_SMSO);
2538 if (changed & GRID_ATTR_HIDDEN)
2539 tty_putcode(tty, TTYC_INVIS);
2540 if (changed & GRID_ATTR_STRIKETHROUGH)
2541 tty_putcode(tty, TTYC_SMXX);
2542 if (changed & GRID_ATTR_OVERLINE)
2543 tty_putcode(tty, TTYC_SMOL);
2544 if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2545 tty_putcode(tty, TTYC_SMACS);
2547 memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
2550 static void
2551 tty_colours(struct tty *tty, const struct grid_cell *gc)
2553 struct grid_cell *tc = &tty->cell;
2554 int have_ax;
2556 /* No changes? Nothing is necessary. */
2557 if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
2558 return;
2561 * Is either the default colour? This is handled specially because the
2562 * best solution might be to reset both colours to default, in which
2563 * case if only one is default need to fall onward to set the other
2564 * colour.
2566 if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
2568 * If don't have AX but do have op, send sgr0 (op can't
2569 * actually be used because it is sometimes the same as sgr0
2570 * and sometimes isn't). This resets both colours to default.
2572 * Otherwise, try to set the default colour only as needed.
2574 have_ax = tty_term_flag(tty->term, TTYC_AX);
2575 if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2576 tty_reset(tty);
2577 else {
2578 if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
2579 if (have_ax)
2580 tty_puts(tty, "\033[39m");
2581 else if (tc->fg != 7)
2582 tty_putcode1(tty, TTYC_SETAF, 7);
2583 tc->fg = gc->fg;
2585 if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
2586 if (have_ax)
2587 tty_puts(tty, "\033[49m");
2588 else if (tc->bg != 0)
2589 tty_putcode1(tty, TTYC_SETAB, 0);
2590 tc->bg = gc->bg;
2595 /* Set the foreground colour. */
2596 if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
2597 tty_colours_fg(tty, gc);
2600 * Set the background colour. This must come after the foreground as
2601 * tty_colour_fg() can call tty_reset().
2603 if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
2604 tty_colours_bg(tty, gc);
2606 /* Set the underscore colour. */
2607 if (gc->us != tc->us)
2608 tty_colours_us(tty, gc);
2611 static void
2612 tty_check_fg(struct tty *tty, struct colour_palette *palette,
2613 struct grid_cell *gc)
2615 u_char r, g, b;
2616 u_int colours;
2617 int c;
2620 * Perform substitution if this pane has a palette. If the bright
2621 * attribute is set, use the bright entry in the palette by changing to
2622 * the aixterm colour.
2624 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2625 c = gc->fg;
2626 if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
2627 c += 90;
2628 if ((c = colour_palette_get(palette, c)) != -1)
2629 gc->fg = c;
2632 /* Is this a 24-bit colour? */
2633 if (gc->fg & COLOUR_FLAG_RGB) {
2634 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2635 if (tty->term->flags & TERM_RGBCOLOURS)
2636 return;
2637 colour_split_rgb(gc->fg, &r, &g, &b);
2638 gc->fg = colour_find_rgb(r, g, b);
2641 /* How many colours does this terminal have? */
2642 if (tty->term->flags & TERM_256COLOURS)
2643 colours = 256;
2644 else
2645 colours = tty_term_number(tty->term, TTYC_COLORS);
2647 /* Is this a 256-colour colour? */
2648 if (gc->fg & COLOUR_FLAG_256) {
2649 /* And not a 256 colour mode? */
2650 if (colours < 256) {
2651 gc->fg = colour_256to16(gc->fg);
2652 if (gc->fg & 8) {
2653 gc->fg &= 7;
2654 if (colours >= 16)
2655 gc->fg += 90;
2658 return;
2661 /* Is this an aixterm colour? */
2662 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2663 gc->fg -= 90;
2664 gc->attr |= GRID_ATTR_BRIGHT;
2668 static void
2669 tty_check_bg(struct tty *tty, struct colour_palette *palette,
2670 struct grid_cell *gc)
2672 u_char r, g, b;
2673 u_int colours;
2674 int c;
2676 /* Perform substitution if this pane has a palette. */
2677 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2678 if ((c = colour_palette_get(palette, gc->bg)) != -1)
2679 gc->bg = c;
2682 /* Is this a 24-bit colour? */
2683 if (gc->bg & COLOUR_FLAG_RGB) {
2684 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2685 if (tty->term->flags & TERM_RGBCOLOURS)
2686 return;
2687 colour_split_rgb(gc->bg, &r, &g, &b);
2688 gc->bg = colour_find_rgb(r, g, b);
2691 /* How many colours does this terminal have? */
2692 if (tty->term->flags & TERM_256COLOURS)
2693 colours = 256;
2694 else
2695 colours = tty_term_number(tty->term, TTYC_COLORS);
2697 /* Is this a 256-colour colour? */
2698 if (gc->bg & COLOUR_FLAG_256) {
2700 * And not a 256 colour mode? Translate to 16-colour
2701 * palette. Bold background doesn't exist portably, so just
2702 * discard the bold bit if set.
2704 if (colours < 256) {
2705 gc->bg = colour_256to16(gc->bg);
2706 if (gc->bg & 8) {
2707 gc->bg &= 7;
2708 if (colours >= 16)
2709 gc->bg += 90;
2712 return;
2715 /* Is this an aixterm colour? */
2716 if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
2717 gc->bg -= 90;
2720 static void
2721 tty_check_us(__unused struct tty *tty, struct colour_palette *palette,
2722 struct grid_cell *gc)
2724 int c;
2726 /* Perform substitution if this pane has a palette. */
2727 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2728 if ((c = colour_palette_get(palette, gc->us)) != -1)
2729 gc->us = c;
2732 /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2733 if (gc->us & COLOUR_FLAG_256)
2734 gc->us = colour_256toRGB (gc->us);
2737 static void
2738 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
2740 struct grid_cell *tc = &tty->cell;
2741 char s[32];
2743 /* Is this a 24-bit or 256-colour colour? */
2744 if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
2745 if (tty_try_colour(tty, gc->fg, "38") == 0)
2746 goto save;
2747 /* Should not get here, already converted in tty_check_fg. */
2748 return;
2751 /* Is this an aixterm bright colour? */
2752 if (gc->fg >= 90 && gc->fg <= 97) {
2753 if (tty->term->flags & TERM_256COLOURS) {
2754 xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2755 tty_puts(tty, s);
2756 } else
2757 tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
2758 goto save;
2761 /* Otherwise set the foreground colour. */
2762 tty_putcode1(tty, TTYC_SETAF, gc->fg);
2764 save:
2765 /* Save the new values in the terminal current cell. */
2766 tc->fg = gc->fg;
2769 static void
2770 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
2772 struct grid_cell *tc = &tty->cell;
2773 char s[32];
2775 /* Is this a 24-bit or 256-colour colour? */
2776 if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
2777 if (tty_try_colour(tty, gc->bg, "48") == 0)
2778 goto save;
2779 /* Should not get here, already converted in tty_check_bg. */
2780 return;
2783 /* Is this an aixterm bright colour? */
2784 if (gc->bg >= 90 && gc->bg <= 97) {
2785 if (tty->term->flags & TERM_256COLOURS) {
2786 xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2787 tty_puts(tty, s);
2788 } else
2789 tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
2790 goto save;
2793 /* Otherwise set the background colour. */
2794 tty_putcode1(tty, TTYC_SETAB, gc->bg);
2796 save:
2797 /* Save the new values in the terminal current cell. */
2798 tc->bg = gc->bg;
2801 static void
2802 tty_colours_us(struct tty *tty, const struct grid_cell *gc)
2804 struct grid_cell *tc = &tty->cell;
2805 u_int c;
2806 u_char r, g, b;
2808 /* Clear underline colour. */
2809 if (gc->us == 0) {
2810 tty_putcode(tty, TTYC_OL);
2811 goto save;
2814 /* Must be an RGB colour - this should never happen. */
2815 if (~gc->us & COLOUR_FLAG_RGB)
2816 return;
2819 * Setulc and setal follows the ncurses(3) one argument "direct colour"
2820 * capability format. Calculate the colour value.
2822 colour_split_rgb(gc->us, &r, &g, &b);
2823 c = (65536 * r) + (256 * g) + b;
2826 * Write the colour. Only use setal if the RGB flag is set because the
2827 * non-RGB version may be wrong.
2829 if (tty_term_has(tty->term, TTYC_SETULC))
2830 tty_putcode1(tty, TTYC_SETULC, c);
2831 else if (tty_term_has(tty->term, TTYC_SETAL) &&
2832 tty_term_has(tty->term, TTYC_RGB))
2833 tty_putcode1(tty, TTYC_SETAL, c);
2835 save:
2836 /* Save the new values in the terminal current cell. */
2837 tc->us = gc->us;
2840 static int
2841 tty_try_colour(struct tty *tty, int colour, const char *type)
2843 u_char r, g, b;
2845 if (colour & COLOUR_FLAG_256) {
2846 if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
2847 tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2848 else if (tty_term_has(tty->term, TTYC_SETAB))
2849 tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2850 return (0);
2853 if (colour & COLOUR_FLAG_RGB) {
2854 colour_split_rgb(colour & 0xffffff, &r, &g, &b);
2855 if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
2856 tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
2857 else if (tty_term_has(tty->term, TTYC_SETRGBB))
2858 tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
2859 return (0);
2862 return (-1);
2865 static void
2866 tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
2868 memcpy(gc, &grid_default_cell, sizeof *gc);
2869 gc->fg = wp->palette.fg;
2870 gc->bg = wp->palette.bg;
2873 void
2874 tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
2876 struct options *oo = wp->options;
2877 struct format_tree *ft;
2879 memcpy(gc, &grid_default_cell, sizeof *gc);
2881 if (wp->flags & PANE_STYLECHANGED) {
2882 log_debug("%%%u: style changed", wp->id);
2883 wp->flags &= ~PANE_STYLECHANGED;
2885 ft = format_create(NULL, NULL, FORMAT_PANE|wp->id,
2886 FORMAT_NOJOBS);
2887 format_defaults(ft, NULL, NULL, NULL, wp);
2888 tty_window_default_style(&wp->cached_active_gc, wp);
2889 style_add(&wp->cached_active_gc, oo, "window-active-style", ft);
2890 tty_window_default_style(&wp->cached_gc, wp);
2891 style_add(&wp->cached_gc, oo, "window-style", ft);
2892 format_free(ft);
2895 if (gc->fg == 8) {
2896 if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
2897 gc->fg = wp->cached_active_gc.fg;
2898 else
2899 gc->fg = wp->cached_gc.fg;
2902 if (gc->bg == 8) {
2903 if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
2904 gc->bg = wp->cached_active_gc.bg;
2905 else
2906 gc->bg = wp->cached_gc.bg;
2910 static void
2911 tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
2912 struct colour_palette *palette, u_int bg)
2914 struct grid_cell gc;
2916 memcpy(&gc, &grid_default_cell, sizeof gc);
2917 gc.bg = bg;
2918 tty_attributes(tty, &gc, defaults, palette);