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

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