Move the user-visible parts of all options (names, types, limit, default
[tmux-openbsd.git] / tty.c
blobf7698708750d3e2af2a34ce29ecb8605f5d9a60b
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <termios.h>
27 #include <unistd.h>
29 #include "tmux.h"
31 void tty_read_callback(struct bufferevent *, void *);
32 void tty_error_callback(struct bufferevent *, short, void *);
34 int tty_try_256(struct tty *, u_char, const char *);
35 int tty_try_88(struct tty *, u_char, const char *);
37 void tty_colours(struct tty *, const struct grid_cell *);
38 void tty_check_fg(struct tty *, struct grid_cell *);
39 void tty_check_bg(struct tty *, struct grid_cell *);
40 void tty_colours_fg(struct tty *, const struct grid_cell *);
41 void tty_colours_bg(struct tty *, const struct grid_cell *);
43 void tty_redraw_region(struct tty *, const struct tty_ctx *);
44 void tty_emulate_repeat(
45 struct tty *, enum tty_code_code, enum tty_code_code, u_int);
46 void tty_cell(struct tty *,
47 const struct grid_cell *, const struct grid_utf8 *);
49 #define tty_use_acs(tty) \
50 (tty_term_has(tty->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
52 void
53 tty_init(struct tty *tty, int fd, char *term)
55 char *path;
57 memset(tty, 0, sizeof *tty);
58 tty->log_fd = -1;
60 if (term == NULL || *term == '\0')
61 tty->termname = xstrdup("unknown");
62 else
63 tty->termname = xstrdup(term);
64 tty->fd = fd;
66 if ((path = ttyname(fd)) == NULL)
67 fatalx("ttyname failed");
68 tty->path = xstrdup(path);
70 tty->flags = 0;
71 tty->term_flags = 0;
74 int
75 tty_resize(struct tty *tty)
77 struct winsize ws;
78 u_int sx, sy;
80 if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
81 sx = ws.ws_col;
82 if (sx == 0)
83 sx = 80;
84 sy = ws.ws_row;
85 if (sy == 0)
86 sy = 24;
87 } else {
88 sx = 80;
89 sy = 24;
91 if (sx == tty->sx && sy == tty->sy)
92 return (0);
93 tty->sx = sx;
94 tty->sy = sy;
96 tty->cx = UINT_MAX;
97 tty->cy = UINT_MAX;
99 tty->rupper = UINT_MAX;
100 tty->rlower = UINT_MAX;
103 * If the terminal has been started, reset the actual scroll region and
104 * cursor position, as this may not have happened.
106 if (tty->flags & TTY_STARTED) {
107 tty_cursor(tty, 0, 0);
108 tty_region(tty, 0, tty->sy - 1);
111 return (1);
115 tty_open(struct tty *tty, const char *overrides, char **cause)
117 char out[64];
118 int fd;
120 if (debug_level > 3) {
121 xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
122 fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
123 if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
124 fatal("fcntl failed");
125 tty->log_fd = fd;
128 tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
129 if (tty->term == NULL) {
130 tty_close(tty);
131 return (-1);
133 tty->flags |= TTY_OPENED;
135 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
137 tty->event = bufferevent_new(
138 tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
140 tty_start_tty(tty);
142 tty_keys_init(tty);
144 return (0);
147 /* ARGSUSED */
148 void
149 tty_read_callback(unused struct bufferevent *bufev, void *data)
151 struct tty *tty = data;
153 while (tty_keys_next(tty))
157 /* ARGSUSED */
158 void
159 tty_error_callback(
160 unused struct bufferevent *bufev, unused short what, unused void *data)
164 void
165 tty_start_tty(struct tty *tty)
167 struct termios tio;
168 int mode;
170 if (tty->fd == -1)
171 return;
173 if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
174 fatal("fcntl failed");
175 if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
176 fatal("fcntl failed");
178 bufferevent_enable(tty->event, EV_READ|EV_WRITE);
180 if (tcgetattr(tty->fd, &tty->tio) != 0)
181 fatal("tcgetattr failed");
182 memcpy(&tio, &tty->tio, sizeof tio);
183 tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
184 tio.c_iflag |= IGNBRK;
185 tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
186 tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
187 ECHOPRT|ECHOKE|ECHOCTL|ISIG);
188 tio.c_cc[VMIN] = 1;
189 tio.c_cc[VTIME] = 0;
190 if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
191 fatal("tcsetattr failed");
192 tcflush(tty->fd, TCIOFLUSH);
194 tty_putcode(tty, TTYC_SMCUP);
196 tty_putcode(tty, TTYC_SGR0);
197 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
199 tty_putcode(tty, TTYC_RMKX);
200 if (tty_use_acs(tty))
201 tty_putcode(tty, TTYC_ENACS);
202 tty_putcode(tty, TTYC_CLEAR);
204 tty_putcode(tty, TTYC_CNORM);
205 if (tty_term_has(tty->term, TTYC_KMOUS))
206 tty_puts(tty, "\033[?1000l");
208 tty->cx = UINT_MAX;
209 tty->cy = UINT_MAX;
211 tty->rlower = UINT_MAX;
212 tty->rupper = UINT_MAX;
214 tty->mode = MODE_CURSOR;
216 tty->flags |= TTY_STARTED;
219 void
220 tty_stop_tty(struct tty *tty)
222 struct winsize ws;
223 int mode;
225 if (!(tty->flags & TTY_STARTED))
226 return;
227 tty->flags &= ~TTY_STARTED;
229 bufferevent_disable(tty->event, EV_READ|EV_WRITE);
232 * Be flexible about error handling and try not kill the server just
233 * because the fd is invalid. Things like ssh -t can easily leave us
234 * with a dead tty.
236 if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
237 return;
238 if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
239 return;
241 tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
242 if (tty_use_acs(tty))
243 tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
244 tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
245 tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
246 tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
248 tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
249 if (tty_term_has(tty->term, TTYC_KMOUS))
250 tty_raw(tty, "\033[?1000l");
252 tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
254 if ((mode = fcntl(tty->fd, F_GETFL)) != -1)
255 fcntl(tty->fd, F_SETFL, mode & ~O_NONBLOCK);
258 void
259 tty_close(struct tty *tty)
261 if (tty->log_fd != -1) {
262 close(tty->log_fd);
263 tty->log_fd = -1;
266 evtimer_del(&tty->key_timer);
267 tty_stop_tty(tty);
269 if (tty->flags & TTY_OPENED) {
270 bufferevent_free(tty->event);
272 tty_term_free(tty->term);
273 tty_keys_free(tty);
275 tty->flags &= ~TTY_OPENED;
278 if (tty->fd != -1) {
279 close(tty->fd);
280 tty->fd = -1;
284 void
285 tty_free(struct tty *tty)
287 tty_close(tty);
289 if (tty->path != NULL)
290 xfree(tty->path);
291 if (tty->termname != NULL)
292 xfree(tty->termname);
295 void
296 tty_raw(struct tty *tty, const char *s)
298 write(tty->fd, s, strlen(s));
301 void
302 tty_putcode(struct tty *tty, enum tty_code_code code)
304 tty_puts(tty, tty_term_string(tty->term, code));
307 void
308 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
310 if (a < 0)
311 return;
312 tty_puts(tty, tty_term_string1(tty->term, code, a));
315 void
316 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
318 if (a < 0 || b < 0)
319 return;
320 tty_puts(tty, tty_term_string2(tty->term, code, a, b));
323 void
324 tty_puts(struct tty *tty, const char *s)
326 if (*s == '\0')
327 return;
328 bufferevent_write(tty->event, s, strlen(s));
330 if (tty->log_fd != -1)
331 write(tty->log_fd, s, strlen(s));
334 void
335 tty_putc(struct tty *tty, u_char ch)
337 const char *acs;
338 u_int sx;
340 if (tty->cell.attr & GRID_ATTR_CHARSET) {
341 acs = tty_acs_get(tty, ch);
342 if (acs != NULL)
343 bufferevent_write(tty->event, acs, strlen(acs));
344 else
345 bufferevent_write(tty->event, &ch, 1);
346 } else
347 bufferevent_write(tty->event, &ch, 1);
349 if (ch >= 0x20 && ch != 0x7f) {
350 sx = tty->sx;
351 if (tty->term->flags & TERM_EARLYWRAP)
352 sx--;
354 if (tty->cx >= sx) {
355 tty->cx = 1;
356 if (tty->cy != tty->rlower)
357 tty->cy++;
358 } else
359 tty->cx++;
362 if (tty->log_fd != -1)
363 write(tty->log_fd, &ch, 1);
366 void
367 tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
369 size_t size;
371 size = grid_utf8_size(gu);
372 bufferevent_write(tty->event, gu->data, size);
373 if (tty->log_fd != -1)
374 write(tty->log_fd, gu->data, size);
375 tty->cx += gu->width;
378 void
379 tty_set_title(struct tty *tty, const char *title)
381 if (strstr(tty->termname, "xterm") == NULL &&
382 strstr(tty->termname, "rxvt") == NULL &&
383 strcmp(tty->termname, "screen") != 0)
384 return;
386 tty_puts(tty, "\033]0;");
387 tty_puts(tty, title);
388 tty_putc(tty, '\007');
391 void
392 tty_update_mode(struct tty *tty, int mode)
394 int changed;
396 if (tty->flags & TTY_NOCURSOR)
397 mode &= ~MODE_CURSOR;
399 changed = mode ^ tty->mode;
400 if (changed & MODE_CURSOR) {
401 if (mode & MODE_CURSOR)
402 tty_putcode(tty, TTYC_CNORM);
403 else
404 tty_putcode(tty, TTYC_CIVIS);
406 if (changed & ALL_MOUSE_MODES) {
407 if (mode & ALL_MOUSE_MODES) {
408 if (mode & MODE_MOUSE_STANDARD)
409 tty_puts(tty, "\033[?1000h");
410 else if (mode & MODE_MOUSE_HIGHLIGHT)
411 tty_puts(tty, "\033[?1001h");
412 else if (mode & MODE_MOUSE_BUTTON)
413 tty_puts(tty, "\033[?1002h");
414 else if (mode & MODE_MOUSE_ANY)
415 tty_puts(tty, "\033[?1003h");
416 } else {
417 if (tty->mode & MODE_MOUSE_STANDARD)
418 tty_puts(tty, "\033[?1000l");
419 else if (tty->mode & MODE_MOUSE_HIGHLIGHT)
420 tty_puts(tty, "\033[?1001l");
421 else if (tty->mode & MODE_MOUSE_BUTTON)
422 tty_puts(tty, "\033[?1002l");
423 else if (tty->mode & MODE_MOUSE_ANY)
424 tty_puts(tty, "\033[?1003l");
427 if (changed & MODE_KKEYPAD) {
428 if (mode & MODE_KKEYPAD)
429 tty_putcode(tty, TTYC_SMKX);
430 else
431 tty_putcode(tty, TTYC_RMKX);
433 tty->mode = mode;
436 void
437 tty_emulate_repeat(
438 struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
440 if (tty_term_has(tty->term, code))
441 tty_putcode1(tty, code, n);
442 else {
443 while (n-- > 0)
444 tty_putcode(tty, code1);
449 * Redraw scroll region using data from screen (already updated). Used when
450 * CSR not supported, or window is a pane that doesn't take up the full
451 * width of the terminal.
453 void
454 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
456 struct window_pane *wp = ctx->wp;
457 struct screen *s = wp->screen;
458 u_int i;
461 * If region is >= 50% of the screen, just schedule a window redraw. In
462 * most cases, this is likely to be followed by some more scrolling -
463 * without this, the entire pane ends up being redrawn many times which
464 * can be much more data.
466 if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
467 wp->flags |= PANE_REDRAW;
468 return;
471 if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
472 for (i = ctx->ocy; i < screen_size_y(s); i++)
473 tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
474 } else {
475 for (i = ctx->orupper; i <= ctx->orlower; i++)
476 tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
480 void
481 tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
483 const struct grid_cell *gc;
484 struct grid_line *gl;
485 struct grid_cell tmpgc;
486 const struct grid_utf8 *gu;
487 u_int i, sx;
489 tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
491 sx = screen_size_x(s);
492 if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
493 sx = s->grid->linedata[s->grid->hsize + py].cellsize;
494 if (sx > tty->sx)
495 sx = tty->sx;
498 * Don't move the cursor to the start permission if it will wrap there
499 * itself.
501 gl = NULL;
502 if (py != 0)
503 gl = &s->grid->linedata[s->grid->hsize + py - 1];
504 if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
505 tty->cx < tty->sx || ox != 0 ||
506 (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
507 tty_cursor(tty, ox, oy + py);
509 for (i = 0; i < sx; i++) {
510 gc = grid_view_peek_cell(s->grid, i, py);
512 gu = NULL;
513 if (gc->flags & GRID_FLAG_UTF8)
514 gu = grid_view_peek_utf8(s->grid, i, py);
516 if (screen_check_selection(s, i, py)) {
517 memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
518 tmpgc.data = gc->data;
519 tmpgc.flags = gc->flags &
520 ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
521 tmpgc.flags |= s->sel.cell.flags &
522 (GRID_FLAG_FG256|GRID_FLAG_BG256);
523 tty_cell(tty, &tmpgc, gu);
524 } else
525 tty_cell(tty, gc, gu);
528 if (sx >= tty->sx) {
529 tty_update_mode(tty, tty->mode);
530 return;
532 tty_reset(tty);
534 tty_cursor(tty, ox + sx, oy + py);
535 if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
536 tty_putcode(tty, TTYC_EL);
537 else {
538 for (i = sx; i < screen_size_x(s); i++)
539 tty_putc(tty, ' ');
541 tty_update_mode(tty, tty->mode);
544 void
545 tty_write(void (*cmdfn)(
546 struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
548 struct window_pane *wp = ctx->wp;
549 struct client *c;
550 u_int i;
552 /* wp can be NULL if updating the screen but not the terminal. */
553 if (wp == NULL)
554 return;
556 if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
557 return;
558 if (!window_pane_visible(wp))
559 return;
561 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
562 c = ARRAY_ITEM(&clients, i);
563 if (c == NULL || c->session == NULL)
564 continue;
565 if (c->flags & CLIENT_SUSPENDED)
566 continue;
568 if (c->session->curw->window == wp->window) {
569 if (c->tty.term == NULL)
570 continue;
571 if (c->tty.flags & (TTY_FREEZE|TTY_BACKOFF))
572 continue;
573 cmdfn(&c->tty, ctx);
578 void
579 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
581 struct window_pane *wp = ctx->wp;
582 struct screen *s = wp->screen;
583 u_int i;
585 if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
586 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
587 return;
590 tty_reset(tty);
592 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
594 if (tty_term_has(tty->term, TTYC_ICH) ||
595 tty_term_has(tty->term, TTYC_ICH1))
596 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
597 else if (tty_term_has(tty->term, TTYC_SMIR) &&
598 tty_term_has(tty->term, TTYC_RMIR)) {
599 tty_putcode(tty, TTYC_SMIR);
600 for (i = 0; i < ctx->num; i++)
601 tty_putc(tty, ' ');
602 tty_putcode(tty, TTYC_RMIR);
603 } else
604 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
607 void
608 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
610 struct window_pane *wp = ctx->wp;
611 struct screen *s = wp->screen;
613 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
614 (!tty_term_has(tty->term, TTYC_DCH) &&
615 !tty_term_has(tty->term, TTYC_DCH1))) {
616 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
617 return;
620 tty_reset(tty);
622 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
624 if (tty_term_has(tty->term, TTYC_DCH) ||
625 tty_term_has(tty->term, TTYC_DCH1))
626 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
629 void
630 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
632 struct window_pane *wp = ctx->wp;
633 struct screen *s = wp->screen;
635 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
636 !tty_term_has(tty->term, TTYC_CSR) ||
637 !tty_term_has(tty->term, TTYC_IL1)) {
638 tty_redraw_region(tty, ctx);
639 return;
642 tty_reset(tty);
644 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
645 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
647 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
650 void
651 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
653 struct window_pane *wp = ctx->wp;
654 struct screen *s = wp->screen;
656 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
657 !tty_term_has(tty->term, TTYC_CSR) ||
658 !tty_term_has(tty->term, TTYC_DL1)) {
659 tty_redraw_region(tty, ctx);
660 return;
663 tty_reset(tty);
665 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
666 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
668 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
671 void
672 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
674 struct window_pane *wp = ctx->wp;
675 struct screen *s = wp->screen;
676 u_int i;
678 tty_reset(tty);
680 tty_cursor_pane(tty, ctx, 0, ctx->ocy);
682 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
683 tty_term_has(tty->term, TTYC_EL)) {
684 tty_putcode(tty, TTYC_EL);
685 } else {
686 for (i = 0; i < screen_size_x(s); i++)
687 tty_putc(tty, ' ');
691 void
692 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
694 struct window_pane *wp = ctx->wp;
695 struct screen *s = wp->screen;
696 u_int i;
698 tty_reset(tty);
700 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
702 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
703 tty_term_has(tty->term, TTYC_EL))
704 tty_putcode(tty, TTYC_EL);
705 else {
706 for (i = ctx->ocx; i < screen_size_x(s); i++)
707 tty_putc(tty, ' ');
711 void
712 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
714 struct window_pane *wp = ctx->wp;
715 u_int i;
717 tty_reset(tty);
719 if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
720 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
721 tty_putcode(tty, TTYC_EL1);
722 } else {
723 tty_cursor_pane(tty, ctx, 0, ctx->ocy);
724 for (i = 0; i < ctx->ocx + 1; i++)
725 tty_putc(tty, ' ');
729 void
730 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
732 struct window_pane *wp = ctx->wp;
733 struct screen *s = wp->screen;
735 if (ctx->ocy != ctx->orupper)
736 return;
738 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
739 !tty_term_has(tty->term, TTYC_CSR) ||
740 !tty_term_has(tty->term, TTYC_RI)) {
741 tty_redraw_region(tty, ctx);
742 return;
745 tty_reset(tty);
747 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
748 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
750 tty_putcode(tty, TTYC_RI);
753 void
754 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
756 struct window_pane *wp = ctx->wp;
757 struct screen *s = wp->screen;
759 if (ctx->ocy != ctx->orlower)
760 return;
762 if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
763 !tty_term_has(tty->term, TTYC_CSR)) {
764 tty_redraw_region(tty, ctx);
765 return;
769 * If this line wrapped naturally (ctx->num is nonzero), don't do
770 * anything - the cursor can just be moved to the last cell and wrap
771 * naturally.
773 if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
774 return;
776 tty_reset(tty);
778 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
779 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
781 tty_putc(tty, '\n');
784 void
785 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
787 struct window_pane *wp = ctx->wp;
788 struct screen *s = wp->screen;
789 u_int i, j;
791 tty_reset(tty);
793 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
794 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
796 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
797 tty_term_has(tty->term, TTYC_EL)) {
798 tty_putcode(tty, TTYC_EL);
799 if (ctx->ocy != screen_size_y(s) - 1) {
800 tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
801 for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
802 tty_putcode(tty, TTYC_EL);
803 if (i == screen_size_y(s) - 1)
804 continue;
805 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
806 tty->cy++;
809 } else {
810 for (i = ctx->ocx; i < screen_size_x(s); i++)
811 tty_putc(tty, ' ');
812 for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
813 tty_cursor_pane(tty, ctx, 0, j);
814 for (i = 0; i < screen_size_x(s); i++)
815 tty_putc(tty, ' ');
820 void
821 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
823 struct window_pane *wp = ctx->wp;
824 struct screen *s = wp->screen;
825 u_int i, j;
827 tty_reset(tty);
829 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
830 tty_cursor_pane(tty, ctx, 0, 0);
832 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
833 tty_term_has(tty->term, TTYC_EL)) {
834 for (i = 0; i < ctx->ocy; i++) {
835 tty_putcode(tty, TTYC_EL);
836 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
837 tty->cy++;
839 } else {
840 for (j = 0; j < ctx->ocy; j++) {
841 tty_cursor_pane(tty, ctx, 0, j);
842 for (i = 0; i < screen_size_x(s); i++)
843 tty_putc(tty, ' ');
846 for (i = 0; i <= ctx->ocx; i++)
847 tty_putc(tty, ' ');
850 void
851 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
853 struct window_pane *wp = ctx->wp;
854 struct screen *s = wp->screen;
855 u_int i, j;
857 tty_reset(tty);
859 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
860 tty_cursor_pane(tty, ctx, 0, 0);
862 if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
863 tty_term_has(tty->term, TTYC_EL)) {
864 for (i = 0; i < screen_size_y(s); i++) {
865 tty_putcode(tty, TTYC_EL);
866 if (i != screen_size_y(s) - 1) {
867 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
868 tty->cy++;
871 } else {
872 for (j = 0; j < screen_size_y(s); j++) {
873 tty_cursor_pane(tty, ctx, 0, j);
874 for (i = 0; i < screen_size_x(s); i++)
875 tty_putc(tty, ' ');
880 void
881 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
883 struct window_pane *wp = ctx->wp;
884 struct screen *s = wp->screen;
885 u_int i, j;
887 tty_reset(tty);
889 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
891 for (j = 0; j < screen_size_y(s); j++) {
892 tty_cursor_pane(tty, ctx, 0, j);
893 for (i = 0; i < screen_size_x(s); i++)
894 tty_putc(tty, 'E');
898 void
899 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
901 struct window_pane *wp = ctx->wp;
902 struct screen *s = wp->screen;
903 u_int cx;
905 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
907 /* Is the cursor in the very last position? */
908 if (ctx->ocx > wp->sx - ctx->last_width) {
909 if (wp->xoff != 0 || wp->sx != tty->sx) {
911 * The pane doesn't fill the entire line, the linefeed
912 * will already have happened, so just move the cursor.
914 tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
915 } else if (tty->cx < tty->sx) {
917 * The cursor isn't in the last position already, so
918 * move as far left as possinble and redraw the last
919 * cell to move into the last position.
921 cx = screen_size_x(s) - ctx->last_width;
922 tty_cursor_pane(tty, ctx, cx, ctx->ocy);
923 tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
925 } else
926 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
928 tty_cell(tty, ctx->cell, ctx->utf8);
931 void
932 tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
934 struct window_pane *wp = ctx->wp;
937 * Cannot rely on not being a partial character, so just redraw the
938 * whole line.
940 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
943 void
944 tty_cell(
945 struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
947 u_int i;
949 /* Skip last character if terminal is stupid. */
950 if (tty->term->flags & TERM_EARLYWRAP &&
951 tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
952 return;
954 /* If this is a padding character, do nothing. */
955 if (gc->flags & GRID_FLAG_PADDING)
956 return;
958 /* Set the attributes. */
959 tty_attributes(tty, gc);
961 /* If not UTF-8, write directly. */
962 if (!(gc->flags & GRID_FLAG_UTF8)) {
963 if (gc->data < 0x20 || gc->data == 0x7f)
964 return;
965 tty_putc(tty, gc->data);
966 return;
969 /* If the terminal doesn't support UTF-8, write underscores. */
970 if (!(tty->flags & TTY_UTF8)) {
971 for (i = 0; i < gu->width; i++)
972 tty_putc(tty, '_');
973 return;
976 /* Otherwise, write UTF-8. */
977 tty_pututf8(tty, gu);
980 void
981 tty_reset(struct tty *tty)
983 struct grid_cell *gc = &tty->cell;
985 if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
986 return;
988 if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
989 tty_putcode(tty, TTYC_RMACS);
990 tty_putcode(tty, TTYC_SGR0);
991 memcpy(gc, &grid_default_cell, sizeof *gc);
994 /* Set region inside pane. */
995 void
996 tty_region_pane(
997 struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
999 struct window_pane *wp = ctx->wp;
1001 tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
1004 /* Set region at absolute position. */
1005 void
1006 tty_region(struct tty *tty, u_int rupper, u_int rlower)
1008 if (tty->rlower == rlower && tty->rupper == rupper)
1009 return;
1010 if (!tty_term_has(tty->term, TTYC_CSR))
1011 return;
1013 tty->rupper = rupper;
1014 tty->rlower = rlower;
1017 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1018 * 0,0 if it is beyond the last column (they do not reset their wrap
1019 * flag so further output causes a line feed). As a workaround, do an
1020 * explicit move to 0 first.
1022 if (tty->cx >= tty->sx)
1023 tty_cursor(tty, 0, tty->cy);
1025 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1026 tty_cursor(tty, 0, 0);
1029 /* Move cursor inside pane. */
1030 void
1031 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1033 struct window_pane *wp = ctx->wp;
1035 tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
1038 /* Move cursor to absolute position. */
1039 void
1040 tty_cursor(struct tty *tty, u_int cx, u_int cy)
1042 struct tty_term *term = tty->term;
1043 u_int thisx, thisy;
1044 int change;
1046 if (cx > tty->sx - 1)
1047 cx = tty->sx - 1;
1049 thisx = tty->cx;
1050 thisy = tty->cy;
1052 /* No change. */
1053 if (cx == thisx && cy == thisy)
1054 return;
1056 /* Very end of the line, just use absolute movement. */
1057 if (thisx > tty->sx - 1)
1058 goto absolute;
1060 /* Move to home position (0, 0). */
1061 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1062 tty_putcode(tty, TTYC_HOME);
1063 goto out;
1066 /* Zero on the next line. */
1067 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1068 tty_putc(tty, '\r');
1069 tty_putc(tty, '\n');
1070 goto out;
1073 /* Moving column or row. */
1074 if (cy == thisy) {
1076 * Moving column only, row staying the same.
1079 /* To left edge. */
1080 if (cx == 0) {
1081 tty_putc(tty, '\r');
1082 goto out;
1085 /* One to the left. */
1086 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1087 tty_putcode(tty, TTYC_CUB1);
1088 goto out;
1091 /* One to the right. */
1092 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1093 tty_putcode(tty, TTYC_CUF1);
1094 goto out;
1097 /* Calculate difference. */
1098 change = thisx - cx; /* +ve left, -ve right */
1101 * Use HPA if change is larger than absolute, otherwise move
1102 * the cursor with CUB/CUF.
1104 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1105 tty_putcode1(tty, TTYC_HPA, cx);
1106 goto out;
1107 } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1108 tty_putcode1(tty, TTYC_CUB, change);
1109 goto out;
1110 } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1111 tty_putcode1(tty, TTYC_CUF, -change);
1112 goto out;
1114 } else if (cx == thisx) {
1116 * Moving row only, column staying the same.
1119 /* One above. */
1120 if (thisy != tty->rupper &&
1121 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1122 tty_putcode(tty, TTYC_CUU1);
1123 goto out;
1126 /* One below. */
1127 if (thisy != tty->rlower &&
1128 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1129 tty_putcode(tty, TTYC_CUD1);
1130 goto out;
1133 /* Calculate difference. */
1134 change = thisy - cy; /* +ve up, -ve down */
1137 * Try to use VPA if change is larger than absolute or if this
1138 * change would cross the scroll region, otherwise use CUU/CUD.
1140 if ((u_int) abs(change) > cy ||
1141 (change < 0 && cy - change > tty->rlower) ||
1142 (change > 0 && cy - change < tty->rupper)) {
1143 if (tty_term_has(term, TTYC_VPA)) {
1144 tty_putcode1(tty, TTYC_VPA, cy);
1145 goto out;
1147 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1148 tty_putcode1(tty, TTYC_CUU, change);
1149 goto out;
1150 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1151 tty_putcode1(tty, TTYC_CUD, -change);
1152 goto out;
1156 absolute:
1157 /* Absolute movement. */
1158 tty_putcode2(tty, TTYC_CUP, cy, cx);
1160 out:
1161 tty->cx = cx;
1162 tty->cy = cy;
1165 void
1166 tty_attributes(struct tty *tty, const struct grid_cell *gc)
1168 struct grid_cell *tc = &tty->cell, gc2;
1169 u_char changed;
1171 memcpy(&gc2, gc, sizeof gc2);
1174 * If no setab, try to use the reverse attribute as a best-effort for a
1175 * non-default background. This is a bit of a hack but it doesn't do
1176 * any serious harm and makes a couple of applications happier.
1178 if (!tty_term_has(tty->term, TTYC_SETAB)) {
1179 if (gc2.attr & GRID_ATTR_REVERSE) {
1180 if (gc2.fg != 7 && gc2.fg != 8)
1181 gc2.attr &= ~GRID_ATTR_REVERSE;
1182 } else {
1183 if (gc2.bg != 0 && gc2.bg != 8)
1184 gc2.attr |= GRID_ATTR_REVERSE;
1188 /* Fix up the colours if necessary. */
1189 tty_check_fg(tty, &gc2);
1190 tty_check_bg(tty, &gc2);
1192 /* If any bits are being cleared, reset everything. */
1193 if (tc->attr & ~gc2.attr)
1194 tty_reset(tty);
1197 * Set the colours. This may call tty_reset() (so it comes next) and
1198 * may add to (NOT remove) the desired attributes by changing new_attr.
1200 tty_colours(tty, &gc2);
1202 /* Filter out attribute bits already set. */
1203 changed = gc2.attr & ~tc->attr;
1204 tc->attr = gc2.attr;
1206 /* Set the attributes. */
1207 if (changed & GRID_ATTR_BRIGHT)
1208 tty_putcode(tty, TTYC_BOLD);
1209 if (changed & GRID_ATTR_DIM)
1210 tty_putcode(tty, TTYC_DIM);
1211 if (changed & GRID_ATTR_ITALICS)
1212 tty_putcode(tty, TTYC_SMSO);
1213 if (changed & GRID_ATTR_UNDERSCORE)
1214 tty_putcode(tty, TTYC_SMUL);
1215 if (changed & GRID_ATTR_BLINK)
1216 tty_putcode(tty, TTYC_BLINK);
1217 if (changed & GRID_ATTR_REVERSE) {
1218 if (tty_term_has(tty->term, TTYC_REV))
1219 tty_putcode(tty, TTYC_REV);
1220 else if (tty_term_has(tty->term, TTYC_SMSO))
1221 tty_putcode(tty, TTYC_SMSO);
1223 if (changed & GRID_ATTR_HIDDEN)
1224 tty_putcode(tty, TTYC_INVIS);
1225 if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1226 tty_putcode(tty, TTYC_SMACS);
1229 void
1230 tty_colours(struct tty *tty, const struct grid_cell *gc)
1232 struct grid_cell *tc = &tty->cell;
1233 u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1234 int have_ax, fg_default, bg_default;
1236 /* No changes? Nothing is necessary. */
1237 if (fg == tc->fg && bg == tc->bg &&
1238 ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1239 return;
1242 * Is either the default colour? This is handled specially because the
1243 * best solution might be to reset both colours to default, in which
1244 * case if only one is default need to fall onward to set the other
1245 * colour.
1247 fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1248 bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1249 if (fg_default || bg_default) {
1251 * If don't have AX but do have op, send sgr0 (op can't
1252 * actually be used because it is sometimes the same as sgr0
1253 * and sometimes isn't). This resets both colours to default.
1255 * Otherwise, try to set the default colour only as needed.
1257 have_ax = tty_term_has(tty->term, TTYC_AX);
1258 if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1259 tty_reset(tty);
1260 else {
1261 if (fg_default &&
1262 (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1263 if (have_ax)
1264 tty_puts(tty, "\033[39m");
1265 else if (tc->fg != 7 ||
1266 tc->flags & GRID_FLAG_FG256)
1267 tty_putcode1(tty, TTYC_SETAF, 7);
1268 tc->fg = 8;
1269 tc->flags &= ~GRID_FLAG_FG256;
1271 if (bg_default &&
1272 (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1273 if (have_ax)
1274 tty_puts(tty, "\033[49m");
1275 else if (tc->bg != 0 ||
1276 tc->flags & GRID_FLAG_BG256)
1277 tty_putcode1(tty, TTYC_SETAB, 0);
1278 tc->bg = 8;
1279 tc->flags &= ~GRID_FLAG_BG256;
1284 /* Set the foreground colour. */
1285 if (!fg_default && (fg != tc->fg ||
1286 ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1287 tty_colours_fg(tty, gc);
1290 * Set the background colour. This must come after the foreground as
1291 * tty_colour_fg() can call tty_reset().
1293 if (!bg_default && (bg != tc->bg ||
1294 ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1295 tty_colours_bg(tty, gc);
1298 void
1299 tty_check_fg(struct tty *tty, struct grid_cell *gc)
1301 u_int colours;
1303 /* Is this a 256-colour colour? */
1304 if (gc->flags & GRID_FLAG_FG256) {
1305 /* And not a 256 colour mode? */
1306 if (!(tty->term->flags & TERM_88COLOURS) &&
1307 !(tty->term_flags & TERM_88COLOURS) &&
1308 !(tty->term->flags & TERM_256COLOURS) &&
1309 !(tty->term_flags & TERM_256COLOURS)) {
1310 gc->fg = colour_256to16(gc->fg);
1311 if (gc->fg & 8) {
1312 gc->fg &= 7;
1313 gc->attr |= GRID_ATTR_BRIGHT;
1314 } else
1315 gc->attr &= ~GRID_ATTR_BRIGHT;
1316 gc->flags &= ~GRID_FLAG_FG256;
1318 return;
1321 /* Is this an aixterm colour? */
1322 colours = tty_term_number(tty->term, TTYC_COLORS);
1323 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1324 gc->fg -= 90;
1325 gc->attr |= GRID_ATTR_BRIGHT;
1329 void
1330 tty_check_bg(struct tty *tty, struct grid_cell *gc)
1332 u_int colours;
1334 /* Is this a 256-colour colour? */
1335 if (gc->flags & GRID_FLAG_BG256) {
1337 * And not a 256 colour mode? Translate to 16-colour
1338 * palette. Bold background doesn't exist portably, so just
1339 * discard the bold bit if set.
1341 if (!(tty->term->flags & TERM_88COLOURS) &&
1342 !(tty->term_flags & TERM_88COLOURS) &&
1343 !(tty->term->flags & TERM_256COLOURS) &&
1344 !(tty->term_flags & TERM_256COLOURS)) {
1345 gc->bg = colour_256to16(gc->bg);
1346 if (gc->bg & 8)
1347 gc->bg &= 7;
1348 gc->attr &= ~GRID_ATTR_BRIGHT;
1349 gc->flags &= ~GRID_FLAG_BG256;
1351 return;
1354 /* Is this an aixterm colour? */
1355 colours = tty_term_number(tty->term, TTYC_COLORS);
1356 if (gc->bg >= 100 && gc->bg <= 107 && colours < 16) {
1357 gc->bg -= 90;
1358 gc->attr |= GRID_ATTR_BRIGHT;
1362 void
1363 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1365 struct grid_cell *tc = &tty->cell;
1366 u_char fg = gc->fg;
1367 char s[32];
1369 /* Is this a 256-colour colour? */
1370 if (gc->flags & GRID_FLAG_FG256) {
1371 /* Try as 256 colours or translating to 88. */
1372 if (tty_try_256(tty, fg, "38") == 0)
1373 goto save_fg;
1374 if (tty_try_88(tty, fg, "38") == 0)
1375 goto save_fg;
1376 /* Else already handled by tty_check_fg. */
1377 return;
1380 /* Is this an aixterm bright colour? */
1381 if (fg >= 90 && fg <= 97) {
1382 xsnprintf(s, sizeof s, "\033[%dm", fg);
1383 tty_puts(tty, s);
1384 goto save_fg;
1387 /* Otherwise set the foreground colour. */
1388 tty_putcode1(tty, TTYC_SETAF, fg);
1390 save_fg:
1391 /* Save the new values in the terminal current cell. */
1392 tc->fg = fg;
1393 tc->flags &= ~GRID_FLAG_FG256;
1394 tc->flags |= gc->flags & GRID_FLAG_FG256;
1397 void
1398 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1400 struct grid_cell *tc = &tty->cell;
1401 u_char bg = gc->bg;
1402 char s[32];
1404 /* Is this a 256-colour colour? */
1405 if (gc->flags & GRID_FLAG_BG256) {
1406 /* Try as 256 colours or translating to 88. */
1407 if (tty_try_256(tty, bg, "48") == 0)
1408 goto save_bg;
1409 if (tty_try_88(tty, bg, "48") == 0)
1410 goto save_bg;
1411 /* Else already handled by tty_check_bg. */
1412 return;
1415 /* Is this an aixterm bright colour? */
1416 if (bg >= 100 && bg <= 107) {
1417 /* 16 colour terminals or above only. */
1418 if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1419 xsnprintf(s, sizeof s, "\033[%dm", bg);
1420 tty_puts(tty, s);
1421 goto save_bg;
1423 bg -= 100;
1424 /* no such thing as a bold background */
1427 /* Otherwise set the background colour. */
1428 tty_putcode1(tty, TTYC_SETAB, bg);
1430 save_bg:
1431 /* Save the new values in the terminal current cell. */
1432 tc->bg = bg;
1433 tc->flags &= ~GRID_FLAG_BG256;
1434 tc->flags |= gc->flags & GRID_FLAG_BG256;
1438 tty_try_256(struct tty *tty, u_char colour, const char *type)
1440 char s[32];
1442 if (!(tty->term->flags & TERM_256COLOURS) &&
1443 !(tty->term_flags & TERM_256COLOURS))
1444 return (-1);
1446 xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1447 tty_puts(tty, s);
1448 return (0);
1452 tty_try_88(struct tty *tty, u_char colour, const char *type)
1454 char s[32];
1456 if (!(tty->term->flags & TERM_88COLOURS) &&
1457 !(tty->term_flags & TERM_88COLOURS))
1458 return (-1);
1459 colour = colour_256to88(colour);
1461 xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1462 tty_puts(tty, s);
1463 return (0);