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

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