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

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