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

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