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

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