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

1.85    ! nicm        1: /* $OpenBSD: tty.c,v 1.84 2010/02/24 19:08:39 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:        }
                    405:        if (changed & MODE_MOUSE) {
                    406:                if (mode & MODE_MOUSE)
                    407:                        tty_puts(tty, "\033[?1000h");
                    408:                else
                    409:                        tty_puts(tty, "\033[?1000l");
1.76      nicm      410:        }
                    411:        if (changed & MODE_KKEYPAD) {
                    412:                if (mode & MODE_KKEYPAD)
                    413:                        tty_putcode(tty, TTYC_SMKX);
                    414:                else
                    415:                        tty_putcode(tty, TTYC_RMKX);
1.1       nicm      416:        }
                    417:        tty->mode = mode;
                    418: }
                    419:
                    420: void
                    421: tty_emulate_repeat(
                    422:     struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
                    423: {
                    424:        if (tty_term_has(tty->term, code))
                    425:                tty_putcode1(tty, code, n);
                    426:        else {
                    427:                while (n-- > 0)
                    428:                        tty_putcode(tty, code1);
                    429:        }
                    430: }
                    431:
                    432: /*
                    433:  * Redraw scroll region using data from screen (already updated). Used when
                    434:  * CSR not supported, or window is a pane that doesn't take up the full
                    435:  * width of the terminal.
                    436:  */
                    437: void
1.24      nicm      438: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      439: {
1.14      nicm      440:        struct window_pane      *wp = ctx->wp;
                    441:        struct screen           *s = wp->screen;
                    442:        u_int                    i;
1.1       nicm      443:
                    444:        /*
                    445:         * If region is >= 50% of the screen, just schedule a window redraw. In
                    446:         * most cases, this is likely to be followed by some more scrolling -
                    447:         * without this, the entire pane ends up being redrawn many times which
                    448:         * can be much more data.
                    449:         */
1.14      nicm      450:        if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
1.1       nicm      451:                wp->flags |= PANE_REDRAW;
                    452:                return;
                    453:        }
                    454:
1.14      nicm      455:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    456:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.1       nicm      457:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    458:        } else {
1.14      nicm      459:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.1       nicm      460:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    461:        }
                    462: }
                    463:
                    464: void
                    465: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
                    466: {
                    467:        const struct grid_cell  *gc;
1.46      nicm      468:        struct grid_line        *gl;
1.16      nicm      469:        struct grid_cell         tmpgc;
1.1       nicm      470:        const struct grid_utf8  *gu;
                    471:        u_int                    i, sx;
                    472:
1.35      nicm      473:        tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
                    474:
1.1       nicm      475:        sx = screen_size_x(s);
1.19      nicm      476:        if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
                    477:                sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1       nicm      478:        if (sx > tty->sx)
                    479:                sx = tty->sx;
                    480:
1.46      nicm      481:        /*
                    482:         * Don't move the cursor to the start permission if it will wrap there
1.50      nicm      483:         * itself.
1.46      nicm      484:         */
                    485:        gl = NULL;
                    486:        if (py != 0)
                    487:                gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83      nicm      488:        if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47      nicm      489:            tty->cx < tty->sx || ox != 0 ||
                    490:            (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46      nicm      491:                tty_cursor(tty, ox, oy + py);
                    492:
1.1       nicm      493:        for (i = 0; i < sx; i++) {
                    494:                gc = grid_view_peek_cell(s->grid, i, py);
                    495:
                    496:                gu = NULL;
                    497:                if (gc->flags & GRID_FLAG_UTF8)
                    498:                        gu = grid_view_peek_utf8(s->grid, i, py);
                    499:
                    500:                if (screen_check_selection(s, i, py)) {
1.16      nicm      501:                        memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
                    502:                        tmpgc.data = gc->data;
1.77      nicm      503:                        tmpgc.flags = gc->flags &
1.38      nicm      504:                            ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
                    505:                        tmpgc.flags |= s->sel.cell.flags &
                    506:                            (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16      nicm      507:                        tty_cell(tty, &tmpgc, gu);
1.1       nicm      508:                } else
                    509:                        tty_cell(tty, gc, gu);
                    510:        }
                    511:
1.35      nicm      512:        if (sx >= tty->sx) {
                    513:                tty_update_mode(tty, tty->mode);
1.1       nicm      514:                return;
1.35      nicm      515:        }
1.1       nicm      516:        tty_reset(tty);
                    517:
1.41      nicm      518:        tty_cursor(tty, ox + sx, oy + py);
1.1       nicm      519:        if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
                    520:                tty_putcode(tty, TTYC_EL);
                    521:        else {
                    522:                for (i = sx; i < screen_size_x(s); i++)
                    523:                        tty_putc(tty, ' ');
1.15      nicm      524:        }
1.35      nicm      525:        tty_update_mode(tty, tty->mode);
1.15      nicm      526: }
                    527:
                    528: void
1.24      nicm      529: tty_write(void (*cmdfn)(
                    530:     struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
1.15      nicm      531: {
                    532:        struct window_pane      *wp = ctx->wp;
                    533:        struct client           *c;
                    534:        u_int                    i;
                    535:
1.75      nicm      536:        /* wp can be NULL if updating the screen but not the terminal. */
1.15      nicm      537:        if (wp == NULL)
                    538:                return;
                    539:
                    540:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    541:                return;
                    542:        if (wp->window->flags & WINDOW_HIDDEN || !window_pane_visible(wp))
                    543:                return;
                    544:
                    545:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    546:                c = ARRAY_ITEM(&clients, i);
                    547:                if (c == NULL || c->session == NULL)
                    548:                        continue;
                    549:                if (c->flags & CLIENT_SUSPENDED)
                    550:                        continue;
                    551:
                    552:                if (c->session->curw->window == wp->window) {
                    553:                        if (c->tty.flags & TTY_FREEZE || c->tty.term == NULL)
                    554:                                continue;
                    555:                        cmdfn(&c->tty, ctx);
                    556:                }
1.1       nicm      557:        }
                    558: }
                    559:
                    560: void
1.24      nicm      561: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      562: {
1.77      nicm      563:        struct window_pane      *wp = ctx->wp;
1.12      nicm      564:        struct screen           *s = wp->screen;
1.24      nicm      565:        u_int                    i;
1.1       nicm      566:
                    567:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
1.14      nicm      568:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      569:                return;
                    570:        }
                    571:
                    572:        tty_reset(tty);
                    573:
1.77      nicm      574:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      575:
1.1       nicm      576:        if (tty_term_has(tty->term, TTYC_ICH) ||
                    577:            tty_term_has(tty->term, TTYC_ICH1))
1.12      nicm      578:                tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.77      nicm      579:        else if (tty_term_has(tty->term, TTYC_SMIR) &&
1.72      nicm      580:            tty_term_has(tty->term, TTYC_RMIR)) {
1.1       nicm      581:                tty_putcode(tty, TTYC_SMIR);
1.24      nicm      582:                for (i = 0; i < ctx->num; i++)
1.1       nicm      583:                        tty_putc(tty, ' ');
                    584:                tty_putcode(tty, TTYC_RMIR);
1.72      nicm      585:        } else
                    586:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      587: }
                    588:
                    589: void
1.24      nicm      590: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      591: {
1.77      nicm      592:        struct window_pane      *wp = ctx->wp;
1.12      nicm      593:        struct screen           *s = wp->screen;
1.1       nicm      594:
1.26      nicm      595:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    596:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    597:            !tty_term_has(tty->term, TTYC_DCH1))) {
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.26      nicm      606:        if (tty_term_has(tty->term, TTYC_DCH) ||
                    607:            tty_term_has(tty->term, TTYC_DCH1))
                    608:                tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1       nicm      609: }
                    610:
                    611: void
1.24      nicm      612: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      613: {
1.77      nicm      614:        struct window_pane      *wp = ctx->wp;
1.12      nicm      615:        struct screen           *s = wp->screen;
1.1       nicm      616:
1.77      nicm      617:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    618:            !tty_term_has(tty->term, TTYC_CSR) ||
1.72      nicm      619:            !tty_term_has(tty->term, TTYC_IL1)) {
1.14      nicm      620:                tty_redraw_region(tty, ctx);
1.1       nicm      621:                return;
                    622:        }
                    623:
                    624:        tty_reset(tty);
                    625:
1.77      nicm      626:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    627:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      628:
1.12      nicm      629:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      630: }
                    631:
                    632: void
1.24      nicm      633: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      634: {
1.77      nicm      635:        struct window_pane      *wp = ctx->wp;
1.12      nicm      636:        struct screen           *s = wp->screen;
1.1       nicm      637:
1.77      nicm      638:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
1.72      nicm      639:            !tty_term_has(tty->term, TTYC_CSR) ||
                    640:            !tty_term_has(tty->term, TTYC_DL1)) {
1.14      nicm      641:                tty_redraw_region(tty, ctx);
1.1       nicm      642:                return;
                    643:        }
                    644:
                    645:        tty_reset(tty);
                    646:
1.77      nicm      647:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    648:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      649:
1.12      nicm      650:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      651: }
                    652:
                    653: void
1.24      nicm      654: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      655: {
1.77      nicm      656:        struct window_pane      *wp = ctx->wp;
1.12      nicm      657:        struct screen           *s = wp->screen;
                    658:        u_int                    i;
1.1       nicm      659:
                    660:        tty_reset(tty);
                    661:
1.77      nicm      662:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41      nicm      663:
1.1       nicm      664:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    665:            tty_term_has(tty->term, TTYC_EL)) {
                    666:                tty_putcode(tty, TTYC_EL);
                    667:        } else {
                    668:                for (i = 0; i < screen_size_x(s); i++)
                    669:                        tty_putc(tty, ' ');
                    670:        }
                    671: }
                    672:
                    673: void
1.24      nicm      674: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      675: {
1.77      nicm      676:        struct window_pane      *wp = ctx->wp;
1.12      nicm      677:        struct screen           *s = wp->screen;
                    678:        u_int                    i;
1.1       nicm      679:
                    680:        tty_reset(tty);
                    681:
1.41      nicm      682:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    683:
1.1       nicm      684:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    685:            tty_term_has(tty->term, TTYC_EL))
                    686:                tty_putcode(tty, TTYC_EL);
                    687:        else {
1.14      nicm      688:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      689:                        tty_putc(tty, ' ');
                    690:        }
                    691: }
                    692:
                    693: void
1.24      nicm      694: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      695: {
1.77      nicm      696:        struct window_pane      *wp = ctx->wp;
1.12      nicm      697:        u_int                    i;
1.1       nicm      698:
                    699:        tty_reset(tty);
                    700:
                    701:        if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41      nicm      702:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      703:                tty_putcode(tty, TTYC_EL1);
                    704:        } else {
1.41      nicm      705:                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.14      nicm      706:                for (i = 0; i < ctx->ocx + 1; i++)
1.1       nicm      707:                        tty_putc(tty, ' ');
                    708:        }
                    709: }
                    710:
                    711: void
1.24      nicm      712: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      713: {
1.77      nicm      714:        struct window_pane      *wp = ctx->wp;
1.12      nicm      715:        struct screen           *s = wp->screen;
1.1       nicm      716:
1.56      nicm      717:        if (ctx->ocy != ctx->orupper)
                    718:                return;
                    719:
1.77      nicm      720:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
1.70      nicm      721:            !tty_term_has(tty->term, TTYC_CSR) ||
                    722:            !tty_term_has(tty->term, TTYC_RI)) {
1.14      nicm      723:                tty_redraw_region(tty, ctx);
1.1       nicm      724:                return;
                    725:        }
                    726:
1.56      nicm      727:        tty_reset(tty);
1.77      nicm      728:
1.56      nicm      729:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    730:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm      731:
1.56      nicm      732:        tty_putcode(tty, TTYC_RI);
1.1       nicm      733: }
                    734:
                    735: void
1.24      nicm      736: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      737: {
1.77      nicm      738:        struct window_pane      *wp = ctx->wp;
1.12      nicm      739:        struct screen           *s = wp->screen;
1.1       nicm      740:
1.56      nicm      741:        if (ctx->ocy != ctx->orlower)
                    742:                return;
                    743:
1.77      nicm      744:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
1.1       nicm      745:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      746:                tty_redraw_region(tty, ctx);
1.1       nicm      747:                return;
                    748:        }
1.52      nicm      749:
                    750:        /*
                    751:         * If this line wrapped naturally (ctx->num is nonzero), don't do
                    752:         * anything - the cursor can just be moved to the last cell and wrap
                    753:         * naturally.
                    754:         */
                    755:        if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
                    756:                return;
1.1       nicm      757:
1.56      nicm      758:        tty_reset(tty);
1.77      nicm      759:
1.56      nicm      760:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    761:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm      762:
1.56      nicm      763:        tty_putc(tty, '\n');
1.1       nicm      764: }
                    765:
                    766: void
1.24      nicm      767: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      768: {
1.77      nicm      769:        struct window_pane      *wp = ctx->wp;
1.12      nicm      770:        struct screen           *s = wp->screen;
                    771:        u_int                    i, j;
1.1       nicm      772:
                    773:        tty_reset(tty);
                    774:
1.39      nicm      775:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      776:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39      nicm      777:
1.1       nicm      778:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    779:            tty_term_has(tty->term, TTYC_EL)) {
                    780:                tty_putcode(tty, TTYC_EL);
1.14      nicm      781:                if (ctx->ocy != screen_size_y(s) - 1) {
1.41      nicm      782:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14      nicm      783:                        for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1       nicm      784:                                tty_putcode(tty, TTYC_EL);
                    785:                                if (i == screen_size_y(s) - 1)
                    786:                                        continue;
                    787:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    788:                                tty->cy++;
                    789:                        }
                    790:                }
                    791:        } else {
1.14      nicm      792:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      793:                        tty_putc(tty, ' ');
1.68      nicm      794:                for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41      nicm      795:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      796:                        for (i = 0; i < screen_size_x(s); i++)
                    797:                                tty_putc(tty, ' ');
                    798:                }
                    799:        }
                    800: }
                    801:
                    802: void
1.24      nicm      803: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      804: {
1.77      nicm      805:        struct window_pane      *wp = ctx->wp;
1.12      nicm      806:        struct screen           *s = wp->screen;
                    807:        u_int                    i, j;
1.1       nicm      808:
                    809:        tty_reset(tty);
                    810:
1.39      nicm      811:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      812:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm      813:
1.1       nicm      814:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    815:            tty_term_has(tty->term, TTYC_EL)) {
1.14      nicm      816:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm      817:                        tty_putcode(tty, TTYC_EL);
                    818:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    819:                        tty->cy++;
                    820:                }
                    821:        } else {
1.14      nicm      822:                for (j = 0; j < ctx->ocy; j++) {
1.41      nicm      823:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      824:                        for (i = 0; i < screen_size_x(s); i++)
                    825:                                tty_putc(tty, ' ');
                    826:                }
                    827:        }
1.14      nicm      828:        for (i = 0; i <= ctx->ocx; i++)
1.1       nicm      829:                tty_putc(tty, ' ');
                    830: }
                    831:
                    832: void
1.24      nicm      833: tty_cmd_clearscreen(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)) {
                    846:                for (i = 0; i < screen_size_y(s); i++) {
                    847:                        tty_putcode(tty, TTYC_EL);
                    848:                        if (i != screen_size_y(s) - 1) {
                    849:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    850:                                tty->cy++;
                    851:                        }
                    852:                }
                    853:        } else {
                    854:                for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      855:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      856:                        for (i = 0; i < screen_size_x(s); i++)
                    857:                                tty_putc(tty, ' ');
                    858:                }
1.4       nicm      859:        }
                    860: }
                    861:
                    862: void
1.24      nicm      863: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm      864: {
1.12      nicm      865:        struct window_pane      *wp = ctx->wp;
                    866:        struct screen           *s = wp->screen;
                    867:        u_int                    i, j;
1.4       nicm      868:
                    869:        tty_reset(tty);
                    870:
1.39      nicm      871:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4       nicm      872:
                    873:        for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      874:                tty_cursor_pane(tty, ctx, 0, j);
1.4       nicm      875:                for (i = 0; i < screen_size_x(s); i++)
                    876:                        tty_putc(tty, 'E');
1.1       nicm      877:        }
                    878: }
                    879:
                    880: void
1.24      nicm      881: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      882: {
1.46      nicm      883:        struct window_pane      *wp = ctx->wp;
                    884:        struct screen           *s = wp->screen;
1.58      nicm      885:        u_int                    cx;
1.46      nicm      886:
                    887:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    888:
1.57      nicm      889:        /* Is the cursor in the very last position? */
                    890:        if (ctx->ocx > wp->sx - ctx->last_width) {
                    891:                if (wp->xoff != 0 || wp->sx != tty->sx) {
                    892:                        /*
                    893:                         * The pane doesn't fill the entire line, the linefeed
                    894:                         * will already have happened, so just move the cursor.
                    895:                         */
                    896:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
                    897:                } else if (tty->cx < tty->sx) {
                    898:                        /*
                    899:                         * The cursor isn't in the last position already, so
                    900:                         * move as far left as possinble and redraw the last
                    901:                         * cell to move into the last position.
                    902:                         */
1.50      nicm      903:                        cx = screen_size_x(s) - ctx->last_width;
                    904:                        tty_cursor_pane(tty, ctx, cx, ctx->ocy);
                    905:                        tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
                    906:                }
                    907:        } else
1.46      nicm      908:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      909:
1.12      nicm      910:        tty_cell(tty, ctx->cell, ctx->utf8);
1.1       nicm      911: }
                    912:
                    913: void
1.24      nicm      914: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      915: {
1.53      nicm      916:        struct window_pane      *wp = ctx->wp;
1.1       nicm      917:
1.53      nicm      918:        /*
                    919:         * Cannot rely on not being a partial character, so just redraw the
                    920:         * whole line.
                    921:         */
1.77      nicm      922:        tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      923: }
                    924:
                    925: void
                    926: tty_cell(
                    927:     struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
                    928: {
                    929:        u_int   i;
                    930:
                    931:        /* Skip last character if terminal is stupid. */
                    932:        if (tty->term->flags & TERM_EARLYWRAP &&
                    933:            tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
                    934:                return;
                    935:
                    936:        /* If this is a padding character, do nothing. */
                    937:        if (gc->flags & GRID_FLAG_PADDING)
                    938:                return;
                    939:
                    940:        /* Set the attributes. */
                    941:        tty_attributes(tty, gc);
                    942:
                    943:        /* If not UTF-8, write directly. */
                    944:        if (!(gc->flags & GRID_FLAG_UTF8)) {
                    945:                if (gc->data < 0x20 || gc->data == 0x7f)
                    946:                        return;
                    947:                tty_putc(tty, gc->data);
                    948:                return;
                    949:        }
                    950:
                    951:        /* If the terminal doesn't support UTF-8, write underscores. */
                    952:        if (!(tty->flags & TTY_UTF8)) {
                    953:                for (i = 0; i < gu->width; i++)
                    954:                        tty_putc(tty, '_');
                    955:                return;
                    956:        }
                    957:
                    958:        /* Otherwise, write UTF-8. */
1.5       nicm      959:        tty_pututf8(tty, gu);
1.1       nicm      960: }
                    961:
                    962: void
                    963: tty_reset(struct tty *tty)
                    964: {
                    965:        struct grid_cell        *gc = &tty->cell;
                    966:
                    967:        if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
                    968:                return;
                    969:
                    970:        if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
                    971:                tty_putcode(tty, TTYC_RMACS);
                    972:        tty_putcode(tty, TTYC_SGR0);
                    973:        memcpy(gc, &grid_default_cell, sizeof *gc);
                    974: }
                    975:
1.40      nicm      976: /* Set region inside pane. */
1.1       nicm      977: void
1.39      nicm      978: tty_region_pane(
                    979:     struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1       nicm      980: {
1.77      nicm      981:        struct window_pane      *wp = ctx->wp;
1.39      nicm      982:
1.40      nicm      983:        tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
1.39      nicm      984: }
                    985:
1.40      nicm      986: /* Set region at absolute position. */
1.39      nicm      987: void
1.40      nicm      988: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm      989: {
                    990:        if (tty->rlower == rlower && tty->rupper == rupper)
                    991:                return;
1.1       nicm      992:        if (!tty_term_has(tty->term, TTYC_CSR))
                    993:                return;
1.39      nicm      994:
                    995:        tty->rupper = rupper;
                    996:        tty->rlower = rlower;
1.55      nicm      997:
                    998:        /*
                    999:         * Some terminals (such as PuTTY) do not correctly reset the cursor to
                   1000:         * 0,0 if it is beyond the last column (they do not reset their wrap
                   1001:         * flag so further output causes a line feed). As a workaround, do an
                   1002:         * explicit move to 0 first.
                   1003:         */
                   1004:        if (tty->cx >= tty->sx)
                   1005:                tty_cursor(tty, 0, tty->cy);
1.42      nicm     1006:
1.39      nicm     1007:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78      nicm     1008:        tty_cursor(tty, 0, 0);
1.1       nicm     1009: }
                   1010:
1.42      nicm     1011: /* Move cursor inside pane. */
1.1       nicm     1012: void
1.41      nicm     1013: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                   1014: {
1.77      nicm     1015:        struct window_pane      *wp = ctx->wp;
1.41      nicm     1016:
                   1017:        tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
                   1018: }
                   1019:
1.42      nicm     1020: /* Move cursor to absolute position. */
1.41      nicm     1021: void
                   1022: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm     1023: {
1.42      nicm     1024:        struct tty_term *term = tty->term;
                   1025:        u_int            thisx, thisy;
                   1026:        int              change;
1.77      nicm     1027:
1.42      nicm     1028:        if (cx > tty->sx - 1)
                   1029:                cx = tty->sx - 1;
                   1030:
                   1031:        thisx = tty->cx;
                   1032:        thisy = tty->cy;
                   1033:
                   1034:        /* No change. */
                   1035:        if (cx == thisx && cy == thisy)
                   1036:                return;
                   1037:
1.43      nicm     1038:        /* Very end of the line, just use absolute movement. */
                   1039:        if (thisx > tty->sx - 1)
                   1040:                goto absolute;
                   1041:
1.42      nicm     1042:        /* Move to home position (0, 0). */
                   1043:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                   1044:                tty_putcode(tty, TTYC_HOME);
                   1045:                goto out;
                   1046:        }
                   1047:
                   1048:        /* Zero on the next line. */
1.48      nicm     1049:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1       nicm     1050:                tty_putc(tty, '\r');
1.42      nicm     1051:                tty_putc(tty, '\n');
                   1052:                goto out;
                   1053:        }
                   1054:
1.45      nicm     1055:        /* Moving column or row. */
1.42      nicm     1056:        if (cy == thisy) {
1.45      nicm     1057:                /*
                   1058:                 * Moving column only, row staying the same.
                   1059:                 */
                   1060:
1.42      nicm     1061:                /* To left edge. */
                   1062:                if (cx == 0)    {
                   1063:                        tty_putc(tty, '\r');
                   1064:                        goto out;
                   1065:                }
                   1066:
                   1067:                /* One to the left. */
                   1068:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   1069:                        tty_putcode(tty, TTYC_CUB1);
                   1070:                        goto out;
                   1071:                }
                   1072:
                   1073:                /* One to the right. */
                   1074:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   1075:                        tty_putcode(tty, TTYC_CUF1);
                   1076:                        goto out;
                   1077:                }
                   1078:
                   1079:                /* Calculate difference. */
                   1080:                change = thisx - cx;    /* +ve left, -ve right */
                   1081:
                   1082:                /*
                   1083:                 * Use HPA if change is larger than absolute, otherwise move
                   1084:                 * the cursor with CUB/CUF.
                   1085:                 */
                   1086:                if (abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
                   1087:                        tty_putcode1(tty, TTYC_HPA, cx);
                   1088:                        goto out;
                   1089:                } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
                   1090:                        tty_putcode1(tty, TTYC_CUB, change);
                   1091:                        goto out;
                   1092:                } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
                   1093:                        tty_putcode1(tty, TTYC_CUF, -change);
                   1094:                        goto out;
                   1095:                }
1.45      nicm     1096:        } else if (cx == thisx) {
                   1097:                /*
                   1098:                 * Moving row only, column staying the same.
                   1099:                 */
1.42      nicm     1100:
                   1101:                /* One above. */
1.77      nicm     1102:                if (thisy != tty->rupper &&
1.42      nicm     1103:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   1104:                        tty_putcode(tty, TTYC_CUU1);
                   1105:                        goto out;
                   1106:                }
                   1107:
                   1108:                /* One below. */
1.49      nicm     1109:                if (thisy != tty->rlower &&
1.42      nicm     1110:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   1111:                        tty_putcode(tty, TTYC_CUD1);
                   1112:                        goto out;
                   1113:                }
                   1114:
                   1115:                /* Calculate difference. */
                   1116:                change = thisy - cy;    /* +ve up, -ve down */
                   1117:
                   1118:                /*
1.77      nicm     1119:                 * Try to use VPA if change is larger than absolute or if this
                   1120:                 * change would cross the scroll region, otherwise use CUU/CUD.
1.42      nicm     1121:                 */
1.44      nicm     1122:                if (abs(change) > cy ||
1.42      nicm     1123:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     1124:                    (change > 0 && cy - change < tty->rupper)) {
                   1125:                            if (tty_term_has(term, TTYC_VPA)) {
                   1126:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   1127:                                    goto out;
                   1128:                            }
1.42      nicm     1129:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   1130:                        tty_putcode1(tty, TTYC_CUU, change);
                   1131:                        goto out;
                   1132:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   1133:                        tty_putcode1(tty, TTYC_CUD, -change);
                   1134:                        goto out;
                   1135:                }
                   1136:        }
                   1137:
1.43      nicm     1138: absolute:
1.42      nicm     1139:        /* Absolute movement. */
                   1140:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   1141:
                   1142: out:
                   1143:        tty->cx = cx;
                   1144:        tty->cy = cy;
1.1       nicm     1145: }
                   1146:
                   1147: void
                   1148: tty_attributes(struct tty *tty, const struct grid_cell *gc)
                   1149: {
1.63      nicm     1150:        struct grid_cell        *tc = &tty->cell, gc2;
1.85    ! nicm     1151:        u_char                   changed;
1.61      nicm     1152:
1.85    ! nicm     1153:        memcpy(&gc2, gc, sizeof gc2);
1.63      nicm     1154:
1.18      nicm     1155:        /*
                   1156:         * If no setab, try to use the reverse attribute as a best-effort for a
                   1157:         * non-default background. This is a bit of a hack but it doesn't do
                   1158:         * any serious harm and makes a couple of applications happier.
                   1159:         */
                   1160:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85    ! nicm     1161:                if (gc2.attr & GRID_ATTR_REVERSE) {
        !          1162:                        if (gc2.fg != 7 && gc2.fg != 8)
1.64      nicm     1163:                                gc2.attr &= ~GRID_ATTR_REVERSE;
1.18      nicm     1164:                } else {
1.85    ! nicm     1165:                        if (gc2.bg != 0 && gc2.bg != 8)
1.64      nicm     1166:                                gc2.attr |= GRID_ATTR_REVERSE;
1.18      nicm     1167:                }
                   1168:        }
1.1       nicm     1169:
1.85    ! nicm     1170:        /* Fix up the colours if necessary. */
        !          1171:        tty_check_fg(tty, &gc2);
        !          1172:        tty_check_bg(tty, &gc2);
        !          1173:
1.64      nicm     1174:        /* If any bits are being cleared, reset everything. */
1.85    ! nicm     1175:        if (tc->attr & ~gc2.attr)
1.64      nicm     1176:                tty_reset(tty);
                   1177:
                   1178:        /*
                   1179:         * Set the colours. This may call tty_reset() (so it comes next) and
                   1180:         * may add to (NOT remove) the desired attributes by changing new_attr.
                   1181:         */
1.85    ! nicm     1182:        tty_colours(tty, &gc2);
1.64      nicm     1183:
1.1       nicm     1184:        /* Filter out attribute bits already set. */
1.85    ! nicm     1185:        changed = gc2.attr & ~tc->attr;
        !          1186:        tc->attr = gc2.attr;
1.1       nicm     1187:
                   1188:        /* Set the attributes. */
                   1189:        if (changed & GRID_ATTR_BRIGHT)
                   1190:                tty_putcode(tty, TTYC_BOLD);
                   1191:        if (changed & GRID_ATTR_DIM)
                   1192:                tty_putcode(tty, TTYC_DIM);
                   1193:        if (changed & GRID_ATTR_ITALICS)
                   1194:                tty_putcode(tty, TTYC_SMSO);
                   1195:        if (changed & GRID_ATTR_UNDERSCORE)
                   1196:                tty_putcode(tty, TTYC_SMUL);
                   1197:        if (changed & GRID_ATTR_BLINK)
                   1198:                tty_putcode(tty, TTYC_BLINK);
                   1199:        if (changed & GRID_ATTR_REVERSE) {
                   1200:                if (tty_term_has(tty->term, TTYC_REV))
                   1201:                        tty_putcode(tty, TTYC_REV);
                   1202:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   1203:                        tty_putcode(tty, TTYC_SMSO);
                   1204:        }
                   1205:        if (changed & GRID_ATTR_HIDDEN)
                   1206:                tty_putcode(tty, TTYC_INVIS);
                   1207:        if (changed & GRID_ATTR_CHARSET)
                   1208:                tty_putcode(tty, TTYC_SMACS);
                   1209: }
                   1210:
1.61      nicm     1211: void
1.85    ! nicm     1212: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1213: {
1.61      nicm     1214:        struct grid_cell        *tc = &tty->cell;
1.62      nicm     1215:        u_char                   fg = gc->fg, bg = gc->bg, flags = gc->flags;
                   1216:        int                      have_ax, fg_default, bg_default;
1.61      nicm     1217:
                   1218:        /* No changes? Nothing is necessary. */
1.62      nicm     1219:        if (fg == tc->fg && bg == tc->bg &&
                   1220:            ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1.63      nicm     1221:                return;
1.1       nicm     1222:
1.61      nicm     1223:        /*
                   1224:         * Is either the default colour? This is handled specially because the
                   1225:         * best solution might be to reset both colours to default, in which
                   1226:         * case if only one is default need to fall onward to set the other
                   1227:         * colour.
                   1228:         */
1.62      nicm     1229:        fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
                   1230:        bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1.61      nicm     1231:        if (fg_default || bg_default) {
                   1232:                /*
                   1233:                 * If don't have AX but do have op, send sgr0 (op can't
                   1234:                 * actually be used because it is sometimes the same as sgr0
                   1235:                 * and sometimes isn't). This resets both colours to default.
                   1236:                 *
                   1237:                 * Otherwise, try to set the default colour only as needed.
                   1238:                 */
                   1239:                have_ax = tty_term_has(tty->term, TTYC_AX);
                   1240:                if (!have_ax && tty_term_has(tty->term, TTYC_OP))
                   1241:                        tty_reset(tty);
                   1242:                else {
                   1243:                        if (fg_default &&
1.81      nicm     1244:                            (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1.61      nicm     1245:                                if (have_ax)
                   1246:                                        tty_puts(tty, "\033[39m");
1.81      nicm     1247:                                else if (tc->fg != 7 ||
                   1248:                                    tc->flags & GRID_FLAG_FG256)
1.61      nicm     1249:                                        tty_putcode1(tty, TTYC_SETAF, 7);
                   1250:                                tc->fg = 8;
                   1251:                                tc->flags &= ~GRID_FLAG_FG256;
                   1252:                        }
                   1253:                        if (bg_default &&
1.81      nicm     1254:                            (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1.77      nicm     1255:                                if (have_ax)
1.61      nicm     1256:                                        tty_puts(tty, "\033[49m");
1.81      nicm     1257:                                else if (tc->bg != 0 ||
                   1258:                                    tc->flags & GRID_FLAG_BG256)
1.61      nicm     1259:                                        tty_putcode1(tty, TTYC_SETAB, 0);
                   1260:                                tc->bg = 8;
                   1261:                                tc->flags &= ~GRID_FLAG_BG256;
                   1262:                        }
                   1263:                }
                   1264:        }
1.1       nicm     1265:
1.61      nicm     1266:        /* Set the foreground colour. */
                   1267:        if (!fg_default && (fg != tc->fg ||
1.62      nicm     1268:            ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1.85    ! nicm     1269:                tty_colours_fg(tty, gc);
1.1       nicm     1270:
1.61      nicm     1271:        /*
                   1272:         * Set the background colour. This must come after the foreground as
                   1273:         * tty_colour_fg() can call tty_reset().
                   1274:         */
                   1275:        if (!bg_default && (bg != tc->bg ||
1.62      nicm     1276:            ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1.73      nicm     1277:                tty_colours_bg(tty, gc);
1.1       nicm     1278: }
                   1279:
                   1280: void
1.85    ! nicm     1281: tty_check_fg(struct tty *tty, struct grid_cell *gc)
        !          1282: {
        !          1283:        u_int   colours;
        !          1284:
        !          1285:        /* Is this a 256-colour colour? */
        !          1286:        if (gc->flags & GRID_FLAG_FG256) {
        !          1287:                /* And not a 256 colour mode? */
        !          1288:                if (!(tty->term->flags & TERM_88COLOURS) &&
        !          1289:                    !(tty->term_flags & TERM_88COLOURS) &&
        !          1290:                    !(tty->term->flags & TERM_256COLOURS) &&
        !          1291:                    !(tty->term_flags & TERM_256COLOURS)) {
        !          1292:                        gc->fg = colour_256to16(gc->fg);
        !          1293:                        if (gc->fg & 8) {
        !          1294:                                gc->fg &= 7;
        !          1295:                                gc->attr |= GRID_ATTR_BRIGHT;
        !          1296:                        } else
        !          1297:                                gc->attr &= ~GRID_ATTR_BRIGHT;
        !          1298:                        gc->flags &= ~GRID_FLAG_FG256;
        !          1299:                }
        !          1300:                return;
        !          1301:        }
        !          1302:
        !          1303:        /* Is this an aixterm colour? */
        !          1304:        colours = tty_term_number(tty->term, TTYC_COLORS);
        !          1305:        if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
        !          1306:                gc->fg -= 90;
        !          1307:                gc->attr |= GRID_ATTR_BRIGHT;
        !          1308:        }
        !          1309: }
        !          1310:
        !          1311: void
        !          1312: tty_check_bg(struct tty *tty, struct grid_cell *gc)
        !          1313: {
        !          1314:        u_int   colours;
        !          1315:
        !          1316:        /* Is this a 256-colour colour? */
        !          1317:        if (gc->flags & GRID_FLAG_BG256) {
        !          1318:                /*
        !          1319:                 * And not a 256 colour mode? Translate to 16-colour
        !          1320:                 * palette. Bold background doesn't exist portably, so just
        !          1321:                 * discard the bold bit if set.
        !          1322:                 */
        !          1323:                if (!(tty->term->flags & TERM_88COLOURS) &&
        !          1324:                    !(tty->term_flags & TERM_88COLOURS) &&
        !          1325:                    !(tty->term->flags & TERM_256COLOURS) &&
        !          1326:                    !(tty->term_flags & TERM_256COLOURS)) {
        !          1327:                        gc->bg = colour_256to16(gc->bg);
        !          1328:                        if (gc->bg & 8)
        !          1329:                                gc->bg &= 7;
        !          1330:                        gc->attr &= ~GRID_ATTR_BRIGHT;
        !          1331:                        gc->flags &= ~GRID_FLAG_BG256;
        !          1332:                }
        !          1333:                return;
        !          1334:        }
        !          1335:
        !          1336:        /* Is this an aixterm colour? */
        !          1337:        colours = tty_term_number(tty->term, TTYC_COLORS);
        !          1338:        if (gc->bg >= 100 && gc->bg <= 107 && colours < 16) {
        !          1339:                gc->bg -= 90;
        !          1340:                gc->attr |= GRID_ATTR_BRIGHT;
        !          1341:        }
        !          1342: }
        !          1343:
        !          1344: void
        !          1345: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1346: {
1.61      nicm     1347:        struct grid_cell        *tc = &tty->cell;
                   1348:        u_char                   fg = gc->fg;
1.79      nicm     1349:        char                     s[32];
1.1       nicm     1350:
1.61      nicm     1351:        /* Is this a 256-colour colour? */
1.1       nicm     1352:        if (gc->flags & GRID_FLAG_FG256) {
1.61      nicm     1353:                /* Try as 256 colours or translating to 88. */
1.1       nicm     1354:                if (tty_try_256(tty, fg, "38") == 0)
1.61      nicm     1355:                        goto save_fg;
1.1       nicm     1356:                if (tty_try_88(tty, fg, "38") == 0)
1.61      nicm     1357:                        goto save_fg;
1.85    ! nicm     1358:                /* Else already handled by tty_check_fg. */
        !          1359:                return;
1.1       nicm     1360:        }
                   1361:
1.79      nicm     1362:        /* Is this an aixterm bright colour? */
                   1363:        if (fg >= 90 && fg <= 97) {
1.85    ! nicm     1364:                xsnprintf(s, sizeof s, "\033[%dm", fg);
        !          1365:                tty_puts(tty, s);
        !          1366:                goto save_fg;
1.79      nicm     1367:        }
                   1368:
1.61      nicm     1369:        /* Otherwise set the foreground colour. */
                   1370:        tty_putcode1(tty, TTYC_SETAF, fg);
                   1371:
1.77      nicm     1372: save_fg:
1.61      nicm     1373:        /* Save the new values in the terminal current cell. */
                   1374:        tc->fg = fg;
                   1375:        tc->flags &= ~GRID_FLAG_FG256;
                   1376:        tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1       nicm     1377: }
                   1378:
                   1379: void
1.73      nicm     1380: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1381: {
1.61      nicm     1382:        struct grid_cell        *tc = &tty->cell;
                   1383:        u_char                   bg = gc->bg;
1.79      nicm     1384:        char                     s[32];
1.1       nicm     1385:
1.61      nicm     1386:        /* Is this a 256-colour colour? */
1.1       nicm     1387:        if (gc->flags & GRID_FLAG_BG256) {
1.61      nicm     1388:                /* Try as 256 colours or translating to 88. */
1.1       nicm     1389:                if (tty_try_256(tty, bg, "48") == 0)
1.61      nicm     1390:                        goto save_bg;
1.1       nicm     1391:                if (tty_try_88(tty, bg, "48") == 0)
1.61      nicm     1392:                        goto save_bg;
1.85    ! nicm     1393:                /* Else already handled by tty_check_bg. */
        !          1394:                return;
1.79      nicm     1395:        }
                   1396:
                   1397:        /* Is this an aixterm bright colour? */
                   1398:        if (bg >= 100 && bg <= 107) {
                   1399:                /* 16 colour terminals or above only. */
                   1400:                if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
                   1401:                        xsnprintf(s, sizeof s, "\033[%dm", bg);
                   1402:                        tty_puts(tty, s);
                   1403:                        goto save_bg;
                   1404:                }
                   1405:                bg -= 100;
                   1406:                /* no such thing as a bold background */
1.1       nicm     1407:        }
                   1408:
1.61      nicm     1409:        /* Otherwise set the background colour. */
                   1410:        tty_putcode1(tty, TTYC_SETAB, bg);
                   1411:
                   1412: save_bg:
                   1413:        /* Save the new values in the terminal current cell. */
                   1414:        tc->bg = bg;
                   1415:        tc->flags &= ~GRID_FLAG_BG256;
                   1416:        tc->flags |= gc->flags & GRID_FLAG_BG256;
                   1417: }
                   1418:
                   1419: int
                   1420: tty_try_256(struct tty *tty, u_char colour, const char *type)
                   1421: {
                   1422:        char    s[32];
                   1423:
                   1424:        if (!(tty->term->flags & TERM_256COLOURS) &&
                   1425:            !(tty->term_flags & TERM_256COLOURS))
                   1426:                return (-1);
                   1427:
                   1428:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1429:        tty_puts(tty, s);
                   1430:        return (0);
                   1431: }
                   1432:
                   1433: int
                   1434: tty_try_88(struct tty *tty, u_char colour, const char *type)
                   1435: {
                   1436:        char    s[32];
                   1437:
                   1438:        if (!(tty->term->flags & TERM_88COLOURS) &&
                   1439:            !(tty->term_flags & TERM_88COLOURS))
                   1440:                return (-1);
                   1441:        colour = colour_256to88(colour);
                   1442:
                   1443:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1444:        tty_puts(tty, s);
                   1445:        return (0);
1.1       nicm     1446: }