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

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