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

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