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

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