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

1.230   ! nicm        1: /* $OpenBSD: tty.c,v 1.229 2017/02/06 22:05:11 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.1       nicm      448: }
                    449:
                    450: void
                    451: tty_putc(struct tty *tty, u_char ch)
                    452: {
1.90      nicm      453:        const char      *acs;
1.1       nicm      454:
1.90      nicm      455:        if (tty->cell.attr & GRID_ATTR_CHARSET) {
                    456:                acs = tty_acs_get(tty, ch);
                    457:                if (acs != NULL)
                    458:                        bufferevent_write(tty->event, acs, strlen(acs));
                    459:                else
                    460:                        bufferevent_write(tty->event, &ch, 1);
                    461:        } else
                    462:                bufferevent_write(tty->event, &ch, 1);
1.1       nicm      463:
                    464:        if (ch >= 0x20 && ch != 0x7f) {
1.211     nicm      465:                if (tty->cx >= tty->sx) {
1.46      nicm      466:                        tty->cx = 1;
                    467:                        if (tty->cy != tty->rlower)
                    468:                                tty->cy++;
1.211     nicm      469:
                    470:                        /*
                    471:                         * On !xenl terminals, force the cursor position to
                    472:                         * where we think it should be after a line wrap - this
                    473:                         * means it works on sensible terminals as well.
                    474:                         */
                    475:                        if (tty->term->flags & TERM_EARLYWRAP)
                    476:                                tty_putcode2(tty, TTYC_CUP, tty->cy, tty->cx);
1.1       nicm      477:                } else
                    478:                        tty->cx++;
                    479:        }
                    480:
1.192     nicm      481:        if (tty_log_fd != -1)
                    482:                write(tty_log_fd, &ch, 1);
1.1       nicm      483: }
                    484:
                    485: void
1.147     nicm      486: tty_putn(struct tty *tty, const void *buf, size_t len, u_int width)
1.5       nicm      487: {
1.147     nicm      488:        bufferevent_write(tty->event, buf, len);
1.192     nicm      489:        if (tty_log_fd != -1)
                    490:                write(tty_log_fd, buf, len);
1.147     nicm      491:        tty->cx += width;
1.5       nicm      492: }
                    493:
1.207     nicm      494: static void
1.180     nicm      495: tty_set_italics(struct tty *tty)
                    496: {
                    497:        const char      *s;
                    498:
                    499:        if (tty_term_has(tty->term, TTYC_SITM)) {
1.191     nicm      500:                s = options_get_string(global_options, "default-terminal");
1.180     nicm      501:                if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) {
                    502:                        tty_putcode(tty, TTYC_SITM);
                    503:                        return;
                    504:                }
                    505:        }
                    506:        tty_putcode(tty, TTYC_SMSO);
                    507: }
                    508:
                    509: void
1.1       nicm      510: tty_set_title(struct tty *tty, const char *title)
                    511: {
1.105     nicm      512:        if (!tty_term_has(tty->term, TTYC_TSL) ||
                    513:            !tty_term_has(tty->term, TTYC_FSL))
1.1       nicm      514:                return;
                    515:
1.105     nicm      516:        tty_putcode(tty, TTYC_TSL);
1.1       nicm      517:        tty_puts(tty, title);
1.105     nicm      518:        tty_putcode(tty, TTYC_FSL);
1.1       nicm      519: }
                    520:
1.208     nicm      521: static void
1.107     nicm      522: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
                    523: {
                    524:        if (*ccolour == '\0')
                    525:                tty_putcode(tty, TTYC_CR);
                    526:        else
1.160     nicm      527:                tty_putcode_ptr1(tty, TTYC_CS, ccolour);
1.138     nicm      528:        free(tty->ccolour);
1.107     nicm      529:        tty->ccolour = xstrdup(ccolour);
                    530: }
                    531:
                    532: void
                    533: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1       nicm      534: {
                    535:        int     changed;
                    536:
1.197     nicm      537:        if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0)
1.107     nicm      538:                tty_force_cursor_colour(tty, s->ccolour);
                    539:
1.1       nicm      540:        if (tty->flags & TTY_NOCURSOR)
                    541:                mode &= ~MODE_CURSOR;
                    542:
                    543:        changed = mode ^ tty->mode;
1.183     nicm      544:        if (changed & MODE_BLINKING) {
                    545:                if (tty_term_has(tty->term, TTYC_CVVIS))
                    546:                        tty_putcode(tty, TTYC_CVVIS);
                    547:                else
                    548:                        tty_putcode(tty, TTYC_CNORM);
                    549:                changed |= MODE_CURSOR;
                    550:        }
                    551:        if (changed & MODE_CURSOR) {
                    552:                if (mode & MODE_CURSOR)
                    553:                        tty_putcode(tty, TTYC_CNORM);
                    554:                else
1.1       nicm      555:                        tty_putcode(tty, TTYC_CIVIS);
1.108     nicm      556:        }
1.181     nicm      557:        if (s != NULL && tty->cstyle != s->cstyle) {
1.160     nicm      558:                if (tty_term_has(tty->term, TTYC_SS)) {
1.108     nicm      559:                        if (s->cstyle == 0 &&
1.160     nicm      560:                            tty_term_has(tty->term, TTYC_SE))
                    561:                                tty_putcode(tty, TTYC_SE);
1.108     nicm      562:                        else
1.160     nicm      563:                                tty_putcode1(tty, TTYC_SS, s->cstyle);
1.108     nicm      564:                }
                    565:                tty->cstyle = s->cstyle;
1.1       nicm      566:        }
1.195     nicm      567:        if (changed & ALL_MOUSE_MODES) {
1.94      nicm      568:                if (mode & ALL_MOUSE_MODES) {
1.153     nicm      569:                        /*
                    570:                         * Enable the SGR (1006) extension unconditionally, as
1.221     nicm      571:                         * it is safe from misinterpretation.
1.153     nicm      572:                         */
                    573:                        tty_puts(tty, "\033[?1006h");
1.225     nicm      574:                        if (mode & MODE_MOUSE_ALL)
                    575:                                tty_puts(tty, "\033[?1003h");
                    576:                        else if (mode & MODE_MOUSE_BUTTON)
1.94      nicm      577:                                tty_puts(tty, "\033[?1002h");
1.98      nicm      578:                        else if (mode & MODE_MOUSE_STANDARD)
                    579:                                tty_puts(tty, "\033[?1000h");
1.86      nicm      580:                } else {
1.225     nicm      581:                        if (tty->mode & MODE_MOUSE_ALL)
                    582:                                tty_puts(tty, "\033[?1003l");
                    583:                        else if (tty->mode & MODE_MOUSE_BUTTON)
1.94      nicm      584:                                tty_puts(tty, "\033[?1002l");
1.98      nicm      585:                        else if (tty->mode & MODE_MOUSE_STANDARD)
                    586:                                tty_puts(tty, "\033[?1000l");
1.153     nicm      587:                        tty_puts(tty, "\033[?1006l");
1.86      nicm      588:                }
1.76      nicm      589:        }
                    590:        if (changed & MODE_KKEYPAD) {
                    591:                if (mode & MODE_KKEYPAD)
                    592:                        tty_putcode(tty, TTYC_SMKX);
                    593:                else
                    594:                        tty_putcode(tty, TTYC_RMKX);
1.115     nicm      595:        }
                    596:        if (changed & MODE_BRACKETPASTE) {
                    597:                if (mode & MODE_BRACKETPASTE)
                    598:                        tty_puts(tty, "\033[?2004h");
                    599:                else
                    600:                        tty_puts(tty, "\033[?2004l");
1.1       nicm      601:        }
                    602:        tty->mode = mode;
                    603: }
                    604:
1.207     nicm      605: static void
1.168     nicm      606: tty_emulate_repeat(struct tty *tty, enum tty_code_code code,
                    607:     enum tty_code_code code1, u_int n)
1.1       nicm      608: {
                    609:        if (tty_term_has(tty->term, code))
                    610:                tty_putcode1(tty, code, n);
                    611:        else {
                    612:                while (n-- > 0)
                    613:                        tty_putcode(tty, code1);
                    614:        }
                    615: }
                    616:
1.207     nicm      617: static void
1.133     nicm      618: tty_repeat_space(struct tty *tty, u_int n)
                    619: {
                    620:        while (n-- > 0)
                    621:                tty_putc(tty, ' ');
                    622: }
                    623:
1.1       nicm      624: /*
1.119     nicm      625:  * Is the region large enough to be worth redrawing once later rather than
                    626:  * probably several times now? Currently yes if it is more than 50% of the
                    627:  * pane.
                    628:  */
1.207     nicm      629: static int
1.194     nicm      630: tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx)
1.119     nicm      631: {
                    632:        struct window_pane      *wp = ctx->wp;
                    633:
                    634:        return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
                    635: }
                    636:
                    637: /*
1.176     nicm      638:  * Return if BCE is needed but the terminal doesn't have it - it'll need to be
                    639:  * emulated.
                    640:  */
1.207     nicm      641: static int
1.210     nicm      642: tty_fake_bce(const struct tty *tty, const struct window_pane *wp, u_int bg)
1.176     nicm      643: {
                    644:        struct grid_cell        gc;
                    645:
1.210     nicm      646:        if (tty_term_flag(tty->term, TTYC_BCE))
                    647:                return (0);
                    648:
1.176     nicm      649:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.203     nicm      650:        if (wp != NULL)
                    651:                tty_default_colours(&gc, wp);
1.176     nicm      652:
1.210     nicm      653:        if (bg != 8 || gc.bg != 8)
                    654:                return (1);
                    655:        return (0);
1.176     nicm      656: }
                    657:
                    658: /*
1.1       nicm      659:  * Redraw scroll region using data from screen (already updated). Used when
                    660:  * CSR not supported, or window is a pane that doesn't take up the full
                    661:  * width of the terminal.
                    662:  */
1.207     nicm      663: static void
1.24      nicm      664: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      665: {
1.14      nicm      666:        struct window_pane      *wp = ctx->wp;
                    667:        struct screen           *s = wp->screen;
1.204     nicm      668:        u_int                    i;
1.1       nicm      669:
                    670:        /*
1.119     nicm      671:         * If region is large, schedule a window redraw. In most cases this is
                    672:         * likely to be followed by some more scrolling.
1.1       nicm      673:         */
1.119     nicm      674:        if (tty_large_region(tty, ctx)) {
1.1       nicm      675:                wp->flags |= PANE_REDRAW;
                    676:                return;
                    677:        }
                    678:
1.14      nicm      679:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    680:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.176     nicm      681:                        tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1       nicm      682:        } else {
1.14      nicm      683:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.176     nicm      684:                        tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff);
1.1       nicm      685:        }
                    686: }
                    687:
                    688: void
1.176     nicm      689: tty_draw_pane(struct tty *tty, const struct window_pane *wp, u_int py, u_int ox,
                    690:     u_int oy)
                    691: {
                    692:        tty_draw_line(tty, wp, wp->screen, py, ox, oy);
                    693: }
                    694:
                    695: void
                    696: tty_draw_line(struct tty *tty, const struct window_pane *wp,
                    697:     struct screen *s, u_int py, u_int ox, u_int oy)
1.1       nicm      698: {
1.209     nicm      699:        struct grid_cell         gc, tmp_gc;
1.46      nicm      700:        struct grid_line        *gl;
1.1       nicm      701:        u_int                    i, sx;
1.181     nicm      702:        int                      flags;
1.1       nicm      703:
1.181     nicm      704:        flags = tty->flags & TTY_NOCURSOR;
                    705:        tty->flags |= TTY_NOCURSOR;
                    706:        tty_update_mode(tty, tty->mode, s);
1.35      nicm      707:
1.214     nicm      708:        tty_region_off(tty);
                    709:        tty_margin_off(tty);
                    710:
1.1       nicm      711:        sx = screen_size_x(s);
1.19      nicm      712:        if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
                    713:                sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1       nicm      714:        if (sx > tty->sx)
                    715:                sx = tty->sx;
                    716:
1.46      nicm      717:        /*
1.174     nicm      718:         * Don't move the cursor to the start position if it will wrap there
1.50      nicm      719:         * itself.
1.46      nicm      720:         */
                    721:        gl = NULL;
                    722:        if (py != 0)
                    723:                gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83      nicm      724:        if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47      nicm      725:            tty->cx < tty->sx || ox != 0 ||
                    726:            (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46      nicm      727:                tty_cursor(tty, ox, oy + py);
                    728:
1.1       nicm      729:        for (i = 0; i < sx; i++) {
1.193     nicm      730:                grid_view_get_cell(s->grid, i, py, &gc);
1.209     nicm      731:                if (gc.flags & GRID_FLAG_SELECTED) {
                    732:                        screen_select_cell(s, &tmp_gc, &gc);
                    733:                        tty_cell(tty, &tmp_gc, wp);
                    734:                } else
                    735:                        tty_cell(tty, &gc, wp);
1.1       nicm      736:        }
                    737:
1.181     nicm      738:        if (sx < tty->sx) {
1.210     nicm      739:                tty_default_attributes(tty, wp, 8);
1.181     nicm      740:
                    741:                tty_cursor(tty, ox + sx, oy + py);
                    742:                if (sx != screen_size_x(s) &&
                    743:                    ox + screen_size_x(s) >= tty->sx &&
                    744:                    tty_term_has(tty->term, TTYC_EL) &&
1.210     nicm      745:                    !tty_fake_bce(tty, wp, 8))
1.181     nicm      746:                        tty_putcode(tty, TTYC_EL);
                    747:                else
                    748:                        tty_repeat_space(tty, screen_size_x(s) - sx);
1.35      nicm      749:        }
1.1       nicm      750:
1.181     nicm      751:        tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags;
1.107     nicm      752:        tty_update_mode(tty, tty->mode, s);
1.15      nicm      753: }
                    754:
1.201     nicm      755: static int
1.182     nicm      756: tty_client_ready(struct client *c, struct window_pane *wp)
                    757: {
                    758:        if (c->session == NULL || c->tty.term == NULL)
                    759:                return (0);
                    760:        if (c->flags & CLIENT_SUSPENDED)
                    761:                return (0);
                    762:        if (c->tty.flags & TTY_FREEZE)
                    763:                return (0);
                    764:        if (c->session->curw->window != wp->window)
                    765:                return (0);
                    766:        return (1);
                    767: }
                    768:
1.15      nicm      769: void
1.182     nicm      770: tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *),
                    771:     struct tty_ctx *ctx)
1.15      nicm      772: {
                    773:        struct window_pane      *wp = ctx->wp;
                    774:        struct client           *c;
                    775:
1.75      nicm      776:        /* wp can be NULL if updating the screen but not the terminal. */
1.15      nicm      777:        if (wp == NULL)
                    778:                return;
                    779:
                    780:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    781:                return;
1.129     nicm      782:        if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15      nicm      783:                return;
                    784:
1.178     nicm      785:        TAILQ_FOREACH(c, &clients, entry) {
1.182     nicm      786:                if (!tty_client_ready(c, wp))
1.15      nicm      787:                        continue;
                    788:
1.139     nicm      789:                ctx->xoff = wp->xoff;
                    790:                ctx->yoff = wp->yoff;
                    791:                if (status_at_line(c) == 0)
                    792:                        ctx->yoff++;
1.113     nicm      793:
1.139     nicm      794:                cmdfn(&c->tty, ctx);
1.1       nicm      795:        }
                    796: }
                    797:
                    798: void
1.24      nicm      799: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      800: {
1.77      nicm      801:        struct window_pane      *wp = ctx->wp;
1.1       nicm      802:
1.210     nicm      803:        if (!tty_pane_full_width(tty, ctx) ||
                    804:            tty_fake_bce(tty, wp, ctx->bg) ||
                    805:            (!tty_term_has(tty->term, TTYC_ICH) &&
                    806:            !tty_term_has(tty->term, TTYC_ICH1))) {
1.176     nicm      807:                tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      808:                return;
                    809:        }
                    810:
1.210     nicm      811:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      812:
1.77      nicm      813:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      814:
1.206     nicm      815:        tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1       nicm      816: }
                    817:
                    818: void
1.24      nicm      819: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      820: {
1.77      nicm      821:        struct window_pane      *wp = ctx->wp;
1.1       nicm      822:
1.210     nicm      823:        if (!tty_pane_full_width(tty, ctx) ||
                    824:            tty_fake_bce(tty, wp, ctx->bg) ||
1.26      nicm      825:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    826:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.176     nicm      827:                tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      828:                return;
                    829:        }
                    830:
1.210     nicm      831:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      832:
1.77      nicm      833:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      834:
1.206     nicm      835:        tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.146     nicm      836: }
                    837:
                    838: void
                    839: tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx)
                    840: {
                    841:        u_int   i;
                    842:
1.176     nicm      843:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.146     nicm      844:
                    845:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    846:
1.210     nicm      847:        if (tty_term_has(tty->term, TTYC_ECH) &&
                    848:            !tty_fake_bce(tty, ctx->wp, ctx->bg))
1.146     nicm      849:                tty_putcode1(tty, TTYC_ECH, ctx->num);
                    850:        else {
                    851:                for (i = 0; i < ctx->num; i++)
                    852:                        tty_putc(tty, ' ');
                    853:        }
1.1       nicm      854: }
                    855:
                    856: void
1.24      nicm      857: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      858: {
1.210     nicm      859:        if (!tty_pane_full_width(tty, ctx) ||
                    860:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.77      nicm      861:            !tty_term_has(tty->term, TTYC_CSR) ||
1.72      nicm      862:            !tty_term_has(tty->term, TTYC_IL1)) {
1.14      nicm      863:                tty_redraw_region(tty, ctx);
1.1       nicm      864:                return;
                    865:        }
                    866:
1.210     nicm      867:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm      868:
1.77      nicm      869:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm      870:        tty_margin_off(tty);
1.77      nicm      871:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      872:
1.12      nicm      873:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      874: }
                    875:
                    876: void
1.24      nicm      877: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      878: {
1.210     nicm      879:        if (!tty_pane_full_width(tty, ctx) ||
                    880:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.72      nicm      881:            !tty_term_has(tty->term, TTYC_CSR) ||
                    882:            !tty_term_has(tty->term, TTYC_DL1)) {
1.14      nicm      883:                tty_redraw_region(tty, ctx);
1.1       nicm      884:                return;
                    885:        }
                    886:
1.210     nicm      887:        tty_default_attributes(tty, ctx->wp, ctx->bg);
1.1       nicm      888:
1.77      nicm      889:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm      890:        tty_margin_off(tty);
1.77      nicm      891:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      892:
1.12      nicm      893:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      894: }
                    895:
                    896: void
1.24      nicm      897: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      898: {
1.77      nicm      899:        struct window_pane      *wp = ctx->wp;
1.12      nicm      900:        struct screen           *s = wp->screen;
1.229     nicm      901:        u_int                    sx = screen_size_x(s);
1.1       nicm      902:
1.210     nicm      903:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      904:
1.77      nicm      905:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41      nicm      906:
1.210     nicm      907:        if (tty_pane_full_width(tty, ctx) &&
                    908:            !tty_fake_bce(tty, wp, ctx->bg) &&
1.176     nicm      909:            tty_term_has(tty->term, TTYC_EL))
1.1       nicm      910:                tty_putcode(tty, TTYC_EL);
1.133     nicm      911:        else
1.229     nicm      912:                tty_repeat_space(tty, sx);
1.1       nicm      913: }
                    914:
                    915: void
1.24      nicm      916: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      917: {
1.77      nicm      918:        struct window_pane      *wp = ctx->wp;
1.12      nicm      919:        struct screen           *s = wp->screen;
1.229     nicm      920:        u_int                    sx = screen_size_x(s);
1.1       nicm      921:
1.210     nicm      922:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      923:
1.41      nicm      924:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    925:
1.176     nicm      926:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm      927:            tty_term_has(tty->term, TTYC_EL) &&
                    928:            !tty_fake_bce(tty, wp, ctx->bg))
1.1       nicm      929:                tty_putcode(tty, TTYC_EL);
1.133     nicm      930:        else
1.229     nicm      931:                tty_repeat_space(tty, sx - ctx->ocx);
1.1       nicm      932: }
                    933:
                    934: void
1.24      nicm      935: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      936: {
1.210     nicm      937:        struct window_pane      *wp = ctx->wp;
                    938:
                    939:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm      940:
1.230   ! nicm      941:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
        !           942:
1.210     nicm      943:        if (ctx->xoff == 0 &&
                    944:            tty_term_has(tty->term, TTYC_EL1) &&
1.230   ! nicm      945:            !tty_fake_bce(tty, ctx->wp, ctx->bg))
1.1       nicm      946:                tty_putcode(tty, TTYC_EL1);
1.230   ! nicm      947:        else
1.133     nicm      948:                tty_repeat_space(tty, ctx->ocx + 1);
1.1       nicm      949: }
                    950:
                    951: void
1.24      nicm      952: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      953: {
1.56      nicm      954:        if (ctx->ocy != ctx->orupper)
                    955:                return;
                    956:
1.210     nicm      957:        if (!tty_pane_full_width(tty, ctx) ||
                    958:            tty_fake_bce(tty, ctx->wp, ctx->bg) ||
1.70      nicm      959:            !tty_term_has(tty->term, TTYC_CSR) ||
                    960:            !tty_term_has(tty->term, TTYC_RI)) {
1.14      nicm      961:                tty_redraw_region(tty, ctx);
1.1       nicm      962:                return;
                    963:        }
                    964:
1.176     nicm      965:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.77      nicm      966:
1.56      nicm      967:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.214     nicm      968:        tty_margin_off(tty);
1.56      nicm      969:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm      970:
1.56      nicm      971:        tty_putcode(tty, TTYC_RI);
1.1       nicm      972: }
                    973:
                    974: void
1.24      nicm      975: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      976: {
1.77      nicm      977:        struct window_pane      *wp = ctx->wp;
1.1       nicm      978:
1.56      nicm      979:        if (ctx->ocy != ctx->orlower)
                    980:                return;
                    981:
1.212     nicm      982:        if ((!tty_pane_full_width(tty, ctx) && !tty_use_margin(tty)) ||
1.210     nicm      983:            tty_fake_bce(tty, wp, ctx->bg) ||
1.1       nicm      984:            !tty_term_has(tty->term, TTYC_CSR)) {
1.128     nicm      985:                if (tty_large_region(tty, ctx))
                    986:                        wp->flags |= PANE_REDRAW;
1.140     nicm      987:                else
1.128     nicm      988:                        tty_redraw_region(tty, ctx);
1.1       nicm      989:                return;
                    990:        }
1.52      nicm      991:
                    992:        /*
1.212     nicm      993:         * If this line wrapped naturally (ctx->num is nonzero) and we are not
                    994:         * using margins, don't do anything - the cursor can just be moved
                    995:         * to the last cell and wrap naturally.
1.52      nicm      996:         */
1.214     nicm      997:        if ((!tty_use_margin(tty) ||
                    998:            tty_pane_full_width(tty, ctx)) &&
1.212     nicm      999:            ctx->num != 0 &&
1.216     nicm     1000:            !(tty->term->flags & TERM_EARLYWRAP))
1.52      nicm     1001:                return;
1.1       nicm     1002:
1.176     nicm     1003:        tty_attributes(tty, &grid_default_cell, wp);
1.77      nicm     1004:
1.56      nicm     1005:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.212     nicm     1006:        tty_margin_pane(tty, ctx);
                   1007:
                   1008:        /*
                   1009:         * If we want to wrap a pane, the cursor needs to be exactly on the
                   1010:         * right of the region. But if the pane isn't on the right, it may be
                   1011:         * off the edge - if so, move the cursor back to the right.
                   1012:         */
                   1013:        if (ctx->xoff + ctx->ocx > tty->rright)
1.216     nicm     1014:                tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy);
1.212     nicm     1015:        else
                   1016:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm     1017:
1.56      nicm     1018:        tty_putc(tty, '\n');
1.1       nicm     1019: }
                   1020:
                   1021: void
1.24      nicm     1022: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1023: {
1.77      nicm     1024:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1025:        struct screen           *s = wp->screen;
1.204     nicm     1026:        u_int                    i, j;
1.229     nicm     1027:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1028:
1.210     nicm     1029:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1030:
1.229     nicm     1031:        tty_region_pane(tty, ctx, 0, sy - 1);
1.214     nicm     1032:        tty_margin_off(tty);
1.39      nicm     1033:
1.176     nicm     1034:        if (tty_pane_full_width(tty, ctx) &&
1.229     nicm     1035:            status_at_line(tty->client) <= 0 &&
                   1036:            tty_term_has(tty->term, TTYC_ED)) {
                   1037:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                   1038:                tty_putcode(tty, TTYC_ED);
                   1039:        } else if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1040:            tty_term_has(tty->term, TTYC_EL) &&
                   1041:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.229     nicm     1042:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1043:                tty_putcode(tty, TTYC_EL);
1.229     nicm     1044:                if (ctx->ocy != sy - 1) {
1.41      nicm     1045:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.229     nicm     1046:                        for (i = ctx->ocy + 1; i < sy; i++) {
1.1       nicm     1047:                                tty_putcode(tty, TTYC_EL);
1.229     nicm     1048:                                if (i == sy - 1)
1.1       nicm     1049:                                        continue;
                   1050:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1051:                                tty->cy++;
                   1052:                        }
                   1053:                }
                   1054:        } else {
1.229     nicm     1055:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                   1056:                tty_repeat_space(tty, sx - ctx->ocx);
                   1057:                for (j = ctx->ocy + 1; j < sy; j++) {
1.41      nicm     1058:                        tty_cursor_pane(tty, ctx, 0, j);
1.229     nicm     1059:                        tty_repeat_space(tty, sx);
1.1       nicm     1060:                }
                   1061:        }
                   1062: }
                   1063:
                   1064: void
1.24      nicm     1065: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1066: {
1.77      nicm     1067:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1068:        struct screen           *s = wp->screen;
1.204     nicm     1069:        u_int                    i, j;
1.229     nicm     1070:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1071:
1.227     nicm     1072:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1073:
1.229     nicm     1074:        tty_region_pane(tty, ctx, 0, sy - 1);
1.214     nicm     1075:        tty_margin_off(tty);
1.39      nicm     1076:
1.230   ! nicm     1077:        if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1078:            tty_term_has(tty->term, TTYC_EL) &&
                   1079:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.229     nicm     1080:                tty_cursor_pane(tty, ctx, 0, 0);
1.14      nicm     1081:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm     1082:                        tty_putcode(tty, TTYC_EL);
                   1083:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1084:                        tty->cy++;
                   1085:                }
                   1086:        } else {
1.229     nicm     1087:                tty_cursor_pane(tty, ctx, 0, 0);
1.14      nicm     1088:                for (j = 0; j < ctx->ocy; j++) {
1.41      nicm     1089:                        tty_cursor_pane(tty, ctx, 0, j);
1.229     nicm     1090:                        tty_repeat_space(tty, sx);
1.1       nicm     1091:                }
                   1092:        }
1.229     nicm     1093:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133     nicm     1094:        tty_repeat_space(tty, ctx->ocx + 1);
1.1       nicm     1095: }
                   1096:
                   1097: void
1.24      nicm     1098: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1099: {
1.77      nicm     1100:        struct window_pane      *wp = ctx->wp;
1.12      nicm     1101:        struct screen           *s = wp->screen;
1.204     nicm     1102:        u_int                    i, j;
1.229     nicm     1103:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1104:
1.210     nicm     1105:        tty_default_attributes(tty, wp, ctx->bg);
1.1       nicm     1106:
1.229     nicm     1107:        tty_region_pane(tty, ctx, 0, sy - 1);
1.214     nicm     1108:        tty_margin_off(tty);
1.39      nicm     1109:
1.176     nicm     1110:        if (tty_pane_full_width(tty, ctx) &&
1.229     nicm     1111:            status_at_line(tty->client) <= 0 &&
                   1112:            tty_term_has(tty->term, TTYC_ED)) {
                   1113:                tty_cursor_pane(tty, ctx, 0, 0);
                   1114:                tty_putcode(tty, TTYC_ED);
1.230   ! nicm     1115:        } else if (tty_pane_full_width(tty, ctx) &&
1.210     nicm     1116:            tty_term_has(tty->term, TTYC_EL) &&
                   1117:            !tty_fake_bce(tty, wp, ctx->bg)) {
1.229     nicm     1118:                tty_cursor_pane(tty, ctx, 0, 0);
                   1119:                for (i = 0; i < sy; i++) {
1.1       nicm     1120:                        tty_putcode(tty, TTYC_EL);
1.229     nicm     1121:                        if (i != sy - 1) {
1.1       nicm     1122:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                   1123:                                tty->cy++;
                   1124:                        }
                   1125:                }
                   1126:        } else {
1.229     nicm     1127:                tty_cursor_pane(tty, ctx, 0, 0);
                   1128:                for (j = 0; j < sy; j++) {
1.41      nicm     1129:                        tty_cursor_pane(tty, ctx, 0, j);
1.229     nicm     1130:                        tty_repeat_space(tty, sx);
1.1       nicm     1131:                }
1.4       nicm     1132:        }
                   1133: }
                   1134:
                   1135: void
1.24      nicm     1136: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm     1137: {
1.12      nicm     1138:        struct window_pane      *wp = ctx->wp;
                   1139:        struct screen           *s = wp->screen;
                   1140:        u_int                    i, j;
1.4       nicm     1141:
1.176     nicm     1142:        tty_attributes(tty, &grid_default_cell, wp);
1.4       nicm     1143:
1.39      nicm     1144:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.214     nicm     1145:        tty_margin_off(tty);
1.4       nicm     1146:
                   1147:        for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm     1148:                tty_cursor_pane(tty, ctx, 0, j);
1.4       nicm     1149:                for (i = 0; i < screen_size_x(s); i++)
                   1150:                        tty_putc(tty, 'E');
1.1       nicm     1151:        }
                   1152: }
                   1153:
                   1154: void
1.24      nicm     1155: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1156: {
1.46      nicm     1157:        struct window_pane      *wp = ctx->wp;
                   1158:        struct screen           *s = wp->screen;
1.203     nicm     1159:        u_int                    cx, width;
1.46      nicm     1160:
1.222     nicm     1161:        if (ctx->xoff + ctx->ocx > tty->sx - 1 && ctx->ocy == ctx->orlower) {
                   1162:                if (tty_pane_full_width(tty, ctx))
                   1163:                        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                   1164:                else
                   1165:                        tty_margin_off(tty);
                   1166:        }
1.46      nicm     1167:
1.57      nicm     1168:        /* Is the cursor in the very last position? */
1.193     nicm     1169:        width = ctx->cell->data.width;
1.102     nicm     1170:        if (ctx->ocx > wp->sx - width) {
1.216     nicm     1171:                if (!tty_pane_full_width(tty, ctx)) {
1.57      nicm     1172:                        /*
                   1173:                         * The pane doesn't fill the entire line, the linefeed
                   1174:                         * will already have happened, so just move the cursor.
                   1175:                         */
1.125     nicm     1176:                        if (ctx->ocy != wp->yoff + wp->screen->rlower)
1.122     nicm     1177:                                tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
                   1178:                        else
                   1179:                                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.218     nicm     1180:                } else if (tty->cy != ctx->yoff + ctx->ocy ||
                   1181:                    tty->cx < tty->sx) {
1.57      nicm     1182:                        /*
                   1183:                         * The cursor isn't in the last position already, so
1.216     nicm     1184:                         * move as far right as possible and redraw the last
1.57      nicm     1185:                         * cell to move into the last position.
                   1186:                         */
1.193     nicm     1187:                        cx = screen_size_x(s) - ctx->last_cell.data.width;
1.50      nicm     1188:                        tty_cursor_pane(tty, ctx, cx, ctx->ocy);
1.176     nicm     1189:                        tty_cell(tty, &ctx->last_cell, wp);
1.50      nicm     1190:                }
                   1191:        } else
1.46      nicm     1192:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm     1193:
1.176     nicm     1194:        tty_cell(tty, ctx->cell, wp);
1.106     nicm     1195: }
                   1196:
                   1197: void
                   1198: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
                   1199: {
                   1200:        char    *buf;
                   1201:        size_t   off;
                   1202:
                   1203:        if (!tty_term_has(tty->term, TTYC_MS))
                   1204:                return;
                   1205:
                   1206:        off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
                   1207:        buf = xmalloc(off);
                   1208:
                   1209:        b64_ntop(ctx->ptr, ctx->num, buf, off);
                   1210:        tty_putcode_ptr2(tty, TTYC_MS, "", buf);
                   1211:
1.138     nicm     1212:        free(buf);
1.100     nicm     1213: }
                   1214:
                   1215: void
                   1216: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
                   1217: {
                   1218:        u_int    i;
                   1219:        u_char  *str = ctx->ptr;
                   1220:
                   1221:        for (i = 0; i < ctx->num; i++)
                   1222:                tty_putc(tty, str[i]);
1.141     nicm     1223:
                   1224:        tty->cx = tty->cy = UINT_MAX;
                   1225:        tty->rupper = tty->rlower = UINT_MAX;
                   1226:
1.176     nicm     1227:        tty_attributes(tty, &grid_default_cell, ctx->wp);
1.141     nicm     1228:        tty_cursor(tty, 0, 0);
1.1       nicm     1229: }
                   1230:
1.207     nicm     1231: static void
1.176     nicm     1232: tty_cell(struct tty *tty, const struct grid_cell *gc,
                   1233:     const struct window_pane *wp)
1.1       nicm     1234: {
1.193     nicm     1235:        u_int   i;
1.1       nicm     1236:
                   1237:        /* Skip last character if terminal is stupid. */
1.211     nicm     1238:        if ((tty->term->flags & TERM_EARLYWRAP) &&
                   1239:            tty->cy == tty->sy - 1 &&
                   1240:            tty->cx == tty->sx - 1)
1.1       nicm     1241:                return;
                   1242:
                   1243:        /* If this is a padding character, do nothing. */
                   1244:        if (gc->flags & GRID_FLAG_PADDING)
                   1245:                return;
                   1246:
                   1247:        /* Set the attributes. */
1.176     nicm     1248:        tty_attributes(tty, gc, wp);
1.1       nicm     1249:
1.147     nicm     1250:        /* Get the cell and if ASCII write with putc to do ACS translation. */
1.193     nicm     1251:        if (gc->data.size == 1) {
                   1252:                if (*gc->data.data < 0x20 || *gc->data.data == 0x7f)
1.1       nicm     1253:                        return;
1.193     nicm     1254:                tty_putc(tty, *gc->data.data);
1.1       nicm     1255:                return;
                   1256:        }
                   1257:
1.147     nicm     1258:        /* If not UTF-8, write _. */
1.1       nicm     1259:        if (!(tty->flags & TTY_UTF8)) {
1.193     nicm     1260:                for (i = 0; i < gc->data.width; i++)
1.1       nicm     1261:                        tty_putc(tty, '_');
                   1262:                return;
                   1263:        }
                   1264:
1.147     nicm     1265:        /* Write the data. */
1.193     nicm     1266:        tty_putn(tty, gc->data.data, gc->data.size, gc->data.width);
1.1       nicm     1267: }
                   1268:
                   1269: void
                   1270: tty_reset(struct tty *tty)
                   1271: {
                   1272:        struct grid_cell        *gc = &tty->cell;
                   1273:
1.228     nicm     1274:        if (!grid_cells_equal(gc, &grid_default_cell)) {
                   1275:                if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
                   1276:                        tty_putcode(tty, TTYC_RMACS);
                   1277:                tty_putcode(tty, TTYC_SGR0);
                   1278:                memcpy(gc, &grid_default_cell, sizeof *gc);
                   1279:        }
1.1       nicm     1280:
1.228     nicm     1281:        memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell);
                   1282:        tty->last_wp = -1;
1.1       nicm     1283: }
                   1284:
1.214     nicm     1285: /* Turn off margin. */
                   1286: void
                   1287: tty_region_off(struct tty *tty)
                   1288: {
                   1289:        tty_region(tty, 0, tty->sy - 1);
                   1290: }
                   1291:
1.40      nicm     1292: /* Set region inside pane. */
1.208     nicm     1293: static void
1.196     nicm     1294: tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper,
                   1295:     u_int rlower)
1.1       nicm     1296: {
1.113     nicm     1297:        tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39      nicm     1298: }
                   1299:
1.40      nicm     1300: /* Set region at absolute position. */
1.214     nicm     1301: static void
1.40      nicm     1302: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm     1303: {
                   1304:        if (tty->rlower == rlower && tty->rupper == rupper)
                   1305:                return;
1.1       nicm     1306:        if (!tty_term_has(tty->term, TTYC_CSR))
                   1307:                return;
1.39      nicm     1308:
                   1309:        tty->rupper = rupper;
                   1310:        tty->rlower = rlower;
1.55      nicm     1311:
                   1312:        /*
                   1313:         * Some terminals (such as PuTTY) do not correctly reset the cursor to
                   1314:         * 0,0 if it is beyond the last column (they do not reset their wrap
                   1315:         * flag so further output causes a line feed). As a workaround, do an
                   1316:         * explicit move to 0 first.
                   1317:         */
                   1318:        if (tty->cx >= tty->sx)
                   1319:                tty_cursor(tty, 0, tty->cy);
1.42      nicm     1320:
1.39      nicm     1321:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.212     nicm     1322:        tty_cursor(tty, 0, 0);
                   1323: }
                   1324:
1.214     nicm     1325: /* Turn off margin. */
                   1326: void
                   1327: tty_margin_off(struct tty *tty)
                   1328: {
                   1329:        tty_margin(tty, 0, tty->sx - 1);
                   1330: }
                   1331:
1.212     nicm     1332: /* Set margin inside pane. */
                   1333: static void
                   1334: tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx)
                   1335: {
                   1336:        tty_margin(tty, ctx->xoff, ctx->xoff + ctx->wp->sx - 1);
                   1337: }
                   1338:
                   1339: /* Set margin at absolute position. */
1.214     nicm     1340: static void
1.212     nicm     1341: tty_margin(struct tty *tty, u_int rleft, u_int rright)
                   1342: {
                   1343:        char s[64];
                   1344:
                   1345:        if (!tty_use_margin(tty))
                   1346:                return;
                   1347:        if (tty->rleft == rleft && tty->rright == rright)
                   1348:                return;
                   1349:
                   1350:        tty->rleft = rleft;
                   1351:        tty->rright = rright;
                   1352:
                   1353:        snprintf(s, sizeof s, "\033[%u;%us", rleft + 1, rright + 1);
                   1354:        tty_puts(tty, s);
1.78      nicm     1355:        tty_cursor(tty, 0, 0);
1.1       nicm     1356: }
                   1357:
1.42      nicm     1358: /* Move cursor inside pane. */
1.208     nicm     1359: static void
1.41      nicm     1360: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                   1361: {
1.113     nicm     1362:        tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41      nicm     1363: }
                   1364:
1.42      nicm     1365: /* Move cursor to absolute position. */
1.41      nicm     1366: void
                   1367: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm     1368: {
1.42      nicm     1369:        struct tty_term *term = tty->term;
                   1370:        u_int            thisx, thisy;
                   1371:        int              change;
1.77      nicm     1372:
1.42      nicm     1373:        if (cx > tty->sx - 1)
                   1374:                cx = tty->sx - 1;
                   1375:
                   1376:        thisx = tty->cx;
                   1377:        thisy = tty->cy;
                   1378:
                   1379:        /* No change. */
                   1380:        if (cx == thisx && cy == thisy)
                   1381:                return;
                   1382:
1.43      nicm     1383:        /* Very end of the line, just use absolute movement. */
                   1384:        if (thisx > tty->sx - 1)
                   1385:                goto absolute;
                   1386:
1.42      nicm     1387:        /* Move to home position (0, 0). */
                   1388:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                   1389:                tty_putcode(tty, TTYC_HOME);
                   1390:                goto out;
                   1391:        }
                   1392:
                   1393:        /* Zero on the next line. */
1.48      nicm     1394:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1       nicm     1395:                tty_putc(tty, '\r');
1.42      nicm     1396:                tty_putc(tty, '\n');
                   1397:                goto out;
                   1398:        }
                   1399:
1.45      nicm     1400:        /* Moving column or row. */
1.42      nicm     1401:        if (cy == thisy) {
1.45      nicm     1402:                /*
                   1403:                 * Moving column only, row staying the same.
                   1404:                 */
                   1405:
1.42      nicm     1406:                /* To left edge. */
1.202     nicm     1407:                if (cx == 0) {
1.42      nicm     1408:                        tty_putc(tty, '\r');
                   1409:                        goto out;
                   1410:                }
                   1411:
                   1412:                /* One to the left. */
                   1413:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   1414:                        tty_putcode(tty, TTYC_CUB1);
                   1415:                        goto out;
                   1416:                }
                   1417:
                   1418:                /* One to the right. */
                   1419:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   1420:                        tty_putcode(tty, TTYC_CUF1);
                   1421:                        goto out;
                   1422:                }
                   1423:
                   1424:                /* Calculate difference. */
                   1425:                change = thisx - cx;    /* +ve left, -ve right */
                   1426:
                   1427:                /*
                   1428:                 * Use HPA if change is larger than absolute, otherwise move
                   1429:                 * the cursor with CUB/CUF.
                   1430:                 */
1.87      nicm     1431:                if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42      nicm     1432:                        tty_putcode1(tty, TTYC_HPA, cx);
                   1433:                        goto out;
                   1434:                } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1.202     nicm     1435:                        if (change == 2 && tty_term_has(term, TTYC_CUB1)) {
                   1436:                                tty_putcode(tty, TTYC_CUB1);
                   1437:                                tty_putcode(tty, TTYC_CUB1);
                   1438:                                goto out;
                   1439:                        }
1.42      nicm     1440:                        tty_putcode1(tty, TTYC_CUB, change);
                   1441:                        goto out;
                   1442:                } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
                   1443:                        tty_putcode1(tty, TTYC_CUF, -change);
                   1444:                        goto out;
                   1445:                }
1.45      nicm     1446:        } else if (cx == thisx) {
                   1447:                /*
                   1448:                 * Moving row only, column staying the same.
                   1449:                 */
1.42      nicm     1450:
                   1451:                /* One above. */
1.77      nicm     1452:                if (thisy != tty->rupper &&
1.42      nicm     1453:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   1454:                        tty_putcode(tty, TTYC_CUU1);
                   1455:                        goto out;
                   1456:                }
                   1457:
                   1458:                /* One below. */
1.49      nicm     1459:                if (thisy != tty->rlower &&
1.42      nicm     1460:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   1461:                        tty_putcode(tty, TTYC_CUD1);
                   1462:                        goto out;
                   1463:                }
                   1464:
                   1465:                /* Calculate difference. */
                   1466:                change = thisy - cy;    /* +ve up, -ve down */
                   1467:
                   1468:                /*
1.77      nicm     1469:                 * Try to use VPA if change is larger than absolute or if this
                   1470:                 * change would cross the scroll region, otherwise use CUU/CUD.
1.42      nicm     1471:                 */
1.87      nicm     1472:                if ((u_int) abs(change) > cy ||
1.42      nicm     1473:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     1474:                    (change > 0 && cy - change < tty->rupper)) {
                   1475:                            if (tty_term_has(term, TTYC_VPA)) {
                   1476:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   1477:                                    goto out;
                   1478:                            }
1.42      nicm     1479:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   1480:                        tty_putcode1(tty, TTYC_CUU, change);
                   1481:                        goto out;
                   1482:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   1483:                        tty_putcode1(tty, TTYC_CUD, -change);
                   1484:                        goto out;
                   1485:                }
                   1486:        }
                   1487:
1.43      nicm     1488: absolute:
1.42      nicm     1489:        /* Absolute movement. */
                   1490:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   1491:
                   1492: out:
                   1493:        tty->cx = cx;
                   1494:        tty->cy = cy;
1.1       nicm     1495: }
                   1496:
                   1497: void
1.176     nicm     1498: tty_attributes(struct tty *tty, const struct grid_cell *gc,
                   1499:     const struct window_pane *wp)
1.1       nicm     1500: {
1.63      nicm     1501:        struct grid_cell        *tc = &tty->cell, gc2;
1.85      nicm     1502:        u_char                   changed;
1.61      nicm     1503:
1.228     nicm     1504:        /* Ignore cell if it is the same as the last one. */
                   1505:        if (wp != NULL &&
                   1506:            (int)wp->id == tty->last_wp &&
                   1507:            ~(wp->window->flags & WINDOW_STYLECHANGED) &&
                   1508:            gc->attr == tty->last_cell.attr &&
                   1509:            gc->fg == tty->last_cell.fg &&
                   1510:            gc->bg == tty->last_cell.bg)
                   1511:                return;
                   1512:        tty->last_wp = (wp != NULL ? (int)wp->id : -1);
                   1513:        memcpy(&tty->last_cell, gc, sizeof tty->last_cell);
                   1514:
                   1515:        /* Copy cell and update default colours. */
1.85      nicm     1516:        memcpy(&gc2, gc, sizeof gc2);
1.203     nicm     1517:        if (wp != NULL)
                   1518:                tty_default_colours(&gc2, wp);
1.63      nicm     1519:
1.18      nicm     1520:        /*
                   1521:         * If no setab, try to use the reverse attribute as a best-effort for a
                   1522:         * non-default background. This is a bit of a hack but it doesn't do
                   1523:         * any serious harm and makes a couple of applications happier.
                   1524:         */
                   1525:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85      nicm     1526:                if (gc2.attr & GRID_ATTR_REVERSE) {
                   1527:                        if (gc2.fg != 7 && gc2.fg != 8)
1.64      nicm     1528:                                gc2.attr &= ~GRID_ATTR_REVERSE;
1.18      nicm     1529:                } else {
1.85      nicm     1530:                        if (gc2.bg != 0 && gc2.bg != 8)
1.64      nicm     1531:                                gc2.attr |= GRID_ATTR_REVERSE;
1.18      nicm     1532:                }
                   1533:        }
1.1       nicm     1534:
1.85      nicm     1535:        /* Fix up the colours if necessary. */
1.219     nicm     1536:        tty_check_fg(tty, wp, &gc2);
                   1537:        tty_check_bg(tty, wp, &gc2);
1.85      nicm     1538:
1.64      nicm     1539:        /* If any bits are being cleared, reset everything. */
1.85      nicm     1540:        if (tc->attr & ~gc2.attr)
1.64      nicm     1541:                tty_reset(tty);
                   1542:
                   1543:        /*
                   1544:         * Set the colours. This may call tty_reset() (so it comes next) and
                   1545:         * may add to (NOT remove) the desired attributes by changing new_attr.
                   1546:         */
1.85      nicm     1547:        tty_colours(tty, &gc2);
1.64      nicm     1548:
1.1       nicm     1549:        /* Filter out attribute bits already set. */
1.85      nicm     1550:        changed = gc2.attr & ~tc->attr;
                   1551:        tc->attr = gc2.attr;
1.1       nicm     1552:
                   1553:        /* Set the attributes. */
                   1554:        if (changed & GRID_ATTR_BRIGHT)
                   1555:                tty_putcode(tty, TTYC_BOLD);
                   1556:        if (changed & GRID_ATTR_DIM)
                   1557:                tty_putcode(tty, TTYC_DIM);
1.180     nicm     1558:        if (changed & GRID_ATTR_ITALICS)
                   1559:                tty_set_italics(tty);
1.1       nicm     1560:        if (changed & GRID_ATTR_UNDERSCORE)
                   1561:                tty_putcode(tty, TTYC_SMUL);
                   1562:        if (changed & GRID_ATTR_BLINK)
                   1563:                tty_putcode(tty, TTYC_BLINK);
                   1564:        if (changed & GRID_ATTR_REVERSE) {
                   1565:                if (tty_term_has(tty->term, TTYC_REV))
                   1566:                        tty_putcode(tty, TTYC_REV);
                   1567:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   1568:                        tty_putcode(tty, TTYC_SMSO);
                   1569:        }
                   1570:        if (changed & GRID_ATTR_HIDDEN)
                   1571:                tty_putcode(tty, TTYC_INVIS);
1.90      nicm     1572:        if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1       nicm     1573:                tty_putcode(tty, TTYC_SMACS);
                   1574: }
                   1575:
1.207     nicm     1576: static void
1.85      nicm     1577: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1578: {
1.61      nicm     1579:        struct grid_cell        *tc = &tty->cell;
1.204     nicm     1580:        int                      have_ax;
1.61      nicm     1581:
                   1582:        /* No changes? Nothing is necessary. */
1.204     nicm     1583:        if (gc->fg == tc->fg && gc->bg == tc->bg)
1.63      nicm     1584:                return;
1.1       nicm     1585:
1.61      nicm     1586:        /*
                   1587:         * Is either the default colour? This is handled specially because the
                   1588:         * best solution might be to reset both colours to default, in which
                   1589:         * case if only one is default need to fall onward to set the other
                   1590:         * colour.
                   1591:         */
1.204     nicm     1592:        if (gc->fg == 8 || gc->bg == 8) {
1.61      nicm     1593:                /*
                   1594:                 * If don't have AX but do have op, send sgr0 (op can't
                   1595:                 * actually be used because it is sometimes the same as sgr0
                   1596:                 * and sometimes isn't). This resets both colours to default.
                   1597:                 *
                   1598:                 * Otherwise, try to set the default colour only as needed.
                   1599:                 */
1.174     nicm     1600:                have_ax = tty_term_flag(tty->term, TTYC_AX);
1.61      nicm     1601:                if (!have_ax && tty_term_has(tty->term, TTYC_OP))
                   1602:                        tty_reset(tty);
                   1603:                else {
1.204     nicm     1604:                        if (gc->fg == 8 && tc->fg != 8) {
1.61      nicm     1605:                                if (have_ax)
                   1606:                                        tty_puts(tty, "\033[39m");
1.204     nicm     1607:                                else if (tc->fg != 7)
1.61      nicm     1608:                                        tty_putcode1(tty, TTYC_SETAF, 7);
                   1609:                                tc->fg = 8;
                   1610:                        }
1.204     nicm     1611:                        if (gc->bg == 8 && tc->bg != 8) {
1.77      nicm     1612:                                if (have_ax)
1.61      nicm     1613:                                        tty_puts(tty, "\033[49m");
1.204     nicm     1614:                                else if (tc->bg != 0)
1.61      nicm     1615:                                        tty_putcode1(tty, TTYC_SETAB, 0);
                   1616:                                tc->bg = 8;
                   1617:                        }
                   1618:                }
                   1619:        }
1.1       nicm     1620:
1.61      nicm     1621:        /* Set the foreground colour. */
1.204     nicm     1622:        if (gc->fg != 8 && gc->fg != tc->fg)
1.85      nicm     1623:                tty_colours_fg(tty, gc);
1.1       nicm     1624:
1.61      nicm     1625:        /*
                   1626:         * Set the background colour. This must come after the foreground as
                   1627:         * tty_colour_fg() can call tty_reset().
                   1628:         */
1.204     nicm     1629:        if (gc->bg != 8 && gc->bg != tc->bg)
1.73      nicm     1630:                tty_colours_bg(tty, gc);
1.1       nicm     1631: }
                   1632:
1.219     nicm     1633: static void
                   1634: tty_check_fg(struct tty *tty, const struct window_pane *wp,
                   1635:     struct grid_cell *gc)
1.85      nicm     1636: {
1.204     nicm     1637:        u_char  r, g, b;
                   1638:        u_int   colours;
1.223     nicm     1639:        int     c;
1.85      nicm     1640:
1.219     nicm     1641:        /* Perform substitution if this pane has a palette */
                   1642:        if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223     nicm     1643:            (c = window_pane_get_palette(wp, gc->fg)) != -1)
                   1644:                gc->fg = c;
1.219     nicm     1645:
1.199     nicm     1646:        /* Is this a 24-bit colour? */
1.204     nicm     1647:        if (gc->fg & COLOUR_FLAG_RGB) {
1.199     nicm     1648:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
                   1649:                if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204     nicm     1650:                        colour_split_rgb(gc->fg, &r, &g, &b);
                   1651:                        gc->fg = colour_find_rgb(r, g, b);
                   1652:                } else
1.200     nicm     1653:                        return;
1.199     nicm     1654:        }
1.224     nicm     1655:
                   1656:        /* How many colours does this terminal have? */
                   1657:        if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
                   1658:                colours = 256;
                   1659:        else
                   1660:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     1661:
1.85      nicm     1662:        /* Is this a 256-colour colour? */
1.204     nicm     1663:        if (gc->fg & COLOUR_FLAG_256) {
1.85      nicm     1664:                /* And not a 256 colour mode? */
1.224     nicm     1665:                if (colours != 256) {
1.85      nicm     1666:                        gc->fg = colour_256to16(gc->fg);
                   1667:                        if (gc->fg & 8) {
                   1668:                                gc->fg &= 7;
1.175     nicm     1669:                                if (colours >= 16)
                   1670:                                        gc->fg += 90;
                   1671:                                else
                   1672:                                        gc->attr |= GRID_ATTR_BRIGHT;
1.85      nicm     1673:                        } else
                   1674:                                gc->attr &= ~GRID_ATTR_BRIGHT;
                   1675:                }
                   1676:                return;
                   1677:        }
                   1678:
                   1679:        /* Is this an aixterm colour? */
                   1680:        if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
                   1681:                gc->fg -= 90;
                   1682:                gc->attr |= GRID_ATTR_BRIGHT;
                   1683:        }
                   1684: }
                   1685:
1.219     nicm     1686: static void
                   1687: tty_check_bg(struct tty *tty, const struct window_pane *wp,
                   1688:     struct grid_cell *gc)
1.85      nicm     1689: {
1.204     nicm     1690:        u_char  r, g, b;
                   1691:        u_int   colours;
1.223     nicm     1692:        int     c;
1.85      nicm     1693:
1.219     nicm     1694:        /* Perform substitution if this pane has a palette */
                   1695:        if ((~gc->flags & GRID_FLAG_NOPALETTE) &&
1.223     nicm     1696:            (c = window_pane_get_palette(wp, gc->bg)) != -1)
                   1697:                gc->bg = c;
1.219     nicm     1698:
1.199     nicm     1699:        /* Is this a 24-bit colour? */
1.204     nicm     1700:        if (gc->bg & COLOUR_FLAG_RGB) {
1.199     nicm     1701:                /* Not a 24-bit terminal? Translate to 256-colour palette. */
                   1702:                if (!tty_term_flag(tty->term, TTYC_TC)) {
1.204     nicm     1703:                        colour_split_rgb(gc->bg, &r, &g, &b);
                   1704:                        gc->bg = colour_find_rgb(r, g, b);
                   1705:                } else
1.200     nicm     1706:                        return;
1.199     nicm     1707:        }
1.224     nicm     1708:
                   1709:        /* How many colours does this terminal have? */
                   1710:        if ((tty->term->flags|tty->term_flags) & TERM_256COLOURS)
                   1711:                colours = 256;
                   1712:        else
                   1713:                colours = tty_term_number(tty->term, TTYC_COLORS);
1.175     nicm     1714:
1.85      nicm     1715:        /* Is this a 256-colour colour? */
1.204     nicm     1716:        if (gc->bg & COLOUR_FLAG_256) {
1.85      nicm     1717:                /*
                   1718:                 * And not a 256 colour mode? Translate to 16-colour
                   1719:                 * palette. Bold background doesn't exist portably, so just
                   1720:                 * discard the bold bit if set.
                   1721:                 */
1.224     nicm     1722:                if (colours != 256) {
1.85      nicm     1723:                        gc->bg = colour_256to16(gc->bg);
1.175     nicm     1724:                        if (gc->bg & 8) {
1.85      nicm     1725:                                gc->bg &= 7;
1.175     nicm     1726:                                if (colours >= 16)
                   1727:                                        gc->fg += 90;
                   1728:                        }
1.85      nicm     1729:                }
                   1730:                return;
                   1731:        }
                   1732:
                   1733:        /* Is this an aixterm colour? */
1.175     nicm     1734:        if (gc->bg >= 90 && gc->bg <= 97 && colours < 16)
1.85      nicm     1735:                gc->bg -= 90;
                   1736: }
                   1737:
1.207     nicm     1738: static void
1.85      nicm     1739: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1740: {
1.61      nicm     1741:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     1742:        char                     s[32];
1.1       nicm     1743:
1.204     nicm     1744:        /* Is this a 24-bit or 256-colour colour? */
                   1745:        if (gc->fg & COLOUR_FLAG_RGB ||
                   1746:            gc->fg & COLOUR_FLAG_256) {
                   1747:                if (tty_try_colour(tty, gc->fg, "38") == 0)
1.61      nicm     1748:                        goto save_fg;
1.199     nicm     1749:                /* Should not get here, already converted in tty_check_fg. */
1.85      nicm     1750:                return;
1.1       nicm     1751:        }
                   1752:
1.79      nicm     1753:        /* Is this an aixterm bright colour? */
1.204     nicm     1754:        if (gc->fg >= 90 && gc->fg <= 97) {
                   1755:                xsnprintf(s, sizeof s, "\033[%dm", gc->fg);
1.85      nicm     1756:                tty_puts(tty, s);
                   1757:                goto save_fg;
1.79      nicm     1758:        }
                   1759:
1.61      nicm     1760:        /* Otherwise set the foreground colour. */
1.204     nicm     1761:        tty_putcode1(tty, TTYC_SETAF, gc->fg);
1.61      nicm     1762:
1.77      nicm     1763: save_fg:
1.61      nicm     1764:        /* Save the new values in the terminal current cell. */
1.204     nicm     1765:        tc->fg = gc->fg;
1.1       nicm     1766: }
                   1767:
1.207     nicm     1768: static void
1.73      nicm     1769: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1       nicm     1770: {
1.61      nicm     1771:        struct grid_cell        *tc = &tty->cell;
1.79      nicm     1772:        char                     s[32];
1.1       nicm     1773:
1.204     nicm     1774:        /* Is this a 24-bit or 256-colour colour? */
                   1775:        if (gc->bg & COLOUR_FLAG_RGB ||
                   1776:            gc->bg & COLOUR_FLAG_256) {
                   1777:                if (tty_try_colour(tty, gc->bg, "48") == 0)
1.61      nicm     1778:                        goto save_bg;
1.199     nicm     1779:                /* Should not get here, already converted in tty_check_bg. */
1.85      nicm     1780:                return;
1.79      nicm     1781:        }
                   1782:
                   1783:        /* Is this an aixterm bright colour? */
1.204     nicm     1784:        if (gc->bg >= 90 && gc->bg <= 97) {
                   1785:                xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10);
1.175     nicm     1786:                tty_puts(tty, s);
                   1787:                goto save_bg;
1.1       nicm     1788:        }
                   1789:
1.61      nicm     1790:        /* Otherwise set the background colour. */
1.204     nicm     1791:        tty_putcode1(tty, TTYC_SETAB, gc->bg);
1.61      nicm     1792:
                   1793: save_bg:
                   1794:        /* Save the new values in the terminal current cell. */
1.204     nicm     1795:        tc->bg = gc->bg;
1.61      nicm     1796: }
                   1797:
1.207     nicm     1798: static int
1.204     nicm     1799: tty_try_colour(struct tty *tty, int colour, const char *type)
1.61      nicm     1800: {
1.204     nicm     1801:        u_char  r, g, b;
1.61      nicm     1802:        char    s[32];
                   1803:
1.204     nicm     1804:        if (colour & COLOUR_FLAG_256) {
                   1805:                /*
                   1806:                 * If the user has specified -2 to the client, setaf and setab
                   1807:                 * may not work (or they may not want to use them), so send the
                   1808:                 * usual sequence.
                   1809:                 */
                   1810:                if (tty->term_flags & TERM_256COLOURS)
                   1811:                        goto fallback_256;
1.189     nicm     1812:
1.204     nicm     1813:                /*
                   1814:                 * If the terminfo entry has 256 colours and setaf and setab
                   1815:                 * exist, assume that they work correctly.
                   1816:                 */
                   1817:                if (tty->term->flags & TERM_256COLOURS) {
                   1818:                        if (*type == '3') {
                   1819:                                if (!tty_term_has(tty->term, TTYC_SETAF))
                   1820:                                        goto fallback_256;
                   1821:                                tty_putcode1(tty, TTYC_SETAF, colour & 0xff);
                   1822:                        } else {
                   1823:                                if (!tty_term_has(tty->term, TTYC_SETAB))
                   1824:                                        goto fallback_256;
                   1825:                                tty_putcode1(tty, TTYC_SETAB, colour & 0xff);
                   1826:                        }
                   1827:                        return (0);
1.188     nicm     1828:                }
1.204     nicm     1829:                goto fallback_256;
                   1830:        }
                   1831:
                   1832:        if (colour & COLOUR_FLAG_RGB) {
                   1833:                if (!tty_term_flag(tty->term, TTYC_TC))
                   1834:                        return (-1);
                   1835:
                   1836:                colour_split_rgb(colour & 0xffffff, &r, &g, &b);
                   1837:                xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type,
                   1838:                    r, g, b);
                   1839:                tty_puts(tty, s);
1.165     nicm     1840:                return (0);
                   1841:        }
1.61      nicm     1842:
1.165     nicm     1843:        return (-1);
1.188     nicm     1844:
1.204     nicm     1845: fallback_256:
                   1846:        xsnprintf(s, sizeof s, "\033[%s;5;%dm", type, colour & 0xff);
1.188     nicm     1847:        tty_puts(tty, s);
                   1848:        return (0);
1.176     nicm     1849: }
                   1850:
1.207     nicm     1851: static void
1.176     nicm     1852: tty_default_colours(struct grid_cell *gc, const struct window_pane *wp)
                   1853: {
1.203     nicm     1854:        struct window           *w = wp->window;
                   1855:        struct options          *oo = w->options;
                   1856:        const struct grid_cell  *agc, *pgc, *wgc;
1.223     nicm     1857:        int                      c;
1.203     nicm     1858:
                   1859:        if (w->flags & WINDOW_STYLECHANGED) {
                   1860:                w->flags &= ~WINDOW_STYLECHANGED;
                   1861:                agc = options_get_style(oo, "window-active-style");
                   1862:                memcpy(&w->active_style, agc, sizeof w->active_style);
                   1863:                wgc = options_get_style(oo, "window-style");
                   1864:                memcpy(&w->style, wgc, sizeof w->style);
                   1865:        } else {
                   1866:                agc = &w->active_style;
                   1867:                wgc = &w->style;
                   1868:        }
1.176     nicm     1869:        pgc = &wp->colgc;
                   1870:
1.204     nicm     1871:        if (gc->fg == 8) {
                   1872:                if (pgc->fg != 8)
1.176     nicm     1873:                        gc->fg = pgc->fg;
1.204     nicm     1874:                else if (wp == w->active && agc->fg != 8)
1.176     nicm     1875:                        gc->fg = agc->fg;
1.204     nicm     1876:                else
1.176     nicm     1877:                        gc->fg = wgc->fg;
1.219     nicm     1878:
1.223     nicm     1879:                if (gc->fg != 8 &&
                   1880:                    (c = window_pane_get_palette(wp, gc->fg)) != -1)
                   1881:                        gc->fg = c;
1.176     nicm     1882:        }
                   1883:
1.204     nicm     1884:        if (gc->bg == 8) {
                   1885:                if (pgc->bg != 8)
1.176     nicm     1886:                        gc->bg = pgc->bg;
1.204     nicm     1887:                else if (wp == w->active && agc->bg != 8)
1.176     nicm     1888:                        gc->bg = agc->bg;
1.204     nicm     1889:                else
1.176     nicm     1890:                        gc->bg = wgc->bg;
1.219     nicm     1891:
1.223     nicm     1892:                if (gc->bg != 8 &&
                   1893:                    (c = window_pane_get_palette(wp, gc->bg)) != -1)
                   1894:                        gc->bg = c;
1.176     nicm     1895:        }
1.210     nicm     1896: }
                   1897:
                   1898: static void
                   1899: tty_default_attributes(struct tty *tty, const struct window_pane *wp, u_int bg)
                   1900: {
                   1901:        static struct grid_cell gc;
                   1902:
                   1903:        memcpy(&gc, &grid_default_cell, sizeof gc);
                   1904:        gc.bg = bg;
                   1905:        tty_attributes(tty, &gc, wp);
1.1       nicm     1906: }