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

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