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

1.398   ! nicm        1: /* $OpenBSD: tty.c,v 1.397 2021/08/06 07:32:21 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.396     nicm     1351:        u_int                    i, j, ux, sx, width, hidden;
                   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.395     nicm     1452:
                   1453:                hidden = 0;
                   1454:                for (j = 0; j < gcp->data.width; j++) {
1.396     nicm     1455:                        if (!tty_check_overlay(tty, atx + ux + j, aty))
                   1456:                                hidden++;
1.395     nicm     1457:                }
1.396     nicm     1458:                if (hidden != 0 && hidden == gcp->data.width) {
                   1459:                        if (~gcp->flags & GRID_FLAG_PADDING)
                   1460:                                ux += gcp->data.width;
                   1461:                } else if (hidden != 0 || ux + gcp->data.width > nx) {
1.395     nicm     1462:                        if (~gcp->flags & GRID_FLAG_PADDING) {
                   1463:                                tty_attributes(tty, &last, defaults, palette);
                   1464:                                tty_cursor(tty, atx + ux, aty);
                   1465:                                for (j = 0; j < gcp->data.width; j++) {
1.397     nicm     1466:                                        if (ux > nx)
1.395     nicm     1467:                                                break;
1.397     nicm     1468:                                        if (tty_check_overlay(tty, atx + ux,
                   1469:                                            aty))
                   1470:                                                tty_putc(tty, ' ');
                   1471:                                        else {
                   1472:                                                tty_cursor(tty, atx + ux + 1,
                   1473:                                                    aty);
                   1474:                                        }
1.395     nicm     1475:                                        ux++;
                   1476:                                }
1.297     nicm     1477:                        }
1.299     nicm     1478:                } else if (gcp->attr & GRID_ATTR_CHARSET) {
1.373     nicm     1479:                        tty_attributes(tty, &last, defaults, palette);
1.315     nicm     1480:                        tty_cursor(tty, atx + ux, aty);
1.299     nicm     1481:                        for (j = 0; j < gcp->data.size; j++)
                   1482:                                tty_putc(tty, gcp->data.data[j]);
1.346     nicm     1483:                        ux += gcp->data.width;
1.381     nicm     1484:                } else if (~gcp->flags & GRID_FLAG_PADDING) {
1.298     nicm     1485:                        memcpy(buf + len, gcp->data.data, gcp->data.size);
                   1486:                        len += gcp->data.size;
                   1487:                        width += gcp->data.width;
1.250     nicm     1488:                }
                   1489:        }
1.315     nicm     1490:        if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) {
1.373     nicm     1491:                tty_attributes(tty, &last, defaults, palette);
1.315     nicm     1492:                if (last.flags & GRID_FLAG_CLEARED) {
                   1493:                        log_debug("%s: %zu cleared (end)", __func__, len);
1.373     nicm     1494:                        tty_clear_line(tty, defaults, aty, atx + ux, width,
                   1495:                            last.bg);
1.315     nicm     1496:                } else {
1.329     nicm     1497:                        if (!wrapped || atx != 0 || ux != 0)
                   1498:                                tty_cursor(tty, atx + ux, aty);
1.291     nicm     1499:                        tty_putn(tty, buf, len, width);
                   1500:                }
1.315     nicm     1501:                ux += width;
1.1       nicm     1502:        }
                   1503:
1.309     nicm     1504:        if (!cleared && ux < nx) {
1.315     nicm     1505:                log_debug("%s: %u to end of line (%zu cleared)", __func__,
                   1506:                    nx - ux, len);
1.373     nicm     1507:                tty_default_attributes(tty, defaults, palette, 8);
                   1508:                tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8);
1.35      nicm     1509:        }
1.1       nicm     1510:
1.181     nicm     1511:        tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107     nicm     1512:        tty_update_mode(tty, tty->mode, s);
1.15      nicm     1513: }
                   1514:
1.348     nicm     1515: void
                   1516: tty_sync_start(struct tty *tty)
                   1517: {
1.361     nicm     1518:        if (tty->flags & TTY_BLOCK)
                   1519:                return;
                   1520:        if (tty->flags & TTY_SYNCING)
                   1521:                return;
                   1522:        tty->flags |= TTY_SYNCING;
                   1523:
                   1524:        if (tty_term_has(tty->term, TTYC_SYNC)) {
                   1525:                log_debug("%s sync start", tty->client->name);
1.360     nicm     1526:                tty_putcode1(tty, TTYC_SYNC, 1);
1.357     nicm     1527:        }
1.348     nicm     1528: }
                   1529:
                   1530: void
                   1531: tty_sync_end(struct tty *tty)
                   1532: {
1.361     nicm     1533:        if (tty->flags & TTY_BLOCK)
                   1534:                return;
                   1535:        if (~tty->flags & TTY_SYNCING)
                   1536:                return;
                   1537:        tty->flags &= ~TTY_SYNCING;
                   1538:
                   1539:        if (tty_term_has(tty->term, TTYC_SYNC)) {
                   1540:                log_debug("%s sync end", tty->client->name);
1.360     nicm     1541:                tty_putcode1(tty, TTYC_SYNC, 2);
1.357     nicm     1542:        }
1.348     nicm     1543: }
                   1544:
1.201     nicm     1545: static int
1.373     nicm     1546: tty_client_ready(struct client *c)
1.182     nicm     1547: {
                   1548:        if (c->session == NULL || c->tty.term == NULL)
                   1549:                return (0);
1.305     nicm     1550:        if (c->flags & (CLIENT_REDRAWWINDOW|CLIENT_SUSPENDED))
1.182     nicm     1551:                return (0);
                   1552:        if (c->tty.flags & TTY_FREEZE)
                   1553:                return (0);
                   1554:        return (1);
                   1555: }
                   1556:
1.15      nicm     1557: void
1.182     nicm     1558: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
                   1559:     struct tty_ctx *ctx)
1.15      nicm     1560: {
1.373     nicm     1561:        struct client   *c;
                   1562:        int              state;
1.15      nicm     1563:
1.373     nicm     1564:        if (ctx->set_client_cb == NULL)
1.15      nicm     1565:                return;
1.178     nicm     1566:        TAILQ_FOREACH(c, &clients, entry) {
1.373     nicm     1567:                if (!tty_client_ready(c))
1.15      nicm     1568:                        continue;
1.373     nicm     1569:                state = ctx->set_client_cb(ctx, c);
                   1570:                if (state == -1)
1.358     nicm     1571:                        break;
1.373     nicm     1572:                if (state == 0)
                   1573:                        continue;
1.139     nicm     1574:                cmdfn(&c->tty, ctx);
1.1       nicm     1575:        }
                   1576: }
                   1577:
                   1578: void
1.24      nicm     1579: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1580: {
1.394     nicm     1581:        struct client   *c = tty->client;
                   1582:
1.309     nicm     1583:        if (ctx->bigger ||
1.372     nicm     1584:            !tty_full_width(tty, ctx) ||
1.373     nicm     1585:            tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.210     nicm     1586:            (!tty_term_has(tty->term, TTYC_ICH) &&
1.394     nicm     1587:            !tty_term_has(tty->term, TTYC_ICH1)) ||
                   1588:            c->overlay_check != NULL) {
1.309     nicm     1589:                tty_draw_pane(tty, ctx, ctx->ocy);
1.1       nicm     1590:                return;
                   1591:        }
                   1592:
1.373     nicm     1593:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1594:
1.77      nicm     1595:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm     1596:
1.206     nicm     1597:        tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1       nicm     1598: }
                   1599:
                   1600: void
1.24      nicm     1601: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1602: {
1.394     nicm     1603:        struct client   *c = tty->client;
                   1604:
1.309     nicm     1605:        if (ctx->bigger ||
1.372     nicm     1606:            !tty_full_width(tty, ctx) ||
1.373     nicm     1607:            tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.26      nicm     1608:            (!tty_term_has(tty->term, TTYC_DCH) &&
1.394     nicm     1609:            !tty_term_has(tty->term, TTYC_DCH1)) ||
                   1610:            c->overlay_check != NULL) {
1.309     nicm     1611:                tty_draw_pane(tty, ctx, ctx->ocy);
1.1       nicm     1612:                return;
                   1613:        }
                   1614:
1.373     nicm     1615:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1616:
1.77      nicm     1617:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm     1618:
1.206     nicm     1619:        tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146     nicm     1620: }
                   1621:
                   1622: void
                   1623: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
                   1624: {
1.373     nicm     1625:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.146     nicm     1626:
1.388     nicm     1627:        tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg);
1.1       nicm     1628: }
                   1629:
                   1630: void
1.24      nicm     1631: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1632: {
1.394     nicm     1633:        struct client   *c = tty->client;
                   1634:
1.309     nicm     1635:        if (ctx->bigger ||
1.372     nicm     1636:            !tty_full_width(tty, ctx) ||
1.373     nicm     1637:            tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.77      nicm     1638:            !tty_term_has(tty->term, TTYC_CSR) ||
1.307     nicm     1639:            !tty_term_has(tty->term, TTYC_IL1) ||
1.373     nicm     1640:            ctx->sx == 1 ||
1.394     nicm     1641:            ctx->sy == 1 ||
                   1642:            c->overlay_check != NULL) {
1.14      nicm     1643:                tty_redraw_region(tty, ctx);
1.1       nicm     1644:                return;
                   1645:        }
                   1646:
1.373     nicm     1647:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1648:
1.77      nicm     1649:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm     1650:        tty_margin_off(tty);
1.77      nicm     1651:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1652:
1.12      nicm     1653:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.279     nicm     1654:        tty->cx = tty->cy = UINT_MAX;
1.1       nicm     1655: }
                   1656:
                   1657: void
1.24      nicm     1658: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1659: {
1.394     nicm     1660:        struct client   *c = tty->client;
                   1661:
1.309     nicm     1662:        if (ctx->bigger ||
1.372     nicm     1663:            !tty_full_width(tty, ctx) ||
1.373     nicm     1664:            tty_fake_bce(tty, &ctx->defaults, ctx->bg) ||
1.72      nicm     1665:            !tty_term_has(tty->term, TTYC_CSR) ||
1.307     nicm     1666:            !tty_term_has(tty->term, TTYC_DL1) ||
1.373     nicm     1667:            ctx->sx == 1 ||
1.394     nicm     1668:            ctx->sy == 1 ||
                   1669:            c->overlay_check != NULL) {
1.14      nicm     1670:                tty_redraw_region(tty, ctx);
1.1       nicm     1671:                return;
                   1672:        }
                   1673:
1.373     nicm     1674:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1675:
1.77      nicm     1676:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm     1677:        tty_margin_off(tty);
1.77      nicm     1678:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1679:
1.12      nicm     1680:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.279     nicm     1681:        tty->cx = tty->cy = UINT_MAX;
1.1       nicm     1682: }
                   1683:
                   1684: void
1.24      nicm     1685: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1686: {
1.373     nicm     1687:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1688:
1.372     nicm     1689:        tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg);
1.1       nicm     1690: }
                   1691:
                   1692: void
1.24      nicm     1693: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1694: {
1.373     nicm     1695:        u_int   nx = ctx->sx - ctx->ocx;
1.1       nicm     1696:
1.373     nicm     1697:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1698:
1.309     nicm     1699:        tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg);
1.1       nicm     1700: }
                   1701:
                   1702: void
1.24      nicm     1703: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1704: {
1.373     nicm     1705:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1706:
1.309     nicm     1707:        tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg);
1.1       nicm     1708: }
                   1709:
                   1710: void
1.24      nicm     1711: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1712: {
1.394     nicm     1713:        struct client   *c = tty->client;
                   1714:
1.56      nicm     1715:        if (ctx->ocy != ctx->orupper)
                   1716:                return;
                   1717:
1.309     nicm     1718:        if (ctx->bigger ||
1.372     nicm     1719:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373     nicm     1720:            tty_fake_bce(tty, &ctx->defaults, 8) ||
1.70      nicm     1721:            !tty_term_has(tty->term, TTYC_CSR) ||
1.331     nicm     1722:            (!tty_term_has(tty->term, TTYC_RI) &&
                   1723:            !tty_term_has(tty->term, TTYC_RIN)) ||
1.373     nicm     1724:            ctx->sx == 1 ||
1.394     nicm     1725:            ctx->sy == 1 ||
                   1726:            c->overlay_check != NULL) {
1.14      nicm     1727:                tty_redraw_region(tty, ctx);
1.1       nicm     1728:                return;
                   1729:        }
                   1730:
1.373     nicm     1731:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.77      nicm     1732:
1.56      nicm     1733:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.331     nicm     1734:        tty_margin_pane(tty, ctx);
1.56      nicm     1735:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm     1736:
1.331     nicm     1737:        if (tty_term_has(tty->term, TTYC_RI))
                   1738:                tty_putcode(tty, TTYC_RI);
                   1739:        else
                   1740:                tty_putcode1(tty, TTYC_RIN, 1);
1.1       nicm     1741: }
                   1742:
                   1743: void
1.24      nicm     1744: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1745: {
1.394     nicm     1746:        struct client   *c = tty->client;
                   1747:
1.56      nicm     1748:        if (ctx->ocy != ctx->orlower)
                   1749:                return;
                   1750:
1.309     nicm     1751:        if (ctx->bigger ||
1.372     nicm     1752:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373     nicm     1753:            tty_fake_bce(tty, &ctx->defaults, 8) ||
1.307     nicm     1754:            !tty_term_has(tty->term, TTYC_CSR) ||
1.373     nicm     1755:            ctx->sx == 1 ||
1.394     nicm     1756:            ctx->sy == 1 ||
                   1757:            c->overlay_check != NULL) {
1.236     nicm     1758:                tty_redraw_region(tty, ctx);
1.1       nicm     1759:                return;
                   1760:        }
1.52      nicm     1761:
1.373     nicm     1762:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.77      nicm     1763:
1.56      nicm     1764:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212     nicm     1765:        tty_margin_pane(tty, ctx);
                   1766:
                   1767:        /*
1.301     nicm     1768:         * If we want to wrap a pane while using margins, the cursor needs to
                   1769:         * be exactly on the right of the region. If the cursor is entirely off
                   1770:         * the edge - move it back to the right. Some terminals are funny about
                   1771:         * this and insert extra spaces, so only use the right if margins are
                   1772:         * enabled.
1.212     nicm     1773:         */
1.301     nicm     1774:        if (ctx->xoff + ctx->ocx > tty->rright) {
                   1775:                if (!tty_use_margin(tty))
                   1776:                        tty_cursor(tty, 0, ctx->yoff + ctx->ocy);
                   1777:                else
                   1778:                        tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
                   1779:        } else
1.212     nicm     1780:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm     1781:
1.56      nicm     1782:        tty_putc(tty, '\n');
1.240     nicm     1783: }
                   1784:
                   1785: void
                   1786: tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx)
                   1787: {
1.394     nicm     1788:        struct client   *c = tty->client;
                   1789:        u_int            i;
1.240     nicm     1790:
1.309     nicm     1791:        if (ctx->bigger ||
1.372     nicm     1792:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373     nicm     1793:            tty_fake_bce(tty, &ctx->defaults, 8) ||
1.307     nicm     1794:            !tty_term_has(tty->term, TTYC_CSR) ||
1.373     nicm     1795:            ctx->sx == 1 ||
1.394     nicm     1796:            ctx->sy == 1 ||
                   1797:            c->overlay_check != NULL) {
1.240     nicm     1798:                tty_redraw_region(tty, ctx);
                   1799:                return;
                   1800:        }
                   1801:
1.373     nicm     1802:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.240     nicm     1803:
                   1804:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                   1805:        tty_margin_pane(tty, ctx);
                   1806:
1.284     nicm     1807:        if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) {
1.301     nicm     1808:                if (!tty_use_margin(tty))
                   1809:                        tty_cursor(tty, 0, tty->rlower);
                   1810:                else
                   1811:                        tty_cursor(tty, tty->rright, tty->rlower);
1.284     nicm     1812:                for (i = 0; i < ctx->num; i++)
1.240     nicm     1813:                        tty_putc(tty, '\n');
1.301     nicm     1814:        } else {
1.353     nicm     1815:                if (tty->cy == UINT_MAX)
                   1816:                        tty_cursor(tty, 0, 0);
                   1817:                else
                   1818:                        tty_cursor(tty, 0, tty->cy);
1.284     nicm     1819:                tty_putcode1(tty, TTYC_INDN, ctx->num);
1.331     nicm     1820:        }
                   1821: }
                   1822:
                   1823: void
                   1824: tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx)
                   1825: {
1.394     nicm     1826:        u_int            i;
                   1827:        struct client   *c = tty->client;
1.331     nicm     1828:
                   1829:        if (ctx->bigger ||
1.372     nicm     1830:            (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.373     nicm     1831:            tty_fake_bce(tty, &ctx->defaults, 8) ||
1.331     nicm     1832:            !tty_term_has(tty->term, TTYC_CSR) ||
                   1833:            (!tty_term_has(tty->term, TTYC_RI) &&
                   1834:            !tty_term_has(tty->term, TTYC_RIN)) ||
1.373     nicm     1835:            ctx->sx == 1 ||
1.394     nicm     1836:            ctx->sy == 1 ||
                   1837:            c->overlay_check != NULL) {
1.331     nicm     1838:                tty_redraw_region(tty, ctx);
                   1839:                return;
                   1840:        }
                   1841:
1.373     nicm     1842:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.331     nicm     1843:
                   1844:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                   1845:        tty_margin_pane(tty, ctx);
                   1846:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
                   1847:
                   1848:        if (tty_term_has(tty->term, TTYC_RIN))
                   1849:                tty_putcode1(tty, TTYC_RIN, ctx->num);
                   1850:        else {
                   1851:                for (i = 0; i < ctx->num; i++)
                   1852:                        tty_putcode(tty, TTYC_RI);
1.301     nicm     1853:        }
1.1       nicm     1854: }
                   1855:
                   1856: void
1.24      nicm     1857: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1858: {
1.373     nicm     1859:        u_int   px, py, nx, ny;
1.1       nicm     1860:
1.373     nicm     1861:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1862:
1.372     nicm     1863:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1864:        tty_margin_off(tty);
1.39      nicm     1865:
1.309     nicm     1866:        px = 0;
1.372     nicm     1867:        nx = ctx->sx;
1.309     nicm     1868:        py = ctx->ocy + 1;
1.372     nicm     1869:        ny = ctx->sy - ctx->ocy - 1;
1.271     nicm     1870:
1.309     nicm     1871:        tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271     nicm     1872:
1.309     nicm     1873:        px = ctx->ocx;
1.372     nicm     1874:        nx = ctx->sx - ctx->ocx;
1.309     nicm     1875:        py = ctx->ocy;
1.271     nicm     1876:
1.309     nicm     1877:        tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1       nicm     1878: }
                   1879:
                   1880: void
1.24      nicm     1881: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1882: {
1.373     nicm     1883:        u_int   px, py, nx, ny;
1.1       nicm     1884:
1.373     nicm     1885:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1886:
1.372     nicm     1887:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1888:        tty_margin_off(tty);
1.39      nicm     1889:
1.309     nicm     1890:        px = 0;
1.372     nicm     1891:        nx = ctx->sx;
1.309     nicm     1892:        py = 0;
1.319     nicm     1893:        ny = ctx->ocy;
1.271     nicm     1894:
1.309     nicm     1895:        tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.271     nicm     1896:
1.309     nicm     1897:        px = 0;
1.271     nicm     1898:        nx = ctx->ocx + 1;
1.309     nicm     1899:        py = ctx->ocy;
1.271     nicm     1900:
1.309     nicm     1901:        tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg);
1.1       nicm     1902: }
                   1903:
                   1904: void
1.24      nicm     1905: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1906: {
1.373     nicm     1907:        u_int   px, py, nx, ny;
1.1       nicm     1908:
1.373     nicm     1909:        tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg);
1.1       nicm     1910:
1.372     nicm     1911:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1912:        tty_margin_off(tty);
1.39      nicm     1913:
1.309     nicm     1914:        px = 0;
1.372     nicm     1915:        nx = ctx->sx;
1.309     nicm     1916:        py = 0;
1.372     nicm     1917:        ny = ctx->sy;
1.271     nicm     1918:
1.309     nicm     1919:        tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg);
1.4       nicm     1920: }
                   1921:
                   1922: void
1.24      nicm     1923: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm     1924: {
1.373     nicm     1925:        u_int   i, j;
1.4       nicm     1926:
1.309     nicm     1927:        if (ctx->bigger) {
1.373     nicm     1928:                ctx->redraw_cb(ctx);
1.309     nicm     1929:                return;
                   1930:        }
                   1931:
1.373     nicm     1932:        tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette);
1.4       nicm     1933:
1.372     nicm     1934:        tty_region_pane(tty, ctx, 0, ctx->sy - 1);
1.214     nicm     1935:        tty_margin_off(tty);
1.4       nicm     1936:
1.372     nicm     1937:        for (j = 0; j < ctx->sy; j++) {
1.41      nicm     1938:                tty_cursor_pane(tty, ctx, 0, j);
1.372     nicm     1939:                for (i = 0; i < ctx->sx; i++)
1.4       nicm     1940:                        tty_putc(tty, 'E');
1.1       nicm     1941:        }
                   1942: }
                   1943:
                   1944: void
1.24      nicm     1945: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1946: {
1.394     nicm     1947:        const struct grid_cell  *gcp = ctx->cell;
                   1948:        struct screen           *s = ctx->s;
                   1949:        u_int                    i, px, py;
                   1950:
                   1951:        px = ctx->xoff + ctx->ocx - ctx->wox;
                   1952:        py = ctx->yoff + ctx->ocy - ctx->woy;
                   1953:        if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) ||
                   1954:            (gcp->data.width == 1 && !tty_check_overlay(tty, px, py)))
                   1955:                return;
                   1956:
                   1957:        /* Handle partially obstructed wide characters. */
                   1958:        if (gcp->data.width > 1) {
                   1959:                for (i = 0; i < gcp->data.width; i++) {
                   1960:                        if (!tty_check_overlay(tty, px + i, py)) {
                   1961:                                tty_draw_line(tty, s, s->cx, s->cy,
                   1962:                                    gcp->data.width, px, py, &ctx->defaults,
                   1963:                                    ctx->palette);
                   1964:                                return;
                   1965:                        }
                   1966:                }
                   1967:        }
1.309     nicm     1968:
1.372     nicm     1969:        if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 &&
1.306     nicm     1970:            ctx->ocy == ctx->orlower &&
1.372     nicm     1971:            tty_full_width(tty, ctx))
1.306     nicm     1972:                tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46      nicm     1973:
1.306     nicm     1974:        tty_margin_off(tty);
1.253     nicm     1975:        tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1976:
1.373     nicm     1977:        tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette);
1.239     nicm     1978: }
                   1979:
                   1980: void
                   1981: tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
                   1982: {
1.394     nicm     1983:        u_int   i, hide = 0;
                   1984:
1.309     nicm     1985:        if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1))
                   1986:                return;
                   1987:
                   1988:        if (ctx->bigger &&
1.372     nicm     1989:            (ctx->xoff + ctx->ocx < ctx->wox ||
                   1990:            ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) {
1.309     nicm     1991:                if (!ctx->wrapped ||
1.372     nicm     1992:                    !tty_full_width(tty, ctx) ||
1.382     nicm     1993:                    (tty->term->flags & TERM_NOAM) ||
1.309     nicm     1994:                    ctx->xoff + ctx->ocx != 0 ||
                   1995:                    ctx->yoff + ctx->ocy != tty->cy + 1 ||
                   1996:                    tty->cx < tty->sx ||
                   1997:                    tty->cy == tty->rlower)
                   1998:                        tty_draw_pane(tty, ctx, ctx->ocy);
                   1999:                else
1.373     nicm     2000:                        ctx->redraw_cb(ctx);
1.309     nicm     2001:                return;
                   2002:        }
                   2003:
1.306     nicm     2004:        tty_margin_off(tty);
1.253     nicm     2005:        tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy);
1.239     nicm     2006:
1.373     nicm     2007:        tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette);
1.394     nicm     2008:        for (i = 0; i < ctx->num; i++) {
                   2009:                if (!tty_check_overlay(tty, tty->cx + i, tty->cy))
                   2010:                        break;
                   2011:        }
                   2012:        tty_putn(tty, ctx->ptr, i, i);
                   2013:        for (; i < ctx->num; i++) {
                   2014:                if (tty_check_overlay(tty, tty->cx + hide, tty->cy))
                   2015:                        break;
                   2016:                hide++;
                   2017:        }
                   2018:        tty_cursor(tty, tty->cx + hide, tty->cy);
                   2019:        tty_putn(tty, (char *)ctx->ptr + i, ctx->num - i, ctx->num - i);
1.106     nicm     2020: }
                   2021:
                   2022: void
                   2023: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
                   2024: {
1.383     nicm     2025:        tty_set_selection(tty, ctx->ptr, ctx->num);
                   2026: }
                   2027:
                   2028: void
                   2029: tty_set_selection(struct tty *tty, const char *buf, size_t len)
                   2030: {
                   2031:        char    *encoded;
                   2032:        size_t   size;
1.106     nicm     2033:
1.384     nicm     2034:        if (~tty->flags & TTY_STARTED)
                   2035:                return;
1.106     nicm     2036:        if (!tty_term_has(tty->term, TTYC_MS))
1.383     nicm     2037:                return;
1.106     nicm     2038:
1.383     nicm     2039:        size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
                   2040:        encoded = xmalloc(size);
1.106     nicm     2041:
1.383     nicm     2042:        b64_ntop(buf, len, encoded, size);
                   2043:        tty_putcode_ptr2(tty, TTYC_MS, "", encoded);
1.398   ! nicm     2044:        tty->client->redraw = EVBUFFER_LENGTH(tty->out);
1.106     nicm     2045:
1.383     nicm     2046:        free(encoded);
1.100     nicm     2047: }
                   2048:
                   2049: void
                   2050: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
                   2051: {
1.256     nicm     2052:        tty_add(tty, ctx->ptr, ctx->num);
1.242     nicm     2053:        tty_invalidate(tty);
1.348     nicm     2054: }
                   2055:
                   2056: void
                   2057: tty_cmd_syncstart(struct tty *tty, __unused const struct tty_ctx *ctx)
                   2058: {
                   2059:        tty_sync_start(tty);
1.1       nicm     2060: }
                   2061:
1.377     nicm     2062: void
1.373     nicm     2063: tty_cell(struct tty *tty, const struct grid_cell *gc,
                   2064:     const struct grid_cell *defaults, int *palette)
1.1       nicm     2065: {
1.298     nicm     2066:        const struct grid_cell  *gcp;
1.1       nicm     2067:
                   2068:        /* Skip last character if terminal is stupid. */
1.382     nicm     2069:        if ((tty->term->flags & TERM_NOAM) &&
1.211     nicm     2070:            tty->cy == tty->sy - 1 &&
                   2071:            tty->cx == tty->sx - 1)
1.1       nicm     2072:                return;
                   2073:
                   2074:        /* If this is a padding character, do nothing. */
                   2075:        if (gc->flags & GRID_FLAG_PADDING)
                   2076:                return;
                   2077:
1.377     nicm     2078:        /* Check the output codeset and apply attributes. */
                   2079:        gcp = tty_check_codeset(tty, gc);
                   2080:        tty_attributes(tty, gcp, defaults, palette);
1.1       nicm     2081:
1.377     nicm     2082:        /* If it is a single character, write with putc to handle ACS. */
1.298     nicm     2083:        if (gcp->data.size == 1) {
1.377     nicm     2084:                tty_attributes(tty, gcp, defaults, palette);
1.298     nicm     2085:                if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f)
1.1       nicm     2086:                        return;
1.298     nicm     2087:                tty_putc(tty, *gcp->data.data);
1.1       nicm     2088:                return;
                   2089:        }
                   2090:
1.147     nicm     2091:        /* Write the data. */
1.298     nicm     2092:        tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width);
1.1       nicm     2093: }
                   2094:
                   2095: void
                   2096: tty_reset(struct tty *tty)
                   2097: {
                   2098:        struct grid_cell        *gc = &tty->cell;
                   2099:
1.228     nicm     2100:        if (!grid_cells_equal(gc, &grid_default_cell)) {
1.285     nicm     2101:                if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.228     nicm     2102:                        tty_putcode(tty, TTYC_RMACS);
                   2103:                tty_putcode(tty, TTYC_SGR0);
                   2104:                memcpy(gc, &grid_default_cell, sizeof *gc);
                   2105:        }
                   2106:        memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
1.242     nicm     2107: }
                   2108:
                   2109: static void
                   2110: tty_invalidate(struct tty *tty)
                   2111: {
                   2112:        memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
                   2113:        memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
                   2114:
                   2115:        tty->cx = tty->cy = UINT_MAX;
                   2116:        tty->rupper = tty->rleft = UINT_MAX;
                   2117:        tty->rlower = tty->rright = UINT_MAX;
                   2118:
                   2119:        if (tty->flags & TTY_STARTED) {
1.330     nicm     2120:                if (tty_use_margin(tty))
1.365     nicm     2121:                        tty_putcode(tty, TTYC_ENMG);
1.242     nicm     2122:                tty_putcode(tty, TTYC_SGR0);
                   2123:
                   2124:                tty->mode = ALL_MODES;
                   2125:                tty_update_mode(tty, MODE_CURSOR, NULL);
                   2126:
                   2127:                tty_cursor(tty, 0, 0);
                   2128:                tty_region_off(tty);
                   2129:                tty_margin_off(tty);
                   2130:        } else
                   2131:                tty->mode = MODE_CURSOR;
1.1       nicm     2132: }
                   2133:
1.214     nicm     2134: /* Turn off margin. */
                   2135: void
                   2136: tty_region_off(struct tty *tty)
                   2137: {
                   2138:        tty_region(tty, 0, tty->sy - 1);
                   2139: }
                   2140:
1.40      nicm     2141: /* Set region inside pane. */
1.208     nicm     2142: static void
1.196     nicm     2143: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
                   2144:     u_int rlower)
1.1       nicm     2145: {
1.372     nicm     2146:        tty_region(tty, ctx->yoff + rupper - ctx->woy,
                   2147:            ctx->yoff + rlower - ctx->woy);
1.39      nicm     2148: }
                   2149:
1.40      nicm     2150: /* Set region at absolute position. */
1.214     nicm     2151: static void
1.40      nicm     2152: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm     2153: {
                   2154:        if (tty->rlower == rlower && tty->rupper == rupper)
                   2155:                return;
1.1       nicm     2156:        if (!tty_term_has(tty->term, TTYC_CSR))
                   2157:                return;
1.39      nicm     2158:
                   2159:        tty->rupper = rupper;
                   2160:        tty->rlower = rlower;
1.55      nicm     2161:
                   2162:        /*
                   2163:         * Some terminals (such as PuTTY) do not correctly reset the cursor to
                   2164:         * 0,0 if it is beyond the last column (they do not reset their wrap
                   2165:         * flag so further output causes a line feed). As a workaround, do an
                   2166:         * explicit move to 0 first.
                   2167:         */
1.353     nicm     2168:        if (tty->cx >= tty->sx) {
                   2169:                if (tty->cy == UINT_MAX)
                   2170:                        tty_cursor(tty, 0, 0);
                   2171:                else
                   2172:                        tty_cursor(tty, 0, tty->cy);
                   2173:        }
1.42      nicm     2174:
1.39      nicm     2175:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.249     nicm     2176:        tty->cx = tty->cy = UINT_MAX;
1.212     nicm     2177: }
                   2178:
1.214     nicm     2179: /* Turn off margin. */
                   2180: void
                   2181: tty_margin_off(struct tty *tty)
                   2182: {
                   2183:        tty_margin(tty, 0, tty->sx - 1);
                   2184: }
                   2185:
1.212     nicm     2186: /* Set margin inside pane. */
                   2187: static void
                   2188: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
                   2189: {
1.372     nicm     2190:        tty_margin(tty, ctx->xoff - ctx->wox,
1.373     nicm     2191:            ctx->xoff + ctx->sx - 1 - ctx->wox);
1.212     nicm     2192: }
                   2193:
                   2194: /* Set margin at absolute position. */
1.214     nicm     2195: static void
1.212     nicm     2196: tty_margin(struct tty *tty, u_int rleft, u_int rright)
                   2197: {
                   2198:        if (!tty_use_margin(tty))
                   2199:                return;
                   2200:        if (tty->rleft == rleft && tty->rright == rright)
                   2201:                return;
                   2202:
1.232     nicm     2203:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.231     nicm     2204:
1.212     nicm     2205:        tty->rleft = rleft;
                   2206:        tty->rright = rright;
                   2207:
1.232     nicm     2208:        if (rleft == 0 && rright == tty->sx - 1)
1.365     nicm     2209:                tty_putcode(tty, TTYC_CLMG);
1.232     nicm     2210:        else
1.365     nicm     2211:                tty_putcode2(tty, TTYC_CMG, rleft, rright);
1.249     nicm     2212:        tty->cx = tty->cy = UINT_MAX;
1.253     nicm     2213: }
                   2214:
                   2215: /*
                   2216:  * Move the cursor, unless it would wrap itself when the next character is
                   2217:  * printed.
                   2218:  */
                   2219: static void
                   2220: tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx,
                   2221:     u_int cx, u_int cy)
                   2222: {
1.268     nicm     2223:        if (!ctx->wrapped ||
1.372     nicm     2224:            !tty_full_width(tty, ctx) ||
1.382     nicm     2225:            (tty->term->flags & TERM_NOAM) ||
1.253     nicm     2226:            ctx->xoff + cx != 0 ||
                   2227:            ctx->yoff + cy != tty->cy + 1 ||
1.254     nicm     2228:            tty->cx < tty->sx ||
                   2229:            tty->cy == tty->rlower)
1.253     nicm     2230:                tty_cursor_pane(tty, ctx, cx, cy);
                   2231:        else
                   2232:                log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy);
1.1       nicm     2233: }
                   2234:
1.42      nicm     2235: /* Move cursor inside pane. */
1.208     nicm     2236: static void
1.41      nicm     2237: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                   2238: {
1.372     nicm     2239:        tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy);
1.41      nicm     2240: }
                   2241:
1.42      nicm     2242: /* Move cursor to absolute position. */
1.41      nicm     2243: void
                   2244: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm     2245: {
1.42      nicm     2246:        struct tty_term *term = tty->term;
                   2247:        u_int            thisx, thisy;
                   2248:        int              change;
1.354     nicm     2249:
                   2250:        if (tty->flags & TTY_BLOCK)
                   2251:                return;
1.77      nicm     2252:
1.42      nicm     2253:        if (cx > tty->sx - 1)
                   2254:                cx = tty->sx - 1;
                   2255:
                   2256:        thisx = tty->cx;
                   2257:        thisy = tty->cy;
                   2258:
                   2259:        /* No change. */
                   2260:        if (cx == thisx && cy == thisy)
                   2261:                return;
                   2262:
1.43      nicm     2263:        /* Very end of the line, just use absolute movement. */
                   2264:        if (thisx > tty->sx - 1)
                   2265:                goto absolute;
                   2266:
1.42      nicm     2267:        /* Move to home position (0, 0). */
                   2268:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                   2269:                tty_putcode(tty, TTYC_HOME);
                   2270:                goto out;
                   2271:        }
                   2272:
                   2273:        /* Zero on the next line. */
1.293     nicm     2274:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower &&
                   2275:            (!tty_use_margin(tty) || tty->rleft == 0)) {
1.1       nicm     2276:                tty_putc(tty, '\r');
1.42      nicm     2277:                tty_putc(tty, '\n');
                   2278:                goto out;
                   2279:        }
                   2280:
1.45      nicm     2281:        /* Moving column or row. */
1.42      nicm     2282:        if (cy == thisy) {
1.45      nicm     2283:                /*
                   2284:                 * Moving column only, row staying the same.
                   2285:                 */
                   2286:
1.42      nicm     2287:                /* To left edge. */
1.294     nicm     2288:                if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) {
1.42      nicm     2289:                        tty_putc(tty, '\r');
                   2290:                        goto out;
                   2291:                }
                   2292:
                   2293:                /* One to the left. */
                   2294:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   2295:                        tty_putcode(tty, TTYC_CUB1);
                   2296:                        goto out;
                   2297:                }
                   2298:
                   2299:                /* One to the right. */
                   2300:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   2301:                        tty_putcode(tty, TTYC_CUF1);
                   2302:                        goto out;
                   2303:                }
                   2304:
                   2305:                /* Calculate difference. */
                   2306:                change = thisx - cx;    /* +ve left, -ve right */
                   2307:
                   2308:                /*
                   2309:                 * Use HPA if change is larger than absolute, otherwise move
                   2310:                 * the cursor with CUB/CUF.
                   2311:                 */
1.87      nicm     2312:                if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42      nicm     2313:                        tty_putcode1(tty, TTYC_HPA, cx);
                   2314:                        goto out;
1.333     nicm     2315:                } else if (change > 0 &&
                   2316:                    tty_term_has(term, TTYC_CUB) &&
                   2317:                    !tty_use_margin(tty)) {
1.202     nicm     2318:                        if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
                   2319:                                tty_putcode(tty, TTYC_CUB1);
                   2320:                                tty_putcode(tty, TTYC_CUB1);
                   2321:                                goto out;
                   2322:                        }
1.42      nicm     2323:                        tty_putcode1(tty, TTYC_CUB, change);
                   2324:                        goto out;
1.333     nicm     2325:                } else if (change < 0 &&
                   2326:                    tty_term_has(term, TTYC_CUF) &&
                   2327:                    !tty_use_margin(tty)) {
1.42      nicm     2328:                        tty_putcode1(tty, TTYC_CUF, -change);
                   2329:                        goto out;
                   2330:                }
1.45      nicm     2331:        } else if (cx == thisx) {
                   2332:                /*
                   2333:                 * Moving row only, column staying the same.
                   2334:                 */
1.42      nicm     2335:
                   2336:                /* One above. */
1.77      nicm     2337:                if (thisy != tty->rupper &&
1.42      nicm     2338:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   2339:                        tty_putcode(tty, TTYC_CUU1);
                   2340:                        goto out;
                   2341:                }
                   2342:
                   2343:                /* One below. */
1.49      nicm     2344:                if (thisy != tty->rlower &&
1.42      nicm     2345:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   2346:                        tty_putcode(tty, TTYC_CUD1);
                   2347:                        goto out;
                   2348:                }
                   2349:
                   2350:                /* Calculate difference. */
                   2351:                change = thisy - cy;    /* +ve up, -ve down */
                   2352:
                   2353:                /*
1.77      nicm     2354:                 * Try to use VPA if change is larger than absolute or if this
                   2355:                 * change would cross the scroll region, otherwise use CUU/CUD.
1.42      nicm     2356:                 */
1.87      nicm     2357:                if ((u_int) abs(change) > cy ||
1.42      nicm     2358:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     2359:                    (change > 0 && cy - change < tty->rupper)) {
                   2360:                            if (tty_term_has(term, TTYC_VPA)) {
                   2361:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   2362:                                    goto out;
                   2363:                            }
1.42      nicm     2364:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   2365:                        tty_putcode1(tty, TTYC_CUU, change);
                   2366:                        goto out;
                   2367:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   2368:                        tty_putcode1(tty, TTYC_CUD, -change);
                   2369:                        goto out;
                   2370:                }
                   2371:        }
                   2372:
1.43      nicm     2373: absolute:
1.42      nicm     2374:        /* Absolute movement. */
                   2375:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   2376:
                   2377: out:
                   2378:        tty->cx = cx;
                   2379:        tty->cy = cy;
1.1       nicm     2380: }
                   2381:
                   2382: void
1.176     nicm     2383: tty_attributes(struct tty *tty, const struct grid_cell *gc,
1.373     nicm     2384:     const struct grid_cell *defaults, int *palette)
1.1       nicm     2385: {
1.63      nicm     2386:        struct grid_cell        *tc = &tty->cell, gc2;
1.255     nicm     2387:        int                      changed;
1.61      nicm     2388:
1.373     nicm     2389:        /* Copy cell and update default colours. */
                   2390:        memcpy(&gc2, gc, sizeof gc2);
                   2391:        if (gc2.fg == 8)
                   2392:                gc2.fg = defaults->fg;
                   2393:        if (gc2.bg == 8)
                   2394:                gc2.bg = defaults->bg;
                   2395:
1.228     nicm     2396:        /* Ignore cell if it is the same as the last one. */
1.373     nicm     2397:        if (gc2.attr == tty->last_cell.attr &&
                   2398:            gc2.fg == tty->last_cell.fg &&
                   2399:            gc2.bg == tty->last_cell.bg &&
                   2400:            gc2.us == tty->last_cell.us)
1.228     nicm     2401:                return;
1.63      nicm     2402:
1.18      nicm     2403:        /*
                   2404:         * If no setab, try to use the reverse attribute as a best-effort for a
                   2405:         * non-default background. This is a bit of a hack but it doesn't do
                   2406:         * any serious harm and makes a couple of applications happier.
                   2407:         */
                   2408:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85      nicm     2409:                if (gc2.attr & GRID_ATTR_REVERSE) {
1.310     nicm     2410:                        if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg))
1.64      nicm     2411:                                gc2.attr &= ~GRID_ATTR_REVERSE;
1.18      nicm     2412:                } else {
1.310     nicm     2413:                        if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg))
1.64      nicm     2414:                                gc2.attr |= GRID_ATTR_REVERSE;
1.18      nicm     2415:                }
                   2416:        }
1.1       nicm     2417:
1.85      nicm     2418:        /* Fix up the colours if necessary. */
1.373     nicm     2419:        tty_check_fg(tty, palette, &gc2);
                   2420:        tty_check_bg(tty, palette, &gc2);
                   2421:        tty_check_us(tty, palette, &gc2);
1.85      nicm     2422:
1.328     nicm     2423:        /*
                   2424:         * If any bits are being cleared or the underline colour is now default,
                   2425:         * reset everything.
                   2426:         */
                   2427:        if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0))
1.64      nicm     2428:                tty_reset(tty);
                   2429:
                   2430:        /*
                   2431:         * Set the colours. This may call tty_reset() (so it comes next) and
1.328     nicm     2432:         * may add to (NOT remove) the desired attributes.
1.64      nicm     2433:         */
1.85      nicm     2434:        tty_colours(tty, &gc2);
1.64      nicm     2435:
1.1       nicm     2436:        /* Filter out attribute bits already set. */
1.85      nicm     2437:        changed = gc2.attr & ~tc->attr;
                   2438:        tc->attr = gc2.attr;
1.1       nicm     2439:
                   2440:        /* Set the attributes. */
                   2441:        if (changed & GRID_ATTR_BRIGHT)
                   2442:                tty_putcode(tty, TTYC_BOLD);
                   2443:        if (changed & GRID_ATTR_DIM)
                   2444:                tty_putcode(tty, TTYC_DIM);
1.180     nicm     2445:        if (changed & GRID_ATTR_ITALICS)
                   2446:                tty_set_italics(tty);
1.308     nicm     2447:        if (changed & GRID_ATTR_ALL_UNDERSCORE) {
                   2448:                if ((changed & GRID_ATTR_UNDERSCORE) ||
                   2449:                    !tty_term_has(tty->term, TTYC_SMULX))
                   2450:                        tty_putcode(tty, TTYC_SMUL);
                   2451:                else if (changed & GRID_ATTR_UNDERSCORE_2)
                   2452:                        tty_putcode1(tty, TTYC_SMULX, 2);
                   2453:                else if (changed & GRID_ATTR_UNDERSCORE_3)
                   2454:                        tty_putcode1(tty, TTYC_SMULX, 3);
                   2455:                else if (changed & GRID_ATTR_UNDERSCORE_4)
                   2456:                        tty_putcode1(tty, TTYC_SMULX, 4);
                   2457:                else if (changed & GRID_ATTR_UNDERSCORE_5)
                   2458:                        tty_putcode1(tty, TTYC_SMULX, 5);
                   2459:        }
1.1       nicm     2460:        if (changed & GRID_ATTR_BLINK)
                   2461:                tty_putcode(tty, TTYC_BLINK);
                   2462:        if (changed & GRID_ATTR_REVERSE) {
                   2463:                if (tty_term_has(tty->term, TTYC_REV))
                   2464:                        tty_putcode(tty, TTYC_REV);
                   2465:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   2466:                        tty_putcode(tty, TTYC_SMSO);
                   2467:        }
                   2468:        if (changed & GRID_ATTR_HIDDEN)
                   2469:                tty_putcode(tty, TTYC_INVIS);
1.255     nicm     2470:        if (changed & GRID_ATTR_STRIKETHROUGH)
                   2471:                tty_putcode(tty, TTYC_SMXX);
1.324     nicm     2472:        if (changed & GRID_ATTR_OVERLINE)
                   2473:                tty_putcode(tty, TTYC_SMOL);
1.285     nicm     2474:        if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty))
1.1       nicm     2475:                tty_putcode(tty, TTYC_SMACS);
1.373     nicm     2476:
                   2477:        memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell);
1.1       nicm     2478: }
                   2479:
1.207     nicm     2480: static void
1.85      nicm     2481: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     2482: {
1.61      nicm     2483:        struct grid_cell        *tc = &tty->cell;
1.204     nicm     2484:        int                      have_ax;
1.61      nicm     2485:
                   2486:        /* No changes? Nothing is necessary. */
1.328     nicm     2487:        if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us)
1.63      nicm     2488:                return;
1.1       nicm     2489:
1.61      nicm     2490:        /*
                   2491:         * Is either the default colour? This is handled specially because the
                   2492:         * best solution might be to reset both colours to default, in which
                   2493:         * case if only one is default need to fall onward to set the other
                   2494:         * colour.
                   2495:         */
1.310     nicm     2496:        if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) {
1.61      nicm     2497:                /*
                   2498:                 * If don't have AX but do have op, send sgr0 (op can't
                   2499:                 * actually be used because it is sometimes the same as sgr0
                   2500:                 * and sometimes isn't). This resets both colours to default.
                   2501:                 *
                   2502:                 * Otherwise, try to set the default colour only as needed.
                   2503:                 */
1.174     nicm     2504:                have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61      nicm     2505:                if (!have_ax && tty_term_has(tty->term, TTYC_OP))
                   2506:                        tty_reset(tty);
                   2507:                else {
1.310     nicm     2508:                        if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) {
1.61      nicm     2509:                                if (have_ax)
                   2510:                                        tty_puts(tty, "\033[39m");
1.204     nicm     2511:                                else if (tc->fg != 7)
1.61      nicm     2512:                                        tty_putcode1(tty, TTYC_SETAF, 7);
1.310     nicm     2513:                                tc->fg = gc->fg;
1.61      nicm     2514:                        }
1.310     nicm     2515:                        if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) {
1.77      nicm     2516:                                if (have_ax)
1.61      nicm     2517:                                        tty_puts(tty, "\033[49m");
1.204     nicm     2518:                                else if (tc->bg != 0)
1.61      nicm     2519:                                        tty_putcode1(tty, TTYC_SETAB, 0);
1.312     nicm     2520:                                tc->bg = gc->bg;
1.61      nicm     2521:                        }
                   2522:                }
                   2523:        }
1.1       nicm     2524:
1.61      nicm     2525:        /* Set the foreground colour. */
1.310     nicm     2526:        if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg)
1.85      nicm     2527:                tty_colours_fg(tty, gc);
1.1       nicm     2528:
1.61      nicm     2529:        /*
                   2530:         * Set the background colour. This must come after the foreground as
                   2531:         * tty_colour_fg() can call tty_reset().
                   2532:         */
1.310     nicm     2533:        if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg)
1.73      nicm     2534:                tty_colours_bg(tty, gc);
1.328     nicm     2535:
                   2536:        /* Set the underscore color. */
                   2537:        if (gc->us != tc->us)
                   2538:                tty_colours_us(tty, gc);
1.1       nicm     2539: }
                   2540:
1.219     nicm     2541: static void
1.373     nicm     2542: tty_check_fg(struct tty *tty, int *palette, struct grid_cell *gc)
1.85      nicm     2543: {
1.204     nicm     2544:        u_char  r, g, b;
                   2545:        u_int   colours;
1.223     nicm     2546:        int     c;
1.85      nicm     2547:
1.288     nicm     2548:        /*
                   2549:         * Perform substitution if this pane has a palette. If the bright
                   2550:         * attribute is set, use the bright entry in the palette by changing to
                   2551:         * the aixterm colour.
                   2552:         */
                   2553:        if (~gc->flags & GRID_FLAG_NOPALETTE) {
                   2554:                c = gc->fg;
1.289     nicm     2555:                if (c < 8 && gc->attr & GRID_ATTR_BRIGHT)
1.288     nicm     2556:                        c += 90;
1.373     nicm     2557:                if ((c = tty_get_palette(palette, c)) != -1)
1.288     nicm     2558:                        gc->fg = c;
                   2559:        }
1.219     nicm     2560:
1.199     nicm     2561:        /* Is this a 24-bit colour? */
1.204     nicm     2562:        if (gc->fg & COLOUR_FLAG_RGB) {
1.199     nicm     2563:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359     nicm     2564:                if (tty->term->flags & TERM_RGBCOLOURS)
1.200     nicm     2565:                        return;
1.341     nicm     2566:                colour_split_rgb(gc->fg, &r, &g, &b);
                   2567:                gc->fg = colour_find_rgb(r, g, b);
1.199     nicm     2568:        }
1.224     nicm     2569:
                   2570:        /* How many colours does this terminal have? */
1.359     nicm     2571:        if (tty->term->flags & TERM_256COLOURS)
1.224     nicm     2572:                colours = 256;
                   2573:        else
                   2574:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     2575:
1.85      nicm     2576:        /* Is this a 256-colour colour? */
1.204     nicm     2577:        if (gc->fg & COLOUR_FLAG_256) {
1.85      nicm     2578:                /* And not a 256 colour mode? */
1.387     nicm     2579:                if (colours < 256) {
1.85      nicm     2580:                        gc->fg = colour_256to16(gc->fg);
                   2581:                        if (gc->fg & 8) {
                   2582:                                gc->fg &= 7;
1.175     nicm     2583:                                if (colours >= 16)
                   2584:                                        gc->fg += 90;
1.332     nicm     2585:                        }
1.85      nicm     2586:                }
                   2587:                return;
                   2588:        }
                   2589:
                   2590:        /* Is this an aixterm colour? */
                   2591:        if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
                   2592:                gc->fg -= 90;
                   2593:                gc->attr |= GRID_ATTR_BRIGHT;
                   2594:        }
                   2595: }
                   2596:
1.219     nicm     2597: static void
1.373     nicm     2598: tty_check_bg(struct tty *tty, int *palette, struct grid_cell *gc)
1.85      nicm     2599: {
1.204     nicm     2600:        u_char  r, g, b;
                   2601:        u_int   colours;
1.223     nicm     2602:        int     c;
1.85      nicm     2603:
1.288     nicm     2604:        /* Perform substitution if this pane has a palette. */
                   2605:        if (~gc->flags & GRID_FLAG_NOPALETTE) {
1.373     nicm     2606:                if ((c = tty_get_palette(palette, gc->bg)) != -1)
1.288     nicm     2607:                        gc->bg = c;
                   2608:        }
1.219     nicm     2609:
1.199     nicm     2610:        /* Is this a 24-bit colour? */
1.204     nicm     2611:        if (gc->bg & COLOUR_FLAG_RGB) {
1.199     nicm     2612:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
1.359     nicm     2613:                if (tty->term->flags & TERM_RGBCOLOURS)
1.200     nicm     2614:                        return;
1.341     nicm     2615:                colour_split_rgb(gc->bg, &r, &g, &b);
                   2616:                gc->bg = colour_find_rgb(r, g, b);
1.199     nicm     2617:        }
1.224     nicm     2618:
                   2619:        /* How many colours does this terminal have? */
1.359     nicm     2620:        if (tty->term->flags & TERM_256COLOURS)
1.224     nicm     2621:                colours = 256;
                   2622:        else
                   2623:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     2624:
1.85      nicm     2625:        /* Is this a 256-colour colour? */
1.204     nicm     2626:        if (gc->bg & COLOUR_FLAG_256) {
1.85      nicm     2627:                /*
                   2628:                 * And not a 256 colour mode? Translate to 16-colour
                   2629:                 * palette. Bold background doesn't exist portably, so just
                   2630:                 * discard the bold bit if set.
                   2631:                 */
1.387     nicm     2632:                if (colours < 256) {
1.85      nicm     2633:                        gc->bg = colour_256to16(gc->bg);
1.175     nicm     2634:                        if (gc->bg & 8) {
1.85      nicm     2635:                                gc->bg &= 7;
1.175     nicm     2636:                                if (colours >= 16)
1.322     nicm     2637:                                        gc->bg += 90;
1.175     nicm     2638:                        }
1.85      nicm     2639:                }
                   2640:                return;
                   2641:        }
                   2642:
                   2643:        /* Is this an aixterm colour? */
1.175     nicm     2644:        if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85      nicm     2645:                gc->bg -= 90;
                   2646: }
                   2647:
1.207     nicm     2648: static void
1.373     nicm     2649: tty_check_us(__unused struct tty *tty, int *palette, struct grid_cell *gc)
1.328     nicm     2650: {
                   2651:        int     c;
                   2652:
                   2653:        /* Perform substitution if this pane has a palette. */
                   2654:        if (~gc->flags & GRID_FLAG_NOPALETTE) {
1.373     nicm     2655:                if ((c = tty_get_palette(palette, gc->us)) != -1)
1.328     nicm     2656:                        gc->us = c;
                   2657:        }
                   2658:
                   2659:        /* Underscore colour is set as RGB so convert a 256 colour to RGB. */
                   2660:        if (gc->us & COLOUR_FLAG_256)
                   2661:                gc->us = colour_256toRGB (gc->us);
                   2662: }
                   2663:
                   2664: static void
1.85      nicm     2665: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     2666: {
1.61      nicm     2667:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     2668:        char                     s[32];
1.1       nicm     2669:
1.204     nicm     2670:        /* Is this a 24-bit or 256-colour colour? */
1.302     nicm     2671:        if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) {
1.204     nicm     2672:                if (tty_try_colour(tty, gc->fg, "38") == 0)
1.385     nicm     2673:                        goto save;
1.199     nicm     2674:                /* Should not get here, already converted in tty_check_fg. */
1.85      nicm     2675:                return;
1.1       nicm     2676:        }
                   2677:
1.79      nicm     2678:        /* Is this an aixterm bright colour? */
1.204     nicm     2679:        if (gc->fg >= 90 && gc->fg <= 97) {
1.359     nicm     2680:                if (tty->term->flags & TERM_256COLOURS) {
1.323     nicm     2681:                        xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
                   2682:                        tty_puts(tty, s);
                   2683:                } else
                   2684:                        tty_putcode1(tty, TTYC_SETAF, gc->fg - 90 + 8);
1.385     nicm     2685:                goto save;
1.79      nicm     2686:        }
                   2687:
1.61      nicm     2688:        /* Otherwise set the foreground colour. */
1.204     nicm     2689:        tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61      nicm     2690:
1.385     nicm     2691: save:
1.61      nicm     2692:        /* Save the new values in the terminal current cell. */
1.204     nicm     2693:        tc->fg = gc->fg;
1.1       nicm     2694: }
                   2695:
1.207     nicm     2696: static void
1.73      nicm     2697: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     2698: {
1.61      nicm     2699:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     2700:        char                     s[32];
1.1       nicm     2701:
1.204     nicm     2702:        /* Is this a 24-bit or 256-colour colour? */
1.302     nicm     2703:        if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) {
1.204     nicm     2704:                if (tty_try_colour(tty, gc->bg, "48") == 0)
1.385     nicm     2705:                        goto save;
1.199     nicm     2706:                /* Should not get here, already converted in tty_check_bg. */
1.85      nicm     2707:                return;
1.79      nicm     2708:        }
                   2709:
                   2710:        /* Is this an aixterm bright colour? */
1.204     nicm     2711:        if (gc->bg >= 90 && gc->bg <= 97) {
1.359     nicm     2712:                if (tty->term->flags & TERM_256COLOURS) {
1.323     nicm     2713:                        xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
                   2714:                        tty_puts(tty, s);
                   2715:                } else
                   2716:                        tty_putcode1(tty, TTYC_SETAB, gc->bg - 90 + 8);
1.385     nicm     2717:                goto save;
1.1       nicm     2718:        }
                   2719:
1.61      nicm     2720:        /* Otherwise set the background colour. */
1.204     nicm     2721:        tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61      nicm     2722:
1.385     nicm     2723: save:
1.61      nicm     2724:        /* Save the new values in the terminal current cell. */
1.204     nicm     2725:        tc->bg = gc->bg;
1.328     nicm     2726: }
                   2727:
                   2728: static void
                   2729: tty_colours_us(struct tty *tty, const struct grid_cell *gc)
                   2730: {
                   2731:        struct grid_cell        *tc = &tty->cell;
                   2732:        u_int                    c;
                   2733:        u_char                   r, g, b;
                   2734:
1.385     nicm     2735:        /* Clear underline colour. */
                   2736:        if (gc->us == 0) {
                   2737:                tty_putcode(tty, TTYC_OL);
                   2738:                goto save;
                   2739:        }
                   2740:
1.328     nicm     2741:        /* Must be an RGB colour - this should never happen. */
                   2742:        if (~gc->us & COLOUR_FLAG_RGB)
                   2743:                return;
                   2744:
                   2745:        /*
1.385     nicm     2746:         * Setulc and setal follows the ncurses(3) one argument "direct colour"
1.328     nicm     2747:         * capability format. Calculate the colour value.
                   2748:         */
                   2749:        colour_split_rgb(gc->us, &r, &g, &b);
                   2750:        c = (65536 * r) + (256 * g) + b;
                   2751:
1.385     nicm     2752:        /*
                   2753:         * Write the colour. Only use setal if the RGB flag is set because the
                   2754:         * non-RGB version may be wrong.
                   2755:         */
                   2756:        if (tty_term_has(tty->term, TTYC_SETULC))
                   2757:                tty_putcode1(tty, TTYC_SETULC, c);
                   2758:        else if (tty_term_has(tty->term, TTYC_SETAL) &&
                   2759:            tty_term_has(tty->term, TTYC_RGB))
                   2760:                tty_putcode1(tty, TTYC_SETAL, c);
1.328     nicm     2761:
1.385     nicm     2762: save:
1.328     nicm     2763:        /* Save the new values in the terminal current cell. */
                   2764:        tc->us = gc->us;
1.61      nicm     2765: }
                   2766:
1.207     nicm     2767: static int
1.204     nicm     2768: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61      nicm     2769: {
1.204     nicm     2770:        u_char  r, g, b;
1.61      nicm     2771:
1.204     nicm     2772:        if (colour & COLOUR_FLAG_256) {
1.359     nicm     2773:                if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF))
                   2774:                        tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
                   2775:                else if (tty_term_has(tty->term, TTYC_SETAB))
                   2776:                        tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
                   2777:                return (0);
1.204     nicm     2778:        }
                   2779:
                   2780:        if (colour & COLOUR_FLAG_RGB) {
1.341     nicm     2781:                colour_split_rgb(colour & 0xffffff, &r, &g, &b);
1.359     nicm     2782:                if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF))
1.286     nicm     2783:                        tty_putcode3(tty, TTYC_SETRGBF, r, g, b);
1.359     nicm     2784:                else if (tty_term_has(tty->term, TTYC_SETRGBB))
1.286     nicm     2785:                        tty_putcode3(tty, TTYC_SETRGBB, r, g, b);
1.165     nicm     2786:                return (0);
                   2787:        }
1.61      nicm     2788:
1.165     nicm     2789:        return (-1);
1.176     nicm     2790: }
                   2791:
1.207     nicm     2792: static void
1.371     nicm     2793: tty_window_default_style(struct grid_cell *gc, struct window_pane *wp)
                   2794: {
                   2795:        memcpy(gc, &grid_default_cell, sizeof *gc);
                   2796:        gc->fg = wp->fg;
                   2797:        gc->bg = wp->bg;
                   2798: }
                   2799:
1.373     nicm     2800: void
1.317     nicm     2801: tty_default_colours(struct grid_cell *gc, struct window_pane *wp)
1.176     nicm     2802: {
1.326     nicm     2803:        struct options  *oo = wp->options;
1.373     nicm     2804:
                   2805:        memcpy(gc, &grid_default_cell, sizeof *gc);
1.326     nicm     2806:
                   2807:        if (wp->flags & PANE_STYLECHANGED) {
                   2808:                wp->flags &= ~PANE_STYLECHANGED;
1.371     nicm     2809:
                   2810:                tty_window_default_style(&wp->cached_active_gc, wp);
                   2811:                style_add(&wp->cached_active_gc, oo, "window-active-style",
                   2812:                    NULL);
                   2813:                tty_window_default_style(&wp->cached_gc, wp);
                   2814:                style_add(&wp->cached_gc, oo, "window-style", NULL);
1.203     nicm     2815:        }
1.176     nicm     2816:
1.204     nicm     2817:        if (gc->fg == 8) {
1.370     nicm     2818:                if (wp == wp->window->active && wp->cached_active_gc.fg != 8)
                   2819:                        gc->fg = wp->cached_active_gc.fg;
1.204     nicm     2820:                else
1.370     nicm     2821:                        gc->fg = wp->cached_gc.fg;
1.176     nicm     2822:        }
                   2823:
1.204     nicm     2824:        if (gc->bg == 8) {
1.370     nicm     2825:                if (wp == wp->window->active && wp->cached_active_gc.bg != 8)
                   2826:                        gc->bg = wp->cached_active_gc.bg;
1.204     nicm     2827:                else
1.370     nicm     2828:                        gc->bg = wp->cached_gc.bg;
1.176     nicm     2829:        }
1.210     nicm     2830: }
                   2831:
                   2832: static void
1.373     nicm     2833: tty_default_attributes(struct tty *tty, const struct grid_cell *defaults,
                   2834:     int *palette, u_int bg)
1.210     nicm     2835: {
1.373     nicm     2836:        struct grid_cell        gc;
1.210     nicm     2837:
                   2838:        memcpy(&gc, &grid_default_cell, sizeof gc);
                   2839:        gc.bg = bg;
1.373     nicm     2840:        tty_attributes(tty, &gc, defaults, palette);
1.1       nicm     2841: }