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

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