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

1.372   ! nicm        1: /* $OpenBSD: tty.c,v 1.371 2020/05/16 15:01:31 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.253     nicm       44: static void    tty_cursor_pane_unless_wrap(struct tty *,
                     45:                    const struct tty_ctx *, u_int, u_int);
1.242     nicm       46: static void    tty_invalidate(struct tty *);
1.207     nicm       47: static void    tty_colours(struct tty *, const struct grid_cell *);
1.317     nicm       48: static void    tty_check_fg(struct tty *, struct window_pane *,
1.219     nicm       49:                    struct grid_cell *);
1.317     nicm       50: static void    tty_check_bg(struct tty *, struct window_pane *,
1.219     nicm       51:                    struct grid_cell *);
1.328     nicm       52: static void    tty_check_us(struct tty *, struct window_pane *,
                     53:                    struct grid_cell *);
1.207     nicm       54: static void    tty_colours_fg(struct tty *, const struct grid_cell *);
                     55: static void    tty_colours_bg(struct tty *, const struct grid_cell *);
1.328     nicm       56: static void    tty_colours_us(struct tty *, const struct grid_cell *);
1.207     nicm       57:
1.208     nicm       58: static void    tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
                     59:                    u_int);
1.214     nicm       60: static void    tty_region(struct tty *, u_int, u_int);
1.212     nicm       61: static void    tty_margin_pane(struct tty *, const struct tty_ctx *);
1.214     nicm       62: static void    tty_margin(struct tty *, u_int, u_int);
1.207     nicm       63: static int     tty_large_region(struct tty *, const struct tty_ctx *);
1.317     nicm       64: static int     tty_fake_bce(const struct tty *, struct window_pane *, u_int);
1.207     nicm       65: static void    tty_redraw_region(struct tty *, const struct tty_ctx *);
                     66: static void    tty_emulate_repeat(struct tty *, enum tty_code_code,
                     67:                    enum tty_code_code, u_int);
                     68: static void    tty_repeat_space(struct tty *, u_int);
1.309     nicm       69: static void    tty_draw_pane(struct tty *, const struct tty_ctx *, u_int);
1.207     nicm       70: static void    tty_cell(struct tty *, const struct grid_cell *,
1.317     nicm       71:                    struct window_pane *);
                     72: static void    tty_default_colours(struct grid_cell *, struct window_pane *);
                     73: static void    tty_default_attributes(struct tty *, struct window_pane *,
1.210     nicm       74:                    u_int);
1.1       nicm       75:
1.212     nicm       76: #define tty_use_margin(tty) \
1.359     nicm       77:        (tty->term->flags & TERM_DECSLRM)
1.372   ! nicm       78: #define tty_full_width(tty, ctx) \
        !            79:        ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx)
1.132     nicm       80:
1.266     nicm       81: #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */)
                     82: #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8)
                     83: #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8)
                     84:
1.192     nicm       85: void
                     86: tty_create_log(void)
                     87: {
                     88:        char    name[64];
                     89:
                     90:        xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
                     91:
                     92:        tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
                     93:        if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
                     94:                fatal("fcntl failed");
                     95: }
                     96:
1.185     nicm       97: int
1.359     nicm       98: tty_init(struct tty *tty, struct client *c, int fd)
1.1       nicm       99: {
1.185     nicm      100:        if (!isatty(fd))
                    101:                return (-1);
                    102:
1.30      nicm      103:        memset(tty, 0, sizeof *tty);
1.22      nicm      104:
1.30      nicm      105:        tty->fd = fd;
1.136     nicm      106:        tty->client = c;
1.30      nicm      107:
1.108     nicm      108:        tty->cstyle = 0;
1.107     nicm      109:        tty->ccolour = xstrdup("");
1.30      nicm      110:
1.185     nicm      111:        return (0);
1.31      nicm      112: }
                    113:
1.287     nicm      114: void
1.31      nicm      115: tty_resize(struct tty *tty)
                    116: {
1.258     nicm      117:        struct client   *c = tty->client;
                    118:        struct winsize   ws;
1.334     nicm      119:        u_int            sx, sy, xpixel, ypixel;
1.31      nicm      120:
                    121:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88      nicm      122:                sx = ws.ws_col;
1.334     nicm      123:                if (sx == 0) {
1.88      nicm      124:                        sx = 80;
1.334     nicm      125:                        xpixel = 0;
                    126:                } else
                    127:                        xpixel = ws.ws_xpixel / sx;
1.88      nicm      128:                sy = ws.ws_row;
1.334     nicm      129:                if (sy == 0) {
1.88      nicm      130:                        sy = 24;
1.334     nicm      131:                        ypixel = 0;
                    132:                } else
                    133:                        ypixel = ws.ws_ypixel / sy;
1.88      nicm      134:        } else {
                    135:                sx = 80;
                    136:                sy = 24;
1.334     nicm      137:                xpixel = 0;
                    138:                ypixel = 0;
1.31      nicm      139:        }
1.334     nicm      140:        log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy,
                    141:            xpixel, ypixel);
                    142:        tty_set_size(tty, sx, sy, xpixel, ypixel);
1.242     nicm      143:        tty_invalidate(tty);
1.114     nicm      144: }
                    145:
1.287     nicm      146: void
1.334     nicm      147: tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel)
1.212     nicm      148: {
1.114     nicm      149:        tty->sx = sx;
                    150:        tty->sy = sy;
1.334     nicm      151:        tty->xpixel = xpixel;
                    152:        tty->ypixel = ypixel;
1.1       nicm      153: }
                    154:
1.244     nicm      155: static void
                    156: tty_read_callback(__unused int fd, __unused short events, void *data)
                    157: {
                    158:        struct tty      *tty = data;
1.258     nicm      159:        struct client   *c = tty->client;
1.244     nicm      160:        size_t           size = EVBUFFER_LENGTH(tty->in);
                    161:        int              nread;
                    162:
                    163:        nread = evbuffer_read(tty->in, tty->fd, -1);
1.295     nicm      164:        if (nread == 0 || nread == -1) {
1.290     nicm      165:                event_del(&tty->event_in);
1.295     nicm      166:                server_client_lost(tty->client);
1.244     nicm      167:                return;
1.290     nicm      168:        }
1.258     nicm      169:        log_debug("%s: read %d bytes (already %zu)", c->name, nread, size);
1.244     nicm      170:
                    171:        while (tty_keys_next(tty))
                    172:                ;
                    173: }
                    174:
                    175: static void
1.266     nicm      176: tty_timer_callback(__unused int fd, __unused short events, void *data)
                    177: {
                    178:        struct tty      *tty = data;
                    179:        struct client   *c = tty->client;
                    180:        struct timeval   tv = { .tv_usec = TTY_BLOCK_INTERVAL };
                    181:
                    182:        log_debug("%s: %zu discarded", c->name, tty->discarded);
                    183:
1.305     nicm      184:        c->flags |= CLIENT_ALLREDRAWFLAGS;
1.266     nicm      185:        c->discarded += tty->discarded;
                    186:
                    187:        if (tty->discarded < TTY_BLOCK_STOP(tty)) {
                    188:                tty->flags &= ~TTY_BLOCK;
                    189:                tty_invalidate(tty);
                    190:                return;
                    191:        }
                    192:        tty->discarded = 0;
                    193:        evtimer_add(&tty->timer, &tv);
                    194: }
                    195:
                    196: static int
                    197: tty_block_maybe(struct tty *tty)
                    198: {
                    199:        struct client   *c = tty->client;
                    200:        size_t           size = EVBUFFER_LENGTH(tty->out);
                    201:        struct timeval   tv = { .tv_usec = TTY_BLOCK_INTERVAL };
                    202:
                    203:        if (size < TTY_BLOCK_START(tty))
                    204:                return (0);
                    205:
                    206:        if (tty->flags & TTY_BLOCK)
                    207:                return (1);
                    208:        tty->flags |= TTY_BLOCK;
                    209:
                    210:        log_debug("%s: can't keep up, %zu discarded", c->name, size);
                    211:
                    212:        evbuffer_drain(tty->out, size);
                    213:        c->discarded += size;
                    214:
                    215:        tty->discarded = 0;
                    216:        evtimer_add(&tty->timer, &tv);
                    217:        return (1);
                    218: }
                    219:
                    220: static void
1.244     nicm      221: tty_write_callback(__unused int fd, __unused short events, void *data)
                    222: {
1.246     nicm      223:        struct tty      *tty = data;
1.258     nicm      224:        struct client   *c = tty->client;
1.246     nicm      225:        size_t           size = EVBUFFER_LENGTH(tty->out);
                    226:        int              nwrite;
1.244     nicm      227:
                    228:        nwrite = evbuffer_write(tty->out, tty->fd);
                    229:        if (nwrite == -1)
                    230:                return;
1.258     nicm      231:        log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size);
1.244     nicm      232:
1.270     nicm      233:        if (c->redraw > 0) {
                    234:                if ((size_t)nwrite >= c->redraw)
                    235:                        c->redraw = 0;
                    236:                else
                    237:                        c->redraw -= nwrite;
                    238:                log_debug("%s: waiting for redraw, %zu bytes left", c->name,
                    239:                    c->redraw);
                    240:        } else if (tty_block_maybe(tty))
1.266     nicm      241:                return;
                    242:
1.247     nicm      243:        if (EVBUFFER_LENGTH(tty->out) != 0)
                    244:                event_add(&tty->event_out, NULL);
1.244     nicm      245: }
                    246:
1.1       nicm      247: int
1.166     nicm      248: tty_open(struct tty *tty, char **cause)
1.1       nicm      249: {
1.359     nicm      250:        struct client   *c = tty->client;
                    251:
                    252:        tty->term = tty_term_create(tty, c->term_name, &c->term_features,
                    253:            tty->fd, cause);
1.21      nicm      254:        if (tty->term == NULL) {
                    255:                tty_close(tty);
                    256:                return (-1);
                    257:        }
                    258:        tty->flags |= TTY_OPENED;
1.1       nicm      259:
1.266     nicm      260:        tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER);
1.244     nicm      261:
                    262:        event_set(&tty->event_in, tty->fd, EV_PERSIST|EV_READ,
                    263:            tty_read_callback, tty);
                    264:        tty->in = evbuffer_new();
1.311     nicm      265:        if (tty->in == NULL)
                    266:                fatal("out of memory");
1.1       nicm      267:
1.244     nicm      268:        event_set(&tty->event_out, tty->fd, EV_WRITE, tty_write_callback, tty);
                    269:        tty->out = evbuffer_new();
1.311     nicm      270:        if (tty->out == NULL)
                    271:                fatal("out of memory");
1.1       nicm      272:
1.266     nicm      273:        evtimer_set(&tty->timer, tty_timer_callback, tty);
                    274:
1.1       nicm      275:        tty_start_tty(tty);
                    276:
1.148     nicm      277:        tty_keys_build(tty);
1.1       nicm      278:
                    279:        return (0);
                    280: }
                    281:
1.340     nicm      282: static void
                    283: tty_start_timer_callback(__unused int fd, __unused short events, void *data)
                    284: {
                    285:        struct tty      *tty = data;
                    286:        struct client   *c = tty->client;
                    287:
                    288:        log_debug("%s: start timer fired", c->name);
1.364     nicm      289:        tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
1.340     nicm      290: }
                    291:
1.244     nicm      292: void
                    293: tty_start_tty(struct tty *tty)
1.1       nicm      294: {
1.285     nicm      295:        struct client   *c = tty->client;
                    296:        struct termios   tio;
1.340     nicm      297:        struct timeval   tv = { .tv_sec = 1 };
1.33      nicm      298:
1.244     nicm      299:        if (tty->fd != -1 && tcgetattr(tty->fd, &tty->tio) == 0) {
                    300:                setblocking(tty->fd, 0);
                    301:                event_add(&tty->event_in, NULL);
                    302:
                    303:                memcpy(&tio, &tty->tio, sizeof tio);
                    304:                tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
                    305:                tio.c_iflag |= IGNBRK;
                    306:                tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
                    307:                tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
                    308:                    ECHOPRT|ECHOKE|ISIG);
                    309:                tio.c_cc[VMIN] = 1;
                    310:                tio.c_cc[VTIME] = 0;
                    311:                if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
                    312:                        tcflush(tty->fd, TCIOFLUSH);
                    313:        }
1.1       nicm      314:
1.10      nicm      315:        tty_putcode(tty, TTYC_SMCUP);
1.1       nicm      316:
1.252     nicm      317:        tty_putcode(tty, TTYC_SMKX);
1.285     nicm      318:        tty_putcode(tty, TTYC_CLEAR);
                    319:
                    320:        if (tty_acs_needed(tty)) {
                    321:                log_debug("%s: using capabilities for ACS", c->name);
1.90      nicm      322:                tty_putcode(tty, TTYC_ENACS);
1.285     nicm      323:        } else
                    324:                log_debug("%s: using UTF-8 for ACS", c->name);
1.1       nicm      325:
                    326:        tty_putcode(tty, TTYC_CNORM);
1.343     nicm      327:        if (tty_term_has(tty->term, TTYC_KMOUS)) {
                    328:                tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l");
                    329:                tty_puts(tty, "\033[?1006l\033[?1005l");
                    330:        }
1.121     nicm      331:
1.367     nicm      332:        if (options_get_number(global_options, "focus-events")) {
                    333:                tty->flags |= TTY_FOCUS;
                    334:                tty_raw(tty, tty_term_string(tty->term, TTYC_ENFCS));
                    335:        }
1.369     nicm      336:        if (tty->term->flags & TERM_VT100LIKE)
1.352     nicm      337:                tty_puts(tty, "\033[?7727h");
1.1       nicm      338:
1.340     nicm      339:        evtimer_set(&tty->start_timer, tty_start_timer_callback, tty);
                    340:        evtimer_add(&tty->start_timer, &tv);
                    341:
1.20      nicm      342:        tty->flags |= TTY_STARTED;
1.242     nicm      343:        tty_invalidate(tty);
1.107     nicm      344:
1.336     nicm      345:        if (*tty->ccolour != '\0')
                    346:                tty_force_cursor_colour(tty, "");
1.177     nicm      347:
                    348:        tty->mouse_drag_flag = 0;
                    349:        tty->mouse_drag_update = NULL;
                    350:        tty->mouse_drag_release = NULL;
1.347     nicm      351: }
                    352:
                    353: void
                    354: tty_send_requests(struct tty *tty)
                    355: {
                    356:        if (~tty->flags & TTY_STARTED)
                    357:                return;
                    358:
1.369     nicm      359:        if (tty->term->flags & TERM_VT100LIKE) {
1.347     nicm      360:                if (~tty->flags & TTY_HAVEDA)
1.351     nicm      361:                        tty_puts(tty, "\033[>c");
1.364     nicm      362:                if (~tty->flags & TTY_HAVEXDA)
                    363:                        tty_puts(tty, "\033[>q");
1.347     nicm      364:        } else
1.364     nicm      365:                tty->flags |= (TTY_HAVEDA|TTY_HAVEXDA);
1.128     nicm      366: }
                    367:
                    368: void
1.1       nicm      369: tty_stop_tty(struct tty *tty)
                    370: {
                    371:        struct winsize  ws;
                    372:
1.20      nicm      373:        if (!(tty->flags & TTY_STARTED))
                    374:                return;
                    375:        tty->flags &= ~TTY_STARTED;
1.340     nicm      376:
                    377:        evtimer_del(&tty->start_timer);
1.20      nicm      378:
1.266     nicm      379:        event_del(&tty->timer);
                    380:        tty->flags &= ~TTY_BLOCK;
                    381:
1.244     nicm      382:        event_del(&tty->event_in);
                    383:        event_del(&tty->event_out);
1.66      nicm      384:
1.1       nicm      385:        /*
                    386:         * Be flexible about error handling and try not kill the server just
                    387:         * because the fd is invalid. Things like ssh -t can easily leave us
                    388:         * with a dead tty.
                    389:         */
                    390:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
                    391:                return;
                    392:        if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
                    393:                return;
                    394:
                    395:        tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.285     nicm      396:        if (tty_acs_needed(tty))
1.90      nicm      397:                tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1       nicm      398:        tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
                    399:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
                    400:        tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160     nicm      401:        if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
                    402:                if (tty_term_has(tty->term, TTYC_SE))
                    403:                        tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109     nicm      404:                else
1.160     nicm      405:                        tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108     nicm      406:        }
1.173     nicm      407:        if (tty->mode & MODE_BRACKETPASTE)
1.366     nicm      408:                tty_raw(tty, tty_term_string(tty->term, TTYC_DSBP));
1.336     nicm      409:        if (*tty->ccolour != '\0')
                    410:                tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1       nicm      411:
                    412:        tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
1.343     nicm      413:        if (tty_term_has(tty->term, TTYC_KMOUS)) {
                    414:                tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l");
                    415:                tty_raw(tty, "\033[?1006l\033[?1005l");
                    416:        }
1.151     nicm      417:
1.367     nicm      418:        if (tty->flags & TTY_FOCUS) {
                    419:                tty->flags &= ~TTY_FOCUS;
                    420:                tty_raw(tty, tty_term_string(tty->term, TTYC_DSFCS));
                    421:        }
1.369     nicm      422:        if (tty->term->flags & TERM_VT100LIKE)
1.352     nicm      423:                tty_raw(tty, "\033[?7727l");
1.10      nicm      424:
1.212     nicm      425:        if (tty_use_margin(tty))
1.365     nicm      426:                tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG));
1.10      nicm      427:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150     nicm      428:
                    429:        setblocking(tty->fd, 1);
1.1       nicm      430: }
                    431:
                    432: void
1.20      nicm      433: tty_close(struct tty *tty)
1.1       nicm      434: {
1.123     nicm      435:        if (event_initialized(&tty->key_timer))
                    436:                evtimer_del(&tty->key_timer);
1.20      nicm      437:        tty_stop_tty(tty);
1.1       nicm      438:
1.21      nicm      439:        if (tty->flags & TTY_OPENED) {
1.244     nicm      440:                evbuffer_free(tty->in);
                    441:                event_del(&tty->event_in);
                    442:                evbuffer_free(tty->out);
                    443:                event_del(&tty->event_out);
1.66      nicm      444:
1.21      nicm      445:                tty_term_free(tty->term);
                    446:                tty_keys_free(tty);
                    447:
                    448:                tty->flags &= ~TTY_OPENED;
                    449:        }
1.1       nicm      450:
1.21      nicm      451:        if (tty->fd != -1) {
                    452:                close(tty->fd);
                    453:                tty->fd = -1;
                    454:        }
1.1       nicm      455: }
                    456:
                    457: void
1.20      nicm      458: tty_free(struct tty *tty)
1.1       nicm      459: {
1.20      nicm      460:        tty_close(tty);
1.138     nicm      461:        free(tty->ccolour);
1.1       nicm      462: }
                    463:
                    464: void
1.359     nicm      465: tty_update_features(struct tty *tty)
1.212     nicm      466: {
1.359     nicm      467:        struct client   *c = tty->client;
                    468:
1.362     nicm      469:        if (tty_apply_features(tty->term, c->term_features))
                    470:                tty_term_apply_overrides(tty->term);
1.212     nicm      471:
                    472:        if (tty_use_margin(tty))
1.365     nicm      473:                tty_putcode(tty, TTYC_ENMG);
1.212     nicm      474: }
                    475:
                    476: void
1.1       nicm      477: tty_raw(struct tty *tty, const char *s)
                    478: {
1.150     nicm      479:        ssize_t n, slen;
                    480:        u_int   i;
                    481:
                    482:        slen = strlen(s);
                    483:        for (i = 0; i < 5; i++) {
                    484:                n = write(tty->fd, s, slen);
                    485:                if (n >= 0) {
                    486:                        s += n;
                    487:                        slen -= n;
                    488:                        if (slen == 0)
                    489:                                break;
                    490:                } else if (n == -1 && errno != EAGAIN)
                    491:                        break;
                    492:                usleep(100);
                    493:        }
1.1       nicm      494: }
                    495:
                    496: void
                    497: tty_putcode(struct tty *tty, enum tty_code_code code)
                    498: {
                    499:        tty_puts(tty, tty_term_string(tty->term, code));
                    500: }
                    501:
                    502: void
                    503: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
                    504: {
                    505:        if (a < 0)
                    506:                return;
                    507:        tty_puts(tty, tty_term_string1(tty->term, code, a));
                    508: }
                    509:
                    510: void
                    511: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
                    512: {
                    513:        if (a < 0 || b < 0)
                    514:                return;
                    515:        tty_puts(tty, tty_term_string2(tty->term, code, a, b));
                    516: }
                    517:
                    518: void
1.286     nicm      519: tty_putcode3(struct tty *tty, enum tty_code_code code, int a, int b, int c)
                    520: {
                    521:        if (a < 0 || b < 0 || c < 0)
                    522:                return;
                    523:        tty_puts(tty, tty_term_string3(tty->term, code, a, b, c));
                    524: }
                    525:
                    526: void
1.107     nicm      527: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
                    528: {
                    529:        if (a != NULL)
                    530:                tty_puts(tty, tty_term_ptr1(tty->term, code, a));
                    531: }
                    532:
                    533: void
1.167     nicm      534: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
                    535:     const void *b)
1.106     nicm      536: {
                    537:        if (a != NULL && b != NULL)
                    538:                tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
                    539: }
                    540:
1.243     nicm      541: static void
                    542: tty_add(struct tty *tty, const char *buf, size_t len)
                    543: {
1.258     nicm      544:        struct client   *c = tty->client;
                    545:
1.266     nicm      546:        if (tty->flags & TTY_BLOCK) {
                    547:                tty->discarded += len;
                    548:                return;
                    549:        }
                    550:
1.244     nicm      551:        evbuffer_add(tty->out, buf, len);
1.270     nicm      552:        log_debug("%s: %.*s", c->name, (int)len, buf);
1.266     nicm      553:        c->written += len;
1.243     nicm      554:
                    555:        if (tty_log_fd != -1)
                    556:                write(tty_log_fd, buf, len);
1.244     nicm      557:        if (tty->flags & TTY_STARTED)
                    558:                event_add(&tty->event_out, NULL);
1.243     nicm      559: }
                    560:
1.106     nicm      561: void
1.1       nicm      562: tty_puts(struct tty *tty, const char *s)
                    563: {
1.243     nicm      564:        if (*s != '\0')
                    565:                tty_add(tty, s, strlen(s));
1.1       nicm      566: }
                    567:
                    568: void
                    569: tty_putc(struct tty *tty, u_char ch)
                    570: {
1.90      nicm      571:        const char      *acs;
1.1       nicm      572:
1.359     nicm      573:        if ((tty->term->flags & TERM_NOXENL) &&
1.325     nicm      574:            ch >= 0x20 && ch != 0x7f &&
                    575:            tty->cy == tty->sy - 1 &&
                    576:            tty->cx + 1 >= tty->sx)
                    577:                return;
                    578:
1.90      nicm      579:        if (tty->cell.attr & GRID_ATTR_CHARSET) {
                    580:                acs = tty_acs_get(tty, ch);
1.243     nicm      581:                if (acs != NULL)
                    582:                        tty_add(tty, acs, strlen(acs));
                    583:                else
                    584:                        tty_add(tty, &ch, 1);
                    585:        } else
                    586:                tty_add(tty, &ch, 1);
1.1       nicm      587:
                    588:        if (ch >= 0x20 && ch != 0x7f) {
1.211     nicm      589:                if (tty->cx >= tty->sx) {
1.46      nicm      590:                        tty->cx = 1;
                    591:                        if (tty->cy != tty->rlower)
                    592:                                tty->cy++;
1.211     nicm      593:
                    594:                        /*
                    595:                         * On !xenl terminals, force the cursor position to
                    596:                         * where we think it should be after a line wrap - this
                    597:                         * means it works on sensible terminals as well.
                    598:                         */
1.359     nicm      599:                        if (tty->term->flags & TERM_NOXENL)
1.211     nicm      600:                                tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1       nicm      601:                } else
                    602:                        tty->cx++;
                    603:        }
                    604: }
                    605:
                    606: void
1.147     nicm      607: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5       nicm      608: {
1.359     nicm      609:        if ((tty->term->flags & TERM_NOXENL) &&
1.325     nicm      610:            tty->cy == tty->sy - 1 &&
                    611:            tty->cx + len >= tty->sx)
                    612:                len = tty->sx - tty->cx - 1;
                    613:
1.243     nicm      614:        tty_add(tty, buf, len);
1.268     nicm      615:        if (tty->cx + width > tty->sx) {
                    616:                tty->cx = (tty->cx + width) - tty->sx;
                    617:                if (tty->cx <= tty->sx)
                    618:                        tty->cy++;
                    619:                else
                    620:                        tty->cx = tty->cy = UINT_MAX;
                    621:        } else
1.254     nicm      622:                tty->cx += width;
1.5       nicm      623: }
                    624:
1.207     nicm      625: static void
1.180     nicm      626: tty_set_italics(struct tty *tty)
                    627: {
                    628:        const char      *s;
                    629:
                    630:        if (tty_term_has(tty->term, TTYC_SITM)) {
1.191     nicm      631:                s = options_get_string(global_options, "default-terminal");
1.180     nicm      632:                if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
                    633:                        tty_putcode(tty, TTYC_SITM);
                    634:                        return;
                    635:                }
                    636:        }
                    637:        tty_putcode(tty, TTYC_SMSO);
                    638: }
                    639:
                    640: void
1.1       nicm      641: tty_set_title(struct tty *tty, const char *title)
                    642: {
1.105     nicm      643:        if (!tty_term_has(tty->term, TTYC_TSL) ||
                    644:            !tty_term_has(tty->term, TTYC_FSL))
1.1       nicm      645:                return;
                    646:
1.105     nicm      647:        tty_putcode(tty, TTYC_TSL);
1.1       nicm      648:        tty_puts(tty, title);
1.105     nicm      649:        tty_putcode(tty, TTYC_FSL);
1.1       nicm      650: }
                    651:
1.208     nicm      652: static void
1.107     nicm      653: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
                    654: {
                    655:        if (*ccolour == '\0')
                    656:                tty_putcode(tty, TTYC_CR);
                    657:        else
1.160     nicm      658:                tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138     nicm      659:        free(tty->ccolour);
1.107     nicm      660:        tty->ccolour = xstrdup(ccolour);
                    661: }
                    662:
                    663: void
                    664: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1       nicm      665: {
1.344     nicm      666:        struct client   *c = tty->client;
                    667:        int              changed;
1.1       nicm      668:
1.197     nicm      669:        if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107     nicm      670:                tty_force_cursor_colour(tty, s->ccolour);
                    671:
1.1       nicm      672:        if (tty->flags & TTY_NOCURSOR)
                    673:                mode &= ~MODE_CURSOR;
                    674:
                    675:        changed = mode ^ tty->mode;
1.368     nicm      676:        if (changed != 0)
                    677:                log_debug("%s: update mode %x to %x", c->name, tty->mode, mode);
1.344     nicm      678:
1.183     nicm      679:        if (changed & MODE_BLINKING) {
                    680:                if (tty_term_has(tty->term, TTYC_CVVIS))
                    681:                        tty_putcode(tty, TTYC_CVVIS);
                    682:                else
                    683:                        tty_putcode(tty, TTYC_CNORM);
                    684:                changed |= MODE_CURSOR;
                    685:        }
                    686:        if (changed & MODE_CURSOR) {
                    687:                if (mode & MODE_CURSOR)
                    688:                        tty_putcode(tty, TTYC_CNORM);
                    689:                else
1.1       nicm      690:                        tty_putcode(tty, TTYC_CIVIS);
1.108     nicm      691:        }
1.181     nicm      692:        if (s != NULL && tty->cstyle != s->cstyle) {
1.160     nicm      693:                if (tty_term_has(tty->term, TTYC_SS)) {
1.108     nicm      694:                        if (s->cstyle == 0 &&
1.160     nicm      695:                            tty_term_has(tty->term, TTYC_SE))
                    696:                                tty_putcode(tty, TTYC_SE);
1.108     nicm      697:                        else
1.160     nicm      698:                                tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108     nicm      699:                }
                    700:                tty->cstyle = s->cstyle;
1.1       nicm      701:        }
1.344     nicm      702:        if ((changed & ALL_MOUSE_MODES) &&
                    703:            tty_term_has(tty->term, TTYC_KMOUS)) {
                    704:                if ((mode & ALL_MOUSE_MODES) == 0)
                    705:                        tty_puts(tty, "\033[?1006l");
                    706:                if ((changed & MODE_MOUSE_STANDARD) &&
                    707:                    (~mode & MODE_MOUSE_STANDARD))
                    708:                        tty_puts(tty, "\033[?1000l");
                    709:                if ((changed & MODE_MOUSE_BUTTON) &&
                    710:                    (~mode & MODE_MOUSE_BUTTON))
                    711:                        tty_puts(tty, "\033[?1002l");
                    712:                if ((changed & MODE_MOUSE_ALL) &&
                    713:                    (~mode & MODE_MOUSE_ALL))
                    714:                        tty_puts(tty, "\033[?1003l");
                    715:
                    716:                if (mode & ALL_MOUSE_MODES)
1.153     nicm      717:                        tty_puts(tty, "\033[?1006h");
1.344     nicm      718:                if ((changed & MODE_MOUSE_STANDARD) &&
                    719:                    (mode & MODE_MOUSE_STANDARD))
                    720:                        tty_puts(tty, "\033[?1000h");
                    721:                if ((changed & MODE_MOUSE_BUTTON) &&
                    722:                    (mode & MODE_MOUSE_BUTTON))
                    723:                        tty_puts(tty, "\033[?1002h");
                    724:                if ((changed & MODE_MOUSE_ALL) &&
                    725:                    (mode & MODE_MOUSE_ALL))
                    726:                        tty_puts(tty, "\033[?1003h");
1.115     nicm      727:        }
                    728:        if (changed & MODE_BRACKETPASTE) {
                    729:                if (mode & MODE_BRACKETPASTE)
1.366     nicm      730:                        tty_putcode(tty, TTYC_ENBP);
1.115     nicm      731:                else
1.366     nicm      732:                        tty_putcode(tty, TTYC_DSBP);
1.1       nicm      733:        }
                    734:        tty->mode = mode;
                    735: }
                    736:
1.207     nicm      737: static void
1.168     nicm      738: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
                    739:     enum tty_code_code code1, u_int n)
1.1       nicm      740: {
                    741:        if (tty_term_has(tty->term, code))
                    742:                tty_putcode1(tty, code, n);
                    743:        else {
                    744:                while (n-- > 0)
                    745:                        tty_putcode(tty, code1);
                    746:        }
                    747: }
                    748:
1.207     nicm      749: static void
1.133     nicm      750: tty_repeat_space(struct tty *tty, u_int n)
                    751: {
1.257     nicm      752:        static char s[500];
                    753:
                    754:        if (*s != ' ')
                    755:                memset(s, ' ', sizeof s);
                    756:
                    757:        while (n > sizeof s) {
                    758:                tty_putn(tty, s, sizeof s, sizeof s);
                    759:                n -= sizeof s;
                    760:        }
                    761:        if (n != 0)
                    762:                tty_putn(tty, s, n, n);
1.304     nicm      763: }
                    764:
1.309     nicm      765: /* Is this window bigger than the terminal? */
                    766: int
                    767: tty_window_bigger(struct tty *tty)
                    768: {
                    769:        struct client   *c = tty->client;
                    770:        struct window   *w = c->session->curw->window;
                    771:
                    772:        return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy);
                    773: }
                    774:
                    775: /* What offset should this window be drawn at? */
                    776: int
                    777: tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
                    778: {
                    779:        *ox = tty->oox;
                    780:        *oy = tty->ooy;
                    781:        *sx = tty->osx;
                    782:        *sy = tty->osy;
                    783:
                    784:        return (tty->oflag);
                    785: }
                    786:
                    787: /* What offset should this window be drawn at? */
                    788: static int
                    789: tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy)
                    790: {
                    791:        struct client           *c = tty->client;
                    792:        struct window           *w = c->session->curw->window;
                    793:        struct window_pane      *wp = w->active;
                    794:        u_int                    cx, cy, lines;
                    795:
                    796:        lines = status_line_size(c);
                    797:
                    798:        if (tty->sx >= w->sx && tty->sy - lines >= w->sy) {
                    799:                *ox = 0;
                    800:                *oy = 0;
                    801:                *sx = w->sx;
                    802:                *sy = w->sy;
                    803:
                    804:                c->pan_window = NULL;
                    805:                return (0);
                    806:        }
                    807:
                    808:        *sx = tty->sx;
                    809:        *sy = tty->sy - lines;
                    810:
                    811:        if (c->pan_window == w) {
                    812:                if (*sx >= w->sx)
                    813:                        c->pan_ox = 0;
                    814:                else if (c->pan_ox + *sx > w->sx)
                    815:                        c->pan_ox = w->sx - *sx;
                    816:                *ox = c->pan_ox;
                    817:                if (*sy >= w->sy)
                    818:                        c->pan_oy = 0;
                    819:                else if (c->pan_oy + *sy > w->sy)
                    820:                        c->pan_oy = w->sy - *sy;
                    821:                *oy = c->pan_oy;
                    822:                return (1);
                    823:        }
                    824:
                    825:        if (~wp->screen->mode & MODE_CURSOR) {
                    826:                *ox = 0;
                    827:                *oy = 0;
                    828:        } else {
                    829:                cx = wp->xoff + wp->screen->cx;
                    830:                cy = wp->yoff + wp->screen->cy;
                    831:
                    832:                if (cx < *sx)
                    833:                        *ox = 0;
                    834:                else if (cx > w->sx - *sx)
                    835:                        *ox = w->sx - *sx;
                    836:                else
                    837:                        *ox = cx - *sx / 2;
                    838:
                    839:                if (cy < *sy)
                    840:                        *oy = 0;
                    841:                else if (cy > w->sy - *sy)
                    842:                        *oy = w->sy - *sy;
                    843:                else
                    844:                        *oy = cy - *sy / 2;
                    845:        }
                    846:
                    847:        c->pan_window = NULL;
                    848:        return (1);
                    849: }
                    850:
                    851: /* Update stored offsets for a window and redraw if necessary. */
                    852: void
                    853: tty_update_window_offset(struct window *w)
                    854: {
                    855:        struct client   *c;
                    856:
                    857:        TAILQ_FOREACH(c, &clients, entry) {
                    858:                if (c->session != NULL && c->session->curw->window == w)
                    859:                        tty_update_client_offset(c);
                    860:        }
                    861: }
                    862:
                    863: /* Update stored offsets for a client and redraw if necessary. */
                    864: void
                    865: tty_update_client_offset(struct client *c)
1.304     nicm      866: {
1.309     nicm      867:        u_int   ox, oy, sx, sy;
1.314     nicm      868:
                    869:        if (~c->flags & CLIENT_TERMINAL)
                    870:                return;
1.304     nicm      871:
1.309     nicm      872:        c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy);
                    873:        if (ox == c->tty.oox &&
                    874:            oy == c->tty.ooy &&
                    875:            sx == c->tty.osx &&
                    876:            sy == c->tty.osy)
                    877:                return;
                    878:
                    879:        log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)",
                    880:            __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy,
                    881:            ox, oy, sx, sy);
                    882:
                    883:        c->tty.oox = ox;
                    884:        c->tty.ooy = oy;
                    885:        c->tty.osx = sx;
                    886:        c->tty.osy = sy;
                    887:
                    888:        c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS);
1.133     nicm      889: }
                    890:
1.1       nicm      891: /*
1.119     nicm      892:  * Is the region large enough to be worth redrawing once later rather than
                    893:  * probably several times now? Currently yes if it is more than 50% of the
                    894:  * pane.
                    895:  */
1.207     nicm      896: static int
1.194     nicm      897: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119     nicm      898: {
1.372   ! nicm      899:        return (ctx->orlower - ctx->orupper >= ctx->sy / 2);
1.119     nicm      900: }
                    901:
                    902: /*
1.176     nicm      903:  * Return if BCE is needed but the terminal doesn't have it - it'll need to be
                    904:  * emulated.
                    905:  */
1.207     nicm      906: static int
1.317     nicm      907: tty_fake_bce(const struct tty *tty, struct window_pane *wp, u_int bg)
1.176     nicm      908: {
                    909:        struct grid_cell        gc;
                    910:
1.210     nicm      911:        if (tty_term_flag(tty->term, TTYC_BCE))
                    912:                return (0);
                    913:
1.176     nicm      914:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.203     nicm      915:        if (wp != NULL)
                    916:                tty_default_colours(&gc, wp);
1.176     nicm      917:
1.310     nicm      918:        if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc.bg))
1.210     nicm      919:                return (1);
                    920:        return (0);
1.176     nicm      921: }
                    922:
                    923: /*
1.1       nicm      924:  * Redraw scroll region using data from screen (already updated). Used when
                    925:  * CSR not supported, or window is a pane that doesn't take up the full
                    926:  * width of the terminal.
                    927:  */
1.207     nicm      928: static void
1.24      nicm      929: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      930: {
1.358     nicm      931:        struct client           *c = tty->client;
1.14      nicm      932:        struct window_pane      *wp = ctx->wp;
1.204     nicm      933:        u_int                    i;
1.1       nicm      934:
                    935:        /*
1.119     nicm      936:         * If region is large, schedule a window redraw. In most cases this is
                    937:         * likely to be followed by some more scrolling.
1.1       nicm      938:         */
1.119     nicm      939:        if (tty_large_region(tty, ctx)) {
1.358     nicm      940:                log_debug("%s: %s, large redraw of %%%u", __func__, c->name, wp->id);
1.1       nicm      941:                wp->flags |= PANE_REDRAW;
                    942:                return;
                    943:        }
                    944:
1.14      nicm      945:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
1.372   ! nicm      946:                for (i = ctx->ocy; i < ctx->sy; i++)
1.309     nicm      947:                        tty_draw_pane(tty, ctx, i);
1.1       nicm      948:        } else {
1.14      nicm      949:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.309     nicm      950:                        tty_draw_pane(tty, ctx, i);
1.1       nicm      951:        }
                    952: }
                    953:
1.309     nicm      954: /* Is this position visible in the pane? */
                    955: static int
                    956: tty_is_visible(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
                    957:     u_int nx, u_int ny)
                    958: {
                    959:        u_int   xoff = ctx->xoff + px, yoff = ctx->yoff + py, lines;
                    960:
                    961:        if (!ctx->bigger)
                    962:                return (1);
                    963:
                    964:        if (status_at_line(tty->client) == 0)
                    965:                lines = status_line_size(tty->client);
                    966:        else
                    967:                lines = 0;
                    968:
1.372   ! nicm      969:        if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx ||
        !           970:            yoff + ny <= ctx->woy || yoff >= lines + ctx->woy + ctx->wsy)
1.309     nicm      971:                return (0);
                    972:        return (1);
                    973: }
                    974:
                    975: /* Clamp line position to visible part of pane. */
                    976: static int
                    977: tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
                    978:     u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry)
                    979: {
                    980:        struct window_pane      *wp = ctx->wp;
                    981:        u_int                    xoff = wp->xoff + px;
                    982:
                    983:        if (!tty_is_visible(tty, ctx, px, py, nx, 1))
                    984:                return (0);
1.372   ! nicm      985:        *ry = ctx->yoff + py - ctx->woy;
1.309     nicm      986:
1.372   ! nicm      987:        if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1.309     nicm      988:                /* All visible. */
                    989:                *i = 0;
1.372   ! nicm      990:                *x = ctx->xoff + px - ctx->wox;
1.309     nicm      991:                *rx = nx;
1.372   ! nicm      992:        } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1.309     nicm      993:                /* Both left and right not visible. */
1.372   ! nicm      994:                *i = ctx->wox;
1.309     nicm      995:                *x = 0;
1.372   ! nicm      996:                *rx = ctx->wsx;
        !           997:        } else if (xoff < ctx->wox) {
1.309     nicm      998:                /* Left not visible. */
1.372   ! nicm      999:                *i = ctx->wox - (ctx->xoff + px);
1.309     nicm     1000:                *x = 0;
                   1001:                *rx = nx - *i;
                   1002:        } else {
                   1003:                /* Right not visible. */
                   1004:                *i = 0;
1.372   ! nicm     1005:                *x = (ctx->xoff + px) - ctx->wox;
        !          1006:                *rx = ctx->wsx - *x;
1.309     nicm     1007:        }
                   1008:        if (*rx > nx)
                   1009:                fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
                   1010:
                   1011:        return (1);
                   1012: }
                   1013:
                   1014: /* Clear a line. */
1.271     nicm     1015: static void
1.317     nicm     1016: tty_clear_line(struct tty *tty, struct window_pane *wp, u_int py, u_int px,
                   1017:     u_int nx, u_int bg)
1.271     nicm     1018: {
1.309     nicm     1019:        struct client   *c = tty->client;
                   1020:
                   1021:        log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
1.271     nicm     1022:
                   1023:        /* Nothing to clear. */
                   1024:        if (nx == 0)
                   1025:                return;
                   1026:
                   1027:        /* If genuine BCE is available, can try escape sequences. */
                   1028:        if (!tty_fake_bce(tty, wp, bg)) {
                   1029:                /* Off the end of the line, use EL if available. */
                   1030:                if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) {
                   1031:                        tty_cursor(tty, px, py);
                   1032:                        tty_putcode(tty, TTYC_EL);
                   1033:                        return;
                   1034:                }
                   1035:
                   1036:                /* At the start of the line. Use EL1. */
                   1037:                if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) {
                   1038:                        tty_cursor(tty, px + nx - 1, py);
                   1039:                        tty_putcode(tty, TTYC_EL1);
                   1040:                        return;
                   1041:                }
                   1042:
                   1043:                /* Section of line. Use ECH if possible. */
                   1044:                if (tty_term_has(tty->term, TTYC_ECH)) {
                   1045:                        tty_cursor(tty, px, py);
                   1046:                        tty_putcode1(tty, TTYC_ECH, nx);
                   1047:                        return;
                   1048:                }
                   1049:        }
                   1050:
                   1051:        /* Couldn't use an escape sequence, use spaces. */
1.272     nicm     1052:        tty_cursor(tty, px, py);
1.271     nicm     1053:        tty_repeat_space(tty, nx);
                   1054: }
                   1055:
1.309     nicm     1056: /* Clear a line, adjusting to visible part of pane. */
                   1057: static void
                   1058: tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py,
                   1059:     u_int px, u_int nx, u_int bg)
                   1060: {
                   1061:        struct client   *c = tty->client;
                   1062:        u_int            i, x, rx, ry;
                   1063:
                   1064:        log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py);
                   1065:
                   1066:        if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry))
                   1067:                tty_clear_line(tty, ctx->wp, ry, x, rx, bg);
                   1068: }
                   1069:
                   1070: /* Clamp area position to visible part of pane. */
                   1071: static int
                   1072: tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
                   1073:     u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
                   1074:     u_int *ry)
                   1075: {
                   1076:        struct window_pane      *wp = ctx->wp;
                   1077:        u_int                    xoff = wp->xoff + px, yoff = wp->yoff + py;
                   1078:
                   1079:        if (!tty_is_visible(tty, ctx, px, py, nx, ny))
                   1080:                return (0);
                   1081:
1.372   ! nicm     1082:        if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) {
1.309     nicm     1083:                /* All visible. */
                   1084:                *i = 0;
1.372   ! nicm     1085:                *x = ctx->xoff + px - ctx->wox;
1.309     nicm     1086:                *rx = nx;
1.372   ! nicm     1087:        } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) {
1.309     nicm     1088:                /* Both left and right not visible. */
1.372   ! nicm     1089:                *i = ctx->wox;
1.309     nicm     1090:                *x = 0;
1.372   ! nicm     1091:                *rx = ctx->wsx;
        !          1092:        } else if (xoff < ctx->wox) {
1.309     nicm     1093:                /* Left not visible. */
1.372   ! nicm     1094:                *i = ctx->wox - (ctx->xoff + px);
1.309     nicm     1095:                *x = 0;
                   1096:                *rx = nx - *i;
                   1097:        } else {
                   1098:                /* Right not visible. */
                   1099:                *i = 0;
1.372   ! nicm     1100:                *x = (ctx->xoff + px) - ctx->wox;
        !          1101:                *rx = ctx->wsx - *x;
1.309     nicm     1102:        }
                   1103:        if (*rx > nx)
                   1104:                fatalx("%s: x too big, %u > %u", __func__, *rx, nx);
                   1105:
1.372   ! nicm     1106:        if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) {
1.309     nicm     1107:                /* All visible. */
                   1108:                *j = 0;
1.372   ! nicm     1109:                *y = ctx->yoff + py - ctx->woy;
1.309     nicm     1110:                *ry = ny;
1.372   ! nicm     1111:        } else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) {
1.327     nicm     1112:                /* Both top and bottom not visible. */
1.372   ! nicm     1113:                *j = ctx->woy;
1.309     nicm     1114:                *y = 0;
1.372   ! nicm     1115:                *ry = ctx->wsy;
        !          1116:        } else if (yoff < ctx->woy) {
1.327     nicm     1117:                /* Top not visible. */
1.372   ! nicm     1118:                *j = ctx->woy - (ctx->yoff + py);
1.309     nicm     1119:                *y = 0;
                   1120:                *ry = ny - *j;
                   1121:        } else {
1.327     nicm     1122:                /* Bottom not visible. */
1.309     nicm     1123:                *j = 0;
1.372   ! nicm     1124:                *y = (ctx->yoff + py) - ctx->woy;
        !          1125:                *ry = ctx->wsy - *y;
1.309     nicm     1126:        }
                   1127:        if (*ry > ny)
                   1128:                fatalx("%s: y too big, %u > %u", __func__, *ry, ny);
                   1129:
                   1130:        return (1);
                   1131: }
                   1132:
                   1133: /* Clear an area, adjusting to visible part of pane. */
1.271     nicm     1134: static void
1.317     nicm     1135: tty_clear_area(struct tty *tty, struct window_pane *wp, u_int py, u_int ny,
                   1136:     u_int px, u_int nx, u_int bg)
1.271     nicm     1137: {
1.309     nicm     1138:        struct client   *c = tty->client;
                   1139:        u_int            yy;
                   1140:        char             tmp[64];
1.271     nicm     1141:
1.309     nicm     1142:        log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py);
1.271     nicm     1143:
                   1144:        /* Nothing to clear. */
                   1145:        if (nx == 0 || ny == 0)
                   1146:                return;
                   1147:
                   1148:        /* If genuine BCE is available, can try escape sequences. */
                   1149:        if (!tty_fake_bce(tty, wp, bg)) {
1.276     nicm     1150:                /* Use ED if clearing off the bottom of the terminal. */
1.271     nicm     1151:                if (px == 0 &&
                   1152:                    px + nx >= tty->sx &&
                   1153:                    py + ny >= tty->sy &&
                   1154:                    tty_term_has(tty->term, TTYC_ED)) {
                   1155:                        tty_cursor(tty, 0, py);
                   1156:                        tty_putcode(tty, TTYC_ED);
1.274     nicm     1157:                        return;
                   1158:                }
                   1159:
                   1160:                /*
1.276     nicm     1161:                 * On VT420 compatible terminals we can use DECFRA if the
                   1162:                 * background colour isn't default (because it doesn't work
                   1163:                 * after SGR 0).
1.274     nicm     1164:                 */
1.359     nicm     1165:                if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) {
1.274     nicm     1166:                        xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x",
                   1167:                            py + 1, px + 1, py + ny, px + nx);
                   1168:                        tty_puts(tty, tmp);
1.276     nicm     1169:                        return;
                   1170:                }
                   1171:
1.280     nicm     1172:                /* Full lines can be scrolled away to clear them. */
                   1173:                if (px == 0 &&
1.281     nicm     1174:                    px + nx >= tty->sx &&
1.280     nicm     1175:                    ny > 2 &&
                   1176:                    tty_term_has(tty->term, TTYC_CSR) &&
                   1177:                    tty_term_has(tty->term, TTYC_INDN)) {
                   1178:                        tty_region(tty, py, py + ny - 1);
                   1179:                        tty_margin_off(tty);
1.284     nicm     1180:                        tty_putcode1(tty, TTYC_INDN, ny);
1.280     nicm     1181:                        return;
                   1182:                }
                   1183:
1.276     nicm     1184:                /*
                   1185:                 * If margins are supported, can just scroll the area off to
                   1186:                 * clear it.
                   1187:                 */
1.277     nicm     1188:                if (nx > 2 &&
                   1189:                    ny > 2 &&
1.280     nicm     1190:                    tty_term_has(tty->term, TTYC_CSR) &&
1.277     nicm     1191:                    tty_use_margin(tty) &&
                   1192:                    tty_term_has(tty->term, TTYC_INDN)) {
1.276     nicm     1193:                        tty_region(tty, py, py + ny - 1);
                   1194:                        tty_margin(tty, px, px + nx - 1);
1.284     nicm     1195:                        tty_putcode1(tty, TTYC_INDN, ny);
1.271     nicm     1196:                        return;
                   1197:                }
                   1198:        }
                   1199:
                   1200:        /* Couldn't use an escape sequence, loop over the lines. */
                   1201:        for (yy = py; yy < py + ny; yy++)
                   1202:                tty_clear_line(tty, wp, yy, px, nx, bg);
                   1203: }
                   1204:
1.309     nicm     1205: /* Clear an area in a pane. */
                   1206: static void
                   1207: tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py,
                   1208:     u_int ny, u_int px, u_int nx, u_int bg)
                   1209: {
                   1210:        u_int   i, j, x, y, rx, ry;
                   1211:
                   1212:        if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry))
                   1213:                tty_clear_area(tty, ctx->wp, y, ry, x, rx, bg);
                   1214: }
                   1215:
                   1216: static void
                   1217: tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py)
1.176     nicm     1218: {
1.309     nicm     1219:        struct window_pane      *wp = ctx->wp;
                   1220:        struct screen           *s = wp->screen;
1.372   ! nicm     1221:        u_int                    nx = ctx->sx, i, x, rx, ry;
1.309     nicm     1222:
                   1223:        log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger);
                   1224:
                   1225:        if (!ctx->bigger) {
                   1226:                tty_draw_line(tty, wp, s, 0, py, nx, ctx->xoff, ctx->yoff + py);
                   1227:                return;
                   1228:        }
                   1229:        if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry))
                   1230:                tty_draw_line(tty, wp, s, i, py, rx, x, ry);
1.176     nicm     1231: }
                   1232:
1.298     nicm     1233: static const struct grid_cell *
                   1234: tty_check_codeset(struct tty *tty, const struct grid_cell *gc)
                   1235: {
                   1236:        static struct grid_cell new;
                   1237:        u_int                   n;
                   1238:
                   1239:        /* Characters less than 0x7f are always fine, no matter what. */
                   1240:        if (gc->data.size == 1 && *gc->data.data < 0x7f)
                   1241:                return (gc);
                   1242:
                   1243:        /* UTF-8 terminal and a UTF-8 character - fine. */
1.359     nicm     1244:        if (tty->client->flags & CLIENT_UTF8)
1.298     nicm     1245:                return (gc);
                   1246:
                   1247:        /* Replace by the right number of underscores. */
                   1248:        n = gc->data.width;
                   1249:        if (n > UTF8_SIZE)
                   1250:                n = UTF8_SIZE;
                   1251:        memcpy(&new, gc, sizeof new);
                   1252:        new.data.size = n;
                   1253:        memset(new.data.data, '_', n);
                   1254:        return (&new);
                   1255: }
                   1256:
1.346     nicm     1257: static int
                   1258: tty_check_overlay(struct tty *tty, u_int px, u_int py)
                   1259: {
                   1260:        struct client   *c = tty->client;
                   1261:
                   1262:        if (c->overlay_check == NULL)
                   1263:                return (1);
                   1264:        return (c->overlay_check(c, px, py));
                   1265: }
                   1266:
1.176     nicm     1267: void
1.317     nicm     1268: tty_draw_line(struct tty *tty, struct window_pane *wp, struct screen *s,
                   1269:     u_int px, u_int py, u_int nx, u_int atx, u_int aty)
1.1       nicm     1270: {
1.297     nicm     1271:        struct grid             *gd = s->grid;
1.250     nicm     1272:        struct grid_cell         gc, last;
1.298     nicm     1273:        const struct grid_cell  *gcp;
1.309     nicm     1274:        struct grid_line        *gl;
                   1275:        u_int                    i, j, ux, sx, width;
1.329     nicm     1276:        int                      flags, cleared = 0, wrapped = 0;
1.250     nicm     1277:        char                     buf[512];
1.315     nicm     1278:        size_t                   len;
1.303     nicm     1279:        u_int                    cellsize;
1.1       nicm     1280:
1.309     nicm     1281:        log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__,
                   1282:            px, py, nx, atx, aty);
                   1283:
                   1284:        /*
                   1285:         * py is the line in the screen to draw.
                   1286:         * px is the start x and nx is the width to draw.
                   1287:         * atx,aty is the line on the terminal to draw it.
                   1288:         */
                   1289:
1.259     nicm     1290:        flags = (tty->flags & TTY_NOCURSOR);
1.181     nicm     1291:        tty->flags |= TTY_NOCURSOR;
                   1292:        tty_update_mode(tty, tty->mode, s);
1.35      nicm     1293:
1.214     nicm     1294:        tty_region_off(tty);
                   1295:        tty_margin_off(tty);
                   1296:
1.273     nicm     1297:        /*
                   1298:         * Clamp the width to cellsize - note this is not cellused, because
                   1299:         * there may be empty background cells after it (from BCE).
                   1300:         */
1.1       nicm     1301:        sx = screen_size_x(s);
1.309     nicm     1302:        if (nx > sx)
                   1303:                nx = sx;
1.303     nicm     1304:        cellsize = grid_get_line(gd, gd->hsize + py)->cellsize;
                   1305:        if (sx > cellsize)
                   1306:                sx = cellsize;
1.1       nicm     1307:        if (sx > tty->sx)
                   1308:                sx = tty->sx;
1.309     nicm     1309:        if (sx > nx)
                   1310:                sx = nx;
1.292     nicm     1311:        ux = 0;
1.1       nicm     1312:
1.309     nicm     1313:        if (py == 0)
                   1314:                gl = NULL;
                   1315:        else
                   1316:                gl = grid_get_line(gd, gd->hsize + py - 1);
1.268     nicm     1317:        if (wp == NULL ||
1.309     nicm     1318:            gl == NULL ||
                   1319:            (~gl->flags & GRID_LINE_WRAPPED) ||
                   1320:            atx != 0 ||
1.268     nicm     1321:            tty->cx < tty->sx ||
1.309     nicm     1322:            nx < tty->sx) {
                   1323:                if (nx < tty->sx &&
                   1324:                    atx == 0 &&
                   1325:                    px + sx != nx &&
1.268     nicm     1326:                    tty_term_has(tty->term, TTYC_EL1) &&
                   1327:                    !tty_fake_bce(tty, wp, 8)) {
                   1328:                        tty_default_attributes(tty, wp, 8);
1.309     nicm     1329:                        tty_cursor(tty, nx - 1, aty);
1.268     nicm     1330:                        tty_putcode(tty, TTYC_EL1);
                   1331:                        cleared = 1;
                   1332:                }
1.329     nicm     1333:        } else {
1.309     nicm     1334:                log_debug("%s: wrapped line %u", __func__, aty);
1.329     nicm     1335:                wrapped = 1;
                   1336:        }
1.250     nicm     1337:
                   1338:        memcpy(&last, &grid_default_cell, sizeof last);
                   1339:        len = 0;
                   1340:        width = 0;
1.46      nicm     1341:
1.1       nicm     1342:        for (i = 0; i < sx; i++) {
1.309     nicm     1343:                grid_view_get_cell(gd, px + i, py, &gc);
1.298     nicm     1344:                gcp = tty_check_codeset(tty, &gc);
1.250     nicm     1345:                if (len != 0 &&
1.346     nicm     1346:                    (!tty_check_overlay(tty, atx + ux + width, aty) ||
                   1347:                    (gcp->attr & GRID_ATTR_CHARSET) ||
1.298     nicm     1348:                    gcp->flags != last.flags ||
                   1349:                    gcp->attr != last.attr ||
                   1350:                    gcp->fg != last.fg ||
                   1351:                    gcp->bg != last.bg ||
1.328     nicm     1352:                    gcp->us != last.us ||
1.315     nicm     1353:                    ux + width + gcp->data.width > nx ||
1.298     nicm     1354:                    (sizeof buf) - len < gcp->data.size)) {
1.316     nicm     1355:                        tty_attributes(tty, &last, wp);
1.315     nicm     1356:                        if (last.flags & GRID_FLAG_CLEARED) {
                   1357:                                log_debug("%s: %zu cleared", __func__, len);
                   1358:                                tty_clear_line(tty, wp, aty, atx + ux, width,
                   1359:                                    last.bg);
                   1360:                        } else {
1.329     nicm     1361:                                if (!wrapped || atx != 0 || ux != 0)
                   1362:                                        tty_cursor(tty, atx + ux, aty);
1.315     nicm     1363:                                tty_putn(tty, buf, len, width);
                   1364:                        }
1.292     nicm     1365:                        ux += width;
1.250     nicm     1366:
                   1367:                        len = 0;
                   1368:                        width = 0;
1.329     nicm     1369:                        wrapped = 0;
1.250     nicm     1370:                }
                   1371:
1.298     nicm     1372:                if (gcp->flags & GRID_FLAG_SELECTED)
1.299     nicm     1373:                        screen_select_cell(s, &last, gcp);
1.250     nicm     1374:                else
1.299     nicm     1375:                        memcpy(&last, gcp, sizeof last);
1.346     nicm     1376:                if (!tty_check_overlay(tty, atx + ux, aty))
                   1377:                        ux += gcp->data.width;
                   1378:                else if (ux + gcp->data.width > nx) {
1.299     nicm     1379:                        tty_attributes(tty, &last, wp);
1.315     nicm     1380:                        tty_cursor(tty, atx + ux, aty);
1.298     nicm     1381:                        for (j = 0; j < gcp->data.width; j++) {
1.309     nicm     1382:                                if (ux + j > nx)
1.297     nicm     1383:                                        break;
                   1384:                                tty_putc(tty, ' ');
                   1385:                                ux++;
                   1386:                        }
1.299     nicm     1387:                } else if (gcp->attr & GRID_ATTR_CHARSET) {
                   1388:                        tty_attributes(tty, &last, wp);
1.315     nicm     1389:                        tty_cursor(tty, atx + ux, aty);
1.299     nicm     1390:                        for (j = 0; j < gcp->data.size; j++)
                   1391:                                tty_putc(tty, gcp->data.data[j]);
1.346     nicm     1392:                        ux += gcp->data.width;
1.299     nicm     1393:                } else {
1.298     nicm     1394:                        memcpy(buf + len, gcp->data.data, gcp->data.size);
                   1395:                        len += gcp->data.size;
                   1396:                        width += gcp->data.width;
1.250     nicm     1397:                }
                   1398:        }
1.315     nicm     1399:        if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1.316     nicm     1400:                tty_attributes(tty, &last, wp);
1.315     nicm     1401:                if (last.flags & GRID_FLAG_CLEARED) {
                   1402:                        log_debug("%s: %zu cleared (end)", __func__, len);
                   1403:                        tty_clear_line(tty, wp, aty, atx + ux, width, last.bg);
                   1404:                } else {
1.329     nicm     1405:                        if (!wrapped || atx != 0 || ux != 0)
                   1406:                                tty_cursor(tty, atx + ux, aty);
1.291     nicm     1407:                        tty_putn(tty, buf, len, width);
                   1408:                }
1.315     nicm     1409:                ux += width;
1.1       nicm     1410:        }
                   1411:
1.309     nicm     1412:        if (!cleared && ux < nx) {
1.315     nicm     1413:                log_debug("%s: %u to end of line (%zu cleared)", __func__,
                   1414:                    nx - ux, len);
1.210     nicm     1415:                tty_default_attributes(tty, wp, 8);
1.309     nicm     1416:                tty_clear_line(tty, wp, aty, atx + ux, nx - ux, 8);
1.35      nicm     1417:        }
1.1       nicm     1418:
1.181     nicm     1419:        tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107     nicm     1420:        tty_update_mode(tty, tty->mode, s);
1.15      nicm     1421: }
                   1422:
1.348     nicm     1423: void
                   1424: tty_sync_start(struct tty *tty)
                   1425: {
1.361     nicm     1426:        if (tty->flags & TTY_BLOCK)
                   1427:                return;
                   1428:        if (tty->flags & TTY_SYNCING)
                   1429:                return;
                   1430:        tty->flags |= TTY_SYNCING;
                   1431:
                   1432:        if (tty_term_has(tty->term, TTYC_SYNC)) {
                   1433:                log_debug("%s sync start", tty->client->name);
1.360     nicm     1434:                tty_putcode1(tty, TTYC_SYNC, 1);
1.357     nicm     1435:        }
1.348     nicm     1436: }
                   1437:
                   1438: void
                   1439: tty_sync_end(struct tty *tty)
                   1440: {
1.361     nicm     1441:        if (tty->flags & TTY_BLOCK)
                   1442:                return;
                   1443:        if (~tty->flags & TTY_SYNCING)
                   1444:                return;
                   1445:        tty->flags &= ~TTY_SYNCING;
                   1446:
                   1447:        if (tty_term_has(tty->term, TTYC_SYNC)) {
                   1448:                log_debug("%s sync end", tty->client->name);
1.360     nicm     1449:                tty_putcode1(tty, TTYC_SYNC, 2);
1.357     nicm     1450:        }
1.348     nicm     1451: }
                   1452:
1.201     nicm     1453: static int
1.182     nicm     1454: tty_client_ready(struct client *c, struct window_pane *wp)
                   1455: {
                   1456:        if (c->session == NULL || c->tty.term == NULL)
                   1457:                return (0);
1.305     nicm     1458:        if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1.182     nicm     1459:                return (0);
                   1460:        if (c->tty.flags & TTY_FREEZE)
                   1461:                return (0);
                   1462:        if (c->session->curw->window != wp->window)
                   1463:                return (0);
1.309     nicm     1464:        if (wp->layout_cell == NULL)
                   1465:                return (0);
1.182     nicm     1466:        return (1);
                   1467: }
                   1468:
1.15      nicm     1469: void
1.182     nicm     1470: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
                   1471:     struct tty_ctx *ctx)
1.15      nicm     1472: {
                   1473:        struct window_pane      *wp = ctx->wp;
                   1474:        struct client           *c;
                   1475:
                   1476:        if (wp == NULL)
                   1477:                return;
1.309     nicm     1478:        if (wp->flags & (PANE_REDRAW|PANE_DROP))
1.15      nicm     1479:                return;
                   1480:
1.178     nicm     1481:        TAILQ_FOREACH(c, &clients, entry) {
1.182     nicm     1482:                if (!tty_client_ready(c, wp))
1.15      nicm     1483:                        continue;
1.358     nicm     1484:                if (c->flags & CLIENT_REDRAWPANES) {
                   1485:                        /*
                   1486:                         * Redraw is already deferred to redraw another pane -
                   1487:                         * redraw this one also when that happens.
                   1488:                         */
1.363     nicm     1489:                        log_debug("adding %%%u to deferred redraw", wp->id);
1.358     nicm     1490:                        wp->flags |= PANE_REDRAW;
                   1491:                        break;
                   1492:                }
1.15      nicm     1493:
1.372   ! nicm     1494:                ctx->bigger = tty_window_offset(&c->tty, &ctx->wox, &ctx->woy,
        !          1495:                    &ctx->wsx, &ctx->wsy);
1.309     nicm     1496:
1.139     nicm     1497:                ctx->xoff = wp->xoff;
                   1498:                ctx->yoff = wp->yoff;
1.309     nicm     1499:
1.139     nicm     1500:                if (status_at_line(c) == 0)
1.309     nicm     1501:                        ctx->yoff += status_line_size(c);
1.113     nicm     1502:
1.139     nicm     1503:                cmdfn(&c->tty, ctx);
1.1       nicm     1504:        }
                   1505: }
                   1506:
                   1507: void
1.24      nicm     1508: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1509: {
1.77      nicm     1510:        struct window_pane      *wp = ctx->wp;
1.1       nicm     1511:
1.309     nicm     1512:        if (ctx->bigger ||
1.372   ! nicm     1513:            !tty_full_width(tty, ctx) ||
1.210     nicm     1514:            tty_fake_bce(tty, wp, ctx->bg) ||
                   1515:            (!tty_term_has(tty->term, TTYC_ICH) &&
                   1516:            !tty_term_has(tty->term, TTYC_ICH1))) {
1.309     nicm     1517:                tty_draw_pane(tty, ctx, ctx->ocy);
1.1       nicm     1518:                return;
                   1519:        }
                   1520:
1.210     nicm     1521:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1522:
1.77      nicm     1523:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm     1524:
1.206     nicm     1525:        tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1       nicm     1526: }
                   1527:
                   1528: void
1.24      nicm     1529: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1530: {
1.77      nicm     1531:        struct window_pane      *wp = ctx->wp;
1.1       nicm     1532:
1.309     nicm     1533:        if (ctx->bigger ||
1.372   ! nicm     1534:            !tty_full_width(tty, ctx) ||
1.210     nicm     1535:            tty_fake_bce(tty, wp, ctx->bg) ||
1.26      nicm     1536:            (!tty_term_has(tty->term, TTYC_DCH) &&
                   1537:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.309     nicm     1538:                tty_draw_pane(tty, ctx, ctx->ocy);
1.1       nicm     1539:                return;
                   1540:        }
                   1541:
1.210     nicm     1542:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1543:
1.77      nicm     1544:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm     1545:
1.206     nicm     1546:        tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146     nicm     1547: }
                   1548:
                   1549: void
                   1550: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
                   1551: {
1.309     nicm     1552:        if (ctx->bigger) {
                   1553:                tty_draw_pane(tty, ctx, ctx->ocy);
                   1554:                return;
                   1555:        }
                   1556:
1.275     nicm     1557:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.146     nicm     1558:
                   1559:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                   1560:
1.210     nicm     1561:        if (tty_term_has(tty->term, TTYC_ECH) &&
1.262     nicm     1562:            !tty_fake_bce(tty, ctx->wp, 8))
1.146     nicm     1563:                tty_putcode1(tty, TTYC_ECH, ctx->num);
1.257     nicm     1564:        else
                   1565:                tty_repeat_space(tty, ctx->num);
1.1       nicm     1566: }
                   1567:
                   1568: void
1.24      nicm     1569: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1570: {
1.309     nicm     1571:        if (ctx->bigger ||
1.372   ! nicm     1572:            !tty_full_width(tty, ctx) ||
1.210     nicm     1573:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77      nicm     1574:            !tty_term_has(tty->term, TTYC_CSR) ||
1.307     nicm     1575:            !tty_term_has(tty->term, TTYC_IL1) ||
                   1576:            ctx->wp->sx == 1 ||
                   1577:            ctx->wp->sy == 1) {
1.14      nicm     1578:                tty_redraw_region(tty, ctx);
1.1       nicm     1579:                return;
                   1580:        }
                   1581:
1.210     nicm     1582:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm     1583:
1.77      nicm     1584:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm     1585:        tty_margin_off(tty);
1.77      nicm     1586:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1587:
1.12      nicm     1588:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.279     nicm     1589:        tty->cx = tty->cy = UINT_MAX;
1.1       nicm     1590: }
                   1591:
                   1592: void
1.24      nicm     1593: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1594: {
1.309     nicm     1595:        if (ctx->bigger ||
1.372   ! nicm     1596:            !tty_full_width(tty, ctx) ||
1.210     nicm     1597:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72      nicm     1598:            !tty_term_has(tty->term, TTYC_CSR) ||
1.307     nicm     1599:            !tty_term_has(tty->term, TTYC_DL1) ||
                   1600:            ctx->wp->sx == 1 ||
                   1601:            ctx->wp->sy == 1) {
1.14      nicm     1602:                tty_redraw_region(tty, ctx);
1.1       nicm     1603:                return;
                   1604:        }
                   1605:
1.210     nicm     1606:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm     1607:
1.77      nicm     1608:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm     1609:        tty_margin_off(tty);
1.77      nicm     1610:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1611:
1.12      nicm     1612:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.279     nicm     1613:        tty->cx = tty->cy = UINT_MAX;
1.1       nicm     1614: }
                   1615:
                   1616: void
1.24      nicm     1617: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1618: {
1.77      nicm     1619:        struct window_pane      *wp = ctx->wp;
1.1       nicm     1620:
1.210     nicm     1621:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1622:
1.372   ! nicm     1623:        tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1.1       nicm     1624: }
                   1625:
                   1626: void
1.24      nicm     1627: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1628: {
1.77      nicm     1629:        struct window_pane      *wp = ctx->wp;
1.372   ! nicm     1630:        u_int                    nx = ctx->sx - ctx->ocx;
1.1       nicm     1631:
1.210     nicm     1632:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1633:
1.309     nicm     1634:        tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1.1       nicm     1635: }
                   1636:
                   1637: void
1.24      nicm     1638: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1639: {
1.210     nicm     1640:        struct window_pane      *wp = ctx->wp;
                   1641:
                   1642:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1643:
1.309     nicm     1644:        tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1.1       nicm     1645: }
                   1646:
                   1647: void
1.24      nicm     1648: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1649: {
1.278     nicm     1650:        struct window_pane      *wp = ctx->wp;
                   1651:
1.56      nicm     1652:        if (ctx->ocy != ctx->orupper)
                   1653:                return;
                   1654:
1.309     nicm     1655:        if (ctx->bigger ||
1.372   ! nicm     1656:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.278     nicm     1657:            tty_fake_bce(tty, wp, 8) ||
1.70      nicm     1658:            !tty_term_has(tty->term, TTYC_CSR) ||
1.331     nicm     1659:            (!tty_term_has(tty->term, TTYC_RI) &&
                   1660:            !tty_term_has(tty->term, TTYC_RIN)) ||
1.307     nicm     1661:            ctx->wp->sx == 1 ||
                   1662:            ctx->wp->sy == 1) {
1.14      nicm     1663:                tty_redraw_region(tty, ctx);
1.1       nicm     1664:                return;
                   1665:        }
                   1666:
1.278     nicm     1667:        tty_default_attributes(tty, wp, ctx->bg);
1.77      nicm     1668:
1.56      nicm     1669:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.331     nicm     1670:        tty_margin_pane(tty, ctx);
1.56      nicm     1671:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm     1672:
1.331     nicm     1673:        if (tty_term_has(tty->term, TTYC_RI))
                   1674:                tty_putcode(tty, TTYC_RI);
                   1675:        else
                   1676:                tty_putcode1(tty, TTYC_RIN, 1);
1.1       nicm     1677: }
                   1678:
                   1679: void
1.24      nicm     1680: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1681: {
1.77      nicm     1682:        struct window_pane      *wp = ctx->wp;
1.1       nicm     1683:
1.56      nicm     1684:        if (ctx->ocy != ctx->orlower)
                   1685:                return;
                   1686:
1.309     nicm     1687:        if (ctx->bigger ||
1.372   ! nicm     1688:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262     nicm     1689:            tty_fake_bce(tty, wp, 8) ||
1.307     nicm     1690:            !tty_term_has(tty->term, TTYC_CSR) ||
                   1691:            wp->sx == 1 ||
                   1692:            wp->sy == 1) {
1.236     nicm     1693:                tty_redraw_region(tty, ctx);
1.1       nicm     1694:                return;
                   1695:        }
1.52      nicm     1696:
1.278     nicm     1697:        tty_default_attributes(tty, wp, ctx->bg);
1.77      nicm     1698:
1.56      nicm     1699:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212     nicm     1700:        tty_margin_pane(tty, ctx);
                   1701:
                   1702:        /*
1.301     nicm     1703:         * If we want to wrap a pane while using margins, the cursor needs to
                   1704:         * be exactly on the right of the region. If the cursor is entirely off
                   1705:         * the edge - move it back to the right. Some terminals are funny about
                   1706:         * this and insert extra spaces, so only use the right if margins are
                   1707:         * enabled.
1.212     nicm     1708:         */
1.301     nicm     1709:        if (ctx->xoff + ctx->ocx > tty->rright) {
                   1710:                if (!tty_use_margin(tty))
                   1711:                        tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
                   1712:                else
                   1713:                        tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
                   1714:        } else
1.212     nicm     1715:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm     1716:
1.56      nicm     1717:        tty_putc(tty, '\n');
1.240     nicm     1718: }
                   1719:
                   1720: void
                   1721: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
                   1722: {
                   1723:        struct window_pane      *wp = ctx->wp;
1.284     nicm     1724:        u_int                    i;
1.240     nicm     1725:
1.309     nicm     1726:        if (ctx->bigger ||
1.372   ! nicm     1727:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.262     nicm     1728:            tty_fake_bce(tty, wp, 8) ||
1.307     nicm     1729:            !tty_term_has(tty->term, TTYC_CSR) ||
                   1730:            wp->sx == 1 ||
                   1731:            wp->sy == 1) {
1.240     nicm     1732:                tty_redraw_region(tty, ctx);
                   1733:                return;
                   1734:        }
                   1735:
1.278     nicm     1736:        tty_default_attributes(tty, wp, ctx->bg);
1.240     nicm     1737:
                   1738:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                   1739:        tty_margin_pane(tty, ctx);
                   1740:
1.284     nicm     1741:        if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.301     nicm     1742:                if (!tty_use_margin(tty))
                   1743:                        tty_cursor(tty, 0, tty->rlower);
                   1744:                else
                   1745:                        tty_cursor(tty, tty->rright, tty->rlower);
1.284     nicm     1746:                for (i = 0; i < ctx->num; i++)
1.240     nicm     1747:                        tty_putc(tty, '\n');
1.301     nicm     1748:        } else {
1.353     nicm     1749:                if (tty->cy == UINT_MAX)
                   1750:                        tty_cursor(tty, 0, 0);
                   1751:                else
                   1752:                        tty_cursor(tty, 0, tty->cy);
1.284     nicm     1753:                tty_putcode1(tty, TTYC_INDN, ctx->num);
1.331     nicm     1754:        }
                   1755: }
                   1756:
                   1757: void
                   1758: tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
                   1759: {
                   1760:        struct window_pane      *wp = ctx->wp;
                   1761:        u_int                    i;
                   1762:
                   1763:        if (ctx->bigger ||
1.372   ! nicm     1764:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.331     nicm     1765:            tty_fake_bce(tty, wp, 8) ||
                   1766:            !tty_term_has(tty->term, TTYC_CSR) ||
                   1767:            (!tty_term_has(tty->term, TTYC_RI) &&
                   1768:            !tty_term_has(tty->term, TTYC_RIN)) ||
                   1769:            wp->sx == 1 ||
                   1770:            wp->sy == 1) {
                   1771:                tty_redraw_region(tty, ctx);
                   1772:                return;
                   1773:        }
                   1774:
                   1775:        tty_default_attributes(tty, wp, ctx->bg);
                   1776:
                   1777:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                   1778:        tty_margin_pane(tty, ctx);
                   1779:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
                   1780:
                   1781:        if (tty_term_has(tty->term, TTYC_RIN))
                   1782:                tty_putcode1(tty, TTYC_RIN, ctx->num);
                   1783:        else {
                   1784:                for (i = 0; i < ctx->num; i++)
                   1785:                        tty_putcode(tty, TTYC_RI);
1.301     nicm     1786:        }
1.1       nicm     1787: }
                   1788:
                   1789: void
1.24      nicm     1790: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1791: {
1.77      nicm     1792:        struct window_pane      *wp = ctx->wp;
1.271     nicm     1793:        u_int                    px, py, nx, ny;
1.1       nicm     1794:
1.210     nicm     1795:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1796:
1.372   ! nicm     1797:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1798:        tty_margin_off(tty);
1.39      nicm     1799:
1.309     nicm     1800:        px = 0;
1.372   ! nicm     1801:        nx = ctx->sx;
1.309     nicm     1802:        py = ctx->ocy + 1;
1.372   ! nicm     1803:        ny = ctx->sy - ctx->ocy - 1;
1.271     nicm     1804:
1.309     nicm     1805:        tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271     nicm     1806:
1.309     nicm     1807:        px = ctx->ocx;
1.372   ! nicm     1808:        nx = ctx->sx - ctx->ocx;
1.309     nicm     1809:        py = ctx->ocy;
1.271     nicm     1810:
1.309     nicm     1811:        tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1       nicm     1812: }
                   1813:
                   1814: void
1.24      nicm     1815: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1816: {
1.77      nicm     1817:        struct window_pane      *wp = ctx->wp;
1.271     nicm     1818:        u_int                    px, py, nx, ny;
1.1       nicm     1819:
1.227     nicm     1820:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1821:
1.372   ! nicm     1822:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1823:        tty_margin_off(tty);
1.39      nicm     1824:
1.309     nicm     1825:        px = 0;
1.372   ! nicm     1826:        nx = ctx->sx;
1.309     nicm     1827:        py = 0;
1.319     nicm     1828:        ny = ctx->ocy;
1.271     nicm     1829:
1.309     nicm     1830:        tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271     nicm     1831:
1.309     nicm     1832:        px = 0;
1.271     nicm     1833:        nx = ctx->ocx + 1;
1.309     nicm     1834:        py = ctx->ocy;
1.271     nicm     1835:
1.309     nicm     1836:        tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1       nicm     1837: }
                   1838:
                   1839: void
1.24      nicm     1840: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1841: {
1.77      nicm     1842:        struct window_pane      *wp = ctx->wp;
1.271     nicm     1843:        u_int                    px, py, nx, ny;
1.1       nicm     1844:
1.210     nicm     1845:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1846:
1.372   ! nicm     1847:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1848:        tty_margin_off(tty);
1.39      nicm     1849:
1.309     nicm     1850:        px = 0;
1.372   ! nicm     1851:        nx = ctx->sx;
1.309     nicm     1852:        py = 0;
1.372   ! nicm     1853:        ny = ctx->sy;
1.271     nicm     1854:
1.309     nicm     1855:        tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.4       nicm     1856: }
                   1857:
                   1858: void
1.24      nicm     1859: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm     1860: {
1.12      nicm     1861:        struct window_pane      *wp = ctx->wp;
                   1862:        u_int                    i, j;
1.4       nicm     1863:
1.309     nicm     1864:        if (ctx->bigger) {
                   1865:                wp->flags |= PANE_REDRAW;
                   1866:                return;
                   1867:        }
                   1868:
1.176     nicm     1869:        tty_attributes(tty, &grid_default_cell, wp);
1.4       nicm     1870:
1.372   ! nicm     1871:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1872:        tty_margin_off(tty);
1.4       nicm     1873:
1.372   ! nicm     1874:        for (j = 0; j < ctx->sy; j++) {
1.41      nicm     1875:                tty_cursor_pane(tty, ctx, 0, j);
1.372   ! nicm     1876:                for (i = 0; i < ctx->sx; i++)
1.4       nicm     1877:                        tty_putc(tty, 'E');
1.1       nicm     1878:        }
                   1879: }
                   1880:
                   1881: void
1.24      nicm     1882: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1883: {
1.309     nicm     1884:        if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1))
                   1885:                return;
                   1886:
1.372   ! nicm     1887:        if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
1.306     nicm     1888:            ctx->ocy == ctx->orlower &&
1.372   ! nicm     1889:            tty_full_width(tty, ctx))
1.306     nicm     1890:                tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46      nicm     1891:
1.306     nicm     1892:        tty_margin_off(tty);
1.253     nicm     1893:        tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1894:
1.237     nicm     1895:        tty_cell(tty, ctx->cell, ctx->wp);
1.239     nicm     1896: }
                   1897:
                   1898: void
                   1899: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
                   1900: {
1.309     nicm     1901:        struct window_pane      *wp = ctx->wp;
                   1902:
                   1903:        if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
                   1904:                return;
                   1905:
                   1906:        if (ctx->bigger &&
1.372   ! nicm     1907:            (ctx->xoff + ctx->ocx < ctx->wox ||
        !          1908:            ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
1.309     nicm     1909:                if (!ctx->wrapped ||
1.372   ! nicm     1910:                    !tty_full_width(tty, ctx) ||
1.359     nicm     1911:                    (tty->term->flags & TERM_NOXENL) ||
1.309     nicm     1912:                    ctx->xoff + ctx->ocx != 0 ||
                   1913:                    ctx->yoff + ctx->ocy != tty->cy + 1 ||
                   1914:                    tty->cx < tty->sx ||
                   1915:                    tty->cy == tty->rlower)
                   1916:                        tty_draw_pane(tty, ctx, ctx->ocy);
                   1917:                else
                   1918:                        wp->flags |= PANE_REDRAW;
                   1919:                return;
                   1920:        }
                   1921:
1.306     nicm     1922:        tty_margin_off(tty);
1.253     nicm     1923:        tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239     nicm     1924:
                   1925:        tty_attributes(tty, ctx->cell, ctx->wp);
                   1926:        tty_putn(tty, ctx->ptr, ctx->num, ctx->num);
1.106     nicm     1927: }
                   1928:
                   1929: void
                   1930: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
                   1931: {
                   1932:        char    *buf;
                   1933:        size_t   off;
                   1934:
                   1935:        if (!tty_term_has(tty->term, TTYC_MS))
                   1936:                return;
                   1937:
                   1938:        off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
                   1939:        buf = xmalloc(off);
                   1940:
                   1941:        b64_ntop(ctx->ptr, ctx->num, buf, off);
                   1942:        tty_putcode_ptr2(tty, TTYC_MS, "", buf);
                   1943:
1.138     nicm     1944:        free(buf);
1.100     nicm     1945: }
                   1946:
                   1947: void
                   1948: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
                   1949: {
1.256     nicm     1950:        tty_add(tty, ctx->ptr, ctx->num);
1.242     nicm     1951:        tty_invalidate(tty);
1.348     nicm     1952: }
                   1953:
                   1954: void
                   1955: tty_cmd_syncstart(struct tty *tty, __unused const struct tty_ctx *ctx)
                   1956: {
                   1957:        tty_sync_start(tty);
1.1       nicm     1958: }
                   1959:
1.207     nicm     1960: static void
1.317     nicm     1961: tty_cell(struct tty *tty, const struct grid_cell *gc, struct window_pane *wp)
1.1       nicm     1962: {
1.298     nicm     1963:        const struct grid_cell  *gcp;
1.1       nicm     1964:
                   1965:        /* Skip last character if terminal is stupid. */
1.359     nicm     1966:        if ((tty->term->flags & TERM_NOXENL) &&
1.211     nicm     1967:            tty->cy == tty->sy - 1 &&
                   1968:            tty->cx == tty->sx - 1)
1.1       nicm     1969:                return;
                   1970:
                   1971:        /* If this is a padding character, do nothing. */
                   1972:        if (gc->flags & GRID_FLAG_PADDING)
                   1973:                return;
                   1974:
                   1975:        /* Set the attributes. */
1.176     nicm     1976:        tty_attributes(tty, gc, wp);
1.1       nicm     1977:
1.147     nicm     1978:        /* Get the cell and if ASCII write with putc to do ACS translation. */
1.298     nicm     1979:        gcp = tty_check_codeset(tty, gc);
                   1980:        if (gcp->data.size == 1) {
                   1981:                if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
1.1       nicm     1982:                        return;
1.298     nicm     1983:                tty_putc(tty, *gcp->data.data);
1.1       nicm     1984:                return;
                   1985:        }
                   1986:
1.147     nicm     1987:        /* Write the data. */
1.298     nicm     1988:        tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
1.1       nicm     1989: }
                   1990:
                   1991: void
                   1992: tty_reset(struct tty *tty)
                   1993: {
                   1994:        struct grid_cell        *gc = &tty->cell;
                   1995:
1.228     nicm     1996:        if (!grid_cells_equal(gc, &grid_default_cell)) {
1.285     nicm     1997:                if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.228     nicm     1998:                        tty_putcode(tty, TTYC_RMACS);
                   1999:                tty_putcode(tty, TTYC_SGR0);
                   2000:                memcpy(gc, &grid_default_cell, sizeof *gc);
                   2001:        }
1.1       nicm     2002:
1.228     nicm     2003:        memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
                   2004:        tty->last_wp = -1;
1.242     nicm     2005: }
                   2006:
                   2007: static void
                   2008: tty_invalidate(struct tty *tty)
                   2009: {
                   2010:        memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
                   2011:
                   2012:        memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
                   2013:        tty->last_wp = -1;
                   2014:
                   2015:        tty->cx = tty->cy = UINT_MAX;
                   2016:
                   2017:        tty->rupper = tty->rleft = UINT_MAX;
                   2018:        tty->rlower = tty->rright = UINT_MAX;
                   2019:
                   2020:        if (tty->flags & TTY_STARTED) {
1.330     nicm     2021:                if (tty_use_margin(tty))
1.365     nicm     2022:                        tty_putcode(tty, TTYC_ENMG);
1.242     nicm     2023:                tty_putcode(tty, TTYC_SGR0);
                   2024:
                   2025:                tty->mode = ALL_MODES;
                   2026:                tty_update_mode(tty, MODE_CURSOR, NULL);
                   2027:
                   2028:                tty_cursor(tty, 0, 0);
                   2029:                tty_region_off(tty);
                   2030:                tty_margin_off(tty);
                   2031:        } else
                   2032:                tty->mode = MODE_CURSOR;
1.1       nicm     2033: }
                   2034:
1.214     nicm     2035: /* Turn off margin. */
                   2036: void
                   2037: tty_region_off(struct tty *tty)
                   2038: {
                   2039:        tty_region(tty, 0, tty->sy - 1);
                   2040: }
                   2041:
1.40      nicm     2042: /* Set region inside pane. */
1.208     nicm     2043: static void
1.196     nicm     2044: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
                   2045:     u_int rlower)
1.1       nicm     2046: {
1.372   ! nicm     2047:        tty_region(tty, ctx->yoff + rupper - ctx->woy,
        !          2048:            ctx->yoff + rlower - ctx->woy);
1.39      nicm     2049: }
                   2050:
1.40      nicm     2051: /* Set region at absolute position. */
1.214     nicm     2052: static void
1.40      nicm     2053: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm     2054: {
                   2055:        if (tty->rlower == rlower && tty->rupper == rupper)
                   2056:                return;
1.1       nicm     2057:        if (!tty_term_has(tty->term, TTYC_CSR))
                   2058:                return;
1.39      nicm     2059:
                   2060:        tty->rupper = rupper;
                   2061:        tty->rlower = rlower;
1.55      nicm     2062:
                   2063:        /*
                   2064:         * Some terminals (such as PuTTY) do not correctly reset the cursor to
                   2065:         * 0,0 if it is beyond the last column (they do not reset their wrap
                   2066:         * flag so further output causes a line feed). As a workaround, do an
                   2067:         * explicit move to 0 first.
                   2068:         */
1.353     nicm     2069:        if (tty->cx >= tty->sx) {
                   2070:                if (tty->cy == UINT_MAX)
                   2071:                        tty_cursor(tty, 0, 0);
                   2072:                else
                   2073:                        tty_cursor(tty, 0, tty->cy);
                   2074:        }
1.42      nicm     2075:
1.39      nicm     2076:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249     nicm     2077:        tty->cx = tty->cy = UINT_MAX;
1.212     nicm     2078: }
                   2079:
1.214     nicm     2080: /* Turn off margin. */
                   2081: void
                   2082: tty_margin_off(struct tty *tty)
                   2083: {
                   2084:        tty_margin(tty, 0, tty->sx - 1);
                   2085: }
                   2086:
1.212     nicm     2087: /* Set margin inside pane. */
                   2088: static void
                   2089: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
                   2090: {
1.372   ! nicm     2091:        tty_margin(tty, ctx->xoff - ctx->wox,
        !          2092:            ctx->xoff + ctx->wp->sx - 1 - ctx->wox);
1.212     nicm     2093: }
                   2094:
                   2095: /* Set margin at absolute position. */
1.214     nicm     2096: static void
1.212     nicm     2097: tty_margin(struct tty *tty, u_int rleft, u_int rright)
                   2098: {
                   2099:        if (!tty_use_margin(tty))
                   2100:                return;
                   2101:        if (tty->rleft == rleft && tty->rright == rright)
                   2102:                return;
                   2103:
1.232     nicm     2104:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231     nicm     2105:
1.212     nicm     2106:        tty->rleft = rleft;
                   2107:        tty->rright = rright;
                   2108:
1.232     nicm     2109:        if (rleft == 0 && rright == tty->sx - 1)
1.365     nicm     2110:                tty_putcode(tty, TTYC_CLMG);
1.232     nicm     2111:        else
1.365     nicm     2112:                tty_putcode2(tty, TTYC_CMG, rleft, rright);
1.249     nicm     2113:        tty->cx = tty->cy = UINT_MAX;
1.253     nicm     2114: }
                   2115:
                   2116: /*
                   2117:  * Move the cursor, unless it would wrap itself when the next character is
                   2118:  * printed.
                   2119:  */
                   2120: static void
                   2121: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
                   2122:     u_int cx, u_int cy)
                   2123: {
1.268     nicm     2124:        if (!ctx->wrapped ||
1.372   ! nicm     2125:            !tty_full_width(tty, ctx) ||
1.359     nicm     2126:            (tty->term->flags & TERM_NOXENL) ||
1.253     nicm     2127:            ctx->xoff + cx != 0 ||
                   2128:            ctx->yoff + cy != tty->cy + 1 ||
1.254     nicm     2129:            tty->cx < tty->sx ||
                   2130:            tty->cy == tty->rlower)
1.253     nicm     2131:                tty_cursor_pane(tty, ctx, cx, cy);
                   2132:        else
                   2133:                log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1       nicm     2134: }
                   2135:
1.42      nicm     2136: /* Move cursor inside pane. */
1.208     nicm     2137: static void
1.41      nicm     2138: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                   2139: {
1.372   ! nicm     2140:        tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
1.41      nicm     2141: }
                   2142:
1.42      nicm     2143: /* Move cursor to absolute position. */
1.41      nicm     2144: void
                   2145: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm     2146: {
1.42      nicm     2147:        struct tty_term *term = tty->term;
                   2148:        u_int            thisx, thisy;
                   2149:        int              change;
1.354     nicm     2150:
                   2151:        if (tty->flags & TTY_BLOCK)
                   2152:                return;
1.77      nicm     2153:
1.42      nicm     2154:        if (cx > tty->sx - 1)
                   2155:                cx = tty->sx - 1;
                   2156:
                   2157:        thisx = tty->cx;
                   2158:        thisy = tty->cy;
                   2159:
                   2160:        /* No change. */
                   2161:        if (cx == thisx && cy == thisy)
                   2162:                return;
                   2163:
1.43      nicm     2164:        /* Very end of the line, just use absolute movement. */
                   2165:        if (thisx > tty->sx - 1)
                   2166:                goto absolute;
                   2167:
1.42      nicm     2168:        /* Move to home position (0, 0). */
                   2169:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                   2170:                tty_putcode(tty, TTYC_HOME);
                   2171:                goto out;
                   2172:        }
                   2173:
                   2174:        /* Zero on the next line. */
1.293     nicm     2175:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
                   2176:            (!tty_use_margin(tty) || tty->rleft == 0)) {
1.1       nicm     2177:                tty_putc(tty, '\r');
1.42      nicm     2178:                tty_putc(tty, '\n');
                   2179:                goto out;
                   2180:        }
                   2181:
1.45      nicm     2182:        /* Moving column or row. */
1.42      nicm     2183:        if (cy == thisy) {
1.45      nicm     2184:                /*
                   2185:                 * Moving column only, row staying the same.
                   2186:                 */
                   2187:
1.42      nicm     2188:                /* To left edge. */
1.294     nicm     2189:                if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
1.42      nicm     2190:                        tty_putc(tty, '\r');
                   2191:                        goto out;
                   2192:                }
                   2193:
                   2194:                /* One to the left. */
                   2195:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   2196:                        tty_putcode(tty, TTYC_CUB1);
                   2197:                        goto out;
                   2198:                }
                   2199:
                   2200:                /* One to the right. */
                   2201:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   2202:                        tty_putcode(tty, TTYC_CUF1);
                   2203:                        goto out;
                   2204:                }
                   2205:
                   2206:                /* Calculate difference. */
                   2207:                change = thisx - cx;    /* +ve left, -ve right */
                   2208:
                   2209:                /*
                   2210:                 * Use HPA if change is larger than absolute, otherwise move
                   2211:                 * the cursor with CUB/CUF.
                   2212:                 */
1.87      nicm     2213:                if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42      nicm     2214:                        tty_putcode1(tty, TTYC_HPA, cx);
                   2215:                        goto out;
1.333     nicm     2216:                } else if (change > 0 &&
                   2217:                    tty_term_has(term, TTYC_CUB) &&
                   2218:                    !tty_use_margin(tty)) {
1.202     nicm     2219:                        if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
                   2220:                                tty_putcode(tty, TTYC_CUB1);
                   2221:                                tty_putcode(tty, TTYC_CUB1);
                   2222:                                goto out;
                   2223:                        }
1.42      nicm     2224:                        tty_putcode1(tty, TTYC_CUB, change);
                   2225:                        goto out;
1.333     nicm     2226:                } else if (change < 0 &&
                   2227:                    tty_term_has(term, TTYC_CUF) &&
                   2228:                    !tty_use_margin(tty)) {
1.42      nicm     2229:                        tty_putcode1(tty, TTYC_CUF, -change);
                   2230:                        goto out;
                   2231:                }
1.45      nicm     2232:        } else if (cx == thisx) {
                   2233:                /*
                   2234:                 * Moving row only, column staying the same.
                   2235:                 */
1.42      nicm     2236:
                   2237:                /* One above. */
1.77      nicm     2238:                if (thisy != tty->rupper &&
1.42      nicm     2239:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   2240:                        tty_putcode(tty, TTYC_CUU1);
                   2241:                        goto out;
                   2242:                }
                   2243:
                   2244:                /* One below. */
1.49      nicm     2245:                if (thisy != tty->rlower &&
1.42      nicm     2246:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   2247:                        tty_putcode(tty, TTYC_CUD1);
                   2248:                        goto out;
                   2249:                }
                   2250:
                   2251:                /* Calculate difference. */
                   2252:                change = thisy - cy;    /* +ve up, -ve down */
                   2253:
                   2254:                /*
1.77      nicm     2255:                 * Try to use VPA if change is larger than absolute or if this
                   2256:                 * change would cross the scroll region, otherwise use CUU/CUD.
1.42      nicm     2257:                 */
1.87      nicm     2258:                if ((u_int) abs(change) > cy ||
1.42      nicm     2259:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     2260:                    (change > 0 && cy - change < tty->rupper)) {
                   2261:                            if (tty_term_has(term, TTYC_VPA)) {
                   2262:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   2263:                                    goto out;
                   2264:                            }
1.42      nicm     2265:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   2266:                        tty_putcode1(tty, TTYC_CUU, change);
                   2267:                        goto out;
                   2268:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   2269:                        tty_putcode1(tty, TTYC_CUD, -change);
                   2270:                        goto out;
                   2271:                }
                   2272:        }
                   2273:
1.43      nicm     2274: absolute:
1.42      nicm     2275:        /* Absolute movement. */
                   2276:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   2277:
                   2278: out:
                   2279:        tty->cx = cx;
                   2280:        tty->cy = cy;
1.1       nicm     2281: }
                   2282:
                   2283: void
1.176     nicm     2284: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1.317     nicm     2285:     struct window_pane *wp)
1.1       nicm     2286: {
1.63      nicm     2287:        struct grid_cell        *tc = &tty->cell, gc2;
1.255     nicm     2288:        int                      changed;
1.61      nicm     2289:
1.228     nicm     2290:        /* Ignore cell if it is the same as the last one. */
                   2291:        if (wp != NULL &&
                   2292:            (int)wp->id == tty->last_wp &&
1.326     nicm     2293:            ~(wp->flags & PANE_STYLECHANGED) &&
1.228     nicm     2294:            gc->attr == tty->last_cell.attr &&
                   2295:            gc->fg == tty->last_cell.fg &&
1.328     nicm     2296:            gc->bg == tty->last_cell.bg &&
                   2297:            gc->us == tty->last_cell.us)
1.228     nicm     2298:                return;
                   2299:        tty->last_wp = (wp != NULL ? (int)wp->id : -1);
                   2300:        memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
                   2301:
                   2302:        /* Copy cell and update default colours. */
1.85      nicm     2303:        memcpy(&gc2, gc, sizeof gc2);
1.203     nicm     2304:        if (wp != NULL)
                   2305:                tty_default_colours(&gc2, wp);
1.63      nicm     2306:
1.18      nicm     2307:        /*
                   2308:         * If no setab, try to use the reverse attribute as a best-effort for a
                   2309:         * non-default background. This is a bit of a hack but it doesn't do
                   2310:         * any serious harm and makes a couple of applications happier.
                   2311:         */
                   2312:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85      nicm     2313:                if (gc2.attr & GRID_ATTR_REVERSE) {
1.310     nicm     2314:                        if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
1.64      nicm     2315:                                gc2.attr &= ~GRID_ATTR_REVERSE;
1.18      nicm     2316:                } else {
1.310     nicm     2317:                        if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
1.64      nicm     2318:                                gc2.attr |= GRID_ATTR_REVERSE;
1.18      nicm     2319:                }
                   2320:        }
1.1       nicm     2321:
1.85      nicm     2322:        /* Fix up the colours if necessary. */
1.219     nicm     2323:        tty_check_fg(tty, wp, &gc2);
                   2324:        tty_check_bg(tty, wp, &gc2);
1.328     nicm     2325:        tty_check_us(tty, wp, &gc2);
1.85      nicm     2326:
1.328     nicm     2327:        /*
                   2328:         * If any bits are being cleared or the underline colour is now default,
                   2329:         * reset everything.
                   2330:         */
                   2331:        if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
1.64      nicm     2332:                tty_reset(tty);
                   2333:
                   2334:        /*
                   2335:         * Set the colours. This may call tty_reset() (so it comes next) and
1.328     nicm     2336:         * may add to (NOT remove) the desired attributes.
1.64      nicm     2337:         */
1.85      nicm     2338:        tty_colours(tty, &gc2);
1.64      nicm     2339:
1.1       nicm     2340:        /* Filter out attribute bits already set. */
1.85      nicm     2341:        changed = gc2.attr & ~tc->attr;
                   2342:        tc->attr = gc2.attr;
1.1       nicm     2343:
                   2344:        /* Set the attributes. */
                   2345:        if (changed & GRID_ATTR_BRIGHT)
                   2346:                tty_putcode(tty, TTYC_BOLD);
                   2347:        if (changed & GRID_ATTR_DIM)
                   2348:                tty_putcode(tty, TTYC_DIM);
1.180     nicm     2349:        if (changed & GRID_ATTR_ITALICS)
                   2350:                tty_set_italics(tty);
1.308     nicm     2351:        if (changed & GRID_ATTR_ALL_UNDERSCORE) {
                   2352:                if ((changed & GRID_ATTR_UNDERSCORE) ||
                   2353:                    !tty_term_has(tty->term, TTYC_SMULX))
                   2354:                        tty_putcode(tty, TTYC_SMUL);
                   2355:                else if (changed & GRID_ATTR_UNDERSCORE_2)
                   2356:                        tty_putcode1(tty, TTYC_SMULX, 2);
                   2357:                else if (changed & GRID_ATTR_UNDERSCORE_3)
                   2358:                        tty_putcode1(tty, TTYC_SMULX, 3);
                   2359:                else if (changed & GRID_ATTR_UNDERSCORE_4)
                   2360:                        tty_putcode1(tty, TTYC_SMULX, 4);
                   2361:                else if (changed & GRID_ATTR_UNDERSCORE_5)
                   2362:                        tty_putcode1(tty, TTYC_SMULX, 5);
                   2363:        }
1.1       nicm     2364:        if (changed & GRID_ATTR_BLINK)
                   2365:                tty_putcode(tty, TTYC_BLINK);
                   2366:        if (changed & GRID_ATTR_REVERSE) {
                   2367:                if (tty_term_has(tty->term, TTYC_REV))
                   2368:                        tty_putcode(tty, TTYC_REV);
                   2369:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   2370:                        tty_putcode(tty, TTYC_SMSO);
                   2371:        }
                   2372:        if (changed & GRID_ATTR_HIDDEN)
                   2373:                tty_putcode(tty, TTYC_INVIS);
1.255     nicm     2374:        if (changed & GRID_ATTR_STRIKETHROUGH)
                   2375:                tty_putcode(tty, TTYC_SMXX);
1.324     nicm     2376:        if (changed & GRID_ATTR_OVERLINE)
                   2377:                tty_putcode(tty, TTYC_SMOL);
1.285     nicm     2378:        if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.1       nicm     2379:                tty_putcode(tty, TTYC_SMACS);
                   2380: }
                   2381:
1.207     nicm     2382: static void
1.85      nicm     2383: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     2384: {
1.61      nicm     2385:        struct grid_cell        *tc = &tty->cell;
1.204     nicm     2386:        int                      have_ax;
1.61      nicm     2387:
                   2388:        /* No changes? Nothing is necessary. */
1.328     nicm     2389:        if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
1.63      nicm     2390:                return;
1.1       nicm     2391:
1.61      nicm     2392:        /*
                   2393:         * Is either the default colour? This is handled specially because the
                   2394:         * best solution might be to reset both colours to default, in which
                   2395:         * case if only one is default need to fall onward to set the other
                   2396:         * colour.
                   2397:         */
1.310     nicm     2398:        if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
1.61      nicm     2399:                /*
                   2400:                 * If don't have AX but do have op, send sgr0 (op can't
                   2401:                 * actually be used because it is sometimes the same as sgr0
                   2402:                 * and sometimes isn't). This resets both colours to default.
                   2403:                 *
                   2404:                 * Otherwise, try to set the default colour only as needed.
                   2405:                 */
1.174     nicm     2406:                have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61      nicm     2407:                if (!have_ax && tty_term_has(tty->term, TTYC_OP))
                   2408:                        tty_reset(tty);
                   2409:                else {
1.310     nicm     2410:                        if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
1.61      nicm     2411:                                if (have_ax)
                   2412:                                        tty_puts(tty, "\033[39m");
1.204     nicm     2413:                                else if (tc->fg != 7)
1.61      nicm     2414:                                        tty_putcode1(tty, TTYC_SETAF, 7);
1.310     nicm     2415:                                tc->fg = gc->fg;
1.61      nicm     2416:                        }
1.310     nicm     2417:                        if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
1.77      nicm     2418:                                if (have_ax)
1.61      nicm     2419:                                        tty_puts(tty, "\033[49m");
1.204     nicm     2420:                                else if (tc->bg != 0)
1.61      nicm     2421:                                        tty_putcode1(tty, TTYC_SETAB, 0);
1.312     nicm     2422:                                tc->bg = gc->bg;
1.61      nicm     2423:                        }
                   2424:                }
                   2425:        }
1.1       nicm     2426:
1.61      nicm     2427:        /* Set the foreground colour. */
1.310     nicm     2428:        if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
1.85      nicm     2429:                tty_colours_fg(tty, gc);
1.1       nicm     2430:
1.61      nicm     2431:        /*
                   2432:         * Set the background colour. This must come after the foreground as
                   2433:         * tty_colour_fg() can call tty_reset().
                   2434:         */
1.310     nicm     2435:        if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
1.73      nicm     2436:                tty_colours_bg(tty, gc);
1.328     nicm     2437:
                   2438:        /* Set the underscore color. */
                   2439:        if (gc->us != tc->us)
                   2440:                tty_colours_us(tty, gc);
1.1       nicm     2441: }
                   2442:
1.219     nicm     2443: static void
1.317     nicm     2444: tty_check_fg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85      nicm     2445: {
1.204     nicm     2446:        u_char  r, g, b;
                   2447:        u_int   colours;
1.223     nicm     2448:        int     c;
1.85      nicm     2449:
1.288     nicm     2450:        /*
                   2451:         * Perform substitution if this pane has a palette. If the bright
                   2452:         * attribute is set, use the bright entry in the palette by changing to
                   2453:         * the aixterm colour.
                   2454:         */
                   2455:        if (~gc->flags & GRID_FLAG_NOPALETTE) {
                   2456:                c = gc->fg;
1.289     nicm     2457:                if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
1.288     nicm     2458:                        c += 90;
                   2459:                if ((c = window_pane_get_palette(wp, c)) != -1)
                   2460:                        gc->fg = c;
                   2461:        }
1.219     nicm     2462:
1.199     nicm     2463:        /* Is this a 24-bit colour? */
1.204     nicm     2464:        if (gc->fg & COLOUR_FLAG_RGB) {
1.199     nicm     2465:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359     nicm     2466:                if (tty->term->flags & TERM_RGBCOLOURS)
1.200     nicm     2467:                        return;
1.341     nicm     2468:                colour_split_rgb(gc->fg, &r, &g, &b);
                   2469:                gc->fg = colour_find_rgb(r, g, b);
1.199     nicm     2470:        }
1.224     nicm     2471:
                   2472:        /* How many colours does this terminal have? */
1.359     nicm     2473:        if (tty->term->flags & TERM_256COLOURS)
1.224     nicm     2474:                colours = 256;
                   2475:        else
                   2476:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     2477:
1.85      nicm     2478:        /* Is this a 256-colour colour? */
1.204     nicm     2479:        if (gc->fg & COLOUR_FLAG_256) {
1.85      nicm     2480:                /* And not a 256 colour mode? */
1.224     nicm     2481:                if (colours != 256) {
1.85      nicm     2482:                        gc->fg = colour_256to16(gc->fg);
                   2483:                        if (gc->fg & 8) {
                   2484:                                gc->fg &= 7;
1.175     nicm     2485:                                if (colours >= 16)
                   2486:                                        gc->fg += 90;
1.332     nicm     2487:                        }
1.85      nicm     2488:                }
                   2489:                return;
                   2490:        }
                   2491:
                   2492:        /* Is this an aixterm colour? */
                   2493:        if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
                   2494:                gc->fg -= 90;
                   2495:                gc->attr |= GRID_ATTR_BRIGHT;
                   2496:        }
                   2497: }
                   2498:
1.219     nicm     2499: static void
1.317     nicm     2500: tty_check_bg(struct tty *tty, struct window_pane *wp, struct grid_cell *gc)
1.85      nicm     2501: {
1.204     nicm     2502:        u_char  r, g, b;
                   2503:        u_int   colours;
1.223     nicm     2504:        int     c;
1.85      nicm     2505:
1.288     nicm     2506:        /* Perform substitution if this pane has a palette. */
                   2507:        if (~gc->flags & GRID_FLAG_NOPALETTE) {
                   2508:                if ((c = window_pane_get_palette(wp, gc->bg)) != -1)
                   2509:                        gc->bg = c;
                   2510:        }
1.219     nicm     2511:
1.199     nicm     2512:        /* Is this a 24-bit colour? */
1.204     nicm     2513:        if (gc->bg & COLOUR_FLAG_RGB) {
1.199     nicm     2514:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359     nicm     2515:                if (tty->term->flags & TERM_RGBCOLOURS)
1.200     nicm     2516:                        return;
1.341     nicm     2517:                colour_split_rgb(gc->bg, &r, &g, &b);
                   2518:                gc->bg = colour_find_rgb(r, g, b);
1.199     nicm     2519:        }
1.224     nicm     2520:
                   2521:        /* How many colours does this terminal have? */
1.359     nicm     2522:        if (tty->term->flags & TERM_256COLOURS)
1.224     nicm     2523:                colours = 256;
                   2524:        else
                   2525:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     2526:
1.85      nicm     2527:        /* Is this a 256-colour colour? */
1.204     nicm     2528:        if (gc->bg & COLOUR_FLAG_256) {
1.85      nicm     2529:                /*
                   2530:                 * And not a 256 colour mode? Translate to 16-colour
                   2531:                 * palette. Bold background doesn't exist portably, so just
                   2532:                 * discard the bold bit if set.
                   2533:                 */
1.224     nicm     2534:                if (colours != 256) {
1.85      nicm     2535:                        gc->bg = colour_256to16(gc->bg);
1.175     nicm     2536:                        if (gc->bg & 8) {
1.85      nicm     2537:                                gc->bg &= 7;
1.175     nicm     2538:                                if (colours >= 16)
1.322     nicm     2539:                                        gc->bg += 90;
1.175     nicm     2540:                        }
1.85      nicm     2541:                }
                   2542:                return;
                   2543:        }
                   2544:
                   2545:        /* Is this an aixterm colour? */
1.175     nicm     2546:        if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85      nicm     2547:                gc->bg -= 90;
                   2548: }
                   2549:
1.207     nicm     2550: static void
1.335     nicm     2551: tty_check_us(__unused struct tty *tty, struct window_pane *wp,
                   2552:     struct grid_cell *gc)
1.328     nicm     2553: {
                   2554:        int     c;
                   2555:
                   2556:        /* Perform substitution if this pane has a palette. */
                   2557:        if (~gc->flags & GRID_FLAG_NOPALETTE) {
                   2558:                if ((c = window_pane_get_palette(wp, gc->us)) != -1)
                   2559:                        gc->us = c;
                   2560:        }
                   2561:
                   2562:        /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
                   2563:        if (gc->us & COLOUR_FLAG_256)
                   2564:                gc->us = colour_256toRGB (gc->us);
                   2565: }
                   2566:
                   2567: static void
1.85      nicm     2568: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     2569: {
1.61      nicm     2570:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     2571:        char                     s[32];
1.1       nicm     2572:
1.204     nicm     2573:        /* Is this a 24-bit or 256-colour colour? */
1.302     nicm     2574:        if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
1.204     nicm     2575:                if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61      nicm     2576:                        goto save_fg;
1.199     nicm     2577:                /* Should not get here, already converted in tty_check_fg. */
1.85      nicm     2578:                return;
1.1       nicm     2579:        }
                   2580:
1.79      nicm     2581:        /* Is this an aixterm bright colour? */
1.204     nicm     2582:        if (gc->fg >= 90 && gc->fg <= 97) {
1.359     nicm     2583:                if (tty->term->flags & TERM_256COLOURS) {
1.323     nicm     2584:                        xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
                   2585:                        tty_puts(tty, s);
                   2586:                } else
                   2587:                        tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
1.85      nicm     2588:                goto save_fg;
1.79      nicm     2589:        }
                   2590:
1.61      nicm     2591:        /* Otherwise set the foreground colour. */
1.204     nicm     2592:        tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61      nicm     2593:
1.77      nicm     2594: save_fg:
1.61      nicm     2595:        /* Save the new values in the terminal current cell. */
1.204     nicm     2596:        tc->fg = gc->fg;
1.1       nicm     2597: }
                   2598:
1.207     nicm     2599: static void
1.73      nicm     2600: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     2601: {
1.61      nicm     2602:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     2603:        char                     s[32];
1.1       nicm     2604:
1.204     nicm     2605:        /* Is this a 24-bit or 256-colour colour? */
1.302     nicm     2606:        if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
1.204     nicm     2607:                if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61      nicm     2608:                        goto save_bg;
1.199     nicm     2609:                /* Should not get here, already converted in tty_check_bg. */
1.85      nicm     2610:                return;
1.79      nicm     2611:        }
                   2612:
                   2613:        /* Is this an aixterm bright colour? */
1.204     nicm     2614:        if (gc->bg >= 90 && gc->bg <= 97) {
1.359     nicm     2615:                if (tty->term->flags & TERM_256COLOURS) {
1.323     nicm     2616:                        xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
                   2617:                        tty_puts(tty, s);
                   2618:                } else
                   2619:                        tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
1.175     nicm     2620:                goto save_bg;
1.1       nicm     2621:        }
                   2622:
1.61      nicm     2623:        /* Otherwise set the background colour. */
1.204     nicm     2624:        tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61      nicm     2625:
                   2626: save_bg:
                   2627:        /* Save the new values in the terminal current cell. */
1.204     nicm     2628:        tc->bg = gc->bg;
1.328     nicm     2629: }
                   2630:
                   2631: static void
                   2632: tty_colours_us(struct tty *tty, const struct grid_cell *gc)
                   2633: {
                   2634:        struct grid_cell        *tc = &tty->cell;
                   2635:        u_int                    c;
                   2636:        u_char                   r, g, b;
                   2637:
                   2638:        /* Must be an RGB colour - this should never happen. */
                   2639:        if (~gc->us & COLOUR_FLAG_RGB)
                   2640:                return;
                   2641:
                   2642:        /*
                   2643:         * Setulc follows the ncurses(3) one argument "direct colour"
                   2644:         * capability format. Calculate the colour value.
                   2645:         */
                   2646:        colour_split_rgb(gc->us, &r, &g, &b);
                   2647:        c = (65536 * r) + (256 * g) + b;
                   2648:
                   2649:        /* Write the colour. */
                   2650:        tty_putcode1(tty, TTYC_SETULC, c);
                   2651:
                   2652:        /* Save the new values in the terminal current cell. */
                   2653:        tc->us = gc->us;
1.61      nicm     2654: }
                   2655:
1.207     nicm     2656: static int
1.204     nicm     2657: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61      nicm     2658: {
1.204     nicm     2659:        u_char  r, g, b;
1.61      nicm     2660:
1.204     nicm     2661:        if (colour & COLOUR_FLAG_256) {
1.359     nicm     2662:                if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
                   2663:                        tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
                   2664:                else if (tty_term_has(tty->term, TTYC_SETAB))
                   2665:                        tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
                   2666:                return (0);
1.204     nicm     2667:        }
                   2668:
                   2669:        if (colour & COLOUR_FLAG_RGB) {
1.341     nicm     2670:                colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1.359     nicm     2671:                if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
1.286     nicm     2672:                        tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
1.359     nicm     2673:                else if (tty_term_has(tty->term, TTYC_SETRGBB))
1.286     nicm     2674:                        tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
1.165     nicm     2675:                return (0);
                   2676:        }
1.61      nicm     2677:
1.165     nicm     2678:        return (-1);
1.176     nicm     2679: }
                   2680:
1.207     nicm     2681: static void
1.371     nicm     2682: tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
                   2683: {
                   2684:        memcpy(gc, &grid_default_cell, sizeof *gc);
                   2685:        gc->fg = wp->fg;
                   2686:        gc->bg = wp->bg;
                   2687: }
                   2688:
                   2689: static void
1.317     nicm     2690: tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
1.176     nicm     2691: {
1.326     nicm     2692:        struct options  *oo = wp->options;
                   2693:        int              c;
                   2694:
                   2695:        if (wp->flags & PANE_STYLECHANGED) {
                   2696:                wp->flags &= ~PANE_STYLECHANGED;
1.371     nicm     2697:
                   2698:                tty_window_default_style(&wp->cached_active_gc, wp);
                   2699:                style_add(&wp->cached_active_gc, oo, "window-active-style",
                   2700:                    NULL);
                   2701:                tty_window_default_style(&wp->cached_gc, wp);
                   2702:                style_add(&wp->cached_gc, oo, "window-style", NULL);
1.203     nicm     2703:        }
1.176     nicm     2704:
1.204     nicm     2705:        if (gc->fg == 8) {
1.370     nicm     2706:                if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
                   2707:                        gc->fg = wp->cached_active_gc.fg;
1.204     nicm     2708:                else
1.370     nicm     2709:                        gc->fg = wp->cached_gc.fg;
1.219     nicm     2710:
1.310     nicm     2711:                if (gc->fg != 8) {
                   2712:                        c = window_pane_get_palette(wp, gc->fg);
                   2713:                        if (c != -1)
                   2714:                                gc->fg = c;
                   2715:                }
1.176     nicm     2716:        }
                   2717:
1.204     nicm     2718:        if (gc->bg == 8) {
1.370     nicm     2719:                if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
                   2720:                        gc->bg = wp->cached_active_gc.bg;
1.204     nicm     2721:                else
1.370     nicm     2722:                        gc->bg = wp->cached_gc.bg;
1.219     nicm     2723:
1.310     nicm     2724:                if (gc->bg != 8) {
                   2725:                        c = window_pane_get_palette(wp, gc->bg);
                   2726:                        if (c != -1)
                   2727:                                gc->bg = c;
                   2728:                }
1.176     nicm     2729:        }
1.210     nicm     2730: }
                   2731:
                   2732: static void
1.317     nicm     2733: tty_default_attributes(struct tty *tty, struct window_pane *wp, u_int bg)
1.210     nicm     2734: {
                   2735:        static struct grid_cell gc;
                   2736:
                   2737:        memcpy(&gc, &grid_default_cell, sizeof gc);
                   2738:        gc.bg = bg;
                   2739:        tty_attributes(tty, &gc, wp);
1.1       nicm     2740: }