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

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