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

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