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

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