[BACK]Return to tty.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/tty.c, Revision 1.40

1.40    ! nicm        1: /* $OpenBSD: tty.c,v 1.39 2009/10/12 09:09:35 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      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.
                      9:  *
                     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.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20: #include <sys/ioctl.h>
                     21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
                     24: #include <string.h>
                     25: #include <termios.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
                     30: void   tty_fill_acs(struct tty *);
                     31:
                     32: int    tty_try_256(struct tty *, u_char, const char *);
                     33: int    tty_try_88(struct tty *, u_char, const char *);
                     34:
                     35: void   tty_attributes_fg(struct tty *, const struct grid_cell *);
                     36: void   tty_attributes_bg(struct tty *, const struct grid_cell *);
                     37:
1.24      nicm       38: void   tty_redraw_region(struct tty *, const struct tty_ctx *);
1.13      nicm       39: void   tty_emulate_repeat(
                     40:            struct tty *, enum tty_code_code, enum tty_code_code, u_int);
1.14      nicm       41: void   tty_cell(struct tty *,
                     42:            const struct grid_cell *, const struct grid_utf8 *);
1.1       nicm       43:
                     44: void
1.30      nicm       45: tty_init(struct tty *tty, int fd, char *term)
1.1       nicm       46: {
1.30      nicm       47:        char    *path;
                     48:
                     49:        memset(tty, 0, sizeof *tty);
1.23      nicm       50:        tty->log_fd = -1;
1.22      nicm       51:
1.9       nicm       52:        if (term == NULL || *term == '\0')
1.1       nicm       53:                tty->termname = xstrdup("unknown");
                     54:        else
                     55:                tty->termname = xstrdup(term);
1.30      nicm       56:
                     57:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                     58:                fatal("fcntl failed");
                     59:        tty->fd = fd;
                     60:
                     61:        if ((path = ttyname(fd)) == NULL)
                     62:                fatalx("ttyname failed");
                     63:        tty->path = xstrdup(path);
                     64:
1.1       nicm       65:        tty->flags = 0;
                     66:        tty->term_flags = 0;
1.31      nicm       67: }
                     68:
                     69: void
                     70: tty_resize(struct tty *tty)
                     71: {
                     72:        struct winsize  ws;
                     73:
                     74:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
                     75:                tty->sx = ws.ws_col;
                     76:                tty->sy = ws.ws_row;
                     77:        }
                     78:        if (tty->sx == 0)
                     79:                tty->sx = 80;
                     80:        if (tty->sy == 0)
                     81:                tty->sy = 24;
                     82:
                     83:        tty->cx = UINT_MAX;
                     84:        tty->cy = UINT_MAX;
                     85:
                     86:        tty->rupper = UINT_MAX;
                     87:        tty->rlower = UINT_MAX;
1.1       nicm       88: }
                     89:
                     90: int
1.17      nicm       91: tty_open(struct tty *tty, const char *overrides, char **cause)
1.1       nicm       92: {
1.30      nicm       93:        int     fd;
1.1       nicm       94:
1.30      nicm       95:        if (debug_level > 3) {
                     96:                fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
                     97:                if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                     98:                        fatal("fcntl failed");
                     99:                tty->log_fd = fd;
1.1       nicm      100:        }
                    101:
1.17      nicm      102:        tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
1.21      nicm      103:        if (tty->term == NULL) {
                    104:                tty_close(tty);
                    105:                return (-1);
                    106:        }
                    107:        tty->flags |= TTY_OPENED;
1.1       nicm      108:
                    109:        tty->in = buffer_create(BUFSIZ);
                    110:        tty->out = buffer_create(BUFSIZ);
                    111:
1.29      nicm      112:        tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
1.1       nicm      113:
                    114:        tty_start_tty(tty);
                    115:
                    116:        tty_keys_init(tty);
                    117:
                    118:        tty_fill_acs(tty);
                    119:
                    120:        return (0);
                    121: }
                    122:
                    123: void
                    124: tty_start_tty(struct tty *tty)
                    125: {
                    126:        struct termios   tio;
1.34      nicm      127:        int              what, mode;
1.33      nicm      128:
                    129:        if (tty->fd == -1)
                    130:                return;
1.1       nicm      131:
1.34      nicm      132:        if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
                    133:                fatal("fcntl failed");
                    134:        if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    135:                fatal("fcntl failed");
                    136:
1.1       nicm      137:        if (tcgetattr(tty->fd, &tty->tio) != 0)
                    138:                fatal("tcgetattr failed");
                    139:        memcpy(&tio, &tty->tio, sizeof tio);
                    140:        tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
                    141:        tio.c_iflag |= IGNBRK;
                    142:        tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
                    143:        tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
                    144:            ECHOPRT|ECHOKE|ECHOCTL|ISIG);
                    145:        tio.c_cc[VMIN] = 1;
                    146:         tio.c_cc[VTIME] = 0;
                    147:        if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
                    148:                fatal("tcsetattr failed");
                    149:
                    150:        what = 0;
                    151:        if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
                    152:                fatal("ioctl(TIOCFLUSH)");
                    153:
1.10      nicm      154:        tty_putcode(tty, TTYC_SMCUP);
1.1       nicm      155:
1.25      nicm      156:        tty_putcode(tty, TTYC_SGR0);
                    157:        memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
                    158:
1.1       nicm      159:        tty_putcode(tty, TTYC_SMKX);
                    160:        tty_putcode(tty, TTYC_ENACS);
                    161:        tty_putcode(tty, TTYC_CLEAR);
                    162:
                    163:        tty_putcode(tty, TTYC_CNORM);
                    164:        if (tty_term_has(tty->term, TTYC_KMOUS))
                    165:                tty_puts(tty, "\033[?1000l");
                    166:
                    167:        tty->cx = UINT_MAX;
                    168:        tty->cy = UINT_MAX;
                    169:
                    170:        tty->rlower = UINT_MAX;
                    171:        tty->rupper = UINT_MAX;
                    172:
                    173:        tty->mode = MODE_CURSOR;
1.20      nicm      174:
                    175:        tty->flags |= TTY_STARTED;
1.1       nicm      176: }
                    177:
                    178: void
                    179: tty_stop_tty(struct tty *tty)
                    180: {
                    181:        struct winsize  ws;
1.34      nicm      182:        int             mode;
1.1       nicm      183:
1.20      nicm      184:        if (!(tty->flags & TTY_STARTED))
                    185:                return;
                    186:        tty->flags &= ~TTY_STARTED;
                    187:
1.1       nicm      188:        /*
                    189:         * Be flexible about error handling and try not kill the server just
                    190:         * because the fd is invalid. Things like ssh -t can easily leave us
                    191:         * with a dead tty.
                    192:         */
1.34      nicm      193:        if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
                    194:                return;
                    195:        if (fcntl(tty->fd, F_SETFL, mode & ~O_NONBLOCK) == -1)
                    196:                return;
1.1       nicm      197:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
                    198:                return;
                    199:        if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
                    200:                return;
                    201:
                    202:        tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
                    203:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
                    204:        tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
                    205:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
                    206:        tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
                    207:
                    208:        tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
                    209:        if (tty_term_has(tty->term, TTYC_KMOUS))
                    210:                tty_raw(tty, "\033[?1000l");
1.10      nicm      211:
                    212:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.1       nicm      213: }
                    214:
                    215: void
                    216: tty_fill_acs(struct tty *tty)
                    217: {
                    218:        const char *ptr;
                    219:
                    220:        memset(tty->acs, 0, sizeof tty->acs);
                    221:        if (!tty_term_has(tty->term, TTYC_ACSC))
                    222:                return;
                    223:
                    224:        ptr = tty_term_string(tty->term, TTYC_ACSC);
                    225:        if (strlen(ptr) % 2 != 0)
                    226:                return;
                    227:        for (; *ptr != '\0'; ptr += 2)
                    228:                tty->acs[(u_char) ptr[0]] = ptr[1];
                    229: }
                    230:
                    231: u_char
                    232: tty_get_acs(struct tty *tty, u_char ch)
                    233: {
                    234:        if (tty->acs[ch] != '\0')
                    235:                return (tty->acs[ch]);
                    236:        return (ch);
                    237: }
                    238:
                    239: void
1.20      nicm      240: tty_close(struct tty *tty)
1.1       nicm      241: {
                    242:        if (tty->log_fd != -1) {
                    243:                close(tty->log_fd);
                    244:                tty->log_fd = -1;
                    245:        }
                    246:
1.20      nicm      247:        tty_stop_tty(tty);
1.1       nicm      248:
1.21      nicm      249:        if (tty->flags & TTY_OPENED) {
                    250:                tty_term_free(tty->term);
                    251:                tty_keys_free(tty);
                    252:
                    253:                buffer_destroy(tty->in);
                    254:                buffer_destroy(tty->out);
1.1       nicm      255:
1.21      nicm      256:                tty->flags &= ~TTY_OPENED;
                    257:        }
1.1       nicm      258:
1.21      nicm      259:        if (tty->fd != -1) {
                    260:                close(tty->fd);
                    261:                tty->fd = -1;
                    262:        }
1.1       nicm      263: }
                    264:
                    265: void
1.20      nicm      266: tty_free(struct tty *tty)
1.1       nicm      267: {
1.20      nicm      268:        tty_close(tty);
1.1       nicm      269:
                    270:        if (tty->path != NULL)
                    271:                xfree(tty->path);
                    272:        if (tty->termname != NULL)
                    273:                xfree(tty->termname);
                    274: }
                    275:
                    276: void
                    277: tty_raw(struct tty *tty, const char *s)
                    278: {
                    279:        write(tty->fd, s, strlen(s));
                    280: }
                    281:
                    282: void
                    283: tty_putcode(struct tty *tty, enum tty_code_code code)
                    284: {
                    285:        tty_puts(tty, tty_term_string(tty->term, code));
                    286: }
                    287:
                    288: void
                    289: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
                    290: {
                    291:        if (a < 0)
                    292:                return;
                    293:        tty_puts(tty, tty_term_string1(tty->term, code, a));
                    294: }
                    295:
                    296: void
                    297: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
                    298: {
                    299:        if (a < 0 || b < 0)
                    300:                return;
                    301:        tty_puts(tty, tty_term_string2(tty->term, code, a, b));
                    302: }
                    303:
                    304: void
                    305: tty_puts(struct tty *tty, const char *s)
                    306: {
                    307:        if (*s == '\0')
                    308:                return;
                    309:        buffer_write(tty->out, s, strlen(s));
                    310:
                    311:        if (tty->log_fd != -1)
                    312:                write(tty->log_fd, s, strlen(s));
                    313: }
                    314:
                    315: void
                    316: tty_putc(struct tty *tty, u_char ch)
                    317: {
                    318:        u_int   sx;
                    319:
                    320:        if (tty->cell.attr & GRID_ATTR_CHARSET)
                    321:                ch = tty_get_acs(tty, ch);
                    322:        buffer_write8(tty->out, ch);
                    323:
                    324:        if (ch >= 0x20 && ch != 0x7f) {
                    325:                sx = tty->sx;
                    326:                if (tty->term->flags & TERM_EARLYWRAP)
                    327:                        sx--;
                    328:
                    329:                if (tty->cx == sx) {
                    330:                        tty->cx = 0;
                    331:                        tty->cy++;
                    332:                } else
                    333:                        tty->cx++;
                    334:        }
                    335:
                    336:        if (tty->log_fd != -1)
                    337:                write(tty->log_fd, &ch, 1);
                    338: }
                    339:
                    340: void
1.5       nicm      341: tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
                    342: {
                    343:        u_int   i, width;
                    344:
                    345:        for (i = 0; i < UTF8_SIZE; i++) {
                    346:                if (gu->data[i] == 0xff)
                    347:                        break;
                    348:                buffer_write8(tty->out, gu->data[i]);
                    349:                if (tty->log_fd != -1)
                    350:                        write(tty->log_fd, &gu->data[i], 1);
                    351:        }
                    352:
                    353:        width = utf8_width(gu->data);
                    354:        tty->cx += width;
                    355: }
                    356:
                    357: void
1.1       nicm      358: tty_set_title(struct tty *tty, const char *title)
                    359: {
                    360:        if (strstr(tty->termname, "xterm") == NULL &&
                    361:            strstr(tty->termname, "rxvt") == NULL &&
                    362:            strcmp(tty->termname, "screen") != 0)
                    363:                return;
                    364:
                    365:        tty_puts(tty, "\033]0;");
                    366:        tty_puts(tty, title);
                    367:        tty_putc(tty, '\007');
                    368: }
                    369:
                    370: void
                    371: tty_update_mode(struct tty *tty, int mode)
                    372: {
                    373:        int     changed;
                    374:
                    375:        if (tty->flags & TTY_NOCURSOR)
                    376:                mode &= ~MODE_CURSOR;
                    377:
                    378:        changed = mode ^ tty->mode;
                    379:        if (changed & MODE_CURSOR) {
                    380:                if (mode & MODE_CURSOR)
                    381:                        tty_putcode(tty, TTYC_CNORM);
                    382:                else
                    383:                        tty_putcode(tty, TTYC_CIVIS);
                    384:        }
                    385:        if (changed & MODE_MOUSE) {
                    386:                if (mode & MODE_MOUSE)
                    387:                        tty_puts(tty, "\033[?1000h");
                    388:                else
                    389:                        tty_puts(tty, "\033[?1000l");
                    390:        }
                    391:        tty->mode = mode;
                    392: }
                    393:
                    394: void
                    395: tty_emulate_repeat(
                    396:     struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
                    397: {
                    398:        if (tty_term_has(tty->term, code))
                    399:                tty_putcode1(tty, code, n);
                    400:        else {
                    401:                while (n-- > 0)
                    402:                        tty_putcode(tty, code1);
                    403:        }
                    404: }
                    405:
                    406: /*
                    407:  * Redraw scroll region using data from screen (already updated). Used when
                    408:  * CSR not supported, or window is a pane that doesn't take up the full
                    409:  * width of the terminal.
                    410:  */
                    411: void
1.24      nicm      412: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      413: {
1.14      nicm      414:        struct window_pane      *wp = ctx->wp;
                    415:        struct screen           *s = wp->screen;
                    416:        u_int                    i;
1.1       nicm      417:
                    418:        /*
                    419:         * If region is >= 50% of the screen, just schedule a window redraw. In
                    420:         * most cases, this is likely to be followed by some more scrolling -
                    421:         * without this, the entire pane ends up being redrawn many times which
                    422:         * can be much more data.
                    423:         */
1.14      nicm      424:        if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
1.1       nicm      425:                wp->flags |= PANE_REDRAW;
                    426:                return;
                    427:        }
                    428:
1.14      nicm      429:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    430:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.1       nicm      431:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    432:        } else {
1.14      nicm      433:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.1       nicm      434:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    435:        }
                    436: }
                    437:
                    438: void
                    439: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
                    440: {
                    441:        const struct grid_cell  *gc;
1.16      nicm      442:        struct grid_cell         tmpgc;
1.1       nicm      443:        const struct grid_utf8  *gu;
                    444:        u_int                    i, sx;
                    445:
1.35      nicm      446:        tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
                    447:
1.1       nicm      448:        sx = screen_size_x(s);
1.19      nicm      449:        if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
                    450:                sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1       nicm      451:        if (sx > tty->sx)
                    452:                sx = tty->sx;
                    453:
                    454:        tty_cursor(tty, 0, py, ox, oy);
                    455:        for (i = 0; i < sx; i++) {
                    456:                gc = grid_view_peek_cell(s->grid, i, py);
                    457:
                    458:                gu = NULL;
                    459:                if (gc->flags & GRID_FLAG_UTF8)
                    460:                        gu = grid_view_peek_utf8(s->grid, i, py);
                    461:
                    462:                if (screen_check_selection(s, i, py)) {
1.16      nicm      463:                        memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
                    464:                        tmpgc.data = gc->data;
1.38      nicm      465:                        tmpgc.flags = gc->flags &
                    466:                            ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
                    467:                        tmpgc.flags |= s->sel.cell.flags &
                    468:                            (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16      nicm      469:                        tty_cell(tty, &tmpgc, gu);
1.1       nicm      470:                } else
                    471:                        tty_cell(tty, gc, gu);
                    472:        }
                    473:
1.35      nicm      474:        if (sx >= tty->sx) {
                    475:                tty_update_mode(tty, tty->mode);
1.1       nicm      476:                return;
1.35      nicm      477:        }
1.1       nicm      478:        tty_reset(tty);
                    479:
                    480:        tty_cursor(tty, sx, py, ox, oy);
                    481:        if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
                    482:                tty_putcode(tty, TTYC_EL);
                    483:        else {
                    484:                for (i = sx; i < screen_size_x(s); i++)
                    485:                        tty_putc(tty, ' ');
1.15      nicm      486:        }
1.35      nicm      487:        tty_update_mode(tty, tty->mode);
1.15      nicm      488: }
                    489:
                    490: void
1.24      nicm      491: tty_write(void (*cmdfn)(
                    492:     struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
1.15      nicm      493: {
                    494:        struct window_pane      *wp = ctx->wp;
                    495:        struct client           *c;
                    496:        u_int                    i;
                    497:
                    498:        if (wp == NULL)
                    499:                return;
                    500:
                    501:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    502:                return;
                    503:        if (wp->window->flags & WINDOW_HIDDEN || !window_pane_visible(wp))
                    504:                return;
                    505:
                    506:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    507:                c = ARRAY_ITEM(&clients, i);
                    508:                if (c == NULL || c->session == NULL)
                    509:                        continue;
                    510:                if (c->flags & CLIENT_SUSPENDED)
                    511:                        continue;
                    512:
                    513:                if (c->session->curw->window == wp->window) {
                    514:                        if (c->tty.flags & TTY_FREEZE || c->tty.term == NULL)
                    515:                                continue;
                    516:                        cmdfn(&c->tty, ctx);
                    517:                }
1.1       nicm      518:        }
                    519: }
                    520:
                    521: void
1.24      nicm      522: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      523: {
1.12      nicm      524:        struct window_pane      *wp = ctx->wp;
                    525:        struct screen           *s = wp->screen;
1.24      nicm      526:        u_int                    i;
1.1       nicm      527:
                    528:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
1.14      nicm      529:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      530:                return;
                    531:        }
                    532:
                    533:        tty_reset(tty);
                    534:
1.14      nicm      535:        tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      536:        if (tty_term_has(tty->term, TTYC_ICH) ||
                    537:            tty_term_has(tty->term, TTYC_ICH1))
1.12      nicm      538:                tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1       nicm      539:        else {
                    540:                tty_putcode(tty, TTYC_SMIR);
1.24      nicm      541:                for (i = 0; i < ctx->num; i++)
1.1       nicm      542:                        tty_putc(tty, ' ');
                    543:                tty_putcode(tty, TTYC_RMIR);
                    544:        }
                    545: }
                    546:
                    547: void
1.24      nicm      548: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      549: {
1.12      nicm      550:        struct window_pane      *wp = ctx->wp;
                    551:        struct screen           *s = wp->screen;
1.1       nicm      552:
1.26      nicm      553:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    554:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    555:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.14      nicm      556:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      557:                return;
                    558:        }
                    559:
                    560:        tty_reset(tty);
                    561:
1.14      nicm      562:        tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.26      nicm      563:        if (tty_term_has(tty->term, TTYC_DCH) ||
                    564:            tty_term_has(tty->term, TTYC_DCH1))
                    565:                tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1       nicm      566: }
                    567:
                    568: void
1.24      nicm      569: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      570: {
1.12      nicm      571:        struct window_pane      *wp = ctx->wp;
                    572:        struct screen           *s = wp->screen;
1.1       nicm      573:
                    574:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    575:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      576:                tty_redraw_region(tty, ctx);
1.1       nicm      577:                return;
                    578:        }
                    579:
                    580:        tty_reset(tty);
                    581:
1.39      nicm      582:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.1       nicm      583:
1.14      nicm      584:        tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.12      nicm      585:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      586: }
                    587:
                    588: void
1.24      nicm      589: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      590: {
1.12      nicm      591:        struct window_pane      *wp = ctx->wp;
                    592:        struct screen           *s = wp->screen;
1.1       nicm      593:
                    594:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    595:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      596:                tty_redraw_region(tty, ctx);
1.1       nicm      597:                return;
                    598:        }
                    599:
                    600:        tty_reset(tty);
                    601:
1.39      nicm      602:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.1       nicm      603:
1.14      nicm      604:        tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.12      nicm      605:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      606: }
                    607:
                    608: void
1.24      nicm      609: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      610: {
1.12      nicm      611:        struct window_pane      *wp = ctx->wp;
                    612:        struct screen           *s = wp->screen;
                    613:        u_int                    i;
1.1       nicm      614:
                    615:        tty_reset(tty);
                    616:
1.14      nicm      617:        tty_cursor(tty, 0, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      618:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    619:            tty_term_has(tty->term, TTYC_EL)) {
                    620:                tty_putcode(tty, TTYC_EL);
                    621:        } else {
                    622:                for (i = 0; i < screen_size_x(s); i++)
                    623:                        tty_putc(tty, ' ');
                    624:        }
                    625: }
                    626:
                    627: void
1.24      nicm      628: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      629: {
1.12      nicm      630:        struct window_pane      *wp = ctx->wp;
                    631:        struct screen           *s = wp->screen;
                    632:        u_int                    i;
1.1       nicm      633:
                    634:        tty_reset(tty);
                    635:
1.14      nicm      636:        tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      637:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    638:            tty_term_has(tty->term, TTYC_EL))
                    639:                tty_putcode(tty, TTYC_EL);
                    640:        else {
1.14      nicm      641:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      642:                        tty_putc(tty, ' ');
                    643:        }
                    644: }
                    645:
                    646: void
1.24      nicm      647: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      648: {
1.12      nicm      649:        struct window_pane      *wp = ctx->wp;
                    650:        u_int                    i;
1.1       nicm      651:
                    652:        tty_reset(tty);
                    653:
                    654:        if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.14      nicm      655:                tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      656:                tty_putcode(tty, TTYC_EL1);
                    657:        } else {
1.14      nicm      658:                tty_cursor(tty, 0, ctx->ocy, wp->xoff, wp->yoff);
                    659:                for (i = 0; i < ctx->ocx + 1; i++)
1.1       nicm      660:                        tty_putc(tty, ' ');
                    661:        }
                    662: }
                    663:
                    664: void
1.24      nicm      665: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      666: {
1.12      nicm      667:        struct window_pane      *wp = ctx->wp;
                    668:        struct screen           *s = wp->screen;
1.1       nicm      669:
                    670:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    671:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      672:                tty_redraw_region(tty, ctx);
1.1       nicm      673:                return;
                    674:        }
                    675:
1.14      nicm      676:        if (ctx->ocy == ctx->orupper) {
1.38      nicm      677:                tty_reset(tty);
1.39      nicm      678:
                    679:                tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.14      nicm      680:                tty_cursor(tty, ctx->ocx, ctx->orupper, wp->xoff, wp->yoff);
1.39      nicm      681:
1.1       nicm      682:                tty_putcode(tty, TTYC_RI);
                    683:        }
                    684: }
                    685:
                    686: void
1.24      nicm      687: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      688: {
1.12      nicm      689:        struct window_pane      *wp = ctx->wp;
                    690:        struct screen           *s = wp->screen;
1.1       nicm      691:
                    692:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    693:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      694:                tty_redraw_region(tty, ctx);
1.1       nicm      695:                return;
                    696:        }
                    697:
1.14      nicm      698:        if (ctx->ocy == ctx->orlower) {
1.37      nicm      699:                tty_reset(tty);
1.39      nicm      700:
                    701:                tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.14      nicm      702:                tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.39      nicm      703:
1.1       nicm      704:                tty_putc(tty, '\n');
                    705:        }
                    706: }
                    707:
                    708: void
1.24      nicm      709: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      710: {
1.12      nicm      711:        struct window_pane      *wp = ctx->wp;
                    712:        struct screen           *s = wp->screen;
                    713:        u_int                    i, j;
1.1       nicm      714:
                    715:        tty_reset(tty);
                    716:
1.39      nicm      717:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.14      nicm      718:        tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.39      nicm      719:
1.1       nicm      720:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    721:            tty_term_has(tty->term, TTYC_EL)) {
                    722:                tty_putcode(tty, TTYC_EL);
1.14      nicm      723:                if (ctx->ocy != screen_size_y(s) - 1) {
                    724:                        tty_cursor(tty, 0, ctx->ocy + 1, wp->xoff, wp->yoff);
                    725:                        for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1       nicm      726:                                tty_putcode(tty, TTYC_EL);
                    727:                                if (i == screen_size_y(s) - 1)
                    728:                                        continue;
                    729:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    730:                                tty->cy++;
                    731:                        }
                    732:                }
                    733:        } else {
1.14      nicm      734:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      735:                        tty_putc(tty, ' ');
1.14      nicm      736:                for (j = ctx->ocy; j < screen_size_y(s); j++) {
1.1       nicm      737:                        tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
                    738:                        for (i = 0; i < screen_size_x(s); i++)
                    739:                                tty_putc(tty, ' ');
                    740:                }
                    741:        }
                    742: }
                    743:
                    744: void
1.24      nicm      745: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      746: {
1.12      nicm      747:        struct window_pane      *wp = ctx->wp;
                    748:        struct screen           *s = wp->screen;
                    749:        u_int                    i, j;
1.1       nicm      750:
                    751:        tty_reset(tty);
                    752:
1.39      nicm      753:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.1       nicm      754:        tty_cursor(tty, 0, 0, wp->xoff, wp->yoff);
1.39      nicm      755:
1.1       nicm      756:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    757:            tty_term_has(tty->term, TTYC_EL)) {
1.14      nicm      758:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm      759:                        tty_putcode(tty, TTYC_EL);
                    760:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    761:                        tty->cy++;
                    762:                }
                    763:        } else {
1.14      nicm      764:                for (j = 0; j < ctx->ocy; j++) {
1.1       nicm      765:                        tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
                    766:                        for (i = 0; i < screen_size_x(s); i++)
                    767:                                tty_putc(tty, ' ');
                    768:                }
                    769:        }
1.14      nicm      770:        for (i = 0; i <= ctx->ocx; i++)
1.1       nicm      771:                tty_putc(tty, ' ');
                    772: }
                    773:
                    774: void
1.24      nicm      775: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      776: {
1.12      nicm      777:        struct window_pane      *wp = ctx->wp;
                    778:        struct screen           *s = wp->screen;
                    779:        u_int                    i, j;
1.1       nicm      780:
                    781:        tty_reset(tty);
                    782:
1.39      nicm      783:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.1       nicm      784:        tty_cursor(tty, 0, 0, wp->xoff, wp->yoff);
1.39      nicm      785:
1.1       nicm      786:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    787:            tty_term_has(tty->term, TTYC_EL)) {
                    788:                for (i = 0; i < screen_size_y(s); i++) {
                    789:                        tty_putcode(tty, TTYC_EL);
                    790:                        if (i != screen_size_y(s) - 1) {
                    791:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    792:                                tty->cy++;
                    793:                        }
                    794:                }
                    795:        } else {
                    796:                for (j = 0; j < screen_size_y(s); j++) {
                    797:                        tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
                    798:                        for (i = 0; i < screen_size_x(s); i++)
                    799:                                tty_putc(tty, ' ');
                    800:                }
1.4       nicm      801:        }
                    802: }
                    803:
                    804: void
1.24      nicm      805: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm      806: {
1.12      nicm      807:        struct window_pane      *wp = ctx->wp;
                    808:        struct screen           *s = wp->screen;
                    809:        u_int                    i, j;
1.4       nicm      810:
                    811:        tty_reset(tty);
                    812:
1.39      nicm      813:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4       nicm      814:
                    815:        for (j = 0; j < screen_size_y(s); j++) {
                    816:                tty_cursor(tty, 0, j, wp->xoff, wp->yoff);
                    817:                for (i = 0; i < screen_size_x(s); i++)
                    818:                        tty_putc(tty, 'E');
1.1       nicm      819:        }
                    820: }
                    821:
                    822: void
1.24      nicm      823: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      824: {
1.12      nicm      825:        struct window_pane      *wp = ctx->wp;
1.1       nicm      826:
1.14      nicm      827:        tty_cursor(tty, ctx->ocx, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      828:
1.12      nicm      829:        tty_cell(tty, ctx->cell, ctx->utf8);
1.1       nicm      830: }
                    831:
                    832: void
1.24      nicm      833: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      834: {
1.12      nicm      835:        u_char  *ptr = ctx->ptr;
1.11      nicm      836:        size_t   i;
1.1       nicm      837:
1.11      nicm      838:        for (i = 0; i < UTF8_SIZE; i++) {
1.12      nicm      839:                if (ptr[i] == 0xff)
1.11      nicm      840:                        break;
1.12      nicm      841:                tty_putc(tty, ptr[i]);
1.11      nicm      842:        }
1.1       nicm      843: }
                    844:
                    845: void
                    846: tty_cell(
                    847:     struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
                    848: {
                    849:        u_int   i;
                    850:
                    851:        /* Skip last character if terminal is stupid. */
                    852:        if (tty->term->flags & TERM_EARLYWRAP &&
                    853:            tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
                    854:                return;
                    855:
                    856:        /* If this is a padding character, do nothing. */
                    857:        if (gc->flags & GRID_FLAG_PADDING)
                    858:                return;
                    859:
                    860:        /* Set the attributes. */
                    861:        tty_attributes(tty, gc);
                    862:
                    863:        /* If not UTF-8, write directly. */
                    864:        if (!(gc->flags & GRID_FLAG_UTF8)) {
                    865:                if (gc->data < 0x20 || gc->data == 0x7f)
                    866:                        return;
                    867:                tty_putc(tty, gc->data);
                    868:                return;
                    869:        }
                    870:
                    871:        /* If the terminal doesn't support UTF-8, write underscores. */
                    872:        if (!(tty->flags & TTY_UTF8)) {
                    873:                for (i = 0; i < gu->width; i++)
                    874:                        tty_putc(tty, '_');
                    875:                return;
                    876:        }
                    877:
                    878:        /* Otherwise, write UTF-8. */
1.5       nicm      879:        tty_pututf8(tty, gu);
1.1       nicm      880: }
                    881:
                    882: void
                    883: tty_reset(struct tty *tty)
                    884: {
                    885:        struct grid_cell        *gc = &tty->cell;
                    886:
                    887:        if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
                    888:                return;
                    889:
                    890:        if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
                    891:                tty_putcode(tty, TTYC_RMACS);
                    892:        tty_putcode(tty, TTYC_SGR0);
                    893:        memcpy(gc, &grid_default_cell, sizeof *gc);
                    894: }
                    895:
1.40    ! nicm      896: /* Set region inside pane. */
1.1       nicm      897: void
1.39      nicm      898: tty_region_pane(
                    899:     struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1       nicm      900: {
1.39      nicm      901:        struct window_pane      *wp = ctx->wp;
                    902:
1.40    ! nicm      903:        tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
1.39      nicm      904: }
                    905:
1.40    ! nicm      906: /* Set region at absolute position. */
1.39      nicm      907: void
1.40    ! nicm      908: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm      909: {
                    910:        if (tty->rlower == rlower && tty->rupper == rupper)
                    911:                return;
1.1       nicm      912:        if (!tty_term_has(tty->term, TTYC_CSR))
                    913:                return;
1.39      nicm      914:
                    915:        tty->rupper = rupper;
                    916:        tty->rlower = rlower;
                    917:
                    918:        tty->cx = 0;
                    919:        tty->cy = 0;
                    920:
                    921:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.1       nicm      922: }
                    923:
                    924: void
                    925: tty_cursor(struct tty *tty, u_int cx, u_int cy, u_int ox, u_int oy)
                    926: {
                    927:        if (ox + cx == 0 && tty->cx != 0 && tty->cy == oy + cy) {
                    928:                tty->cx = 0;
                    929:                tty_putc(tty, '\r');
                    930:        } else if (tty->cx != ox + cx || tty->cy != oy + cy) {
                    931:                tty->cx = ox + cx;
                    932:                tty->cy = oy + cy;
                    933:                tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
                    934:        }
                    935: }
                    936:
                    937: void
                    938: tty_attributes(struct tty *tty, const struct grid_cell *gc)
                    939: {
                    940:        struct grid_cell        *tc = &tty->cell;
                    941:        u_char                   changed;
1.18      nicm      942:        u_int                    fg, bg, attr;
                    943:
                    944:        /*
                    945:         * If no setab, try to use the reverse attribute as a best-effort for a
                    946:         * non-default background. This is a bit of a hack but it doesn't do
                    947:         * any serious harm and makes a couple of applications happier.
                    948:         */
                    949:        fg = gc->fg; bg = gc->bg; attr = gc->attr;
                    950:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
                    951:                if (attr & GRID_ATTR_REVERSE) {
                    952:                        if (fg != 7 && fg != 8)
                    953:                                attr &= ~GRID_ATTR_REVERSE;
                    954:                } else {
                    955:                        if (bg != 0 && bg != 8)
                    956:                                attr |= GRID_ATTR_REVERSE;
                    957:                }
                    958:        }
1.1       nicm      959:
                    960:        /* If any bits are being cleared, reset everything. */
1.18      nicm      961:        if (tc->attr & ~attr)
1.1       nicm      962:                tty_reset(tty);
                    963:
                    964:        /* Filter out attribute bits already set. */
1.18      nicm      965:        changed = attr & ~tc->attr;
                    966:        tc->attr = attr;
1.1       nicm      967:
                    968:        /* Set the attributes. */
                    969:        if (changed & GRID_ATTR_BRIGHT)
                    970:                tty_putcode(tty, TTYC_BOLD);
                    971:        if (changed & GRID_ATTR_DIM)
                    972:                tty_putcode(tty, TTYC_DIM);
                    973:        if (changed & GRID_ATTR_ITALICS)
                    974:                tty_putcode(tty, TTYC_SMSO);
                    975:        if (changed & GRID_ATTR_UNDERSCORE)
                    976:                tty_putcode(tty, TTYC_SMUL);
                    977:        if (changed & GRID_ATTR_BLINK)
                    978:                tty_putcode(tty, TTYC_BLINK);
                    979:        if (changed & GRID_ATTR_REVERSE) {
                    980:                if (tty_term_has(tty->term, TTYC_REV))
                    981:                        tty_putcode(tty, TTYC_REV);
                    982:                else if (tty_term_has(tty->term, TTYC_SMSO))
                    983:                        tty_putcode(tty, TTYC_SMSO);
                    984:        }
                    985:        if (changed & GRID_ATTR_HIDDEN)
                    986:                tty_putcode(tty, TTYC_INVIS);
                    987:        if (changed & GRID_ATTR_CHARSET)
                    988:                tty_putcode(tty, TTYC_SMACS);
                    989:
                    990:        /* Set foreground colour. */
                    991:        if (fg != tc->fg ||
                    992:            (gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
                    993:                tty_attributes_fg(tty, gc);
                    994:                tc->fg = fg;
1.8       nicm      995:                tc->flags &= ~GRID_FLAG_FG256;
                    996:                tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1       nicm      997:        }
                    998:
                    999:        /* Set background colour. */
                   1000:        if (bg != tc->bg ||
                   1001:            (gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
                   1002:                tty_attributes_bg(tty, gc);
                   1003:                tc->bg = bg;
1.8       nicm     1004:                tc->flags &= ~GRID_FLAG_BG256;
                   1005:                tc->flags |= gc->flags & GRID_FLAG_BG256;
1.1       nicm     1006:        }
                   1007: }
                   1008:
                   1009: int
                   1010: tty_try_256(struct tty *tty, u_char colour, const char *type)
                   1011: {
                   1012:        char    s[32];
                   1013:
                   1014:        if (!(tty->term->flags & TERM_256COLOURS) &&
                   1015:            !(tty->term_flags & TERM_256COLOURS))
                   1016:                return (-1);
                   1017:
                   1018:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1019:        tty_puts(tty, s);
                   1020:        return (0);
                   1021: }
                   1022:
                   1023: int
                   1024: tty_try_88(struct tty *tty, u_char colour, const char *type)
                   1025: {
                   1026:        char    s[32];
                   1027:
                   1028:        if (!(tty->term->flags & TERM_88COLOURS) &&
                   1029:            !(tty->term_flags & TERM_88COLOURS))
                   1030:                return (-1);
                   1031:        colour = colour_256to88(colour);
                   1032:
                   1033:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1034:        tty_puts(tty, s);
                   1035:        return (0);
                   1036: }
                   1037:
                   1038: void
                   1039: tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
                   1040: {
                   1041:        u_char  fg;
                   1042:
                   1043:        fg = gc->fg;
                   1044:        if (gc->flags & GRID_FLAG_FG256) {
                   1045:                if (tty_try_256(tty, fg, "38") == 0)
                   1046:                        return;
                   1047:                if (tty_try_88(tty, fg, "38") == 0)
                   1048:                        return;
                   1049:                fg = colour_256to16(fg);
                   1050:                if (fg & 8) {
                   1051:                        fg &= 7;
                   1052:                        tty_putcode(tty, TTYC_BOLD);
                   1053:                        tty->cell.attr |= GRID_ATTR_BRIGHT;
                   1054:                } else if (tty->cell.attr & GRID_ATTR_BRIGHT)
                   1055:                        tty_reset(tty);
                   1056:        }
                   1057:
                   1058:        if (fg == 8 &&
                   1059:            !(tty->term->flags & TERM_HASDEFAULTS) &&
                   1060:            !(tty->term_flags & TERM_HASDEFAULTS))
                   1061:                fg = 7;
                   1062:        if (fg == 8)
                   1063:                tty_puts(tty, "\033[39m");
                   1064:        else
                   1065:                tty_putcode1(tty, TTYC_SETAF, fg);
                   1066: }
                   1067:
                   1068: void
                   1069: tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
                   1070: {
                   1071:        u_char  bg;
                   1072:
                   1073:        bg = gc->bg;
                   1074:        if (gc->flags & GRID_FLAG_BG256) {
                   1075:                if (tty_try_256(tty, bg, "48") == 0)
                   1076:                        return;
                   1077:                if (tty_try_88(tty, bg, "48") == 0)
                   1078:                        return;
                   1079:                bg = colour_256to16(bg);
                   1080:                if (bg & 8)
                   1081:                        bg &= 7;
                   1082:        }
                   1083:
                   1084:        if (bg == 8 &&
                   1085:            !(tty->term->flags & TERM_HASDEFAULTS) &&
                   1086:            !(tty->term_flags & TERM_HASDEFAULTS))
                   1087:                bg = 0;
                   1088:        if (bg == 8)
                   1089:                tty_puts(tty, "\033[49m");
                   1090:        else
                   1091:                tty_putcode1(tty, TTYC_SETAB, bg);
                   1092: }