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

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