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

1.250   ! nicm        1: /* $OpenBSD: tty.c,v 1.249 2017/02/16 12:56:01 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.198     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.106     nicm       22: #include <netinet/in.h>
                     23:
1.212     nicm       24: #include <curses.h>
1.1       nicm       25: #include <errno.h>
                     26: #include <fcntl.h>
1.106     nicm       27: #include <resolv.h>
1.42      nicm       28: #include <stdlib.h>
1.1       nicm       29: #include <string.h>
                     30: #include <termios.h>
                     31: #include <unistd.h>
                     32:
                     33: #include "tmux.h"
                     34:
1.207     nicm       35: static int     tty_log_fd = -1;
1.192     nicm       36:
1.207     nicm       37: static int     tty_client_ready(struct client *, struct window_pane *);
1.201     nicm       38:
1.207     nicm       39: static void    tty_set_italics(struct tty *);
                     40: static int     tty_try_colour(struct tty *, int, const char *);
1.208     nicm       41: static void    tty_force_cursor_colour(struct tty *, const char *);
                     42: static void    tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
                     43:                    u_int);
1.242     nicm       44: static void    tty_invalidate(struct tty *);
1.207     nicm       45: static void    tty_colours(struct tty *, const struct grid_cell *);
1.219     nicm       46: static void    tty_check_fg(struct tty *, const struct window_pane *,
                     47:                    struct grid_cell *);
                     48: static void    tty_check_bg(struct tty *, const struct window_pane *,
                     49:                    struct grid_cell *);
1.207     nicm       50: static void    tty_colours_fg(struct tty *, const struct grid_cell *);
                     51: static void    tty_colours_bg(struct tty *, const struct grid_cell *);
                     52:
1.208     nicm       53: static void    tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
                     54:                    u_int);
1.214     nicm       55: static void    tty_region(struct tty *, u_int, u_int);
1.212     nicm       56: static void    tty_margin_pane(struct tty *, const struct tty_ctx *);
1.214     nicm       57: static void    tty_margin(struct tty *, u_int, u_int);
1.207     nicm       58: static int     tty_large_region(struct tty *, const struct tty_ctx *);
1.210     nicm       59: static int     tty_fake_bce(const struct tty *, const struct window_pane *,
                     60:                    u_int);
1.207     nicm       61: static void    tty_redraw_region(struct tty *, const struct tty_ctx *);
                     62: static void    tty_emulate_repeat(struct tty *, enum tty_code_code,
                     63:                    enum tty_code_code, u_int);
                     64: static void    tty_repeat_space(struct tty *, u_int);
                     65: static void    tty_cell(struct tty *, const struct grid_cell *,
                     66:                    const struct window_pane *);
                     67: static void    tty_default_colours(struct grid_cell *,
                     68:                    const struct window_pane *);
1.210     nicm       69: static void    tty_default_attributes(struct tty *, const struct window_pane *,
                     70:                    u_int);
1.1       nicm       71:
1.90      nicm       72: #define tty_use_acs(tty) \
1.131     nicm       73:        (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
1.212     nicm       74: #define tty_use_margin(tty) \
1.214     nicm       75:        ((tty)->term_type == TTY_VT420)
1.90      nicm       76:
1.229     nicm       77: #define tty_pane_full_width(tty, ctx)                                  \
1.132     nicm       78:        ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
                     79:
1.192     nicm       80: void
                     81: tty_create_log(void)
                     82: {
                     83:        char    name[64];
                     84:
                     85:        xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
                     86:
                     87:        tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
                     88:        if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
                     89:                fatal("fcntl failed");
                     90: }
                     91:
1.185     nicm       92: int
1.136     nicm       93: tty_init(struct tty *tty, struct client *c, int fd, char *term)
1.1       nicm       94: {
1.30      nicm       95:        char    *path;
                     96:
1.185     nicm       97:        if (!isatty(fd))
                     98:                return (-1);
                     99:
1.30      nicm      100:        memset(tty, 0, sizeof *tty);
1.22      nicm      101:
1.9       nicm      102:        if (term == NULL || *term == '\0')
1.220     nicm      103:                tty->term_name = xstrdup("unknown");
1.1       nicm      104:        else
1.220     nicm      105:                tty->term_name = xstrdup(term);
1.30      nicm      106:        tty->fd = fd;
1.136     nicm      107:        tty->client = c;
1.30      nicm      108:
                    109:        if ((path = ttyname(fd)) == NULL)
1.185     nicm      110:                return (-1);
1.30      nicm      111:        tty->path = xstrdup(path);
1.108     nicm      112:        tty->cstyle = 0;
1.107     nicm      113:        tty->ccolour = xstrdup("");
1.30      nicm      114:
1.1       nicm      115:        tty->flags = 0;
1.212     nicm      116:
1.1       nicm      117:        tty->term_flags = 0;
1.212     nicm      118:        tty->term_type = TTY_UNKNOWN;
1.185     nicm      119:
                    120:        return (0);
1.31      nicm      121: }
                    122:
1.88      nicm      123: int
1.31      nicm      124: tty_resize(struct tty *tty)
                    125: {
                    126:        struct winsize  ws;
1.88      nicm      127:        u_int           sx, sy;
1.31      nicm      128:
                    129:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88      nicm      130:                sx = ws.ws_col;
                    131:                if (sx == 0)
                    132:                        sx = 80;
                    133:                sy = ws.ws_row;
                    134:                if (sy == 0)
                    135:                        sy = 24;
                    136:        } else {
                    137:                sx = 80;
                    138:                sy = 24;
1.31      nicm      139:        }
1.235     nicm      140:        log_debug("%s: %s now %ux%u", __func__, tty->path, sx, sy);
1.114     nicm      141:        if (!tty_set_size(tty, sx, sy))
1.88      nicm      142:                return (0);
1.242     nicm      143:        tty_invalidate(tty);
1.114     nicm      144:        return (1);
                    145: }
                    146:
                    147: int
1.212     nicm      148: tty_set_size(struct tty *tty, u_int sx, u_int sy)
                    149: {
1.114     nicm      150:        if (sx == tty->sx && sy == tty->sy)
                    151:                return (0);
                    152:        tty->sx = sx;
                    153:        tty->sy = sy;
1.88      nicm      154:        return (1);
1.1       nicm      155: }
                    156:
1.244     nicm      157: static void
                    158: tty_read_callback(__unused int fd, __unused short events, void *data)
                    159: {
                    160:        struct tty      *tty = data;
                    161:        size_t           size = EVBUFFER_LENGTH(tty->in);
                    162:        int              nread;
                    163:
                    164:        nread = evbuffer_read(tty->in, tty->fd, -1);
                    165:        if (nread == -1)
                    166:                return;
                    167:        log_debug("%s: read %d bytes (already %zu)", tty->path, nread, size);
                    168:
                    169:        while (tty_keys_next(tty))
                    170:                ;
                    171: }
                    172:
                    173: static void
                    174: tty_write_callback(__unused int fd, __unused short events, void *data)
                    175: {
1.246     nicm      176:        struct tty      *tty = data;
                    177:        size_t           size = EVBUFFER_LENGTH(tty->out);
                    178:        int              nwrite;
1.244     nicm      179:
                    180:        nwrite = evbuffer_write(tty->out, tty->fd);
                    181:        if (nwrite == -1)
                    182:                return;
                    183:        log_debug("%s: wrote %d bytes (of %zu)", tty->path, nwrite, size);
                    184:
1.247     nicm      185:        if (EVBUFFER_LENGTH(tty->out) != 0)
                    186:                event_add(&tty->event_out, NULL);
1.244     nicm      187: }
                    188:
1.1       nicm      189: int
1.166     nicm      190: tty_open(struct tty *tty, char **cause)
1.1       nicm      191: {
1.220     nicm      192:        tty->term = tty_term_find(tty->term_name, tty->fd, cause);
1.21      nicm      193:        if (tty->term == NULL) {
                    194:                tty_close(tty);
                    195:                return (-1);
                    196:        }
                    197:        tty->flags |= TTY_OPENED;
1.1       nicm      198:
1.246     nicm      199:        tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE);
1.244     nicm      200:
                    201:        event_set(&tty->event_in, tty->fd, EV_PERSIST|EV_READ,
                    202:            tty_read_callback, tty);
                    203:        tty->in = evbuffer_new();
1.1       nicm      204:
1.244     nicm      205:        event_set(&tty->event_out, tty->fd, EV_WRITE, tty_write_callback, tty);
                    206:        tty->out = evbuffer_new();
1.1       nicm      207:
                    208:        tty_start_tty(tty);
                    209:
1.148     nicm      210:        tty_keys_build(tty);
1.1       nicm      211:
                    212:        return (0);
                    213: }
                    214:
1.244     nicm      215: void
                    216: tty_start_tty(struct tty *tty)
1.1       nicm      217: {
1.128     nicm      218:        struct termios  tio;
1.33      nicm      219:
1.244     nicm      220:        if (tty->fd != -1 && tcgetattr(tty->fd, &tty->tio) == 0) {
                    221:                setblocking(tty->fd, 0);
                    222:                event_add(&tty->event_in, NULL);
                    223:
                    224:                memcpy(&tio, &tty->tio, sizeof tio);
                    225:                tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
                    226:                tio.c_iflag |= IGNBRK;
                    227:                tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
                    228:                tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
                    229:                    ECHOPRT|ECHOKE|ISIG);
                    230:                tio.c_cc[VMIN] = 1;
                    231:                tio.c_cc[VTIME] = 0;
                    232:                if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
                    233:                        tcflush(tty->fd, TCIOFLUSH);
                    234:        }
1.1       nicm      235:
1.10      nicm      236:        tty_putcode(tty, TTYC_SMCUP);
1.1       nicm      237:
1.76      nicm      238:        tty_putcode(tty, TTYC_RMKX);
1.90      nicm      239:        if (tty_use_acs(tty))
                    240:                tty_putcode(tty, TTYC_ENACS);
1.1       nicm      241:        tty_putcode(tty, TTYC_CLEAR);
                    242:
                    243:        tty_putcode(tty, TTYC_CNORM);
                    244:        if (tty_term_has(tty->term, TTYC_KMOUS))
1.179     nicm      245:                tty_puts(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.121     nicm      246:
1.189     nicm      247:        if (tty_term_flag(tty->term, TTYC_XT)) {
1.191     nicm      248:                if (options_get_number(global_options, "focus-events")) {
1.162     nicm      249:                        tty->flags |= TTY_FOCUS;
                    250:                        tty_puts(tty, "\033[?1004h");
                    251:                }
1.212     nicm      252:                tty_puts(tty, "\033[c");
1.162     nicm      253:        }
1.1       nicm      254:
1.20      nicm      255:        tty->flags |= TTY_STARTED;
1.242     nicm      256:        tty_invalidate(tty);
1.107     nicm      257:
                    258:        tty_force_cursor_colour(tty, "");
1.177     nicm      259:
                    260:        tty->mouse_drag_flag = 0;
                    261:        tty->mouse_drag_update = NULL;
                    262:        tty->mouse_drag_release = NULL;
1.128     nicm      263: }
                    264:
                    265: void
1.1       nicm      266: tty_stop_tty(struct tty *tty)
                    267: {
                    268:        struct winsize  ws;
                    269:
1.20      nicm      270:        if (!(tty->flags & TTY_STARTED))
                    271:                return;
                    272:        tty->flags &= ~TTY_STARTED;
                    273:
1.244     nicm      274:        event_del(&tty->event_in);
                    275:        event_del(&tty->event_out);
1.66      nicm      276:
1.1       nicm      277:        /*
                    278:         * Be flexible about error handling and try not kill the server just
                    279:         * because the fd is invalid. Things like ssh -t can easily leave us
                    280:         * with a dead tty.
                    281:         */
                    282:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
                    283:                return;
                    284:        if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
                    285:                return;
                    286:
                    287:        tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90      nicm      288:        if (tty_use_acs(tty))
                    289:                tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1       nicm      290:        tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
                    291:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
                    292:        tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160     nicm      293:        if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
                    294:                if (tty_term_has(tty->term, TTYC_SE))
                    295:                        tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109     nicm      296:                else
1.160     nicm      297:                        tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108     nicm      298:        }
1.173     nicm      299:        if (tty->mode & MODE_BRACKETPASTE)
                    300:                tty_raw(tty, "\033[?2004l");
1.107     nicm      301:        tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1       nicm      302:
                    303:        tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
                    304:        if (tty_term_has(tty->term, TTYC_KMOUS))
1.179     nicm      305:                tty_raw(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.151     nicm      306:
1.189     nicm      307:        if (tty_term_flag(tty->term, TTYC_XT)) {
1.162     nicm      308:                if (tty->flags & TTY_FOCUS) {
                    309:                        tty->flags &= ~TTY_FOCUS;
1.172     nicm      310:                        tty_raw(tty, "\033[?1004l");
1.162     nicm      311:                }
                    312:        }
1.10      nicm      313:
1.212     nicm      314:        if (tty_use_margin(tty))
                    315:                tty_raw(tty, "\033[?69l"); /* DECLRMM */
1.10      nicm      316:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150     nicm      317:
                    318:        setblocking(tty->fd, 1);
1.1       nicm      319: }
                    320:
                    321: void
1.20      nicm      322: tty_close(struct tty *tty)
1.1       nicm      323: {
1.123     nicm      324:        if (event_initialized(&tty->key_timer))
                    325:                evtimer_del(&tty->key_timer);
1.20      nicm      326:        tty_stop_tty(tty);
1.1       nicm      327:
1.21      nicm      328:        if (tty->flags & TTY_OPENED) {
1.244     nicm      329:                evbuffer_free(tty->in);
                    330:                event_del(&tty->event_in);
                    331:                evbuffer_free(tty->out);
                    332:                event_del(&tty->event_out);
1.66      nicm      333:
1.21      nicm      334:                tty_term_free(tty->term);
                    335:                tty_keys_free(tty);
                    336:
                    337:                tty->flags &= ~TTY_OPENED;
                    338:        }
1.1       nicm      339:
1.21      nicm      340:        if (tty->fd != -1) {
                    341:                close(tty->fd);
                    342:                tty->fd = -1;
                    343:        }
1.1       nicm      344: }
                    345:
                    346: void
1.20      nicm      347: tty_free(struct tty *tty)
1.1       nicm      348: {
1.20      nicm      349:        tty_close(tty);
1.1       nicm      350:
1.138     nicm      351:        free(tty->ccolour);
1.190     nicm      352:        free(tty->path);
1.220     nicm      353:        free(tty->term_name);
1.1       nicm      354: }
                    355:
                    356: void
1.212     nicm      357: tty_set_type(struct tty *tty, int type)
                    358: {
                    359:        tty->term_type = type;
                    360:
                    361:        if (tty_use_margin(tty))
                    362:                tty_puts(tty, "\033[?69h"); /* DECLRMM */
                    363: }
                    364:
                    365: void
1.1       nicm      366: tty_raw(struct tty *tty, const char *s)
                    367: {
1.150     nicm      368:        ssize_t n, slen;
                    369:        u_int   i;
                    370:
                    371:        slen = strlen(s);
                    372:        for (i = 0; i < 5; i++) {
                    373:                n = write(tty->fd, s, slen);
                    374:                if (n >= 0) {
                    375:                        s += n;
                    376:                        slen -= n;
                    377:                        if (slen == 0)
                    378:                                break;
                    379:                } else if (n == -1 && errno != EAGAIN)
                    380:                        break;
                    381:                usleep(100);
                    382:        }
1.1       nicm      383: }
                    384:
                    385: void
                    386: tty_putcode(struct tty *tty, enum tty_code_code code)
                    387: {
                    388:        tty_puts(tty, tty_term_string(tty->term, code));
                    389: }
                    390:
                    391: void
                    392: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
                    393: {
                    394:        if (a < 0)
                    395:                return;
                    396:        tty_puts(tty, tty_term_string1(tty->term, code, a));
                    397: }
                    398:
                    399: void
                    400: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
                    401: {
                    402:        if (a < 0 || b < 0)
                    403:                return;
                    404:        tty_puts(tty, tty_term_string2(tty->term, code, a, b));
                    405: }
                    406:
                    407: void
1.107     nicm      408: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
                    409: {
                    410:        if (a != NULL)
                    411:                tty_puts(tty, tty_term_ptr1(tty->term, code, a));
                    412: }
                    413:
                    414: void
1.167     nicm      415: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
                    416:     const void *b)
1.106     nicm      417: {
                    418:        if (a != NULL && b != NULL)
                    419:                tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
                    420: }
                    421:
1.243     nicm      422: static void
                    423: tty_add(struct tty *tty, const char *buf, size_t len)
                    424: {
1.244     nicm      425:        evbuffer_add(tty->out, buf, len);
                    426:        log_debug("%s: %.*s", tty->path, (int)len, (const char *)buf);
1.243     nicm      427:
                    428:        if (tty_log_fd != -1)
                    429:                write(tty_log_fd, buf, len);
1.244     nicm      430:        if (tty->flags & TTY_STARTED)
                    431:                event_add(&tty->event_out, NULL);
1.243     nicm      432: }
                    433:
1.106     nicm      434: void
1.1       nicm      435: tty_puts(struct tty *tty, const char *s)
                    436: {
1.243     nicm      437:        if (*s != '\0')
                    438:                tty_add(tty, s, strlen(s));
1.1       nicm      439: }
                    440:
                    441: void
                    442: tty_putc(struct tty *tty, u_char ch)
                    443: {
1.90      nicm      444:        const char      *acs;
1.1       nicm      445:
1.90      nicm      446:        if (tty->cell.attr & GRID_ATTR_CHARSET) {
                    447:                acs = tty_acs_get(tty, ch);
1.243     nicm      448:                if (acs != NULL)
                    449:                        tty_add(tty, acs, strlen(acs));
                    450:                else
                    451:                        tty_add(tty, &ch, 1);
                    452:        } else
                    453:                tty_add(tty, &ch, 1);
1.1       nicm      454:
                    455:        if (ch >= 0x20 && ch != 0x7f) {
1.211     nicm      456:                if (tty->cx >= tty->sx) {
1.46      nicm      457:                        tty->cx = 1;
                    458:                        if (tty->cy != tty->rlower)
                    459:                                tty->cy++;
1.211     nicm      460:
                    461:                        /*
                    462:                         * On !xenl terminals, force the cursor position to
                    463:                         * where we think it should be after a line wrap - this
                    464:                         * means it works on sensible terminals as well.
                    465:                         */
                    466:                        if (tty->term->flags & TERM_EARLYWRAP)
                    467:                                tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1       nicm      468:                } else
                    469:                        tty->cx++;
                    470:        }
                    471: }
                    472:
                    473: void
1.147     nicm      474: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5       nicm      475: {
1.243     nicm      476:        tty_add(tty, buf, len);
1.147     nicm      477:        tty->cx += width;
1.5       nicm      478: }
                    479:
1.207     nicm      480: static void
1.180     nicm      481: tty_set_italics(struct tty *tty)
                    482: {
                    483:        const char      *s;
                    484:
                    485:        if (tty_term_has(tty->term, TTYC_SITM)) {
1.191     nicm      486:                s = options_get_string(global_options, "default-terminal");
1.180     nicm      487:                if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
                    488:                        tty_putcode(tty, TTYC_SITM);
                    489:                        return;
                    490:                }
                    491:        }
                    492:        tty_putcode(tty, TTYC_SMSO);
                    493: }
                    494:
                    495: void
1.1       nicm      496: tty_set_title(struct tty *tty, const char *title)
                    497: {
1.105     nicm      498:        if (!tty_term_has(tty->term, TTYC_TSL) ||
                    499:            !tty_term_has(tty->term, TTYC_FSL))
1.1       nicm      500:                return;
                    501:
1.105     nicm      502:        tty_putcode(tty, TTYC_TSL);
1.1       nicm      503:        tty_puts(tty, title);
1.105     nicm      504:        tty_putcode(tty, TTYC_FSL);
1.1       nicm      505: }
                    506:
1.208     nicm      507: static void
1.107     nicm      508: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
                    509: {
                    510:        if (*ccolour == '\0')
                    511:                tty_putcode(tty, TTYC_CR);
                    512:        else
1.160     nicm      513:                tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138     nicm      514:        free(tty->ccolour);
1.107     nicm      515:        tty->ccolour = xstrdup(ccolour);
                    516: }
                    517:
                    518: void
                    519: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1       nicm      520: {
                    521:        int     changed;
                    522:
1.197     nicm      523:        if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107     nicm      524:                tty_force_cursor_colour(tty, s->ccolour);
                    525:
1.1       nicm      526:        if (tty->flags & TTY_NOCURSOR)
                    527:                mode &= ~MODE_CURSOR;
                    528:
                    529:        changed = mode ^ tty->mode;
1.183     nicm      530:        if (changed & MODE_BLINKING) {
                    531:                if (tty_term_has(tty->term, TTYC_CVVIS))
                    532:                        tty_putcode(tty, TTYC_CVVIS);
                    533:                else
                    534:                        tty_putcode(tty, TTYC_CNORM);
                    535:                changed |= MODE_CURSOR;
                    536:        }
                    537:        if (changed & MODE_CURSOR) {
                    538:                if (mode & MODE_CURSOR)
                    539:                        tty_putcode(tty, TTYC_CNORM);
                    540:                else
1.1       nicm      541:                        tty_putcode(tty, TTYC_CIVIS);
1.108     nicm      542:        }
1.181     nicm      543:        if (s != NULL && tty->cstyle != s->cstyle) {
1.160     nicm      544:                if (tty_term_has(tty->term, TTYC_SS)) {
1.108     nicm      545:                        if (s->cstyle == 0 &&
1.160     nicm      546:                            tty_term_has(tty->term, TTYC_SE))
                    547:                                tty_putcode(tty, TTYC_SE);
1.108     nicm      548:                        else
1.160     nicm      549:                                tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108     nicm      550:                }
                    551:                tty->cstyle = s->cstyle;
1.1       nicm      552:        }
1.195     nicm      553:        if (changed & ALL_MOUSE_MODES) {
1.94      nicm      554:                if (mode & ALL_MOUSE_MODES) {
1.153     nicm      555:                        /*
                    556:                         * Enable the SGR (1006) extension unconditionally, as
1.221     nicm      557:                         * it is safe from misinterpretation.
1.153     nicm      558:                         */
                    559:                        tty_puts(tty, "\033[?1006h");
1.225     nicm      560:                        if (mode & MODE_MOUSE_ALL)
                    561:                                tty_puts(tty, "\033[?1003h");
                    562:                        else if (mode & MODE_MOUSE_BUTTON)
1.94      nicm      563:                                tty_puts(tty, "\033[?1002h");
1.98      nicm      564:                        else if (mode & MODE_MOUSE_STANDARD)
                    565:                                tty_puts(tty, "\033[?1000h");
1.86      nicm      566:                } else {
1.225     nicm      567:                        if (tty->mode & MODE_MOUSE_ALL)
                    568:                                tty_puts(tty, "\033[?1003l");
                    569:                        else if (tty->mode & MODE_MOUSE_BUTTON)
1.94      nicm      570:                                tty_puts(tty, "\033[?1002l");
1.98      nicm      571:                        else if (tty->mode & MODE_MOUSE_STANDARD)
                    572:                                tty_puts(tty, "\033[?1000l");
1.153     nicm      573:                        tty_puts(tty, "\033[?1006l");
1.86      nicm      574:                }
1.76      nicm      575:        }
                    576:        if (changed & MODE_KKEYPAD) {
                    577:                if (mode & MODE_KKEYPAD)
                    578:                        tty_putcode(tty, TTYC_SMKX);
                    579:                else
                    580:                        tty_putcode(tty, TTYC_RMKX);
1.115     nicm      581:        }
                    582:        if (changed & MODE_BRACKETPASTE) {
                    583:                if (mode & MODE_BRACKETPASTE)
                    584:                        tty_puts(tty, "\033[?2004h");
                    585:                else
                    586:                        tty_puts(tty, "\033[?2004l");
1.1       nicm      587:        }
                    588:        tty->mode = mode;
                    589: }
                    590:
1.207     nicm      591: static void
1.168     nicm      592: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
                    593:     enum tty_code_code code1, u_int n)
1.1       nicm      594: {
                    595:        if (tty_term_has(tty->term, code))
                    596:                tty_putcode1(tty, code, n);
                    597:        else {
                    598:                while (n-- > 0)
                    599:                        tty_putcode(tty, code1);
                    600:        }
                    601: }
                    602:
1.207     nicm      603: static void
1.133     nicm      604: tty_repeat_space(struct tty *tty, u_int n)
                    605: {
                    606:        while (n-- > 0)
                    607:                tty_putc(tty, ' ');
                    608: }
                    609:
1.1       nicm      610: /*
1.119     nicm      611:  * Is the region large enough to be worth redrawing once later rather than
                    612:  * probably several times now? Currently yes if it is more than 50% of the
                    613:  * pane.
                    614:  */
1.207     nicm      615: static int
1.194     nicm      616: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119     nicm      617: {
                    618:        struct window_pane      *wp = ctx->wp;
                    619:
                    620:        return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
                    621: }
                    622:
                    623: /*
1.176     nicm      624:  * Return if BCE is needed but the terminal doesn't have it - it'll need to be
                    625:  * emulated.
                    626:  */
1.207     nicm      627: static int
1.210     nicm      628: tty_fake_bce(const struct tty *tty, const struct window_pane *wp, u_int bg)
1.176     nicm      629: {
                    630:        struct grid_cell        gc;
                    631:
1.210     nicm      632:        if (tty_term_flag(tty->term, TTYC_BCE))
                    633:                return (0);
                    634:
1.176     nicm      635:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.203     nicm      636:        if (wp != NULL)
                    637:                tty_default_colours(&gc, wp);
1.176     nicm      638:
1.210     nicm      639:        if (bg != 8 || gc.bg != 8)
                    640:                return (1);
                    641:        return (0);
1.176     nicm      642: }
                    643:
                    644: /*
1.1       nicm      645:  * Redraw scroll region using data from screen (already updated). Used when
                    646:  * CSR not supported, or window is a pane that doesn't take up the full
                    647:  * width of the terminal.
                    648:  */
1.207     nicm      649: static void
1.24      nicm      650: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      651: {
1.14      nicm      652:        struct window_pane      *wp = ctx->wp;
                    653:        struct screen           *s = wp->screen;
1.204     nicm      654:        u_int                    i;
1.1       nicm      655:
                    656:        /*
1.119     nicm      657:         * If region is large, schedule a window redraw. In most cases this is
                    658:         * likely to be followed by some more scrolling.
1.1       nicm      659:         */
1.119     nicm      660:        if (tty_large_region(tty, ctx)) {
1.1       nicm      661:                wp->flags |= PANE_REDRAW;
                    662:                return;
                    663:        }
                    664:
1.14      nicm      665:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    666:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.176     nicm      667:                        tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1       nicm      668:        } else {
1.14      nicm      669:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.176     nicm      670:                        tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1       nicm      671:        }
                    672: }
                    673:
                    674: void
1.176     nicm      675: tty_draw_pane(struct tty *tty, const struct window_pane *wp, u_int py, u_int ox,
                    676:     u_int oy)
                    677: {
                    678:        tty_draw_line(tty, wp, wp->screen, py, ox, oy);
                    679: }
                    680:
                    681: void
                    682: tty_draw_line(struct tty *tty, const struct window_pane *wp,
                    683:     struct screen *s, u_int py, u_int ox, u_int oy)
1.1       nicm      684: {
1.250   ! nicm      685:        struct grid_cell         gc, last;
        !           686:        u_int                    i, j, sx, width;
        !           687:        int                      flags = (tty->flags & TTY_NOCURSOR);
        !           688:        char                     buf[512];
        !           689:        size_t                   len;
1.1       nicm      690:
1.181     nicm      691:        tty->flags |= TTY_NOCURSOR;
                    692:        tty_update_mode(tty, tty->mode, s);
1.35      nicm      693:
1.214     nicm      694:        tty_region_off(tty);
                    695:        tty_margin_off(tty);
                    696:
1.1       nicm      697:        sx = screen_size_x(s);
1.250   ! nicm      698:        if (sx > s->grid->linedata[s->grid->hsize + py].cellused)
        !           699:                sx = s->grid->linedata[s->grid->hsize + py].cellused;
1.1       nicm      700:        if (sx > tty->sx)
                    701:                sx = tty->sx;
                    702:
1.250   ! nicm      703:        tty_cursor(tty, ox, oy + py);
        !           704:
        !           705:        memcpy(&last, &grid_default_cell, sizeof last);
        !           706:        len = 0;
        !           707:        width = 0;
1.46      nicm      708:
1.1       nicm      709:        for (i = 0; i < sx; i++) {
1.193     nicm      710:                grid_view_get_cell(s->grid, i, py, &gc);
1.250   ! nicm      711:                if (len != 0 &&
        !           712:                    (gc.attr & GRID_ATTR_CHARSET ||
        !           713:                    gc.flags != last.flags ||
        !           714:                    gc.attr != last.attr ||
        !           715:                    gc.fg != last.fg ||
        !           716:                    gc.bg != last.bg ||
        !           717:                    (sizeof buf) - len < gc.data.size)) {
        !           718:                        tty_attributes(tty, &last, wp);
        !           719:                        tty_putn(tty, buf, len, width);
        !           720:
        !           721:                        len = 0;
        !           722:                        width = 0;
        !           723:                }
        !           724:
        !           725:                if (gc.flags & GRID_FLAG_SELECTED)
        !           726:                        screen_select_cell(s, &last, &gc);
        !           727:                else
        !           728:                        memcpy(&last, &gc, sizeof last);
        !           729:                if (gc.attr & GRID_ATTR_CHARSET) {
        !           730:                        tty_attributes(tty, &last, wp);
        !           731:                        for (j = 0; j < gc.data.size; j++)
        !           732:                                tty_putc(tty, gc.data.data[j]);
        !           733:                } else {
        !           734:                        memcpy(buf + len, gc.data.data, gc.data.size);
        !           735:                        len += gc.data.size;
        !           736:                        width += gc.data.width;
        !           737:                }
        !           738:        }
        !           739:        if (len != 0) {
        !           740:                tty_attributes(tty, &last, wp);
        !           741:                tty_putn(tty, buf, len, width);
1.1       nicm      742:        }
                    743:
1.181     nicm      744:        if (sx < tty->sx) {
1.210     nicm      745:                tty_default_attributes(tty, wp, 8);
1.181     nicm      746:
                    747:                tty_cursor(tty, ox + sx, oy + py);
                    748:                if (sx != screen_size_x(s) &&
                    749:                    ox + screen_size_x(s) >= tty->sx &&
                    750:                    tty_term_has(tty->term, TTYC_EL) &&
1.210     nicm      751:                    !tty_fake_bce(tty, wp, 8))
1.181     nicm      752:                        tty_putcode(tty, TTYC_EL);
                    753:                else
                    754:                        tty_repeat_space(tty, screen_size_x(s) - sx);
1.35      nicm      755:        }
1.1       nicm      756:
1.181     nicm      757:        tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107     nicm      758:        tty_update_mode(tty, tty->mode, s);
1.15      nicm      759: }
                    760:
1.201     nicm      761: static int
1.182     nicm      762: tty_client_ready(struct client *c, struct window_pane *wp)
                    763: {
                    764:        if (c->session == NULL || c->tty.term == NULL)
                    765:                return (0);
1.244     nicm      766:        if (c->flags & (CLIENT_REDRAW|CLIENT_SUSPENDED))
1.182     nicm      767:                return (0);
                    768:        if (c->tty.flags & TTY_FREEZE)
                    769:                return (0);
                    770:        if (c->session->curw->window != wp->window)
                    771:                return (0);
                    772:        return (1);
                    773: }
                    774:
1.15      nicm      775: void
1.182     nicm      776: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
                    777:     struct tty_ctx *ctx)
1.15      nicm      778: {
                    779:        struct window_pane      *wp = ctx->wp;
                    780:        struct client           *c;
                    781:
1.75      nicm      782:        /* wp can be NULL if updating the screen but not the terminal. */
1.15      nicm      783:        if (wp == NULL)
                    784:                return;
                    785:
                    786:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    787:                return;
1.129     nicm      788:        if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15      nicm      789:                return;
                    790:
1.178     nicm      791:        TAILQ_FOREACH(c, &clients, entry) {
1.182     nicm      792:                if (!tty_client_ready(c, wp))
1.15      nicm      793:                        continue;
                    794:
1.139     nicm      795:                ctx->xoff = wp->xoff;
                    796:                ctx->yoff = wp->yoff;
                    797:                if (status_at_line(c) == 0)
                    798:                        ctx->yoff++;
1.113     nicm      799:
1.139     nicm      800:                cmdfn(&c->tty, ctx);
1.1       nicm      801:        }
                    802: }
                    803:
                    804: void
1.24      nicm      805: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      806: {
1.77      nicm      807:        struct window_pane      *wp = ctx->wp;
1.1       nicm      808:
1.210     nicm      809:        if (!tty_pane_full_width(tty, ctx) ||
                    810:            tty_fake_bce(tty, wp, ctx->bg) ||
                    811:            (!tty_term_has(tty->term, TTYC_ICH) &&
                    812:            !tty_term_has(tty->term, TTYC_ICH1))) {
1.176     nicm      813:                tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      814:                return;
                    815:        }
                    816:
1.210     nicm      817:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      818:
1.77      nicm      819:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      820:
1.206     nicm      821:        tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1       nicm      822: }
                    823:
                    824: void
1.24      nicm      825: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      826: {
1.77      nicm      827:        struct window_pane      *wp = ctx->wp;
1.1       nicm      828:
1.210     nicm      829:        if (!tty_pane_full_width(tty, ctx) ||
                    830:            tty_fake_bce(tty, wp, ctx->bg) ||
1.26      nicm      831:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    832:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.176     nicm      833:                tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      834:                return;
                    835:        }
                    836:
1.210     nicm      837:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      838:
1.77      nicm      839:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      840:
1.206     nicm      841:        tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146     nicm      842: }
                    843:
                    844: void
                    845: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
                    846: {
                    847:        u_int   i;
                    848:
1.176     nicm      849:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.146     nicm      850:
                    851:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    852:
1.210     nicm      853:        if (tty_term_has(tty->term, TTYC_ECH) &&
                    854:            !tty_fake_bce(tty, ctx->wp, ctx->bg))
1.146     nicm      855:                tty_putcode1(tty, TTYC_ECH, ctx->num);
                    856:        else {
                    857:                for (i = 0; i < ctx->num; i++)
                    858:                        tty_putc(tty, ' ');
                    859:        }
1.1       nicm      860: }
                    861:
                    862: void
1.24      nicm      863: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      864: {
1.210     nicm      865:        if (!tty_pane_full_width(tty, ctx) ||
                    866:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77      nicm      867:            !tty_term_has(tty->term, TTYC_CSR) ||
1.72      nicm      868:            !tty_term_has(tty->term, TTYC_IL1)) {
1.14      nicm      869:                tty_redraw_region(tty, ctx);
1.1       nicm      870:                return;
                    871:        }
                    872:
1.210     nicm      873:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm      874:
1.77      nicm      875:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm      876:        tty_margin_off(tty);
1.77      nicm      877:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      878:
1.12      nicm      879:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      880: }
                    881:
                    882: void
1.24      nicm      883: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      884: {
1.210     nicm      885:        if (!tty_pane_full_width(tty, ctx) ||
                    886:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72      nicm      887:            !tty_term_has(tty->term, TTYC_CSR) ||
                    888:            !tty_term_has(tty->term, TTYC_DL1)) {
1.14      nicm      889:                tty_redraw_region(tty, ctx);
1.1       nicm      890:                return;
                    891:        }
                    892:
1.210     nicm      893:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm      894:
1.77      nicm      895:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm      896:        tty_margin_off(tty);
1.77      nicm      897:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      898:
1.12      nicm      899:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      900: }
                    901:
                    902: void
1.24      nicm      903: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      904: {
1.77      nicm      905:        struct window_pane      *wp = ctx->wp;
1.12      nicm      906:        struct screen           *s = wp->screen;
1.229     nicm      907:        u_int                    sx = screen_size_x(s);
1.1       nicm      908:
1.210     nicm      909:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      910:
1.77      nicm      911:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41      nicm      912:
1.210     nicm      913:        if (tty_pane_full_width(tty, ctx) &&
                    914:            !tty_fake_bce(tty, wp, ctx->bg) &&
1.176     nicm      915:            tty_term_has(tty->term, TTYC_EL))
1.1       nicm      916:                tty_putcode(tty, TTYC_EL);
1.133     nicm      917:        else
1.229     nicm      918:                tty_repeat_space(tty, sx);
1.1       nicm      919: }
                    920:
                    921: void
1.24      nicm      922: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      923: {
1.77      nicm      924:        struct window_pane      *wp = ctx->wp;
1.12      nicm      925:        struct screen           *s = wp->screen;
1.229     nicm      926:        u_int                    sx = screen_size_x(s);
1.1       nicm      927:
1.210     nicm      928:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      929:
1.41      nicm      930:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    931:
1.176     nicm      932:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm      933:            tty_term_has(tty->term, TTYC_EL) &&
                    934:            !tty_fake_bce(tty, wp, ctx->bg))
1.1       nicm      935:                tty_putcode(tty, TTYC_EL);
1.133     nicm      936:        else
1.229     nicm      937:                tty_repeat_space(tty, sx - ctx->ocx);
1.1       nicm      938: }
                    939:
                    940: void
1.24      nicm      941: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      942: {
1.210     nicm      943:        struct window_pane      *wp = ctx->wp;
                    944:
                    945:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      946:
1.210     nicm      947:        if (ctx->xoff == 0 &&
                    948:            tty_term_has(tty->term, TTYC_EL1) &&
1.238     nicm      949:            !tty_fake_bce(tty, ctx->wp, ctx->bg)) {
                    950:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      951:                tty_putcode(tty, TTYC_EL1);
1.238     nicm      952:        } else {
                    953:                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133     nicm      954:                tty_repeat_space(tty, ctx->ocx + 1);
1.238     nicm      955:        }
1.1       nicm      956: }
                    957:
                    958: void
1.24      nicm      959: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      960: {
1.56      nicm      961:        if (ctx->ocy != ctx->orupper)
                    962:                return;
                    963:
1.210     nicm      964:        if (!tty_pane_full_width(tty, ctx) ||
                    965:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.70      nicm      966:            !tty_term_has(tty->term, TTYC_CSR) ||
                    967:            !tty_term_has(tty->term, TTYC_RI)) {
1.14      nicm      968:                tty_redraw_region(tty, ctx);
1.1       nicm      969:                return;
                    970:        }
                    971:
1.176     nicm      972:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.77      nicm      973:
1.56      nicm      974:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm      975:        tty_margin_off(tty);
1.56      nicm      976:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm      977:
1.56      nicm      978:        tty_putcode(tty, TTYC_RI);
1.1       nicm      979: }
                    980:
                    981: void
1.24      nicm      982: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      983: {
1.77      nicm      984:        struct window_pane      *wp = ctx->wp;
1.1       nicm      985:
1.56      nicm      986:        if (ctx->ocy != ctx->orlower)
                    987:                return;
                    988:
1.212     nicm      989:        if ((!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.210     nicm      990:            tty_fake_bce(tty, wp, ctx->bg) ||
1.1       nicm      991:            !tty_term_has(tty->term, TTYC_CSR)) {
1.236     nicm      992:                tty_redraw_region(tty, ctx);
1.1       nicm      993:                return;
                    994:        }
1.52      nicm      995:
1.176     nicm      996:        tty_attributes(tty, &grid_default_cell, wp);
1.77      nicm      997:
1.56      nicm      998:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212     nicm      999:        tty_margin_pane(tty, ctx);
                   1000:
                   1001:        /*
                   1002:         * If we want to wrap a pane, the cursor needs to be exactly on the
                   1003:         * right of the region. But if the pane isn't on the right, it may be
                   1004:         * off the edge - if so, move the cursor back to the right.
                   1005:         */
                   1006:        if (ctx->xoff + ctx->ocx > tty->rright)
1.216     nicm     1007:                tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1.212     nicm     1008:        else
                   1009:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm     1010:
1.56      nicm     1011:        tty_putc(tty, '\n');
1.240     nicm     1012: }
                   1013:
                   1014: void
                   1015: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
                   1016: {
                   1017:        struct window_pane      *wp = ctx->wp;
                   1018:        u_int                    i;
                   1019:
                   1020:        if ((!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
                   1021:            tty_fake_bce(tty, wp, ctx->bg) ||
                   1022:            !tty_term_has(tty->term, TTYC_CSR)) {
                   1023:                tty_redraw_region(tty, ctx);
                   1024:                return;
                   1025:        }
                   1026:
                   1027:        tty_attributes(tty, &grid_default_cell, wp);
                   1028:
                   1029:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                   1030:        tty_margin_pane(tty, ctx);
                   1031:
                   1032:        if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.241     nicm     1033:                tty_cursor(tty, tty->rright, tty->rlower);
1.240     nicm     1034:                for (i = 0; i < ctx->num; i++)
                   1035:                        tty_putc(tty, '\n');
                   1036:        } else
                   1037:                tty_putcode1(tty, TTYC_INDN, ctx->num);
1.1       nicm     1038: }
                   1039:
                   1040: void
1.24      nicm     1041: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1042: {
1.77      nicm     1043:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1044:        struct screen           *s = wp->screen;
1.204     nicm     1045:        u_int                    i, j;
1.229     nicm     1046:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1047:
1.210     nicm     1048:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1049:
1.229     nicm     1050:        tty_region_pane(tty, ctx, 0, sy - 1);
1.214     nicm     1051:        tty_margin_off(tty);
1.39      nicm     1052:
1.176     nicm     1053:        if (tty_pane_full_width(tty, ctx) &&
1.248     nicm     1054:            ctx->yoff + wp->sy >= tty->sy - 1 &&
1.229     nicm     1055:            status_at_line(tty->client) <= 0 &&
                   1056:            tty_term_has(tty->term, TTYC_ED)) {
                   1057:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                   1058:                tty_putcode(tty, TTYC_ED);
                   1059:        } else if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1060:            tty_term_has(tty->term, TTYC_EL) &&
                   1061:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.229     nicm     1062:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1063:                tty_putcode(tty, TTYC_EL);
1.229     nicm     1064:                if (ctx->ocy != sy - 1) {
1.41      nicm     1065:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.229     nicm     1066:                        for (i = ctx->ocy + 1; i < sy; i++) {
1.1       nicm     1067:                                tty_putcode(tty, TTYC_EL);
1.229     nicm     1068:                                if (i == sy - 1)
1.1       nicm     1069:                                        continue;
                   1070:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1071:                                tty->cy++;
                   1072:                        }
                   1073:                }
                   1074:        } else {
1.229     nicm     1075:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                   1076:                tty_repeat_space(tty, sx - ctx->ocx);
                   1077:                for (j = ctx->ocy + 1; j < sy; j++) {
1.41      nicm     1078:                        tty_cursor_pane(tty, ctx, 0, j);
1.229     nicm     1079:                        tty_repeat_space(tty, sx);
1.1       nicm     1080:                }
                   1081:        }
                   1082: }
                   1083:
                   1084: void
1.24      nicm     1085: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1086: {
1.77      nicm     1087:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1088:        struct screen           *s = wp->screen;
1.204     nicm     1089:        u_int                    i, j;
1.229     nicm     1090:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1091:
1.227     nicm     1092:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1093:
1.229     nicm     1094:        tty_region_pane(tty, ctx, 0, sy - 1);
1.214     nicm     1095:        tty_margin_off(tty);
1.39      nicm     1096:
1.230     nicm     1097:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1098:            tty_term_has(tty->term, TTYC_EL) &&
                   1099:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.229     nicm     1100:                tty_cursor_pane(tty, ctx, 0, 0);
1.14      nicm     1101:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm     1102:                        tty_putcode(tty, TTYC_EL);
                   1103:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1104:                        tty->cy++;
                   1105:                }
                   1106:        } else {
1.229     nicm     1107:                tty_cursor_pane(tty, ctx, 0, 0);
1.14      nicm     1108:                for (j = 0; j < ctx->ocy; j++) {
1.41      nicm     1109:                        tty_cursor_pane(tty, ctx, 0, j);
1.229     nicm     1110:                        tty_repeat_space(tty, sx);
1.1       nicm     1111:                }
                   1112:        }
1.229     nicm     1113:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133     nicm     1114:        tty_repeat_space(tty, ctx->ocx + 1);
1.1       nicm     1115: }
                   1116:
                   1117: void
1.24      nicm     1118: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1119: {
1.77      nicm     1120:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1121:        struct screen           *s = wp->screen;
1.204     nicm     1122:        u_int                    i, j;
1.229     nicm     1123:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1124:
1.210     nicm     1125:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1126:
1.229     nicm     1127:        tty_region_pane(tty, ctx, 0, sy - 1);
1.214     nicm     1128:        tty_margin_off(tty);
1.39      nicm     1129:
1.176     nicm     1130:        if (tty_pane_full_width(tty, ctx) &&
1.229     nicm     1131:            status_at_line(tty->client) <= 0 &&
                   1132:            tty_term_has(tty->term, TTYC_ED)) {
                   1133:                tty_cursor_pane(tty, ctx, 0, 0);
                   1134:                tty_putcode(tty, TTYC_ED);
1.230     nicm     1135:        } else if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1136:            tty_term_has(tty->term, TTYC_EL) &&
                   1137:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.229     nicm     1138:                tty_cursor_pane(tty, ctx, 0, 0);
                   1139:                for (i = 0; i < sy; i++) {
1.1       nicm     1140:                        tty_putcode(tty, TTYC_EL);
1.229     nicm     1141:                        if (i != sy - 1) {
1.1       nicm     1142:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1143:                                tty->cy++;
                   1144:                        }
                   1145:                }
                   1146:        } else {
1.229     nicm     1147:                tty_cursor_pane(tty, ctx, 0, 0);
                   1148:                for (j = 0; j < sy; j++) {
1.41      nicm     1149:                        tty_cursor_pane(tty, ctx, 0, j);
1.229     nicm     1150:                        tty_repeat_space(tty, sx);
1.1       nicm     1151:                }
1.4       nicm     1152:        }
                   1153: }
                   1154:
                   1155: void
1.24      nicm     1156: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm     1157: {
1.12      nicm     1158:        struct window_pane      *wp = ctx->wp;
                   1159:        struct screen           *s = wp->screen;
                   1160:        u_int                    i, j;
1.4       nicm     1161:
1.176     nicm     1162:        tty_attributes(tty, &grid_default_cell, wp);
1.4       nicm     1163:
1.39      nicm     1164:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214     nicm     1165:        tty_margin_off(tty);
1.4       nicm     1166:
                   1167:        for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm     1168:                tty_cursor_pane(tty, ctx, 0, j);
1.4       nicm     1169:                for (i = 0; i < screen_size_x(s); i++)
                   1170:                        tty_putc(tty, 'E');
1.1       nicm     1171:        }
                   1172: }
                   1173:
                   1174: void
1.24      nicm     1175: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1176: {
1.222     nicm     1177:        if (ctx->xoff + ctx->ocx > tty->sx - 1 && ctx->ocy == ctx->orlower) {
                   1178:                if (tty_pane_full_width(tty, ctx))
                   1179:                        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                   1180:                else
                   1181:                        tty_margin_off(tty);
                   1182:        }
1.46      nicm     1183:
1.233     nicm     1184:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1185:
1.237     nicm     1186:        tty_cell(tty, ctx->cell, ctx->wp);
1.239     nicm     1187: }
                   1188:
                   1189: void
                   1190: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
                   1191: {
                   1192:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                   1193:
                   1194:        tty_attributes(tty, ctx->cell, ctx->wp);
                   1195:        tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106     nicm     1196: }
                   1197:
                   1198: void
                   1199: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
                   1200: {
                   1201:        char    *buf;
                   1202:        size_t   off;
                   1203:
                   1204:        if (!tty_term_has(tty->term, TTYC_MS))
                   1205:                return;
                   1206:
                   1207:        off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
                   1208:        buf = xmalloc(off);
                   1209:
                   1210:        b64_ntop(ctx->ptr, ctx->num, buf, off);
                   1211:        tty_putcode_ptr2(tty, TTYC_MS, "", buf);
                   1212:
1.138     nicm     1213:        free(buf);
1.100     nicm     1214: }
                   1215:
                   1216: void
                   1217: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
                   1218: {
                   1219:        u_int    i;
                   1220:        u_char  *str = ctx->ptr;
                   1221:
                   1222:        for (i = 0; i < ctx->num; i++)
                   1223:                tty_putc(tty, str[i]);
1.242     nicm     1224:        tty_invalidate(tty);
1.1       nicm     1225: }
                   1226:
1.207     nicm     1227: static void
1.176     nicm     1228: tty_cell(struct tty *tty, const struct grid_cell *gc,
                   1229:     const struct window_pane *wp)
1.1       nicm     1230: {
1.193     nicm     1231:        u_int   i;
1.1       nicm     1232:
                   1233:        /* Skip last character if terminal is stupid. */
1.211     nicm     1234:        if ((tty->term->flags & TERM_EARLYWRAP) &&
                   1235:            tty->cy == tty->sy - 1 &&
                   1236:            tty->cx == tty->sx - 1)
1.1       nicm     1237:                return;
                   1238:
                   1239:        /* If this is a padding character, do nothing. */
                   1240:        if (gc->flags & GRID_FLAG_PADDING)
                   1241:                return;
                   1242:
                   1243:        /* Set the attributes. */
1.176     nicm     1244:        tty_attributes(tty, gc, wp);
1.1       nicm     1245:
1.147     nicm     1246:        /* Get the cell and if ASCII write with putc to do ACS translation. */
1.193     nicm     1247:        if (gc->data.size == 1) {
                   1248:                if (*gc->data.data < 0x20 || *gc->data.data == 0x7f)
1.1       nicm     1249:                        return;
1.193     nicm     1250:                tty_putc(tty, *gc->data.data);
1.1       nicm     1251:                return;
                   1252:        }
                   1253:
1.147     nicm     1254:        /* If not UTF-8, write _. */
1.1       nicm     1255:        if (!(tty->flags & TTY_UTF8)) {
1.193     nicm     1256:                for (i = 0; i < gc->data.width; i++)
1.1       nicm     1257:                        tty_putc(tty, '_');
                   1258:                return;
                   1259:        }
                   1260:
1.147     nicm     1261:        /* Write the data. */
1.193     nicm     1262:        tty_putn(tty, gc->data.data, gc->data.size, gc->data.width);
1.1       nicm     1263: }
                   1264:
                   1265: void
                   1266: tty_reset(struct tty *tty)
                   1267: {
                   1268:        struct grid_cell        *gc = &tty->cell;
                   1269:
1.228     nicm     1270:        if (!grid_cells_equal(gc, &grid_default_cell)) {
                   1271:                if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
                   1272:                        tty_putcode(tty, TTYC_RMACS);
                   1273:                tty_putcode(tty, TTYC_SGR0);
                   1274:                memcpy(gc, &grid_default_cell, sizeof *gc);
                   1275:        }
1.1       nicm     1276:
1.228     nicm     1277:        memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
                   1278:        tty->last_wp = -1;
1.242     nicm     1279: }
                   1280:
                   1281: static void
                   1282: tty_invalidate(struct tty *tty)
                   1283: {
                   1284:        memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
                   1285:
                   1286:        memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
                   1287:        tty->last_wp = -1;
                   1288:
                   1289:        tty->cx = tty->cy = UINT_MAX;
                   1290:
                   1291:        tty->rupper = tty->rleft = UINT_MAX;
                   1292:        tty->rlower = tty->rright = UINT_MAX;
                   1293:
                   1294:        if (tty->flags & TTY_STARTED) {
                   1295:                tty_putcode(tty, TTYC_SGR0);
                   1296:
                   1297:                tty->mode = ALL_MODES;
                   1298:                tty_update_mode(tty, MODE_CURSOR, NULL);
                   1299:
                   1300:                tty_cursor(tty, 0, 0);
                   1301:                tty_region_off(tty);
                   1302:                tty_margin_off(tty);
                   1303:        } else
                   1304:                tty->mode = MODE_CURSOR;
1.1       nicm     1305: }
                   1306:
1.214     nicm     1307: /* Turn off margin. */
                   1308: void
                   1309: tty_region_off(struct tty *tty)
                   1310: {
                   1311:        tty_region(tty, 0, tty->sy - 1);
                   1312: }
                   1313:
1.40      nicm     1314: /* Set region inside pane. */
1.208     nicm     1315: static void
1.196     nicm     1316: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
                   1317:     u_int rlower)
1.1       nicm     1318: {
1.113     nicm     1319:        tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39      nicm     1320: }
                   1321:
1.40      nicm     1322: /* Set region at absolute position. */
1.214     nicm     1323: static void
1.40      nicm     1324: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm     1325: {
                   1326:        if (tty->rlower == rlower && tty->rupper == rupper)
                   1327:                return;
1.1       nicm     1328:        if (!tty_term_has(tty->term, TTYC_CSR))
                   1329:                return;
1.39      nicm     1330:
                   1331:        tty->rupper = rupper;
                   1332:        tty->rlower = rlower;
1.55      nicm     1333:
                   1334:        /*
                   1335:         * Some terminals (such as PuTTY) do not correctly reset the cursor to
                   1336:         * 0,0 if it is beyond the last column (they do not reset their wrap
                   1337:         * flag so further output causes a line feed). As a workaround, do an
                   1338:         * explicit move to 0 first.
                   1339:         */
                   1340:        if (tty->cx >= tty->sx)
                   1341:                tty_cursor(tty, 0, tty->cy);
1.42      nicm     1342:
1.39      nicm     1343:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249     nicm     1344:        tty->cx = tty->cy = UINT_MAX;
1.212     nicm     1345: }
                   1346:
1.214     nicm     1347: /* Turn off margin. */
                   1348: void
                   1349: tty_margin_off(struct tty *tty)
                   1350: {
                   1351:        tty_margin(tty, 0, tty->sx - 1);
                   1352: }
                   1353:
1.212     nicm     1354: /* Set margin inside pane. */
                   1355: static void
                   1356: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
                   1357: {
                   1358:        tty_margin(tty, ctx->xoff, ctx->xoff + ctx->wp->sx - 1);
                   1359: }
                   1360:
                   1361: /* Set margin at absolute position. */
1.214     nicm     1362: static void
1.212     nicm     1363: tty_margin(struct tty *tty, u_int rleft, u_int rright)
                   1364: {
                   1365:        char s[64];
                   1366:
                   1367:        if (!tty_use_margin(tty))
                   1368:                return;
                   1369:        if (tty->rleft == rleft && tty->rright == rright)
                   1370:                return;
                   1371:
1.232     nicm     1372:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231     nicm     1373:
1.212     nicm     1374:        tty->rleft = rleft;
                   1375:        tty->rright = rright;
                   1376:
1.232     nicm     1377:        if (rleft == 0 && rright == tty->sx - 1)
                   1378:                snprintf(s, sizeof s, "\033[s");
                   1379:        else
                   1380:                snprintf(s, sizeof s, "\033[%u;%us", rleft + 1, rright + 1);
1.212     nicm     1381:        tty_puts(tty, s);
1.249     nicm     1382:        tty->cx = tty->cy = UINT_MAX;
1.1       nicm     1383: }
                   1384:
1.42      nicm     1385: /* Move cursor inside pane. */
1.208     nicm     1386: static void
1.41      nicm     1387: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                   1388: {
1.113     nicm     1389:        tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41      nicm     1390: }
                   1391:
1.42      nicm     1392: /* Move cursor to absolute position. */
1.41      nicm     1393: void
                   1394: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm     1395: {
1.42      nicm     1396:        struct tty_term *term = tty->term;
                   1397:        u_int            thisx, thisy;
                   1398:        int              change;
1.77      nicm     1399:
1.42      nicm     1400:        if (cx > tty->sx - 1)
                   1401:                cx = tty->sx - 1;
                   1402:
                   1403:        thisx = tty->cx;
                   1404:        thisy = tty->cy;
                   1405:
                   1406:        /* No change. */
                   1407:        if (cx == thisx && cy == thisy)
                   1408:                return;
                   1409:
1.43      nicm     1410:        /* Very end of the line, just use absolute movement. */
                   1411:        if (thisx > tty->sx - 1)
                   1412:                goto absolute;
                   1413:
1.42      nicm     1414:        /* Move to home position (0, 0). */
                   1415:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                   1416:                tty_putcode(tty, TTYC_HOME);
                   1417:                goto out;
                   1418:        }
                   1419:
                   1420:        /* Zero on the next line. */
1.48      nicm     1421:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1       nicm     1422:                tty_putc(tty, '\r');
1.42      nicm     1423:                tty_putc(tty, '\n');
                   1424:                goto out;
                   1425:        }
                   1426:
1.45      nicm     1427:        /* Moving column or row. */
1.42      nicm     1428:        if (cy == thisy) {
1.45      nicm     1429:                /*
                   1430:                 * Moving column only, row staying the same.
                   1431:                 */
                   1432:
1.42      nicm     1433:                /* To left edge. */
1.202     nicm     1434:                if (cx == 0) {
1.42      nicm     1435:                        tty_putc(tty, '\r');
                   1436:                        goto out;
                   1437:                }
                   1438:
                   1439:                /* One to the left. */
                   1440:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   1441:                        tty_putcode(tty, TTYC_CUB1);
                   1442:                        goto out;
                   1443:                }
                   1444:
                   1445:                /* One to the right. */
                   1446:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   1447:                        tty_putcode(tty, TTYC_CUF1);
                   1448:                        goto out;
                   1449:                }
                   1450:
                   1451:                /* Calculate difference. */
                   1452:                change = thisx - cx;    /* +ve left, -ve right */
                   1453:
                   1454:                /*
                   1455:                 * Use HPA if change is larger than absolute, otherwise move
                   1456:                 * the cursor with CUB/CUF.
                   1457:                 */
1.87      nicm     1458:                if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42      nicm     1459:                        tty_putcode1(tty, TTYC_HPA, cx);
                   1460:                        goto out;
                   1461:                } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202     nicm     1462:                        if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
                   1463:                                tty_putcode(tty, TTYC_CUB1);
                   1464:                                tty_putcode(tty, TTYC_CUB1);
                   1465:                                goto out;
                   1466:                        }
1.42      nicm     1467:                        tty_putcode1(tty, TTYC_CUB, change);
                   1468:                        goto out;
                   1469:                } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
                   1470:                        tty_putcode1(tty, TTYC_CUF, -change);
                   1471:                        goto out;
                   1472:                }
1.45      nicm     1473:        } else if (cx == thisx) {
                   1474:                /*
                   1475:                 * Moving row only, column staying the same.
                   1476:                 */
1.42      nicm     1477:
                   1478:                /* One above. */
1.77      nicm     1479:                if (thisy != tty->rupper &&
1.42      nicm     1480:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   1481:                        tty_putcode(tty, TTYC_CUU1);
                   1482:                        goto out;
                   1483:                }
                   1484:
                   1485:                /* One below. */
1.49      nicm     1486:                if (thisy != tty->rlower &&
1.42      nicm     1487:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   1488:                        tty_putcode(tty, TTYC_CUD1);
                   1489:                        goto out;
                   1490:                }
                   1491:
                   1492:                /* Calculate difference. */
                   1493:                change = thisy - cy;    /* +ve up, -ve down */
                   1494:
                   1495:                /*
1.77      nicm     1496:                 * Try to use VPA if change is larger than absolute or if this
                   1497:                 * change would cross the scroll region, otherwise use CUU/CUD.
1.42      nicm     1498:                 */
1.87      nicm     1499:                if ((u_int) abs(change) > cy ||
1.42      nicm     1500:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     1501:                    (change > 0 && cy - change < tty->rupper)) {
                   1502:                            if (tty_term_has(term, TTYC_VPA)) {
                   1503:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   1504:                                    goto out;
                   1505:                            }
1.42      nicm     1506:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   1507:                        tty_putcode1(tty, TTYC_CUU, change);
                   1508:                        goto out;
                   1509:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   1510:                        tty_putcode1(tty, TTYC_CUD, -change);
                   1511:                        goto out;
                   1512:                }
                   1513:        }
                   1514:
1.43      nicm     1515: absolute:
1.42      nicm     1516:        /* Absolute movement. */
                   1517:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   1518:
                   1519: out:
                   1520:        tty->cx = cx;
                   1521:        tty->cy = cy;
1.1       nicm     1522: }
                   1523:
                   1524: void
1.176     nicm     1525: tty_attributes(struct tty *tty, const struct grid_cell *gc,
                   1526:     const struct window_pane *wp)
1.1       nicm     1527: {
1.63      nicm     1528:        struct grid_cell        *tc = &tty->cell, gc2;
1.85      nicm     1529:        u_char                   changed;
1.61      nicm     1530:
1.228     nicm     1531:        /* Ignore cell if it is the same as the last one. */
                   1532:        if (wp != NULL &&
                   1533:            (int)wp->id == tty->last_wp &&
                   1534:            ~(wp->window->flags & WINDOW_STYLECHANGED) &&
                   1535:            gc->attr == tty->last_cell.attr &&
                   1536:            gc->fg == tty->last_cell.fg &&
                   1537:            gc->bg == tty->last_cell.bg)
                   1538:                return;
                   1539:        tty->last_wp = (wp != NULL ? (int)wp->id : -1);
                   1540:        memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
                   1541:
                   1542:        /* Copy cell and update default colours. */
1.85      nicm     1543:        memcpy(&gc2, gc, sizeof gc2);
1.203     nicm     1544:        if (wp != NULL)
                   1545:                tty_default_colours(&gc2, wp);
1.63      nicm     1546:
1.18      nicm     1547:        /*
                   1548:         * If no setab, try to use the reverse attribute as a best-effort for a
                   1549:         * non-default background. This is a bit of a hack but it doesn't do
                   1550:         * any serious harm and makes a couple of applications happier.
                   1551:         */
                   1552:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85      nicm     1553:                if (gc2.attr & GRID_ATTR_REVERSE) {
                   1554:                        if (gc2.fg != 7 && gc2.fg != 8)
1.64      nicm     1555:                                gc2.attr &= ~GRID_ATTR_REVERSE;
1.18      nicm     1556:                } else {
1.85      nicm     1557:                        if (gc2.bg != 0 && gc2.bg != 8)
1.64      nicm     1558:                                gc2.attr |= GRID_ATTR_REVERSE;
1.18      nicm     1559:                }
                   1560:        }
1.1       nicm     1561:
1.85      nicm     1562:        /* Fix up the colours if necessary. */
1.219     nicm     1563:        tty_check_fg(tty, wp, &gc2);
                   1564:        tty_check_bg(tty, wp, &gc2);
1.85      nicm     1565:
1.64      nicm     1566:        /* If any bits are being cleared, reset everything. */
1.85      nicm     1567:        if (tc->attr & ~gc2.attr)
1.64      nicm     1568:                tty_reset(tty);
                   1569:
                   1570:        /*
                   1571:         * Set the colours. This may call tty_reset() (so it comes next) and
                   1572:         * may add to (NOT remove) the desired attributes by changing new_attr.
                   1573:         */
1.85      nicm     1574:        tty_colours(tty, &gc2);
1.64      nicm     1575:
1.1       nicm     1576:        /* Filter out attribute bits already set. */
1.85      nicm     1577:        changed = gc2.attr & ~tc->attr;
                   1578:        tc->attr = gc2.attr;
1.1       nicm     1579:
                   1580:        /* Set the attributes. */
                   1581:        if (changed & GRID_ATTR_BRIGHT)
                   1582:                tty_putcode(tty, TTYC_BOLD);
                   1583:        if (changed & GRID_ATTR_DIM)
                   1584:                tty_putcode(tty, TTYC_DIM);
1.180     nicm     1585:        if (changed & GRID_ATTR_ITALICS)
                   1586:                tty_set_italics(tty);
1.1       nicm     1587:        if (changed & GRID_ATTR_UNDERSCORE)
                   1588:                tty_putcode(tty, TTYC_SMUL);
                   1589:        if (changed & GRID_ATTR_BLINK)
                   1590:                tty_putcode(tty, TTYC_BLINK);
                   1591:        if (changed & GRID_ATTR_REVERSE) {
                   1592:                if (tty_term_has(tty->term, TTYC_REV))
                   1593:                        tty_putcode(tty, TTYC_REV);
                   1594:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   1595:                        tty_putcode(tty, TTYC_SMSO);
                   1596:        }
                   1597:        if (changed & GRID_ATTR_HIDDEN)
                   1598:                tty_putcode(tty, TTYC_INVIS);
1.90      nicm     1599:        if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1       nicm     1600:                tty_putcode(tty, TTYC_SMACS);
                   1601: }
                   1602:
1.207     nicm     1603: static void
1.85      nicm     1604: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1605: {
1.61      nicm     1606:        struct grid_cell        *tc = &tty->cell;
1.204     nicm     1607:        int                      have_ax;
1.61      nicm     1608:
                   1609:        /* No changes? Nothing is necessary. */
1.204     nicm     1610:        if (gc->fg == tc->fg && gc->bg == tc->bg)
1.63      nicm     1611:                return;
1.1       nicm     1612:
1.61      nicm     1613:        /*
                   1614:         * Is either the default colour? This is handled specially because the
                   1615:         * best solution might be to reset both colours to default, in which
                   1616:         * case if only one is default need to fall onward to set the other
                   1617:         * colour.
                   1618:         */
1.204     nicm     1619:        if (gc->fg == 8 || gc->bg == 8) {
1.61      nicm     1620:                /*
                   1621:                 * If don't have AX but do have op, send sgr0 (op can't
                   1622:                 * actually be used because it is sometimes the same as sgr0
                   1623:                 * and sometimes isn't). This resets both colours to default.
                   1624:                 *
                   1625:                 * Otherwise, try to set the default colour only as needed.
                   1626:                 */
1.174     nicm     1627:                have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61      nicm     1628:                if (!have_ax && tty_term_has(tty->term, TTYC_OP))
                   1629:                        tty_reset(tty);
                   1630:                else {
1.204     nicm     1631:                        if (gc->fg == 8 && tc->fg != 8) {
1.61      nicm     1632:                                if (have_ax)
                   1633:                                        tty_puts(tty, "\033[39m");
1.204     nicm     1634:                                else if (tc->fg != 7)
1.61      nicm     1635:                                        tty_putcode1(tty, TTYC_SETAF, 7);
                   1636:                                tc->fg = 8;
                   1637:                        }
1.204     nicm     1638:                        if (gc->bg == 8 && tc->bg != 8) {
1.77      nicm     1639:                                if (have_ax)
1.61      nicm     1640:                                        tty_puts(tty, "\033[49m");
1.204     nicm     1641:                                else if (tc->bg != 0)
1.61      nicm     1642:                                        tty_putcode1(tty, TTYC_SETAB, 0);
                   1643:                                tc->bg = 8;
                   1644:                        }
                   1645:                }
                   1646:        }
1.1       nicm     1647:
1.61      nicm     1648:        /* Set the foreground colour. */
1.204     nicm     1649:        if (gc->fg != 8 && gc->fg != tc->fg)
1.85      nicm     1650:                tty_colours_fg(tty, gc);
1.1       nicm     1651:
1.61      nicm     1652:        /*
                   1653:         * Set the background colour. This must come after the foreground as
                   1654:         * tty_colour_fg() can call tty_reset().
                   1655:         */
1.204     nicm     1656:        if (gc->bg != 8 && gc->bg != tc->bg)
1.73      nicm     1657:                tty_colours_bg(tty, gc);
1.1       nicm     1658: }
                   1659:
1.219     nicm     1660: static void
                   1661: tty_check_fg(struct tty *tty, const struct window_pane *wp,
                   1662:     struct grid_cell *gc)
1.85      nicm     1663: {
1.204     nicm     1664:        u_char  r, g, b;
                   1665:        u_int   colours;
1.223     nicm     1666:        int     c;
1.85      nicm     1667:
1.219     nicm     1668:        /* Perform substitution if this pane has a palette */
                   1669:        if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223     nicm     1670:            (c = window_pane_get_palette(wp, gc->fg)) != -1)
                   1671:                gc->fg = c;
1.219     nicm     1672:
1.199     nicm     1673:        /* Is this a 24-bit colour? */
1.204     nicm     1674:        if (gc->fg & COLOUR_FLAG_RGB) {
1.199     nicm     1675:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
                   1676:                if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204     nicm     1677:                        colour_split_rgb(gc->fg, &r, &g, &b);
                   1678:                        gc->fg = colour_find_rgb(r, g, b);
                   1679:                } else
1.200     nicm     1680:                        return;
1.199     nicm     1681:        }
1.224     nicm     1682:
                   1683:        /* How many colours does this terminal have? */
                   1684:        if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
                   1685:                colours = 256;
                   1686:        else
                   1687:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     1688:
1.85      nicm     1689:        /* Is this a 256-colour colour? */
1.204     nicm     1690:        if (gc->fg & COLOUR_FLAG_256) {
1.85      nicm     1691:                /* And not a 256 colour mode? */
1.224     nicm     1692:                if (colours != 256) {
1.85      nicm     1693:                        gc->fg = colour_256to16(gc->fg);
                   1694:                        if (gc->fg & 8) {
                   1695:                                gc->fg &= 7;
1.175     nicm     1696:                                if (colours >= 16)
                   1697:                                        gc->fg += 90;
                   1698:                                else
                   1699:                                        gc->attr |= GRID_ATTR_BRIGHT;
1.85      nicm     1700:                        } else
                   1701:                                gc->attr &= ~GRID_ATTR_BRIGHT;
                   1702:                }
                   1703:                return;
                   1704:        }
                   1705:
                   1706:        /* Is this an aixterm colour? */
                   1707:        if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
                   1708:                gc->fg -= 90;
                   1709:                gc->attr |= GRID_ATTR_BRIGHT;
                   1710:        }
                   1711: }
                   1712:
1.219     nicm     1713: static void
                   1714: tty_check_bg(struct tty *tty, const struct window_pane *wp,
                   1715:     struct grid_cell *gc)
1.85      nicm     1716: {
1.204     nicm     1717:        u_char  r, g, b;
                   1718:        u_int   colours;
1.223     nicm     1719:        int     c;
1.85      nicm     1720:
1.219     nicm     1721:        /* Perform substitution if this pane has a palette */
                   1722:        if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223     nicm     1723:            (c = window_pane_get_palette(wp, gc->bg)) != -1)
                   1724:                gc->bg = c;
1.219     nicm     1725:
1.199     nicm     1726:        /* Is this a 24-bit colour? */
1.204     nicm     1727:        if (gc->bg & COLOUR_FLAG_RGB) {
1.199     nicm     1728:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
                   1729:                if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204     nicm     1730:                        colour_split_rgb(gc->bg, &r, &g, &b);
                   1731:                        gc->bg = colour_find_rgb(r, g, b);
                   1732:                } else
1.200     nicm     1733:                        return;
1.199     nicm     1734:        }
1.224     nicm     1735:
                   1736:        /* How many colours does this terminal have? */
                   1737:        if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
                   1738:                colours = 256;
                   1739:        else
                   1740:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     1741:
1.85      nicm     1742:        /* Is this a 256-colour colour? */
1.204     nicm     1743:        if (gc->bg & COLOUR_FLAG_256) {
1.85      nicm     1744:                /*
                   1745:                 * And not a 256 colour mode? Translate to 16-colour
                   1746:                 * palette. Bold background doesn't exist portably, so just
                   1747:                 * discard the bold bit if set.
                   1748:                 */
1.224     nicm     1749:                if (colours != 256) {
1.85      nicm     1750:                        gc->bg = colour_256to16(gc->bg);
1.175     nicm     1751:                        if (gc->bg & 8) {
1.85      nicm     1752:                                gc->bg &= 7;
1.175     nicm     1753:                                if (colours >= 16)
                   1754:                                        gc->fg += 90;
                   1755:                        }
1.85      nicm     1756:                }
                   1757:                return;
                   1758:        }
                   1759:
                   1760:        /* Is this an aixterm colour? */
1.175     nicm     1761:        if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85      nicm     1762:                gc->bg -= 90;
                   1763: }
                   1764:
1.207     nicm     1765: static void
1.85      nicm     1766: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1767: {
1.61      nicm     1768:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     1769:        char                     s[32];
1.1       nicm     1770:
1.204     nicm     1771:        /* Is this a 24-bit or 256-colour colour? */
                   1772:        if (gc->fg & COLOUR_FLAG_RGB ||
                   1773:            gc->fg & COLOUR_FLAG_256) {
                   1774:                if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61      nicm     1775:                        goto save_fg;
1.199     nicm     1776:                /* Should not get here, already converted in tty_check_fg. */
1.85      nicm     1777:                return;
1.1       nicm     1778:        }
                   1779:
1.79      nicm     1780:        /* Is this an aixterm bright colour? */
1.204     nicm     1781:        if (gc->fg >= 90 && gc->fg <= 97) {
                   1782:                xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
1.85      nicm     1783:                tty_puts(tty, s);
                   1784:                goto save_fg;
1.79      nicm     1785:        }
                   1786:
1.61      nicm     1787:        /* Otherwise set the foreground colour. */
1.204     nicm     1788:        tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61      nicm     1789:
1.77      nicm     1790: save_fg:
1.61      nicm     1791:        /* Save the new values in the terminal current cell. */
1.204     nicm     1792:        tc->fg = gc->fg;
1.1       nicm     1793: }
                   1794:
1.207     nicm     1795: static void
1.73      nicm     1796: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1797: {
1.61      nicm     1798:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     1799:        char                     s[32];
1.1       nicm     1800:
1.204     nicm     1801:        /* Is this a 24-bit or 256-colour colour? */
                   1802:        if (gc->bg & COLOUR_FLAG_RGB ||
                   1803:            gc->bg & COLOUR_FLAG_256) {
                   1804:                if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61      nicm     1805:                        goto save_bg;
1.199     nicm     1806:                /* Should not get here, already converted in tty_check_bg. */
1.85      nicm     1807:                return;
1.79      nicm     1808:        }
                   1809:
                   1810:        /* Is this an aixterm bright colour? */
1.204     nicm     1811:        if (gc->bg >= 90 && gc->bg <= 97) {
                   1812:                xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
1.175     nicm     1813:                tty_puts(tty, s);
                   1814:                goto save_bg;
1.1       nicm     1815:        }
                   1816:
1.61      nicm     1817:        /* Otherwise set the background colour. */
1.204     nicm     1818:        tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61      nicm     1819:
                   1820: save_bg:
                   1821:        /* Save the new values in the terminal current cell. */
1.204     nicm     1822:        tc->bg = gc->bg;
1.61      nicm     1823: }
                   1824:
1.207     nicm     1825: static int
1.204     nicm     1826: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61      nicm     1827: {
1.204     nicm     1828:        u_char  r, g, b;
1.61      nicm     1829:        char    s[32];
                   1830:
1.204     nicm     1831:        if (colour & COLOUR_FLAG_256) {
                   1832:                /*
                   1833:                 * If the user has specified -2 to the client, setaf and setab
                   1834:                 * may not work (or they may not want to use them), so send the
                   1835:                 * usual sequence.
                   1836:                 */
                   1837:                if (tty->term_flags & TERM_256COLOURS)
                   1838:                        goto fallback_256;
1.189     nicm     1839:
1.204     nicm     1840:                /*
                   1841:                 * If the terminfo entry has 256 colours and setaf and setab
                   1842:                 * exist, assume that they work correctly.
                   1843:                 */
                   1844:                if (tty->term->flags & TERM_256COLOURS) {
                   1845:                        if (*type == '3') {
                   1846:                                if (!tty_term_has(tty->term, TTYC_SETAF))
                   1847:                                        goto fallback_256;
                   1848:                                tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
                   1849:                        } else {
                   1850:                                if (!tty_term_has(tty->term, TTYC_SETAB))
                   1851:                                        goto fallback_256;
                   1852:                                tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
                   1853:                        }
                   1854:                        return (0);
1.188     nicm     1855:                }
1.204     nicm     1856:                goto fallback_256;
                   1857:        }
                   1858:
                   1859:        if (colour & COLOUR_FLAG_RGB) {
                   1860:                if (!tty_term_flag(tty->term, TTYC_TC))
                   1861:                        return (-1);
                   1862:
                   1863:                colour_split_rgb(colour & 0xffffff, &r, &g, &b);
                   1864:                xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type,
                   1865:                    r, g, b);
                   1866:                tty_puts(tty, s);
1.165     nicm     1867:                return (0);
                   1868:        }
1.61      nicm     1869:
1.165     nicm     1870:        return (-1);
1.188     nicm     1871:
1.204     nicm     1872: fallback_256:
                   1873:        xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.188     nicm     1874:        tty_puts(tty, s);
                   1875:        return (0);
1.176     nicm     1876: }
                   1877:
1.207     nicm     1878: static void
1.176     nicm     1879: tty_default_colours(struct grid_cell *gc, const struct window_pane *wp)
                   1880: {
1.203     nicm     1881:        struct window           *w = wp->window;
                   1882:        struct options          *oo = w->options;
                   1883:        const struct grid_cell  *agc, *pgc, *wgc;
1.223     nicm     1884:        int                      c;
1.203     nicm     1885:
                   1886:        if (w->flags & WINDOW_STYLECHANGED) {
                   1887:                w->flags &= ~WINDOW_STYLECHANGED;
                   1888:                agc = options_get_style(oo, "window-active-style");
                   1889:                memcpy(&w->active_style, agc, sizeof w->active_style);
                   1890:                wgc = options_get_style(oo, "window-style");
                   1891:                memcpy(&w->style, wgc, sizeof w->style);
                   1892:        } else {
                   1893:                agc = &w->active_style;
                   1894:                wgc = &w->style;
                   1895:        }
1.176     nicm     1896:        pgc = &wp->colgc;
                   1897:
1.204     nicm     1898:        if (gc->fg == 8) {
                   1899:                if (pgc->fg != 8)
1.176     nicm     1900:                        gc->fg = pgc->fg;
1.204     nicm     1901:                else if (wp == w->active && agc->fg != 8)
1.176     nicm     1902:                        gc->fg = agc->fg;
1.204     nicm     1903:                else
1.176     nicm     1904:                        gc->fg = wgc->fg;
1.219     nicm     1905:
1.223     nicm     1906:                if (gc->fg != 8 &&
                   1907:                    (c = window_pane_get_palette(wp, gc->fg)) != -1)
                   1908:                        gc->fg = c;
1.176     nicm     1909:        }
                   1910:
1.204     nicm     1911:        if (gc->bg == 8) {
                   1912:                if (pgc->bg != 8)
1.176     nicm     1913:                        gc->bg = pgc->bg;
1.204     nicm     1914:                else if (wp == w->active && agc->bg != 8)
1.176     nicm     1915:                        gc->bg = agc->bg;
1.204     nicm     1916:                else
1.176     nicm     1917:                        gc->bg = wgc->bg;
1.219     nicm     1918:
1.223     nicm     1919:                if (gc->bg != 8 &&
                   1920:                    (c = window_pane_get_palette(wp, gc->bg)) != -1)
                   1921:                        gc->bg = c;
1.176     nicm     1922:        }
1.210     nicm     1923: }
                   1924:
                   1925: static void
                   1926: tty_default_attributes(struct tty *tty, const struct window_pane *wp, u_int bg)
                   1927: {
                   1928:        static struct grid_cell gc;
                   1929:
                   1930:        memcpy(&gc, &grid_default_cell, sizeof gc);
                   1931:        gc.bg = bg;
                   1932:        tty_attributes(tty, &gc, wp);
1.1       nicm     1933: }