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

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