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

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