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

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