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

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