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

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