Add an option to set the character used for unused areas of the
[tmux-openbsd.git] / tty.c
blob40735cebc757ebb9fd2f352b9f823a68bfdb17f2
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 #define TTY_QUERY_TIMEOUT 5
88 void
89 tty_create_log(void)
91 char name[64];
93 xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
95 tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
96 if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
97 fatal("fcntl failed");
101 tty_init(struct tty *tty, struct client *c)
103 if (!isatty(c->fd))
104 return (-1);
106 memset(tty, 0, sizeof *tty);
107 tty->client = c;
109 tty->cstyle = SCREEN_CURSOR_DEFAULT;
110 tty->ccolour = -1;
112 if (tcgetattr(c->fd, &tty->tio) != 0)
113 return (-1);
114 return (0);
117 void
118 tty_resize(struct tty *tty)
120 struct client *c = tty->client;
121 struct winsize ws;
122 u_int sx, sy, xpixel, ypixel;
124 if (ioctl(c->fd, TIOCGWINSZ, &ws) != -1) {
125 sx = ws.ws_col;
126 if (sx == 0) {
127 sx = 80;
128 xpixel = 0;
129 } else
130 xpixel = ws.ws_xpixel / sx;
131 sy = ws.ws_row;
132 if (sy == 0) {
133 sy = 24;
134 ypixel = 0;
135 } else
136 ypixel = ws.ws_ypixel / sy;
137 } else {
138 sx = 80;
139 sy = 24;
140 xpixel = 0;
141 ypixel = 0;
143 log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy,
144 xpixel, ypixel);
145 tty_set_size(tty, sx, sy, xpixel, ypixel);
146 tty_invalidate(tty);
149 void
150 tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
152 tty->sx = sx;
153 tty->sy = sy;
154 tty->xpixel = xpixel;
155 tty->ypixel = ypixel;
158 static void
159 tty_read_callback(__unused int fd, __unused short events, void *data)
161 struct tty *tty = data;
162 struct client *c = tty->client;
163 const char *name = c->name;
164 size_t size = EVBUFFER_LENGTH(tty->in);
165 int nread;
167 nread = evbuffer_read(tty->in, c->fd, -1);
168 if (nread == 0 || nread == -1) {
169 if (nread == 0)
170 log_debug("%s: read closed", name);
171 else
172 log_debug("%s: read error: %s", name, strerror(errno));
173 event_del(&tty->event_in);
174 server_client_lost(tty->client);
175 return;
177 log_debug("%s: read %d bytes (already %zu)", name, nread, size);
179 while (tty_keys_next(tty))
183 static void
184 tty_timer_callback(__unused int fd, __unused short events, void *data)
186 struct tty *tty = data;
187 struct client *c = tty->client;
188 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
190 log_debug("%s: %zu discarded", c->name, tty->discarded);
192 c->flags |= CLIENT_ALLREDRAWFLAGS;
193 c->discarded += tty->discarded;
195 if (tty->discarded < TTY_BLOCK_STOP(tty)) {
196 tty->flags &= ~TTY_BLOCK;
197 tty_invalidate(tty);
198 return;
200 tty->discarded = 0;
201 evtimer_add(&tty->timer, &tv);
204 static int
205 tty_block_maybe(struct tty *tty)
207 struct client *c = tty->client;
208 size_t size = EVBUFFER_LENGTH(tty->out);
209 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
211 if (size == 0)
212 tty->flags &= ~TTY_NOBLOCK;
213 else if (tty->flags & TTY_NOBLOCK)
214 return (0);
216 if (size < TTY_BLOCK_START(tty))
217 return (0);
219 if (tty->flags & TTY_BLOCK)
220 return (1);
221 tty->flags |= TTY_BLOCK;
223 log_debug("%s: can't keep up, %zu discarded", c->name, size);
225 evbuffer_drain(tty->out, size);
226 c->discarded += size;
228 tty->discarded = 0;
229 evtimer_add(&tty->timer, &tv);
230 return (1);
233 static void
234 tty_write_callback(__unused int fd, __unused short events, void *data)
236 struct tty *tty = data;
237 struct client *c = tty->client;
238 size_t size = EVBUFFER_LENGTH(tty->out);
239 int nwrite;
241 nwrite = evbuffer_write(tty->out, c->fd);
242 if (nwrite == -1)
243 return;
244 log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
246 if (c->redraw > 0) {
247 if ((size_t)nwrite >= c->redraw)
248 c->redraw = 0;
249 else
250 c->redraw -= nwrite;
251 log_debug("%s: waiting for redraw, %zu bytes left", c->name,
252 c->redraw);
253 } else if (tty_block_maybe(tty))
254 return;
256 if (EVBUFFER_LENGTH(tty->out) != 0)
257 event_add(&tty->event_out, NULL);
261 tty_open(struct tty *tty, char **cause)
263 struct client *c = tty->client;
265 tty->term = tty_term_create(tty, c->term_name, c->term_caps,
266 c->term_ncaps, &c->term_features, cause);
267 if (tty->term == NULL) {
268 tty_close(tty);
269 return (-1);
271 tty->flags |= TTY_OPENED;
273 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
275 event_set(&tty->event_in, c->fd, EV_PERSIST|EV_READ,
276 tty_read_callback, tty);
277 tty->in = evbuffer_new();
278 if (tty->in == NULL)
279 fatal("out of memory");
281 event_set(&tty->event_out, c->fd, EV_WRITE, tty_write_callback, tty);
282 tty->out = evbuffer_new();
283 if (tty->out == NULL)
284 fatal("out of memory");
286 evtimer_set(&tty->timer, tty_timer_callback, tty);
288 tty_start_tty(tty);
290 tty_keys_build(tty);
292 return (0);
295 static void
296 tty_start_timer_callback(__unused int fd, __unused short events, void *data)
298 struct tty *tty = data;
299 struct client *c = tty->client;
301 log_debug("%s: start timer fired", c->name);
302 if ((tty->flags & (TTY_HAVEDA|TTY_HAVEXDA)) == 0)
303 tty_update_features(tty);
304 tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
307 void
308 tty_start_tty(struct tty *tty)
310 struct client *c = tty->client;
311 struct termios tio;
312 struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT };
314 setblocking(c->fd, 0);
315 event_add(&tty->event_in, NULL);
317 memcpy(&tio, &tty->tio, sizeof tio);
318 tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
319 tio.c_iflag |= IGNBRK;
320 tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
321 tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|ECHOPRT|
322 ECHOKE|ISIG);
323 tio.c_cc[VMIN] = 1;
324 tio.c_cc[VTIME] = 0;
325 if (tcsetattr(c->fd, TCSANOW, &tio) == 0)
326 tcflush(c->fd, TCOFLUSH);
328 tty_putcode(tty, TTYC_SMCUP);
330 tty_putcode(tty, TTYC_SMKX);
331 tty_putcode(tty, TTYC_CLEAR);
333 if (tty_acs_needed(tty)) {
334 log_debug("%s: using capabilities for ACS", c->name);
335 tty_putcode(tty, TTYC_ENACS);
336 } else
337 log_debug("%s: using UTF-8 for ACS", c->name);
339 tty_putcode(tty, TTYC_CNORM);
340 if (tty_term_has(tty->term, TTYC_KMOUS)) {
341 tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
342 tty_puts(tty, "\033[?1006l\033[?1005l");
345 evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
346 evtimer_add(&tty->start_timer, &tv);
348 tty->flags |= TTY_STARTED;
349 tty_invalidate(tty);
351 if (tty->ccolour != -1)
352 tty_force_cursor_colour(tty, -1);
354 tty->mouse_drag_flag = 0;
355 tty->mouse_drag_update = NULL;
356 tty->mouse_drag_release = NULL;
359 void
360 tty_send_requests(struct tty *tty)
362 if (~tty->flags & TTY_STARTED)
363 return;
365 if (tty->term->flags & TERM_VT100LIKE) {
366 if (~tty->flags & TTY_HAVEDA)
367 tty_puts(tty, "\033[>c");
368 if (~tty->flags & TTY_HAVEXDA)
369 tty_puts(tty, "\033[>q");
370 } else
371 tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
374 void
375 tty_stop_tty(struct tty *tty)
377 struct client *c = tty->client;
378 struct winsize ws;
380 if (!(tty->flags & TTY_STARTED))
381 return;
382 tty->flags &= ~TTY_STARTED;
384 evtimer_del(&tty->start_timer);
386 event_del(&tty->timer);
387 tty->flags &= ~TTY_BLOCK;
389 event_del(&tty->event_in);
390 event_del(&tty->event_out);
393 * Be flexible about error handling and try not kill the server just
394 * because the fd is invalid. Things like ssh -t can easily leave us
395 * with a dead tty.
397 if (ioctl(c->fd, TIOCGWINSZ, &ws) == -1)
398 return;
399 if (tcsetattr(c->fd, TCSANOW, &tty->tio) == -1)
400 return;
402 tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
403 if (tty_acs_needed(tty))
404 tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
405 tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
406 tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
407 tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
408 if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
409 if (tty_term_has(tty->term, TTYC_SE))
410 tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
411 else if (tty_term_has(tty->term, TTYC_SS))
412 tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
414 if (tty->mode & MODE_BRACKETPASTE)
415 tty_raw(tty, tty_term_string(tty->term, TTYC_DSBP));
416 if (tty->ccolour != -1)
417 tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
419 tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
420 if (tty_term_has(tty->term, TTYC_KMOUS)) {
421 tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
422 tty_raw(tty, "\033[?1006l\033[?1005l");
425 if (tty->term->flags & TERM_VT100LIKE)
426 tty_raw(tty, "\033[?7727l");
427 tty_raw(tty, tty_term_string(tty->term, TTYC_DSFCS));
428 tty_raw(tty, tty_term_string(tty->term, TTYC_DSEKS));
430 if (tty_use_margin(tty))
431 tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG));
432 tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
434 setblocking(c->fd, 1);
437 void
438 tty_close(struct tty *tty)
440 if (event_initialized(&tty->key_timer))
441 evtimer_del(&tty->key_timer);
442 tty_stop_tty(tty);
444 if (tty->flags & TTY_OPENED) {
445 evbuffer_free(tty->in);
446 event_del(&tty->event_in);
447 evbuffer_free(tty->out);
448 event_del(&tty->event_out);
450 tty_term_free(tty->term);
451 tty_keys_free(tty);
453 tty->flags &= ~TTY_OPENED;
457 void
458 tty_free(struct tty *tty)
460 tty_close(tty);
463 void
464 tty_update_features(struct tty *tty)
466 struct client *c = tty->client;
468 if (tty_apply_features(tty->term, c->term_features))
469 tty_term_apply_overrides(tty->term);
471 if (tty_use_margin(tty))
472 tty_putcode(tty, TTYC_ENMG);
473 if (options_get_number(global_options, "extended-keys"))
474 tty_puts(tty, tty_term_string(tty->term, TTYC_ENEKS));
475 if (options_get_number(global_options, "focus-events"))
476 tty_puts(tty, tty_term_string(tty->term, TTYC_ENFCS));
477 if (tty->term->flags & TERM_VT100LIKE)
478 tty_puts(tty, "\033[?7727h");
481 void
482 tty_raw(struct tty *tty, const char *s)
484 struct client *c = tty->client;
485 ssize_t n, slen;
486 u_int i;
488 slen = strlen(s);
489 for (i = 0; i < 5; i++) {
490 n = write(c->fd, s, slen);
491 if (n >= 0) {
492 s += n;
493 slen -= n;
494 if (slen == 0)
495 break;
496 } else if (n == -1 && errno != EAGAIN)
497 break;
498 usleep(100);
502 void
503 tty_putcode(struct tty *tty, enum tty_code_code code)
505 tty_puts(tty, tty_term_string(tty->term, code));
508 void
509 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
511 if (a < 0)
512 return;
513 tty_puts(tty, tty_term_string1(tty->term, code, a));
516 void
517 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
519 if (a < 0 || b < 0)
520 return;
521 tty_puts(tty, tty_term_string2(tty->term, code, a, b));
524 void
525 tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
527 if (a < 0 || b < 0 || c < 0)
528 return;
529 tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
532 void
533 tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
535 if (a != NULL)
536 tty_puts(tty, tty_term_ptr1(tty->term, code, a));
539 void
540 tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
541 const void *b)
543 if (a != NULL && b != NULL)
544 tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
547 static void
548 tty_add(struct tty *tty, const char *buf, size_t len)
550 struct client *c = tty->client;
552 if (tty->flags & TTY_BLOCK) {
553 tty->discarded += len;
554 return;
557 evbuffer_add(tty->out, buf, len);
558 log_debug("%s: %.*s", c->name, (int)len, buf);
559 c->written += len;
561 if (tty_log_fd != -1)
562 write(tty_log_fd, buf, len);
563 if (tty->flags & TTY_STARTED)
564 event_add(&tty->event_out, NULL);
567 void
568 tty_puts(struct tty *tty, const char *s)
570 if (*s != '\0')
571 tty_add(tty, s, strlen(s));
574 void
575 tty_putc(struct tty *tty, u_char ch)
577 const char *acs;
579 if ((tty->term->flags & TERM_NOAM) &&
580 ch >= 0x20 && ch != 0x7f &&
581 tty->cy == tty->sy - 1 &&
582 tty->cx + 1 >= tty->sx)
583 return;
585 if (tty->cell.attr & GRID_ATTR_CHARSET) {
586 acs = tty_acs_get(tty, ch);
587 if (acs != NULL)
588 tty_add(tty, acs, strlen(acs));
589 else
590 tty_add(tty, &ch, 1);
591 } else
592 tty_add(tty, &ch, 1);
594 if (ch >= 0x20 && ch != 0x7f) {
595 if (tty->cx >= tty->sx) {
596 tty->cx = 1;
597 if (tty->cy != tty->rlower)
598 tty->cy++;
601 * On !am terminals, force the cursor position to where
602 * we think it should be after a line wrap - this means
603 * it works on sensible terminals as well.
605 if (tty->term->flags & TERM_NOAM)
606 tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
607 } else
608 tty->cx++;
612 void
613 tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
615 if ((tty->term->flags & TERM_NOAM) &&
616 tty->cy == tty->sy - 1 &&
617 tty->cx + len >= tty->sx)
618 len = tty->sx - tty->cx - 1;
620 tty_add(tty, buf, len);
621 if (tty->cx + width > tty->sx) {
622 tty->cx = (tty->cx + width) - tty->sx;
623 if (tty->cx <= tty->sx)
624 tty->cy++;
625 else
626 tty->cx = tty->cy = UINT_MAX;
627 } else
628 tty->cx += width;
631 static void
632 tty_set_italics(struct tty *tty)
634 const char *s;
636 if (tty_term_has(tty->term, TTYC_SITM)) {
637 s = options_get_string(global_options, "default-terminal");
638 if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
639 tty_putcode(tty, TTYC_SITM);
640 return;
643 tty_putcode(tty, TTYC_SMSO);
646 void
647 tty_set_title(struct tty *tty, const char *title)
649 if (!tty_term_has(tty->term, TTYC_TSL) ||
650 !tty_term_has(tty->term, TTYC_FSL))
651 return;
653 tty_putcode(tty, TTYC_TSL);
654 tty_puts(tty, title);
655 tty_putcode(tty, TTYC_FSL);
658 static void
659 tty_force_cursor_colour(struct tty *tty, int c)
661 u_char r, g, b;
662 char s[13] = "";
664 if (c != -1)
665 c = colour_force_rgb(c);
666 if (c == tty->ccolour)
667 return;
668 if (c == -1)
669 tty_putcode(tty, TTYC_CR);
670 else {
671 colour_split_rgb(c, &r, &g, &b);
672 xsnprintf(s, sizeof s, "rgb:%02hhx/%02hhx/%02hhx", r, g, b);
673 tty_putcode_ptr1(tty, TTYC_CS, s);
675 tty->ccolour = c;
678 static int
679 tty_update_cursor(struct tty *tty, int mode, struct screen *s)
681 enum screen_cursor_style cstyle;
682 int ccolour, changed, cmode = mode;
684 /* Set cursor colour if changed. */
685 if (s != NULL) {
686 ccolour = s->ccolour;
687 if (s->ccolour == -1)
688 ccolour = s->default_ccolour;
689 tty_force_cursor_colour(tty, ccolour);
692 /* If cursor is off, set as invisible. */
693 if (~cmode & MODE_CURSOR) {
694 if (tty->mode & MODE_CURSOR)
695 tty_putcode(tty, TTYC_CIVIS);
696 return (cmode);
699 /* Check if blinking or very visible flag changed or style changed. */
700 if (s == NULL)
701 cstyle = tty->cstyle;
702 else {
703 cstyle = s->cstyle;
704 if (cstyle == SCREEN_CURSOR_DEFAULT) {
705 if (~cmode & MODE_CURSOR_BLINKING_SET) {
706 if (s->default_mode & MODE_CURSOR_BLINKING)
707 cmode |= MODE_CURSOR_BLINKING;
708 else
709 cmode &= ~MODE_CURSOR_BLINKING;
711 cstyle = s->default_cstyle;
715 /* If nothing changed, do nothing. */
716 changed = cmode ^ tty->mode;
717 if ((changed & CURSOR_MODES) == 0 && cstyle == tty->cstyle)
718 return (cmode);
721 * Set cursor style. If an explicit style has been set with DECSCUSR,
722 * set it if supported, otherwise send cvvis for blinking styles.
724 * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
725 * if either the blinking or very visible flags are set.
727 tty_putcode(tty, TTYC_CNORM);
728 switch (cstyle) {
729 case SCREEN_CURSOR_DEFAULT:
730 if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
731 if (tty_term_has(tty->term, TTYC_SE))
732 tty_putcode(tty, TTYC_SE);
733 else
734 tty_putcode1(tty, TTYC_SS, 0);
736 if (cmode & (MODE_CURSOR_BLINKING|MODE_CURSOR_VERY_VISIBLE))
737 tty_putcode(tty, TTYC_CVVIS);
738 break;
739 case SCREEN_CURSOR_BLOCK:
740 if (tty_term_has(tty->term, TTYC_SS)) {
741 if (cmode & MODE_CURSOR_BLINKING)
742 tty_putcode1(tty, TTYC_SS, 1);
743 else
744 tty_putcode1(tty, TTYC_SS, 2);
745 } else if (cmode & MODE_CURSOR_BLINKING)
746 tty_putcode(tty, TTYC_CVVIS);
747 break;
748 case SCREEN_CURSOR_UNDERLINE:
749 if (tty_term_has(tty->term, TTYC_SS)) {
750 if (cmode & MODE_CURSOR_BLINKING)
751 tty_putcode1(tty, TTYC_SS, 3);
752 else
753 tty_putcode1(tty, TTYC_SS, 4);
754 } else if (cmode & MODE_CURSOR_BLINKING)
755 tty_putcode(tty, TTYC_CVVIS);
756 break;
757 case SCREEN_CURSOR_BAR:
758 if (tty_term_has(tty->term, TTYC_SS)) {
759 if (cmode & MODE_CURSOR_BLINKING)
760 tty_putcode1(tty, TTYC_SS, 5);
761 else
762 tty_putcode1(tty, TTYC_SS, 6);
763 } else if (cmode & MODE_CURSOR_BLINKING)
764 tty_putcode(tty, TTYC_CVVIS);
765 break;
767 tty->cstyle = cstyle;
768 return (cmode);
771 void
772 tty_update_mode(struct tty *tty, int mode, struct screen *s)
774 struct tty_term *term = tty->term;
775 struct client *c = tty->client;
776 int changed;
778 if (tty->flags & TTY_NOCURSOR)
779 mode &= ~MODE_CURSOR;
781 if (tty_update_cursor(tty, mode, s) & MODE_CURSOR_BLINKING)
782 mode |= MODE_CURSOR_BLINKING;
783 else
784 mode &= ~MODE_CURSOR_BLINKING;
786 changed = mode ^ tty->mode;
787 if (log_get_level() != 0 && changed != 0) {
788 log_debug("%s: current mode %s", c->name,
789 screen_mode_to_string(tty->mode));
790 log_debug("%s: setting mode %s", c->name,
791 screen_mode_to_string(mode));
794 if ((changed & ALL_MOUSE_MODES) && tty_term_has(term, TTYC_KMOUS)) {
796 * If the mouse modes have changed, clear then all and apply
797 * again. There are differences in how terminals track the
798 * various bits.
800 tty_puts(tty, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l");
801 if (mode & ALL_MOUSE_MODES)
802 tty_puts(tty, "\033[?1006h");
803 if (mode & MODE_MOUSE_ALL)
804 tty_puts(tty, "\033[?1000h\033[?1002h\033[?1003h");
805 if (mode & MODE_MOUSE_BUTTON)
806 tty_puts(tty, "\033[?1000h\033[?1002h");
807 else if (mode & MODE_MOUSE_STANDARD)
808 tty_puts(tty, "\033[?1000h");
810 if (changed & MODE_BRACKETPASTE) {
811 if (mode & MODE_BRACKETPASTE)
812 tty_putcode(tty, TTYC_ENBP);
813 else
814 tty_putcode(tty, TTYC_DSBP);
816 tty->mode = mode;
819 static void
820 tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
821 enum tty_code_code code1, u_int n)
823 if (tty_term_has(tty->term, code))
824 tty_putcode1(tty, code, n);
825 else {
826 while (n-- > 0)
827 tty_putcode(tty, code1);
831 static void
832 tty_repeat_space(struct tty *tty, u_int n)
834 static char s[500];
836 if (*s != ' ')
837 memset(s, ' ', sizeof s);
839 while (n > sizeof s) {
840 tty_putn(tty, s, sizeof s, sizeof s);
841 n -= sizeof s;
843 if (n != 0)
844 tty_putn(tty, s, n, n);
847 /* Is this window bigger than the terminal? */
849 tty_window_bigger(struct tty *tty)
851 struct client *c = tty->client;
852 struct window *w = c->session->curw->window;
854 return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
857 /* What offset should this window be drawn at? */
859 tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
861 *ox = tty->oox;
862 *oy = tty->ooy;
863 *sx = tty->osx;
864 *sy = tty->osy;
866 return (tty->oflag);
869 /* What offset should this window be drawn at? */
870 static int
871 tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
873 struct client *c = tty->client;
874 struct window *w = c->session->curw->window;
875 struct window_pane *wp = server_client_get_pane(c);
876 u_int cx, cy, lines;
878 lines = status_line_size(c);
880 if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
881 *ox = 0;
882 *oy = 0;
883 *sx = w->sx;
884 *sy = w->sy;
886 c->pan_window = NULL;
887 return (0);
890 *sx = tty->sx;
891 *sy = tty->sy - lines;
893 if (c->pan_window == w) {
894 if (*sx >= w->sx)
895 c->pan_ox = 0;
896 else if (c->pan_ox + *sx > w->sx)
897 c->pan_ox = w->sx - *sx;
898 *ox = c->pan_ox;
899 if (*sy >= w->sy)
900 c->pan_oy = 0;
901 else if (c->pan_oy + *sy > w->sy)
902 c->pan_oy = w->sy - *sy;
903 *oy = c->pan_oy;
904 return (1);
907 if (~wp->screen->mode & MODE_CURSOR) {
908 *ox = 0;
909 *oy = 0;
910 } else {
911 cx = wp->xoff + wp->screen->cx;
912 cy = wp->yoff + wp->screen->cy;
914 if (cx < *sx)
915 *ox = 0;
916 else if (cx > w->sx - *sx)
917 *ox = w->sx - *sx;
918 else
919 *ox = cx - *sx / 2;
921 if (cy < *sy)
922 *oy = 0;
923 else if (cy > w->sy - *sy)
924 *oy = w->sy - *sy;
925 else
926 *oy = cy - *sy / 2;
929 c->pan_window = NULL;
930 return (1);
933 /* Update stored offsets for a window and redraw if necessary. */
934 void
935 tty_update_window_offset(struct window *w)
937 struct client *c;
939 TAILQ_FOREACH(c, &clients, entry) {
940 if (c->session != NULL &&
941 c->session->curw != NULL &&
942 c->session->curw->window == w)
943 tty_update_client_offset(c);
947 /* Update stored offsets for a client and redraw if necessary. */
948 void
949 tty_update_client_offset(struct client *c)
951 u_int ox, oy, sx, sy;
953 if (~c->flags & CLIENT_TERMINAL)
954 return;
956 c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
957 if (ox == c->tty.oox &&
958 oy == c->tty.ooy &&
959 sx == c->tty.osx &&
960 sy == c->tty.osy)
961 return;
963 log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
964 __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
965 ox, oy, sx, sy);
967 c->tty.oox = ox;
968 c->tty.ooy = oy;
969 c->tty.osx = sx;
970 c->tty.osy = sy;
972 c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
976 * Is the region large enough to be worth redrawing once later rather than
977 * probably several times now? Currently yes if it is more than 50% of the
978 * pane.
980 static int
981 tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
983 return (ctx->orlower - ctx->orupper >= ctx->sy / 2);
987 * Return if BCE is needed but the terminal doesn't have it - it'll need to be
988 * emulated.
990 static int
991 tty_fake_bce(const struct tty *tty, const struct grid_cell *gc, u_int bg)
993 if (tty_term_flag(tty->term, TTYC_BCE))
994 return (0);
995 if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc->bg))
996 return (1);
997 return (0);
1001 * Redraw scroll region using data from screen (already updated). Used when
1002 * CSR not supported, or window is a pane that doesn't take up the full
1003 * width of the terminal.
1005 static void
1006 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1008 struct client *c = tty->client;
1009 u_int i;
1012 * If region is large, schedule a redraw. In most cases this is likely
1013 * to be followed by some more scrolling.
1015 if (tty_large_region(tty, ctx)) {
1016 log_debug("%s: %s large redraw", __func__, c->name);
1017 ctx->redraw_cb(ctx);
1018 return;
1021 for (i = ctx->orupper; i <= ctx->orlower; i++)
1022 tty_draw_pane(tty, ctx, i);
1025 /* Is this position visible in the pane? */
1026 static int
1027 tty_is_visible(__unused struct tty *tty, const struct tty_ctx *ctx, u_int px,
1028 u_int py, u_int nx, u_int ny)
1030 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1032 if (!ctx->bigger)
1033 return (1);
1035 if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx ||
1036 yoff + ny <= ctx->woy || yoff >= ctx->woy + ctx->wsy)
1037 return (0);
1038 return (1);
1041 /* Clamp line position to visible part of pane. */
1042 static int
1043 tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1044 u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
1046 u_int xoff = ctx->rxoff + px;
1048 if (!tty_is_visible(tty, ctx, px, py, nx, 1))
1049 return (0);
1050 *ry = ctx->yoff + py - ctx->woy;
1052 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1053 /* All visible. */
1054 *i = 0;
1055 *x = ctx->xoff + px - ctx->wox;
1056 *rx = nx;
1057 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1058 /* Both left and right not visible. */
1059 *i = ctx->wox;
1060 *x = 0;
1061 *rx = ctx->wsx;
1062 } else if (xoff < ctx->wox) {
1063 /* Left not visible. */
1064 *i = ctx->wox - (ctx->xoff + px);
1065 *x = 0;
1066 *rx = nx - *i;
1067 } else {
1068 /* Right not visible. */
1069 *i = 0;
1070 *x = (ctx->xoff + px) - ctx->wox;
1071 *rx = ctx->wsx - *x;
1073 if (*rx > nx)
1074 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1076 return (1);
1079 /* Clear a line. */
1080 static void
1081 tty_clear_line(struct tty *tty, const struct grid_cell *defaults, u_int py,
1082 u_int px, u_int nx, u_int bg)
1084 struct client *c = tty->client;
1085 struct overlay_ranges r;
1086 u_int i;
1088 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1090 /* Nothing to clear. */
1091 if (nx == 0)
1092 return;
1094 /* If genuine BCE is available, can try escape sequences. */
1095 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1096 /* Off the end of the line, use EL if available. */
1097 if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
1098 tty_cursor(tty, px, py);
1099 tty_putcode(tty, TTYC_EL);
1100 return;
1103 /* At the start of the line. Use EL1. */
1104 if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1105 tty_cursor(tty, px + nx - 1, py);
1106 tty_putcode(tty, TTYC_EL1);
1107 return;
1110 /* Section of line. Use ECH if possible. */
1111 if (tty_term_has(tty->term, TTYC_ECH)) {
1112 tty_cursor(tty, px, py);
1113 tty_putcode1(tty, TTYC_ECH, nx);
1114 return;
1119 * Couldn't use an escape sequence, use spaces. Clear only the visible
1120 * bit if there is an overlay.
1122 tty_check_overlay_range(tty, px, py, nx, &r);
1123 for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
1124 if (r.nx[i] == 0)
1125 continue;
1126 tty_cursor(tty, r.px[i], py);
1127 tty_repeat_space(tty, r.nx[i]);
1131 /* Clear a line, adjusting to visible part of pane. */
1132 static void
1133 tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1134 u_int px, u_int nx, u_int bg)
1136 struct client *c = tty->client;
1137 u_int i, x, rx, ry;
1139 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1141 if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1142 tty_clear_line(tty, &ctx->defaults, ry, x, rx, bg);
1145 /* Clamp area position to visible part of pane. */
1146 static int
1147 tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1148 u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1149 u_int *ry)
1151 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1153 if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1154 return (0);
1156 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1157 /* All visible. */
1158 *i = 0;
1159 *x = ctx->xoff + px - ctx->wox;
1160 *rx = nx;
1161 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1162 /* Both left and right not visible. */
1163 *i = ctx->wox;
1164 *x = 0;
1165 *rx = ctx->wsx;
1166 } else if (xoff < ctx->wox) {
1167 /* Left not visible. */
1168 *i = ctx->wox - (ctx->xoff + px);
1169 *x = 0;
1170 *rx = nx - *i;
1171 } else {
1172 /* Right not visible. */
1173 *i = 0;
1174 *x = (ctx->xoff + px) - ctx->wox;
1175 *rx = ctx->wsx - *x;
1177 if (*rx > nx)
1178 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1180 if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) {
1181 /* All visible. */
1182 *j = 0;
1183 *y = ctx->yoff + py - ctx->woy;
1184 *ry = ny;
1185 } else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) {
1186 /* Both top and bottom not visible. */
1187 *j = ctx->woy;
1188 *y = 0;
1189 *ry = ctx->wsy;
1190 } else if (yoff < ctx->woy) {
1191 /* Top not visible. */
1192 *j = ctx->woy - (ctx->yoff + py);
1193 *y = 0;
1194 *ry = ny - *j;
1195 } else {
1196 /* Bottom not visible. */
1197 *j = 0;
1198 *y = (ctx->yoff + py) - ctx->woy;
1199 *ry = ctx->wsy - *y;
1201 if (*ry > ny)
1202 fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1204 return (1);
1207 /* Clear an area, adjusting to visible part of pane. */
1208 static void
1209 tty_clear_area(struct tty *tty, const struct grid_cell *defaults, u_int py,
1210 u_int ny, u_int px, u_int nx, u_int bg)
1212 struct client *c = tty->client;
1213 u_int yy;
1214 char tmp[64];
1216 log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1218 /* Nothing to clear. */
1219 if (nx == 0 || ny == 0)
1220 return;
1222 /* If genuine BCE is available, can try escape sequences. */
1223 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1224 /* Use ED if clearing off the bottom of the terminal. */
1225 if (px == 0 &&
1226 px + nx >= tty->sx &&
1227 py + ny >= tty->sy &&
1228 tty_term_has(tty->term, TTYC_ED)) {
1229 tty_cursor(tty, 0, py);
1230 tty_putcode(tty, TTYC_ED);
1231 return;
1235 * On VT420 compatible terminals we can use DECFRA if the
1236 * background colour isn't default (because it doesn't work
1237 * after SGR 0).
1239 if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1240 xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1241 py + 1, px + 1, py + ny, px + nx);
1242 tty_puts(tty, tmp);
1243 return;
1246 /* Full lines can be scrolled away to clear them. */
1247 if (px == 0 &&
1248 px + nx >= tty->sx &&
1249 ny > 2 &&
1250 tty_term_has(tty->term, TTYC_CSR) &&
1251 tty_term_has(tty->term, TTYC_INDN)) {
1252 tty_region(tty, py, py + ny - 1);
1253 tty_margin_off(tty);
1254 tty_putcode1(tty, TTYC_INDN, ny);
1255 return;
1259 * If margins are supported, can just scroll the area off to
1260 * clear it.
1262 if (nx > 2 &&
1263 ny > 2 &&
1264 tty_term_has(tty->term, TTYC_CSR) &&
1265 tty_use_margin(tty) &&
1266 tty_term_has(tty->term, TTYC_INDN)) {
1267 tty_region(tty, py, py + ny - 1);
1268 tty_margin(tty, px, px + nx - 1);
1269 tty_putcode1(tty, TTYC_INDN, ny);
1270 return;
1274 /* Couldn't use an escape sequence, loop over the lines. */
1275 for (yy = py; yy < py + ny; yy++)
1276 tty_clear_line(tty, defaults, yy, px, nx, bg);
1279 /* Clear an area in a pane. */
1280 static void
1281 tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1282 u_int ny, u_int px, u_int nx, u_int bg)
1284 u_int i, j, x, y, rx, ry;
1286 if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1287 tty_clear_area(tty, &ctx->defaults, y, ry, x, rx, bg);
1290 static void
1291 tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1293 struct screen *s = ctx->s;
1294 u_int nx = ctx->sx, i, x, rx, ry;
1296 log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1298 if (!ctx->bigger) {
1299 tty_draw_line(tty, s, 0, py, nx, ctx->xoff, ctx->yoff + py,
1300 &ctx->defaults, ctx->palette);
1301 return;
1303 if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry)) {
1304 tty_draw_line(tty, s, i, py, rx, x, ry, &ctx->defaults,
1305 ctx->palette);
1309 static const struct grid_cell *
1310 tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1312 static struct grid_cell new;
1313 int c;
1315 /* Characters less than 0x7f are always fine, no matter what. */
1316 if (gc->data.size == 1 && *gc->data.data < 0x7f)
1317 return (gc);
1319 /* UTF-8 terminal and a UTF-8 character - fine. */
1320 if (tty->client->flags & CLIENT_UTF8)
1321 return (gc);
1322 memcpy(&new, gc, sizeof new);
1324 /* See if this can be mapped to an ACS character. */
1325 c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size);
1326 if (c != -1) {
1327 utf8_set(&new.data, c);
1328 new.attr |= GRID_ATTR_CHARSET;
1329 return (&new);
1332 /* Replace by the right number of underscores. */
1333 new.data.size = gc->data.width;
1334 if (new.data.size > UTF8_SIZE)
1335 new.data.size = UTF8_SIZE;
1336 memset(new.data.data, '_', new.data.size);
1337 return (&new);
1341 * Check if a single character is obstructed by the overlay and return a
1342 * boolean.
1344 static int
1345 tty_check_overlay(struct tty *tty, u_int px, u_int py)
1347 struct overlay_ranges r;
1350 * A unit width range will always return nx[2] == 0 from a check, even
1351 * with multiple overlays, so it's sufficient to check just the first
1352 * two entries.
1354 tty_check_overlay_range(tty, px, py, 1, &r);
1355 if (r.nx[0] + r.nx[1] == 0)
1356 return (0);
1357 return (1);
1360 /* Return parts of the input range which are visible. */
1361 static void
1362 tty_check_overlay_range(struct tty *tty, u_int px, u_int py, u_int nx,
1363 struct overlay_ranges *r)
1365 struct client *c = tty->client;
1367 if (c->overlay_check == NULL) {
1368 r->px[0] = px;
1369 r->nx[0] = nx;
1370 r->px[1] = 0;
1371 r->nx[1] = 0;
1372 r->px[2] = 0;
1373 r->nx[2] = 0;
1374 return;
1377 c->overlay_check(c, c->overlay_data, px, py, nx, r);
1380 void
1381 tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
1382 u_int atx, u_int aty, const struct grid_cell *defaults,
1383 struct colour_palette *palette)
1385 struct grid *gd = s->grid;
1386 struct grid_cell gc, last;
1387 const struct grid_cell *gcp;
1388 struct grid_line *gl;
1389 struct client *c = tty->client;
1390 struct overlay_ranges r;
1391 u_int i, j, ux, sx, width, hidden, eux, nxx;
1392 u_int cellsize;
1393 int flags, cleared = 0, wrapped = 0;
1394 char buf[512];
1395 size_t len;
1397 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1398 px, py, nx, atx, aty);
1399 log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg,
1400 defaults->bg);
1403 * py is the line in the screen to draw.
1404 * px is the start x and nx is the width to draw.
1405 * atx,aty is the line on the terminal to draw it.
1408 flags = (tty->flags & TTY_NOCURSOR);
1409 tty->flags |= TTY_NOCURSOR;
1410 tty_update_mode(tty, tty->mode, s);
1412 tty_region_off(tty);
1413 tty_margin_off(tty);
1416 * Clamp the width to cellsize - note this is not cellused, because
1417 * there may be empty background cells after it (from BCE).
1419 sx = screen_size_x(s);
1420 if (nx > sx)
1421 nx = sx;
1422 cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1423 if (sx > cellsize)
1424 sx = cellsize;
1425 if (sx > tty->sx)
1426 sx = tty->sx;
1427 if (sx > nx)
1428 sx = nx;
1429 ux = 0;
1431 if (py == 0)
1432 gl = NULL;
1433 else
1434 gl = grid_get_line(gd, gd->hsize + py - 1);
1435 if (gl == NULL ||
1436 (~gl->flags & GRID_LINE_WRAPPED) ||
1437 atx != 0 ||
1438 tty->cx < tty->sx ||
1439 nx < tty->sx) {
1440 if (nx < tty->sx &&
1441 atx == 0 &&
1442 px + sx != nx &&
1443 tty_term_has(tty->term, TTYC_EL1) &&
1444 !tty_fake_bce(tty, defaults, 8) &&
1445 c->overlay_check == NULL) {
1446 tty_default_attributes(tty, defaults, palette, 8);
1447 tty_cursor(tty, nx - 1, aty);
1448 tty_putcode(tty, TTYC_EL1);
1449 cleared = 1;
1451 } else {
1452 log_debug("%s: wrapped line %u", __func__, aty);
1453 wrapped = 1;
1456 memcpy(&last, &grid_default_cell, sizeof last);
1457 len = 0;
1458 width = 0;
1460 for (i = 0; i < sx; i++) {
1461 grid_view_get_cell(gd, px + i, py, &gc);
1462 gcp = tty_check_codeset(tty, &gc);
1463 if (len != 0 &&
1464 (!tty_check_overlay(tty, atx + ux + width, aty) ||
1465 (gcp->attr & GRID_ATTR_CHARSET) ||
1466 gcp->flags != last.flags ||
1467 gcp->attr != last.attr ||
1468 gcp->fg != last.fg ||
1469 gcp->bg != last.bg ||
1470 gcp->us != last.us ||
1471 ux + width + gcp->data.width > nx ||
1472 (sizeof buf) - len < gcp->data.size)) {
1473 tty_attributes(tty, &last, defaults, palette);
1474 if (last.flags & GRID_FLAG_CLEARED) {
1475 log_debug("%s: %zu cleared", __func__, len);
1476 tty_clear_line(tty, defaults, aty, atx + ux,
1477 width, last.bg);
1478 } else {
1479 if (!wrapped || atx != 0 || ux != 0)
1480 tty_cursor(tty, atx + ux, aty);
1481 tty_putn(tty, buf, len, width);
1483 ux += width;
1485 len = 0;
1486 width = 0;
1487 wrapped = 0;
1490 if (gcp->flags & GRID_FLAG_SELECTED)
1491 screen_select_cell(s, &last, gcp);
1492 else
1493 memcpy(&last, gcp, sizeof last);
1495 tty_check_overlay_range(tty, atx + ux, aty, gcp->data.width,
1496 &r);
1497 hidden = 0;
1498 for (j = 0; j < OVERLAY_MAX_RANGES; j++)
1499 hidden += r.nx[j];
1500 hidden = gcp->data.width - hidden;
1501 if (hidden != 0 && hidden == gcp->data.width) {
1502 if (~gcp->flags & GRID_FLAG_PADDING)
1503 ux += gcp->data.width;
1504 } else if (hidden != 0 || ux + gcp->data.width > nx) {
1505 if (~gcp->flags & GRID_FLAG_PADDING) {
1506 tty_attributes(tty, &last, defaults, palette);
1507 for (j = 0; j < OVERLAY_MAX_RANGES; j++) {
1508 if (r.nx[j] == 0)
1509 continue;
1510 /* Effective width drawn so far. */
1511 eux = r.px[j] - atx;
1512 if (eux < nx) {
1513 tty_cursor(tty, r.px[j], aty);
1514 nxx = nx - eux;
1515 if (r.nx[j] > nxx)
1516 r.nx[j] = nxx;
1517 tty_repeat_space(tty, r.nx[j]);
1518 ux = eux + r.nx[j];
1522 } else if (gcp->attr & GRID_ATTR_CHARSET) {
1523 tty_attributes(tty, &last, defaults, palette);
1524 tty_cursor(tty, atx + ux, aty);
1525 for (j = 0; j < gcp->data.size; j++)
1526 tty_putc(tty, gcp->data.data[j]);
1527 ux += gcp->data.width;
1528 } else if (~gcp->flags & GRID_FLAG_PADDING) {
1529 memcpy(buf + len, gcp->data.data, gcp->data.size);
1530 len += gcp->data.size;
1531 width += gcp->data.width;
1534 if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1535 tty_attributes(tty, &last, defaults, palette);
1536 if (last.flags & GRID_FLAG_CLEARED) {
1537 log_debug("%s: %zu cleared (end)", __func__, len);
1538 tty_clear_line(tty, defaults, aty, atx + ux, width,
1539 last.bg);
1540 } else {
1541 if (!wrapped || atx != 0 || ux != 0)
1542 tty_cursor(tty, atx + ux, aty);
1543 tty_putn(tty, buf, len, width);
1545 ux += width;
1548 if (!cleared && ux < nx) {
1549 log_debug("%s: %u to end of line (%zu cleared)", __func__,
1550 nx - ux, len);
1551 tty_default_attributes(tty, defaults, palette, 8);
1552 tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1555 tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1556 tty_update_mode(tty, tty->mode, s);
1559 void
1560 tty_sync_start(struct tty *tty)
1562 if (tty->flags & TTY_BLOCK)
1563 return;
1564 if (tty->flags & TTY_SYNCING)
1565 return;
1566 tty->flags |= TTY_SYNCING;
1568 if (tty_term_has(tty->term, TTYC_SYNC)) {
1569 log_debug("%s sync start", tty->client->name);
1570 tty_putcode1(tty, TTYC_SYNC, 1);
1574 void
1575 tty_sync_end(struct tty *tty)
1577 if (tty->flags & TTY_BLOCK)
1578 return;
1579 if (~tty->flags & TTY_SYNCING)
1580 return;
1581 tty->flags &= ~TTY_SYNCING;
1583 if (tty_term_has(tty->term, TTYC_SYNC)) {
1584 log_debug("%s sync end", tty->client->name);
1585 tty_putcode1(tty, TTYC_SYNC, 2);
1589 static int
1590 tty_client_ready(struct client *c)
1592 if (c->session == NULL || c->tty.term == NULL)
1593 return (0);
1594 if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1595 return (0);
1596 if (c->tty.flags & TTY_FREEZE)
1597 return (0);
1598 return (1);
1601 void
1602 tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1603 struct tty_ctx *ctx)
1605 struct client *c;
1606 int state;
1608 if (ctx->set_client_cb == NULL)
1609 return;
1610 TAILQ_FOREACH(c, &clients, entry) {
1611 if (!tty_client_ready(c))
1612 continue;
1613 state = ctx->set_client_cb(ctx, c);
1614 if (state == -1)
1615 break;
1616 if (state == 0)
1617 continue;
1618 cmdfn(&c->tty, ctx);
1622 void
1623 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1625 struct client *c = tty->client;
1627 if (ctx->bigger ||
1628 !tty_full_width(tty, ctx) ||
1629 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1630 (!tty_term_has(tty->term, TTYC_ICH) &&
1631 !tty_term_has(tty->term, TTYC_ICH1)) ||
1632 c->overlay_check != NULL) {
1633 tty_draw_pane(tty, ctx, ctx->ocy);
1634 return;
1637 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1639 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1641 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1644 void
1645 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1647 struct client *c = tty->client;
1649 if (ctx->bigger ||
1650 !tty_full_width(tty, ctx) ||
1651 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1652 (!tty_term_has(tty->term, TTYC_DCH) &&
1653 !tty_term_has(tty->term, TTYC_DCH1)) ||
1654 c->overlay_check != NULL) {
1655 tty_draw_pane(tty, ctx, ctx->ocy);
1656 return;
1659 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1661 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1663 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1666 void
1667 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1669 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1671 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1674 void
1675 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1677 struct client *c = tty->client;
1679 if (ctx->bigger ||
1680 !tty_full_width(tty, ctx) ||
1681 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1682 !tty_term_has(tty->term, TTYC_CSR) ||
1683 !tty_term_has(tty->term, TTYC_IL1) ||
1684 ctx->sx == 1 ||
1685 ctx->sy == 1 ||
1686 c->overlay_check != NULL) {
1687 tty_redraw_region(tty, ctx);
1688 return;
1691 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1693 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1694 tty_margin_off(tty);
1695 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1697 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1698 tty->cx = tty->cy = UINT_MAX;
1701 void
1702 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1704 struct client *c = tty->client;
1706 if (ctx->bigger ||
1707 !tty_full_width(tty, ctx) ||
1708 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1709 !tty_term_has(tty->term, TTYC_CSR) ||
1710 !tty_term_has(tty->term, TTYC_DL1) ||
1711 ctx->sx == 1 ||
1712 ctx->sy == 1 ||
1713 c->overlay_check != NULL) {
1714 tty_redraw_region(tty, ctx);
1715 return;
1718 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1720 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1721 tty_margin_off(tty);
1722 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1724 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1725 tty->cx = tty->cy = UINT_MAX;
1728 void
1729 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1731 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1733 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1736 void
1737 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1739 u_int nx = ctx->sx - ctx->ocx;
1741 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1743 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1746 void
1747 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1749 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1751 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1754 void
1755 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1757 struct client *c = tty->client;
1759 if (ctx->ocy != ctx->orupper)
1760 return;
1762 if (ctx->bigger ||
1763 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1764 tty_fake_bce(tty, &ctx->defaults, 8) ||
1765 !tty_term_has(tty->term, TTYC_CSR) ||
1766 (!tty_term_has(tty->term, TTYC_RI) &&
1767 !tty_term_has(tty->term, TTYC_RIN)) ||
1768 ctx->sx == 1 ||
1769 ctx->sy == 1 ||
1770 c->overlay_check != NULL) {
1771 tty_redraw_region(tty, ctx);
1772 return;
1775 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1777 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1778 tty_margin_pane(tty, ctx);
1779 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1781 if (tty_term_has(tty->term, TTYC_RI))
1782 tty_putcode(tty, TTYC_RI);
1783 else
1784 tty_putcode1(tty, TTYC_RIN, 1);
1787 void
1788 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1790 struct client *c = tty->client;
1792 if (ctx->ocy != ctx->orlower)
1793 return;
1795 if (ctx->bigger ||
1796 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1797 tty_fake_bce(tty, &ctx->defaults, 8) ||
1798 !tty_term_has(tty->term, TTYC_CSR) ||
1799 ctx->sx == 1 ||
1800 ctx->sy == 1 ||
1801 c->overlay_check != NULL) {
1802 tty_redraw_region(tty, ctx);
1803 return;
1806 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1808 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1809 tty_margin_pane(tty, ctx);
1812 * If we want to wrap a pane while using margins, the cursor needs to
1813 * be exactly on the right of the region. If the cursor is entirely off
1814 * the edge - move it back to the right. Some terminals are funny about
1815 * this and insert extra spaces, so only use the right if margins are
1816 * enabled.
1818 if (ctx->xoff + ctx->ocx > tty->rright) {
1819 if (!tty_use_margin(tty))
1820 tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1821 else
1822 tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1823 } else
1824 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1826 tty_putc(tty, '\n');
1829 void
1830 tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1832 struct client *c = tty->client;
1833 u_int i;
1835 if (ctx->bigger ||
1836 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1837 tty_fake_bce(tty, &ctx->defaults, 8) ||
1838 !tty_term_has(tty->term, TTYC_CSR) ||
1839 ctx->sx == 1 ||
1840 ctx->sy == 1 ||
1841 c->overlay_check != NULL) {
1842 tty_redraw_region(tty, ctx);
1843 return;
1846 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1848 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1849 tty_margin_pane(tty, ctx);
1851 if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1852 if (!tty_use_margin(tty))
1853 tty_cursor(tty, 0, tty->rlower);
1854 else
1855 tty_cursor(tty, tty->rright, tty->rlower);
1856 for (i = 0; i < ctx->num; i++)
1857 tty_putc(tty, '\n');
1858 } else {
1859 if (tty->cy == UINT_MAX)
1860 tty_cursor(tty, 0, 0);
1861 else
1862 tty_cursor(tty, 0, tty->cy);
1863 tty_putcode1(tty, TTYC_INDN, ctx->num);
1867 void
1868 tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
1870 u_int i;
1871 struct client *c = tty->client;
1873 if (ctx->bigger ||
1874 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1875 tty_fake_bce(tty, &ctx->defaults, 8) ||
1876 !tty_term_has(tty->term, TTYC_CSR) ||
1877 (!tty_term_has(tty->term, TTYC_RI) &&
1878 !tty_term_has(tty->term, TTYC_RIN)) ||
1879 ctx->sx == 1 ||
1880 ctx->sy == 1 ||
1881 c->overlay_check != NULL) {
1882 tty_redraw_region(tty, ctx);
1883 return;
1886 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1888 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1889 tty_margin_pane(tty, ctx);
1890 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1892 if (tty_term_has(tty->term, TTYC_RIN))
1893 tty_putcode1(tty, TTYC_RIN, ctx->num);
1894 else {
1895 for (i = 0; i < ctx->num; i++)
1896 tty_putcode(tty, TTYC_RI);
1900 void
1901 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1903 u_int px, py, nx, ny;
1905 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1907 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1908 tty_margin_off(tty);
1910 px = 0;
1911 nx = ctx->sx;
1912 py = ctx->ocy + 1;
1913 ny = ctx->sy - ctx->ocy - 1;
1915 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1917 px = ctx->ocx;
1918 nx = ctx->sx - ctx->ocx;
1919 py = ctx->ocy;
1921 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1924 void
1925 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1927 u_int px, py, nx, ny;
1929 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1931 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1932 tty_margin_off(tty);
1934 px = 0;
1935 nx = ctx->sx;
1936 py = 0;
1937 ny = ctx->ocy;
1939 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1941 px = 0;
1942 nx = ctx->ocx + 1;
1943 py = ctx->ocy;
1945 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1948 void
1949 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1951 u_int px, py, nx, ny;
1953 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1955 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1956 tty_margin_off(tty);
1958 px = 0;
1959 nx = ctx->sx;
1960 py = 0;
1961 ny = ctx->sy;
1963 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1966 void
1967 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1969 u_int i, j;
1971 if (ctx->bigger) {
1972 ctx->redraw_cb(ctx);
1973 return;
1976 tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette);
1978 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1979 tty_margin_off(tty);
1981 for (j = 0; j < ctx->sy; j++) {
1982 tty_cursor_pane(tty, ctx, 0, j);
1983 for (i = 0; i < ctx->sx; i++)
1984 tty_putc(tty, 'E');
1988 void
1989 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1991 const struct grid_cell *gcp = ctx->cell;
1992 struct screen *s = ctx->s;
1993 struct overlay_ranges r;
1994 u_int px, py, i, vis = 0;
1996 px = ctx->xoff + ctx->ocx - ctx->wox;
1997 py = ctx->yoff + ctx->ocy - ctx->woy;
1998 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
1999 (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
2000 return;
2002 /* Handle partially obstructed wide characters. */
2003 if (gcp->data.width > 1) {
2004 tty_check_overlay_range(tty, px, py, gcp->data.width, &r);
2005 for (i = 0; i < OVERLAY_MAX_RANGES; i++)
2006 vis += r.nx[i];
2007 if (vis < gcp->data.width) {
2008 tty_draw_line(tty, s, s->cx, s->cy, gcp->data.width,
2009 px, py, &ctx->defaults, ctx->palette);
2010 return;
2014 if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
2015 ctx->ocy == ctx->orlower &&
2016 tty_full_width(tty, ctx))
2017 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
2019 tty_margin_off(tty);
2020 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2022 tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette);
2025 void
2026 tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
2028 struct overlay_ranges r;
2029 u_int i, px, py, cx;
2030 char *cp = ctx->ptr;
2032 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
2033 return;
2035 if (ctx->bigger &&
2036 (ctx->xoff + ctx->ocx < ctx->wox ||
2037 ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
2038 if (!ctx->wrapped ||
2039 !tty_full_width(tty, ctx) ||
2040 (tty->term->flags & TERM_NOAM) ||
2041 ctx->xoff + ctx->ocx != 0 ||
2042 ctx->yoff + ctx->ocy != tty->cy + 1 ||
2043 tty->cx < tty->sx ||
2044 tty->cy == tty->rlower)
2045 tty_draw_pane(tty, ctx, ctx->ocy);
2046 else
2047 ctx->redraw_cb(ctx);
2048 return;
2051 tty_margin_off(tty);
2052 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2053 tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette);
2055 /* Get tty position from pane position for overlay check. */
2056 px = ctx->xoff + ctx->ocx - ctx->wox;
2057 py = ctx->yoff + ctx->ocy - ctx->woy;
2059 tty_check_overlay_range(tty, px, py, ctx->num, &r);
2060 for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
2061 if (r.nx[i] == 0)
2062 continue;
2063 /* Convert back to pane position for printing. */
2064 cx = r.px[i] - ctx->xoff + ctx->wox;
2065 tty_cursor_pane_unless_wrap(tty, ctx, cx, ctx->ocy);
2066 tty_putn(tty, cp + r.px[i] - px, r.nx[i], r.nx[i]);
2070 void
2071 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
2073 tty_set_selection(tty, ctx->ptr, ctx->num);
2076 void
2077 tty_set_selection(struct tty *tty, const char *buf, size_t len)
2079 char *encoded;
2080 size_t size;
2082 if (~tty->flags & TTY_STARTED)
2083 return;
2084 if (!tty_term_has(tty->term, TTYC_MS))
2085 return;
2087 size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
2088 encoded = xmalloc(size);
2090 b64_ntop(buf, len, encoded, size);
2091 tty->flags |= TTY_NOBLOCK;
2092 tty_putcode_ptr2(tty, TTYC_MS, "", encoded);
2094 free(encoded);
2097 void
2098 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
2100 tty->flags |= TTY_NOBLOCK;
2101 tty_add(tty, ctx->ptr, ctx->num);
2102 tty_invalidate(tty);
2105 void
2106 tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
2108 if (ctx->num == 0x11) {
2110 * This is an overlay and a command that moves the cursor so
2111 * start synchronized updates.
2113 tty_sync_start(tty);
2114 } else if (~ctx->num & 0x10) {
2116 * This is a pane. If there is an overlay, always start;
2117 * otherwise, only if requested.
2119 if (ctx->num || tty->client->overlay_draw != NULL)
2120 tty_sync_start(tty);
2124 void
2125 tty_cell(struct tty *tty, const struct grid_cell *gc,
2126 const struct grid_cell *defaults, struct colour_palette *palette)
2128 const struct grid_cell *gcp;
2130 /* Skip last character if terminal is stupid. */
2131 if ((tty->term->flags & TERM_NOAM) &&
2132 tty->cy == tty->sy - 1 &&
2133 tty->cx == tty->sx - 1)
2134 return;
2136 /* If this is a padding character, do nothing. */
2137 if (gc->flags & GRID_FLAG_PADDING)
2138 return;
2140 /* Check the output codeset and apply attributes. */
2141 gcp = tty_check_codeset(tty, gc);
2142 tty_attributes(tty, gcp, defaults, palette);
2144 /* If it is a single character, write with putc to handle ACS. */
2145 if (gcp->data.size == 1) {
2146 tty_attributes(tty, gcp, defaults, palette);
2147 if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
2148 return;
2149 tty_putc(tty, *gcp->data.data);
2150 return;
2153 /* Write the data. */
2154 tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
2157 void
2158 tty_reset(struct tty *tty)
2160 struct grid_cell *gc = &tty->cell;
2162 if (!grid_cells_equal(gc, &grid_default_cell)) {
2163 if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2164 tty_putcode(tty, TTYC_RMACS);
2165 tty_putcode(tty, TTYC_SGR0);
2166 memcpy(gc, &grid_default_cell, sizeof *gc);
2168 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2171 static void
2172 tty_invalidate(struct tty *tty)
2174 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2175 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2177 tty->cx = tty->cy = UINT_MAX;
2178 tty->rupper = tty->rleft = UINT_MAX;
2179 tty->rlower = tty->rright = UINT_MAX;
2181 if (tty->flags & TTY_STARTED) {
2182 if (tty_use_margin(tty))
2183 tty_putcode(tty, TTYC_ENMG);
2184 tty_putcode(tty, TTYC_SGR0);
2186 tty->mode = ALL_MODES;
2187 tty_update_mode(tty, MODE_CURSOR, NULL);
2189 tty_cursor(tty, 0, 0);
2190 tty_region_off(tty);
2191 tty_margin_off(tty);
2192 } else
2193 tty->mode = MODE_CURSOR;
2196 /* Turn off margin. */
2197 void
2198 tty_region_off(struct tty *tty)
2200 tty_region(tty, 0, tty->sy - 1);
2203 /* Set region inside pane. */
2204 static void
2205 tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2206 u_int rlower)
2208 tty_region(tty, ctx->yoff + rupper - ctx->woy,
2209 ctx->yoff + rlower - ctx->woy);
2212 /* Set region at absolute position. */
2213 static void
2214 tty_region(struct tty *tty, u_int rupper, u_int rlower)
2216 if (tty->rlower == rlower && tty->rupper == rupper)
2217 return;
2218 if (!tty_term_has(tty->term, TTYC_CSR))
2219 return;
2221 tty->rupper = rupper;
2222 tty->rlower = rlower;
2225 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2226 * 0,0 if it is beyond the last column (they do not reset their wrap
2227 * flag so further output causes a line feed). As a workaround, do an
2228 * explicit move to 0 first.
2230 if (tty->cx >= tty->sx) {
2231 if (tty->cy == UINT_MAX)
2232 tty_cursor(tty, 0, 0);
2233 else
2234 tty_cursor(tty, 0, tty->cy);
2237 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2238 tty->cx = tty->cy = UINT_MAX;
2241 /* Turn off margin. */
2242 void
2243 tty_margin_off(struct tty *tty)
2245 tty_margin(tty, 0, tty->sx - 1);
2248 /* Set margin inside pane. */
2249 static void
2250 tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2252 tty_margin(tty, ctx->xoff - ctx->wox,
2253 ctx->xoff + ctx->sx - 1 - ctx->wox);
2256 /* Set margin at absolute position. */
2257 static void
2258 tty_margin(struct tty *tty, u_int rleft, u_int rright)
2260 if (!tty_use_margin(tty))
2261 return;
2262 if (tty->rleft == rleft && tty->rright == rright)
2263 return;
2265 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
2267 tty->rleft = rleft;
2268 tty->rright = rright;
2270 if (rleft == 0 && rright == tty->sx - 1)
2271 tty_putcode(tty, TTYC_CLMG);
2272 else
2273 tty_putcode2(tty, TTYC_CMG, rleft, rright);
2274 tty->cx = tty->cy = UINT_MAX;
2278 * Move the cursor, unless it would wrap itself when the next character is
2279 * printed.
2281 static void
2282 tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2283 u_int cx, u_int cy)
2285 if (!ctx->wrapped ||
2286 !tty_full_width(tty, ctx) ||
2287 (tty->term->flags & TERM_NOAM) ||
2288 ctx->xoff + cx != 0 ||
2289 ctx->yoff + cy != tty->cy + 1 ||
2290 tty->cx < tty->sx ||
2291 tty->cy == tty->rlower)
2292 tty_cursor_pane(tty, ctx, cx, cy);
2293 else
2294 log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
2297 /* Move cursor inside pane. */
2298 static void
2299 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2301 tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
2304 /* Move cursor to absolute position. */
2305 void
2306 tty_cursor(struct tty *tty, u_int cx, u_int cy)
2308 struct tty_term *term = tty->term;
2309 u_int thisx, thisy;
2310 int change;
2312 if (tty->flags & TTY_BLOCK)
2313 return;
2315 thisx = tty->cx;
2316 thisy = tty->cy;
2319 * If in the automargin space, and want to be there, do not move.
2320 * Otherwise, force the cursor to be in range (and complain).
2322 if (cx == thisx && cy == thisy && cx == tty->sx)
2323 return;
2324 if (cx > tty->sx - 1) {
2325 log_debug("%s: x too big %u > %u", __func__, cx, tty->sx - 1);
2326 cx = tty->sx - 1;
2329 /* No change. */
2330 if (cx == thisx && cy == thisy)
2331 return;
2333 /* Currently at the very end of the line - use absolute movement. */
2334 if (thisx > tty->sx - 1)
2335 goto absolute;
2337 /* Move to home position (0, 0). */
2338 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2339 tty_putcode(tty, TTYC_HOME);
2340 goto out;
2343 /* Zero on the next line. */
2344 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2345 (!tty_use_margin(tty) || tty->rleft == 0)) {
2346 tty_putc(tty, '\r');
2347 tty_putc(tty, '\n');
2348 goto out;
2351 /* Moving column or row. */
2352 if (cy == thisy) {
2354 * Moving column only, row staying the same.
2357 /* To left edge. */
2358 if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
2359 tty_putc(tty, '\r');
2360 goto out;
2363 /* One to the left. */
2364 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2365 tty_putcode(tty, TTYC_CUB1);
2366 goto out;
2369 /* One to the right. */
2370 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2371 tty_putcode(tty, TTYC_CUF1);
2372 goto out;
2375 /* Calculate difference. */
2376 change = thisx - cx; /* +ve left, -ve right */
2379 * Use HPA if change is larger than absolute, otherwise move
2380 * the cursor with CUB/CUF.
2382 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
2383 tty_putcode1(tty, TTYC_HPA, cx);
2384 goto out;
2385 } else if (change > 0 &&
2386 tty_term_has(term, TTYC_CUB) &&
2387 !tty_use_margin(tty)) {
2388 if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2389 tty_putcode(tty, TTYC_CUB1);
2390 tty_putcode(tty, TTYC_CUB1);
2391 goto out;
2393 tty_putcode1(tty, TTYC_CUB, change);
2394 goto out;
2395 } else if (change < 0 &&
2396 tty_term_has(term, TTYC_CUF) &&
2397 !tty_use_margin(tty)) {
2398 tty_putcode1(tty, TTYC_CUF, -change);
2399 goto out;
2401 } else if (cx == thisx) {
2403 * Moving row only, column staying the same.
2406 /* One above. */
2407 if (thisy != tty->rupper &&
2408 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2409 tty_putcode(tty, TTYC_CUU1);
2410 goto out;
2413 /* One below. */
2414 if (thisy != tty->rlower &&
2415 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2416 tty_putcode(tty, TTYC_CUD1);
2417 goto out;
2420 /* Calculate difference. */
2421 change = thisy - cy; /* +ve up, -ve down */
2424 * Try to use VPA if change is larger than absolute or if this
2425 * change would cross the scroll region, otherwise use CUU/CUD.
2427 if ((u_int) abs(change) > cy ||
2428 (change < 0 && cy - change > tty->rlower) ||
2429 (change > 0 && cy - change < tty->rupper)) {
2430 if (tty_term_has(term, TTYC_VPA)) {
2431 tty_putcode1(tty, TTYC_VPA, cy);
2432 goto out;
2434 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2435 tty_putcode1(tty, TTYC_CUU, change);
2436 goto out;
2437 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2438 tty_putcode1(tty, TTYC_CUD, -change);
2439 goto out;
2443 absolute:
2444 /* Absolute movement. */
2445 tty_putcode2(tty, TTYC_CUP, cy, cx);
2447 out:
2448 tty->cx = cx;
2449 tty->cy = cy;
2452 void
2453 tty_attributes(struct tty *tty, const struct grid_cell *gc,
2454 const struct grid_cell *defaults, struct colour_palette *palette)
2456 struct grid_cell *tc = &tty->cell, gc2;
2457 int changed;
2459 /* Copy cell and update default colours. */
2460 memcpy(&gc2, gc, sizeof gc2);
2461 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2462 if (gc2.fg == 8)
2463 gc2.fg = defaults->fg;
2464 if (gc2.bg == 8)
2465 gc2.bg = defaults->bg;
2468 /* Ignore cell if it is the same as the last one. */
2469 if (gc2.attr == tty->last_cell.attr &&
2470 gc2.fg == tty->last_cell.fg &&
2471 gc2.bg == tty->last_cell.bg &&
2472 gc2.us == tty->last_cell.us)
2473 return;
2476 * If no setab, try to use the reverse attribute as a best-effort for a
2477 * non-default background. This is a bit of a hack but it doesn't do
2478 * any serious harm and makes a couple of applications happier.
2480 if (!tty_term_has(tty->term, TTYC_SETAB)) {
2481 if (gc2.attr & GRID_ATTR_REVERSE) {
2482 if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
2483 gc2.attr &= ~GRID_ATTR_REVERSE;
2484 } else {
2485 if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
2486 gc2.attr |= GRID_ATTR_REVERSE;
2490 /* Fix up the colours if necessary. */
2491 tty_check_fg(tty, palette, &gc2);
2492 tty_check_bg(tty, palette, &gc2);
2493 tty_check_us(tty, palette, &gc2);
2496 * If any bits are being cleared or the underline colour is now default,
2497 * reset everything.
2499 if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
2500 tty_reset(tty);
2503 * Set the colours. This may call tty_reset() (so it comes next) and
2504 * may add to (NOT remove) the desired attributes.
2506 tty_colours(tty, &gc2);
2508 /* Filter out attribute bits already set. */
2509 changed = gc2.attr & ~tc->attr;
2510 tc->attr = gc2.attr;
2512 /* Set the attributes. */
2513 if (changed & GRID_ATTR_BRIGHT)
2514 tty_putcode(tty, TTYC_BOLD);
2515 if (changed & GRID_ATTR_DIM)
2516 tty_putcode(tty, TTYC_DIM);
2517 if (changed & GRID_ATTR_ITALICS)
2518 tty_set_italics(tty);
2519 if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2520 if ((changed & GRID_ATTR_UNDERSCORE) ||
2521 !tty_term_has(tty->term, TTYC_SMULX))
2522 tty_putcode(tty, TTYC_SMUL);
2523 else if (changed & GRID_ATTR_UNDERSCORE_2)
2524 tty_putcode1(tty, TTYC_SMULX, 2);
2525 else if (changed & GRID_ATTR_UNDERSCORE_3)
2526 tty_putcode1(tty, TTYC_SMULX, 3);
2527 else if (changed & GRID_ATTR_UNDERSCORE_4)
2528 tty_putcode1(tty, TTYC_SMULX, 4);
2529 else if (changed & GRID_ATTR_UNDERSCORE_5)
2530 tty_putcode1(tty, TTYC_SMULX, 5);
2532 if (changed & GRID_ATTR_BLINK)
2533 tty_putcode(tty, TTYC_BLINK);
2534 if (changed & GRID_ATTR_REVERSE) {
2535 if (tty_term_has(tty->term, TTYC_REV))
2536 tty_putcode(tty, TTYC_REV);
2537 else if (tty_term_has(tty->term, TTYC_SMSO))
2538 tty_putcode(tty, TTYC_SMSO);
2540 if (changed & GRID_ATTR_HIDDEN)
2541 tty_putcode(tty, TTYC_INVIS);
2542 if (changed & GRID_ATTR_STRIKETHROUGH)
2543 tty_putcode(tty, TTYC_SMXX);
2544 if (changed & GRID_ATTR_OVERLINE)
2545 tty_putcode(tty, TTYC_SMOL);
2546 if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2547 tty_putcode(tty, TTYC_SMACS);
2549 memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
2552 static void
2553 tty_colours(struct tty *tty, const struct grid_cell *gc)
2555 struct grid_cell *tc = &tty->cell;
2556 int have_ax;
2558 /* No changes? Nothing is necessary. */
2559 if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
2560 return;
2563 * Is either the default colour? This is handled specially because the
2564 * best solution might be to reset both colours to default, in which
2565 * case if only one is default need to fall onward to set the other
2566 * colour.
2568 if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
2570 * If don't have AX but do have op, send sgr0 (op can't
2571 * actually be used because it is sometimes the same as sgr0
2572 * and sometimes isn't). This resets both colours to default.
2574 * Otherwise, try to set the default colour only as needed.
2576 have_ax = tty_term_flag(tty->term, TTYC_AX);
2577 if (!have_ax && tty_term_has(tty->term, TTYC_OP))
2578 tty_reset(tty);
2579 else {
2580 if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
2581 if (have_ax)
2582 tty_puts(tty, "\033[39m");
2583 else if (tc->fg != 7)
2584 tty_putcode1(tty, TTYC_SETAF, 7);
2585 tc->fg = gc->fg;
2587 if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
2588 if (have_ax)
2589 tty_puts(tty, "\033[49m");
2590 else if (tc->bg != 0)
2591 tty_putcode1(tty, TTYC_SETAB, 0);
2592 tc->bg = gc->bg;
2597 /* Set the foreground colour. */
2598 if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
2599 tty_colours_fg(tty, gc);
2602 * Set the background colour. This must come after the foreground as
2603 * tty_colour_fg() can call tty_reset().
2605 if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
2606 tty_colours_bg(tty, gc);
2608 /* Set the underscore colour. */
2609 if (gc->us != tc->us)
2610 tty_colours_us(tty, gc);
2613 static void
2614 tty_check_fg(struct tty *tty, struct colour_palette *palette,
2615 struct grid_cell *gc)
2617 u_char r, g, b;
2618 u_int colours;
2619 int c;
2622 * Perform substitution if this pane has a palette. If the bright
2623 * attribute is set, use the bright entry in the palette by changing to
2624 * the aixterm colour.
2626 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2627 c = gc->fg;
2628 if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
2629 c += 90;
2630 if ((c = colour_palette_get(palette, c)) != -1)
2631 gc->fg = c;
2634 /* Is this a 24-bit colour? */
2635 if (gc->fg & COLOUR_FLAG_RGB) {
2636 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2637 if (tty->term->flags & TERM_RGBCOLOURS)
2638 return;
2639 colour_split_rgb(gc->fg, &r, &g, &b);
2640 gc->fg = colour_find_rgb(r, g, b);
2643 /* How many colours does this terminal have? */
2644 if (tty->term->flags & TERM_256COLOURS)
2645 colours = 256;
2646 else
2647 colours = tty_term_number(tty->term, TTYC_COLORS);
2649 /* Is this a 256-colour colour? */
2650 if (gc->fg & COLOUR_FLAG_256) {
2651 /* And not a 256 colour mode? */
2652 if (colours < 256) {
2653 gc->fg = colour_256to16(gc->fg);
2654 if (gc->fg & 8) {
2655 gc->fg &= 7;
2656 if (colours >= 16)
2657 gc->fg += 90;
2660 return;
2663 /* Is this an aixterm colour? */
2664 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2665 gc->fg -= 90;
2666 gc->attr |= GRID_ATTR_BRIGHT;
2670 static void
2671 tty_check_bg(struct tty *tty, struct colour_palette *palette,
2672 struct grid_cell *gc)
2674 u_char r, g, b;
2675 u_int colours;
2676 int c;
2678 /* Perform substitution if this pane has a palette. */
2679 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2680 if ((c = colour_palette_get(palette, gc->bg)) != -1)
2681 gc->bg = c;
2684 /* Is this a 24-bit colour? */
2685 if (gc->bg & COLOUR_FLAG_RGB) {
2686 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2687 if (tty->term->flags & TERM_RGBCOLOURS)
2688 return;
2689 colour_split_rgb(gc->bg, &r, &g, &b);
2690 gc->bg = colour_find_rgb(r, g, b);
2693 /* How many colours does this terminal have? */
2694 if (tty->term->flags & TERM_256COLOURS)
2695 colours = 256;
2696 else
2697 colours = tty_term_number(tty->term, TTYC_COLORS);
2699 /* Is this a 256-colour colour? */
2700 if (gc->bg & COLOUR_FLAG_256) {
2702 * And not a 256 colour mode? Translate to 16-colour
2703 * palette. Bold background doesn't exist portably, so just
2704 * discard the bold bit if set.
2706 if (colours < 256) {
2707 gc->bg = colour_256to16(gc->bg);
2708 if (gc->bg & 8) {
2709 gc->bg &= 7;
2710 if (colours >= 16)
2711 gc->bg += 90;
2714 return;
2717 /* Is this an aixterm colour? */
2718 if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
2719 gc->bg -= 90;
2722 static void
2723 tty_check_us(__unused struct tty *tty, struct colour_palette *palette,
2724 struct grid_cell *gc)
2726 int c;
2728 /* Perform substitution if this pane has a palette. */
2729 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2730 if ((c = colour_palette_get(palette, gc->us)) != -1)
2731 gc->us = c;
2734 /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
2735 if (gc->us & COLOUR_FLAG_256)
2736 gc->us = colour_256toRGB (gc->us);
2739 static void
2740 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
2742 struct grid_cell *tc = &tty->cell;
2743 char s[32];
2745 /* Is this a 24-bit or 256-colour colour? */
2746 if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
2747 if (tty_try_colour(tty, gc->fg, "38") == 0)
2748 goto save;
2749 /* Should not get here, already converted in tty_check_fg. */
2750 return;
2753 /* Is this an aixterm bright colour? */
2754 if (gc->fg >= 90 && gc->fg <= 97) {
2755 if (tty->term->flags & TERM_256COLOURS) {
2756 xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2757 tty_puts(tty, s);
2758 } else
2759 tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
2760 goto save;
2763 /* Otherwise set the foreground colour. */
2764 tty_putcode1(tty, TTYC_SETAF, gc->fg);
2766 save:
2767 /* Save the new values in the terminal current cell. */
2768 tc->fg = gc->fg;
2771 static void
2772 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
2774 struct grid_cell *tc = &tty->cell;
2775 char s[32];
2777 /* Is this a 24-bit or 256-colour colour? */
2778 if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
2779 if (tty_try_colour(tty, gc->bg, "48") == 0)
2780 goto save;
2781 /* Should not get here, already converted in tty_check_bg. */
2782 return;
2785 /* Is this an aixterm bright colour? */
2786 if (gc->bg >= 90 && gc->bg <= 97) {
2787 if (tty->term->flags & TERM_256COLOURS) {
2788 xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
2789 tty_puts(tty, s);
2790 } else
2791 tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
2792 goto save;
2795 /* Otherwise set the background colour. */
2796 tty_putcode1(tty, TTYC_SETAB, gc->bg);
2798 save:
2799 /* Save the new values in the terminal current cell. */
2800 tc->bg = gc->bg;
2803 static void
2804 tty_colours_us(struct tty *tty, const struct grid_cell *gc)
2806 struct grid_cell *tc = &tty->cell;
2807 u_int c;
2808 u_char r, g, b;
2810 /* Clear underline colour. */
2811 if (gc->us == 0) {
2812 tty_putcode(tty, TTYC_OL);
2813 goto save;
2816 /* Must be an RGB colour - this should never happen. */
2817 if (~gc->us & COLOUR_FLAG_RGB)
2818 return;
2821 * Setulc and setal follows the ncurses(3) one argument "direct colour"
2822 * capability format. Calculate the colour value.
2824 colour_split_rgb(gc->us, &r, &g, &b);
2825 c = (65536 * r) + (256 * g) + b;
2828 * Write the colour. Only use setal if the RGB flag is set because the
2829 * non-RGB version may be wrong.
2831 if (tty_term_has(tty->term, TTYC_SETULC))
2832 tty_putcode1(tty, TTYC_SETULC, c);
2833 else if (tty_term_has(tty->term, TTYC_SETAL) &&
2834 tty_term_has(tty->term, TTYC_RGB))
2835 tty_putcode1(tty, TTYC_SETAL, c);
2837 save:
2838 /* Save the new values in the terminal current cell. */
2839 tc->us = gc->us;
2842 static int
2843 tty_try_colour(struct tty *tty, int colour, const char *type)
2845 u_char r, g, b;
2847 if (colour & COLOUR_FLAG_256) {
2848 if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
2849 tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
2850 else if (tty_term_has(tty->term, TTYC_SETAB))
2851 tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
2852 return (0);
2855 if (colour & COLOUR_FLAG_RGB) {
2856 colour_split_rgb(colour & 0xffffff, &r, &g, &b);
2857 if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
2858 tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
2859 else if (tty_term_has(tty->term, TTYC_SETRGBB))
2860 tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
2861 return (0);
2864 return (-1);
2867 static void
2868 tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
2870 memcpy(gc, &grid_default_cell, sizeof *gc);
2871 gc->fg = wp->palette.fg;
2872 gc->bg = wp->palette.bg;
2875 void
2876 tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
2878 struct options *oo = wp->options;
2879 struct format_tree *ft;
2881 memcpy(gc, &grid_default_cell, sizeof *gc);
2883 if (wp->flags & PANE_STYLECHANGED) {
2884 log_debug("%%%u: style changed", wp->id);
2885 wp->flags &= ~PANE_STYLECHANGED;
2887 ft = format_create(NULL, NULL, FORMAT_PANE|wp->id,
2888 FORMAT_NOJOBS);
2889 format_defaults(ft, NULL, NULL, NULL, wp);
2890 tty_window_default_style(&wp->cached_active_gc, wp);
2891 style_add(&wp->cached_active_gc, oo, "window-active-style", ft);
2892 tty_window_default_style(&wp->cached_gc, wp);
2893 style_add(&wp->cached_gc, oo, "window-style", ft);
2894 format_free(ft);
2897 if (gc->fg == 8) {
2898 if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
2899 gc->fg = wp->cached_active_gc.fg;
2900 else
2901 gc->fg = wp->cached_gc.fg;
2904 if (gc->bg == 8) {
2905 if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
2906 gc->bg = wp->cached_active_gc.bg;
2907 else
2908 gc->bg = wp->cached_gc.bg;
2912 static void
2913 tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
2914 struct colour_palette *palette, u_int bg)
2916 struct grid_cell gc;
2918 memcpy(&gc, &grid_default_cell, sizeof gc);
2919 gc.bg = bg;
2920 tty_attributes(tty, &gc, defaults, palette);
2923 static void
2924 tty_clipboard_query_callback(__unused int fd, __unused short events, void *data)
2926 struct tty *tty = data;
2927 struct client *c = tty->client;
2929 c->flags &= ~CLIENT_CLIPBOARDBUFFER;
2930 free(c->clipboard_panes);
2931 c->clipboard_panes = NULL;
2932 c->clipboard_npanes = 0;
2934 tty->flags &= ~TTY_OSC52QUERY;
2937 void
2938 tty_clipboard_query(struct tty *tty)
2940 struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT };
2942 if ((~tty->flags & TTY_STARTED) || (tty->flags & TTY_OSC52QUERY))
2943 return;
2944 tty_putcode_ptr2(tty, TTYC_MS, "", "?");
2946 tty->flags |= TTY_OSC52QUERY;
2947 evtimer_set(&tty->clipboard_timer, tty_clipboard_query_callback, tty);
2948 evtimer_add(&tty->clipboard_timer, &tv);