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

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