Remove tmux's (already minimal) 88 colour support. Such terminals are
[tmux-openbsd.git] / tty.c
blobbffddac6c46c609e72893ce9334c40a4305ecc87
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 *);
39 void tty_colours(struct tty *, const struct grid_cell *);
40 void tty_check_fg(struct tty *, struct grid_cell *);
41 void tty_check_bg(struct tty *, struct grid_cell *);
42 void tty_colours_fg(struct tty *, const struct grid_cell *);
43 void tty_colours_bg(struct tty *, const struct grid_cell *);
45 int tty_large_region(struct tty *, const struct tty_ctx *);
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_repeat_space(struct tty *, u_int);
50 void tty_cell(struct tty *, const struct grid_cell *);
52 #define tty_use_acs(tty) \
53 (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
55 #define tty_pane_full_width(tty, ctx) \
56 ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
58 void
59 tty_init(struct tty *tty, struct client *c, int fd, char *term)
61 char *path;
63 memset(tty, 0, sizeof *tty);
64 tty->log_fd = -1;
66 if (term == NULL || *term == '\0')
67 tty->termname = xstrdup("unknown");
68 else
69 tty->termname = xstrdup(term);
70 tty->fd = fd;
71 tty->client = c;
73 if ((path = ttyname(fd)) == NULL)
74 fatalx("ttyname failed");
75 tty->path = xstrdup(path);
76 tty->cstyle = 0;
77 tty->ccolour = xstrdup("");
79 tty->flags = 0;
80 tty->term_flags = 0;
83 int
84 tty_resize(struct tty *tty)
86 struct winsize ws;
87 u_int sx, sy;
89 if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
90 sx = ws.ws_col;
91 if (sx == 0)
92 sx = 80;
93 sy = ws.ws_row;
94 if (sy == 0)
95 sy = 24;
96 } else {
97 sx = 80;
98 sy = 24;
100 if (!tty_set_size(tty, sx, sy))
101 return (0);
103 tty->cx = UINT_MAX;
104 tty->cy = UINT_MAX;
106 tty->rupper = UINT_MAX;
107 tty->rlower = UINT_MAX;
110 * If the terminal has been started, reset the actual scroll region and
111 * cursor position, as this may not have happened.
113 if (tty->flags & TTY_STARTED) {
114 tty_cursor(tty, 0, 0);
115 tty_region(tty, 0, tty->sy - 1);
118 return (1);
122 tty_set_size(struct tty *tty, u_int sx, u_int sy) {
123 if (sx == tty->sx && sy == tty->sy)
124 return (0);
125 tty->sx = sx;
126 tty->sy = sy;
127 return (1);
131 tty_open(struct tty *tty, const char *overrides, char **cause)
133 char out[64];
134 int fd;
136 if (debug_level > 3) {
137 xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
138 fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
139 if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
140 fatal("fcntl failed");
141 tty->log_fd = fd;
144 tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
145 if (tty->term == NULL) {
146 tty_close(tty);
147 return (-1);
149 tty->flags |= TTY_OPENED;
151 tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_TIMER);
153 tty->event = bufferevent_new(
154 tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
156 tty_start_tty(tty);
158 tty_keys_build(tty);
160 return (0);
163 void
164 tty_read_callback(unused struct bufferevent *bufev, void *data)
166 struct tty *tty = data;
168 while (tty_keys_next(tty))
172 void
173 tty_error_callback(
174 unused struct bufferevent *bufev, unused short what, unused void *data)
178 void
179 tty_init_termios(int fd, struct termios *orig_tio, struct bufferevent *bufev)
181 struct termios tio;
183 if (fd == -1 || tcgetattr(fd, orig_tio) != 0)
184 return;
186 setblocking(fd, 0);
188 if (bufev != NULL)
189 bufferevent_enable(bufev, EV_READ|EV_WRITE);
191 memcpy(&tio, orig_tio, sizeof tio);
192 tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
193 tio.c_iflag |= IGNBRK;
194 tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
195 tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
196 ECHOPRT|ECHOKE|ECHOCTL|ISIG);
197 tio.c_cc[VMIN] = 1;
198 tio.c_cc[VTIME] = 0;
199 if (tcsetattr(fd, TCSANOW, &tio) == 0)
200 tcflush(fd, TCIOFLUSH);
203 void
204 tty_start_tty(struct tty *tty)
206 tty_init_termios(tty->fd, &tty->tio, tty->event);
208 tty_putcode(tty, TTYC_SMCUP);
210 tty_putcode(tty, TTYC_SGR0);
211 memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
213 tty_putcode(tty, TTYC_RMKX);
214 if (tty_use_acs(tty))
215 tty_putcode(tty, TTYC_ENACS);
216 tty_putcode(tty, TTYC_CLEAR);
218 tty_putcode(tty, TTYC_CNORM);
219 if (tty_term_has(tty->term, TTYC_KMOUS))
220 tty_puts(tty, "\033[?1000l\033[?1006l\033[?1005l");
222 if (tty_term_has(tty->term, TTYC_XT))
223 tty_puts(tty, "\033[c\033[>4;1m\033[?1004h");
225 tty->cx = UINT_MAX;
226 tty->cy = UINT_MAX;
228 tty->rlower = UINT_MAX;
229 tty->rupper = UINT_MAX;
231 tty->mode = MODE_CURSOR;
233 tty->flags |= TTY_STARTED;
235 tty_force_cursor_colour(tty, "");
238 void
239 tty_set_class(struct tty *tty, u_int class)
241 if (tty->class != 0)
242 return;
243 tty->class = class;
246 void
247 tty_stop_tty(struct tty *tty)
249 struct winsize ws;
251 if (!(tty->flags & TTY_STARTED))
252 return;
253 tty->flags &= ~TTY_STARTED;
255 bufferevent_disable(tty->event, EV_READ|EV_WRITE);
258 * Be flexible about error handling and try not kill the server just
259 * because the fd is invalid. Things like ssh -t can easily leave us
260 * with a dead tty.
262 if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
263 return;
264 if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
265 return;
267 tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
268 if (tty_use_acs(tty))
269 tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
270 tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
271 tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
272 tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
273 if (tty_term_has(tty->term, TTYC_CS1) && tty->cstyle != 0) {
274 if (tty_term_has(tty->term, TTYC_CSR1))
275 tty_raw(tty, tty_term_string(tty->term, TTYC_CSR1));
276 else
277 tty_raw(tty, tty_term_string1(tty->term, TTYC_CS1, 0));
279 tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
281 tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
282 if (tty_term_has(tty->term, TTYC_KMOUS))
283 tty_raw(tty, "\033[?1000l\033[?1006l\033[?1005l");
285 if (tty_term_has(tty->term, TTYC_XT))
286 tty_raw(tty, "\033[>4m\033[?1004l");
288 tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
290 setblocking(tty->fd, 1);
293 void
294 tty_close(struct tty *tty)
296 if (tty->log_fd != -1) {
297 close(tty->log_fd);
298 tty->log_fd = -1;
301 if (event_initialized(&tty->key_timer))
302 evtimer_del(&tty->key_timer);
303 tty_stop_tty(tty);
305 if (tty->flags & TTY_OPENED) {
306 bufferevent_free(tty->event);
308 tty_term_free(tty->term);
309 tty_keys_free(tty);
311 tty->flags &= ~TTY_OPENED;
314 if (tty->fd != -1) {
315 close(tty->fd);
316 tty->fd = -1;
320 void
321 tty_free(struct tty *tty)
323 tty_close(tty);
325 free(tty->ccolour);
326 if (tty->path != NULL)
327 free(tty->path);
328 if (tty->termname != NULL)
329 free(tty->termname);
332 void
333 tty_raw(struct tty *tty, const char *s)
335 ssize_t n, slen;
336 u_int i;
338 slen = strlen(s);
339 for (i = 0; i < 5; i++) {
340 n = write(tty->fd, s, slen);
341 if (n >= 0) {
342 s += n;
343 slen -= n;
344 if (slen == 0)
345 break;
346 } else if (n == -1 && errno != EAGAIN)
347 break;
348 usleep(100);
352 void
353 tty_putcode(struct tty *tty, enum tty_code_code code)
355 tty_puts(tty, tty_term_string(tty->term, code));
358 void
359 tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
361 if (a < 0)
362 return;
363 tty_puts(tty, tty_term_string1(tty->term, code, a));
366 void
367 tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
369 if (a < 0 || b < 0)
370 return;
371 tty_puts(tty, tty_term_string2(tty->term, code, a, b));
374 void
375 tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
377 if (a != NULL)
378 tty_puts(tty, tty_term_ptr1(tty->term, code, a));
381 void
382 tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a, const void *b)
384 if (a != NULL && b != NULL)
385 tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
388 void
389 tty_puts(struct tty *tty, const char *s)
391 if (*s == '\0')
392 return;
393 bufferevent_write(tty->event, s, strlen(s));
395 if (tty->log_fd != -1)
396 write(tty->log_fd, s, strlen(s));
399 void
400 tty_putc(struct tty *tty, u_char ch)
402 const char *acs;
403 u_int sx;
405 if (tty->cell.attr & GRID_ATTR_CHARSET) {
406 acs = tty_acs_get(tty, ch);
407 if (acs != NULL)
408 bufferevent_write(tty->event, acs, strlen(acs));
409 else
410 bufferevent_write(tty->event, &ch, 1);
411 } else
412 bufferevent_write(tty->event, &ch, 1);
414 if (ch >= 0x20 && ch != 0x7f) {
415 sx = tty->sx;
416 if (tty->term->flags & TERM_EARLYWRAP)
417 sx--;
419 if (tty->cx >= sx) {
420 tty->cx = 1;
421 if (tty->cy != tty->rlower)
422 tty->cy++;
423 } else
424 tty->cx++;
427 if (tty->log_fd != -1)
428 write(tty->log_fd, &ch, 1);
431 void
432 tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
434 bufferevent_write(tty->event, buf, len);
435 if (tty->log_fd != -1)
436 write(tty->log_fd, buf, len);
437 tty->cx += width;
440 void
441 tty_set_title(struct tty *tty, const char *title)
443 if (!tty_term_has(tty->term, TTYC_TSL) ||
444 !tty_term_has(tty->term, TTYC_FSL))
445 return;
447 tty_putcode(tty, TTYC_TSL);
448 tty_puts(tty, title);
449 tty_putcode(tty, TTYC_FSL);
452 void
453 tty_force_cursor_colour(struct tty *tty, const char *ccolour)
455 if (*ccolour == '\0')
456 tty_putcode(tty, TTYC_CR);
457 else
458 tty_putcode_ptr1(tty, TTYC_CC, ccolour);
459 free(tty->ccolour);
460 tty->ccolour = xstrdup(ccolour);
463 void
464 tty_update_mode(struct tty *tty, int mode, struct screen *s)
466 int changed;
468 if (strcmp(s->ccolour, tty->ccolour))
469 tty_force_cursor_colour(tty, s->ccolour);
471 if (tty->flags & TTY_NOCURSOR)
472 mode &= ~MODE_CURSOR;
474 changed = mode ^ tty->mode;
475 if (changed & MODE_CURSOR) {
476 if (mode & MODE_CURSOR)
477 tty_putcode(tty, TTYC_CNORM);
478 else
479 tty_putcode(tty, TTYC_CIVIS);
481 if (tty->cstyle != s->cstyle) {
482 if (tty_term_has(tty->term, TTYC_CS1)) {
483 if (s->cstyle == 0 &&
484 tty_term_has(tty->term, TTYC_CSR1))
485 tty_putcode(tty, TTYC_CSR1);
486 else
487 tty_putcode1(tty, TTYC_CS1, s->cstyle);
489 tty->cstyle = s->cstyle;
491 if (changed & (ALL_MOUSE_MODES|MODE_MOUSE_UTF8)) {
492 if (mode & ALL_MOUSE_MODES) {
494 * Enable the UTF-8 (1005) extension if configured to.
495 * Enable the SGR (1006) extension unconditionally, as
496 * this is safe from misinterpretation. Do it in this
497 * order, because in some terminals it's the last one
498 * that takes effect and SGR is the preferred one.
500 if (mode & MODE_MOUSE_UTF8)
501 tty_puts(tty, "\033[?1005h");
502 else
503 tty_puts(tty, "\033[?1005l");
504 tty_puts(tty, "\033[?1006h");
506 if (mode & MODE_MOUSE_ANY)
507 tty_puts(tty, "\033[?1003h");
508 else if (mode & MODE_MOUSE_BUTTON)
509 tty_puts(tty, "\033[?1002h");
510 else if (mode & MODE_MOUSE_STANDARD)
511 tty_puts(tty, "\033[?1000h");
512 } else {
513 if (tty->mode & MODE_MOUSE_ANY)
514 tty_puts(tty, "\033[?1003l");
515 else if (tty->mode & MODE_MOUSE_BUTTON)
516 tty_puts(tty, "\033[?1002l");
517 else if (tty->mode & MODE_MOUSE_STANDARD)
518 tty_puts(tty, "\033[?1000l");
520 tty_puts(tty, "\033[?1006l");
521 if (tty->mode & MODE_MOUSE_UTF8)
522 tty_puts(tty, "\033[?1005l");
525 if (changed & MODE_KKEYPAD) {
526 if (mode & MODE_KKEYPAD)
527 tty_putcode(tty, TTYC_SMKX);
528 else
529 tty_putcode(tty, TTYC_RMKX);
531 if (changed & MODE_BRACKETPASTE) {
532 if (mode & MODE_BRACKETPASTE)
533 tty_puts(tty, "\033[?2004h");
534 else
535 tty_puts(tty, "\033[?2004l");
537 tty->mode = mode;
540 void
541 tty_emulate_repeat(
542 struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
544 if (tty_term_has(tty->term, code))
545 tty_putcode1(tty, code, n);
546 else {
547 while (n-- > 0)
548 tty_putcode(tty, code1);
552 void
553 tty_repeat_space(struct tty *tty, u_int n)
555 while (n-- > 0)
556 tty_putc(tty, ' ');
560 * Is the region large enough to be worth redrawing once later rather than
561 * probably several times now? Currently yes if it is more than 50% of the
562 * pane.
565 tty_large_region(unused struct tty *tty, const struct tty_ctx *ctx)
567 struct window_pane *wp = ctx->wp;
569 return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
573 * Redraw scroll region using data from screen (already updated). Used when
574 * CSR not supported, or window is a pane that doesn't take up the full
575 * width of the terminal.
577 void
578 tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
580 struct window_pane *wp = ctx->wp;
581 struct screen *s = wp->screen;
582 u_int i;
585 * If region is large, schedule a window redraw. In most cases this is
586 * likely to be followed by some more scrolling.
588 if (tty_large_region(tty, ctx)) {
589 wp->flags |= PANE_REDRAW;
590 return;
593 if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
594 for (i = ctx->ocy; i < screen_size_y(s); i++)
595 tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
596 } else {
597 for (i = ctx->orupper; i <= ctx->orlower; i++)
598 tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
602 void
603 tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
605 const struct grid_cell *gc;
606 struct grid_line *gl;
607 struct grid_cell tmpgc;
608 struct utf8_data ud;
609 u_int i, sx;
611 tty_update_mode(tty, tty->mode & ~MODE_CURSOR, s);
613 sx = screen_size_x(s);
614 if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
615 sx = s->grid->linedata[s->grid->hsize + py].cellsize;
616 if (sx > tty->sx)
617 sx = tty->sx;
620 * Don't move the cursor to the start permission if it will wrap there
621 * itself.
623 gl = NULL;
624 if (py != 0)
625 gl = &s->grid->linedata[s->grid->hsize + py - 1];
626 if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
627 tty->cx < tty->sx || ox != 0 ||
628 (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
629 tty_cursor(tty, ox, oy + py);
631 for (i = 0; i < sx; i++) {
632 gc = grid_view_peek_cell(s->grid, i, py);
633 if (screen_check_selection(s, i, py)) {
634 memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
635 grid_cell_get(gc, &ud);
636 grid_cell_set(&tmpgc, &ud);
637 tmpgc.flags = gc->flags &
638 ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
639 tmpgc.flags |= s->sel.cell.flags &
640 (GRID_FLAG_FG256|GRID_FLAG_BG256);
641 tty_cell(tty, &tmpgc);
642 } else
643 tty_cell(tty, gc);
646 if (sx >= tty->sx) {
647 tty_update_mode(tty, tty->mode, s);
648 return;
650 tty_reset(tty);
652 tty_cursor(tty, ox + sx, oy + py);
653 if (sx != screen_size_x(s) && ox + screen_size_x(s) >= tty->sx &&
654 tty_term_has(tty->term, TTYC_EL))
655 tty_putcode(tty, TTYC_EL);
656 else
657 tty_repeat_space(tty, screen_size_x(s) - sx);
658 tty_update_mode(tty, tty->mode, s);
661 void
662 tty_write(
663 void (*cmdfn)(struct tty *, const struct tty_ctx *), struct tty_ctx *ctx)
665 struct window_pane *wp = ctx->wp;
666 struct client *c;
667 u_int i;
669 /* wp can be NULL if updating the screen but not the terminal. */
670 if (wp == NULL)
671 return;
673 if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
674 return;
675 if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
676 return;
678 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
679 c = ARRAY_ITEM(&clients, i);
680 if (c == NULL || c->session == NULL || c->tty.term == NULL)
681 continue;
682 if (c->flags & CLIENT_SUSPENDED)
683 continue;
684 if (c->tty.flags & TTY_FREEZE)
685 continue;
686 if (c->session->curw->window != wp->window)
687 continue;
689 ctx->xoff = wp->xoff;
690 ctx->yoff = wp->yoff;
691 if (status_at_line(c) == 0)
692 ctx->yoff++;
694 cmdfn(&c->tty, ctx);
698 void
699 tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
701 struct window_pane *wp = ctx->wp;
703 if (!tty_pane_full_width(tty, ctx)) {
704 tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
705 return;
708 tty_reset(tty);
710 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
712 if (tty_term_has(tty->term, TTYC_ICH) ||
713 tty_term_has(tty->term, TTYC_ICH1))
714 tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
715 else
716 tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
719 void
720 tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
722 struct window_pane *wp = ctx->wp;
724 if (!tty_pane_full_width(tty, ctx) ||
725 (!tty_term_has(tty->term, TTYC_DCH) &&
726 !tty_term_has(tty->term, TTYC_DCH1))) {
727 tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
728 return;
731 tty_reset(tty);
733 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
735 if (tty_term_has(tty->term, TTYC_DCH) ||
736 tty_term_has(tty->term, TTYC_DCH1))
737 tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
740 void
741 tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
743 u_int i;
745 tty_reset(tty);
747 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
749 if (tty_term_has(tty->term, TTYC_ECH))
750 tty_putcode1(tty, TTYC_ECH, ctx->num);
751 else {
752 for (i = 0; i < ctx->num; i++)
753 tty_putc(tty, ' ');
757 void
758 tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
760 if (!tty_pane_full_width(tty, ctx) ||
761 !tty_term_has(tty->term, TTYC_CSR) ||
762 !tty_term_has(tty->term, TTYC_IL1)) {
763 tty_redraw_region(tty, ctx);
764 return;
767 tty_reset(tty);
769 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
770 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
772 tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
775 void
776 tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
778 if (!tty_pane_full_width(tty, ctx) ||
779 !tty_term_has(tty->term, TTYC_CSR) ||
780 !tty_term_has(tty->term, TTYC_DL1)) {
781 tty_redraw_region(tty, ctx);
782 return;
785 tty_reset(tty);
787 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
788 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
790 tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
793 void
794 tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
796 struct window_pane *wp = ctx->wp;
797 struct screen *s = wp->screen;
799 tty_reset(tty);
801 tty_cursor_pane(tty, ctx, 0, ctx->ocy);
803 if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
804 tty_putcode(tty, TTYC_EL);
805 else
806 tty_repeat_space(tty, screen_size_x(s));
809 void
810 tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
812 struct window_pane *wp = ctx->wp;
813 struct screen *s = wp->screen;
815 tty_reset(tty);
817 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
819 if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
820 tty_putcode(tty, TTYC_EL);
821 else
822 tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
825 void
826 tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
828 tty_reset(tty);
830 if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
831 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
832 tty_putcode(tty, TTYC_EL1);
833 } else {
834 tty_cursor_pane(tty, ctx, 0, ctx->ocy);
835 tty_repeat_space(tty, ctx->ocx + 1);
839 void
840 tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
842 if (ctx->ocy != ctx->orupper)
843 return;
845 if (!tty_pane_full_width(tty, ctx) ||
846 !tty_term_has(tty->term, TTYC_CSR) ||
847 !tty_term_has(tty->term, TTYC_RI)) {
848 tty_redraw_region(tty, ctx);
849 return;
852 tty_reset(tty);
854 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
855 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
857 tty_putcode(tty, TTYC_RI);
860 void
861 tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
863 struct window_pane *wp = ctx->wp;
865 if (ctx->ocy != ctx->orlower)
866 return;
868 if (!tty_pane_full_width(tty, ctx) ||
869 !tty_term_has(tty->term, TTYC_CSR)) {
870 if (tty_large_region(tty, ctx))
871 wp->flags |= PANE_REDRAW;
872 else
873 tty_redraw_region(tty, ctx);
874 return;
878 * If this line wrapped naturally (ctx->num is nonzero), don't do
879 * anything - the cursor can just be moved to the last cell and wrap
880 * naturally.
882 if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
883 return;
885 tty_reset(tty);
887 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
888 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
890 tty_putc(tty, '\n');
893 void
894 tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
896 struct window_pane *wp = ctx->wp;
897 struct screen *s = wp->screen;
898 u_int i, j;
900 tty_reset(tty);
902 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
903 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
905 if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
906 tty_putcode(tty, TTYC_EL);
907 if (ctx->ocy != screen_size_y(s) - 1) {
908 tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
909 for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
910 tty_putcode(tty, TTYC_EL);
911 if (i == screen_size_y(s) - 1)
912 continue;
913 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
914 tty->cy++;
917 } else {
918 tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
919 for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
920 tty_cursor_pane(tty, ctx, 0, j);
921 tty_repeat_space(tty, screen_size_x(s));
926 void
927 tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
929 struct window_pane *wp = ctx->wp;
930 struct screen *s = wp->screen;
931 u_int i, j;
933 tty_reset(tty);
935 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
936 tty_cursor_pane(tty, ctx, 0, 0);
938 if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
939 for (i = 0; i < ctx->ocy; i++) {
940 tty_putcode(tty, TTYC_EL);
941 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
942 tty->cy++;
944 } else {
945 for (j = 0; j < ctx->ocy; j++) {
946 tty_cursor_pane(tty, ctx, 0, j);
947 tty_repeat_space(tty, screen_size_x(s));
950 tty_repeat_space(tty, ctx->ocx + 1);
953 void
954 tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
956 struct window_pane *wp = ctx->wp;
957 struct screen *s = wp->screen;
958 u_int i, j;
960 tty_reset(tty);
962 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
963 tty_cursor_pane(tty, ctx, 0, 0);
965 if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
966 for (i = 0; i < screen_size_y(s); i++) {
967 tty_putcode(tty, TTYC_EL);
968 if (i != screen_size_y(s) - 1) {
969 tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
970 tty->cy++;
973 } else {
974 for (j = 0; j < screen_size_y(s); j++) {
975 tty_cursor_pane(tty, ctx, 0, j);
976 tty_repeat_space(tty, screen_size_x(s));
981 void
982 tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
984 struct window_pane *wp = ctx->wp;
985 struct screen *s = wp->screen;
986 u_int i, j;
988 tty_reset(tty);
990 tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
992 for (j = 0; j < screen_size_y(s); j++) {
993 tty_cursor_pane(tty, ctx, 0, j);
994 for (i = 0; i < screen_size_x(s); i++)
995 tty_putc(tty, 'E');
999 void
1000 tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1002 struct window_pane *wp = ctx->wp;
1003 struct screen *s = wp->screen;
1004 u_int cx;
1005 u_int width;
1007 tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1009 /* Is the cursor in the very last position? */
1010 width = grid_cell_width(ctx->cell);
1011 if (ctx->ocx > wp->sx - width) {
1012 if (ctx->xoff != 0 || wp->sx != tty->sx) {
1014 * The pane doesn't fill the entire line, the linefeed
1015 * will already have happened, so just move the cursor.
1017 if (ctx->ocy != wp->yoff + wp->screen->rlower)
1018 tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1019 else
1020 tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1021 } else if (tty->cx < tty->sx) {
1023 * The cursor isn't in the last position already, so
1024 * move as far left as possible and redraw the last
1025 * cell to move into the last position.
1027 cx = screen_size_x(s) - grid_cell_width(&ctx->last_cell);
1028 tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1029 tty_cell(tty, &ctx->last_cell);
1031 } else
1032 tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1034 tty_cell(tty, ctx->cell);
1037 void
1038 tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1040 struct window_pane *wp = ctx->wp;
1043 * Cannot rely on not being a partial character, so just redraw the
1044 * whole line.
1046 tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1049 void
1050 tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1052 char *buf;
1053 size_t off;
1055 if (!tty_term_has(tty->term, TTYC_MS))
1056 return;
1058 off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1059 buf = xmalloc(off);
1061 b64_ntop(ctx->ptr, ctx->num, buf, off);
1062 tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1064 free(buf);
1067 void
1068 tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1070 u_int i;
1071 u_char *str = ctx->ptr;
1073 for (i = 0; i < ctx->num; i++)
1074 tty_putc(tty, str[i]);
1076 tty->cx = tty->cy = UINT_MAX;
1077 tty->rupper = tty->rlower = UINT_MAX;
1079 tty_reset(tty);
1080 tty_cursor(tty, 0, 0);
1083 void
1084 tty_cell(struct tty *tty, const struct grid_cell *gc)
1086 struct utf8_data ud;
1087 u_int i;
1089 /* Skip last character if terminal is stupid. */
1090 if (tty->term->flags & TERM_EARLYWRAP &&
1091 tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
1092 return;
1094 /* If this is a padding character, do nothing. */
1095 if (gc->flags & GRID_FLAG_PADDING)
1096 return;
1098 /* Set the attributes. */
1099 tty_attributes(tty, gc);
1101 /* Get the cell and if ASCII write with putc to do ACS translation. */
1102 grid_cell_get(gc, &ud);
1103 if (ud.size == 1) {
1104 if (*ud.data < 0x20 || *ud.data == 0x7f)
1105 return;
1106 tty_putc(tty, *ud.data);
1107 return;
1110 /* If not UTF-8, write _. */
1111 if (!(tty->flags & TTY_UTF8)) {
1112 for (i = 0; i < ud.width; i++)
1113 tty_putc(tty, '_');
1114 return;
1117 /* Write the data. */
1118 tty_putn(tty, ud.data, ud.size, ud.width);
1121 void
1122 tty_reset(struct tty *tty)
1124 struct grid_cell *gc = &tty->cell;
1126 if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
1127 return;
1129 if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1130 tty_putcode(tty, TTYC_RMACS);
1131 tty_putcode(tty, TTYC_SGR0);
1132 memcpy(gc, &grid_default_cell, sizeof *gc);
1135 /* Set region inside pane. */
1136 void
1137 tty_region_pane(
1138 struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1140 tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1143 /* Set region at absolute position. */
1144 void
1145 tty_region(struct tty *tty, u_int rupper, u_int rlower)
1147 if (tty->rlower == rlower && tty->rupper == rupper)
1148 return;
1149 if (!tty_term_has(tty->term, TTYC_CSR))
1150 return;
1152 tty->rupper = rupper;
1153 tty->rlower = rlower;
1156 * Some terminals (such as PuTTY) do not correctly reset the cursor to
1157 * 0,0 if it is beyond the last column (they do not reset their wrap
1158 * flag so further output causes a line feed). As a workaround, do an
1159 * explicit move to 0 first.
1161 if (tty->cx >= tty->sx)
1162 tty_cursor(tty, 0, tty->cy);
1164 tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1165 tty_cursor(tty, 0, 0);
1168 /* Move cursor inside pane. */
1169 void
1170 tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1172 tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1175 /* Move cursor to absolute position. */
1176 void
1177 tty_cursor(struct tty *tty, u_int cx, u_int cy)
1179 struct tty_term *term = tty->term;
1180 u_int thisx, thisy;
1181 int change;
1183 if (cx > tty->sx - 1)
1184 cx = tty->sx - 1;
1186 thisx = tty->cx;
1187 thisy = tty->cy;
1189 /* No change. */
1190 if (cx == thisx && cy == thisy)
1191 return;
1193 /* Very end of the line, just use absolute movement. */
1194 if (thisx > tty->sx - 1)
1195 goto absolute;
1197 /* Move to home position (0, 0). */
1198 if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1199 tty_putcode(tty, TTYC_HOME);
1200 goto out;
1203 /* Zero on the next line. */
1204 if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1205 tty_putc(tty, '\r');
1206 tty_putc(tty, '\n');
1207 goto out;
1210 /* Moving column or row. */
1211 if (cy == thisy) {
1213 * Moving column only, row staying the same.
1216 /* To left edge. */
1217 if (cx == 0) {
1218 tty_putc(tty, '\r');
1219 goto out;
1222 /* One to the left. */
1223 if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1224 tty_putcode(tty, TTYC_CUB1);
1225 goto out;
1228 /* One to the right. */
1229 if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1230 tty_putcode(tty, TTYC_CUF1);
1231 goto out;
1234 /* Calculate difference. */
1235 change = thisx - cx; /* +ve left, -ve right */
1238 * Use HPA if change is larger than absolute, otherwise move
1239 * the cursor with CUB/CUF.
1241 if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1242 tty_putcode1(tty, TTYC_HPA, cx);
1243 goto out;
1244 } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1245 tty_putcode1(tty, TTYC_CUB, change);
1246 goto out;
1247 } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1248 tty_putcode1(tty, TTYC_CUF, -change);
1249 goto out;
1251 } else if (cx == thisx) {
1253 * Moving row only, column staying the same.
1256 /* One above. */
1257 if (thisy != tty->rupper &&
1258 cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1259 tty_putcode(tty, TTYC_CUU1);
1260 goto out;
1263 /* One below. */
1264 if (thisy != tty->rlower &&
1265 cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1266 tty_putcode(tty, TTYC_CUD1);
1267 goto out;
1270 /* Calculate difference. */
1271 change = thisy - cy; /* +ve up, -ve down */
1274 * Try to use VPA if change is larger than absolute or if this
1275 * change would cross the scroll region, otherwise use CUU/CUD.
1277 if ((u_int) abs(change) > cy ||
1278 (change < 0 && cy - change > tty->rlower) ||
1279 (change > 0 && cy - change < tty->rupper)) {
1280 if (tty_term_has(term, TTYC_VPA)) {
1281 tty_putcode1(tty, TTYC_VPA, cy);
1282 goto out;
1284 } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1285 tty_putcode1(tty, TTYC_CUU, change);
1286 goto out;
1287 } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1288 tty_putcode1(tty, TTYC_CUD, -change);
1289 goto out;
1293 absolute:
1294 /* Absolute movement. */
1295 tty_putcode2(tty, TTYC_CUP, cy, cx);
1297 out:
1298 tty->cx = cx;
1299 tty->cy = cy;
1302 void
1303 tty_attributes(struct tty *tty, const struct grid_cell *gc)
1305 struct grid_cell *tc = &tty->cell, gc2;
1306 u_char changed;
1308 memcpy(&gc2, gc, sizeof gc2);
1311 * If no setab, try to use the reverse attribute as a best-effort for a
1312 * non-default background. This is a bit of a hack but it doesn't do
1313 * any serious harm and makes a couple of applications happier.
1315 if (!tty_term_has(tty->term, TTYC_SETAB)) {
1316 if (gc2.attr & GRID_ATTR_REVERSE) {
1317 if (gc2.fg != 7 && gc2.fg != 8)
1318 gc2.attr &= ~GRID_ATTR_REVERSE;
1319 } else {
1320 if (gc2.bg != 0 && gc2.bg != 8)
1321 gc2.attr |= GRID_ATTR_REVERSE;
1325 /* Fix up the colours if necessary. */
1326 tty_check_fg(tty, &gc2);
1327 tty_check_bg(tty, &gc2);
1329 /* If any bits are being cleared, reset everything. */
1330 if (tc->attr & ~gc2.attr)
1331 tty_reset(tty);
1334 * Set the colours. This may call tty_reset() (so it comes next) and
1335 * may add to (NOT remove) the desired attributes by changing new_attr.
1337 tty_colours(tty, &gc2);
1339 /* Filter out attribute bits already set. */
1340 changed = gc2.attr & ~tc->attr;
1341 tc->attr = gc2.attr;
1343 /* Set the attributes. */
1344 if (changed & GRID_ATTR_BRIGHT)
1345 tty_putcode(tty, TTYC_BOLD);
1346 if (changed & GRID_ATTR_DIM)
1347 tty_putcode(tty, TTYC_DIM);
1348 if (changed & GRID_ATTR_ITALICS)
1350 if (tty_term_has(tty->term, TTYC_SITM))
1351 tty_putcode(tty, TTYC_SITM);
1352 else
1353 tty_putcode(tty, TTYC_SMSO);
1355 if (changed & GRID_ATTR_UNDERSCORE)
1356 tty_putcode(tty, TTYC_SMUL);
1357 if (changed & GRID_ATTR_BLINK)
1358 tty_putcode(tty, TTYC_BLINK);
1359 if (changed & GRID_ATTR_REVERSE) {
1360 if (tty_term_has(tty->term, TTYC_REV))
1361 tty_putcode(tty, TTYC_REV);
1362 else if (tty_term_has(tty->term, TTYC_SMSO))
1363 tty_putcode(tty, TTYC_SMSO);
1365 if (changed & GRID_ATTR_HIDDEN)
1366 tty_putcode(tty, TTYC_INVIS);
1367 if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1368 tty_putcode(tty, TTYC_SMACS);
1371 void
1372 tty_colours(struct tty *tty, const struct grid_cell *gc)
1374 struct grid_cell *tc = &tty->cell;
1375 u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1376 int have_ax, fg_default, bg_default;
1378 /* No changes? Nothing is necessary. */
1379 if (fg == tc->fg && bg == tc->bg &&
1380 ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1381 return;
1384 * Is either the default colour? This is handled specially because the
1385 * best solution might be to reset both colours to default, in which
1386 * case if only one is default need to fall onward to set the other
1387 * colour.
1389 fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1390 bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1391 if (fg_default || bg_default) {
1393 * If don't have AX but do have op, send sgr0 (op can't
1394 * actually be used because it is sometimes the same as sgr0
1395 * and sometimes isn't). This resets both colours to default.
1397 * Otherwise, try to set the default colour only as needed.
1399 have_ax = tty_term_has(tty->term, TTYC_AX);
1400 if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1401 tty_reset(tty);
1402 else {
1403 if (fg_default &&
1404 (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1405 if (have_ax)
1406 tty_puts(tty, "\033[39m");
1407 else if (tc->fg != 7 ||
1408 tc->flags & GRID_FLAG_FG256)
1409 tty_putcode1(tty, TTYC_SETAF, 7);
1410 tc->fg = 8;
1411 tc->flags &= ~GRID_FLAG_FG256;
1413 if (bg_default &&
1414 (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1415 if (have_ax)
1416 tty_puts(tty, "\033[49m");
1417 else if (tc->bg != 0 ||
1418 tc->flags & GRID_FLAG_BG256)
1419 tty_putcode1(tty, TTYC_SETAB, 0);
1420 tc->bg = 8;
1421 tc->flags &= ~GRID_FLAG_BG256;
1426 /* Set the foreground colour. */
1427 if (!fg_default && (fg != tc->fg ||
1428 ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1429 tty_colours_fg(tty, gc);
1432 * Set the background colour. This must come after the foreground as
1433 * tty_colour_fg() can call tty_reset().
1435 if (!bg_default && (bg != tc->bg ||
1436 ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1437 tty_colours_bg(tty, gc);
1440 void
1441 tty_check_fg(struct tty *tty, struct grid_cell *gc)
1443 u_int colours;
1445 /* Is this a 256-colour colour? */
1446 if (gc->flags & GRID_FLAG_FG256) {
1447 /* And not a 256 colour mode? */
1448 if (!(tty->term->flags & TERM_256COLOURS) &&
1449 !(tty->term_flags & TERM_256COLOURS)) {
1450 gc->fg = colour_256to16(gc->fg);
1451 if (gc->fg & 8) {
1452 gc->fg &= 7;
1453 gc->attr |= GRID_ATTR_BRIGHT;
1454 } else
1455 gc->attr &= ~GRID_ATTR_BRIGHT;
1456 gc->flags &= ~GRID_FLAG_FG256;
1458 return;
1461 /* Is this an aixterm colour? */
1462 colours = tty_term_number(tty->term, TTYC_COLORS);
1463 if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1464 gc->fg -= 90;
1465 gc->attr |= GRID_ATTR_BRIGHT;
1469 void
1470 tty_check_bg(struct tty *tty, struct grid_cell *gc)
1472 u_int colours;
1474 /* Is this a 256-colour colour? */
1475 if (gc->flags & GRID_FLAG_BG256) {
1477 * And not a 256 colour mode? Translate to 16-colour
1478 * palette. Bold background doesn't exist portably, so just
1479 * discard the bold bit if set.
1481 if (!(tty->term->flags & TERM_256COLOURS) &&
1482 !(tty->term_flags & TERM_256COLOURS)) {
1483 gc->bg = colour_256to16(gc->bg);
1484 if (gc->bg & 8)
1485 gc->bg &= 7;
1486 gc->attr &= ~GRID_ATTR_BRIGHT;
1487 gc->flags &= ~GRID_FLAG_BG256;
1489 return;
1492 /* Is this an aixterm colour? */
1493 colours = tty_term_number(tty->term, TTYC_COLORS);
1494 if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) {
1495 gc->bg -= 90;
1496 gc->attr |= GRID_ATTR_BRIGHT;
1500 void
1501 tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1503 struct grid_cell *tc = &tty->cell;
1504 u_char fg = gc->fg;
1505 char s[32];
1507 /* Is this a 256-colour colour? */
1508 if (gc->flags & GRID_FLAG_FG256) {
1509 /* Try as 256 colours. */
1510 if (tty_try_256(tty, fg, "38") == 0)
1511 goto save_fg;
1512 /* Else already handled by tty_check_fg. */
1513 return;
1516 /* Is this an aixterm bright colour? */
1517 if (fg >= 90 && fg <= 97) {
1518 xsnprintf(s, sizeof s, "\033[%dm", fg);
1519 tty_puts(tty, s);
1520 goto save_fg;
1523 /* Otherwise set the foreground colour. */
1524 tty_putcode1(tty, TTYC_SETAF, fg);
1526 save_fg:
1527 /* Save the new values in the terminal current cell. */
1528 tc->fg = fg;
1529 tc->flags &= ~GRID_FLAG_FG256;
1530 tc->flags |= gc->flags & GRID_FLAG_FG256;
1533 void
1534 tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1536 struct grid_cell *tc = &tty->cell;
1537 u_char bg = gc->bg;
1538 char s[32];
1540 /* Is this a 256-colour colour? */
1541 if (gc->flags & GRID_FLAG_BG256) {
1542 /* Try as 256 colours. */
1543 if (tty_try_256(tty, bg, "48") == 0)
1544 goto save_bg;
1545 /* Else already handled by tty_check_bg. */
1546 return;
1549 /* Is this an aixterm bright colour? */
1550 if (bg >= 90 && bg <= 97) {
1551 /* 16 colour terminals or above only. */
1552 if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1553 xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
1554 tty_puts(tty, s);
1555 goto save_bg;
1557 bg -= 90;
1558 /* no such thing as a bold background */
1561 /* Otherwise set the background colour. */
1562 tty_putcode1(tty, TTYC_SETAB, bg);
1564 save_bg:
1565 /* Save the new values in the terminal current cell. */
1566 tc->bg = bg;
1567 tc->flags &= ~GRID_FLAG_BG256;
1568 tc->flags |= gc->flags & GRID_FLAG_BG256;
1572 tty_try_256(struct tty *tty, u_char colour, const char *type)
1574 char s[32];
1576 if (!(tty->term->flags & TERM_256COLOURS) &&
1577 !(tty->term_flags & TERM_256COLOURS))
1578 return (-1);
1580 xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1581 tty_puts(tty, s);
1582 return (0);
1585 void
1586 tty_bell(struct tty *tty)
1588 tty_putcode(tty, TTYC_BEL);