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

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