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

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