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

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