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

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