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

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