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

1.96    ! nicm        1: /* $OpenBSD: tty.c,v 1.95 2011/01/03 23:35:22 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>
1.42      nicm       24: #include <stdlib.h>
1.1       nicm       25: #include <string.h>
                     26: #include <termios.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
                     30:
1.67      nicm       31: void   tty_read_callback(struct bufferevent *, void *);
1.66      nicm       32: void   tty_error_callback(struct bufferevent *, short, void *);
                     33:
1.1       nicm       34: int    tty_try_256(struct tty *, u_char, const char *);
                     35: int    tty_try_88(struct tty *, u_char, const char *);
                     36:
1.85      nicm       37: void   tty_colours(struct tty *, const struct grid_cell *);
                     38: void   tty_check_fg(struct tty *, struct grid_cell *);
                     39: void   tty_check_bg(struct tty *, struct grid_cell *);
                     40: void   tty_colours_fg(struct tty *, const struct grid_cell *);
1.73      nicm       41: void   tty_colours_bg(struct tty *, const struct grid_cell *);
1.1       nicm       42:
1.24      nicm       43: void   tty_redraw_region(struct tty *, const struct tty_ctx *);
1.13      nicm       44: void   tty_emulate_repeat(
                     45:            struct tty *, enum tty_code_code, enum tty_code_code, u_int);
1.14      nicm       46: void   tty_cell(struct tty *,
1.77      nicm       47:            const struct grid_cell *, const struct grid_utf8 *);
1.1       nicm       48:
1.90      nicm       49: #define tty_use_acs(tty) \
1.91      nicm       50:        (tty_term_has(tty->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
1.90      nicm       51:
1.1       nicm       52: void
1.30      nicm       53: tty_init(struct tty *tty, int fd, char *term)
1.1       nicm       54: {
1.30      nicm       55:        char    *path;
                     56:
                     57:        memset(tty, 0, sizeof *tty);
1.23      nicm       58:        tty->log_fd = -1;
1.22      nicm       59:
1.9       nicm       60:        if (term == NULL || *term == '\0')
1.1       nicm       61:                tty->termname = xstrdup("unknown");
                     62:        else
                     63:                tty->termname = xstrdup(term);
1.30      nicm       64:        tty->fd = fd;
                     65:
                     66:        if ((path = ttyname(fd)) == NULL)
                     67:                fatalx("ttyname failed");
                     68:        tty->path = xstrdup(path);
                     69:
1.1       nicm       70:        tty->flags = 0;
                     71:        tty->term_flags = 0;
1.31      nicm       72: }
                     73:
1.88      nicm       74: int
1.31      nicm       75: tty_resize(struct tty *tty)
                     76: {
                     77:        struct winsize  ws;
1.88      nicm       78:        u_int           sx, sy;
1.31      nicm       79:
                     80:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88      nicm       81:                sx = ws.ws_col;
                     82:                if (sx == 0)
                     83:                        sx = 80;
                     84:                sy = ws.ws_row;
                     85:                if (sy == 0)
                     86:                        sy = 24;
                     87:        } else {
                     88:                sx = 80;
                     89:                sy = 24;
1.31      nicm       90:        }
1.88      nicm       91:        if (sx == tty->sx && sy == tty->sy)
                     92:                return (0);
                     93:        tty->sx = sx;
                     94:        tty->sy = sy;
1.31      nicm       95:
                     96:        tty->cx = UINT_MAX;
                     97:        tty->cy = UINT_MAX;
                     98:
                     99:        tty->rupper = UINT_MAX;
                    100:        tty->rlower = UINT_MAX;
1.88      nicm      101:
                    102:        /*
                    103:         * If the terminal has been started, reset the actual scroll region and
                    104:         * cursor position, as this may not have happened.
                    105:         */
                    106:        if (tty->flags & TTY_STARTED) {
                    107:                tty_cursor(tty, 0, 0);
                    108:                tty_region(tty, 0, tty->sy - 1);
                    109:        }
                    110:
                    111:        return (1);
1.1       nicm      112: }
                    113:
                    114: int
1.17      nicm      115: tty_open(struct tty *tty, const char *overrides, char **cause)
1.1       nicm      116: {
1.88      nicm      117:        char    out[64];
1.30      nicm      118:        int     fd;
1.1       nicm      119:
1.30      nicm      120:        if (debug_level > 3) {
1.88      nicm      121:                xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
                    122:                fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1.30      nicm      123:                if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                    124:                        fatal("fcntl failed");
                    125:                tty->log_fd = fd;
1.1       nicm      126:        }
                    127:
1.17      nicm      128:        tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
1.21      nicm      129:        if (tty->term == NULL) {
                    130:                tty_close(tty);
                    131:                return (-1);
                    132:        }
                    133:        tty->flags |= TTY_OPENED;
1.1       nicm      134:
1.66      nicm      135:        tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
1.1       nicm      136:
1.66      nicm      137:        tty->event = bufferevent_new(
1.67      nicm      138:            tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
1.1       nicm      139:
                    140:        tty_start_tty(tty);
                    141:
                    142:        tty_keys_init(tty);
                    143:
                    144:        return (0);
                    145: }
                    146:
1.73      nicm      147: /* ARGSUSED */
1.1       nicm      148: void
1.67      nicm      149: tty_read_callback(unused struct bufferevent *bufev, void *data)
                    150: {
                    151:        struct tty      *tty = data;
                    152:
                    153:        while (tty_keys_next(tty))
                    154:                ;
                    155: }
                    156:
1.73      nicm      157: /* ARGSUSED */
1.67      nicm      158: void
1.66      nicm      159: tty_error_callback(
                    160:     unused struct bufferevent *bufev, unused short what, unused void *data)
                    161: {
                    162: }
                    163:
                    164: void
1.1       nicm      165: tty_start_tty(struct tty *tty)
                    166: {
                    167:        struct termios   tio;
1.33      nicm      168:
                    169:        if (tty->fd == -1)
                    170:                return;
1.1       nicm      171:
1.96    ! nicm      172:        setblocking(tty->fd, 0);
1.34      nicm      173:
1.66      nicm      174:        bufferevent_enable(tty->event, EV_READ|EV_WRITE);
                    175:
1.1       nicm      176:        if (tcgetattr(tty->fd, &tty->tio) != 0)
                    177:                fatal("tcgetattr failed");
                    178:        memcpy(&tio, &tty->tio, sizeof tio);
                    179:        tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
                    180:        tio.c_iflag |= IGNBRK;
                    181:        tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
                    182:        tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
                    183:            ECHOPRT|ECHOKE|ECHOCTL|ISIG);
                    184:        tio.c_cc[VMIN] = 1;
1.60      deraadt   185:        tio.c_cc[VTIME] = 0;
1.1       nicm      186:        if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
                    187:                fatal("tcsetattr failed");
1.82      nicm      188:        tcflush(tty->fd, TCIOFLUSH);
1.1       nicm      189:
1.10      nicm      190:        tty_putcode(tty, TTYC_SMCUP);
1.1       nicm      191:
1.25      nicm      192:        tty_putcode(tty, TTYC_SGR0);
                    193:        memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
                    194:
1.76      nicm      195:        tty_putcode(tty, TTYC_RMKX);
1.90      nicm      196:        if (tty_use_acs(tty))
                    197:                tty_putcode(tty, TTYC_ENACS);
1.1       nicm      198:        tty_putcode(tty, TTYC_CLEAR);
                    199:
                    200:        tty_putcode(tty, TTYC_CNORM);
                    201:        if (tty_term_has(tty->term, TTYC_KMOUS))
                    202:                tty_puts(tty, "\033[?1000l");
                    203:
                    204:        tty->cx = UINT_MAX;
                    205:        tty->cy = UINT_MAX;
                    206:
                    207:        tty->rlower = UINT_MAX;
                    208:        tty->rupper = UINT_MAX;
                    209:
                    210:        tty->mode = MODE_CURSOR;
1.20      nicm      211:
                    212:        tty->flags |= TTY_STARTED;
1.1       nicm      213: }
                    214:
                    215: void
                    216: tty_stop_tty(struct tty *tty)
                    217: {
                    218:        struct winsize  ws;
                    219:
1.20      nicm      220:        if (!(tty->flags & TTY_STARTED))
                    221:                return;
                    222:        tty->flags &= ~TTY_STARTED;
                    223:
1.66      nicm      224:        bufferevent_disable(tty->event, EV_READ|EV_WRITE);
                    225:
1.1       nicm      226:        /*
                    227:         * Be flexible about error handling and try not kill the server just
                    228:         * because the fd is invalid. Things like ssh -t can easily leave us
                    229:         * with a dead tty.
                    230:         */
                    231:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
                    232:                return;
                    233:        if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
                    234:                return;
                    235:
                    236:        tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90      nicm      237:        if (tty_use_acs(tty))
                    238:                tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1       nicm      239:        tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
                    240:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
                    241:        tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
                    242:
                    243:        tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
                    244:        if (tty_term_has(tty->term, TTYC_KMOUS))
                    245:                tty_raw(tty, "\033[?1000l");
1.10      nicm      246:
                    247:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.84      nicm      248:
1.96    ! nicm      249:        setblocking(tty->fd, 1);
1.1       nicm      250: }
                    251:
                    252: void
1.20      nicm      253: tty_close(struct tty *tty)
1.1       nicm      254: {
                    255:        if (tty->log_fd != -1) {
                    256:                close(tty->log_fd);
                    257:                tty->log_fd = -1;
                    258:        }
                    259:
1.67      nicm      260:        evtimer_del(&tty->key_timer);
1.20      nicm      261:        tty_stop_tty(tty);
1.1       nicm      262:
1.21      nicm      263:        if (tty->flags & TTY_OPENED) {
1.66      nicm      264:                bufferevent_free(tty->event);
                    265:
1.21      nicm      266:                tty_term_free(tty->term);
                    267:                tty_keys_free(tty);
                    268:
                    269:                tty->flags &= ~TTY_OPENED;
                    270:        }
1.1       nicm      271:
1.21      nicm      272:        if (tty->fd != -1) {
                    273:                close(tty->fd);
                    274:                tty->fd = -1;
                    275:        }
1.1       nicm      276: }
                    277:
                    278: void
1.20      nicm      279: tty_free(struct tty *tty)
1.1       nicm      280: {
1.20      nicm      281:        tty_close(tty);
1.1       nicm      282:
                    283:        if (tty->path != NULL)
                    284:                xfree(tty->path);
                    285:        if (tty->termname != NULL)
                    286:                xfree(tty->termname);
                    287: }
                    288:
                    289: void
                    290: tty_raw(struct tty *tty, const char *s)
                    291: {
                    292:        write(tty->fd, s, strlen(s));
                    293: }
                    294:
                    295: void
                    296: tty_putcode(struct tty *tty, enum tty_code_code code)
                    297: {
                    298:        tty_puts(tty, tty_term_string(tty->term, code));
                    299: }
                    300:
                    301: void
                    302: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
                    303: {
                    304:        if (a < 0)
                    305:                return;
                    306:        tty_puts(tty, tty_term_string1(tty->term, code, a));
                    307: }
                    308:
                    309: void
                    310: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
                    311: {
                    312:        if (a < 0 || b < 0)
                    313:                return;
                    314:        tty_puts(tty, tty_term_string2(tty->term, code, a, b));
                    315: }
                    316:
                    317: void
                    318: tty_puts(struct tty *tty, const char *s)
                    319: {
                    320:        if (*s == '\0')
                    321:                return;
1.66      nicm      322:        bufferevent_write(tty->event, s, strlen(s));
1.1       nicm      323:
                    324:        if (tty->log_fd != -1)
                    325:                write(tty->log_fd, s, strlen(s));
                    326: }
                    327:
                    328: void
                    329: tty_putc(struct tty *tty, u_char ch)
                    330: {
1.90      nicm      331:        const char      *acs;
                    332:        u_int            sx;
1.1       nicm      333:
1.90      nicm      334:        if (tty->cell.attr & GRID_ATTR_CHARSET) {
                    335:                acs = tty_acs_get(tty, ch);
                    336:                if (acs != NULL)
                    337:                        bufferevent_write(tty->event, acs, strlen(acs));
                    338:                else
                    339:                        bufferevent_write(tty->event, &ch, 1);
                    340:        } else
                    341:                bufferevent_write(tty->event, &ch, 1);
1.1       nicm      342:
                    343:        if (ch >= 0x20 && ch != 0x7f) {
                    344:                sx = tty->sx;
                    345:                if (tty->term->flags & TERM_EARLYWRAP)
                    346:                        sx--;
                    347:
1.46      nicm      348:                if (tty->cx >= sx) {
                    349:                        tty->cx = 1;
                    350:                        if (tty->cy != tty->rlower)
                    351:                                tty->cy++;
1.1       nicm      352:                } else
                    353:                        tty->cx++;
                    354:        }
                    355:
                    356:        if (tty->log_fd != -1)
                    357:                write(tty->log_fd, &ch, 1);
                    358: }
                    359:
                    360: void
1.5       nicm      361: tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
                    362: {
1.71      nicm      363:        size_t  size;
1.5       nicm      364:
1.71      nicm      365:        size = grid_utf8_size(gu);
                    366:        bufferevent_write(tty->event, gu->data, size);
                    367:        if (tty->log_fd != -1)
                    368:                write(tty->log_fd, gu->data, size);
1.54      nicm      369:        tty->cx += gu->width;
1.5       nicm      370: }
                    371:
                    372: void
1.1       nicm      373: tty_set_title(struct tty *tty, const char *title)
                    374: {
                    375:        if (strstr(tty->termname, "xterm") == NULL &&
                    376:            strstr(tty->termname, "rxvt") == NULL &&
                    377:            strcmp(tty->termname, "screen") != 0)
                    378:                return;
                    379:
                    380:        tty_puts(tty, "\033]0;");
                    381:        tty_puts(tty, title);
                    382:        tty_putc(tty, '\007');
                    383: }
                    384:
                    385: void
                    386: tty_update_mode(struct tty *tty, int mode)
                    387: {
                    388:        int     changed;
                    389:
                    390:        if (tty->flags & TTY_NOCURSOR)
                    391:                mode &= ~MODE_CURSOR;
                    392:
                    393:        changed = mode ^ tty->mode;
                    394:        if (changed & MODE_CURSOR) {
                    395:                if (mode & MODE_CURSOR)
                    396:                        tty_putcode(tty, TTYC_CNORM);
                    397:                else
                    398:                        tty_putcode(tty, TTYC_CIVIS);
                    399:        }
1.94      nicm      400:        if (changed & ALL_MOUSE_MODES) {
                    401:                if (mode & ALL_MOUSE_MODES) {
1.95      nicm      402:                        if (mode & MODE_MOUSE_UTF8)
                    403:                                tty_puts(tty, "\033[?1005h");
1.94      nicm      404:                        if (mode & MODE_MOUSE_STANDARD)
                    405:                                tty_puts(tty, "\033[?1000h");
                    406:                        else if (mode & MODE_MOUSE_HIGHLIGHT)
                    407:                                tty_puts(tty, "\033[?1001h");
                    408:                        else if (mode & MODE_MOUSE_BUTTON)
                    409:                                tty_puts(tty, "\033[?1002h");
                    410:                        else if (mode & MODE_MOUSE_ANY)
1.86      nicm      411:                                tty_puts(tty, "\033[?1003h");
                    412:                } else {
1.94      nicm      413:                        if (tty->mode & MODE_MOUSE_STANDARD)
                    414:                                tty_puts(tty, "\033[?1000l");
                    415:                        else if (tty->mode & MODE_MOUSE_HIGHLIGHT)
                    416:                                tty_puts(tty, "\033[?1001l");
                    417:                        else if (tty->mode & MODE_MOUSE_BUTTON)
                    418:                                tty_puts(tty, "\033[?1002l");
                    419:                        else if (tty->mode & MODE_MOUSE_ANY)
1.86      nicm      420:                                tty_puts(tty, "\033[?1003l");
1.95      nicm      421:                        if (tty->mode & MODE_MOUSE_UTF8)
                    422:                                tty_puts(tty, "\033[?1005l");
1.86      nicm      423:                }
1.76      nicm      424:        }
                    425:        if (changed & MODE_KKEYPAD) {
                    426:                if (mode & MODE_KKEYPAD)
                    427:                        tty_putcode(tty, TTYC_SMKX);
                    428:                else
                    429:                        tty_putcode(tty, TTYC_RMKX);
1.1       nicm      430:        }
                    431:        tty->mode = mode;
                    432: }
                    433:
                    434: void
                    435: tty_emulate_repeat(
                    436:     struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
                    437: {
                    438:        if (tty_term_has(tty->term, code))
                    439:                tty_putcode1(tty, code, n);
                    440:        else {
                    441:                while (n-- > 0)
                    442:                        tty_putcode(tty, code1);
                    443:        }
                    444: }
                    445:
                    446: /*
                    447:  * Redraw scroll region using data from screen (already updated). Used when
                    448:  * CSR not supported, or window is a pane that doesn't take up the full
                    449:  * width of the terminal.
                    450:  */
                    451: void
1.24      nicm      452: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      453: {
1.14      nicm      454:        struct window_pane      *wp = ctx->wp;
                    455:        struct screen           *s = wp->screen;
                    456:        u_int                    i;
1.1       nicm      457:
                    458:        /*
                    459:         * If region is >= 50% of the screen, just schedule a window redraw. In
                    460:         * most cases, this is likely to be followed by some more scrolling -
                    461:         * without this, the entire pane ends up being redrawn many times which
                    462:         * can be much more data.
                    463:         */
1.14      nicm      464:        if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
1.1       nicm      465:                wp->flags |= PANE_REDRAW;
                    466:                return;
                    467:        }
                    468:
1.14      nicm      469:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    470:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.1       nicm      471:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    472:        } else {
1.14      nicm      473:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.1       nicm      474:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    475:        }
                    476: }
                    477:
                    478: void
                    479: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
                    480: {
                    481:        const struct grid_cell  *gc;
1.46      nicm      482:        struct grid_line        *gl;
1.16      nicm      483:        struct grid_cell         tmpgc;
1.1       nicm      484:        const struct grid_utf8  *gu;
                    485:        u_int                    i, sx;
                    486:
1.35      nicm      487:        tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
                    488:
1.1       nicm      489:        sx = screen_size_x(s);
1.19      nicm      490:        if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
                    491:                sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1       nicm      492:        if (sx > tty->sx)
                    493:                sx = tty->sx;
                    494:
1.46      nicm      495:        /*
                    496:         * Don't move the cursor to the start permission if it will wrap there
1.50      nicm      497:         * itself.
1.46      nicm      498:         */
                    499:        gl = NULL;
                    500:        if (py != 0)
                    501:                gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83      nicm      502:        if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47      nicm      503:            tty->cx < tty->sx || ox != 0 ||
                    504:            (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46      nicm      505:                tty_cursor(tty, ox, oy + py);
                    506:
1.1       nicm      507:        for (i = 0; i < sx; i++) {
                    508:                gc = grid_view_peek_cell(s->grid, i, py);
                    509:
                    510:                gu = NULL;
                    511:                if (gc->flags & GRID_FLAG_UTF8)
                    512:                        gu = grid_view_peek_utf8(s->grid, i, py);
                    513:
                    514:                if (screen_check_selection(s, i, py)) {
1.16      nicm      515:                        memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
                    516:                        tmpgc.data = gc->data;
1.77      nicm      517:                        tmpgc.flags = gc->flags &
1.38      nicm      518:                            ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
                    519:                        tmpgc.flags |= s->sel.cell.flags &
                    520:                            (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16      nicm      521:                        tty_cell(tty, &tmpgc, gu);
1.1       nicm      522:                } else
                    523:                        tty_cell(tty, gc, gu);
                    524:        }
                    525:
1.35      nicm      526:        if (sx >= tty->sx) {
                    527:                tty_update_mode(tty, tty->mode);
1.1       nicm      528:                return;
1.35      nicm      529:        }
1.1       nicm      530:        tty_reset(tty);
                    531:
1.41      nicm      532:        tty_cursor(tty, ox + sx, oy + py);
1.1       nicm      533:        if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
                    534:                tty_putcode(tty, TTYC_EL);
                    535:        else {
                    536:                for (i = sx; i < screen_size_x(s); i++)
                    537:                        tty_putc(tty, ' ');
1.15      nicm      538:        }
1.35      nicm      539:        tty_update_mode(tty, tty->mode);
1.15      nicm      540: }
                    541:
                    542: void
1.24      nicm      543: tty_write(void (*cmdfn)(
                    544:     struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
1.15      nicm      545: {
                    546:        struct window_pane      *wp = ctx->wp;
                    547:        struct client           *c;
                    548:        u_int                    i;
                    549:
1.75      nicm      550:        /* wp can be NULL if updating the screen but not the terminal. */
1.15      nicm      551:        if (wp == NULL)
                    552:                return;
                    553:
                    554:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    555:                return;
1.93      nicm      556:        if (!window_pane_visible(wp))
1.15      nicm      557:                return;
                    558:
                    559:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    560:                c = ARRAY_ITEM(&clients, i);
                    561:                if (c == NULL || c->session == NULL)
                    562:                        continue;
                    563:                if (c->flags & CLIENT_SUSPENDED)
                    564:                        continue;
                    565:
                    566:                if (c->session->curw->window == wp->window) {
1.89      nicm      567:                        if (c->tty.term == NULL)
                    568:                                continue;
                    569:                        if (c->tty.flags & (TTY_FREEZE|TTY_BACKOFF))
1.15      nicm      570:                                continue;
                    571:                        cmdfn(&c->tty, ctx);
                    572:                }
1.1       nicm      573:        }
                    574: }
                    575:
                    576: void
1.24      nicm      577: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      578: {
1.77      nicm      579:        struct window_pane      *wp = ctx->wp;
1.12      nicm      580:        struct screen           *s = wp->screen;
1.24      nicm      581:        u_int                    i;
1.1       nicm      582:
                    583:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
1.14      nicm      584:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      585:                return;
                    586:        }
                    587:
                    588:        tty_reset(tty);
                    589:
1.77      nicm      590:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      591:
1.1       nicm      592:        if (tty_term_has(tty->term, TTYC_ICH) ||
                    593:            tty_term_has(tty->term, TTYC_ICH1))
1.12      nicm      594:                tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.77      nicm      595:        else if (tty_term_has(tty->term, TTYC_SMIR) &&
1.72      nicm      596:            tty_term_has(tty->term, TTYC_RMIR)) {
1.1       nicm      597:                tty_putcode(tty, TTYC_SMIR);
1.24      nicm      598:                for (i = 0; i < ctx->num; i++)
1.1       nicm      599:                        tty_putc(tty, ' ');
                    600:                tty_putcode(tty, TTYC_RMIR);
1.72      nicm      601:        } else
                    602:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      603: }
                    604:
                    605: void
1.24      nicm      606: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      607: {
1.77      nicm      608:        struct window_pane      *wp = ctx->wp;
1.12      nicm      609:        struct screen           *s = wp->screen;
1.1       nicm      610:
1.26      nicm      611:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    612:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    613:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.14      nicm      614:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      615:                return;
                    616:        }
                    617:
                    618:        tty_reset(tty);
                    619:
1.77      nicm      620:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      621:
1.26      nicm      622:        if (tty_term_has(tty->term, TTYC_DCH) ||
                    623:            tty_term_has(tty->term, TTYC_DCH1))
                    624:                tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1       nicm      625: }
                    626:
                    627: void
1.24      nicm      628: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      629: {
1.77      nicm      630:        struct window_pane      *wp = ctx->wp;
1.12      nicm      631:        struct screen           *s = wp->screen;
1.1       nicm      632:
1.77      nicm      633:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    634:            !tty_term_has(tty->term, TTYC_CSR) ||
1.72      nicm      635:            !tty_term_has(tty->term, TTYC_IL1)) {
1.14      nicm      636:                tty_redraw_region(tty, ctx);
1.1       nicm      637:                return;
                    638:        }
                    639:
                    640:        tty_reset(tty);
                    641:
1.77      nicm      642:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    643:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      644:
1.12      nicm      645:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      646: }
                    647:
                    648: void
1.24      nicm      649: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      650: {
1.77      nicm      651:        struct window_pane      *wp = ctx->wp;
1.12      nicm      652:        struct screen           *s = wp->screen;
1.1       nicm      653:
1.77      nicm      654:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
1.72      nicm      655:            !tty_term_has(tty->term, TTYC_CSR) ||
                    656:            !tty_term_has(tty->term, TTYC_DL1)) {
1.14      nicm      657:                tty_redraw_region(tty, ctx);
1.1       nicm      658:                return;
                    659:        }
                    660:
                    661:        tty_reset(tty);
                    662:
1.77      nicm      663:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    664:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      665:
1.12      nicm      666:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      667: }
                    668:
                    669: void
1.24      nicm      670: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      671: {
1.77      nicm      672:        struct window_pane      *wp = ctx->wp;
1.12      nicm      673:        struct screen           *s = wp->screen;
                    674:        u_int                    i;
1.1       nicm      675:
                    676:        tty_reset(tty);
                    677:
1.77      nicm      678:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41      nicm      679:
1.1       nicm      680:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    681:            tty_term_has(tty->term, TTYC_EL)) {
                    682:                tty_putcode(tty, TTYC_EL);
                    683:        } else {
                    684:                for (i = 0; i < screen_size_x(s); i++)
                    685:                        tty_putc(tty, ' ');
                    686:        }
                    687: }
                    688:
                    689: void
1.24      nicm      690: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      691: {
1.77      nicm      692:        struct window_pane      *wp = ctx->wp;
1.12      nicm      693:        struct screen           *s = wp->screen;
                    694:        u_int                    i;
1.1       nicm      695:
                    696:        tty_reset(tty);
                    697:
1.41      nicm      698:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    699:
1.1       nicm      700:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    701:            tty_term_has(tty->term, TTYC_EL))
                    702:                tty_putcode(tty, TTYC_EL);
                    703:        else {
1.14      nicm      704:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      705:                        tty_putc(tty, ' ');
                    706:        }
                    707: }
                    708:
                    709: void
1.24      nicm      710: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      711: {
1.77      nicm      712:        struct window_pane      *wp = ctx->wp;
1.12      nicm      713:        u_int                    i;
1.1       nicm      714:
                    715:        tty_reset(tty);
                    716:
                    717:        if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41      nicm      718:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      719:                tty_putcode(tty, TTYC_EL1);
                    720:        } else {
1.41      nicm      721:                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.14      nicm      722:                for (i = 0; i < ctx->ocx + 1; i++)
1.1       nicm      723:                        tty_putc(tty, ' ');
                    724:        }
                    725: }
                    726:
                    727: void
1.24      nicm      728: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      729: {
1.77      nicm      730:        struct window_pane      *wp = ctx->wp;
1.12      nicm      731:        struct screen           *s = wp->screen;
1.1       nicm      732:
1.56      nicm      733:        if (ctx->ocy != ctx->orupper)
                    734:                return;
                    735:
1.77      nicm      736:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
1.70      nicm      737:            !tty_term_has(tty->term, TTYC_CSR) ||
                    738:            !tty_term_has(tty->term, TTYC_RI)) {
1.14      nicm      739:                tty_redraw_region(tty, ctx);
1.1       nicm      740:                return;
                    741:        }
                    742:
1.56      nicm      743:        tty_reset(tty);
1.77      nicm      744:
1.56      nicm      745:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    746:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm      747:
1.56      nicm      748:        tty_putcode(tty, TTYC_RI);
1.1       nicm      749: }
                    750:
                    751: void
1.24      nicm      752: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      753: {
1.77      nicm      754:        struct window_pane      *wp = ctx->wp;
1.12      nicm      755:        struct screen           *s = wp->screen;
1.1       nicm      756:
1.56      nicm      757:        if (ctx->ocy != ctx->orlower)
                    758:                return;
                    759:
1.77      nicm      760:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
1.1       nicm      761:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      762:                tty_redraw_region(tty, ctx);
1.1       nicm      763:                return;
                    764:        }
1.52      nicm      765:
                    766:        /*
                    767:         * If this line wrapped naturally (ctx->num is nonzero), don't do
                    768:         * anything - the cursor can just be moved to the last cell and wrap
                    769:         * naturally.
                    770:         */
                    771:        if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
                    772:                return;
1.1       nicm      773:
1.56      nicm      774:        tty_reset(tty);
1.77      nicm      775:
1.56      nicm      776:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    777:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm      778:
1.56      nicm      779:        tty_putc(tty, '\n');
1.1       nicm      780: }
                    781:
                    782: void
1.24      nicm      783: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      784: {
1.77      nicm      785:        struct window_pane      *wp = ctx->wp;
1.12      nicm      786:        struct screen           *s = wp->screen;
                    787:        u_int                    i, j;
1.1       nicm      788:
                    789:        tty_reset(tty);
                    790:
1.39      nicm      791:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      792:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39      nicm      793:
1.1       nicm      794:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    795:            tty_term_has(tty->term, TTYC_EL)) {
                    796:                tty_putcode(tty, TTYC_EL);
1.14      nicm      797:                if (ctx->ocy != screen_size_y(s) - 1) {
1.41      nicm      798:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14      nicm      799:                        for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1       nicm      800:                                tty_putcode(tty, TTYC_EL);
                    801:                                if (i == screen_size_y(s) - 1)
                    802:                                        continue;
                    803:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    804:                                tty->cy++;
                    805:                        }
                    806:                }
                    807:        } else {
1.14      nicm      808:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      809:                        tty_putc(tty, ' ');
1.68      nicm      810:                for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41      nicm      811:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      812:                        for (i = 0; i < screen_size_x(s); i++)
                    813:                                tty_putc(tty, ' ');
                    814:                }
                    815:        }
                    816: }
                    817:
                    818: void
1.24      nicm      819: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      820: {
1.77      nicm      821:        struct window_pane      *wp = ctx->wp;
1.12      nicm      822:        struct screen           *s = wp->screen;
                    823:        u_int                    i, j;
1.1       nicm      824:
                    825:        tty_reset(tty);
                    826:
1.39      nicm      827:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      828:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm      829:
1.1       nicm      830:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    831:            tty_term_has(tty->term, TTYC_EL)) {
1.14      nicm      832:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm      833:                        tty_putcode(tty, TTYC_EL);
                    834:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    835:                        tty->cy++;
                    836:                }
                    837:        } else {
1.14      nicm      838:                for (j = 0; j < ctx->ocy; j++) {
1.41      nicm      839:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      840:                        for (i = 0; i < screen_size_x(s); i++)
                    841:                                tty_putc(tty, ' ');
                    842:                }
                    843:        }
1.14      nicm      844:        for (i = 0; i <= ctx->ocx; i++)
1.1       nicm      845:                tty_putc(tty, ' ');
                    846: }
                    847:
                    848: void
1.24      nicm      849: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      850: {
1.77      nicm      851:        struct window_pane      *wp = ctx->wp;
1.12      nicm      852:        struct screen           *s = wp->screen;
                    853:        u_int                    i, j;
1.1       nicm      854:
                    855:        tty_reset(tty);
                    856:
1.39      nicm      857:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      858:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm      859:
1.1       nicm      860:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    861:            tty_term_has(tty->term, TTYC_EL)) {
                    862:                for (i = 0; i < screen_size_y(s); i++) {
                    863:                        tty_putcode(tty, TTYC_EL);
                    864:                        if (i != screen_size_y(s) - 1) {
                    865:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    866:                                tty->cy++;
                    867:                        }
                    868:                }
                    869:        } else {
                    870:                for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      871:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      872:                        for (i = 0; i < screen_size_x(s); i++)
                    873:                                tty_putc(tty, ' ');
                    874:                }
1.4       nicm      875:        }
                    876: }
                    877:
                    878: void
1.24      nicm      879: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm      880: {
1.12      nicm      881:        struct window_pane      *wp = ctx->wp;
                    882:        struct screen           *s = wp->screen;
                    883:        u_int                    i, j;
1.4       nicm      884:
                    885:        tty_reset(tty);
                    886:
1.39      nicm      887:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4       nicm      888:
                    889:        for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      890:                tty_cursor_pane(tty, ctx, 0, j);
1.4       nicm      891:                for (i = 0; i < screen_size_x(s); i++)
                    892:                        tty_putc(tty, 'E');
1.1       nicm      893:        }
                    894: }
                    895:
                    896: void
1.24      nicm      897: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      898: {
1.46      nicm      899:        struct window_pane      *wp = ctx->wp;
                    900:        struct screen           *s = wp->screen;
1.58      nicm      901:        u_int                    cx;
1.46      nicm      902:
                    903:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    904:
1.57      nicm      905:        /* Is the cursor in the very last position? */
                    906:        if (ctx->ocx > wp->sx - ctx->last_width) {
                    907:                if (wp->xoff != 0 || wp->sx != tty->sx) {
                    908:                        /*
                    909:                         * The pane doesn't fill the entire line, the linefeed
                    910:                         * will already have happened, so just move the cursor.
                    911:                         */
                    912:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
                    913:                } else if (tty->cx < tty->sx) {
                    914:                        /*
                    915:                         * The cursor isn't in the last position already, so
                    916:                         * move as far left as possinble and redraw the last
                    917:                         * cell to move into the last position.
                    918:                         */
1.50      nicm      919:                        cx = screen_size_x(s) - ctx->last_width;
                    920:                        tty_cursor_pane(tty, ctx, cx, ctx->ocy);
                    921:                        tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
                    922:                }
                    923:        } else
1.46      nicm      924:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      925:
1.12      nicm      926:        tty_cell(tty, ctx->cell, ctx->utf8);
1.1       nicm      927: }
                    928:
                    929: void
1.24      nicm      930: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      931: {
1.53      nicm      932:        struct window_pane      *wp = ctx->wp;
1.1       nicm      933:
1.53      nicm      934:        /*
                    935:         * Cannot rely on not being a partial character, so just redraw the
                    936:         * whole line.
                    937:         */
1.77      nicm      938:        tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      939: }
                    940:
                    941: void
                    942: tty_cell(
                    943:     struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
                    944: {
                    945:        u_int   i;
                    946:
                    947:        /* Skip last character if terminal is stupid. */
                    948:        if (tty->term->flags & TERM_EARLYWRAP &&
                    949:            tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
                    950:                return;
                    951:
                    952:        /* If this is a padding character, do nothing. */
                    953:        if (gc->flags & GRID_FLAG_PADDING)
                    954:                return;
                    955:
                    956:        /* Set the attributes. */
                    957:        tty_attributes(tty, gc);
                    958:
                    959:        /* If not UTF-8, write directly. */
                    960:        if (!(gc->flags & GRID_FLAG_UTF8)) {
                    961:                if (gc->data < 0x20 || gc->data == 0x7f)
                    962:                        return;
                    963:                tty_putc(tty, gc->data);
                    964:                return;
                    965:        }
                    966:
                    967:        /* If the terminal doesn't support UTF-8, write underscores. */
                    968:        if (!(tty->flags & TTY_UTF8)) {
                    969:                for (i = 0; i < gu->width; i++)
                    970:                        tty_putc(tty, '_');
                    971:                return;
                    972:        }
                    973:
                    974:        /* Otherwise, write UTF-8. */
1.5       nicm      975:        tty_pututf8(tty, gu);
1.1       nicm      976: }
                    977:
                    978: void
                    979: tty_reset(struct tty *tty)
                    980: {
                    981:        struct grid_cell        *gc = &tty->cell;
                    982:
                    983:        if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
                    984:                return;
                    985:
1.90      nicm      986:        if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1       nicm      987:                tty_putcode(tty, TTYC_RMACS);
                    988:        tty_putcode(tty, TTYC_SGR0);
                    989:        memcpy(gc, &grid_default_cell, sizeof *gc);
                    990: }
                    991:
1.40      nicm      992: /* Set region inside pane. */
1.1       nicm      993: void
1.39      nicm      994: tty_region_pane(
                    995:     struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1       nicm      996: {
1.77      nicm      997:        struct window_pane      *wp = ctx->wp;
1.39      nicm      998:
1.40      nicm      999:        tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
1.39      nicm     1000: }
                   1001:
1.40      nicm     1002: /* Set region at absolute position. */
1.39      nicm     1003: void
1.40      nicm     1004: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm     1005: {
                   1006:        if (tty->rlower == rlower && tty->rupper == rupper)
                   1007:                return;
1.1       nicm     1008:        if (!tty_term_has(tty->term, TTYC_CSR))
                   1009:                return;
1.39      nicm     1010:
                   1011:        tty->rupper = rupper;
                   1012:        tty->rlower = rlower;
1.55      nicm     1013:
                   1014:        /*
                   1015:         * Some terminals (such as PuTTY) do not correctly reset the cursor to
                   1016:         * 0,0 if it is beyond the last column (they do not reset their wrap
                   1017:         * flag so further output causes a line feed). As a workaround, do an
                   1018:         * explicit move to 0 first.
                   1019:         */
                   1020:        if (tty->cx >= tty->sx)
                   1021:                tty_cursor(tty, 0, tty->cy);
1.42      nicm     1022:
1.39      nicm     1023:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78      nicm     1024:        tty_cursor(tty, 0, 0);
1.1       nicm     1025: }
                   1026:
1.42      nicm     1027: /* Move cursor inside pane. */
1.1       nicm     1028: void
1.41      nicm     1029: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                   1030: {
1.77      nicm     1031:        struct window_pane      *wp = ctx->wp;
1.41      nicm     1032:
                   1033:        tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
                   1034: }
                   1035:
1.42      nicm     1036: /* Move cursor to absolute position. */
1.41      nicm     1037: void
                   1038: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm     1039: {
1.42      nicm     1040:        struct tty_term *term = tty->term;
                   1041:        u_int            thisx, thisy;
                   1042:        int              change;
1.77      nicm     1043:
1.42      nicm     1044:        if (cx > tty->sx - 1)
                   1045:                cx = tty->sx - 1;
                   1046:
                   1047:        thisx = tty->cx;
                   1048:        thisy = tty->cy;
                   1049:
                   1050:        /* No change. */
                   1051:        if (cx == thisx && cy == thisy)
                   1052:                return;
                   1053:
1.43      nicm     1054:        /* Very end of the line, just use absolute movement. */
                   1055:        if (thisx > tty->sx - 1)
                   1056:                goto absolute;
                   1057:
1.42      nicm     1058:        /* Move to home position (0, 0). */
                   1059:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                   1060:                tty_putcode(tty, TTYC_HOME);
                   1061:                goto out;
                   1062:        }
                   1063:
                   1064:        /* Zero on the next line. */
1.48      nicm     1065:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1       nicm     1066:                tty_putc(tty, '\r');
1.42      nicm     1067:                tty_putc(tty, '\n');
                   1068:                goto out;
                   1069:        }
                   1070:
1.45      nicm     1071:        /* Moving column or row. */
1.42      nicm     1072:        if (cy == thisy) {
1.45      nicm     1073:                /*
                   1074:                 * Moving column only, row staying the same.
                   1075:                 */
                   1076:
1.42      nicm     1077:                /* To left edge. */
                   1078:                if (cx == 0)    {
                   1079:                        tty_putc(tty, '\r');
                   1080:                        goto out;
                   1081:                }
                   1082:
                   1083:                /* One to the left. */
                   1084:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   1085:                        tty_putcode(tty, TTYC_CUB1);
                   1086:                        goto out;
                   1087:                }
                   1088:
                   1089:                /* One to the right. */
                   1090:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   1091:                        tty_putcode(tty, TTYC_CUF1);
                   1092:                        goto out;
                   1093:                }
                   1094:
                   1095:                /* Calculate difference. */
                   1096:                change = thisx - cx;    /* +ve left, -ve right */
                   1097:
                   1098:                /*
                   1099:                 * Use HPA if change is larger than absolute, otherwise move
                   1100:                 * the cursor with CUB/CUF.
                   1101:                 */
1.87      nicm     1102:                if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42      nicm     1103:                        tty_putcode1(tty, TTYC_HPA, cx);
                   1104:                        goto out;
                   1105:                } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
                   1106:                        tty_putcode1(tty, TTYC_CUB, change);
                   1107:                        goto out;
                   1108:                } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
                   1109:                        tty_putcode1(tty, TTYC_CUF, -change);
                   1110:                        goto out;
                   1111:                }
1.45      nicm     1112:        } else if (cx == thisx) {
                   1113:                /*
                   1114:                 * Moving row only, column staying the same.
                   1115:                 */
1.42      nicm     1116:
                   1117:                /* One above. */
1.77      nicm     1118:                if (thisy != tty->rupper &&
1.42      nicm     1119:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   1120:                        tty_putcode(tty, TTYC_CUU1);
                   1121:                        goto out;
                   1122:                }
                   1123:
                   1124:                /* One below. */
1.49      nicm     1125:                if (thisy != tty->rlower &&
1.42      nicm     1126:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   1127:                        tty_putcode(tty, TTYC_CUD1);
                   1128:                        goto out;
                   1129:                }
                   1130:
                   1131:                /* Calculate difference. */
                   1132:                change = thisy - cy;    /* +ve up, -ve down */
                   1133:
                   1134:                /*
1.77      nicm     1135:                 * Try to use VPA if change is larger than absolute or if this
                   1136:                 * change would cross the scroll region, otherwise use CUU/CUD.
1.42      nicm     1137:                 */
1.87      nicm     1138:                if ((u_int) abs(change) > cy ||
1.42      nicm     1139:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     1140:                    (change > 0 && cy - change < tty->rupper)) {
                   1141:                            if (tty_term_has(term, TTYC_VPA)) {
                   1142:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   1143:                                    goto out;
                   1144:                            }
1.42      nicm     1145:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   1146:                        tty_putcode1(tty, TTYC_CUU, change);
                   1147:                        goto out;
                   1148:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   1149:                        tty_putcode1(tty, TTYC_CUD, -change);
                   1150:                        goto out;
                   1151:                }
                   1152:        }
                   1153:
1.43      nicm     1154: absolute:
1.42      nicm     1155:        /* Absolute movement. */
                   1156:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   1157:
                   1158: out:
                   1159:        tty->cx = cx;
                   1160:        tty->cy = cy;
1.1       nicm     1161: }
                   1162:
                   1163: void
                   1164: tty_attributes(struct tty *tty, const struct grid_cell *gc)
                   1165: {
1.63      nicm     1166:        struct grid_cell        *tc = &tty->cell, gc2;
1.85      nicm     1167:        u_char                   changed;
1.61      nicm     1168:
1.85      nicm     1169:        memcpy(&gc2, gc, sizeof gc2);
1.63      nicm     1170:
1.18      nicm     1171:        /*
                   1172:         * If no setab, try to use the reverse attribute as a best-effort for a
                   1173:         * non-default background. This is a bit of a hack but it doesn't do
                   1174:         * any serious harm and makes a couple of applications happier.
                   1175:         */
                   1176:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85      nicm     1177:                if (gc2.attr & GRID_ATTR_REVERSE) {
                   1178:                        if (gc2.fg != 7 && gc2.fg != 8)
1.64      nicm     1179:                                gc2.attr &= ~GRID_ATTR_REVERSE;
1.18      nicm     1180:                } else {
1.85      nicm     1181:                        if (gc2.bg != 0 && gc2.bg != 8)
1.64      nicm     1182:                                gc2.attr |= GRID_ATTR_REVERSE;
1.18      nicm     1183:                }
                   1184:        }
1.1       nicm     1185:
1.85      nicm     1186:        /* Fix up the colours if necessary. */
                   1187:        tty_check_fg(tty, &gc2);
                   1188:        tty_check_bg(tty, &gc2);
                   1189:
1.64      nicm     1190:        /* If any bits are being cleared, reset everything. */
1.85      nicm     1191:        if (tc->attr & ~gc2.attr)
1.64      nicm     1192:                tty_reset(tty);
                   1193:
                   1194:        /*
                   1195:         * Set the colours. This may call tty_reset() (so it comes next) and
                   1196:         * may add to (NOT remove) the desired attributes by changing new_attr.
                   1197:         */
1.85      nicm     1198:        tty_colours(tty, &gc2);
1.64      nicm     1199:
1.1       nicm     1200:        /* Filter out attribute bits already set. */
1.85      nicm     1201:        changed = gc2.attr & ~tc->attr;
                   1202:        tc->attr = gc2.attr;
1.1       nicm     1203:
                   1204:        /* Set the attributes. */
                   1205:        if (changed & GRID_ATTR_BRIGHT)
                   1206:                tty_putcode(tty, TTYC_BOLD);
                   1207:        if (changed & GRID_ATTR_DIM)
                   1208:                tty_putcode(tty, TTYC_DIM);
                   1209:        if (changed & GRID_ATTR_ITALICS)
                   1210:                tty_putcode(tty, TTYC_SMSO);
                   1211:        if (changed & GRID_ATTR_UNDERSCORE)
                   1212:                tty_putcode(tty, TTYC_SMUL);
                   1213:        if (changed & GRID_ATTR_BLINK)
                   1214:                tty_putcode(tty, TTYC_BLINK);
                   1215:        if (changed & GRID_ATTR_REVERSE) {
                   1216:                if (tty_term_has(tty->term, TTYC_REV))
                   1217:                        tty_putcode(tty, TTYC_REV);
                   1218:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   1219:                        tty_putcode(tty, TTYC_SMSO);
                   1220:        }
                   1221:        if (changed & GRID_ATTR_HIDDEN)
                   1222:                tty_putcode(tty, TTYC_INVIS);
1.90      nicm     1223:        if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1       nicm     1224:                tty_putcode(tty, TTYC_SMACS);
                   1225: }
                   1226:
1.61      nicm     1227: void
1.85      nicm     1228: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1229: {
1.61      nicm     1230:        struct grid_cell        *tc = &tty->cell;
1.62      nicm     1231:        u_char                   fg = gc->fg, bg = gc->bg, flags = gc->flags;
                   1232:        int                      have_ax, fg_default, bg_default;
1.61      nicm     1233:
                   1234:        /* No changes? Nothing is necessary. */
1.62      nicm     1235:        if (fg == tc->fg && bg == tc->bg &&
                   1236:            ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1.63      nicm     1237:                return;
1.1       nicm     1238:
1.61      nicm     1239:        /*
                   1240:         * Is either the default colour? This is handled specially because the
                   1241:         * best solution might be to reset both colours to default, in which
                   1242:         * case if only one is default need to fall onward to set the other
                   1243:         * colour.
                   1244:         */
1.62      nicm     1245:        fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
                   1246:        bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1.61      nicm     1247:        if (fg_default || bg_default) {
                   1248:                /*
                   1249:                 * If don't have AX but do have op, send sgr0 (op can't
                   1250:                 * actually be used because it is sometimes the same as sgr0
                   1251:                 * and sometimes isn't). This resets both colours to default.
                   1252:                 *
                   1253:                 * Otherwise, try to set the default colour only as needed.
                   1254:                 */
                   1255:                have_ax = tty_term_has(tty->term, TTYC_AX);
                   1256:                if (!have_ax && tty_term_has(tty->term, TTYC_OP))
                   1257:                        tty_reset(tty);
                   1258:                else {
                   1259:                        if (fg_default &&
1.81      nicm     1260:                            (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1.61      nicm     1261:                                if (have_ax)
                   1262:                                        tty_puts(tty, "\033[39m");
1.81      nicm     1263:                                else if (tc->fg != 7 ||
                   1264:                                    tc->flags & GRID_FLAG_FG256)
1.61      nicm     1265:                                        tty_putcode1(tty, TTYC_SETAF, 7);
                   1266:                                tc->fg = 8;
                   1267:                                tc->flags &= ~GRID_FLAG_FG256;
                   1268:                        }
                   1269:                        if (bg_default &&
1.81      nicm     1270:                            (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1.77      nicm     1271:                                if (have_ax)
1.61      nicm     1272:                                        tty_puts(tty, "\033[49m");
1.81      nicm     1273:                                else if (tc->bg != 0 ||
                   1274:                                    tc->flags & GRID_FLAG_BG256)
1.61      nicm     1275:                                        tty_putcode1(tty, TTYC_SETAB, 0);
                   1276:                                tc->bg = 8;
                   1277:                                tc->flags &= ~GRID_FLAG_BG256;
                   1278:                        }
                   1279:                }
                   1280:        }
1.1       nicm     1281:
1.61      nicm     1282:        /* Set the foreground colour. */
                   1283:        if (!fg_default && (fg != tc->fg ||
1.62      nicm     1284:            ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1.85      nicm     1285:                tty_colours_fg(tty, gc);
1.1       nicm     1286:
1.61      nicm     1287:        /*
                   1288:         * Set the background colour. This must come after the foreground as
                   1289:         * tty_colour_fg() can call tty_reset().
                   1290:         */
                   1291:        if (!bg_default && (bg != tc->bg ||
1.62      nicm     1292:            ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1.73      nicm     1293:                tty_colours_bg(tty, gc);
1.1       nicm     1294: }
                   1295:
                   1296: void
1.85      nicm     1297: tty_check_fg(struct tty *tty, struct grid_cell *gc)
                   1298: {
                   1299:        u_int   colours;
                   1300:
                   1301:        /* Is this a 256-colour colour? */
                   1302:        if (gc->flags & GRID_FLAG_FG256) {
                   1303:                /* And not a 256 colour mode? */
                   1304:                if (!(tty->term->flags & TERM_88COLOURS) &&
                   1305:                    !(tty->term_flags & TERM_88COLOURS) &&
                   1306:                    !(tty->term->flags & TERM_256COLOURS) &&
                   1307:                    !(tty->term_flags & TERM_256COLOURS)) {
                   1308:                        gc->fg = colour_256to16(gc->fg);
                   1309:                        if (gc->fg & 8) {
                   1310:                                gc->fg &= 7;
                   1311:                                gc->attr |= GRID_ATTR_BRIGHT;
                   1312:                        } else
                   1313:                                gc->attr &= ~GRID_ATTR_BRIGHT;
                   1314:                        gc->flags &= ~GRID_FLAG_FG256;
                   1315:                }
                   1316:                return;
                   1317:        }
                   1318:
                   1319:        /* Is this an aixterm colour? */
                   1320:        colours = tty_term_number(tty->term, TTYC_COLORS);
                   1321:        if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
                   1322:                gc->fg -= 90;
                   1323:                gc->attr |= GRID_ATTR_BRIGHT;
                   1324:        }
                   1325: }
                   1326:
                   1327: void
                   1328: tty_check_bg(struct tty *tty, struct grid_cell *gc)
                   1329: {
                   1330:        u_int   colours;
                   1331:
                   1332:        /* Is this a 256-colour colour? */
                   1333:        if (gc->flags & GRID_FLAG_BG256) {
                   1334:                /*
                   1335:                 * And not a 256 colour mode? Translate to 16-colour
                   1336:                 * palette. Bold background doesn't exist portably, so just
                   1337:                 * discard the bold bit if set.
                   1338:                 */
                   1339:                if (!(tty->term->flags & TERM_88COLOURS) &&
                   1340:                    !(tty->term_flags & TERM_88COLOURS) &&
                   1341:                    !(tty->term->flags & TERM_256COLOURS) &&
                   1342:                    !(tty->term_flags & TERM_256COLOURS)) {
                   1343:                        gc->bg = colour_256to16(gc->bg);
                   1344:                        if (gc->bg & 8)
                   1345:                                gc->bg &= 7;
                   1346:                        gc->attr &= ~GRID_ATTR_BRIGHT;
                   1347:                        gc->flags &= ~GRID_FLAG_BG256;
                   1348:                }
                   1349:                return;
                   1350:        }
                   1351:
                   1352:        /* Is this an aixterm colour? */
                   1353:        colours = tty_term_number(tty->term, TTYC_COLORS);
                   1354:        if (gc->bg >= 100 && gc->bg <= 107 && colours < 16) {
                   1355:                gc->bg -= 90;
                   1356:                gc->attr |= GRID_ATTR_BRIGHT;
                   1357:        }
                   1358: }
                   1359:
                   1360: void
                   1361: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1362: {
1.61      nicm     1363:        struct grid_cell        *tc = &tty->cell;
                   1364:        u_char                   fg = gc->fg;
1.79      nicm     1365:        char                     s[32];
1.1       nicm     1366:
1.61      nicm     1367:        /* Is this a 256-colour colour? */
1.1       nicm     1368:        if (gc->flags & GRID_FLAG_FG256) {
1.61      nicm     1369:                /* Try as 256 colours or translating to 88. */
1.1       nicm     1370:                if (tty_try_256(tty, fg, "38") == 0)
1.61      nicm     1371:                        goto save_fg;
1.1       nicm     1372:                if (tty_try_88(tty, fg, "38") == 0)
1.61      nicm     1373:                        goto save_fg;
1.85      nicm     1374:                /* Else already handled by tty_check_fg. */
                   1375:                return;
1.1       nicm     1376:        }
                   1377:
1.79      nicm     1378:        /* Is this an aixterm bright colour? */
                   1379:        if (fg >= 90 && fg <= 97) {
1.85      nicm     1380:                xsnprintf(s, sizeof s, "\033[%dm", fg);
                   1381:                tty_puts(tty, s);
                   1382:                goto save_fg;
1.79      nicm     1383:        }
                   1384:
1.61      nicm     1385:        /* Otherwise set the foreground colour. */
                   1386:        tty_putcode1(tty, TTYC_SETAF, fg);
                   1387:
1.77      nicm     1388: save_fg:
1.61      nicm     1389:        /* Save the new values in the terminal current cell. */
                   1390:        tc->fg = fg;
                   1391:        tc->flags &= ~GRID_FLAG_FG256;
                   1392:        tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1       nicm     1393: }
                   1394:
                   1395: void
1.73      nicm     1396: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1397: {
1.61      nicm     1398:        struct grid_cell        *tc = &tty->cell;
                   1399:        u_char                   bg = gc->bg;
1.79      nicm     1400:        char                     s[32];
1.1       nicm     1401:
1.61      nicm     1402:        /* Is this a 256-colour colour? */
1.1       nicm     1403:        if (gc->flags & GRID_FLAG_BG256) {
1.61      nicm     1404:                /* Try as 256 colours or translating to 88. */
1.1       nicm     1405:                if (tty_try_256(tty, bg, "48") == 0)
1.61      nicm     1406:                        goto save_bg;
1.1       nicm     1407:                if (tty_try_88(tty, bg, "48") == 0)
1.61      nicm     1408:                        goto save_bg;
1.85      nicm     1409:                /* Else already handled by tty_check_bg. */
                   1410:                return;
1.79      nicm     1411:        }
                   1412:
                   1413:        /* Is this an aixterm bright colour? */
                   1414:        if (bg >= 100 && bg <= 107) {
                   1415:                /* 16 colour terminals or above only. */
                   1416:                if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
                   1417:                        xsnprintf(s, sizeof s, "\033[%dm", bg);
                   1418:                        tty_puts(tty, s);
                   1419:                        goto save_bg;
                   1420:                }
                   1421:                bg -= 100;
                   1422:                /* no such thing as a bold background */
1.1       nicm     1423:        }
                   1424:
1.61      nicm     1425:        /* Otherwise set the background colour. */
                   1426:        tty_putcode1(tty, TTYC_SETAB, bg);
                   1427:
                   1428: save_bg:
                   1429:        /* Save the new values in the terminal current cell. */
                   1430:        tc->bg = bg;
                   1431:        tc->flags &= ~GRID_FLAG_BG256;
                   1432:        tc->flags |= gc->flags & GRID_FLAG_BG256;
                   1433: }
                   1434:
                   1435: int
                   1436: tty_try_256(struct tty *tty, u_char colour, const char *type)
                   1437: {
                   1438:        char    s[32];
                   1439:
                   1440:        if (!(tty->term->flags & TERM_256COLOURS) &&
                   1441:            !(tty->term_flags & TERM_256COLOURS))
                   1442:                return (-1);
                   1443:
                   1444:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1445:        tty_puts(tty, s);
                   1446:        return (0);
                   1447: }
                   1448:
                   1449: int
                   1450: tty_try_88(struct tty *tty, u_char colour, const char *type)
                   1451: {
                   1452:        char    s[32];
                   1453:
                   1454:        if (!(tty->term->flags & TERM_88COLOURS) &&
                   1455:            !(tty->term_flags & TERM_88COLOURS))
                   1456:                return (-1);
                   1457:        colour = colour_256to88(colour);
                   1458:
                   1459:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1460:        tty_puts(tty, s);
                   1461:        return (0);
1.1       nicm     1462: }