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

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