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

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