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

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