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

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