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

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