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

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