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

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