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

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