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

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