[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.18

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