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

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