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

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