Support setting the xterm clipboard when copying from copy mode using
[tmux-openbsd.git] / tty.c
blob04503b6070ee39657bf9167bd7aa2f8fe8043624
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 <netinet/in.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <resolv.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <termios.h>
30 #include <unistd.h>
32 #include "tmux.h"
34 void tty_read_callback(struct bufferevent *, void *);
35 void tty_error_callback(struct bufferevent *, short, void *);
37 int tty_try_256(struct tty *, u_char, const char *);
38 int tty_try_88(struct tty *, u_char, const char *);
40 void tty_colours(struct tty *, const struct grid_cell *);
41 void tty_check_fg(struct tty *, struct grid_cell *);
42 void tty_check_bg(struct tty *, struct grid_cell *);
43 void tty_colours_fg(struct tty *, const struct grid_cell *);
44 void tty_colours_bg(struct tty *, const struct grid_cell *);
46 void tty_redraw_region(struct tty *, const struct tty_ctx *);
47 void tty_emulate_repeat(
48 struct tty *, enum tty_code_code, enum tty_code_code, u_int);
49 void tty_cell(struct tty *,
50 const struct grid_cell *, const struct grid_utf8 *);
52 #define tty_use_acs(tty) \
53 (tty_term_has(tty->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
55 void
56 tty_init(struct tty *tty, int fd, char *term)
58 char *path;
60 memset(tty, 0, sizeof *tty);
61 tty->log_fd = -1;
63 if (term == NULL || *term == '\0')
64 tty->termname = xstrdup("unknown");
65 else
66 tty->termname = xstrdup(term);
67 tty->fd = fd;
69 if ((path = ttyname(fd)) == NULL)
70 fatalx("ttyname failed");
71 tty->path = xstrdup(path);
73 tty->flags = 0;
74 tty->term_flags = 0;
77 int
78 tty_resize(struct tty *tty)
80 struct winsize ws;
81 u_int sx, sy;
83 if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
84 sx = ws.ws_col;
85 if (sx == 0)
86 sx = 80;
87 sy = ws.ws_row;
88 if (sy == 0)
89 sy = 24;
90 } else {
91 sx = 80;
92 sy = 24;
94 if (sx == tty->sx && sy == tty->sy)
95 return (0);
96 tty->sx = sx;
97 tty->sy = sy;
99 tty->cx = UINT_MAX;
100 tty->cy = UINT_MAX;
102 tty->rupper = UINT_MAX;
103 tty->rlower = UINT_MAX;
106 * If the terminal has been started, reset the actual scroll region and
107 * cursor position, as this may not have happened.
109 if (tty->flags & TTY_STARTED) {
110 tty_cursor(tty, 0, 0);
111 tty_region(tty, 0, tty->sy - 1);
114 return (1);
118 tty_open(struct tty *tty, const char *overrides, char **cause)
120 char out[64];
121 int fd;
123 if (debug_level > 3) {
124 xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
125 fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
126 if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
127 fatal("fcntl failed");
128 tty->log_fd = fd;
131 tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
132 if (tty->term == NULL) {
133 tty_close(tty);
134 return (-1);
136 tty->flags |= TTY_OPENED;
138 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
140 tty->event = bufferevent_new(
141 tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
143 tty_start_tty(tty);
145 tty_keys_init(tty);
147 return (0);
150 /* ARGSUSED */
151 void
152 tty_read_callback(unused struct bufferevent *bufev, void *data)
154 struct tty *tty = data;
156 while (tty_keys_next(tty))
160 /* ARGSUSED */
161 void
162 tty_error_callback(
163 unused struct bufferevent *bufev, unused short what, unused void *data)
167 void
168 tty_start_tty(struct tty *tty)
170 struct termios tio;
172 if (tty->fd == -1 || tcgetattr(tty->fd, &tty->tio) != 0)
173 return;
175 setblocking(tty->fd, 0);
177 bufferevent_enable(tty->event, EV_READ|EV_WRITE);
179 memcpy(&tio, &tty->tio, sizeof tio);
180 tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
181 tio.c_iflag |= IGNBRK;
182 tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
183 tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
184 ECHOPRT|ECHOKE|ECHOCTL|ISIG);
185 tio.c_cc[VMIN] = 1;
186 tio.c_cc[VTIME] = 0;
187 if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
188 tcflush(tty->fd, TCIOFLUSH);
190 tty_putcode(tty, TTYC_SMCUP);
192 tty_putcode(tty, TTYC_SGR0);
193 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
195 tty_putcode(tty, TTYC_RMKX);
196 if (tty_use_acs(tty))
197 tty_putcode(tty, TTYC_ENACS);
198 tty_putcode(tty, TTYC_CLEAR);
200 tty_putcode(tty, TTYC_CNORM);
201 if (tty_term_has(tty->term, TTYC_KMOUS))
202 tty_puts(tty, "\033[?1000l");
204 tty->cx = UINT_MAX;
205 tty->cy = UINT_MAX;
207 tty->rlower = UINT_MAX;
208 tty->rupper = UINT_MAX;
210 tty->mode = MODE_CURSOR;
212 tty->flags |= TTY_STARTED;
215 void
216 tty_stop_tty(struct tty *tty)
218 struct winsize ws;
220 if (!(tty->flags & TTY_STARTED))
221 return;
222 tty->flags &= ~TTY_STARTED;
224 bufferevent_disable(tty->event, EV_READ|EV_WRITE);
227 * Be flexible about error handling and try not kill the server just
228 * because the fd is invalid. Things like ssh -t can easily leave us
229 * with a dead tty.
231 if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
232 return;
233 if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
234 return;
236 setblocking(tty->fd, 1);
238 tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
239 if (tty_use_acs(tty))
240 tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
241 tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
242 tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
243 tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
245 tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
246 if (tty_term_has(tty->term, TTYC_KMOUS))
247 tty_raw(tty, "\033[?1000l");
249 tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
252 void
253 tty_close(struct tty *tty)
255 if (tty->log_fd != -1) {
256 close(tty->log_fd);
257 tty->log_fd = -1;
260 evtimer_del(&tty->key_timer);
261 tty_stop_tty(tty);
263 if (tty->flags & TTY_OPENED) {
264 bufferevent_free(tty->event);
266 tty_term_free(tty->term);
267 tty_keys_free(tty);
269 tty->flags &= ~TTY_OPENED;
272 if (tty->fd != -1) {
273 close(tty->fd);
274 tty->fd = -1;
278 void
279 tty_free(struct tty *tty)
281 tty_close(tty);
283 if (tty->path != NULL)
284 xfree(tty->path);
285 if (tty->termname != NULL)
286 xfree(tty->termname);
289 void
290 tty_raw(struct tty *tty, const char *s)
292 write(tty->fd, s, strlen(s));
295 void
296 tty_putcode(struct tty *tty, enum tty_code_code code)
298 tty_puts(tty, tty_term_string(tty->term, code));
301 void
302 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
304 if (a < 0)
305 return;
306 tty_puts(tty, tty_term_string1(tty->term, code, a));
309 void
310 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
312 if (a < 0 || b < 0)
313 return;
314 tty_puts(tty, tty_term_string2(tty->term, code, a, b));
317 void
318 tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a, const void *b)
320 if (a != NULL && b != NULL)
321 tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
324 void
325 tty_puts(struct tty *tty, const char *s)
327 if (*s == '\0')
328 return;
329 bufferevent_write(tty->event, s, strlen(s));
331 if (tty->log_fd != -1)
332 write(tty->log_fd, s, strlen(s));
335 void
336 tty_putc(struct tty *tty, u_char ch)
338 const char *acs;
339 u_int sx;
341 if (tty->cell.attr & GRID_ATTR_CHARSET) {
342 acs = tty_acs_get(tty, ch);
343 if (acs != NULL)
344 bufferevent_write(tty->event, acs, strlen(acs));
345 else
346 bufferevent_write(tty->event, &ch, 1);
347 } else
348 bufferevent_write(tty->event, &ch, 1);
350 if (ch >= 0x20 && ch != 0x7f) {
351 sx = tty->sx;
352 if (tty->term->flags & TERM_EARLYWRAP)
353 sx--;
355 if (tty->cx >= sx) {
356 tty->cx = 1;
357 if (tty->cy != tty->rlower)
358 tty->cy++;
359 } else
360 tty->cx++;
363 if (tty->log_fd != -1)
364 write(tty->log_fd, &ch, 1);
367 void
368 tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
370 size_t size;
372 size = grid_utf8_size(gu);
373 bufferevent_write(tty->event, gu->data, size);
374 if (tty->log_fd != -1)
375 write(tty->log_fd, gu->data, size);
376 tty->cx += gu->width;
379 void
380 tty_set_title(struct tty *tty, const char *title)
382 if (!tty_term_has(tty->term, TTYC_TSL) ||
383 !tty_term_has(tty->term, TTYC_FSL))
384 return;
386 tty_putcode(tty, TTYC_TSL);
387 tty_puts(tty, title);
388 tty_putcode(tty, TTYC_FSL);
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_UTF8)
409 tty_puts(tty, "\033[?1005h");
410 if (mode & MODE_MOUSE_ANY)
411 tty_puts(tty, "\033[?1003h");
412 else if (mode & MODE_MOUSE_BUTTON)
413 tty_puts(tty, "\033[?1002h");
414 else if (mode & MODE_MOUSE_STANDARD)
415 tty_puts(tty, "\033[?1000h");
416 } else {
417 if (tty->mode & MODE_MOUSE_ANY)
418 tty_puts(tty, "\033[?1003l");
419 else if (tty->mode & MODE_MOUSE_BUTTON)
420 tty_puts(tty, "\033[?1002l");
421 else if (tty->mode & MODE_MOUSE_STANDARD)
422 tty_puts(tty, "\033[?1000l");
423 if (tty->mode & MODE_MOUSE_UTF8)
424 tty_puts(tty, "\033[?1005l");
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->orlower - ctx->orupper >= 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;
904 u_int width;
905 const struct grid_cell *gc = ctx->cell;
906 const struct grid_utf8 *gu = ctx->utf8;
908 if (gc->flags & GRID_FLAG_UTF8)
909 width = gu->width;
910 else
911 width = 1;
913 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
915 /* Is the cursor in the very last position? */
916 if (ctx->ocx > wp->sx - width) {
917 if (wp->xoff != 0 || wp->sx != tty->sx) {
919 * The pane doesn't fill the entire line, the linefeed
920 * will already have happened, so just move the cursor.
922 tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
923 } else if (tty->cx < tty->sx) {
925 * The cursor isn't in the last position already, so
926 * move as far left as possible and redraw the last
927 * cell to move into the last position.
929 cx = screen_size_x(s) - width;
930 tty_cursor_pane(tty, ctx, cx, ctx->ocy);
931 tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
933 } else
934 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
936 tty_cell(tty, ctx->cell, ctx->utf8);
939 void
940 tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
942 struct window_pane *wp = ctx->wp;
945 * Cannot rely on not being a partial character, so just redraw the
946 * whole line.
948 tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
951 void
952 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
954 char *buf;
955 size_t off;
957 if (!tty_term_has(tty->term, TTYC_MS))
958 return;
960 off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
961 buf = xmalloc(off);
963 b64_ntop(ctx->ptr, ctx->num, buf, off);
964 tty_putcode_ptr2(tty, TTYC_MS, "", buf);
966 xfree(buf);
969 void
970 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
972 u_int i;
973 u_char *str = ctx->ptr;
975 for (i = 0; i < ctx->num; i++)
976 tty_putc(tty, str[i]);
979 void
980 tty_cell(
981 struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
983 u_int i;
985 /* Skip last character if terminal is stupid. */
986 if (tty->term->flags & TERM_EARLYWRAP &&
987 tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
988 return;
990 /* If this is a padding character, do nothing. */
991 if (gc->flags & GRID_FLAG_PADDING)
992 return;
994 /* Set the attributes. */
995 tty_attributes(tty, gc);
997 /* If not UTF-8, write directly. */
998 if (!(gc->flags & GRID_FLAG_UTF8)) {
999 if (gc->data < 0x20 || gc->data == 0x7f)
1000 return;
1001 tty_putc(tty, gc->data);
1002 return;
1005 /* If the terminal doesn't support UTF-8, write underscores. */
1006 if (!(tty->flags & TTY_UTF8)) {
1007 for (i = 0; i < gu->width; i++)
1008 tty_putc(tty, '_');
1009 return;
1012 /* Otherwise, write UTF-8. */
1013 tty_pututf8(tty, gu);
1016 void
1017 tty_reset(struct tty *tty)
1019 struct grid_cell *gc = &tty->cell;
1021 if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
1022 return;
1024 if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1025 tty_putcode(tty, TTYC_RMACS);
1026 tty_putcode(tty, TTYC_SGR0);
1027 memcpy(gc, &grid_default_cell, sizeof *gc);
1030 /* Set region inside pane. */
1031 void
1032 tty_region_pane(
1033 struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1035 struct window_pane *wp = ctx->wp;
1037 tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
1040 /* Set region at absolute position. */
1041 void
1042 tty_region(struct tty *tty, u_int rupper, u_int rlower)
1044 if (tty->rlower == rlower && tty->rupper == rupper)
1045 return;
1046 if (!tty_term_has(tty->term, TTYC_CSR))
1047 return;
1049 tty->rupper = rupper;
1050 tty->rlower = rlower;
1053 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1054 * 0,0 if it is beyond the last column (they do not reset their wrap
1055 * flag so further output causes a line feed). As a workaround, do an
1056 * explicit move to 0 first.
1058 if (tty->cx >= tty->sx)
1059 tty_cursor(tty, 0, tty->cy);
1061 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1062 tty_cursor(tty, 0, 0);
1065 /* Move cursor inside pane. */
1066 void
1067 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1069 struct window_pane *wp = ctx->wp;
1071 tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
1074 /* Move cursor to absolute position. */
1075 void
1076 tty_cursor(struct tty *tty, u_int cx, u_int cy)
1078 struct tty_term *term = tty->term;
1079 u_int thisx, thisy;
1080 int change;
1082 if (cx > tty->sx - 1)
1083 cx = tty->sx - 1;
1085 thisx = tty->cx;
1086 thisy = tty->cy;
1088 /* No change. */
1089 if (cx == thisx && cy == thisy)
1090 return;
1092 /* Very end of the line, just use absolute movement. */
1093 if (thisx > tty->sx - 1)
1094 goto absolute;
1096 /* Move to home position (0, 0). */
1097 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1098 tty_putcode(tty, TTYC_HOME);
1099 goto out;
1102 /* Zero on the next line. */
1103 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1104 tty_putc(tty, '\r');
1105 tty_putc(tty, '\n');
1106 goto out;
1109 /* Moving column or row. */
1110 if (cy == thisy) {
1112 * Moving column only, row staying the same.
1115 /* To left edge. */
1116 if (cx == 0) {
1117 tty_putc(tty, '\r');
1118 goto out;
1121 /* One to the left. */
1122 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1123 tty_putcode(tty, TTYC_CUB1);
1124 goto out;
1127 /* One to the right. */
1128 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1129 tty_putcode(tty, TTYC_CUF1);
1130 goto out;
1133 /* Calculate difference. */
1134 change = thisx - cx; /* +ve left, -ve right */
1137 * Use HPA if change is larger than absolute, otherwise move
1138 * the cursor with CUB/CUF.
1140 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1141 tty_putcode1(tty, TTYC_HPA, cx);
1142 goto out;
1143 } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1144 tty_putcode1(tty, TTYC_CUB, change);
1145 goto out;
1146 } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1147 tty_putcode1(tty, TTYC_CUF, -change);
1148 goto out;
1150 } else if (cx == thisx) {
1152 * Moving row only, column staying the same.
1155 /* One above. */
1156 if (thisy != tty->rupper &&
1157 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1158 tty_putcode(tty, TTYC_CUU1);
1159 goto out;
1162 /* One below. */
1163 if (thisy != tty->rlower &&
1164 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1165 tty_putcode(tty, TTYC_CUD1);
1166 goto out;
1169 /* Calculate difference. */
1170 change = thisy - cy; /* +ve up, -ve down */
1173 * Try to use VPA if change is larger than absolute or if this
1174 * change would cross the scroll region, otherwise use CUU/CUD.
1176 if ((u_int) abs(change) > cy ||
1177 (change < 0 && cy - change > tty->rlower) ||
1178 (change > 0 && cy - change < tty->rupper)) {
1179 if (tty_term_has(term, TTYC_VPA)) {
1180 tty_putcode1(tty, TTYC_VPA, cy);
1181 goto out;
1183 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1184 tty_putcode1(tty, TTYC_CUU, change);
1185 goto out;
1186 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1187 tty_putcode1(tty, TTYC_CUD, -change);
1188 goto out;
1192 absolute:
1193 /* Absolute movement. */
1194 tty_putcode2(tty, TTYC_CUP, cy, cx);
1196 out:
1197 tty->cx = cx;
1198 tty->cy = cy;
1201 void
1202 tty_attributes(struct tty *tty, const struct grid_cell *gc)
1204 struct grid_cell *tc = &tty->cell, gc2;
1205 u_char changed;
1207 memcpy(&gc2, gc, sizeof gc2);
1210 * If no setab, try to use the reverse attribute as a best-effort for a
1211 * non-default background. This is a bit of a hack but it doesn't do
1212 * any serious harm and makes a couple of applications happier.
1214 if (!tty_term_has(tty->term, TTYC_SETAB)) {
1215 if (gc2.attr & GRID_ATTR_REVERSE) {
1216 if (gc2.fg != 7 && gc2.fg != 8)
1217 gc2.attr &= ~GRID_ATTR_REVERSE;
1218 } else {
1219 if (gc2.bg != 0 && gc2.bg != 8)
1220 gc2.attr |= GRID_ATTR_REVERSE;
1224 /* Fix up the colours if necessary. */
1225 tty_check_fg(tty, &gc2);
1226 tty_check_bg(tty, &gc2);
1228 /* If any bits are being cleared, reset everything. */
1229 if (tc->attr & ~gc2.attr)
1230 tty_reset(tty);
1233 * Set the colours. This may call tty_reset() (so it comes next) and
1234 * may add to (NOT remove) the desired attributes by changing new_attr.
1236 tty_colours(tty, &gc2);
1238 /* Filter out attribute bits already set. */
1239 changed = gc2.attr & ~tc->attr;
1240 tc->attr = gc2.attr;
1242 /* Set the attributes. */
1243 if (changed & GRID_ATTR_BRIGHT)
1244 tty_putcode(tty, TTYC_BOLD);
1245 if (changed & GRID_ATTR_DIM)
1246 tty_putcode(tty, TTYC_DIM);
1247 if (changed & GRID_ATTR_ITALICS)
1249 if (tty_term_has(tty->term, TTYC_SITM))
1250 tty_putcode(tty, TTYC_SITM);
1251 else
1252 tty_putcode(tty, TTYC_SMSO);
1254 if (changed & GRID_ATTR_UNDERSCORE)
1255 tty_putcode(tty, TTYC_SMUL);
1256 if (changed & GRID_ATTR_BLINK)
1257 tty_putcode(tty, TTYC_BLINK);
1258 if (changed & GRID_ATTR_REVERSE) {
1259 if (tty_term_has(tty->term, TTYC_REV))
1260 tty_putcode(tty, TTYC_REV);
1261 else if (tty_term_has(tty->term, TTYC_SMSO))
1262 tty_putcode(tty, TTYC_SMSO);
1264 if (changed & GRID_ATTR_HIDDEN)
1265 tty_putcode(tty, TTYC_INVIS);
1266 if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1267 tty_putcode(tty, TTYC_SMACS);
1270 void
1271 tty_colours(struct tty *tty, const struct grid_cell *gc)
1273 struct grid_cell *tc = &tty->cell;
1274 u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1275 int have_ax, fg_default, bg_default;
1277 /* No changes? Nothing is necessary. */
1278 if (fg == tc->fg && bg == tc->bg &&
1279 ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1280 return;
1283 * Is either the default colour? This is handled specially because the
1284 * best solution might be to reset both colours to default, in which
1285 * case if only one is default need to fall onward to set the other
1286 * colour.
1288 fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1289 bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1290 if (fg_default || bg_default) {
1292 * If don't have AX but do have op, send sgr0 (op can't
1293 * actually be used because it is sometimes the same as sgr0
1294 * and sometimes isn't). This resets both colours to default.
1296 * Otherwise, try to set the default colour only as needed.
1298 have_ax = tty_term_has(tty->term, TTYC_AX);
1299 if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1300 tty_reset(tty);
1301 else {
1302 if (fg_default &&
1303 (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1304 if (have_ax)
1305 tty_puts(tty, "\033[39m");
1306 else if (tc->fg != 7 ||
1307 tc->flags & GRID_FLAG_FG256)
1308 tty_putcode1(tty, TTYC_SETAF, 7);
1309 tc->fg = 8;
1310 tc->flags &= ~GRID_FLAG_FG256;
1312 if (bg_default &&
1313 (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1314 if (have_ax)
1315 tty_puts(tty, "\033[49m");
1316 else if (tc->bg != 0 ||
1317 tc->flags & GRID_FLAG_BG256)
1318 tty_putcode1(tty, TTYC_SETAB, 0);
1319 tc->bg = 8;
1320 tc->flags &= ~GRID_FLAG_BG256;
1325 /* Set the foreground colour. */
1326 if (!fg_default && (fg != tc->fg ||
1327 ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1328 tty_colours_fg(tty, gc);
1331 * Set the background colour. This must come after the foreground as
1332 * tty_colour_fg() can call tty_reset().
1334 if (!bg_default && (bg != tc->bg ||
1335 ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1336 tty_colours_bg(tty, gc);
1339 void
1340 tty_check_fg(struct tty *tty, struct grid_cell *gc)
1342 u_int colours;
1344 /* Is this a 256-colour colour? */
1345 if (gc->flags & GRID_FLAG_FG256) {
1346 /* And not a 256 colour mode? */
1347 if (!(tty->term->flags & TERM_88COLOURS) &&
1348 !(tty->term_flags & TERM_88COLOURS) &&
1349 !(tty->term->flags & TERM_256COLOURS) &&
1350 !(tty->term_flags & TERM_256COLOURS)) {
1351 gc->fg = colour_256to16(gc->fg);
1352 if (gc->fg & 8) {
1353 gc->fg &= 7;
1354 gc->attr |= GRID_ATTR_BRIGHT;
1355 } else
1356 gc->attr &= ~GRID_ATTR_BRIGHT;
1357 gc->flags &= ~GRID_FLAG_FG256;
1359 return;
1362 /* Is this an aixterm colour? */
1363 colours = tty_term_number(tty->term, TTYC_COLORS);
1364 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1365 gc->fg -= 90;
1366 gc->attr |= GRID_ATTR_BRIGHT;
1370 void
1371 tty_check_bg(struct tty *tty, struct grid_cell *gc)
1373 u_int colours;
1375 /* Is this a 256-colour colour? */
1376 if (gc->flags & GRID_FLAG_BG256) {
1378 * And not a 256 colour mode? Translate to 16-colour
1379 * palette. Bold background doesn't exist portably, so just
1380 * discard the bold bit if set.
1382 if (!(tty->term->flags & TERM_88COLOURS) &&
1383 !(tty->term_flags & TERM_88COLOURS) &&
1384 !(tty->term->flags & TERM_256COLOURS) &&
1385 !(tty->term_flags & TERM_256COLOURS)) {
1386 gc->bg = colour_256to16(gc->bg);
1387 if (gc->bg & 8)
1388 gc->bg &= 7;
1389 gc->attr &= ~GRID_ATTR_BRIGHT;
1390 gc->flags &= ~GRID_FLAG_BG256;
1392 return;
1395 /* Is this an aixterm colour? */
1396 colours = tty_term_number(tty->term, TTYC_COLORS);
1397 if (gc->bg >= 100 && gc->bg <= 107 && colours < 16) {
1398 gc->bg -= 90;
1399 gc->attr |= GRID_ATTR_BRIGHT;
1403 void
1404 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1406 struct grid_cell *tc = &tty->cell;
1407 u_char fg = gc->fg;
1408 char s[32];
1410 /* Is this a 256-colour colour? */
1411 if (gc->flags & GRID_FLAG_FG256) {
1412 /* Try as 256 colours or translating to 88. */
1413 if (tty_try_256(tty, fg, "38") == 0)
1414 goto save_fg;
1415 if (tty_try_88(tty, fg, "38") == 0)
1416 goto save_fg;
1417 /* Else already handled by tty_check_fg. */
1418 return;
1421 /* Is this an aixterm bright colour? */
1422 if (fg >= 90 && fg <= 97) {
1423 xsnprintf(s, sizeof s, "\033[%dm", fg);
1424 tty_puts(tty, s);
1425 goto save_fg;
1428 /* Otherwise set the foreground colour. */
1429 tty_putcode1(tty, TTYC_SETAF, fg);
1431 save_fg:
1432 /* Save the new values in the terminal current cell. */
1433 tc->fg = fg;
1434 tc->flags &= ~GRID_FLAG_FG256;
1435 tc->flags |= gc->flags & GRID_FLAG_FG256;
1438 void
1439 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1441 struct grid_cell *tc = &tty->cell;
1442 u_char bg = gc->bg;
1443 char s[32];
1445 /* Is this a 256-colour colour? */
1446 if (gc->flags & GRID_FLAG_BG256) {
1447 /* Try as 256 colours or translating to 88. */
1448 if (tty_try_256(tty, bg, "48") == 0)
1449 goto save_bg;
1450 if (tty_try_88(tty, bg, "48") == 0)
1451 goto save_bg;
1452 /* Else already handled by tty_check_bg. */
1453 return;
1456 /* Is this an aixterm bright colour? */
1457 if (bg >= 100 && bg <= 107) {
1458 /* 16 colour terminals or above only. */
1459 if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1460 xsnprintf(s, sizeof s, "\033[%dm", bg);
1461 tty_puts(tty, s);
1462 goto save_bg;
1464 bg -= 100;
1465 /* no such thing as a bold background */
1468 /* Otherwise set the background colour. */
1469 tty_putcode1(tty, TTYC_SETAB, bg);
1471 save_bg:
1472 /* Save the new values in the terminal current cell. */
1473 tc->bg = bg;
1474 tc->flags &= ~GRID_FLAG_BG256;
1475 tc->flags |= gc->flags & GRID_FLAG_BG256;
1479 tty_try_256(struct tty *tty, u_char colour, const char *type)
1481 char s[32];
1483 if (!(tty->term->flags & TERM_256COLOURS) &&
1484 !(tty->term_flags & TERM_256COLOURS))
1485 return (-1);
1487 xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1488 tty_puts(tty, s);
1489 return (0);
1493 tty_try_88(struct tty *tty, u_char colour, const char *type)
1495 char s[32];
1497 if (!(tty->term->flags & TERM_88COLOURS) &&
1498 !(tty->term_flags & TERM_88COLOURS))
1499 return (-1);
1500 colour = colour_256to88(colour);
1502 xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1503 tty_puts(tty, s);
1504 return (0);