Merge branch 'obsd-master'
[tmux.git] / tty.c
blob5b980cc901c22e7b422e51e1b149dca2b0df0dfe
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 <time.h>
32 #include <unistd.h>
34 #include "tmux.h"
36 static int tty_log_fd = -1;
38 static void tty_set_italics(struct tty *);
39 static int tty_try_colour(struct tty *, int, const char *);
40 static void tty_force_cursor_colour(struct tty *, int);
41 static void tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
42 u_int);
43 static void tty_cursor_pane_unless_wrap(struct tty *,
44 const struct tty_ctx *, u_int, u_int);
45 static void tty_colours(struct tty *, const struct grid_cell *);
46 static void tty_check_fg(struct tty *, struct colour_palette *,
47 struct grid_cell *);
48 static void tty_check_bg(struct tty *, struct colour_palette *,
49 struct grid_cell *);
50 static void tty_check_us(struct tty *, struct colour_palette *,
51 struct grid_cell *);
52 static void tty_colours_fg(struct tty *, const struct grid_cell *);
53 static void tty_colours_bg(struct tty *, const struct grid_cell *);
54 static void tty_colours_us(struct tty *, const struct grid_cell *);
56 static void tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
57 u_int);
58 static void tty_region(struct tty *, u_int, u_int);
59 static void tty_margin_pane(struct tty *, const struct tty_ctx *);
60 static void tty_margin(struct tty *, u_int, u_int);
61 static int tty_large_region(struct tty *, const struct tty_ctx *);
62 static int tty_fake_bce(const struct tty *, const struct grid_cell *,
63 u_int);
64 static void tty_redraw_region(struct tty *, const struct tty_ctx *);
65 static void tty_emulate_repeat(struct tty *, enum tty_code_code,
66 enum tty_code_code, u_int);
67 static void tty_repeat_space(struct tty *, u_int);
68 static void tty_draw_pane(struct tty *, const struct tty_ctx *, u_int);
69 static void tty_default_attributes(struct tty *, const struct grid_cell *,
70 struct colour_palette *, u_int, struct hyperlinks *);
71 static int tty_check_overlay(struct tty *, u_int, u_int);
72 static void tty_check_overlay_range(struct tty *, u_int, u_int, u_int,
73 struct overlay_ranges *);
75 #ifdef ENABLE_SIXEL
76 static void tty_write_one(void (*)(struct tty *, const struct tty_ctx *),
77 struct client *, struct tty_ctx *);
78 #endif
80 #define tty_use_margin(tty) \
81 (tty->term->flags & TERM_DECSLRM)
82 #define tty_full_width(tty, ctx) \
83 ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
85 #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
86 #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
87 #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
89 #define TTY_QUERY_TIMEOUT 5
90 #define TTY_REQUEST_LIMIT 30
92 void
93 tty_create_log(void)
95 char name[64];
97 xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
99 tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
100 if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
101 fatal("fcntl failed");
105 tty_init(struct tty *tty, struct client *c)
107 if (!isatty(c->fd))
108 return (-1);
110 memset(tty, 0, sizeof *tty);
111 tty->client = c;
113 tty->cstyle = SCREEN_CURSOR_DEFAULT;
114 tty->ccolour = -1;
115 tty->fg = tty->bg = -1;
117 if (tcgetattr(c->fd, &tty->tio) != 0)
118 return (-1);
119 return (0);
122 void
123 tty_resize(struct tty *tty)
125 struct client *c = tty->client;
126 struct winsize ws;
127 u_int sx, sy, xpixel, ypixel;
129 if (ioctl(c->fd, TIOCGWINSZ, &ws) != -1) {
130 sx = ws.ws_col;
131 if (sx == 0) {
132 sx = 80;
133 xpixel = 0;
134 } else
135 xpixel = ws.ws_xpixel / sx;
136 sy = ws.ws_row;
137 if (sy == 0) {
138 sy = 24;
139 ypixel = 0;
140 } else
141 ypixel = ws.ws_ypixel / sy;
143 if ((xpixel == 0 || ypixel == 0) &&
144 tty->out != NULL &&
145 !(tty->flags & TTY_WINSIZEQUERY) &&
146 (tty->term->flags & TERM_VT100LIKE)) {
147 tty_puts(tty, "\033[18t\033[14t");
148 tty->flags |= TTY_WINSIZEQUERY;
150 } else {
151 sx = 80;
152 sy = 24;
153 xpixel = 0;
154 ypixel = 0;
156 log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy,
157 xpixel, ypixel);
158 tty_set_size(tty, sx, sy, xpixel, ypixel);
159 tty_invalidate(tty);
162 void
163 tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
165 tty->sx = sx;
166 tty->sy = sy;
167 tty->xpixel = xpixel;
168 tty->ypixel = ypixel;
171 static void
172 tty_read_callback(__unused int fd, __unused short events, void *data)
174 struct tty *tty = data;
175 struct client *c = tty->client;
176 const char *name = c->name;
177 size_t size = EVBUFFER_LENGTH(tty->in);
178 int nread;
180 nread = evbuffer_read(tty->in, c->fd, -1);
181 if (nread == 0 || nread == -1) {
182 if (nread == 0)
183 log_debug("%s: read closed", name);
184 else
185 log_debug("%s: read error: %s", name, strerror(errno));
186 event_del(&tty->event_in);
187 server_client_lost(tty->client);
188 return;
190 log_debug("%s: read %d bytes (already %zu)", name, nread, size);
192 while (tty_keys_next(tty))
196 static void
197 tty_timer_callback(__unused int fd, __unused short events, void *data)
199 struct tty *tty = data;
200 struct client *c = tty->client;
201 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
203 log_debug("%s: %zu discarded", c->name, tty->discarded);
205 c->flags |= CLIENT_ALLREDRAWFLAGS;
206 c->discarded += tty->discarded;
208 if (tty->discarded < TTY_BLOCK_STOP(tty)) {
209 tty->flags &= ~TTY_BLOCK;
210 tty_invalidate(tty);
211 return;
213 tty->discarded = 0;
214 evtimer_add(&tty->timer, &tv);
217 static int
218 tty_block_maybe(struct tty *tty)
220 struct client *c = tty->client;
221 size_t size = EVBUFFER_LENGTH(tty->out);
222 struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL };
224 if (size == 0)
225 tty->flags &= ~TTY_NOBLOCK;
226 else if (tty->flags & TTY_NOBLOCK)
227 return (0);
229 if (size < TTY_BLOCK_START(tty))
230 return (0);
232 if (tty->flags & TTY_BLOCK)
233 return (1);
234 tty->flags |= TTY_BLOCK;
236 log_debug("%s: can't keep up, %zu discarded", c->name, size);
238 evbuffer_drain(tty->out, size);
239 c->discarded += size;
241 tty->discarded = 0;
242 evtimer_add(&tty->timer, &tv);
243 return (1);
246 static void
247 tty_write_callback(__unused int fd, __unused short events, void *data)
249 struct tty *tty = data;
250 struct client *c = tty->client;
251 size_t size = EVBUFFER_LENGTH(tty->out);
252 int nwrite;
254 nwrite = evbuffer_write(tty->out, c->fd);
255 if (nwrite == -1)
256 return;
257 log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
259 if (c->redraw > 0) {
260 if ((size_t)nwrite >= c->redraw)
261 c->redraw = 0;
262 else
263 c->redraw -= nwrite;
264 log_debug("%s: waiting for redraw, %zu bytes left", c->name,
265 c->redraw);
266 } else if (tty_block_maybe(tty))
267 return;
269 if (EVBUFFER_LENGTH(tty->out) != 0)
270 event_add(&tty->event_out, NULL);
274 tty_open(struct tty *tty, char **cause)
276 struct client *c = tty->client;
278 tty->term = tty_term_create(tty, c->term_name, c->term_caps,
279 c->term_ncaps, &c->term_features, cause);
280 if (tty->term == NULL) {
281 tty_close(tty);
282 return (-1);
284 tty->flags |= TTY_OPENED;
286 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
288 event_set(&tty->event_in, c->fd, EV_PERSIST|EV_READ,
289 tty_read_callback, tty);
290 tty->in = evbuffer_new();
291 if (tty->in == NULL)
292 fatal("out of memory");
294 event_set(&tty->event_out, c->fd, EV_WRITE, tty_write_callback, tty);
295 tty->out = evbuffer_new();
296 if (tty->out == NULL)
297 fatal("out of memory");
299 evtimer_set(&tty->timer, tty_timer_callback, tty);
301 tty_start_tty(tty);
302 tty_keys_build(tty);
304 return (0);
307 static void
308 tty_start_timer_callback(__unused int fd, __unused short events, void *data)
310 struct tty *tty = data;
311 struct client *c = tty->client;
313 log_debug("%s: start timer fired", c->name);
314 if ((tty->flags & (TTY_HAVEDA|TTY_HAVEDA2|TTY_HAVEXDA)) == 0)
315 tty_update_features(tty);
316 tty->flags |= TTY_ALL_REQUEST_FLAGS;
319 void
320 tty_start_tty(struct tty *tty)
322 struct client *c = tty->client;
323 struct termios tio;
324 struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT };
326 setblocking(c->fd, 0);
327 event_add(&tty->event_in, NULL);
329 memcpy(&tio, &tty->tio, sizeof tio);
330 tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
331 tio.c_iflag |= IGNBRK;
332 tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
333 tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|ECHOPRT|
334 ECHOKE|ISIG);
335 tio.c_cc[VMIN] = 1;
336 tio.c_cc[VTIME] = 0;
337 if (tcsetattr(c->fd, TCSANOW, &tio) == 0)
338 tcflush(c->fd, TCOFLUSH);
340 tty_putcode(tty, TTYC_SMCUP);
342 tty_putcode(tty, TTYC_SMKX);
343 tty_putcode(tty, TTYC_CLEAR);
345 if (tty_acs_needed(tty)) {
346 log_debug("%s: using capabilities for ACS", c->name);
347 tty_putcode(tty, TTYC_ENACS);
348 } else
349 log_debug("%s: using UTF-8 for ACS", c->name);
351 tty_putcode(tty, TTYC_CNORM);
352 if (tty_term_has(tty->term, TTYC_KMOUS)) {
353 tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
354 tty_puts(tty, "\033[?1006l\033[?1005l");
356 if (tty_term_has(tty->term, TTYC_ENBP))
357 tty_putcode(tty, TTYC_ENBP);
359 evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
360 evtimer_add(&tty->start_timer, &tv);
362 tty->flags |= TTY_STARTED;
363 tty_invalidate(tty);
365 if (tty->ccolour != -1)
366 tty_force_cursor_colour(tty, -1);
368 tty->mouse_drag_flag = 0;
369 tty->mouse_drag_update = NULL;
370 tty->mouse_drag_release = NULL;
373 void
374 tty_send_requests(struct tty *tty)
376 if (~tty->flags & TTY_STARTED)
377 return;
379 if (tty->term->flags & TERM_VT100LIKE) {
380 if (~tty->term->flags & TTY_HAVEDA)
381 tty_puts(tty, "\033[c");
382 if (~tty->flags & TTY_HAVEDA2)
383 tty_puts(tty, "\033[>c");
384 if (~tty->flags & TTY_HAVEXDA)
385 tty_puts(tty, "\033[>q");
386 tty_puts(tty, "\033]10;?\033\\");
387 tty_puts(tty, "\033]11;?\033\\");
388 } else
389 tty->flags |= TTY_ALL_REQUEST_FLAGS;
390 tty->last_requests = time(NULL);
393 void
394 tty_repeat_requests(struct tty *tty)
396 time_t t = time(NULL);
398 if (~tty->flags & TTY_STARTED)
399 return;
401 if (t - tty->last_requests <= TTY_REQUEST_LIMIT)
402 return;
403 tty->last_requests = t;
405 if (tty->term->flags & TERM_VT100LIKE) {
406 tty_puts(tty, "\033]10;?\033\\");
407 tty_puts(tty, "\033]11;?\033\\");
411 void
412 tty_stop_tty(struct tty *tty)
414 struct client *c = tty->client;
415 struct winsize ws;
417 if (!(tty->flags & TTY_STARTED))
418 return;
419 tty->flags &= ~TTY_STARTED;
421 evtimer_del(&tty->start_timer);
423 event_del(&tty->timer);
424 tty->flags &= ~TTY_BLOCK;
426 event_del(&tty->event_in);
427 event_del(&tty->event_out);
430 * Be flexible about error handling and try not kill the server just
431 * because the fd is invalid. Things like ssh -t can easily leave us
432 * with a dead tty.
434 if (ioctl(c->fd, TIOCGWINSZ, &ws) == -1)
435 return;
436 if (tcsetattr(c->fd, TCSANOW, &tty->tio) == -1)
437 return;
439 tty_raw(tty, tty_term_string_ii(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
440 if (tty_acs_needed(tty))
441 tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
442 tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
443 tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
444 tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
445 if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
446 if (tty_term_has(tty->term, TTYC_SE))
447 tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
448 else if (tty_term_has(tty->term, TTYC_SS))
449 tty_raw(tty, tty_term_string_i(tty->term, TTYC_SS, 0));
451 if (tty->ccolour != -1)
452 tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
454 tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
455 if (tty_term_has(tty->term, TTYC_KMOUS)) {
456 tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
457 tty_raw(tty, "\033[?1006l\033[?1005l");
459 if (tty_term_has(tty->term, TTYC_DSBP))
460 tty_raw(tty, tty_term_string(tty->term, TTYC_DSBP));
462 if (tty->term->flags & TERM_VT100LIKE)
463 tty_raw(tty, "\033[?7727l");
464 tty_raw(tty, tty_term_string(tty->term, TTYC_DSFCS));
465 tty_raw(tty, tty_term_string(tty->term, TTYC_DSEKS));
467 if (tty_use_margin(tty))
468 tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG));
469 tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
471 setblocking(c->fd, 1);
474 void
475 tty_close(struct tty *tty)
477 if (event_initialized(&tty->key_timer))
478 evtimer_del(&tty->key_timer);
479 tty_stop_tty(tty);
481 if (tty->flags & TTY_OPENED) {
482 evbuffer_free(tty->in);
483 event_del(&tty->event_in);
484 evbuffer_free(tty->out);
485 event_del(&tty->event_out);
487 tty_term_free(tty->term);
488 tty_keys_free(tty);
490 tty->flags &= ~TTY_OPENED;
494 void
495 tty_free(struct tty *tty)
497 tty_close(tty);
500 void
501 tty_update_features(struct tty *tty)
503 struct client *c = tty->client;
505 if (tty_apply_features(tty->term, c->term_features))
506 tty_term_apply_overrides(tty->term);
508 if (tty_use_margin(tty))
509 tty_putcode(tty, TTYC_ENMG);
510 if (options_get_number(global_options, "extended-keys"))
511 tty_puts(tty, tty_term_string(tty->term, TTYC_ENEKS));
512 if (options_get_number(global_options, "focus-events"))
513 tty_puts(tty, tty_term_string(tty->term, TTYC_ENFCS));
514 if (tty->term->flags & TERM_VT100LIKE)
515 tty_puts(tty, "\033[?7727h");
518 * Features might have changed since the first draw during attach. For
519 * example, this happens when DA responses are received.
521 server_redraw_client(c);
523 tty_invalidate(tty);
526 void
527 tty_raw(struct tty *tty, const char *s)
529 struct client *c = tty->client;
530 ssize_t n, slen;
531 u_int i;
533 slen = strlen(s);
534 for (i = 0; i < 5; i++) {
535 n = write(c->fd, s, slen);
536 if (n >= 0) {
537 s += n;
538 slen -= n;
539 if (slen == 0)
540 break;
541 } else if (n == -1 && errno != EAGAIN)
542 break;
543 usleep(100);
547 void
548 tty_putcode(struct tty *tty, enum tty_code_code code)
550 tty_puts(tty, tty_term_string(tty->term, code));
553 void
554 tty_putcode_i(struct tty *tty, enum tty_code_code code, int a)
556 if (a < 0)
557 return;
558 tty_puts(tty, tty_term_string_i(tty->term, code, a));
561 void
562 tty_putcode_ii(struct tty *tty, enum tty_code_code code, int a, int b)
564 if (a < 0 || b < 0)
565 return;
566 tty_puts(tty, tty_term_string_ii(tty->term, code, a, b));
569 void
570 tty_putcode_iii(struct tty *tty, enum tty_code_code code, int a, int b, int c)
572 if (a < 0 || b < 0 || c < 0)
573 return;
574 tty_puts(tty, tty_term_string_iii(tty->term, code, a, b, c));
577 void
578 tty_putcode_s(struct tty *tty, enum tty_code_code code, const char *a)
580 if (a != NULL)
581 tty_puts(tty, tty_term_string_s(tty->term, code, a));
584 void
585 tty_putcode_ss(struct tty *tty, enum tty_code_code code, const char *a,
586 const char *b)
588 if (a != NULL && b != NULL)
589 tty_puts(tty, tty_term_string_ss(tty->term, code, a, b));
592 static void
593 tty_add(struct tty *tty, const char *buf, size_t len)
595 struct client *c = tty->client;
597 if (tty->flags & TTY_BLOCK) {
598 tty->discarded += len;
599 return;
602 evbuffer_add(tty->out, buf, len);
603 log_debug("%s: %.*s", c->name, (int)len, buf);
604 c->written += len;
606 if (tty_log_fd != -1)
607 write(tty_log_fd, buf, len);
608 if (tty->flags & TTY_STARTED)
609 event_add(&tty->event_out, NULL);
612 void
613 tty_puts(struct tty *tty, const char *s)
615 if (*s != '\0')
616 tty_add(tty, s, strlen(s));
619 void
620 tty_putc(struct tty *tty, u_char ch)
622 const char *acs;
624 if ((tty->term->flags & TERM_NOAM) &&
625 ch >= 0x20 && ch != 0x7f &&
626 tty->cy == tty->sy - 1 &&
627 tty->cx + 1 >= tty->sx)
628 return;
630 if (tty->cell.attr & GRID_ATTR_CHARSET) {
631 acs = tty_acs_get(tty, ch);
632 if (acs != NULL)
633 tty_add(tty, acs, strlen(acs));
634 else
635 tty_add(tty, &ch, 1);
636 } else
637 tty_add(tty, &ch, 1);
639 if (ch >= 0x20 && ch != 0x7f) {
640 if (tty->cx >= tty->sx) {
641 tty->cx = 1;
642 if (tty->cy != tty->rlower)
643 tty->cy++;
646 * On !am terminals, force the cursor position to where
647 * we think it should be after a line wrap - this means
648 * it works on sensible terminals as well.
650 if (tty->term->flags & TERM_NOAM)
651 tty_putcode_ii(tty, TTYC_CUP, tty->cy, tty->cx);
652 } else
653 tty->cx++;
657 void
658 tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
660 if ((tty->term->flags & TERM_NOAM) &&
661 tty->cy == tty->sy - 1 &&
662 tty->cx + len >= tty->sx)
663 len = tty->sx - tty->cx - 1;
665 tty_add(tty, buf, len);
666 if (tty->cx + width > tty->sx) {
667 tty->cx = (tty->cx + width) - tty->sx;
668 if (tty->cx <= tty->sx)
669 tty->cy++;
670 else
671 tty->cx = tty->cy = UINT_MAX;
672 } else
673 tty->cx += width;
676 static void
677 tty_set_italics(struct tty *tty)
679 const char *s;
681 if (tty_term_has(tty->term, TTYC_SITM)) {
682 s = options_get_string(global_options, "default-terminal");
683 if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
684 tty_putcode(tty, TTYC_SITM);
685 return;
688 tty_putcode(tty, TTYC_SMSO);
691 void
692 tty_set_title(struct tty *tty, const char *title)
694 if (!tty_term_has(tty->term, TTYC_TSL) ||
695 !tty_term_has(tty->term, TTYC_FSL))
696 return;
698 tty_putcode(tty, TTYC_TSL);
699 tty_puts(tty, title);
700 tty_putcode(tty, TTYC_FSL);
703 void
704 tty_set_path(struct tty *tty, const char *title)
706 if (!tty_term_has(tty->term, TTYC_SWD) ||
707 !tty_term_has(tty->term, TTYC_FSL))
708 return;
710 tty_putcode(tty, TTYC_SWD);
711 tty_puts(tty, title);
712 tty_putcode(tty, TTYC_FSL);
715 static void
716 tty_force_cursor_colour(struct tty *tty, int c)
718 u_char r, g, b;
719 char s[13];
721 if (c != -1)
722 c = colour_force_rgb(c);
723 if (c == tty->ccolour)
724 return;
725 if (c == -1)
726 tty_putcode(tty, TTYC_CR);
727 else {
728 colour_split_rgb(c, &r, &g, &b);
729 xsnprintf(s, sizeof s, "rgb:%02hhx/%02hhx/%02hhx", r, g, b);
730 tty_putcode_s(tty, TTYC_CS, s);
732 tty->ccolour = c;
735 static int
736 tty_update_cursor(struct tty *tty, int mode, struct screen *s)
738 enum screen_cursor_style cstyle;
739 int ccolour, changed, cmode = mode;
741 /* Set cursor colour if changed. */
742 if (s != NULL) {
743 ccolour = s->ccolour;
744 if (s->ccolour == -1)
745 ccolour = s->default_ccolour;
746 tty_force_cursor_colour(tty, ccolour);
749 /* If cursor is off, set as invisible. */
750 if (~cmode & MODE_CURSOR) {
751 if (tty->mode & MODE_CURSOR)
752 tty_putcode(tty, TTYC_CIVIS);
753 return (cmode);
756 /* Check if blinking or very visible flag changed or style changed. */
757 if (s == NULL)
758 cstyle = tty->cstyle;
759 else {
760 cstyle = s->cstyle;
761 if (cstyle == SCREEN_CURSOR_DEFAULT) {
762 if (~cmode & MODE_CURSOR_BLINKING_SET) {
763 if (s->default_mode & MODE_CURSOR_BLINKING)
764 cmode |= MODE_CURSOR_BLINKING;
765 else
766 cmode &= ~MODE_CURSOR_BLINKING;
768 cstyle = s->default_cstyle;
772 /* If nothing changed, do nothing. */
773 changed = cmode ^ tty->mode;
774 if ((changed & CURSOR_MODES) == 0 && cstyle == tty->cstyle)
775 return (cmode);
778 * Set cursor style. If an explicit style has been set with DECSCUSR,
779 * set it if supported, otherwise send cvvis for blinking styles.
781 * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis
782 * if either the blinking or very visible flags are set.
784 tty_putcode(tty, TTYC_CNORM);
785 switch (cstyle) {
786 case SCREEN_CURSOR_DEFAULT:
787 if (tty->cstyle != SCREEN_CURSOR_DEFAULT) {
788 if (tty_term_has(tty->term, TTYC_SE))
789 tty_putcode(tty, TTYC_SE);
790 else
791 tty_putcode_i(tty, TTYC_SS, 0);
793 if (cmode & (MODE_CURSOR_BLINKING|MODE_CURSOR_VERY_VISIBLE))
794 tty_putcode(tty, TTYC_CVVIS);
795 break;
796 case SCREEN_CURSOR_BLOCK:
797 if (tty_term_has(tty->term, TTYC_SS)) {
798 if (cmode & MODE_CURSOR_BLINKING)
799 tty_putcode_i(tty, TTYC_SS, 1);
800 else
801 tty_putcode_i(tty, TTYC_SS, 2);
802 } else if (cmode & MODE_CURSOR_BLINKING)
803 tty_putcode(tty, TTYC_CVVIS);
804 break;
805 case SCREEN_CURSOR_UNDERLINE:
806 if (tty_term_has(tty->term, TTYC_SS)) {
807 if (cmode & MODE_CURSOR_BLINKING)
808 tty_putcode_i(tty, TTYC_SS, 3);
809 else
810 tty_putcode_i(tty, TTYC_SS, 4);
811 } else if (cmode & MODE_CURSOR_BLINKING)
812 tty_putcode(tty, TTYC_CVVIS);
813 break;
814 case SCREEN_CURSOR_BAR:
815 if (tty_term_has(tty->term, TTYC_SS)) {
816 if (cmode & MODE_CURSOR_BLINKING)
817 tty_putcode_i(tty, TTYC_SS, 5);
818 else
819 tty_putcode_i(tty, TTYC_SS, 6);
820 } else if (cmode & MODE_CURSOR_BLINKING)
821 tty_putcode(tty, TTYC_CVVIS);
822 break;
824 tty->cstyle = cstyle;
825 return (cmode);
828 void
829 tty_update_mode(struct tty *tty, int mode, struct screen *s)
831 struct tty_term *term = tty->term;
832 struct client *c = tty->client;
833 int changed;
835 if (tty->flags & TTY_NOCURSOR)
836 mode &= ~MODE_CURSOR;
838 if (tty_update_cursor(tty, mode, s) & MODE_CURSOR_BLINKING)
839 mode |= MODE_CURSOR_BLINKING;
840 else
841 mode &= ~MODE_CURSOR_BLINKING;
843 changed = mode ^ tty->mode;
844 if (log_get_level() != 0 && changed != 0) {
845 log_debug("%s: current mode %s", c->name,
846 screen_mode_to_string(tty->mode));
847 log_debug("%s: setting mode %s", c->name,
848 screen_mode_to_string(mode));
851 if ((changed & ALL_MOUSE_MODES) && tty_term_has(term, TTYC_KMOUS)) {
853 * If the mouse modes have changed, clear then all and apply
854 * again. There are differences in how terminals track the
855 * various bits.
857 tty_puts(tty, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l");
858 if (mode & ALL_MOUSE_MODES)
859 tty_puts(tty, "\033[?1006h");
860 if (mode & MODE_MOUSE_ALL)
861 tty_puts(tty, "\033[?1000h\033[?1002h\033[?1003h");
862 else if (mode & MODE_MOUSE_BUTTON)
863 tty_puts(tty, "\033[?1000h\033[?1002h");
864 else if (mode & MODE_MOUSE_STANDARD)
865 tty_puts(tty, "\033[?1000h");
867 tty->mode = mode;
870 static void
871 tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
872 enum tty_code_code code1, u_int n)
874 if (tty_term_has(tty->term, code))
875 tty_putcode_i(tty, code, n);
876 else {
877 while (n-- > 0)
878 tty_putcode(tty, code1);
882 static void
883 tty_repeat_space(struct tty *tty, u_int n)
885 static char s[500];
887 if (*s != ' ')
888 memset(s, ' ', sizeof s);
890 while (n > sizeof s) {
891 tty_putn(tty, s, sizeof s, sizeof s);
892 n -= sizeof s;
894 if (n != 0)
895 tty_putn(tty, s, n, n);
898 /* Is this window bigger than the terminal? */
900 tty_window_bigger(struct tty *tty)
902 struct client *c = tty->client;
903 struct window *w = c->session->curw->window;
905 return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
908 /* What offset should this window be drawn at? */
910 tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
912 *ox = tty->oox;
913 *oy = tty->ooy;
914 *sx = tty->osx;
915 *sy = tty->osy;
917 return (tty->oflag);
920 /* What offset should this window be drawn at? */
921 static int
922 tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
924 struct client *c = tty->client;
925 struct window *w = c->session->curw->window;
926 struct window_pane *wp = server_client_get_pane(c);
927 u_int cx, cy, lines;
929 lines = status_line_size(c);
931 if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
932 *ox = 0;
933 *oy = 0;
934 *sx = w->sx;
935 *sy = w->sy;
937 c->pan_window = NULL;
938 return (0);
941 *sx = tty->sx;
942 *sy = tty->sy - lines;
944 if (c->pan_window == w) {
945 if (*sx >= w->sx)
946 c->pan_ox = 0;
947 else if (c->pan_ox + *sx > w->sx)
948 c->pan_ox = w->sx - *sx;
949 *ox = c->pan_ox;
950 if (*sy >= w->sy)
951 c->pan_oy = 0;
952 else if (c->pan_oy + *sy > w->sy)
953 c->pan_oy = w->sy - *sy;
954 *oy = c->pan_oy;
955 return (1);
958 if (~wp->screen->mode & MODE_CURSOR) {
959 *ox = 0;
960 *oy = 0;
961 } else {
962 cx = wp->xoff + wp->screen->cx;
963 cy = wp->yoff + wp->screen->cy;
965 if (cx < *sx)
966 *ox = 0;
967 else if (cx > w->sx - *sx)
968 *ox = w->sx - *sx;
969 else
970 *ox = cx - *sx / 2;
972 if (cy < *sy)
973 *oy = 0;
974 else if (cy > w->sy - *sy)
975 *oy = w->sy - *sy;
976 else
977 *oy = cy - *sy / 2;
980 c->pan_window = NULL;
981 return (1);
984 /* Update stored offsets for a window and redraw if necessary. */
985 void
986 tty_update_window_offset(struct window *w)
988 struct client *c;
990 TAILQ_FOREACH(c, &clients, entry) {
991 if (c->session != NULL &&
992 c->session->curw != NULL &&
993 c->session->curw->window == w)
994 tty_update_client_offset(c);
998 /* Update stored offsets for a client and redraw if necessary. */
999 void
1000 tty_update_client_offset(struct client *c)
1002 u_int ox, oy, sx, sy;
1004 if (~c->flags & CLIENT_TERMINAL)
1005 return;
1007 c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
1008 if (ox == c->tty.oox &&
1009 oy == c->tty.ooy &&
1010 sx == c->tty.osx &&
1011 sy == c->tty.osy)
1012 return;
1014 log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
1015 __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
1016 ox, oy, sx, sy);
1018 c->tty.oox = ox;
1019 c->tty.ooy = oy;
1020 c->tty.osx = sx;
1021 c->tty.osy = sy;
1023 c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
1027 * Is the region large enough to be worth redrawing once later rather than
1028 * probably several times now? Currently yes if it is more than 50% of the
1029 * pane.
1031 static int
1032 tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1034 return (ctx->orlower - ctx->orupper >= ctx->sy / 2);
1038 * Return if BCE is needed but the terminal doesn't have it - it'll need to be
1039 * emulated.
1041 static int
1042 tty_fake_bce(const struct tty *tty, const struct grid_cell *gc, u_int bg)
1044 if (tty_term_flag(tty->term, TTYC_BCE))
1045 return (0);
1046 if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc->bg))
1047 return (1);
1048 return (0);
1052 * Redraw scroll region using data from screen (already updated). Used when
1053 * CSR not supported, or window is a pane that doesn't take up the full
1054 * width of the terminal.
1056 static void
1057 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1059 struct client *c = tty->client;
1060 u_int i;
1063 * If region is large, schedule a redraw. In most cases this is likely
1064 * to be followed by some more scrolling.
1066 if (tty_large_region(tty, ctx)) {
1067 log_debug("%s: %s large redraw", __func__, c->name);
1068 ctx->redraw_cb(ctx);
1069 return;
1072 for (i = ctx->orupper; i <= ctx->orlower; i++)
1073 tty_draw_pane(tty, ctx, i);
1076 /* Is this position visible in the pane? */
1077 static int
1078 tty_is_visible(__unused struct tty *tty, const struct tty_ctx *ctx, u_int px,
1079 u_int py, u_int nx, u_int ny)
1081 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1083 if (!ctx->bigger)
1084 return (1);
1086 if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx ||
1087 yoff + ny <= ctx->woy || yoff >= ctx->woy + ctx->wsy)
1088 return (0);
1089 return (1);
1092 /* Clamp line position to visible part of pane. */
1093 static int
1094 tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1095 u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
1097 u_int xoff = ctx->rxoff + px;
1099 if (!tty_is_visible(tty, ctx, px, py, nx, 1))
1100 return (0);
1101 *ry = ctx->yoff + py - ctx->woy;
1103 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1104 /* All visible. */
1105 *i = 0;
1106 *x = ctx->xoff + px - ctx->wox;
1107 *rx = nx;
1108 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1109 /* Both left and right not visible. */
1110 *i = ctx->wox;
1111 *x = 0;
1112 *rx = ctx->wsx;
1113 } else if (xoff < ctx->wox) {
1114 /* Left not visible. */
1115 *i = ctx->wox - (ctx->xoff + px);
1116 *x = 0;
1117 *rx = nx - *i;
1118 } else {
1119 /* Right not visible. */
1120 *i = 0;
1121 *x = (ctx->xoff + px) - ctx->wox;
1122 *rx = ctx->wsx - *x;
1124 if (*rx > nx)
1125 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1127 return (1);
1130 /* Clear a line. */
1131 static void
1132 tty_clear_line(struct tty *tty, const struct grid_cell *defaults, u_int py,
1133 u_int px, u_int nx, u_int bg)
1135 struct client *c = tty->client;
1136 struct overlay_ranges r;
1137 u_int i;
1139 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1141 /* Nothing to clear. */
1142 if (nx == 0)
1143 return;
1145 /* If genuine BCE is available, can try escape sequences. */
1146 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1147 /* Off the end of the line, use EL if available. */
1148 if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
1149 tty_cursor(tty, px, py);
1150 tty_putcode(tty, TTYC_EL);
1151 return;
1154 /* At the start of the line. Use EL1. */
1155 if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1156 tty_cursor(tty, px + nx - 1, py);
1157 tty_putcode(tty, TTYC_EL1);
1158 return;
1161 /* Section of line. Use ECH if possible. */
1162 if (tty_term_has(tty->term, TTYC_ECH)) {
1163 tty_cursor(tty, px, py);
1164 tty_putcode_i(tty, TTYC_ECH, nx);
1165 return;
1170 * Couldn't use an escape sequence, use spaces. Clear only the visible
1171 * bit if there is an overlay.
1173 tty_check_overlay_range(tty, px, py, nx, &r);
1174 for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
1175 if (r.nx[i] == 0)
1176 continue;
1177 tty_cursor(tty, r.px[i], py);
1178 tty_repeat_space(tty, r.nx[i]);
1182 /* Clear a line, adjusting to visible part of pane. */
1183 static void
1184 tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1185 u_int px, u_int nx, u_int bg)
1187 struct client *c = tty->client;
1188 u_int i, x, rx, ry;
1190 log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1192 if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
1193 tty_clear_line(tty, &ctx->defaults, ry, x, rx, bg);
1196 /* Clamp area position to visible part of pane. */
1197 static int
1198 tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
1199 u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
1200 u_int *ry)
1202 u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py;
1204 if (!tty_is_visible(tty, ctx, px, py, nx, ny))
1205 return (0);
1207 if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1208 /* All visible. */
1209 *i = 0;
1210 *x = ctx->xoff + px - ctx->wox;
1211 *rx = nx;
1212 } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1213 /* Both left and right not visible. */
1214 *i = ctx->wox;
1215 *x = 0;
1216 *rx = ctx->wsx;
1217 } else if (xoff < ctx->wox) {
1218 /* Left not visible. */
1219 *i = ctx->wox - (ctx->xoff + px);
1220 *x = 0;
1221 *rx = nx - *i;
1222 } else {
1223 /* Right not visible. */
1224 *i = 0;
1225 *x = (ctx->xoff + px) - ctx->wox;
1226 *rx = ctx->wsx - *x;
1228 if (*rx > nx)
1229 fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
1231 if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) {
1232 /* All visible. */
1233 *j = 0;
1234 *y = ctx->yoff + py - ctx->woy;
1235 *ry = ny;
1236 } else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) {
1237 /* Both top and bottom not visible. */
1238 *j = ctx->woy;
1239 *y = 0;
1240 *ry = ctx->wsy;
1241 } else if (yoff < ctx->woy) {
1242 /* Top not visible. */
1243 *j = ctx->woy - (ctx->yoff + py);
1244 *y = 0;
1245 *ry = ny - *j;
1246 } else {
1247 /* Bottom not visible. */
1248 *j = 0;
1249 *y = (ctx->yoff + py) - ctx->woy;
1250 *ry = ctx->wsy - *y;
1252 if (*ry > ny)
1253 fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
1255 return (1);
1258 /* Clear an area, adjusting to visible part of pane. */
1259 static void
1260 tty_clear_area(struct tty *tty, const struct grid_cell *defaults, u_int py,
1261 u_int ny, u_int px, u_int nx, u_int bg)
1263 struct client *c = tty->client;
1264 u_int yy;
1265 char tmp[64];
1267 log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1269 /* Nothing to clear. */
1270 if (nx == 0 || ny == 0)
1271 return;
1273 /* If genuine BCE is available, can try escape sequences. */
1274 if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) {
1275 /* Use ED if clearing off the bottom of the terminal. */
1276 if (px == 0 &&
1277 px + nx >= tty->sx &&
1278 py + ny >= tty->sy &&
1279 tty_term_has(tty->term, TTYC_ED)) {
1280 tty_cursor(tty, 0, py);
1281 tty_putcode(tty, TTYC_ED);
1282 return;
1286 * On VT420 compatible terminals we can use DECFRA if the
1287 * background colour isn't default (because it doesn't work
1288 * after SGR 0).
1290 if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1291 xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
1292 py + 1, px + 1, py + ny, px + nx);
1293 tty_puts(tty, tmp);
1294 return;
1297 /* Full lines can be scrolled away to clear them. */
1298 if (px == 0 &&
1299 px + nx >= tty->sx &&
1300 ny > 2 &&
1301 tty_term_has(tty->term, TTYC_CSR) &&
1302 tty_term_has(tty->term, TTYC_INDN)) {
1303 tty_region(tty, py, py + ny - 1);
1304 tty_margin_off(tty);
1305 tty_putcode_i(tty, TTYC_INDN, ny);
1306 return;
1310 * If margins are supported, can just scroll the area off to
1311 * clear it.
1313 if (nx > 2 &&
1314 ny > 2 &&
1315 tty_term_has(tty->term, TTYC_CSR) &&
1316 tty_use_margin(tty) &&
1317 tty_term_has(tty->term, TTYC_INDN)) {
1318 tty_region(tty, py, py + ny - 1);
1319 tty_margin(tty, px, px + nx - 1);
1320 tty_putcode_i(tty, TTYC_INDN, ny);
1321 return;
1325 /* Couldn't use an escape sequence, loop over the lines. */
1326 for (yy = py; yy < py + ny; yy++)
1327 tty_clear_line(tty, defaults, yy, px, nx, bg);
1330 /* Clear an area in a pane. */
1331 static void
1332 tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
1333 u_int ny, u_int px, u_int nx, u_int bg)
1335 u_int i, j, x, y, rx, ry;
1337 if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
1338 tty_clear_area(tty, &ctx->defaults, y, ry, x, rx, bg);
1341 static void
1342 tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1344 struct screen *s = ctx->s;
1345 u_int nx = ctx->sx, i, x, rx, ry;
1347 log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
1349 if (!ctx->bigger) {
1350 tty_draw_line(tty, s, 0, py, nx, ctx->xoff, ctx->yoff + py,
1351 &ctx->defaults, ctx->palette);
1352 return;
1354 if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry)) {
1355 tty_draw_line(tty, s, i, py, rx, x, ry, &ctx->defaults,
1356 ctx->palette);
1360 static const struct grid_cell *
1361 tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
1363 static struct grid_cell new;
1364 int c;
1366 /* Characters less than 0x7f are always fine, no matter what. */
1367 if (gc->data.size == 1 && *gc->data.data < 0x7f)
1368 return (gc);
1369 if (gc->flags & GRID_FLAG_TAB)
1370 return (gc);
1372 /* UTF-8 terminal and a UTF-8 character - fine. */
1373 if (tty->client->flags & CLIENT_UTF8)
1374 return (gc);
1375 memcpy(&new, gc, sizeof new);
1377 /* See if this can be mapped to an ACS character. */
1378 c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size);
1379 if (c != -1) {
1380 utf8_set(&new.data, c);
1381 new.attr |= GRID_ATTR_CHARSET;
1382 return (&new);
1385 /* Replace by the right number of underscores. */
1386 new.data.size = gc->data.width;
1387 if (new.data.size > UTF8_SIZE)
1388 new.data.size = UTF8_SIZE;
1389 memset(new.data.data, '_', new.data.size);
1390 return (&new);
1394 * Check if a single character is obstructed by the overlay and return a
1395 * boolean.
1397 static int
1398 tty_check_overlay(struct tty *tty, u_int px, u_int py)
1400 struct overlay_ranges r;
1403 * A unit width range will always return nx[2] == 0 from a check, even
1404 * with multiple overlays, so it's sufficient to check just the first
1405 * two entries.
1407 tty_check_overlay_range(tty, px, py, 1, &r);
1408 if (r.nx[0] + r.nx[1] == 0)
1409 return (0);
1410 return (1);
1413 /* Return parts of the input range which are visible. */
1414 static void
1415 tty_check_overlay_range(struct tty *tty, u_int px, u_int py, u_int nx,
1416 struct overlay_ranges *r)
1418 struct client *c = tty->client;
1420 if (c->overlay_check == NULL) {
1421 r->px[0] = px;
1422 r->nx[0] = nx;
1423 r->px[1] = 0;
1424 r->nx[1] = 0;
1425 r->px[2] = 0;
1426 r->nx[2] = 0;
1427 return;
1430 c->overlay_check(c, c->overlay_data, px, py, nx, r);
1433 void
1434 tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
1435 u_int atx, u_int aty, const struct grid_cell *defaults,
1436 struct colour_palette *palette)
1438 struct grid *gd = s->grid;
1439 struct grid_cell gc, last;
1440 const struct grid_cell *gcp;
1441 struct grid_line *gl;
1442 struct client *c = tty->client;
1443 struct overlay_ranges r;
1444 u_int i, j, ux, sx, width, hidden, eux, nxx;
1445 u_int cellsize;
1446 int flags, cleared = 0, wrapped = 0;
1447 char buf[512];
1448 size_t len;
1450 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1451 px, py, nx, atx, aty);
1452 log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg,
1453 defaults->bg);
1456 * py is the line in the screen to draw.
1457 * px is the start x and nx is the width to draw.
1458 * atx,aty is the line on the terminal to draw it.
1461 flags = (tty->flags & TTY_NOCURSOR);
1462 tty->flags |= TTY_NOCURSOR;
1463 tty_update_mode(tty, tty->mode, s);
1465 tty_region_off(tty);
1466 tty_margin_off(tty);
1469 * Clamp the width to cellsize - note this is not cellused, because
1470 * there may be empty background cells after it (from BCE).
1472 sx = screen_size_x(s);
1473 if (nx > sx)
1474 nx = sx;
1475 cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1476 if (sx > cellsize)
1477 sx = cellsize;
1478 if (sx > tty->sx)
1479 sx = tty->sx;
1480 if (sx > nx)
1481 sx = nx;
1482 ux = 0;
1484 if (py == 0)
1485 gl = NULL;
1486 else
1487 gl = grid_get_line(gd, gd->hsize + py - 1);
1488 if (gl == NULL ||
1489 (~gl->flags & GRID_LINE_WRAPPED) ||
1490 atx != 0 ||
1491 tty->cx < tty->sx ||
1492 nx < tty->sx) {
1493 if (nx < tty->sx &&
1494 atx == 0 &&
1495 px + sx != nx &&
1496 tty_term_has(tty->term, TTYC_EL1) &&
1497 !tty_fake_bce(tty, defaults, 8) &&
1498 c->overlay_check == NULL) {
1499 tty_default_attributes(tty, defaults, palette, 8,
1500 s->hyperlinks);
1501 tty_cursor(tty, nx - 1, aty);
1502 tty_putcode(tty, TTYC_EL1);
1503 cleared = 1;
1505 } else {
1506 log_debug("%s: wrapped line %u", __func__, aty);
1507 wrapped = 1;
1510 memcpy(&last, &grid_default_cell, sizeof last);
1511 len = 0;
1512 width = 0;
1514 for (i = 0; i < sx; i++) {
1515 grid_view_get_cell(gd, px + i, py, &gc);
1516 gcp = tty_check_codeset(tty, &gc);
1517 if (len != 0 &&
1518 (!tty_check_overlay(tty, atx + ux + width, aty) ||
1519 (gcp->attr & GRID_ATTR_CHARSET) ||
1520 gcp->flags != last.flags ||
1521 gcp->attr != last.attr ||
1522 gcp->fg != last.fg ||
1523 gcp->bg != last.bg ||
1524 gcp->us != last.us ||
1525 gcp->link != last.link ||
1526 ux + width + gcp->data.width > nx ||
1527 (sizeof buf) - len < gcp->data.size)) {
1528 tty_attributes(tty, &last, defaults, palette,
1529 s->hyperlinks);
1530 if (last.flags & GRID_FLAG_CLEARED) {
1531 log_debug("%s: %zu cleared", __func__, len);
1532 tty_clear_line(tty, defaults, aty, atx + ux,
1533 width, last.bg);
1534 } else {
1535 if (!wrapped || atx != 0 || ux != 0)
1536 tty_cursor(tty, atx + ux, aty);
1537 tty_putn(tty, buf, len, width);
1539 ux += width;
1541 len = 0;
1542 width = 0;
1543 wrapped = 0;
1546 if (gcp->flags & GRID_FLAG_SELECTED)
1547 screen_select_cell(s, &last, gcp);
1548 else
1549 memcpy(&last, gcp, sizeof last);
1551 tty_check_overlay_range(tty, atx + ux, aty, gcp->data.width,
1552 &r);
1553 hidden = 0;
1554 for (j = 0; j < OVERLAY_MAX_RANGES; j++)
1555 hidden += r.nx[j];
1556 hidden = gcp->data.width - hidden;
1557 if (hidden != 0 && hidden == gcp->data.width) {
1558 if (~gcp->flags & GRID_FLAG_PADDING)
1559 ux += gcp->data.width;
1560 } else if (hidden != 0 || ux + gcp->data.width > nx) {
1561 if (~gcp->flags & GRID_FLAG_PADDING) {
1562 tty_attributes(tty, &last, defaults, palette,
1563 s->hyperlinks);
1564 for (j = 0; j < OVERLAY_MAX_RANGES; j++) {
1565 if (r.nx[j] == 0)
1566 continue;
1567 /* Effective width drawn so far. */
1568 eux = r.px[j] - atx;
1569 if (eux < nx) {
1570 tty_cursor(tty, r.px[j], aty);
1571 nxx = nx - eux;
1572 if (r.nx[j] > nxx)
1573 r.nx[j] = nxx;
1574 tty_repeat_space(tty, r.nx[j]);
1575 ux = eux + r.nx[j];
1579 } else if (gcp->attr & GRID_ATTR_CHARSET) {
1580 tty_attributes(tty, &last, defaults, palette,
1581 s->hyperlinks);
1582 tty_cursor(tty, atx + ux, aty);
1583 for (j = 0; j < gcp->data.size; j++)
1584 tty_putc(tty, gcp->data.data[j]);
1585 ux += gcp->data.width;
1586 } else if (~gcp->flags & GRID_FLAG_PADDING) {
1587 memcpy(buf + len, gcp->data.data, gcp->data.size);
1588 len += gcp->data.size;
1589 width += gcp->data.width;
1592 if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1593 tty_attributes(tty, &last, defaults, palette, s->hyperlinks);
1594 if (last.flags & GRID_FLAG_CLEARED) {
1595 log_debug("%s: %zu cleared (end)", __func__, len);
1596 tty_clear_line(tty, defaults, aty, atx + ux, width,
1597 last.bg);
1598 } else {
1599 if (!wrapped || atx != 0 || ux != 0)
1600 tty_cursor(tty, atx + ux, aty);
1601 tty_putn(tty, buf, len, width);
1603 ux += width;
1606 if (!cleared && ux < nx) {
1607 log_debug("%s: %u to end of line (%zu cleared)", __func__,
1608 nx - ux, len);
1609 tty_default_attributes(tty, defaults, palette, 8,
1610 s->hyperlinks);
1611 tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1614 tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1615 tty_update_mode(tty, tty->mode, s);
1618 #ifdef ENABLE_SIXEL
1619 /* Update context for client. */
1620 static int
1621 tty_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
1623 struct window_pane *wp = ttyctx->arg;
1625 if (c->session->curw->window != wp->window)
1626 return (0);
1627 if (wp->layout_cell == NULL)
1628 return (0);
1630 /* Set the properties relevant to the current client. */
1631 ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy,
1632 &ttyctx->wsx, &ttyctx->wsy);
1634 ttyctx->yoff = ttyctx->ryoff = wp->yoff;
1635 if (status_at_line(c) == 0)
1636 ttyctx->yoff += status_line_size(c);
1638 return (1);
1641 void
1642 tty_draw_images(struct client *c, struct window_pane *wp, struct screen *s)
1644 struct image *im;
1645 struct tty_ctx ttyctx;
1647 TAILQ_FOREACH(im, &s->images, entry) {
1648 memset(&ttyctx, 0, sizeof ttyctx);
1650 /* Set the client independent properties. */
1651 ttyctx.ocx = im->px;
1652 ttyctx.ocy = im->py;
1654 ttyctx.orlower = s->rlower;
1655 ttyctx.orupper = s->rupper;
1657 ttyctx.xoff = ttyctx.rxoff = wp->xoff;
1658 ttyctx.sx = wp->sx;
1659 ttyctx.sy = wp->sy;
1661 ttyctx.ptr = im;
1662 ttyctx.arg = wp;
1663 ttyctx.set_client_cb = tty_set_client_cb;
1664 ttyctx.allow_invisible_panes = 1;
1665 tty_write_one(tty_cmd_sixelimage, c, &ttyctx);
1668 #endif
1670 void
1671 tty_sync_start(struct tty *tty)
1673 if (tty->flags & TTY_BLOCK)
1674 return;
1675 if (tty->flags & TTY_SYNCING)
1676 return;
1677 tty->flags |= TTY_SYNCING;
1679 if (tty_term_has(tty->term, TTYC_SYNC)) {
1680 log_debug("%s sync start", tty->client->name);
1681 tty_putcode_i(tty, TTYC_SYNC, 1);
1685 void
1686 tty_sync_end(struct tty *tty)
1688 if (tty->flags & TTY_BLOCK)
1689 return;
1690 if (~tty->flags & TTY_SYNCING)
1691 return;
1692 tty->flags &= ~TTY_SYNCING;
1694 if (tty_term_has(tty->term, TTYC_SYNC)) {
1695 log_debug("%s sync end", tty->client->name);
1696 tty_putcode_i(tty, TTYC_SYNC, 2);
1700 static int
1701 tty_client_ready(const struct tty_ctx *ctx, struct client *c)
1703 if (c->session == NULL || c->tty.term == NULL)
1704 return (0);
1705 if (c->flags & CLIENT_SUSPENDED)
1706 return (0);
1709 * If invisible panes are allowed (used for passthrough), don't care if
1710 * redrawing or frozen.
1712 if (ctx->allow_invisible_panes)
1713 return (1);
1715 if (c->flags & CLIENT_REDRAWWINDOW)
1716 return (0);
1717 if (c->tty.flags & TTY_FREEZE)
1718 return (0);
1719 return (1);
1722 void
1723 tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1724 struct tty_ctx *ctx)
1726 struct client *c;
1727 int state;
1729 if (ctx->set_client_cb == NULL)
1730 return;
1731 TAILQ_FOREACH(c, &clients, entry) {
1732 if (tty_client_ready(ctx, c)) {
1733 state = ctx->set_client_cb(ctx, c);
1734 if (state == -1)
1735 break;
1736 if (state == 0)
1737 continue;
1738 cmdfn(&c->tty, ctx);
1743 #ifdef ENABLE_SIXEL
1744 /* Only write to the incoming tty instead of every client. */
1745 static void
1746 tty_write_one(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1747 struct client *c, struct tty_ctx *ctx)
1749 if (ctx->set_client_cb == NULL)
1750 return;
1751 if ((ctx->set_client_cb(ctx, c)) == 1)
1752 cmdfn(&c->tty, ctx);
1754 #endif
1756 void
1757 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1759 struct client *c = tty->client;
1761 if (ctx->bigger ||
1762 !tty_full_width(tty, ctx) ||
1763 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1764 (!tty_term_has(tty->term, TTYC_ICH) &&
1765 !tty_term_has(tty->term, TTYC_ICH1)) ||
1766 c->overlay_check != NULL) {
1767 tty_draw_pane(tty, ctx, ctx->ocy);
1768 return;
1771 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1772 ctx->s->hyperlinks);
1774 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1776 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1779 void
1780 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1782 struct client *c = tty->client;
1784 if (ctx->bigger ||
1785 !tty_full_width(tty, ctx) ||
1786 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1787 (!tty_term_has(tty->term, TTYC_DCH) &&
1788 !tty_term_has(tty->term, TTYC_DCH1)) ||
1789 c->overlay_check != NULL) {
1790 tty_draw_pane(tty, ctx, ctx->ocy);
1791 return;
1794 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1795 ctx->s->hyperlinks);
1797 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1799 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1802 void
1803 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1805 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1806 ctx->s->hyperlinks);
1808 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1811 void
1812 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1814 struct client *c = tty->client;
1816 if (ctx->bigger ||
1817 !tty_full_width(tty, ctx) ||
1818 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1819 !tty_term_has(tty->term, TTYC_CSR) ||
1820 !tty_term_has(tty->term, TTYC_IL1) ||
1821 ctx->sx == 1 ||
1822 ctx->sy == 1 ||
1823 c->overlay_check != NULL) {
1824 tty_redraw_region(tty, ctx);
1825 return;
1828 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1829 ctx->s->hyperlinks);
1831 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1832 tty_margin_off(tty);
1833 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1835 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1836 tty->cx = tty->cy = UINT_MAX;
1839 void
1840 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1842 struct client *c = tty->client;
1844 if (ctx->bigger ||
1845 !tty_full_width(tty, ctx) ||
1846 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1847 !tty_term_has(tty->term, TTYC_CSR) ||
1848 !tty_term_has(tty->term, TTYC_DL1) ||
1849 ctx->sx == 1 ||
1850 ctx->sy == 1 ||
1851 c->overlay_check != NULL) {
1852 tty_redraw_region(tty, ctx);
1853 return;
1856 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1857 ctx->s->hyperlinks);
1859 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1860 tty_margin_off(tty);
1861 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1863 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1864 tty->cx = tty->cy = UINT_MAX;
1867 void
1868 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1870 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1871 ctx->s->hyperlinks);
1873 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1876 void
1877 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1879 u_int nx = ctx->sx - ctx->ocx;
1881 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1882 ctx->s->hyperlinks);
1884 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1887 void
1888 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1890 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1891 ctx->s->hyperlinks);
1893 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1896 void
1897 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1899 struct client *c = tty->client;
1901 if (ctx->ocy != ctx->orupper)
1902 return;
1904 if (ctx->bigger ||
1905 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1906 tty_fake_bce(tty, &ctx->defaults, 8) ||
1907 !tty_term_has(tty->term, TTYC_CSR) ||
1908 (!tty_term_has(tty->term, TTYC_RI) &&
1909 !tty_term_has(tty->term, TTYC_RIN)) ||
1910 ctx->sx == 1 ||
1911 ctx->sy == 1 ||
1912 c->overlay_check != NULL) {
1913 tty_redraw_region(tty, ctx);
1914 return;
1917 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1918 ctx->s->hyperlinks);
1920 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1921 tty_margin_pane(tty, ctx);
1922 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1924 if (tty_term_has(tty->term, TTYC_RI))
1925 tty_putcode(tty, TTYC_RI);
1926 else
1927 tty_putcode_i(tty, TTYC_RIN, 1);
1930 void
1931 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1933 struct client *c = tty->client;
1935 if (ctx->ocy != ctx->orlower)
1936 return;
1938 if (ctx->bigger ||
1939 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1940 tty_fake_bce(tty, &ctx->defaults, 8) ||
1941 !tty_term_has(tty->term, TTYC_CSR) ||
1942 ctx->sx == 1 ||
1943 ctx->sy == 1 ||
1944 c->overlay_check != NULL) {
1945 tty_redraw_region(tty, ctx);
1946 return;
1949 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1950 ctx->s->hyperlinks);
1952 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1953 tty_margin_pane(tty, ctx);
1956 * If we want to wrap a pane while using margins, the cursor needs to
1957 * be exactly on the right of the region. If the cursor is entirely off
1958 * the edge - move it back to the right. Some terminals are funny about
1959 * this and insert extra spaces, so only use the right if margins are
1960 * enabled.
1962 if (ctx->xoff + ctx->ocx > tty->rright) {
1963 if (!tty_use_margin(tty))
1964 tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1965 else
1966 tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1967 } else
1968 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1970 tty_putc(tty, '\n');
1973 void
1974 tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1976 struct client *c = tty->client;
1977 u_int i;
1979 if (ctx->bigger ||
1980 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1981 tty_fake_bce(tty, &ctx->defaults, 8) ||
1982 !tty_term_has(tty->term, TTYC_CSR) ||
1983 ctx->sx == 1 ||
1984 ctx->sy == 1 ||
1985 c->overlay_check != NULL) {
1986 tty_redraw_region(tty, ctx);
1987 return;
1990 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1991 ctx->s->hyperlinks);
1993 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1994 tty_margin_pane(tty, ctx);
1996 if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1997 if (!tty_use_margin(tty))
1998 tty_cursor(tty, 0, tty->rlower);
1999 else
2000 tty_cursor(tty, tty->rright, tty->rlower);
2001 for (i = 0; i < ctx->num; i++)
2002 tty_putc(tty, '\n');
2003 } else {
2004 if (tty->cy == UINT_MAX)
2005 tty_cursor(tty, 0, 0);
2006 else
2007 tty_cursor(tty, 0, tty->cy);
2008 tty_putcode_i(tty, TTYC_INDN, ctx->num);
2012 void
2013 tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
2015 u_int i;
2016 struct client *c = tty->client;
2018 if (ctx->bigger ||
2019 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
2020 tty_fake_bce(tty, &ctx->defaults, 8) ||
2021 !tty_term_has(tty->term, TTYC_CSR) ||
2022 (!tty_term_has(tty->term, TTYC_RI) &&
2023 !tty_term_has(tty->term, TTYC_RIN)) ||
2024 ctx->sx == 1 ||
2025 ctx->sy == 1 ||
2026 c->overlay_check != NULL) {
2027 tty_redraw_region(tty, ctx);
2028 return;
2031 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2032 ctx->s->hyperlinks);
2034 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
2035 tty_margin_pane(tty, ctx);
2036 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
2038 if (tty_term_has(tty->term, TTYC_RIN))
2039 tty_putcode_i(tty, TTYC_RIN, ctx->num);
2040 else {
2041 for (i = 0; i < ctx->num; i++)
2042 tty_putcode(tty, TTYC_RI);
2046 void
2047 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
2049 u_int px, py, nx, ny;
2051 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2052 ctx->s->hyperlinks);
2054 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2055 tty_margin_off(tty);
2057 px = 0;
2058 nx = ctx->sx;
2059 py = ctx->ocy + 1;
2060 ny = ctx->sy - ctx->ocy - 1;
2062 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
2064 px = ctx->ocx;
2065 nx = ctx->sx - ctx->ocx;
2066 py = ctx->ocy;
2068 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
2071 void
2072 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
2074 u_int px, py, nx, ny;
2076 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2077 ctx->s->hyperlinks);
2079 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2080 tty_margin_off(tty);
2082 px = 0;
2083 nx = ctx->sx;
2084 py = 0;
2085 ny = ctx->ocy;
2087 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
2089 px = 0;
2090 nx = ctx->ocx + 1;
2091 py = ctx->ocy;
2093 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
2096 void
2097 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
2099 u_int px, py, nx, ny;
2101 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2102 ctx->s->hyperlinks);
2104 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2105 tty_margin_off(tty);
2107 px = 0;
2108 nx = ctx->sx;
2109 py = 0;
2110 ny = ctx->sy;
2112 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
2115 void
2116 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
2118 u_int i, j;
2120 if (ctx->bigger) {
2121 ctx->redraw_cb(ctx);
2122 return;
2125 tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette,
2126 ctx->s->hyperlinks);
2128 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2129 tty_margin_off(tty);
2131 for (j = 0; j < ctx->sy; j++) {
2132 tty_cursor_pane(tty, ctx, 0, j);
2133 for (i = 0; i < ctx->sx; i++)
2134 tty_putc(tty, 'E');
2138 void
2139 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
2141 const struct grid_cell *gcp = ctx->cell;
2142 struct screen *s = ctx->s;
2143 struct overlay_ranges r;
2144 u_int px, py, i, vis = 0;
2146 px = ctx->xoff + ctx->ocx - ctx->wox;
2147 py = ctx->yoff + ctx->ocy - ctx->woy;
2148 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
2149 (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
2150 return;
2152 /* Handle partially obstructed wide characters. */
2153 if (gcp->data.width > 1) {
2154 tty_check_overlay_range(tty, px, py, gcp->data.width, &r);
2155 for (i = 0; i < OVERLAY_MAX_RANGES; i++)
2156 vis += r.nx[i];
2157 if (vis < gcp->data.width) {
2158 tty_draw_line(tty, s, s->cx, s->cy, gcp->data.width,
2159 px, py, &ctx->defaults, ctx->palette);
2160 return;
2164 if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
2165 ctx->ocy == ctx->orlower &&
2166 tty_full_width(tty, ctx))
2167 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
2169 tty_margin_off(tty);
2170 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2172 tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette,
2173 ctx->s->hyperlinks);
2175 if (ctx->num == 1)
2176 tty_invalidate(tty);
2179 void
2180 tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
2182 struct overlay_ranges r;
2183 u_int i, px, py, cx;
2184 char *cp = ctx->ptr;
2186 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
2187 return;
2189 if (ctx->bigger &&
2190 (ctx->xoff + ctx->ocx < ctx->wox ||
2191 ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
2192 if (!ctx->wrapped ||
2193 !tty_full_width(tty, ctx) ||
2194 (tty->term->flags & TERM_NOAM) ||
2195 ctx->xoff + ctx->ocx != 0 ||
2196 ctx->yoff + ctx->ocy != tty->cy + 1 ||
2197 tty->cx < tty->sx ||
2198 tty->cy == tty->rlower)
2199 tty_draw_pane(tty, ctx, ctx->ocy);
2200 else
2201 ctx->redraw_cb(ctx);
2202 return;
2205 tty_margin_off(tty);
2206 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2207 tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette, ctx->s->hyperlinks);
2209 /* Get tty position from pane position for overlay check. */
2210 px = ctx->xoff + ctx->ocx - ctx->wox;
2211 py = ctx->yoff + ctx->ocy - ctx->woy;
2213 tty_check_overlay_range(tty, px, py, ctx->num, &r);
2214 for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
2215 if (r.nx[i] == 0)
2216 continue;
2217 /* Convert back to pane position for printing. */
2218 cx = r.px[i] - ctx->xoff + ctx->wox;
2219 tty_cursor_pane_unless_wrap(tty, ctx, cx, ctx->ocy);
2220 tty_putn(tty, cp + r.px[i] - px, r.nx[i], r.nx[i]);
2224 void
2225 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
2227 tty_set_selection(tty, ctx->ptr2, ctx->ptr, ctx->num);
2230 void
2231 tty_set_selection(struct tty *tty, const char *flags, const char *buf,
2232 size_t len)
2234 char *encoded;
2235 size_t size;
2237 if (~tty->flags & TTY_STARTED)
2238 return;
2239 if (!tty_term_has(tty->term, TTYC_MS))
2240 return;
2242 size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
2243 encoded = xmalloc(size);
2245 b64_ntop(buf, len, encoded, size);
2246 tty->flags |= TTY_NOBLOCK;
2247 tty_putcode_ss(tty, TTYC_MS, flags, encoded);
2249 free(encoded);
2252 void
2253 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
2255 tty->flags |= TTY_NOBLOCK;
2256 tty_add(tty, ctx->ptr, ctx->num);
2257 tty_invalidate(tty);
2260 #ifdef ENABLE_SIXEL
2261 void
2262 tty_cmd_sixelimage(struct tty *tty, const struct tty_ctx *ctx)
2264 struct image *im = ctx->ptr;
2265 struct sixel_image *si = im->data;
2266 struct sixel_image *new;
2267 char *data;
2268 size_t size;
2269 u_int cx = ctx->ocx, cy = ctx->ocy, sx, sy;
2270 u_int i, j, x, y, rx, ry;
2271 int fallback = 0;
2273 if ((~tty->term->flags & TERM_SIXEL) &&
2274 !tty_term_has(tty->term, TTYC_SXL))
2275 fallback = 1;
2276 if (tty->xpixel == 0 || tty->ypixel == 0)
2277 fallback = 1;
2279 sixel_size_in_cells(si, &sx, &sy);
2280 log_debug("%s: image is %ux%u", __func__, sx, sy);
2281 if (!tty_clamp_area(tty, ctx, cx, cy, sx, sy, &i, &j, &x, &y, &rx, &ry))
2282 return;
2283 log_debug("%s: clamping to %u,%u-%u,%u", __func__, i, j, rx, ry);
2285 if (fallback == 1) {
2286 data = xstrdup(im->fallback);
2287 size = strlen(data);
2288 } else {
2289 new = sixel_scale(si, tty->xpixel, tty->ypixel, i, j, rx, ry, 0);
2290 if (new == NULL)
2291 return;
2293 data = sixel_print(new, si, &size);
2295 if (data != NULL) {
2296 log_debug("%s: %zu bytes: %s", __func__, size, data);
2297 tty_region_off(tty);
2298 tty_margin_off(tty);
2299 tty_cursor(tty, x, y);
2301 tty->flags |= TTY_NOBLOCK;
2302 tty_add(tty, data, size);
2303 tty_invalidate(tty);
2304 free(data);
2307 if (fallback == 0)
2308 sixel_free(new);
2310 #endif
2312 void
2313 tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
2315 if (ctx->num == 0x11) {
2317 * This is an overlay and a command that moves the cursor so
2318 * start synchronized updates.
2320 tty_sync_start(tty);
2321 } else if (~ctx->num & 0x10) {
2323 * This is a pane. If there is an overlay, always start;
2324 * otherwise, only if requested.
2326 if (ctx->num || tty->client->overlay_draw != NULL)
2327 tty_sync_start(tty);
2331 void
2332 tty_cell(struct tty *tty, const struct grid_cell *gc,
2333 const struct grid_cell *defaults, struct colour_palette *palette,
2334 struct hyperlinks *hl)
2336 const struct grid_cell *gcp;
2338 /* Skip last character if terminal is stupid. */
2339 if ((tty->term->flags & TERM_NOAM) &&
2340 tty->cy == tty->sy - 1 &&
2341 tty->cx == tty->sx - 1)
2342 return;
2344 /* If this is a padding character, do nothing. */
2345 if (gc->flags & GRID_FLAG_PADDING)
2346 return;
2348 /* Check the output codeset and apply attributes. */
2349 gcp = tty_check_codeset(tty, gc);
2350 tty_attributes(tty, gcp, defaults, palette, hl);
2352 /* If it is a single character, write with putc to handle ACS. */
2353 if (gcp->data.size == 1) {
2354 tty_attributes(tty, gcp, defaults, palette, hl);
2355 if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
2356 return;
2357 tty_putc(tty, *gcp->data.data);
2358 return;
2361 /* Write the data. */
2362 tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
2365 void
2366 tty_reset(struct tty *tty)
2368 struct grid_cell *gc = &tty->cell;
2370 if (!grid_cells_equal(gc, &grid_default_cell)) {
2371 if (gc->link != 0)
2372 tty_putcode_ss(tty, TTYC_HLS, "", "");
2373 if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2374 tty_putcode(tty, TTYC_RMACS);
2375 tty_putcode(tty, TTYC_SGR0);
2376 memcpy(gc, &grid_default_cell, sizeof *gc);
2378 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2381 void
2382 tty_invalidate(struct tty *tty)
2384 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2385 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2387 tty->cx = tty->cy = UINT_MAX;
2388 tty->rupper = tty->rleft = UINT_MAX;
2389 tty->rlower = tty->rright = UINT_MAX;
2391 if (tty->flags & TTY_STARTED) {
2392 if (tty_use_margin(tty))
2393 tty_putcode(tty, TTYC_ENMG);
2394 tty_putcode(tty, TTYC_SGR0);
2396 tty->mode = ALL_MODES;
2397 tty_update_mode(tty, MODE_CURSOR, NULL);
2399 tty_cursor(tty, 0, 0);
2400 tty_region_off(tty);
2401 tty_margin_off(tty);
2402 } else
2403 tty->mode = MODE_CURSOR;
2406 /* Turn off margin. */
2407 void
2408 tty_region_off(struct tty *tty)
2410 tty_region(tty, 0, tty->sy - 1);
2413 /* Set region inside pane. */
2414 static void
2415 tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2416 u_int rlower)
2418 tty_region(tty, ctx->yoff + rupper - ctx->woy,
2419 ctx->yoff + rlower - ctx->woy);
2422 /* Set region at absolute position. */
2423 static void
2424 tty_region(struct tty *tty, u_int rupper, u_int rlower)
2426 if (tty->rlower == rlower && tty->rupper == rupper)
2427 return;
2428 if (!tty_term_has(tty->term, TTYC_CSR))
2429 return;
2431 tty->rupper = rupper;
2432 tty->rlower = rlower;
2435 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2436 * 0,0 if it is beyond the last column (they do not reset their wrap
2437 * flag so further output causes a line feed). As a workaround, do an
2438 * explicit move to 0 first.
2440 if (tty->cx >= tty->sx) {
2441 if (tty->cy == UINT_MAX)
2442 tty_cursor(tty, 0, 0);
2443 else
2444 tty_cursor(tty, 0, tty->cy);
2447 tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower);
2448 tty->cx = tty->cy = UINT_MAX;
2451 /* Turn off margin. */
2452 void
2453 tty_margin_off(struct tty *tty)
2455 tty_margin(tty, 0, tty->sx - 1);
2458 /* Set margin inside pane. */
2459 static void
2460 tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2462 tty_margin(tty, ctx->xoff - ctx->wox,
2463 ctx->xoff + ctx->sx - 1 - ctx->wox);
2466 /* Set margin at absolute position. */
2467 static void
2468 tty_margin(struct tty *tty, u_int rleft, u_int rright)
2470 if (!tty_use_margin(tty))
2471 return;
2472 if (tty->rleft == rleft && tty->rright == rright)
2473 return;
2475 tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower);
2477 tty->rleft = rleft;
2478 tty->rright = rright;
2480 if (rleft == 0 && rright == tty->sx - 1)
2481 tty_putcode(tty, TTYC_CLMG);
2482 else
2483 tty_putcode_ii(tty, TTYC_CMG, rleft, rright);
2484 tty->cx = tty->cy = UINT_MAX;
2488 * Move the cursor, unless it would wrap itself when the next character is
2489 * printed.
2491 static void
2492 tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2493 u_int cx, u_int cy)
2495 if (!ctx->wrapped ||
2496 !tty_full_width(tty, ctx) ||
2497 (tty->term->flags & TERM_NOAM) ||
2498 ctx->xoff + cx != 0 ||
2499 ctx->yoff + cy != tty->cy + 1 ||
2500 tty->cx < tty->sx ||
2501 tty->cy == tty->rlower)
2502 tty_cursor_pane(tty, ctx, cx, cy);
2503 else
2504 log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
2507 /* Move cursor inside pane. */
2508 static void
2509 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2511 tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
2514 /* Move cursor to absolute position. */
2515 void
2516 tty_cursor(struct tty *tty, u_int cx, u_int cy)
2518 struct tty_term *term = tty->term;
2519 u_int thisx, thisy;
2520 int change;
2522 if (tty->flags & TTY_BLOCK)
2523 return;
2525 thisx = tty->cx;
2526 thisy = tty->cy;
2529 * If in the automargin space, and want to be there, do not move.
2530 * Otherwise, force the cursor to be in range (and complain).
2532 if (cx == thisx && cy == thisy && cx == tty->sx)
2533 return;
2534 if (cx > tty->sx - 1) {
2535 log_debug("%s: x too big %u > %u", __func__, cx, tty->sx - 1);
2536 cx = tty->sx - 1;
2539 /* No change. */
2540 if (cx == thisx && cy == thisy)
2541 return;
2543 /* Currently at the very end of the line - use absolute movement. */
2544 if (thisx > tty->sx - 1)
2545 goto absolute;
2547 /* Move to home position (0, 0). */
2548 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2549 tty_putcode(tty, TTYC_HOME);
2550 goto out;
2553 /* Zero on the next line. */
2554 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2555 (!tty_use_margin(tty) || tty->rleft == 0)) {
2556 tty_putc(tty, '\r');
2557 tty_putc(tty, '\n');
2558 goto out;
2561 /* Moving column or row. */
2562 if (cy == thisy) {
2564 * Moving column only, row staying the same.
2567 /* To left edge. */
2568 if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
2569 tty_putc(tty, '\r');
2570 goto out;
2573 /* One to the left. */
2574 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2575 tty_putcode(tty, TTYC_CUB1);
2576 goto out;
2579 /* One to the right. */
2580 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2581 tty_putcode(tty, TTYC_CUF1);
2582 goto out;
2585 /* Calculate difference. */
2586 change = thisx - cx; /* +ve left, -ve right */
2589 * Use HPA if change is larger than absolute, otherwise move
2590 * the cursor with CUB/CUF.
2592 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
2593 tty_putcode_i(tty, TTYC_HPA, cx);
2594 goto out;
2595 } else if (change > 0 &&
2596 tty_term_has(term, TTYC_CUB) &&
2597 !tty_use_margin(tty)) {
2598 if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2599 tty_putcode(tty, TTYC_CUB1);
2600 tty_putcode(tty, TTYC_CUB1);
2601 goto out;
2603 tty_putcode_i(tty, TTYC_CUB, change);
2604 goto out;
2605 } else if (change < 0 &&
2606 tty_term_has(term, TTYC_CUF) &&
2607 !tty_use_margin(tty)) {
2608 tty_putcode_i(tty, TTYC_CUF, -change);
2609 goto out;
2611 } else if (cx == thisx) {
2613 * Moving row only, column staying the same.
2616 /* One above. */
2617 if (thisy != tty->rupper &&
2618 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2619 tty_putcode(tty, TTYC_CUU1);
2620 goto out;
2623 /* One below. */
2624 if (thisy != tty->rlower &&
2625 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2626 tty_putcode(tty, TTYC_CUD1);
2627 goto out;
2630 /* Calculate difference. */
2631 change = thisy - cy; /* +ve up, -ve down */
2634 * Try to use VPA if change is larger than absolute or if this
2635 * change would cross the scroll region, otherwise use CUU/CUD.
2637 if ((u_int) abs(change) > cy ||
2638 (change < 0 && cy - change > tty->rlower) ||
2639 (change > 0 && cy - change < tty->rupper)) {
2640 if (tty_term_has(term, TTYC_VPA)) {
2641 tty_putcode_i(tty, TTYC_VPA, cy);
2642 goto out;
2644 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2645 tty_putcode_i(tty, TTYC_CUU, change);
2646 goto out;
2647 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2648 tty_putcode_i(tty, TTYC_CUD, -change);
2649 goto out;
2653 absolute:
2654 /* Absolute movement. */
2655 tty_putcode_ii(tty, TTYC_CUP, cy, cx);
2657 out:
2658 tty->cx = cx;
2659 tty->cy = cy;
2662 static void
2663 tty_hyperlink(struct tty *tty, const struct grid_cell *gc,
2664 struct hyperlinks *hl)
2666 const char *uri, *id;
2668 if (gc->link == tty->cell.link)
2669 return;
2670 tty->cell.link = gc->link;
2672 if (hl == NULL)
2673 return;
2675 if (gc->link == 0 || !hyperlinks_get(hl, gc->link, &uri, NULL, &id))
2676 tty_putcode_ss(tty, TTYC_HLS, "", "");
2677 else
2678 tty_putcode_ss(tty, TTYC_HLS, id, uri);
2681 void
2682 tty_attributes(struct tty *tty, const struct grid_cell *gc,
2683 const struct grid_cell *defaults, struct colour_palette *palette,
2684 struct hyperlinks *hl)
2686 struct grid_cell *tc = &tty->cell, gc2;
2687 int changed;
2689 /* Copy cell and update default colours. */
2690 memcpy(&gc2, gc, sizeof gc2);
2691 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2692 if (gc2.fg == 8)
2693 gc2.fg = defaults->fg;
2694 if (gc2.bg == 8)
2695 gc2.bg = defaults->bg;
2698 /* Ignore cell if it is the same as the last one. */
2699 if (gc2.attr == tty->last_cell.attr &&
2700 gc2.fg == tty->last_cell.fg &&
2701 gc2.bg == tty->last_cell.bg &&
2702 gc2.us == tty->last_cell.us &&
2703 gc2.link == tty->last_cell.link)
2704 return;
2707 * If no setab, try to use the reverse attribute as a best-effort for a
2708 * non-default background. This is a bit of a hack but it doesn't do
2709 * any serious harm and makes a couple of applications happier.
2711 if (!tty_term_has(tty->term, TTYC_SETAB)) {
2712 if (gc2.attr & GRID_ATTR_REVERSE) {
2713 if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
2714 gc2.attr &= ~GRID_ATTR_REVERSE;
2715 } else {
2716 if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
2717 gc2.attr |= GRID_ATTR_REVERSE;
2721 /* Fix up the colours if necessary. */
2722 tty_check_fg(tty, palette, &gc2);
2723 tty_check_bg(tty, palette, &gc2);
2724 tty_check_us(tty, palette, &gc2);
2727 * If any bits are being cleared or the underline colour is now default,
2728 * reset everything.
2730 if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
2731 tty_reset(tty);
2734 * Set the colours. This may call tty_reset() (so it comes next) and
2735 * may add to (NOT remove) the desired attributes.
2737 tty_colours(tty, &gc2);
2739 /* Filter out attribute bits already set. */
2740 changed = gc2.attr & ~tc->attr;
2741 tc->attr = gc2.attr;
2743 /* Set the attributes. */
2744 if (changed & GRID_ATTR_BRIGHT)
2745 tty_putcode(tty, TTYC_BOLD);
2746 if (changed & GRID_ATTR_DIM)
2747 tty_putcode(tty, TTYC_DIM);
2748 if (changed & GRID_ATTR_ITALICS)
2749 tty_set_italics(tty);
2750 if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2751 if ((changed & GRID_ATTR_UNDERSCORE) ||
2752 !tty_term_has(tty->term, TTYC_SMULX))
2753 tty_putcode(tty, TTYC_SMUL);
2754 else if (changed & GRID_ATTR_UNDERSCORE_2)
2755 tty_putcode_i(tty, TTYC_SMULX, 2);
2756 else if (changed & GRID_ATTR_UNDERSCORE_3)
2757 tty_putcode_i(tty, TTYC_SMULX, 3);
2758 else if (changed & GRID_ATTR_UNDERSCORE_4)
2759 tty_putcode_i(tty, TTYC_SMULX, 4);
2760 else if (changed & GRID_ATTR_UNDERSCORE_5)
2761 tty_putcode_i(tty, TTYC_SMULX, 5);
2763 if (changed & GRID_ATTR_BLINK)
2764 tty_putcode(tty, TTYC_BLINK);
2765 if (changed & GRID_ATTR_REVERSE) {
2766 if (tty_term_has(tty->term, TTYC_REV))
2767 tty_putcode(tty, TTYC_REV);
2768 else if (tty_term_has(tty->term, TTYC_SMSO))
2769 tty_putcode(tty, TTYC_SMSO);
2771 if (changed & GRID_ATTR_HIDDEN)
2772 tty_putcode(tty, TTYC_INVIS);
2773 if (changed & GRID_ATTR_STRIKETHROUGH)
2774 tty_putcode(tty, TTYC_SMXX);
2775 if (changed & GRID_ATTR_OVERLINE)
2776 tty_putcode(tty, TTYC_SMOL);
2777 if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2778 tty_putcode(tty, TTYC_SMACS);
2780 /* Set hyperlink if any. */
2781 tty_hyperlink(tty, gc, hl);
2783 memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
2786 static void
2787 tty_colours(struct tty *tty, const struct grid_cell *gc)
2789 struct grid_cell *tc = &tty->cell;
2791 /* No changes? Nothing is necessary. */
2792 if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
2793 return;
2796 * Is either the default colour? This is handled specially because the
2797 * best solution might be to reset both colours to default, in which
2798 * case if only one is default need to fall onward to set the other
2799 * colour.
2801 if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
2803 * If don't have AX, send sgr0. This resets both colours to default.
2804 * Otherwise, try to set the default colour only as needed.
2806 if (!tty_term_flag(tty->term, TTYC_AX))
2807 tty_reset(tty);
2808 else {
2809 if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
2810 tty_puts(tty, "\033[39m");
2811 tc->fg = gc->fg;
2813 if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
2814 tty_puts(tty, "\033[49m");
2815 tc->bg = gc->bg;
2820 /* Set the foreground colour. */
2821 if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
2822 tty_colours_fg(tty, gc);
2825 * Set the background colour. This must come after the foreground as
2826 * tty_colours_fg() can call tty_reset().
2828 if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
2829 tty_colours_bg(tty, gc);
2831 /* Set the underscore colour. */
2832 if (gc->us != tc->us)
2833 tty_colours_us(tty, gc);
2836 static void
2837 tty_check_fg(struct tty *tty, struct colour_palette *palette,
2838 struct grid_cell *gc)
2840 u_char r, g, b;
2841 u_int colours;
2842 int c;
2845 * Perform substitution if this pane has a palette. If the bright
2846 * attribute is set and Nobr is not present, use the bright entry in
2847 * the palette by changing to the aixterm colour
2849 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2850 c = gc->fg;
2851 if (c < 8 &&
2852 gc->attr & GRID_ATTR_BRIGHT &&
2853 !tty_term_has(tty->term, TTYC_NOBR))
2854 c += 90;
2855 if ((c = colour_palette_get(palette, c)) != -1)
2856 gc->fg = c;
2859 /* Is this a 24-bit colour? */
2860 if (gc->fg & COLOUR_FLAG_RGB) {
2861 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2862 if (tty->term->flags & TERM_RGBCOLOURS)
2863 return;
2864 colour_split_rgb(gc->fg, &r, &g, &b);
2865 gc->fg = colour_find_rgb(r, g, b);
2868 /* How many colours does this terminal have? */
2869 if (tty->term->flags & TERM_256COLOURS)
2870 colours = 256;
2871 else
2872 colours = tty_term_number(tty->term, TTYC_COLORS);
2874 /* Is this a 256-colour colour? */
2875 if (gc->fg & COLOUR_FLAG_256) {
2876 /* And not a 256 colour mode? */
2877 if (colours < 256) {
2878 gc->fg = colour_256to16(gc->fg);
2879 if (gc->fg & 8) {
2880 gc->fg &= 7;
2881 if (colours >= 16)
2882 gc->fg += 90;
2885 return;
2888 /* Is this an aixterm colour? */
2889 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2890 gc->fg -= 90;
2891 gc->attr |= GRID_ATTR_BRIGHT;
2895 static void
2896 tty_check_bg(struct tty *tty, struct colour_palette *palette,
2897 struct grid_cell *gc)
2899 u_char r, g, b;
2900 u_int colours;
2901 int c;
2903 /* Perform substitution if this pane has a palette. */
2904 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2905 if ((c = colour_palette_get(palette, gc->bg)) != -1)
2906 gc->bg = c;
2909 /* Is this a 24-bit colour? */
2910 if (gc->bg & COLOUR_FLAG_RGB) {
2911 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2912 if (tty->term->flags & TERM_RGBCOLOURS)
2913 return;
2914 colour_split_rgb(gc->bg, &r, &g, &b);
2915 gc->bg = colour_find_rgb(r, g, b);
2918 /* How many colours does this terminal have? */
2919 if (tty->term->flags & TERM_256COLOURS)
2920 colours = 256;
2921 else
2922 colours = tty_term_number(tty->term, TTYC_COLORS);
2924 /* Is this a 256-colour colour? */
2925 if (gc->bg & COLOUR_FLAG_256) {
2927 * And not a 256 colour mode? Translate to 16-colour
2928 * palette. Bold background doesn't exist portably, so just
2929 * discard the bold bit if set.
2931 if (colours < 256) {
2932 gc->bg = colour_256to16(gc->bg);
2933 if (gc->bg & 8) {
2934 gc->bg &= 7;
2935 if (colours >= 16)
2936 gc->bg += 90;
2939 return;
2942 /* Is this an aixterm colour? */
2943 if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
2944 gc->bg -= 90;
2947 static void
2948 tty_check_us(__unused struct tty *tty, struct colour_palette *palette,
2949 struct grid_cell *gc)
2951 int c;
2953 /* Perform substitution if this pane has a palette. */
2954 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2955 if ((c = colour_palette_get(palette, gc->us)) != -1)
2956 gc->us = c;
2959 /* Convert underscore colour if only RGB can be supported. */
2960 if (!tty_term_has(tty->term, TTYC_SETULC1)) {
2961 if ((c = colour_force_rgb (gc->us)) == -1)
2962 gc->us = 8;
2963 else
2964 gc->us = c;
2968 static void
2969 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
2971 struct grid_cell *tc = &tty->cell;
2972 char s[32];
2975 * If the current colour is an aixterm bright colour and the new is not,
2976 * reset because some terminals do not clear bright correctly.
2978 if (tty->cell.fg >= 90 &&
2979 tty->cell.bg <= 97 &&
2980 (gc->fg < 90 || gc->fg > 97))
2981 tty_reset(tty);
2983 /* Is this a 24-bit or 256-colour colour? */
2984 if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
2985 if (tty_try_colour(tty, gc->fg, "38") == 0)
2986 goto save;
2987 /* Should not get here, already converted in tty_check_fg. */
2988 return;
2991 /* Is this an aixterm bright colour? */
2992 if (gc->fg >= 90 && gc->fg <= 97) {
2993 if (tty->term->flags & TERM_256COLOURS) {
2994 xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2995 tty_puts(tty, s);
2996 } else
2997 tty_putcode_i(tty, TTYC_SETAF, gc->fg - 90 + 8);
2998 goto save;
3001 /* Otherwise set the foreground colour. */
3002 tty_putcode_i(tty, TTYC_SETAF, gc->fg);
3004 save:
3005 /* Save the new values in the terminal current cell. */
3006 tc->fg = gc->fg;
3009 static void
3010 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
3012 struct grid_cell *tc = &tty->cell;
3013 char s[32];
3015 /* Is this a 24-bit or 256-colour colour? */
3016 if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
3017 if (tty_try_colour(tty, gc->bg, "48") == 0)
3018 goto save;
3019 /* Should not get here, already converted in tty_check_bg. */
3020 return;
3023 /* Is this an aixterm bright colour? */
3024 if (gc->bg >= 90 && gc->bg <= 97) {
3025 if (tty->term->flags & TERM_256COLOURS) {
3026 xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
3027 tty_puts(tty, s);
3028 } else
3029 tty_putcode_i(tty, TTYC_SETAB, gc->bg - 90 + 8);
3030 goto save;
3033 /* Otherwise set the background colour. */
3034 tty_putcode_i(tty, TTYC_SETAB, gc->bg);
3036 save:
3037 /* Save the new values in the terminal current cell. */
3038 tc->bg = gc->bg;
3041 static void
3042 tty_colours_us(struct tty *tty, const struct grid_cell *gc)
3044 struct grid_cell *tc = &tty->cell;
3045 u_int c;
3046 u_char r, g, b;
3048 /* Clear underline colour. */
3049 if (COLOUR_DEFAULT(gc->us)) {
3050 tty_putcode(tty, TTYC_OL);
3051 goto save;
3055 * If this is not an RGB colour, use Setulc1 if it exists, otherwise
3056 * convert.
3058 if (~gc->us & COLOUR_FLAG_RGB) {
3059 c = gc->us;
3060 if ((~c & COLOUR_FLAG_256) && (c >= 90 && c <= 97))
3061 c -= 82;
3062 tty_putcode_i(tty, TTYC_SETULC1, c & ~COLOUR_FLAG_256);
3063 return;
3067 * Setulc and setal follows the ncurses(3) one argument "direct colour"
3068 * capability format. Calculate the colour value.
3070 colour_split_rgb(gc->us, &r, &g, &b);
3071 c = (65536 * r) + (256 * g) + b;
3074 * Write the colour. Only use setal if the RGB flag is set because the
3075 * non-RGB version may be wrong.
3077 if (tty_term_has(tty->term, TTYC_SETULC))
3078 tty_putcode_i(tty, TTYC_SETULC, c);
3079 else if (tty_term_has(tty->term, TTYC_SETAL) &&
3080 tty_term_has(tty->term, TTYC_RGB))
3081 tty_putcode_i(tty, TTYC_SETAL, c);
3083 save:
3084 /* Save the new values in the terminal current cell. */
3085 tc->us = gc->us;
3088 static int
3089 tty_try_colour(struct tty *tty, int colour, const char *type)
3091 u_char r, g, b;
3093 if (colour & COLOUR_FLAG_256) {
3094 if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
3095 tty_putcode_i(tty, TTYC_SETAF, colour & 0xff);
3096 else if (tty_term_has(tty->term, TTYC_SETAB))
3097 tty_putcode_i(tty, TTYC_SETAB, colour & 0xff);
3098 return (0);
3101 if (colour & COLOUR_FLAG_RGB) {
3102 colour_split_rgb(colour & 0xffffff, &r, &g, &b);
3103 if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
3104 tty_putcode_iii(tty, TTYC_SETRGBF, r, g, b);
3105 else if (tty_term_has(tty->term, TTYC_SETRGBB))
3106 tty_putcode_iii(tty, TTYC_SETRGBB, r, g, b);
3107 return (0);
3110 return (-1);
3113 static void
3114 tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
3116 memcpy(gc, &grid_default_cell, sizeof *gc);
3117 gc->fg = wp->palette.fg;
3118 gc->bg = wp->palette.bg;
3121 void
3122 tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
3124 struct options *oo = wp->options;
3125 struct format_tree *ft;
3127 memcpy(gc, &grid_default_cell, sizeof *gc);
3129 if (wp->flags & PANE_STYLECHANGED) {
3130 log_debug("%%%u: style changed", wp->id);
3131 wp->flags &= ~PANE_STYLECHANGED;
3133 ft = format_create(NULL, NULL, FORMAT_PANE|wp->id,
3134 FORMAT_NOJOBS);
3135 format_defaults(ft, NULL, NULL, NULL, wp);
3136 tty_window_default_style(&wp->cached_active_gc, wp);
3137 style_add(&wp->cached_active_gc, oo, "window-active-style", ft);
3138 tty_window_default_style(&wp->cached_gc, wp);
3139 style_add(&wp->cached_gc, oo, "window-style", ft);
3140 format_free(ft);
3143 if (gc->fg == 8) {
3144 if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
3145 gc->fg = wp->cached_active_gc.fg;
3146 else
3147 gc->fg = wp->cached_gc.fg;
3150 if (gc->bg == 8) {
3151 if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
3152 gc->bg = wp->cached_active_gc.bg;
3153 else
3154 gc->bg = wp->cached_gc.bg;
3158 static void
3159 tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
3160 struct colour_palette *palette, u_int bg, struct hyperlinks *hl)
3162 struct grid_cell gc;
3164 memcpy(&gc, &grid_default_cell, sizeof gc);
3165 gc.bg = bg;
3166 tty_attributes(tty, &gc, defaults, palette, hl);
3169 static void
3170 tty_clipboard_query_callback(__unused int fd, __unused short events, void *data)
3172 struct tty *tty = data;
3173 struct client *c = tty->client;
3175 c->flags &= ~CLIENT_CLIPBOARDBUFFER;
3176 free(c->clipboard_panes);
3177 c->clipboard_panes = NULL;
3178 c->clipboard_npanes = 0;
3180 tty->flags &= ~TTY_OSC52QUERY;
3183 void
3184 tty_clipboard_query(struct tty *tty)
3186 struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT };
3188 if ((~tty->flags & TTY_STARTED) || (tty->flags & TTY_OSC52QUERY))
3189 return;
3190 tty_putcode_ss(tty, TTYC_MS, "", "?");
3192 tty->flags |= TTY_OSC52QUERY;
3193 evtimer_set(&tty->clipboard_timer, tty_clipboard_query_callback, tty);
3194 evtimer_add(&tty->clipboard_timer, &tv);