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

1.211   ! nicm        1: /* $OpenBSD: tty.c,v 1.210 2016/10/13 20:27:27 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.1       nicm       24: #include <errno.h>
                     25: #include <fcntl.h>
1.106     nicm       26: #include <resolv.h>
1.42      nicm       27: #include <stdlib.h>
1.1       nicm       28: #include <string.h>
                     29: #include <termios.h>
                     30: #include <unistd.h>
                     31:
                     32: #include "tmux.h"
                     33:
1.207     nicm       34: static int     tty_log_fd = -1;
1.192     nicm       35:
1.208     nicm       36: static void    tty_init_termios(int, struct termios *, struct bufferevent *);
                     37:
1.207     nicm       38: static void    tty_read_callback(struct bufferevent *, void *);
                     39: static void    tty_error_callback(struct bufferevent *, short, void *);
1.66      nicm       40:
1.207     nicm       41: static int     tty_client_ready(struct client *, struct window_pane *);
1.201     nicm       42:
1.207     nicm       43: static void    tty_set_italics(struct tty *);
                     44: static int     tty_try_colour(struct tty *, int, const char *);
1.208     nicm       45: static void    tty_force_cursor_colour(struct tty *, const char *);
                     46: static void    tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int,
                     47:                    u_int);
1.207     nicm       48:
                     49: static void    tty_colours(struct tty *, const struct grid_cell *);
                     50: static void    tty_check_fg(struct tty *, struct grid_cell *);
                     51: static void    tty_check_bg(struct tty *, struct grid_cell *);
                     52: static void    tty_colours_fg(struct tty *, const struct grid_cell *);
                     53: static void    tty_colours_bg(struct tty *, const struct grid_cell *);
                     54:
1.208     nicm       55: static void    tty_region_pane(struct tty *, const struct tty_ctx *, u_int,
                     56:                    u_int);
1.207     nicm       57: static int     tty_large_region(struct tty *, const struct tty_ctx *);
1.210     nicm       58: static int     tty_fake_bce(const struct tty *, const struct window_pane *,
                     59:                    u_int);
1.207     nicm       60: static void    tty_redraw_region(struct tty *, const struct tty_ctx *);
                     61: static void    tty_emulate_repeat(struct tty *, enum tty_code_code,
                     62:                    enum tty_code_code, u_int);
                     63: static void    tty_repeat_space(struct tty *, u_int);
                     64: static void    tty_cell(struct tty *, const struct grid_cell *,
                     65:                    const struct window_pane *);
                     66: static void    tty_default_colours(struct grid_cell *,
                     67:                    const struct window_pane *);
1.210     nicm       68: static void    tty_default_attributes(struct tty *, const struct window_pane *,
                     69:                    u_int);
1.1       nicm       70:
1.90      nicm       71: #define tty_use_acs(tty) \
1.131     nicm       72:        (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
1.90      nicm       73:
1.132     nicm       74: #define tty_pane_full_width(tty, ctx) \
                     75:        ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx)
                     76:
1.192     nicm       77: void
                     78: tty_create_log(void)
                     79: {
                     80:        char    name[64];
                     81:
                     82:        xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid());
                     83:
                     84:        tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
                     85:        if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1)
                     86:                fatal("fcntl failed");
                     87: }
                     88:
1.185     nicm       89: int
1.136     nicm       90: tty_init(struct tty *tty, struct client *c, int fd, char *term)
1.1       nicm       91: {
1.30      nicm       92:        char    *path;
                     93:
1.185     nicm       94:        if (!isatty(fd))
                     95:                return (-1);
                     96:
1.30      nicm       97:        memset(tty, 0, sizeof *tty);
1.22      nicm       98:
1.9       nicm       99:        if (term == NULL || *term == '\0')
1.1       nicm      100:                tty->termname = xstrdup("unknown");
                    101:        else
                    102:                tty->termname = xstrdup(term);
1.30      nicm      103:        tty->fd = fd;
1.136     nicm      104:        tty->client = c;
1.30      nicm      105:
                    106:        if ((path = ttyname(fd)) == NULL)
1.185     nicm      107:                return (-1);
1.30      nicm      108:        tty->path = xstrdup(path);
1.108     nicm      109:        tty->cstyle = 0;
1.107     nicm      110:        tty->ccolour = xstrdup("");
1.30      nicm      111:
1.1       nicm      112:        tty->flags = 0;
                    113:        tty->term_flags = 0;
1.185     nicm      114:
                    115:        return (0);
1.31      nicm      116: }
                    117:
1.88      nicm      118: int
1.31      nicm      119: tty_resize(struct tty *tty)
                    120: {
                    121:        struct winsize  ws;
1.88      nicm      122:        u_int           sx, sy;
1.31      nicm      123:
                    124:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88      nicm      125:                sx = ws.ws_col;
                    126:                if (sx == 0)
                    127:                        sx = 80;
                    128:                sy = ws.ws_row;
                    129:                if (sy == 0)
                    130:                        sy = 24;
                    131:        } else {
                    132:                sx = 80;
                    133:                sy = 24;
1.31      nicm      134:        }
1.114     nicm      135:        if (!tty_set_size(tty, sx, sy))
1.88      nicm      136:                return (0);
1.31      nicm      137:
                    138:        tty->cx = UINT_MAX;
                    139:        tty->cy = UINT_MAX;
                    140:
                    141:        tty->rupper = UINT_MAX;
                    142:        tty->rlower = UINT_MAX;
1.88      nicm      143:
                    144:        /*
                    145:         * If the terminal has been started, reset the actual scroll region and
                    146:         * cursor position, as this may not have happened.
                    147:         */
                    148:        if (tty->flags & TTY_STARTED) {
                    149:                tty_cursor(tty, 0, 0);
                    150:                tty_region(tty, 0, tty->sy - 1);
                    151:        }
                    152:
1.114     nicm      153:        return (1);
                    154: }
                    155:
                    156: int
                    157: tty_set_size(struct tty *tty, u_int sx, u_int sy) {
                    158:        if (sx == tty->sx && sy == tty->sy)
                    159:                return (0);
                    160:        tty->sx = sx;
                    161:        tty->sy = sy;
1.88      nicm      162:        return (1);
1.1       nicm      163: }
                    164:
                    165: int
1.166     nicm      166: tty_open(struct tty *tty, char **cause)
1.1       nicm      167: {
1.166     nicm      168:        tty->term = tty_term_find(tty->termname, tty->fd, cause);
1.21      nicm      169:        if (tty->term == NULL) {
                    170:                tty_close(tty);
                    171:                return (-1);
                    172:        }
                    173:        tty->flags |= TTY_OPENED;
1.1       nicm      174:
1.149     nicm      175:        tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_TIMER);
1.1       nicm      176:
1.196     nicm      177:        tty->event = bufferevent_new(tty->fd, tty_read_callback, NULL,
                    178:            tty_error_callback, tty);
1.1       nicm      179:
                    180:        tty_start_tty(tty);
                    181:
1.148     nicm      182:        tty_keys_build(tty);
1.1       nicm      183:
                    184:        return (0);
                    185: }
                    186:
1.207     nicm      187: static void
1.194     nicm      188: tty_read_callback(__unused struct bufferevent *bufev, void *data)
1.67      nicm      189: {
                    190:        struct tty      *tty = data;
                    191:
                    192:        while (tty_keys_next(tty))
                    193:                ;
                    194: }
                    195:
1.207     nicm      196: static void
1.194     nicm      197: tty_error_callback(__unused struct bufferevent *bufev, __unused short what,
                    198:     __unused void *data)
1.66      nicm      199: {
                    200: }
                    201:
1.208     nicm      202: static void
1.127     nicm      203: tty_init_termios(int fd, struct termios *orig_tio, struct bufferevent *bufev)
1.1       nicm      204: {
1.128     nicm      205:        struct termios  tio;
1.33      nicm      206:
1.127     nicm      207:        if (fd == -1 || tcgetattr(fd, orig_tio) != 0)
1.33      nicm      208:                return;
1.1       nicm      209:
1.127     nicm      210:        setblocking(fd, 0);
1.34      nicm      211:
1.127     nicm      212:        if (bufev != NULL)
                    213:                bufferevent_enable(bufev, EV_READ|EV_WRITE);
1.66      nicm      214:
1.127     nicm      215:        memcpy(&tio, orig_tio, sizeof tio);
1.1       nicm      216:        tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
                    217:        tio.c_iflag |= IGNBRK;
                    218:        tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
                    219:        tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
1.169     jsg       220:            ECHOPRT|ECHOKE|ISIG);
1.1       nicm      221:        tio.c_cc[VMIN] = 1;
1.60      deraadt   222:        tio.c_cc[VTIME] = 0;
1.127     nicm      223:        if (tcsetattr(fd, TCSANOW, &tio) == 0)
                    224:                tcflush(fd, TCIOFLUSH);
                    225: }
                    226:
                    227: void
                    228: tty_start_tty(struct tty *tty)
                    229: {
                    230:        tty_init_termios(tty->fd, &tty->tio, tty->event);
1.1       nicm      231:
1.10      nicm      232:        tty_putcode(tty, TTYC_SMCUP);
1.1       nicm      233:
1.25      nicm      234:        tty_putcode(tty, TTYC_SGR0);
                    235:        memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
                    236:
1.76      nicm      237:        tty_putcode(tty, TTYC_RMKX);
1.90      nicm      238:        if (tty_use_acs(tty))
                    239:                tty_putcode(tty, TTYC_ENACS);
1.1       nicm      240:        tty_putcode(tty, TTYC_CLEAR);
                    241:
                    242:        tty_putcode(tty, TTYC_CNORM);
                    243:        if (tty_term_has(tty->term, TTYC_KMOUS))
1.179     nicm      244:                tty_puts(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.121     nicm      245:
1.189     nicm      246:        if (tty_term_flag(tty->term, TTYC_XT)) {
1.191     nicm      247:                if (options_get_number(global_options, "focus-events")) {
1.162     nicm      248:                        tty->flags |= TTY_FOCUS;
                    249:                        tty_puts(tty, "\033[?1004h");
                    250:                }
                    251:        }
1.1       nicm      252:
                    253:        tty->cx = UINT_MAX;
                    254:        tty->cy = UINT_MAX;
                    255:
                    256:        tty->rlower = UINT_MAX;
                    257:        tty->rupper = UINT_MAX;
                    258:
                    259:        tty->mode = MODE_CURSOR;
1.20      nicm      260:
                    261:        tty->flags |= TTY_STARTED;
1.107     nicm      262:
                    263:        tty_force_cursor_colour(tty, "");
1.177     nicm      264:
                    265:        tty->mouse_drag_flag = 0;
                    266:        tty->mouse_drag_update = NULL;
                    267:        tty->mouse_drag_release = NULL;
1.128     nicm      268: }
                    269:
                    270: void
1.1       nicm      271: tty_stop_tty(struct tty *tty)
                    272: {
                    273:        struct winsize  ws;
                    274:
1.20      nicm      275:        if (!(tty->flags & TTY_STARTED))
                    276:                return;
                    277:        tty->flags &= ~TTY_STARTED;
                    278:
1.66      nicm      279:        bufferevent_disable(tty->event, EV_READ|EV_WRITE);
                    280:
1.1       nicm      281:        /*
                    282:         * Be flexible about error handling and try not kill the server just
                    283:         * because the fd is invalid. Things like ssh -t can easily leave us
                    284:         * with a dead tty.
                    285:         */
                    286:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
                    287:                return;
                    288:        if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
                    289:                return;
                    290:
                    291:        tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90      nicm      292:        if (tty_use_acs(tty))
                    293:                tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1       nicm      294:        tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
                    295:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
                    296:        tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.160     nicm      297:        if (tty_term_has(tty->term, TTYC_SS) && tty->cstyle != 0) {
                    298:                if (tty_term_has(tty->term, TTYC_SE))
                    299:                        tty_raw(tty, tty_term_string(tty->term, TTYC_SE));
1.109     nicm      300:                else
1.160     nicm      301:                        tty_raw(tty, tty_term_string1(tty->term, TTYC_SS, 0));
1.108     nicm      302:        }
1.173     nicm      303:        if (tty->mode & MODE_BRACKETPASTE)
                    304:                tty_raw(tty, "\033[?2004l");
1.107     nicm      305:        tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1       nicm      306:
                    307:        tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
                    308:        if (tty_term_has(tty->term, TTYC_KMOUS))
1.179     nicm      309:                tty_raw(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l");
1.151     nicm      310:
1.189     nicm      311:        if (tty_term_flag(tty->term, TTYC_XT)) {
1.162     nicm      312:                if (tty->flags & TTY_FOCUS) {
                    313:                        tty->flags &= ~TTY_FOCUS;
1.172     nicm      314:                        tty_raw(tty, "\033[?1004l");
1.162     nicm      315:                }
                    316:        }
1.10      nicm      317:
                    318:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.150     nicm      319:
                    320:        setblocking(tty->fd, 1);
1.1       nicm      321: }
                    322:
                    323: void
1.20      nicm      324: tty_close(struct tty *tty)
1.1       nicm      325: {
1.123     nicm      326:        if (event_initialized(&tty->key_timer))
                    327:                evtimer_del(&tty->key_timer);
1.20      nicm      328:        tty_stop_tty(tty);
1.1       nicm      329:
1.21      nicm      330:        if (tty->flags & TTY_OPENED) {
1.66      nicm      331:                bufferevent_free(tty->event);
                    332:
1.21      nicm      333:                tty_term_free(tty->term);
                    334:                tty_keys_free(tty);
                    335:
                    336:                tty->flags &= ~TTY_OPENED;
                    337:        }
1.1       nicm      338:
1.21      nicm      339:        if (tty->fd != -1) {
                    340:                close(tty->fd);
                    341:                tty->fd = -1;
                    342:        }
1.1       nicm      343: }
                    344:
                    345: void
1.20      nicm      346: tty_free(struct tty *tty)
1.1       nicm      347: {
1.20      nicm      348:        tty_close(tty);
1.1       nicm      349:
1.138     nicm      350:        free(tty->ccolour);
1.190     nicm      351:        free(tty->path);
                    352:        free(tty->termname);
1.1       nicm      353: }
                    354:
                    355: void
                    356: tty_raw(struct tty *tty, const char *s)
                    357: {
1.150     nicm      358:        ssize_t n, slen;
                    359:        u_int   i;
                    360:
                    361:        slen = strlen(s);
                    362:        for (i = 0; i < 5; i++) {
                    363:                n = write(tty->fd, s, slen);
                    364:                if (n >= 0) {
                    365:                        s += n;
                    366:                        slen -= n;
                    367:                        if (slen == 0)
                    368:                                break;
                    369:                } else if (n == -1 && errno != EAGAIN)
                    370:                        break;
                    371:                usleep(100);
                    372:        }
1.1       nicm      373: }
                    374:
                    375: void
                    376: tty_putcode(struct tty *tty, enum tty_code_code code)
                    377: {
                    378:        tty_puts(tty, tty_term_string(tty->term, code));
                    379: }
                    380:
                    381: void
                    382: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
                    383: {
                    384:        if (a < 0)
                    385:                return;
                    386:        tty_puts(tty, tty_term_string1(tty->term, code, a));
                    387: }
                    388:
                    389: void
                    390: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
                    391: {
                    392:        if (a < 0 || b < 0)
                    393:                return;
                    394:        tty_puts(tty, tty_term_string2(tty->term, code, a, b));
                    395: }
                    396:
                    397: void
1.107     nicm      398: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
                    399: {
                    400:        if (a != NULL)
                    401:                tty_puts(tty, tty_term_ptr1(tty->term, code, a));
                    402: }
                    403:
                    404: void
1.167     nicm      405: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a,
                    406:     const void *b)
1.106     nicm      407: {
                    408:        if (a != NULL && b != NULL)
                    409:                tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
                    410: }
                    411:
                    412: void
1.1       nicm      413: tty_puts(struct tty *tty, const char *s)
                    414: {
                    415:        if (*s == '\0')
                    416:                return;
1.66      nicm      417:        bufferevent_write(tty->event, s, strlen(s));
1.1       nicm      418:
1.192     nicm      419:        if (tty_log_fd != -1)
                    420:                write(tty_log_fd, s, strlen(s));
1.1       nicm      421: }
                    422:
                    423: void
                    424: tty_putc(struct tty *tty, u_char ch)
                    425: {
1.90      nicm      426:        const char      *acs;
1.1       nicm      427:
1.90      nicm      428:        if (tty->cell.attr & GRID_ATTR_CHARSET) {
                    429:                acs = tty_acs_get(tty, ch);
                    430:                if (acs != NULL)
                    431:                        bufferevent_write(tty->event, acs, strlen(acs));
                    432:                else
                    433:                        bufferevent_write(tty->event, &ch, 1);
                    434:        } else
                    435:                bufferevent_write(tty->event, &ch, 1);
1.1       nicm      436:
                    437:        if (ch >= 0x20 && ch != 0x7f) {
1.211   ! nicm      438:                if (tty->cx >= tty->sx) {
1.46      nicm      439:                        tty->cx = 1;
                    440:                        if (tty->cy != tty->rlower)
                    441:                                tty->cy++;
1.211   ! nicm      442:
        !           443:                        /*
        !           444:                         * On !xenl terminals, force the cursor position to
        !           445:                         * where we think it should be after a line wrap - this
        !           446:                         * means it works on sensible terminals as well.
        !           447:                         */
        !           448:                        if (tty->term->flags & TERM_EARLYWRAP)
        !           449:                                tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1       nicm      450:                } else
                    451:                        tty->cx++;
                    452:        }
                    453:
1.192     nicm      454:        if (tty_log_fd != -1)
                    455:                write(tty_log_fd, &ch, 1);
1.1       nicm      456: }
                    457:
                    458: void
1.147     nicm      459: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5       nicm      460: {
1.147     nicm      461:        bufferevent_write(tty->event, buf, len);
1.192     nicm      462:        if (tty_log_fd != -1)
                    463:                write(tty_log_fd, buf, len);
1.147     nicm      464:        tty->cx += width;
1.5       nicm      465: }
                    466:
1.207     nicm      467: static void
1.180     nicm      468: tty_set_italics(struct tty *tty)
                    469: {
                    470:        const char      *s;
                    471:
                    472:        if (tty_term_has(tty->term, TTYC_SITM)) {
1.191     nicm      473:                s = options_get_string(global_options, "default-terminal");
1.180     nicm      474:                if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
                    475:                        tty_putcode(tty, TTYC_SITM);
                    476:                        return;
                    477:                }
                    478:        }
                    479:        tty_putcode(tty, TTYC_SMSO);
                    480: }
                    481:
                    482: void
1.1       nicm      483: tty_set_title(struct tty *tty, const char *title)
                    484: {
1.105     nicm      485:        if (!tty_term_has(tty->term, TTYC_TSL) ||
                    486:            !tty_term_has(tty->term, TTYC_FSL))
1.1       nicm      487:                return;
                    488:
1.105     nicm      489:        tty_putcode(tty, TTYC_TSL);
1.1       nicm      490:        tty_puts(tty, title);
1.105     nicm      491:        tty_putcode(tty, TTYC_FSL);
1.1       nicm      492: }
                    493:
1.208     nicm      494: static void
1.107     nicm      495: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
                    496: {
                    497:        if (*ccolour == '\0')
                    498:                tty_putcode(tty, TTYC_CR);
                    499:        else
1.160     nicm      500:                tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138     nicm      501:        free(tty->ccolour);
1.107     nicm      502:        tty->ccolour = xstrdup(ccolour);
                    503: }
                    504:
                    505: void
                    506: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1       nicm      507: {
                    508:        int     changed;
                    509:
1.197     nicm      510:        if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107     nicm      511:                tty_force_cursor_colour(tty, s->ccolour);
                    512:
1.1       nicm      513:        if (tty->flags & TTY_NOCURSOR)
                    514:                mode &= ~MODE_CURSOR;
                    515:
                    516:        changed = mode ^ tty->mode;
1.183     nicm      517:        if (changed & MODE_BLINKING) {
                    518:                if (tty_term_has(tty->term, TTYC_CVVIS))
                    519:                        tty_putcode(tty, TTYC_CVVIS);
                    520:                else
                    521:                        tty_putcode(tty, TTYC_CNORM);
                    522:                changed |= MODE_CURSOR;
                    523:        }
                    524:        if (changed & MODE_CURSOR) {
                    525:                if (mode & MODE_CURSOR)
                    526:                        tty_putcode(tty, TTYC_CNORM);
                    527:                else
1.1       nicm      528:                        tty_putcode(tty, TTYC_CIVIS);
1.108     nicm      529:        }
1.181     nicm      530:        if (s != NULL && tty->cstyle != s->cstyle) {
1.160     nicm      531:                if (tty_term_has(tty->term, TTYC_SS)) {
1.108     nicm      532:                        if (s->cstyle == 0 &&
1.160     nicm      533:                            tty_term_has(tty->term, TTYC_SE))
                    534:                                tty_putcode(tty, TTYC_SE);
1.108     nicm      535:                        else
1.160     nicm      536:                                tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108     nicm      537:                }
                    538:                tty->cstyle = s->cstyle;
1.1       nicm      539:        }
1.195     nicm      540:        if (changed & ALL_MOUSE_MODES) {
1.94      nicm      541:                if (mode & ALL_MOUSE_MODES) {
1.153     nicm      542:                        /*
                    543:                         * Enable the SGR (1006) extension unconditionally, as
                    544:                         * this is safe from misinterpretation. Do it in this
                    545:                         * order, because in some terminals it's the last one
                    546:                         * that takes effect and SGR is the preferred one.
                    547:                         */
                    548:                        tty_puts(tty, "\033[?1006h");
1.170     nicm      549:                        if (mode & MODE_MOUSE_BUTTON)
1.94      nicm      550:                                tty_puts(tty, "\033[?1002h");
1.98      nicm      551:                        else if (mode & MODE_MOUSE_STANDARD)
                    552:                                tty_puts(tty, "\033[?1000h");
1.86      nicm      553:                } else {
1.170     nicm      554:                        if (tty->mode & MODE_MOUSE_BUTTON)
1.94      nicm      555:                                tty_puts(tty, "\033[?1002l");
1.98      nicm      556:                        else if (tty->mode & MODE_MOUSE_STANDARD)
                    557:                                tty_puts(tty, "\033[?1000l");
1.153     nicm      558:                        tty_puts(tty, "\033[?1006l");
1.86      nicm      559:                }
1.76      nicm      560:        }
                    561:        if (changed & MODE_KKEYPAD) {
                    562:                if (mode & MODE_KKEYPAD)
                    563:                        tty_putcode(tty, TTYC_SMKX);
                    564:                else
                    565:                        tty_putcode(tty, TTYC_RMKX);
1.115     nicm      566:        }
                    567:        if (changed & MODE_BRACKETPASTE) {
                    568:                if (mode & MODE_BRACKETPASTE)
                    569:                        tty_puts(tty, "\033[?2004h");
                    570:                else
                    571:                        tty_puts(tty, "\033[?2004l");
1.1       nicm      572:        }
                    573:        tty->mode = mode;
                    574: }
                    575:
1.207     nicm      576: static void
1.168     nicm      577: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
                    578:     enum tty_code_code code1, u_int n)
1.1       nicm      579: {
                    580:        if (tty_term_has(tty->term, code))
                    581:                tty_putcode1(tty, code, n);
                    582:        else {
                    583:                while (n-- > 0)
                    584:                        tty_putcode(tty, code1);
                    585:        }
                    586: }
                    587:
1.207     nicm      588: static void
1.133     nicm      589: tty_repeat_space(struct tty *tty, u_int n)
                    590: {
                    591:        while (n-- > 0)
                    592:                tty_putc(tty, ' ');
                    593: }
                    594:
1.1       nicm      595: /*
1.119     nicm      596:  * Is the region large enough to be worth redrawing once later rather than
                    597:  * probably several times now? Currently yes if it is more than 50% of the
                    598:  * pane.
                    599:  */
1.207     nicm      600: static int
1.194     nicm      601: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119     nicm      602: {
                    603:        struct window_pane      *wp = ctx->wp;
                    604:
                    605:        return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
                    606: }
                    607:
                    608: /*
1.176     nicm      609:  * Return if BCE is needed but the terminal doesn't have it - it'll need to be
                    610:  * emulated.
                    611:  */
1.207     nicm      612: static int
1.210     nicm      613: tty_fake_bce(const struct tty *tty, const struct window_pane *wp, u_int bg)
1.176     nicm      614: {
                    615:        struct grid_cell        gc;
                    616:
1.210     nicm      617:        if (tty_term_flag(tty->term, TTYC_BCE))
                    618:                return (0);
                    619:
1.176     nicm      620:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.203     nicm      621:        if (wp != NULL)
                    622:                tty_default_colours(&gc, wp);
1.176     nicm      623:
1.210     nicm      624:        if (bg != 8 || gc.bg != 8)
                    625:                return (1);
                    626:        return (0);
1.176     nicm      627: }
                    628:
                    629: /*
1.1       nicm      630:  * Redraw scroll region using data from screen (already updated). Used when
                    631:  * CSR not supported, or window is a pane that doesn't take up the full
                    632:  * width of the terminal.
                    633:  */
1.207     nicm      634: static void
1.24      nicm      635: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      636: {
1.14      nicm      637:        struct window_pane      *wp = ctx->wp;
                    638:        struct screen           *s = wp->screen;
1.204     nicm      639:        u_int                    i;
1.1       nicm      640:
                    641:        /*
1.119     nicm      642:         * If region is large, schedule a window redraw. In most cases this is
                    643:         * likely to be followed by some more scrolling.
1.1       nicm      644:         */
1.119     nicm      645:        if (tty_large_region(tty, ctx)) {
1.1       nicm      646:                wp->flags |= PANE_REDRAW;
                    647:                return;
                    648:        }
                    649:
1.14      nicm      650:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    651:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.176     nicm      652:                        tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1       nicm      653:        } else {
1.14      nicm      654:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.176     nicm      655:                        tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1       nicm      656:        }
                    657: }
                    658:
                    659: void
1.176     nicm      660: tty_draw_pane(struct tty *tty, const struct window_pane *wp, u_int py, u_int ox,
                    661:     u_int oy)
                    662: {
                    663:        tty_draw_line(tty, wp, wp->screen, py, ox, oy);
                    664: }
                    665:
                    666: void
                    667: tty_draw_line(struct tty *tty, const struct window_pane *wp,
                    668:     struct screen *s, u_int py, u_int ox, u_int oy)
1.1       nicm      669: {
1.209     nicm      670:        struct grid_cell         gc, tmp_gc;
1.46      nicm      671:        struct grid_line        *gl;
1.1       nicm      672:        u_int                    i, sx;
1.181     nicm      673:        int                      flags;
1.1       nicm      674:
1.181     nicm      675:        flags = tty->flags & TTY_NOCURSOR;
                    676:        tty->flags |= TTY_NOCURSOR;
                    677:        tty_update_mode(tty, tty->mode, s);
1.35      nicm      678:
1.1       nicm      679:        sx = screen_size_x(s);
1.19      nicm      680:        if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
                    681:                sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1       nicm      682:        if (sx > tty->sx)
                    683:                sx = tty->sx;
                    684:
1.46      nicm      685:        /*
1.174     nicm      686:         * Don't move the cursor to the start position if it will wrap there
1.50      nicm      687:         * itself.
1.46      nicm      688:         */
                    689:        gl = NULL;
                    690:        if (py != 0)
                    691:                gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83      nicm      692:        if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47      nicm      693:            tty->cx < tty->sx || ox != 0 ||
                    694:            (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46      nicm      695:                tty_cursor(tty, ox, oy + py);
                    696:
1.1       nicm      697:        for (i = 0; i < sx; i++) {
1.193     nicm      698:                grid_view_get_cell(s->grid, i, py, &gc);
1.209     nicm      699:                if (gc.flags & GRID_FLAG_SELECTED) {
                    700:                        screen_select_cell(s, &tmp_gc, &gc);
                    701:                        tty_cell(tty, &tmp_gc, wp);
                    702:                } else
                    703:                        tty_cell(tty, &gc, wp);
1.1       nicm      704:        }
                    705:
1.181     nicm      706:        if (sx < tty->sx) {
1.210     nicm      707:                tty_default_attributes(tty, wp, 8);
1.181     nicm      708:
                    709:                tty_cursor(tty, ox + sx, oy + py);
                    710:                if (sx != screen_size_x(s) &&
                    711:                    ox + screen_size_x(s) >= tty->sx &&
                    712:                    tty_term_has(tty->term, TTYC_EL) &&
1.210     nicm      713:                    !tty_fake_bce(tty, wp, 8))
1.181     nicm      714:                        tty_putcode(tty, TTYC_EL);
                    715:                else
                    716:                        tty_repeat_space(tty, screen_size_x(s) - sx);
1.35      nicm      717:        }
1.1       nicm      718:
1.181     nicm      719:        tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107     nicm      720:        tty_update_mode(tty, tty->mode, s);
1.15      nicm      721: }
                    722:
1.201     nicm      723: static int
1.182     nicm      724: tty_client_ready(struct client *c, struct window_pane *wp)
                    725: {
                    726:        if (c->session == NULL || c->tty.term == NULL)
                    727:                return (0);
                    728:        if (c->flags & CLIENT_SUSPENDED)
                    729:                return (0);
                    730:        if (c->tty.flags & TTY_FREEZE)
                    731:                return (0);
                    732:        if (c->session->curw->window != wp->window)
                    733:                return (0);
                    734:        return (1);
                    735: }
                    736:
1.15      nicm      737: void
1.182     nicm      738: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
                    739:     struct tty_ctx *ctx)
1.15      nicm      740: {
                    741:        struct window_pane      *wp = ctx->wp;
                    742:        struct client           *c;
                    743:
1.75      nicm      744:        /* wp can be NULL if updating the screen but not the terminal. */
1.15      nicm      745:        if (wp == NULL)
                    746:                return;
                    747:
                    748:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    749:                return;
1.129     nicm      750:        if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15      nicm      751:                return;
                    752:
1.178     nicm      753:        TAILQ_FOREACH(c, &clients, entry) {
1.182     nicm      754:                if (!tty_client_ready(c, wp))
1.15      nicm      755:                        continue;
                    756:
1.139     nicm      757:                ctx->xoff = wp->xoff;
                    758:                ctx->yoff = wp->yoff;
                    759:                if (status_at_line(c) == 0)
                    760:                        ctx->yoff++;
1.113     nicm      761:
1.139     nicm      762:                cmdfn(&c->tty, ctx);
1.1       nicm      763:        }
                    764: }
                    765:
                    766: void
1.24      nicm      767: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      768: {
1.77      nicm      769:        struct window_pane      *wp = ctx->wp;
1.1       nicm      770:
1.210     nicm      771:        if (!tty_pane_full_width(tty, ctx) ||
                    772:            tty_fake_bce(tty, wp, ctx->bg) ||
                    773:            (!tty_term_has(tty->term, TTYC_ICH) &&
                    774:            !tty_term_has(tty->term, TTYC_ICH1))) {
1.176     nicm      775:                tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      776:                return;
                    777:        }
                    778:
1.210     nicm      779:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      780:
1.77      nicm      781:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      782:
1.206     nicm      783:        tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1       nicm      784: }
                    785:
                    786: void
1.24      nicm      787: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      788: {
1.77      nicm      789:        struct window_pane      *wp = ctx->wp;
1.1       nicm      790:
1.210     nicm      791:        if (!tty_pane_full_width(tty, ctx) ||
                    792:            tty_fake_bce(tty, wp, ctx->bg) ||
1.26      nicm      793:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    794:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.176     nicm      795:                tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      796:                return;
                    797:        }
                    798:
1.210     nicm      799:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      800:
1.77      nicm      801:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      802:
1.206     nicm      803:        tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146     nicm      804: }
                    805:
                    806: void
                    807: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
                    808: {
                    809:        u_int   i;
                    810:
1.176     nicm      811:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.146     nicm      812:
                    813:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    814:
1.210     nicm      815:        if (tty_term_has(tty->term, TTYC_ECH) &&
                    816:            !tty_fake_bce(tty, ctx->wp, ctx->bg))
1.146     nicm      817:                tty_putcode1(tty, TTYC_ECH, ctx->num);
                    818:        else {
                    819:                for (i = 0; i < ctx->num; i++)
                    820:                        tty_putc(tty, ' ');
                    821:        }
1.1       nicm      822: }
                    823:
                    824: void
1.24      nicm      825: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      826: {
1.210     nicm      827:        if (!tty_pane_full_width(tty, ctx) ||
                    828:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77      nicm      829:            !tty_term_has(tty->term, TTYC_CSR) ||
1.72      nicm      830:            !tty_term_has(tty->term, TTYC_IL1)) {
1.14      nicm      831:                tty_redraw_region(tty, ctx);
1.1       nicm      832:                return;
                    833:        }
                    834:
1.210     nicm      835:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm      836:
1.77      nicm      837:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    838:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      839:
1.12      nicm      840:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      841: }
                    842:
                    843: void
1.24      nicm      844: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      845: {
1.210     nicm      846:        if (!tty_pane_full_width(tty, ctx) ||
                    847:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72      nicm      848:            !tty_term_has(tty->term, TTYC_CSR) ||
                    849:            !tty_term_has(tty->term, TTYC_DL1)) {
1.14      nicm      850:                tty_redraw_region(tty, ctx);
1.1       nicm      851:                return;
                    852:        }
                    853:
1.210     nicm      854:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm      855:
1.77      nicm      856:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    857:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      858:
1.12      nicm      859:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      860: }
                    861:
                    862: void
1.24      nicm      863: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      864: {
1.77      nicm      865:        struct window_pane      *wp = ctx->wp;
1.12      nicm      866:        struct screen           *s = wp->screen;
1.1       nicm      867:
1.210     nicm      868:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      869:
1.77      nicm      870:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41      nicm      871:
1.210     nicm      872:        if (tty_pane_full_width(tty, ctx) &&
                    873:            !tty_fake_bce(tty, wp, ctx->bg) &&
1.176     nicm      874:            tty_term_has(tty->term, TTYC_EL))
1.1       nicm      875:                tty_putcode(tty, TTYC_EL);
1.133     nicm      876:        else
                    877:                tty_repeat_space(tty, screen_size_x(s));
1.1       nicm      878: }
                    879:
                    880: void
1.24      nicm      881: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      882: {
1.77      nicm      883:        struct window_pane      *wp = ctx->wp;
1.12      nicm      884:        struct screen           *s = wp->screen;
1.1       nicm      885:
1.210     nicm      886:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      887:
1.41      nicm      888:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    889:
1.176     nicm      890:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm      891:            tty_term_has(tty->term, TTYC_EL) &&
                    892:            !tty_fake_bce(tty, wp, ctx->bg))
1.1       nicm      893:                tty_putcode(tty, TTYC_EL);
1.133     nicm      894:        else
                    895:                tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.1       nicm      896: }
                    897:
                    898: void
1.24      nicm      899: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      900: {
1.210     nicm      901:        struct window_pane      *wp = ctx->wp;
                    902:
                    903:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      904:
1.210     nicm      905:        if (ctx->xoff == 0 &&
                    906:            tty_term_has(tty->term, TTYC_EL1) &&
                    907:            !tty_fake_bce(tty, ctx->wp, ctx->bg)) {
1.41      nicm      908:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      909:                tty_putcode(tty, TTYC_EL1);
                    910:        } else {
1.41      nicm      911:                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133     nicm      912:                tty_repeat_space(tty, ctx->ocx + 1);
1.1       nicm      913:        }
                    914: }
                    915:
                    916: void
1.24      nicm      917: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      918: {
1.56      nicm      919:        if (ctx->ocy != ctx->orupper)
                    920:                return;
                    921:
1.210     nicm      922:        if (!tty_pane_full_width(tty, ctx) ||
                    923:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.70      nicm      924:            !tty_term_has(tty->term, TTYC_CSR) ||
                    925:            !tty_term_has(tty->term, TTYC_RI)) {
1.14      nicm      926:                tty_redraw_region(tty, ctx);
1.1       nicm      927:                return;
                    928:        }
                    929:
1.176     nicm      930:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.77      nicm      931:
1.56      nicm      932:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    933:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm      934:
1.56      nicm      935:        tty_putcode(tty, TTYC_RI);
1.1       nicm      936: }
                    937:
                    938: void
1.24      nicm      939: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      940: {
1.77      nicm      941:        struct window_pane      *wp = ctx->wp;
1.1       nicm      942:
1.56      nicm      943:        if (ctx->ocy != ctx->orlower)
                    944:                return;
                    945:
1.210     nicm      946:        if (!tty_pane_full_width(tty, ctx) ||
                    947:            tty_fake_bce(tty, wp, ctx->bg) ||
1.1       nicm      948:            !tty_term_has(tty->term, TTYC_CSR)) {
1.128     nicm      949:                if (tty_large_region(tty, ctx))
                    950:                        wp->flags |= PANE_REDRAW;
1.140     nicm      951:                else
1.128     nicm      952:                        tty_redraw_region(tty, ctx);
1.1       nicm      953:                return;
                    954:        }
1.52      nicm      955:
                    956:        /*
                    957:         * If this line wrapped naturally (ctx->num is nonzero), don't do
                    958:         * anything - the cursor can just be moved to the last cell and wrap
                    959:         * naturally.
                    960:         */
                    961:        if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
                    962:                return;
1.1       nicm      963:
1.176     nicm      964:        tty_attributes(tty, &grid_default_cell, wp);
1.77      nicm      965:
1.56      nicm      966:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    967:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm      968:
1.56      nicm      969:        tty_putc(tty, '\n');
1.1       nicm      970: }
                    971:
                    972: void
1.24      nicm      973: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      974: {
1.77      nicm      975:        struct window_pane      *wp = ctx->wp;
1.12      nicm      976:        struct screen           *s = wp->screen;
1.204     nicm      977:        u_int                    i, j;
1.1       nicm      978:
1.210     nicm      979:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      980:
1.39      nicm      981:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      982:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39      nicm      983:
1.176     nicm      984:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm      985:            tty_term_has(tty->term, TTYC_EL) &&
                    986:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.1       nicm      987:                tty_putcode(tty, TTYC_EL);
1.14      nicm      988:                if (ctx->ocy != screen_size_y(s) - 1) {
1.41      nicm      989:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14      nicm      990:                        for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1       nicm      991:                                tty_putcode(tty, TTYC_EL);
                    992:                                if (i == screen_size_y(s) - 1)
                    993:                                        continue;
                    994:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    995:                                tty->cy++;
                    996:                        }
                    997:                }
                    998:        } else {
1.133     nicm      999:                tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.68      nicm     1000:                for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41      nicm     1001:                        tty_cursor_pane(tty, ctx, 0, j);
1.133     nicm     1002:                        tty_repeat_space(tty, screen_size_x(s));
1.1       nicm     1003:                }
                   1004:        }
                   1005: }
                   1006:
                   1007: void
1.24      nicm     1008: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1009: {
1.77      nicm     1010:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1011:        struct screen           *s = wp->screen;
1.204     nicm     1012:        u_int                    i, j;
1.1       nicm     1013:
1.176     nicm     1014:        tty_attributes(tty, &grid_default_cell, wp);
1.1       nicm     1015:
1.39      nicm     1016:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm     1017:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm     1018:
1.176     nicm     1019:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1020:            tty_term_has(tty->term, TTYC_EL) &&
                   1021:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.14      nicm     1022:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm     1023:                        tty_putcode(tty, TTYC_EL);
                   1024:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1025:                        tty->cy++;
                   1026:                }
                   1027:        } else {
1.14      nicm     1028:                for (j = 0; j < ctx->ocy; j++) {
1.41      nicm     1029:                        tty_cursor_pane(tty, ctx, 0, j);
1.133     nicm     1030:                        tty_repeat_space(tty, screen_size_x(s));
1.1       nicm     1031:                }
                   1032:        }
1.133     nicm     1033:        tty_repeat_space(tty, ctx->ocx + 1);
1.1       nicm     1034: }
                   1035:
                   1036: void
1.24      nicm     1037: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1038: {
1.77      nicm     1039:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1040:        struct screen           *s = wp->screen;
1.204     nicm     1041:        u_int                    i, j;
1.1       nicm     1042:
1.210     nicm     1043:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1044:
1.39      nicm     1045:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm     1046:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm     1047:
1.176     nicm     1048:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1049:            tty_term_has(tty->term, TTYC_EL) &&
                   1050:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.1       nicm     1051:                for (i = 0; i < screen_size_y(s); i++) {
                   1052:                        tty_putcode(tty, TTYC_EL);
                   1053:                        if (i != screen_size_y(s) - 1) {
                   1054:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1055:                                tty->cy++;
                   1056:                        }
                   1057:                }
                   1058:        } else {
                   1059:                for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm     1060:                        tty_cursor_pane(tty, ctx, 0, j);
1.133     nicm     1061:                        tty_repeat_space(tty, screen_size_x(s));
1.1       nicm     1062:                }
1.4       nicm     1063:        }
                   1064: }
                   1065:
                   1066: void
1.24      nicm     1067: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm     1068: {
1.12      nicm     1069:        struct window_pane      *wp = ctx->wp;
                   1070:        struct screen           *s = wp->screen;
                   1071:        u_int                    i, j;
1.4       nicm     1072:
1.176     nicm     1073:        tty_attributes(tty, &grid_default_cell, wp);
1.4       nicm     1074:
1.39      nicm     1075:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4       nicm     1076:
                   1077:        for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm     1078:                tty_cursor_pane(tty, ctx, 0, j);
1.4       nicm     1079:                for (i = 0; i < screen_size_x(s); i++)
                   1080:                        tty_putc(tty, 'E');
1.1       nicm     1081:        }
                   1082: }
                   1083:
                   1084: void
1.24      nicm     1085: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1086: {
1.46      nicm     1087:        struct window_pane      *wp = ctx->wp;
                   1088:        struct screen           *s = wp->screen;
1.203     nicm     1089:        u_int                    cx, width;
1.46      nicm     1090:
1.203     nicm     1091:        if (ctx->ocy == ctx->orlower)
                   1092:                tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.46      nicm     1093:
1.57      nicm     1094:        /* Is the cursor in the very last position? */
1.193     nicm     1095:        width = ctx->cell->data.width;
1.102     nicm     1096:        if (ctx->ocx > wp->sx - width) {
1.113     nicm     1097:                if (ctx->xoff != 0 || wp->sx != tty->sx) {
1.57      nicm     1098:                        /*
                   1099:                         * The pane doesn't fill the entire line, the linefeed
                   1100:                         * will already have happened, so just move the cursor.
                   1101:                         */
1.125     nicm     1102:                        if (ctx->ocy != wp->yoff + wp->screen->rlower)
1.122     nicm     1103:                                tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
                   1104:                        else
                   1105:                                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.57      nicm     1106:                } else if (tty->cx < tty->sx) {
                   1107:                        /*
                   1108:                         * The cursor isn't in the last position already, so
1.102     nicm     1109:                         * move as far left as possible and redraw the last
1.57      nicm     1110:                         * cell to move into the last position.
                   1111:                         */
1.193     nicm     1112:                        cx = screen_size_x(s) - ctx->last_cell.data.width;
1.50      nicm     1113:                        tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1.176     nicm     1114:                        tty_cell(tty, &ctx->last_cell, wp);
1.50      nicm     1115:                }
                   1116:        } else
1.46      nicm     1117:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1118:
1.176     nicm     1119:        tty_cell(tty, ctx->cell, wp);
1.1       nicm     1120: }
                   1121:
                   1122: void
1.24      nicm     1123: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1124: {
1.53      nicm     1125:        struct window_pane      *wp = ctx->wp;
1.1       nicm     1126:
1.53      nicm     1127:        /*
                   1128:         * Cannot rely on not being a partial character, so just redraw the
                   1129:         * whole line.
                   1130:         */
1.176     nicm     1131:        tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.106     nicm     1132: }
                   1133:
                   1134: void
                   1135: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
                   1136: {
                   1137:        char    *buf;
                   1138:        size_t   off;
                   1139:
                   1140:        if (!tty_term_has(tty->term, TTYC_MS))
                   1141:                return;
                   1142:
                   1143:        off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
                   1144:        buf = xmalloc(off);
                   1145:
                   1146:        b64_ntop(ctx->ptr, ctx->num, buf, off);
                   1147:        tty_putcode_ptr2(tty, TTYC_MS, "", buf);
                   1148:
1.138     nicm     1149:        free(buf);
1.100     nicm     1150: }
                   1151:
                   1152: void
                   1153: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
                   1154: {
                   1155:        u_int    i;
                   1156:        u_char  *str = ctx->ptr;
                   1157:
                   1158:        for (i = 0; i < ctx->num; i++)
                   1159:                tty_putc(tty, str[i]);
1.141     nicm     1160:
                   1161:        tty->cx = tty->cy = UINT_MAX;
                   1162:        tty->rupper = tty->rlower = UINT_MAX;
                   1163:
1.176     nicm     1164:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.141     nicm     1165:        tty_cursor(tty, 0, 0);
1.1       nicm     1166: }
                   1167:
1.207     nicm     1168: static void
1.176     nicm     1169: tty_cell(struct tty *tty, const struct grid_cell *gc,
                   1170:     const struct window_pane *wp)
1.1       nicm     1171: {
1.193     nicm     1172:        u_int   i;
1.1       nicm     1173:
                   1174:        /* Skip last character if terminal is stupid. */
1.211   ! nicm     1175:        if ((tty->term->flags & TERM_EARLYWRAP) &&
        !          1176:            tty->cy == tty->sy - 1 &&
        !          1177:            tty->cx == tty->sx - 1)
1.1       nicm     1178:                return;
                   1179:
                   1180:        /* If this is a padding character, do nothing. */
                   1181:        if (gc->flags & GRID_FLAG_PADDING)
                   1182:                return;
                   1183:
                   1184:        /* Set the attributes. */
1.176     nicm     1185:        tty_attributes(tty, gc, wp);
1.1       nicm     1186:
1.147     nicm     1187:        /* Get the cell and if ASCII write with putc to do ACS translation. */
1.193     nicm     1188:        if (gc->data.size == 1) {
                   1189:                if (*gc->data.data < 0x20 || *gc->data.data == 0x7f)
1.1       nicm     1190:                        return;
1.193     nicm     1191:                tty_putc(tty, *gc->data.data);
1.1       nicm     1192:                return;
                   1193:        }
                   1194:
1.147     nicm     1195:        /* If not UTF-8, write _. */
1.1       nicm     1196:        if (!(tty->flags & TTY_UTF8)) {
1.193     nicm     1197:                for (i = 0; i < gc->data.width; i++)
1.1       nicm     1198:                        tty_putc(tty, '_');
                   1199:                return;
                   1200:        }
                   1201:
1.147     nicm     1202:        /* Write the data. */
1.193     nicm     1203:        tty_putn(tty, gc->data.data, gc->data.size, gc->data.width);
1.1       nicm     1204: }
                   1205:
                   1206: void
                   1207: tty_reset(struct tty *tty)
                   1208: {
                   1209:        struct grid_cell        *gc = &tty->cell;
                   1210:
1.205     nicm     1211:        if (grid_cells_equal(gc, &grid_default_cell))
1.1       nicm     1212:                return;
                   1213:
1.90      nicm     1214:        if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1       nicm     1215:                tty_putcode(tty, TTYC_RMACS);
                   1216:        tty_putcode(tty, TTYC_SGR0);
                   1217:        memcpy(gc, &grid_default_cell, sizeof *gc);
                   1218: }
                   1219:
1.40      nicm     1220: /* Set region inside pane. */
1.208     nicm     1221: static void
1.196     nicm     1222: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
                   1223:     u_int rlower)
1.1       nicm     1224: {
1.113     nicm     1225:        tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39      nicm     1226: }
                   1227:
1.40      nicm     1228: /* Set region at absolute position. */
1.39      nicm     1229: void
1.40      nicm     1230: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm     1231: {
                   1232:        if (tty->rlower == rlower && tty->rupper == rupper)
                   1233:                return;
1.1       nicm     1234:        if (!tty_term_has(tty->term, TTYC_CSR))
                   1235:                return;
1.39      nicm     1236:
                   1237:        tty->rupper = rupper;
                   1238:        tty->rlower = rlower;
1.55      nicm     1239:
                   1240:        /*
                   1241:         * Some terminals (such as PuTTY) do not correctly reset the cursor to
                   1242:         * 0,0 if it is beyond the last column (they do not reset their wrap
                   1243:         * flag so further output causes a line feed). As a workaround, do an
                   1244:         * explicit move to 0 first.
                   1245:         */
                   1246:        if (tty->cx >= tty->sx)
                   1247:                tty_cursor(tty, 0, tty->cy);
1.42      nicm     1248:
1.39      nicm     1249:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78      nicm     1250:        tty_cursor(tty, 0, 0);
1.1       nicm     1251: }
                   1252:
1.42      nicm     1253: /* Move cursor inside pane. */
1.208     nicm     1254: static void
1.41      nicm     1255: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                   1256: {
1.113     nicm     1257:        tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41      nicm     1258: }
                   1259:
1.42      nicm     1260: /* Move cursor to absolute position. */
1.41      nicm     1261: void
                   1262: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm     1263: {
1.42      nicm     1264:        struct tty_term *term = tty->term;
                   1265:        u_int            thisx, thisy;
                   1266:        int              change;
1.77      nicm     1267:
1.42      nicm     1268:        if (cx > tty->sx - 1)
                   1269:                cx = tty->sx - 1;
                   1270:
                   1271:        thisx = tty->cx;
                   1272:        thisy = tty->cy;
                   1273:
                   1274:        /* No change. */
                   1275:        if (cx == thisx && cy == thisy)
                   1276:                return;
                   1277:
1.43      nicm     1278:        /* Very end of the line, just use absolute movement. */
                   1279:        if (thisx > tty->sx - 1)
                   1280:                goto absolute;
                   1281:
1.42      nicm     1282:        /* Move to home position (0, 0). */
                   1283:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                   1284:                tty_putcode(tty, TTYC_HOME);
                   1285:                goto out;
                   1286:        }
                   1287:
                   1288:        /* Zero on the next line. */
1.48      nicm     1289:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1       nicm     1290:                tty_putc(tty, '\r');
1.42      nicm     1291:                tty_putc(tty, '\n');
                   1292:                goto out;
                   1293:        }
                   1294:
1.45      nicm     1295:        /* Moving column or row. */
1.42      nicm     1296:        if (cy == thisy) {
1.45      nicm     1297:                /*
                   1298:                 * Moving column only, row staying the same.
                   1299:                 */
                   1300:
1.42      nicm     1301:                /* To left edge. */
1.202     nicm     1302:                if (cx == 0) {
1.42      nicm     1303:                        tty_putc(tty, '\r');
                   1304:                        goto out;
                   1305:                }
                   1306:
                   1307:                /* One to the left. */
                   1308:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   1309:                        tty_putcode(tty, TTYC_CUB1);
                   1310:                        goto out;
                   1311:                }
                   1312:
                   1313:                /* One to the right. */
                   1314:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   1315:                        tty_putcode(tty, TTYC_CUF1);
                   1316:                        goto out;
                   1317:                }
                   1318:
                   1319:                /* Calculate difference. */
                   1320:                change = thisx - cx;    /* +ve left, -ve right */
                   1321:
                   1322:                /*
                   1323:                 * Use HPA if change is larger than absolute, otherwise move
                   1324:                 * the cursor with CUB/CUF.
                   1325:                 */
1.87      nicm     1326:                if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42      nicm     1327:                        tty_putcode1(tty, TTYC_HPA, cx);
                   1328:                        goto out;
                   1329:                } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202     nicm     1330:                        if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
                   1331:                                tty_putcode(tty, TTYC_CUB1);
                   1332:                                tty_putcode(tty, TTYC_CUB1);
                   1333:                                goto out;
                   1334:                        }
1.42      nicm     1335:                        tty_putcode1(tty, TTYC_CUB, change);
                   1336:                        goto out;
                   1337:                } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
                   1338:                        tty_putcode1(tty, TTYC_CUF, -change);
                   1339:                        goto out;
                   1340:                }
1.45      nicm     1341:        } else if (cx == thisx) {
                   1342:                /*
                   1343:                 * Moving row only, column staying the same.
                   1344:                 */
1.42      nicm     1345:
                   1346:                /* One above. */
1.77      nicm     1347:                if (thisy != tty->rupper &&
1.42      nicm     1348:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   1349:                        tty_putcode(tty, TTYC_CUU1);
                   1350:                        goto out;
                   1351:                }
                   1352:
                   1353:                /* One below. */
1.49      nicm     1354:                if (thisy != tty->rlower &&
1.42      nicm     1355:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   1356:                        tty_putcode(tty, TTYC_CUD1);
                   1357:                        goto out;
                   1358:                }
                   1359:
                   1360:                /* Calculate difference. */
                   1361:                change = thisy - cy;    /* +ve up, -ve down */
                   1362:
                   1363:                /*
1.77      nicm     1364:                 * Try to use VPA if change is larger than absolute or if this
                   1365:                 * change would cross the scroll region, otherwise use CUU/CUD.
1.42      nicm     1366:                 */
1.87      nicm     1367:                if ((u_int) abs(change) > cy ||
1.42      nicm     1368:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     1369:                    (change > 0 && cy - change < tty->rupper)) {
                   1370:                            if (tty_term_has(term, TTYC_VPA)) {
                   1371:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   1372:                                    goto out;
                   1373:                            }
1.42      nicm     1374:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   1375:                        tty_putcode1(tty, TTYC_CUU, change);
                   1376:                        goto out;
                   1377:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   1378:                        tty_putcode1(tty, TTYC_CUD, -change);
                   1379:                        goto out;
                   1380:                }
                   1381:        }
                   1382:
1.43      nicm     1383: absolute:
1.42      nicm     1384:        /* Absolute movement. */
                   1385:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   1386:
                   1387: out:
                   1388:        tty->cx = cx;
                   1389:        tty->cy = cy;
1.1       nicm     1390: }
                   1391:
                   1392: void
1.176     nicm     1393: tty_attributes(struct tty *tty, const struct grid_cell *gc,
                   1394:     const struct window_pane *wp)
1.1       nicm     1395: {
1.63      nicm     1396:        struct grid_cell        *tc = &tty->cell, gc2;
1.85      nicm     1397:        u_char                   changed;
1.61      nicm     1398:
1.85      nicm     1399:        memcpy(&gc2, gc, sizeof gc2);
1.203     nicm     1400:        if (wp != NULL)
                   1401:                tty_default_colours(&gc2, wp);
1.63      nicm     1402:
1.18      nicm     1403:        /*
                   1404:         * If no setab, try to use the reverse attribute as a best-effort for a
                   1405:         * non-default background. This is a bit of a hack but it doesn't do
                   1406:         * any serious harm and makes a couple of applications happier.
                   1407:         */
                   1408:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85      nicm     1409:                if (gc2.attr & GRID_ATTR_REVERSE) {
                   1410:                        if (gc2.fg != 7 && gc2.fg != 8)
1.64      nicm     1411:                                gc2.attr &= ~GRID_ATTR_REVERSE;
1.18      nicm     1412:                } else {
1.85      nicm     1413:                        if (gc2.bg != 0 && gc2.bg != 8)
1.64      nicm     1414:                                gc2.attr |= GRID_ATTR_REVERSE;
1.18      nicm     1415:                }
                   1416:        }
1.1       nicm     1417:
1.85      nicm     1418:        /* Fix up the colours if necessary. */
                   1419:        tty_check_fg(tty, &gc2);
                   1420:        tty_check_bg(tty, &gc2);
                   1421:
1.64      nicm     1422:        /* If any bits are being cleared, reset everything. */
1.85      nicm     1423:        if (tc->attr & ~gc2.attr)
1.64      nicm     1424:                tty_reset(tty);
                   1425:
                   1426:        /*
                   1427:         * Set the colours. This may call tty_reset() (so it comes next) and
                   1428:         * may add to (NOT remove) the desired attributes by changing new_attr.
                   1429:         */
1.85      nicm     1430:        tty_colours(tty, &gc2);
1.64      nicm     1431:
1.1       nicm     1432:        /* Filter out attribute bits already set. */
1.85      nicm     1433:        changed = gc2.attr & ~tc->attr;
                   1434:        tc->attr = gc2.attr;
1.1       nicm     1435:
                   1436:        /* Set the attributes. */
                   1437:        if (changed & GRID_ATTR_BRIGHT)
                   1438:                tty_putcode(tty, TTYC_BOLD);
                   1439:        if (changed & GRID_ATTR_DIM)
                   1440:                tty_putcode(tty, TTYC_DIM);
1.180     nicm     1441:        if (changed & GRID_ATTR_ITALICS)
                   1442:                tty_set_italics(tty);
1.1       nicm     1443:        if (changed & GRID_ATTR_UNDERSCORE)
                   1444:                tty_putcode(tty, TTYC_SMUL);
                   1445:        if (changed & GRID_ATTR_BLINK)
                   1446:                tty_putcode(tty, TTYC_BLINK);
                   1447:        if (changed & GRID_ATTR_REVERSE) {
                   1448:                if (tty_term_has(tty->term, TTYC_REV))
                   1449:                        tty_putcode(tty, TTYC_REV);
                   1450:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   1451:                        tty_putcode(tty, TTYC_SMSO);
                   1452:        }
                   1453:        if (changed & GRID_ATTR_HIDDEN)
                   1454:                tty_putcode(tty, TTYC_INVIS);
1.90      nicm     1455:        if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1       nicm     1456:                tty_putcode(tty, TTYC_SMACS);
                   1457: }
                   1458:
1.207     nicm     1459: static void
1.85      nicm     1460: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1461: {
1.61      nicm     1462:        struct grid_cell        *tc = &tty->cell;
1.204     nicm     1463:        int                      have_ax;
1.61      nicm     1464:
                   1465:        /* No changes? Nothing is necessary. */
1.204     nicm     1466:        if (gc->fg == tc->fg && gc->bg == tc->bg)
1.63      nicm     1467:                return;
1.1       nicm     1468:
1.61      nicm     1469:        /*
                   1470:         * Is either the default colour? This is handled specially because the
                   1471:         * best solution might be to reset both colours to default, in which
                   1472:         * case if only one is default need to fall onward to set the other
                   1473:         * colour.
                   1474:         */
1.204     nicm     1475:        if (gc->fg == 8 || gc->bg == 8) {
1.61      nicm     1476:                /*
                   1477:                 * If don't have AX but do have op, send sgr0 (op can't
                   1478:                 * actually be used because it is sometimes the same as sgr0
                   1479:                 * and sometimes isn't). This resets both colours to default.
                   1480:                 *
                   1481:                 * Otherwise, try to set the default colour only as needed.
                   1482:                 */
1.174     nicm     1483:                have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61      nicm     1484:                if (!have_ax && tty_term_has(tty->term, TTYC_OP))
                   1485:                        tty_reset(tty);
                   1486:                else {
1.204     nicm     1487:                        if (gc->fg == 8 && tc->fg != 8) {
1.61      nicm     1488:                                if (have_ax)
                   1489:                                        tty_puts(tty, "\033[39m");
1.204     nicm     1490:                                else if (tc->fg != 7)
1.61      nicm     1491:                                        tty_putcode1(tty, TTYC_SETAF, 7);
                   1492:                                tc->fg = 8;
                   1493:                        }
1.204     nicm     1494:                        if (gc->bg == 8 && tc->bg != 8) {
1.77      nicm     1495:                                if (have_ax)
1.61      nicm     1496:                                        tty_puts(tty, "\033[49m");
1.204     nicm     1497:                                else if (tc->bg != 0)
1.61      nicm     1498:                                        tty_putcode1(tty, TTYC_SETAB, 0);
                   1499:                                tc->bg = 8;
                   1500:                        }
                   1501:                }
                   1502:        }
1.1       nicm     1503:
1.61      nicm     1504:        /* Set the foreground colour. */
1.204     nicm     1505:        if (gc->fg != 8 && gc->fg != tc->fg)
1.85      nicm     1506:                tty_colours_fg(tty, gc);
1.1       nicm     1507:
1.61      nicm     1508:        /*
                   1509:         * Set the background colour. This must come after the foreground as
                   1510:         * tty_colour_fg() can call tty_reset().
                   1511:         */
1.204     nicm     1512:        if (gc->bg != 8 && gc->bg != tc->bg)
1.73      nicm     1513:                tty_colours_bg(tty, gc);
1.1       nicm     1514: }
                   1515:
                   1516: void
1.85      nicm     1517: tty_check_fg(struct tty *tty, struct grid_cell *gc)
                   1518: {
1.204     nicm     1519:        u_char  r, g, b;
                   1520:        u_int   colours;
1.85      nicm     1521:
1.199     nicm     1522:        /* Is this a 24-bit colour? */
1.204     nicm     1523:        if (gc->fg & COLOUR_FLAG_RGB) {
1.199     nicm     1524:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
                   1525:                if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204     nicm     1526:                        colour_split_rgb(gc->fg, &r, &g, &b);
                   1527:                        gc->fg = colour_find_rgb(r, g, b);
                   1528:                } else
1.200     nicm     1529:                        return;
1.199     nicm     1530:        }
1.175     nicm     1531:        colours = tty_term_number(tty->term, TTYC_COLORS);
                   1532:
1.85      nicm     1533:        /* Is this a 256-colour colour? */
1.204     nicm     1534:        if (gc->fg & COLOUR_FLAG_256) {
1.85      nicm     1535:                /* And not a 256 colour mode? */
1.158     nicm     1536:                if (!(tty->term->flags & TERM_256COLOURS) &&
1.85      nicm     1537:                    !(tty->term_flags & TERM_256COLOURS)) {
                   1538:                        gc->fg = colour_256to16(gc->fg);
                   1539:                        if (gc->fg & 8) {
                   1540:                                gc->fg &= 7;
1.175     nicm     1541:                                if (colours >= 16)
                   1542:                                        gc->fg += 90;
                   1543:                                else
                   1544:                                        gc->attr |= GRID_ATTR_BRIGHT;
1.85      nicm     1545:                        } else
                   1546:                                gc->attr &= ~GRID_ATTR_BRIGHT;
                   1547:                }
                   1548:                return;
                   1549:        }
                   1550:
                   1551:        /* Is this an aixterm colour? */
                   1552:        if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
                   1553:                gc->fg -= 90;
                   1554:                gc->attr |= GRID_ATTR_BRIGHT;
                   1555:        }
                   1556: }
                   1557:
                   1558: void
                   1559: tty_check_bg(struct tty *tty, struct grid_cell *gc)
                   1560: {
1.204     nicm     1561:        u_char  r, g, b;
                   1562:        u_int   colours;
1.85      nicm     1563:
1.199     nicm     1564:        /* Is this a 24-bit colour? */
1.204     nicm     1565:        if (gc->bg & COLOUR_FLAG_RGB) {
1.199     nicm     1566:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
                   1567:                if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204     nicm     1568:                        colour_split_rgb(gc->bg, &r, &g, &b);
                   1569:                        gc->bg = colour_find_rgb(r, g, b);
                   1570:                } else
1.200     nicm     1571:                        return;
1.199     nicm     1572:        }
1.175     nicm     1573:        colours = tty_term_number(tty->term, TTYC_COLORS);
                   1574:
1.85      nicm     1575:        /* Is this a 256-colour colour? */
1.204     nicm     1576:        if (gc->bg & COLOUR_FLAG_256) {
1.85      nicm     1577:                /*
                   1578:                 * And not a 256 colour mode? Translate to 16-colour
                   1579:                 * palette. Bold background doesn't exist portably, so just
                   1580:                 * discard the bold bit if set.
                   1581:                 */
1.158     nicm     1582:                if (!(tty->term->flags & TERM_256COLOURS) &&
1.85      nicm     1583:                    !(tty->term_flags & TERM_256COLOURS)) {
                   1584:                        gc->bg = colour_256to16(gc->bg);
1.175     nicm     1585:                        if (gc->bg & 8) {
1.85      nicm     1586:                                gc->bg &= 7;
1.175     nicm     1587:                                if (colours >= 16)
                   1588:                                        gc->fg += 90;
                   1589:                        }
1.85      nicm     1590:                }
                   1591:                return;
                   1592:        }
                   1593:
                   1594:        /* Is this an aixterm colour? */
1.175     nicm     1595:        if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85      nicm     1596:                gc->bg -= 90;
                   1597: }
                   1598:
1.207     nicm     1599: static void
1.85      nicm     1600: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1601: {
1.61      nicm     1602:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     1603:        char                     s[32];
1.1       nicm     1604:
1.204     nicm     1605:        /* Is this a 24-bit or 256-colour colour? */
                   1606:        if (gc->fg & COLOUR_FLAG_RGB ||
                   1607:            gc->fg & COLOUR_FLAG_256) {
                   1608:                if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61      nicm     1609:                        goto save_fg;
1.199     nicm     1610:                /* Should not get here, already converted in tty_check_fg. */
1.85      nicm     1611:                return;
1.1       nicm     1612:        }
                   1613:
1.79      nicm     1614:        /* Is this an aixterm bright colour? */
1.204     nicm     1615:        if (gc->fg >= 90 && gc->fg <= 97) {
                   1616:                xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
1.85      nicm     1617:                tty_puts(tty, s);
                   1618:                goto save_fg;
1.79      nicm     1619:        }
                   1620:
1.61      nicm     1621:        /* Otherwise set the foreground colour. */
1.204     nicm     1622:        tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61      nicm     1623:
1.77      nicm     1624: save_fg:
1.61      nicm     1625:        /* Save the new values in the terminal current cell. */
1.204     nicm     1626:        tc->fg = gc->fg;
1.1       nicm     1627: }
                   1628:
1.207     nicm     1629: static void
1.73      nicm     1630: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1631: {
1.61      nicm     1632:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     1633:        char                     s[32];
1.1       nicm     1634:
1.204     nicm     1635:        /* Is this a 24-bit or 256-colour colour? */
                   1636:        if (gc->bg & COLOUR_FLAG_RGB ||
                   1637:            gc->bg & COLOUR_FLAG_256) {
                   1638:                if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61      nicm     1639:                        goto save_bg;
1.199     nicm     1640:                /* Should not get here, already converted in tty_check_bg. */
1.85      nicm     1641:                return;
1.79      nicm     1642:        }
                   1643:
                   1644:        /* Is this an aixterm bright colour? */
1.204     nicm     1645:        if (gc->bg >= 90 && gc->bg <= 97) {
                   1646:                xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
1.175     nicm     1647:                tty_puts(tty, s);
                   1648:                goto save_bg;
1.1       nicm     1649:        }
                   1650:
1.61      nicm     1651:        /* Otherwise set the background colour. */
1.204     nicm     1652:        tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61      nicm     1653:
                   1654: save_bg:
                   1655:        /* Save the new values in the terminal current cell. */
1.204     nicm     1656:        tc->bg = gc->bg;
1.61      nicm     1657: }
                   1658:
1.207     nicm     1659: static int
1.204     nicm     1660: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61      nicm     1661: {
1.204     nicm     1662:        u_char  r, g, b;
1.61      nicm     1663:        char    s[32];
                   1664:
1.204     nicm     1665:        if (colour & COLOUR_FLAG_256) {
                   1666:                /*
                   1667:                 * If the user has specified -2 to the client, setaf and setab
                   1668:                 * may not work (or they may not want to use them), so send the
                   1669:                 * usual sequence.
                   1670:                 */
                   1671:                if (tty->term_flags & TERM_256COLOURS)
                   1672:                        goto fallback_256;
1.189     nicm     1673:
1.204     nicm     1674:                /*
                   1675:                 * If the terminfo entry has 256 colours and setaf and setab
                   1676:                 * exist, assume that they work correctly.
                   1677:                 */
                   1678:                if (tty->term->flags & TERM_256COLOURS) {
                   1679:                        if (*type == '3') {
                   1680:                                if (!tty_term_has(tty->term, TTYC_SETAF))
                   1681:                                        goto fallback_256;
                   1682:                                tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
                   1683:                        } else {
                   1684:                                if (!tty_term_has(tty->term, TTYC_SETAB))
                   1685:                                        goto fallback_256;
                   1686:                                tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
                   1687:                        }
                   1688:                        return (0);
1.188     nicm     1689:                }
1.204     nicm     1690:                goto fallback_256;
                   1691:        }
                   1692:
                   1693:        if (colour & COLOUR_FLAG_RGB) {
                   1694:                if (!tty_term_flag(tty->term, TTYC_TC))
                   1695:                        return (-1);
                   1696:
                   1697:                colour_split_rgb(colour & 0xffffff, &r, &g, &b);
                   1698:                xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type,
                   1699:                    r, g, b);
                   1700:                tty_puts(tty, s);
1.165     nicm     1701:                return (0);
                   1702:        }
1.61      nicm     1703:
1.165     nicm     1704:        return (-1);
1.188     nicm     1705:
1.204     nicm     1706: fallback_256:
                   1707:        xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.188     nicm     1708:        tty_puts(tty, s);
                   1709:        return (0);
1.176     nicm     1710: }
                   1711:
1.207     nicm     1712: static void
1.176     nicm     1713: tty_default_colours(struct grid_cell *gc, const struct window_pane *wp)
                   1714: {
1.203     nicm     1715:        struct window           *w = wp->window;
                   1716:        struct options          *oo = w->options;
                   1717:        const struct grid_cell  *agc, *pgc, *wgc;
                   1718:
                   1719:        if (w->flags & WINDOW_STYLECHANGED) {
                   1720:                w->flags &= ~WINDOW_STYLECHANGED;
                   1721:                agc = options_get_style(oo, "window-active-style");
                   1722:                memcpy(&w->active_style, agc, sizeof w->active_style);
                   1723:                wgc = options_get_style(oo, "window-style");
                   1724:                memcpy(&w->style, wgc, sizeof w->style);
                   1725:        } else {
                   1726:                agc = &w->active_style;
                   1727:                wgc = &w->style;
                   1728:        }
1.176     nicm     1729:        pgc = &wp->colgc;
                   1730:
1.204     nicm     1731:        if (gc->fg == 8) {
                   1732:                if (pgc->fg != 8)
1.176     nicm     1733:                        gc->fg = pgc->fg;
1.204     nicm     1734:                else if (wp == w->active && agc->fg != 8)
1.176     nicm     1735:                        gc->fg = agc->fg;
1.204     nicm     1736:                else
1.176     nicm     1737:                        gc->fg = wgc->fg;
                   1738:        }
                   1739:
1.204     nicm     1740:        if (gc->bg == 8) {
                   1741:                if (pgc->bg != 8)
1.176     nicm     1742:                        gc->bg = pgc->bg;
1.204     nicm     1743:                else if (wp == w->active && agc->bg != 8)
1.176     nicm     1744:                        gc->bg = agc->bg;
1.204     nicm     1745:                else
1.176     nicm     1746:                        gc->bg = wgc->bg;
                   1747:        }
1.210     nicm     1748: }
                   1749:
                   1750: static void
                   1751: tty_default_attributes(struct tty *tty, const struct window_pane *wp, u_int bg)
                   1752: {
                   1753:        static struct grid_cell gc;
                   1754:
                   1755:        memcpy(&gc, &grid_default_cell, sizeof gc);
                   1756:        gc.bg = bg;
                   1757:        tty_attributes(tty, &gc, wp);
1.1       nicm     1758: }