Merge branch 'obsd-master'
[tmux.git] / tty.c
blob2c9b72998c034459877f1f15bc124714f61d6149
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);
1370 /* UTF-8 terminal and a UTF-8 character - fine. */
1371 if (tty->client->flags & CLIENT_UTF8)
1372 return (gc);
1373 memcpy(&new, gc, sizeof new);
1375 /* See if this can be mapped to an ACS character. */
1376 c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size);
1377 if (c != -1) {
1378 utf8_set(&new.data, c);
1379 new.attr |= GRID_ATTR_CHARSET;
1380 return (&new);
1383 /* Replace by the right number of underscores. */
1384 new.data.size = gc->data.width;
1385 if (new.data.size > UTF8_SIZE)
1386 new.data.size = UTF8_SIZE;
1387 memset(new.data.data, '_', new.data.size);
1388 return (&new);
1392 * Check if a single character is obstructed by the overlay and return a
1393 * boolean.
1395 static int
1396 tty_check_overlay(struct tty *tty, u_int px, u_int py)
1398 struct overlay_ranges r;
1401 * A unit width range will always return nx[2] == 0 from a check, even
1402 * with multiple overlays, so it's sufficient to check just the first
1403 * two entries.
1405 tty_check_overlay_range(tty, px, py, 1, &r);
1406 if (r.nx[0] + r.nx[1] == 0)
1407 return (0);
1408 return (1);
1411 /* Return parts of the input range which are visible. */
1412 static void
1413 tty_check_overlay_range(struct tty *tty, u_int px, u_int py, u_int nx,
1414 struct overlay_ranges *r)
1416 struct client *c = tty->client;
1418 if (c->overlay_check == NULL) {
1419 r->px[0] = px;
1420 r->nx[0] = nx;
1421 r->px[1] = 0;
1422 r->nx[1] = 0;
1423 r->px[2] = 0;
1424 r->nx[2] = 0;
1425 return;
1428 c->overlay_check(c, c->overlay_data, px, py, nx, r);
1431 void
1432 tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx,
1433 u_int atx, u_int aty, const struct grid_cell *defaults,
1434 struct colour_palette *palette)
1436 struct grid *gd = s->grid;
1437 struct grid_cell gc, last;
1438 const struct grid_cell *gcp;
1439 struct grid_line *gl;
1440 struct client *c = tty->client;
1441 struct overlay_ranges r;
1442 u_int i, j, ux, sx, width, hidden, eux, nxx;
1443 u_int cellsize;
1444 int flags, cleared = 0, wrapped = 0;
1445 char buf[512];
1446 size_t len;
1448 log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
1449 px, py, nx, atx, aty);
1450 log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg,
1451 defaults->bg);
1454 * py is the line in the screen to draw.
1455 * px is the start x and nx is the width to draw.
1456 * atx,aty is the line on the terminal to draw it.
1459 flags = (tty->flags & TTY_NOCURSOR);
1460 tty->flags |= TTY_NOCURSOR;
1461 tty_update_mode(tty, tty->mode, s);
1463 tty_region_off(tty);
1464 tty_margin_off(tty);
1467 * Clamp the width to cellsize - note this is not cellused, because
1468 * there may be empty background cells after it (from BCE).
1470 sx = screen_size_x(s);
1471 if (nx > sx)
1472 nx = sx;
1473 cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
1474 if (sx > cellsize)
1475 sx = cellsize;
1476 if (sx > tty->sx)
1477 sx = tty->sx;
1478 if (sx > nx)
1479 sx = nx;
1480 ux = 0;
1482 if (py == 0)
1483 gl = NULL;
1484 else
1485 gl = grid_get_line(gd, gd->hsize + py - 1);
1486 if (gl == NULL ||
1487 (~gl->flags & GRID_LINE_WRAPPED) ||
1488 atx != 0 ||
1489 tty->cx < tty->sx ||
1490 nx < tty->sx) {
1491 if (nx < tty->sx &&
1492 atx == 0 &&
1493 px + sx != nx &&
1494 tty_term_has(tty->term, TTYC_EL1) &&
1495 !tty_fake_bce(tty, defaults, 8) &&
1496 c->overlay_check == NULL) {
1497 tty_default_attributes(tty, defaults, palette, 8,
1498 s->hyperlinks);
1499 tty_cursor(tty, nx - 1, aty);
1500 tty_putcode(tty, TTYC_EL1);
1501 cleared = 1;
1503 } else {
1504 log_debug("%s: wrapped line %u", __func__, aty);
1505 wrapped = 1;
1508 memcpy(&last, &grid_default_cell, sizeof last);
1509 len = 0;
1510 width = 0;
1512 for (i = 0; i < sx; i++) {
1513 grid_view_get_cell(gd, px + i, py, &gc);
1514 gcp = tty_check_codeset(tty, &gc);
1515 if (len != 0 &&
1516 (!tty_check_overlay(tty, atx + ux + width, aty) ||
1517 (gcp->attr & GRID_ATTR_CHARSET) ||
1518 gcp->flags != last.flags ||
1519 gcp->attr != last.attr ||
1520 gcp->fg != last.fg ||
1521 gcp->bg != last.bg ||
1522 gcp->us != last.us ||
1523 gcp->link != last.link ||
1524 ux + width + gcp->data.width > nx ||
1525 (sizeof buf) - len < gcp->data.size)) {
1526 tty_attributes(tty, &last, defaults, palette,
1527 s->hyperlinks);
1528 if (last.flags & GRID_FLAG_CLEARED) {
1529 log_debug("%s: %zu cleared", __func__, len);
1530 tty_clear_line(tty, defaults, aty, atx + ux,
1531 width, last.bg);
1532 } else {
1533 if (!wrapped || atx != 0 || ux != 0)
1534 tty_cursor(tty, atx + ux, aty);
1535 tty_putn(tty, buf, len, width);
1537 ux += width;
1539 len = 0;
1540 width = 0;
1541 wrapped = 0;
1544 if (gcp->flags & GRID_FLAG_SELECTED)
1545 screen_select_cell(s, &last, gcp);
1546 else
1547 memcpy(&last, gcp, sizeof last);
1549 tty_check_overlay_range(tty, atx + ux, aty, gcp->data.width,
1550 &r);
1551 hidden = 0;
1552 for (j = 0; j < OVERLAY_MAX_RANGES; j++)
1553 hidden += r.nx[j];
1554 hidden = gcp->data.width - hidden;
1555 if (hidden != 0 && hidden == gcp->data.width) {
1556 if (~gcp->flags & GRID_FLAG_PADDING)
1557 ux += gcp->data.width;
1558 } else if (hidden != 0 || ux + gcp->data.width > nx) {
1559 if (~gcp->flags & GRID_FLAG_PADDING) {
1560 tty_attributes(tty, &last, defaults, palette,
1561 s->hyperlinks);
1562 for (j = 0; j < OVERLAY_MAX_RANGES; j++) {
1563 if (r.nx[j] == 0)
1564 continue;
1565 /* Effective width drawn so far. */
1566 eux = r.px[j] - atx;
1567 if (eux < nx) {
1568 tty_cursor(tty, r.px[j], aty);
1569 nxx = nx - eux;
1570 if (r.nx[j] > nxx)
1571 r.nx[j] = nxx;
1572 tty_repeat_space(tty, r.nx[j]);
1573 ux = eux + r.nx[j];
1577 } else if (gcp->attr & GRID_ATTR_CHARSET) {
1578 tty_attributes(tty, &last, defaults, palette,
1579 s->hyperlinks);
1580 tty_cursor(tty, atx + ux, aty);
1581 for (j = 0; j < gcp->data.size; j++)
1582 tty_putc(tty, gcp->data.data[j]);
1583 ux += gcp->data.width;
1584 } else if (~gcp->flags & GRID_FLAG_PADDING) {
1585 memcpy(buf + len, gcp->data.data, gcp->data.size);
1586 len += gcp->data.size;
1587 width += gcp->data.width;
1590 if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1591 tty_attributes(tty, &last, defaults, palette, s->hyperlinks);
1592 if (last.flags & GRID_FLAG_CLEARED) {
1593 log_debug("%s: %zu cleared (end)", __func__, len);
1594 tty_clear_line(tty, defaults, aty, atx + ux, width,
1595 last.bg);
1596 } else {
1597 if (!wrapped || atx != 0 || ux != 0)
1598 tty_cursor(tty, atx + ux, aty);
1599 tty_putn(tty, buf, len, width);
1601 ux += width;
1604 if (!cleared && ux < nx) {
1605 log_debug("%s: %u to end of line (%zu cleared)", __func__,
1606 nx - ux, len);
1607 tty_default_attributes(tty, defaults, palette, 8,
1608 s->hyperlinks);
1609 tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1612 tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1613 tty_update_mode(tty, tty->mode, s);
1616 #ifdef ENABLE_SIXEL
1617 /* Update context for client. */
1618 static int
1619 tty_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
1621 struct window_pane *wp = ttyctx->arg;
1623 if (c->session->curw->window != wp->window)
1624 return (0);
1625 if (wp->layout_cell == NULL)
1626 return (0);
1628 /* Set the properties relevant to the current client. */
1629 ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy,
1630 &ttyctx->wsx, &ttyctx->wsy);
1632 ttyctx->yoff = ttyctx->ryoff = wp->yoff;
1633 if (status_at_line(c) == 0)
1634 ttyctx->yoff += status_line_size(c);
1636 return (1);
1639 void
1640 tty_draw_images(struct client *c, struct window_pane *wp, struct screen *s)
1642 struct image *im;
1643 struct tty_ctx ttyctx;
1645 TAILQ_FOREACH(im, &s->images, entry) {
1646 memset(&ttyctx, 0, sizeof ttyctx);
1648 /* Set the client independent properties. */
1649 ttyctx.ocx = im->px;
1650 ttyctx.ocy = im->py;
1652 ttyctx.orlower = s->rlower;
1653 ttyctx.orupper = s->rupper;
1655 ttyctx.xoff = ttyctx.rxoff = wp->xoff;
1656 ttyctx.sx = wp->sx;
1657 ttyctx.sy = wp->sy;
1659 ttyctx.ptr = im;
1660 ttyctx.arg = wp;
1661 ttyctx.set_client_cb = tty_set_client_cb;
1662 ttyctx.allow_invisible_panes = 1;
1663 tty_write_one(tty_cmd_sixelimage, c, &ttyctx);
1666 #endif
1668 void
1669 tty_sync_start(struct tty *tty)
1671 if (tty->flags & TTY_BLOCK)
1672 return;
1673 if (tty->flags & TTY_SYNCING)
1674 return;
1675 tty->flags |= TTY_SYNCING;
1677 if (tty_term_has(tty->term, TTYC_SYNC)) {
1678 log_debug("%s sync start", tty->client->name);
1679 tty_putcode_i(tty, TTYC_SYNC, 1);
1683 void
1684 tty_sync_end(struct tty *tty)
1686 if (tty->flags & TTY_BLOCK)
1687 return;
1688 if (~tty->flags & TTY_SYNCING)
1689 return;
1690 tty->flags &= ~TTY_SYNCING;
1692 if (tty_term_has(tty->term, TTYC_SYNC)) {
1693 log_debug("%s sync end", tty->client->name);
1694 tty_putcode_i(tty, TTYC_SYNC, 2);
1698 static int
1699 tty_client_ready(const struct tty_ctx *ctx, struct client *c)
1701 if (c->session == NULL || c->tty.term == NULL)
1702 return (0);
1703 if (c->flags & CLIENT_SUSPENDED)
1704 return (0);
1707 * If invisible panes are allowed (used for passthrough), don't care if
1708 * redrawing or frozen.
1710 if (ctx->allow_invisible_panes)
1711 return (1);
1713 if (c->flags & CLIENT_REDRAWWINDOW)
1714 return (0);
1715 if (c->tty.flags & TTY_FREEZE)
1716 return (0);
1717 return (1);
1720 void
1721 tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1722 struct tty_ctx *ctx)
1724 struct client *c;
1725 int state;
1727 if (ctx->set_client_cb == NULL)
1728 return;
1729 TAILQ_FOREACH(c, &clients, entry) {
1730 if (tty_client_ready(ctx, c)) {
1731 state = ctx->set_client_cb(ctx, c);
1732 if (state == -1)
1733 break;
1734 if (state == 0)
1735 continue;
1736 cmdfn(&c->tty, ctx);
1741 #ifdef ENABLE_SIXEL
1742 /* Only write to the incoming tty instead of every client. */
1743 static void
1744 tty_write_one(void (*cmdfn)(struct tty *, const struct tty_ctx *),
1745 struct client *c, struct tty_ctx *ctx)
1747 if (ctx->set_client_cb == NULL)
1748 return;
1749 if ((ctx->set_client_cb(ctx, c)) == 1)
1750 cmdfn(&c->tty, ctx);
1752 #endif
1754 void
1755 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1757 struct client *c = tty->client;
1759 if (ctx->bigger ||
1760 !tty_full_width(tty, ctx) ||
1761 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1762 (!tty_term_has(tty->term, TTYC_ICH) &&
1763 !tty_term_has(tty->term, TTYC_ICH1)) ||
1764 c->overlay_check != NULL) {
1765 tty_draw_pane(tty, ctx, ctx->ocy);
1766 return;
1769 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1770 ctx->s->hyperlinks);
1772 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1774 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1777 void
1778 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1780 struct client *c = tty->client;
1782 if (ctx->bigger ||
1783 !tty_full_width(tty, ctx) ||
1784 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1785 (!tty_term_has(tty->term, TTYC_DCH) &&
1786 !tty_term_has(tty->term, TTYC_DCH1)) ||
1787 c->overlay_check != NULL) {
1788 tty_draw_pane(tty, ctx, ctx->ocy);
1789 return;
1792 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1793 ctx->s->hyperlinks);
1795 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1797 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1800 void
1801 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
1803 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1804 ctx->s->hyperlinks);
1806 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1809 void
1810 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1812 struct client *c = tty->client;
1814 if (ctx->bigger ||
1815 !tty_full_width(tty, ctx) ||
1816 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1817 !tty_term_has(tty->term, TTYC_CSR) ||
1818 !tty_term_has(tty->term, TTYC_IL1) ||
1819 ctx->sx == 1 ||
1820 ctx->sy == 1 ||
1821 c->overlay_check != NULL) {
1822 tty_redraw_region(tty, ctx);
1823 return;
1826 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1827 ctx->s->hyperlinks);
1829 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1830 tty_margin_off(tty);
1831 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1833 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1834 tty->cx = tty->cy = UINT_MAX;
1837 void
1838 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1840 struct client *c = tty->client;
1842 if (ctx->bigger ||
1843 !tty_full_width(tty, ctx) ||
1844 tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1845 !tty_term_has(tty->term, TTYC_CSR) ||
1846 !tty_term_has(tty->term, TTYC_DL1) ||
1847 ctx->sx == 1 ||
1848 ctx->sy == 1 ||
1849 c->overlay_check != NULL) {
1850 tty_redraw_region(tty, ctx);
1851 return;
1854 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1855 ctx->s->hyperlinks);
1857 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1858 tty_margin_off(tty);
1859 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1861 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1862 tty->cx = tty->cy = UINT_MAX;
1865 void
1866 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1868 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1869 ctx->s->hyperlinks);
1871 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1874 void
1875 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1877 u_int nx = ctx->sx - ctx->ocx;
1879 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1880 ctx->s->hyperlinks);
1882 tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1885 void
1886 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1888 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1889 ctx->s->hyperlinks);
1891 tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1894 void
1895 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1897 struct client *c = tty->client;
1899 if (ctx->ocy != ctx->orupper)
1900 return;
1902 if (ctx->bigger ||
1903 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1904 tty_fake_bce(tty, &ctx->defaults, 8) ||
1905 !tty_term_has(tty->term, TTYC_CSR) ||
1906 (!tty_term_has(tty->term, TTYC_RI) &&
1907 !tty_term_has(tty->term, TTYC_RIN)) ||
1908 ctx->sx == 1 ||
1909 ctx->sy == 1 ||
1910 c->overlay_check != NULL) {
1911 tty_redraw_region(tty, ctx);
1912 return;
1915 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1916 ctx->s->hyperlinks);
1918 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1919 tty_margin_pane(tty, ctx);
1920 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1922 if (tty_term_has(tty->term, TTYC_RI))
1923 tty_putcode(tty, TTYC_RI);
1924 else
1925 tty_putcode_i(tty, TTYC_RIN, 1);
1928 void
1929 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1931 struct client *c = tty->client;
1933 if (ctx->ocy != ctx->orlower)
1934 return;
1936 if (ctx->bigger ||
1937 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1938 tty_fake_bce(tty, &ctx->defaults, 8) ||
1939 !tty_term_has(tty->term, TTYC_CSR) ||
1940 ctx->sx == 1 ||
1941 ctx->sy == 1 ||
1942 c->overlay_check != NULL) {
1943 tty_redraw_region(tty, ctx);
1944 return;
1947 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1948 ctx->s->hyperlinks);
1950 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1951 tty_margin_pane(tty, ctx);
1954 * If we want to wrap a pane while using margins, the cursor needs to
1955 * be exactly on the right of the region. If the cursor is entirely off
1956 * the edge - move it back to the right. Some terminals are funny about
1957 * this and insert extra spaces, so only use the right if margins are
1958 * enabled.
1960 if (ctx->xoff + ctx->ocx > tty->rright) {
1961 if (!tty_use_margin(tty))
1962 tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
1963 else
1964 tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1965 } else
1966 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1968 tty_putc(tty, '\n');
1971 void
1972 tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
1974 struct client *c = tty->client;
1975 u_int i;
1977 if (ctx->bigger ||
1978 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1979 tty_fake_bce(tty, &ctx->defaults, 8) ||
1980 !tty_term_has(tty->term, TTYC_CSR) ||
1981 ctx->sx == 1 ||
1982 ctx->sy == 1 ||
1983 c->overlay_check != NULL) {
1984 tty_redraw_region(tty, ctx);
1985 return;
1988 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
1989 ctx->s->hyperlinks);
1991 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1992 tty_margin_pane(tty, ctx);
1994 if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1995 if (!tty_use_margin(tty))
1996 tty_cursor(tty, 0, tty->rlower);
1997 else
1998 tty_cursor(tty, tty->rright, tty->rlower);
1999 for (i = 0; i < ctx->num; i++)
2000 tty_putc(tty, '\n');
2001 } else {
2002 if (tty->cy == UINT_MAX)
2003 tty_cursor(tty, 0, 0);
2004 else
2005 tty_cursor(tty, 0, tty->cy);
2006 tty_putcode_i(tty, TTYC_INDN, ctx->num);
2010 void
2011 tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
2013 u_int i;
2014 struct client *c = tty->client;
2016 if (ctx->bigger ||
2017 (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
2018 tty_fake_bce(tty, &ctx->defaults, 8) ||
2019 !tty_term_has(tty->term, TTYC_CSR) ||
2020 (!tty_term_has(tty->term, TTYC_RI) &&
2021 !tty_term_has(tty->term, TTYC_RIN)) ||
2022 ctx->sx == 1 ||
2023 ctx->sy == 1 ||
2024 c->overlay_check != NULL) {
2025 tty_redraw_region(tty, ctx);
2026 return;
2029 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2030 ctx->s->hyperlinks);
2032 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
2033 tty_margin_pane(tty, ctx);
2034 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
2036 if (tty_term_has(tty->term, TTYC_RIN))
2037 tty_putcode_i(tty, TTYC_RIN, ctx->num);
2038 else {
2039 for (i = 0; i < ctx->num; i++)
2040 tty_putcode(tty, TTYC_RI);
2044 void
2045 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
2047 u_int px, py, nx, ny;
2049 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2050 ctx->s->hyperlinks);
2052 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2053 tty_margin_off(tty);
2055 px = 0;
2056 nx = ctx->sx;
2057 py = ctx->ocy + 1;
2058 ny = ctx->sy - ctx->ocy - 1;
2060 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
2062 px = ctx->ocx;
2063 nx = ctx->sx - ctx->ocx;
2064 py = ctx->ocy;
2066 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
2069 void
2070 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
2072 u_int px, py, nx, ny;
2074 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2075 ctx->s->hyperlinks);
2077 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2078 tty_margin_off(tty);
2080 px = 0;
2081 nx = ctx->sx;
2082 py = 0;
2083 ny = ctx->ocy;
2085 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
2087 px = 0;
2088 nx = ctx->ocx + 1;
2089 py = ctx->ocy;
2091 tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
2094 void
2095 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
2097 u_int px, py, nx, ny;
2099 tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg,
2100 ctx->s->hyperlinks);
2102 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2103 tty_margin_off(tty);
2105 px = 0;
2106 nx = ctx->sx;
2107 py = 0;
2108 ny = ctx->sy;
2110 tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
2113 void
2114 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
2116 u_int i, j;
2118 if (ctx->bigger) {
2119 ctx->redraw_cb(ctx);
2120 return;
2123 tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette,
2124 ctx->s->hyperlinks);
2126 tty_region_pane(tty, ctx, 0, ctx->sy - 1);
2127 tty_margin_off(tty);
2129 for (j = 0; j < ctx->sy; j++) {
2130 tty_cursor_pane(tty, ctx, 0, j);
2131 for (i = 0; i < ctx->sx; i++)
2132 tty_putc(tty, 'E');
2136 void
2137 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
2139 const struct grid_cell *gcp = ctx->cell;
2140 struct screen *s = ctx->s;
2141 struct overlay_ranges r;
2142 u_int px, py, i, vis = 0;
2144 px = ctx->xoff + ctx->ocx - ctx->wox;
2145 py = ctx->yoff + ctx->ocy - ctx->woy;
2146 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
2147 (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
2148 return;
2150 /* Handle partially obstructed wide characters. */
2151 if (gcp->data.width > 1) {
2152 tty_check_overlay_range(tty, px, py, gcp->data.width, &r);
2153 for (i = 0; i < OVERLAY_MAX_RANGES; i++)
2154 vis += r.nx[i];
2155 if (vis < gcp->data.width) {
2156 tty_draw_line(tty, s, s->cx, s->cy, gcp->data.width,
2157 px, py, &ctx->defaults, ctx->palette);
2158 return;
2162 if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
2163 ctx->ocy == ctx->orlower &&
2164 tty_full_width(tty, ctx))
2165 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
2167 tty_margin_off(tty);
2168 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2170 tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette,
2171 ctx->s->hyperlinks);
2173 if (ctx->num == 1)
2174 tty_invalidate(tty);
2177 void
2178 tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
2180 struct overlay_ranges r;
2181 u_int i, px, py, cx;
2182 char *cp = ctx->ptr;
2184 if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
2185 return;
2187 if (ctx->bigger &&
2188 (ctx->xoff + ctx->ocx < ctx->wox ||
2189 ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
2190 if (!ctx->wrapped ||
2191 !tty_full_width(tty, ctx) ||
2192 (tty->term->flags & TERM_NOAM) ||
2193 ctx->xoff + ctx->ocx != 0 ||
2194 ctx->yoff + ctx->ocy != tty->cy + 1 ||
2195 tty->cx < tty->sx ||
2196 tty->cy == tty->rlower)
2197 tty_draw_pane(tty, ctx, ctx->ocy);
2198 else
2199 ctx->redraw_cb(ctx);
2200 return;
2203 tty_margin_off(tty);
2204 tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
2205 tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette, ctx->s->hyperlinks);
2207 /* Get tty position from pane position for overlay check. */
2208 px = ctx->xoff + ctx->ocx - ctx->wox;
2209 py = ctx->yoff + ctx->ocy - ctx->woy;
2211 tty_check_overlay_range(tty, px, py, ctx->num, &r);
2212 for (i = 0; i < OVERLAY_MAX_RANGES; i++) {
2213 if (r.nx[i] == 0)
2214 continue;
2215 /* Convert back to pane position for printing. */
2216 cx = r.px[i] - ctx->xoff + ctx->wox;
2217 tty_cursor_pane_unless_wrap(tty, ctx, cx, ctx->ocy);
2218 tty_putn(tty, cp + r.px[i] - px, r.nx[i], r.nx[i]);
2222 void
2223 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
2225 tty_set_selection(tty, ctx->ptr2, ctx->ptr, ctx->num);
2228 void
2229 tty_set_selection(struct tty *tty, const char *flags, const char *buf,
2230 size_t len)
2232 char *encoded;
2233 size_t size;
2235 if (~tty->flags & TTY_STARTED)
2236 return;
2237 if (!tty_term_has(tty->term, TTYC_MS))
2238 return;
2240 size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
2241 encoded = xmalloc(size);
2243 b64_ntop(buf, len, encoded, size);
2244 tty->flags |= TTY_NOBLOCK;
2245 tty_putcode_ss(tty, TTYC_MS, flags, encoded);
2247 free(encoded);
2250 void
2251 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
2253 tty->flags |= TTY_NOBLOCK;
2254 tty_add(tty, ctx->ptr, ctx->num);
2255 tty_invalidate(tty);
2258 #ifdef ENABLE_SIXEL
2259 void
2260 tty_cmd_sixelimage(struct tty *tty, const struct tty_ctx *ctx)
2262 struct image *im = ctx->ptr;
2263 struct sixel_image *si = im->data;
2264 struct sixel_image *new;
2265 char *data;
2266 size_t size;
2267 u_int cx = ctx->ocx, cy = ctx->ocy, sx, sy;
2268 u_int i, j, x, y, rx, ry;
2269 int fallback = 0;
2271 if ((~tty->term->flags & TERM_SIXEL) &&
2272 !tty_term_has(tty->term, TTYC_SXL))
2273 fallback = 1;
2274 if (tty->xpixel == 0 || tty->ypixel == 0)
2275 fallback = 1;
2277 sixel_size_in_cells(si, &sx, &sy);
2278 log_debug("%s: image is %ux%u", __func__, sx, sy);
2279 if (!tty_clamp_area(tty, ctx, cx, cy, sx, sy, &i, &j, &x, &y, &rx, &ry))
2280 return;
2281 log_debug("%s: clamping to %u,%u-%u,%u", __func__, i, j, rx, ry);
2283 if (fallback == 1) {
2284 data = xstrdup(im->fallback);
2285 size = strlen(data);
2286 } else {
2287 new = sixel_scale(si, tty->xpixel, tty->ypixel, i, j, rx, ry, 0);
2288 if (new == NULL)
2289 return;
2291 data = sixel_print(new, si, &size);
2293 if (data != NULL) {
2294 log_debug("%s: %zu bytes: %s", __func__, size, data);
2295 tty_region_off(tty);
2296 tty_margin_off(tty);
2297 tty_cursor(tty, x, y);
2299 tty->flags |= TTY_NOBLOCK;
2300 tty_add(tty, data, size);
2301 tty_invalidate(tty);
2302 free(data);
2305 if (fallback == 0)
2306 sixel_free(new);
2308 #endif
2310 void
2311 tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx)
2313 if (ctx->num == 0x11) {
2315 * This is an overlay and a command that moves the cursor so
2316 * start synchronized updates.
2318 tty_sync_start(tty);
2319 } else if (~ctx->num & 0x10) {
2321 * This is a pane. If there is an overlay, always start;
2322 * otherwise, only if requested.
2324 if (ctx->num || tty->client->overlay_draw != NULL)
2325 tty_sync_start(tty);
2329 void
2330 tty_cell(struct tty *tty, const struct grid_cell *gc,
2331 const struct grid_cell *defaults, struct colour_palette *palette,
2332 struct hyperlinks *hl)
2334 const struct grid_cell *gcp;
2336 /* Skip last character if terminal is stupid. */
2337 if ((tty->term->flags & TERM_NOAM) &&
2338 tty->cy == tty->sy - 1 &&
2339 tty->cx == tty->sx - 1)
2340 return;
2342 /* If this is a padding character, do nothing. */
2343 if (gc->flags & GRID_FLAG_PADDING)
2344 return;
2346 /* Check the output codeset and apply attributes. */
2347 gcp = tty_check_codeset(tty, gc);
2348 tty_attributes(tty, gcp, defaults, palette, hl);
2350 /* If it is a single character, write with putc to handle ACS. */
2351 if (gcp->data.size == 1) {
2352 tty_attributes(tty, gcp, defaults, palette, hl);
2353 if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
2354 return;
2355 tty_putc(tty, *gcp->data.data);
2356 return;
2359 /* Write the data. */
2360 tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
2363 void
2364 tty_reset(struct tty *tty)
2366 struct grid_cell *gc = &tty->cell;
2368 if (!grid_cells_equal(gc, &grid_default_cell)) {
2369 if (gc->link != 0)
2370 tty_putcode_ss(tty, TTYC_HLS, "", "");
2371 if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2372 tty_putcode(tty, TTYC_RMACS);
2373 tty_putcode(tty, TTYC_SGR0);
2374 memcpy(gc, &grid_default_cell, sizeof *gc);
2376 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2379 void
2380 tty_invalidate(struct tty *tty)
2382 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
2383 memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
2385 tty->cx = tty->cy = UINT_MAX;
2386 tty->rupper = tty->rleft = UINT_MAX;
2387 tty->rlower = tty->rright = UINT_MAX;
2389 if (tty->flags & TTY_STARTED) {
2390 if (tty_use_margin(tty))
2391 tty_putcode(tty, TTYC_ENMG);
2392 tty_putcode(tty, TTYC_SGR0);
2394 tty->mode = ALL_MODES;
2395 tty_update_mode(tty, MODE_CURSOR, NULL);
2397 tty_cursor(tty, 0, 0);
2398 tty_region_off(tty);
2399 tty_margin_off(tty);
2400 } else
2401 tty->mode = MODE_CURSOR;
2404 /* Turn off margin. */
2405 void
2406 tty_region_off(struct tty *tty)
2408 tty_region(tty, 0, tty->sy - 1);
2411 /* Set region inside pane. */
2412 static void
2413 tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
2414 u_int rlower)
2416 tty_region(tty, ctx->yoff + rupper - ctx->woy,
2417 ctx->yoff + rlower - ctx->woy);
2420 /* Set region at absolute position. */
2421 static void
2422 tty_region(struct tty *tty, u_int rupper, u_int rlower)
2424 if (tty->rlower == rlower && tty->rupper == rupper)
2425 return;
2426 if (!tty_term_has(tty->term, TTYC_CSR))
2427 return;
2429 tty->rupper = rupper;
2430 tty->rlower = rlower;
2433 * Some terminals (such as PuTTY) do not correctly reset the cursor to
2434 * 0,0 if it is beyond the last column (they do not reset their wrap
2435 * flag so further output causes a line feed). As a workaround, do an
2436 * explicit move to 0 first.
2438 if (tty->cx >= tty->sx) {
2439 if (tty->cy == UINT_MAX)
2440 tty_cursor(tty, 0, 0);
2441 else
2442 tty_cursor(tty, 0, tty->cy);
2445 tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower);
2446 tty->cx = tty->cy = UINT_MAX;
2449 /* Turn off margin. */
2450 void
2451 tty_margin_off(struct tty *tty)
2453 tty_margin(tty, 0, tty->sx - 1);
2456 /* Set margin inside pane. */
2457 static void
2458 tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
2460 tty_margin(tty, ctx->xoff - ctx->wox,
2461 ctx->xoff + ctx->sx - 1 - ctx->wox);
2464 /* Set margin at absolute position. */
2465 static void
2466 tty_margin(struct tty *tty, u_int rleft, u_int rright)
2468 if (!tty_use_margin(tty))
2469 return;
2470 if (tty->rleft == rleft && tty->rright == rright)
2471 return;
2473 tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower);
2475 tty->rleft = rleft;
2476 tty->rright = rright;
2478 if (rleft == 0 && rright == tty->sx - 1)
2479 tty_putcode(tty, TTYC_CLMG);
2480 else
2481 tty_putcode_ii(tty, TTYC_CMG, rleft, rright);
2482 tty->cx = tty->cy = UINT_MAX;
2486 * Move the cursor, unless it would wrap itself when the next character is
2487 * printed.
2489 static void
2490 tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
2491 u_int cx, u_int cy)
2493 if (!ctx->wrapped ||
2494 !tty_full_width(tty, ctx) ||
2495 (tty->term->flags & TERM_NOAM) ||
2496 ctx->xoff + cx != 0 ||
2497 ctx->yoff + cy != tty->cy + 1 ||
2498 tty->cx < tty->sx ||
2499 tty->cy == tty->rlower)
2500 tty_cursor_pane(tty, ctx, cx, cy);
2501 else
2502 log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
2505 /* Move cursor inside pane. */
2506 static void
2507 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
2509 tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
2512 /* Move cursor to absolute position. */
2513 void
2514 tty_cursor(struct tty *tty, u_int cx, u_int cy)
2516 struct tty_term *term = tty->term;
2517 u_int thisx, thisy;
2518 int change;
2520 if (tty->flags & TTY_BLOCK)
2521 return;
2523 thisx = tty->cx;
2524 thisy = tty->cy;
2527 * If in the automargin space, and want to be there, do not move.
2528 * Otherwise, force the cursor to be in range (and complain).
2530 if (cx == thisx && cy == thisy && cx == tty->sx)
2531 return;
2532 if (cx > tty->sx - 1) {
2533 log_debug("%s: x too big %u > %u", __func__, cx, tty->sx - 1);
2534 cx = tty->sx - 1;
2537 /* No change. */
2538 if (cx == thisx && cy == thisy)
2539 return;
2541 /* Currently at the very end of the line - use absolute movement. */
2542 if (thisx > tty->sx - 1)
2543 goto absolute;
2545 /* Move to home position (0, 0). */
2546 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
2547 tty_putcode(tty, TTYC_HOME);
2548 goto out;
2551 /* Zero on the next line. */
2552 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
2553 (!tty_use_margin(tty) || tty->rleft == 0)) {
2554 tty_putc(tty, '\r');
2555 tty_putc(tty, '\n');
2556 goto out;
2559 /* Moving column or row. */
2560 if (cy == thisy) {
2562 * Moving column only, row staying the same.
2565 /* To left edge. */
2566 if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
2567 tty_putc(tty, '\r');
2568 goto out;
2571 /* One to the left. */
2572 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
2573 tty_putcode(tty, TTYC_CUB1);
2574 goto out;
2577 /* One to the right. */
2578 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
2579 tty_putcode(tty, TTYC_CUF1);
2580 goto out;
2583 /* Calculate difference. */
2584 change = thisx - cx; /* +ve left, -ve right */
2587 * Use HPA if change is larger than absolute, otherwise move
2588 * the cursor with CUB/CUF.
2590 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
2591 tty_putcode_i(tty, TTYC_HPA, cx);
2592 goto out;
2593 } else if (change > 0 &&
2594 tty_term_has(term, TTYC_CUB) &&
2595 !tty_use_margin(tty)) {
2596 if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
2597 tty_putcode(tty, TTYC_CUB1);
2598 tty_putcode(tty, TTYC_CUB1);
2599 goto out;
2601 tty_putcode_i(tty, TTYC_CUB, change);
2602 goto out;
2603 } else if (change < 0 &&
2604 tty_term_has(term, TTYC_CUF) &&
2605 !tty_use_margin(tty)) {
2606 tty_putcode_i(tty, TTYC_CUF, -change);
2607 goto out;
2609 } else if (cx == thisx) {
2611 * Moving row only, column staying the same.
2614 /* One above. */
2615 if (thisy != tty->rupper &&
2616 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
2617 tty_putcode(tty, TTYC_CUU1);
2618 goto out;
2621 /* One below. */
2622 if (thisy != tty->rlower &&
2623 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
2624 tty_putcode(tty, TTYC_CUD1);
2625 goto out;
2628 /* Calculate difference. */
2629 change = thisy - cy; /* +ve up, -ve down */
2632 * Try to use VPA if change is larger than absolute or if this
2633 * change would cross the scroll region, otherwise use CUU/CUD.
2635 if ((u_int) abs(change) > cy ||
2636 (change < 0 && cy - change > tty->rlower) ||
2637 (change > 0 && cy - change < tty->rupper)) {
2638 if (tty_term_has(term, TTYC_VPA)) {
2639 tty_putcode_i(tty, TTYC_VPA, cy);
2640 goto out;
2642 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
2643 tty_putcode_i(tty, TTYC_CUU, change);
2644 goto out;
2645 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
2646 tty_putcode_i(tty, TTYC_CUD, -change);
2647 goto out;
2651 absolute:
2652 /* Absolute movement. */
2653 tty_putcode_ii(tty, TTYC_CUP, cy, cx);
2655 out:
2656 tty->cx = cx;
2657 tty->cy = cy;
2660 static void
2661 tty_hyperlink(struct tty *tty, const struct grid_cell *gc,
2662 struct hyperlinks *hl)
2664 const char *uri, *id;
2666 if (gc->link == tty->cell.link)
2667 return;
2668 tty->cell.link = gc->link;
2670 if (hl == NULL)
2671 return;
2673 if (gc->link == 0 || !hyperlinks_get(hl, gc->link, &uri, NULL, &id))
2674 tty_putcode_ss(tty, TTYC_HLS, "", "");
2675 else
2676 tty_putcode_ss(tty, TTYC_HLS, id, uri);
2679 void
2680 tty_attributes(struct tty *tty, const struct grid_cell *gc,
2681 const struct grid_cell *defaults, struct colour_palette *palette,
2682 struct hyperlinks *hl)
2684 struct grid_cell *tc = &tty->cell, gc2;
2685 int changed;
2687 /* Copy cell and update default colours. */
2688 memcpy(&gc2, gc, sizeof gc2);
2689 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2690 if (gc2.fg == 8)
2691 gc2.fg = defaults->fg;
2692 if (gc2.bg == 8)
2693 gc2.bg = defaults->bg;
2696 /* Ignore cell if it is the same as the last one. */
2697 if (gc2.attr == tty->last_cell.attr &&
2698 gc2.fg == tty->last_cell.fg &&
2699 gc2.bg == tty->last_cell.bg &&
2700 gc2.us == tty->last_cell.us &&
2701 gc2.link == tty->last_cell.link)
2702 return;
2705 * If no setab, try to use the reverse attribute as a best-effort for a
2706 * non-default background. This is a bit of a hack but it doesn't do
2707 * any serious harm and makes a couple of applications happier.
2709 if (!tty_term_has(tty->term, TTYC_SETAB)) {
2710 if (gc2.attr & GRID_ATTR_REVERSE) {
2711 if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
2712 gc2.attr &= ~GRID_ATTR_REVERSE;
2713 } else {
2714 if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
2715 gc2.attr |= GRID_ATTR_REVERSE;
2719 /* Fix up the colours if necessary. */
2720 tty_check_fg(tty, palette, &gc2);
2721 tty_check_bg(tty, palette, &gc2);
2722 tty_check_us(tty, palette, &gc2);
2725 * If any bits are being cleared or the underline colour is now default,
2726 * reset everything.
2728 if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
2729 tty_reset(tty);
2732 * Set the colours. This may call tty_reset() (so it comes next) and
2733 * may add to (NOT remove) the desired attributes.
2735 tty_colours(tty, &gc2);
2737 /* Filter out attribute bits already set. */
2738 changed = gc2.attr & ~tc->attr;
2739 tc->attr = gc2.attr;
2741 /* Set the attributes. */
2742 if (changed & GRID_ATTR_BRIGHT)
2743 tty_putcode(tty, TTYC_BOLD);
2744 if (changed & GRID_ATTR_DIM)
2745 tty_putcode(tty, TTYC_DIM);
2746 if (changed & GRID_ATTR_ITALICS)
2747 tty_set_italics(tty);
2748 if (changed & GRID_ATTR_ALL_UNDERSCORE) {
2749 if ((changed & GRID_ATTR_UNDERSCORE) ||
2750 !tty_term_has(tty->term, TTYC_SMULX))
2751 tty_putcode(tty, TTYC_SMUL);
2752 else if (changed & GRID_ATTR_UNDERSCORE_2)
2753 tty_putcode_i(tty, TTYC_SMULX, 2);
2754 else if (changed & GRID_ATTR_UNDERSCORE_3)
2755 tty_putcode_i(tty, TTYC_SMULX, 3);
2756 else if (changed & GRID_ATTR_UNDERSCORE_4)
2757 tty_putcode_i(tty, TTYC_SMULX, 4);
2758 else if (changed & GRID_ATTR_UNDERSCORE_5)
2759 tty_putcode_i(tty, TTYC_SMULX, 5);
2761 if (changed & GRID_ATTR_BLINK)
2762 tty_putcode(tty, TTYC_BLINK);
2763 if (changed & GRID_ATTR_REVERSE) {
2764 if (tty_term_has(tty->term, TTYC_REV))
2765 tty_putcode(tty, TTYC_REV);
2766 else if (tty_term_has(tty->term, TTYC_SMSO))
2767 tty_putcode(tty, TTYC_SMSO);
2769 if (changed & GRID_ATTR_HIDDEN)
2770 tty_putcode(tty, TTYC_INVIS);
2771 if (changed & GRID_ATTR_STRIKETHROUGH)
2772 tty_putcode(tty, TTYC_SMXX);
2773 if (changed & GRID_ATTR_OVERLINE)
2774 tty_putcode(tty, TTYC_SMOL);
2775 if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
2776 tty_putcode(tty, TTYC_SMACS);
2778 /* Set hyperlink if any. */
2779 tty_hyperlink(tty, gc, hl);
2781 memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
2784 static void
2785 tty_colours(struct tty *tty, const struct grid_cell *gc)
2787 struct grid_cell *tc = &tty->cell;
2789 /* No changes? Nothing is necessary. */
2790 if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
2791 return;
2794 * Is either the default colour? This is handled specially because the
2795 * best solution might be to reset both colours to default, in which
2796 * case if only one is default need to fall onward to set the other
2797 * colour.
2799 if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
2801 * If don't have AX, send sgr0. This resets both colours to default.
2802 * Otherwise, try to set the default colour only as needed.
2804 if (!tty_term_flag(tty->term, TTYC_AX))
2805 tty_reset(tty);
2806 else {
2807 if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
2808 tty_puts(tty, "\033[39m");
2809 tc->fg = gc->fg;
2811 if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
2812 tty_puts(tty, "\033[49m");
2813 tc->bg = gc->bg;
2818 /* Set the foreground colour. */
2819 if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
2820 tty_colours_fg(tty, gc);
2823 * Set the background colour. This must come after the foreground as
2824 * tty_colours_fg() can call tty_reset().
2826 if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
2827 tty_colours_bg(tty, gc);
2829 /* Set the underscore colour. */
2830 if (gc->us != tc->us)
2831 tty_colours_us(tty, gc);
2834 static void
2835 tty_check_fg(struct tty *tty, struct colour_palette *palette,
2836 struct grid_cell *gc)
2838 u_char r, g, b;
2839 u_int colours;
2840 int c;
2843 * Perform substitution if this pane has a palette. If the bright
2844 * attribute is set and Nobr is not present, use the bright entry in
2845 * the palette by changing to the aixterm colour
2847 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2848 c = gc->fg;
2849 if (c < 8 &&
2850 gc->attr & GRID_ATTR_BRIGHT &&
2851 !tty_term_has(tty->term, TTYC_NOBR))
2852 c += 90;
2853 if ((c = colour_palette_get(palette, c)) != -1)
2854 gc->fg = c;
2857 /* Is this a 24-bit colour? */
2858 if (gc->fg & COLOUR_FLAG_RGB) {
2859 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2860 if (tty->term->flags & TERM_RGBCOLOURS)
2861 return;
2862 colour_split_rgb(gc->fg, &r, &g, &b);
2863 gc->fg = colour_find_rgb(r, g, b);
2866 /* How many colours does this terminal have? */
2867 if (tty->term->flags & TERM_256COLOURS)
2868 colours = 256;
2869 else
2870 colours = tty_term_number(tty->term, TTYC_COLORS);
2872 /* Is this a 256-colour colour? */
2873 if (gc->fg & COLOUR_FLAG_256) {
2874 /* And not a 256 colour mode? */
2875 if (colours < 256) {
2876 gc->fg = colour_256to16(gc->fg);
2877 if (gc->fg & 8) {
2878 gc->fg &= 7;
2879 if (colours >= 16)
2880 gc->fg += 90;
2883 return;
2886 /* Is this an aixterm colour? */
2887 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
2888 gc->fg -= 90;
2889 gc->attr |= GRID_ATTR_BRIGHT;
2893 static void
2894 tty_check_bg(struct tty *tty, struct colour_palette *palette,
2895 struct grid_cell *gc)
2897 u_char r, g, b;
2898 u_int colours;
2899 int c;
2901 /* Perform substitution if this pane has a palette. */
2902 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2903 if ((c = colour_palette_get(palette, gc->bg)) != -1)
2904 gc->bg = c;
2907 /* Is this a 24-bit colour? */
2908 if (gc->bg & COLOUR_FLAG_RGB) {
2909 /* Not a 24-bit terminal? Translate to 256-colour palette. */
2910 if (tty->term->flags & TERM_RGBCOLOURS)
2911 return;
2912 colour_split_rgb(gc->bg, &r, &g, &b);
2913 gc->bg = colour_find_rgb(r, g, b);
2916 /* How many colours does this terminal have? */
2917 if (tty->term->flags & TERM_256COLOURS)
2918 colours = 256;
2919 else
2920 colours = tty_term_number(tty->term, TTYC_COLORS);
2922 /* Is this a 256-colour colour? */
2923 if (gc->bg & COLOUR_FLAG_256) {
2925 * And not a 256 colour mode? Translate to 16-colour
2926 * palette. Bold background doesn't exist portably, so just
2927 * discard the bold bit if set.
2929 if (colours < 256) {
2930 gc->bg = colour_256to16(gc->bg);
2931 if (gc->bg & 8) {
2932 gc->bg &= 7;
2933 if (colours >= 16)
2934 gc->bg += 90;
2937 return;
2940 /* Is this an aixterm colour? */
2941 if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
2942 gc->bg -= 90;
2945 static void
2946 tty_check_us(__unused struct tty *tty, struct colour_palette *palette,
2947 struct grid_cell *gc)
2949 int c;
2951 /* Perform substitution if this pane has a palette. */
2952 if (~gc->flags & GRID_FLAG_NOPALETTE) {
2953 if ((c = colour_palette_get(palette, gc->us)) != -1)
2954 gc->us = c;
2957 /* Convert underscore colour if only RGB can be supported. */
2958 if (!tty_term_has(tty->term, TTYC_SETULC1)) {
2959 if ((c = colour_force_rgb (gc->us)) == -1)
2960 gc->us = 8;
2961 else
2962 gc->us = c;
2966 static void
2967 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
2969 struct grid_cell *tc = &tty->cell;
2970 char s[32];
2973 * If the current colour is an aixterm bright colour and the new is not,
2974 * reset because some terminals do not clear bright correctly.
2976 if (tty->cell.fg >= 90 &&
2977 tty->cell.bg <= 97 &&
2978 (gc->fg < 90 || gc->fg > 97))
2979 tty_reset(tty);
2981 /* Is this a 24-bit or 256-colour colour? */
2982 if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
2983 if (tty_try_colour(tty, gc->fg, "38") == 0)
2984 goto save;
2985 /* Should not get here, already converted in tty_check_fg. */
2986 return;
2989 /* Is this an aixterm bright colour? */
2990 if (gc->fg >= 90 && gc->fg <= 97) {
2991 if (tty->term->flags & TERM_256COLOURS) {
2992 xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
2993 tty_puts(tty, s);
2994 } else
2995 tty_putcode_i(tty, TTYC_SETAF, gc->fg - 90 + 8);
2996 goto save;
2999 /* Otherwise set the foreground colour. */
3000 tty_putcode_i(tty, TTYC_SETAF, gc->fg);
3002 save:
3003 /* Save the new values in the terminal current cell. */
3004 tc->fg = gc->fg;
3007 static void
3008 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
3010 struct grid_cell *tc = &tty->cell;
3011 char s[32];
3013 /* Is this a 24-bit or 256-colour colour? */
3014 if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
3015 if (tty_try_colour(tty, gc->bg, "48") == 0)
3016 goto save;
3017 /* Should not get here, already converted in tty_check_bg. */
3018 return;
3021 /* Is this an aixterm bright colour? */
3022 if (gc->bg >= 90 && gc->bg <= 97) {
3023 if (tty->term->flags & TERM_256COLOURS) {
3024 xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
3025 tty_puts(tty, s);
3026 } else
3027 tty_putcode_i(tty, TTYC_SETAB, gc->bg - 90 + 8);
3028 goto save;
3031 /* Otherwise set the background colour. */
3032 tty_putcode_i(tty, TTYC_SETAB, gc->bg);
3034 save:
3035 /* Save the new values in the terminal current cell. */
3036 tc->bg = gc->bg;
3039 static void
3040 tty_colours_us(struct tty *tty, const struct grid_cell *gc)
3042 struct grid_cell *tc = &tty->cell;
3043 u_int c;
3044 u_char r, g, b;
3046 /* Clear underline colour. */
3047 if (COLOUR_DEFAULT(gc->us)) {
3048 tty_putcode(tty, TTYC_OL);
3049 goto save;
3053 * If this is not an RGB colour, use Setulc1 if it exists, otherwise
3054 * convert.
3056 if (~gc->us & COLOUR_FLAG_RGB) {
3057 c = gc->us;
3058 if ((~c & COLOUR_FLAG_256) && (c >= 90 && c <= 97))
3059 c -= 82;
3060 tty_putcode_i(tty, TTYC_SETULC1, c & ~COLOUR_FLAG_256);
3061 return;
3065 * Setulc and setal follows the ncurses(3) one argument "direct colour"
3066 * capability format. Calculate the colour value.
3068 colour_split_rgb(gc->us, &r, &g, &b);
3069 c = (65536 * r) + (256 * g) + b;
3072 * Write the colour. Only use setal if the RGB flag is set because the
3073 * non-RGB version may be wrong.
3075 if (tty_term_has(tty->term, TTYC_SETULC))
3076 tty_putcode_i(tty, TTYC_SETULC, c);
3077 else if (tty_term_has(tty->term, TTYC_SETAL) &&
3078 tty_term_has(tty->term, TTYC_RGB))
3079 tty_putcode_i(tty, TTYC_SETAL, c);
3081 save:
3082 /* Save the new values in the terminal current cell. */
3083 tc->us = gc->us;
3086 static int
3087 tty_try_colour(struct tty *tty, int colour, const char *type)
3089 u_char r, g, b;
3091 if (colour & COLOUR_FLAG_256) {
3092 if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
3093 tty_putcode_i(tty, TTYC_SETAF, colour & 0xff);
3094 else if (tty_term_has(tty->term, TTYC_SETAB))
3095 tty_putcode_i(tty, TTYC_SETAB, colour & 0xff);
3096 return (0);
3099 if (colour & COLOUR_FLAG_RGB) {
3100 colour_split_rgb(colour & 0xffffff, &r, &g, &b);
3101 if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
3102 tty_putcode_iii(tty, TTYC_SETRGBF, r, g, b);
3103 else if (tty_term_has(tty->term, TTYC_SETRGBB))
3104 tty_putcode_iii(tty, TTYC_SETRGBB, r, g, b);
3105 return (0);
3108 return (-1);
3111 static void
3112 tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
3114 memcpy(gc, &grid_default_cell, sizeof *gc);
3115 gc->fg = wp->palette.fg;
3116 gc->bg = wp->palette.bg;
3119 void
3120 tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
3122 struct options *oo = wp->options;
3123 struct format_tree *ft;
3125 memcpy(gc, &grid_default_cell, sizeof *gc);
3127 if (wp->flags & PANE_STYLECHANGED) {
3128 log_debug("%%%u: style changed", wp->id);
3129 wp->flags &= ~PANE_STYLECHANGED;
3131 ft = format_create(NULL, NULL, FORMAT_PANE|wp->id,
3132 FORMAT_NOJOBS);
3133 format_defaults(ft, NULL, NULL, NULL, wp);
3134 tty_window_default_style(&wp->cached_active_gc, wp);
3135 style_add(&wp->cached_active_gc, oo, "window-active-style", ft);
3136 tty_window_default_style(&wp->cached_gc, wp);
3137 style_add(&wp->cached_gc, oo, "window-style", ft);
3138 format_free(ft);
3141 if (gc->fg == 8) {
3142 if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
3143 gc->fg = wp->cached_active_gc.fg;
3144 else
3145 gc->fg = wp->cached_gc.fg;
3148 if (gc->bg == 8) {
3149 if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
3150 gc->bg = wp->cached_active_gc.bg;
3151 else
3152 gc->bg = wp->cached_gc.bg;
3156 static void
3157 tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
3158 struct colour_palette *palette, u_int bg, struct hyperlinks *hl)
3160 struct grid_cell gc;
3162 memcpy(&gc, &grid_default_cell, sizeof gc);
3163 gc.bg = bg;
3164 tty_attributes(tty, &gc, defaults, palette, hl);
3167 static void
3168 tty_clipboard_query_callback(__unused int fd, __unused short events, void *data)
3170 struct tty *tty = data;
3171 struct client *c = tty->client;
3173 c->flags &= ~CLIENT_CLIPBOARDBUFFER;
3174 free(c->clipboard_panes);
3175 c->clipboard_panes = NULL;
3176 c->clipboard_npanes = 0;
3178 tty->flags &= ~TTY_OSC52QUERY;
3181 void
3182 tty_clipboard_query(struct tty *tty)
3184 struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT };
3186 if ((~tty->flags & TTY_STARTED) || (tty->flags & TTY_OSC52QUERY))
3187 return;
3188 tty_putcode_ss(tty, TTYC_MS, "", "?");
3190 tty->flags |= TTY_OSC52QUERY;
3191 evtimer_set(&tty->clipboard_timer, tty_clipboard_query_callback, tty);
3192 evtimer_add(&tty->clipboard_timer, &tv);