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

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