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

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