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

1.144   ! nicm        1: /* $OpenBSD: tty.c,v 1.143 2012/11/22 14:10:53 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))
1.144   ! nicm      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.128     nicm      240: }
                    241:
                    242: void
1.1       nicm      243: tty_stop_tty(struct tty *tty)
                    244: {
                    245:        struct winsize  ws;
                    246:
1.20      nicm      247:        if (!(tty->flags & TTY_STARTED))
                    248:                return;
                    249:        tty->flags &= ~TTY_STARTED;
                    250:
1.66      nicm      251:        bufferevent_disable(tty->event, EV_READ|EV_WRITE);
                    252:
1.1       nicm      253:        /*
                    254:         * Be flexible about error handling and try not kill the server just
                    255:         * because the fd is invalid. Things like ssh -t can easily leave us
                    256:         * with a dead tty.
                    257:         */
                    258:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
                    259:                return;
                    260:        if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
                    261:                return;
                    262:
1.103     nicm      263:        setblocking(tty->fd, 1);
                    264:
1.1       nicm      265:        tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90      nicm      266:        if (tty_use_acs(tty))
                    267:                tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1       nicm      268:        tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
                    269:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
                    270:        tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.108     nicm      271:        if (tty_term_has(tty->term, TTYC_CS1) && tty->cstyle != 0) {
                    272:                if (tty_term_has(tty->term, TTYC_CSR1))
                    273:                        tty_raw(tty, tty_term_string(tty->term, TTYC_CSR1));
1.109     nicm      274:                else
1.108     nicm      275:                        tty_raw(tty, tty_term_string1(tty->term, TTYC_CS1, 0));
                    276:        }
1.107     nicm      277:        tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1       nicm      278:
                    279:        tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
                    280:        if (tty_term_has(tty->term, TTYC_KMOUS))
                    281:                tty_raw(tty, "\033[?1000l");
1.10      nicm      282:
                    283:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.1       nicm      284: }
                    285:
                    286: void
1.20      nicm      287: tty_close(struct tty *tty)
1.1       nicm      288: {
                    289:        if (tty->log_fd != -1) {
                    290:                close(tty->log_fd);
                    291:                tty->log_fd = -1;
                    292:        }
                    293:
1.123     nicm      294:        if (event_initialized(&tty->key_timer))
                    295:                evtimer_del(&tty->key_timer);
1.20      nicm      296:        tty_stop_tty(tty);
1.1       nicm      297:
1.21      nicm      298:        if (tty->flags & TTY_OPENED) {
1.66      nicm      299:                bufferevent_free(tty->event);
                    300:
1.21      nicm      301:                tty_term_free(tty->term);
                    302:                tty_keys_free(tty);
                    303:
                    304:                tty->flags &= ~TTY_OPENED;
                    305:        }
1.1       nicm      306:
1.21      nicm      307:        if (tty->fd != -1) {
                    308:                close(tty->fd);
                    309:                tty->fd = -1;
                    310:        }
1.1       nicm      311: }
                    312:
                    313: void
1.20      nicm      314: tty_free(struct tty *tty)
1.1       nicm      315: {
1.20      nicm      316:        tty_close(tty);
1.1       nicm      317:
1.138     nicm      318:        free(tty->ccolour);
1.1       nicm      319:        if (tty->path != NULL)
1.138     nicm      320:                free(tty->path);
1.1       nicm      321:        if (tty->termname != NULL)
1.138     nicm      322:                free(tty->termname);
1.1       nicm      323: }
                    324:
                    325: void
                    326: tty_raw(struct tty *tty, const char *s)
                    327: {
                    328:        write(tty->fd, s, strlen(s));
                    329: }
                    330:
                    331: void
                    332: tty_putcode(struct tty *tty, enum tty_code_code code)
                    333: {
                    334:        tty_puts(tty, tty_term_string(tty->term, code));
                    335: }
                    336:
                    337: void
                    338: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
                    339: {
                    340:        if (a < 0)
                    341:                return;
                    342:        tty_puts(tty, tty_term_string1(tty->term, code, a));
                    343: }
                    344:
                    345: void
                    346: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
                    347: {
                    348:        if (a < 0 || b < 0)
                    349:                return;
                    350:        tty_puts(tty, tty_term_string2(tty->term, code, a, b));
                    351: }
                    352:
                    353: void
1.107     nicm      354: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
                    355: {
                    356:        if (a != NULL)
                    357:                tty_puts(tty, tty_term_ptr1(tty->term, code, a));
                    358: }
                    359:
                    360: void
1.106     nicm      361: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a, const void *b)
                    362: {
                    363:        if (a != NULL && b != NULL)
                    364:                tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
                    365: }
                    366:
                    367: void
1.1       nicm      368: tty_puts(struct tty *tty, const char *s)
                    369: {
                    370:        if (*s == '\0')
                    371:                return;
1.66      nicm      372:        bufferevent_write(tty->event, s, strlen(s));
1.1       nicm      373:
                    374:        if (tty->log_fd != -1)
                    375:                write(tty->log_fd, s, strlen(s));
                    376: }
                    377:
                    378: void
                    379: tty_putc(struct tty *tty, u_char ch)
                    380: {
1.90      nicm      381:        const char      *acs;
                    382:        u_int            sx;
1.1       nicm      383:
1.90      nicm      384:        if (tty->cell.attr & GRID_ATTR_CHARSET) {
                    385:                acs = tty_acs_get(tty, ch);
                    386:                if (acs != NULL)
                    387:                        bufferevent_write(tty->event, acs, strlen(acs));
                    388:                else
                    389:                        bufferevent_write(tty->event, &ch, 1);
                    390:        } else
                    391:                bufferevent_write(tty->event, &ch, 1);
1.1       nicm      392:
                    393:        if (ch >= 0x20 && ch != 0x7f) {
                    394:                sx = tty->sx;
                    395:                if (tty->term->flags & TERM_EARLYWRAP)
                    396:                        sx--;
                    397:
1.46      nicm      398:                if (tty->cx >= sx) {
                    399:                        tty->cx = 1;
                    400:                        if (tty->cy != tty->rlower)
                    401:                                tty->cy++;
1.1       nicm      402:                } else
                    403:                        tty->cx++;
                    404:        }
                    405:
                    406:        if (tty->log_fd != -1)
                    407:                write(tty->log_fd, &ch, 1);
                    408: }
                    409:
                    410: void
1.5       nicm      411: tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
                    412: {
1.71      nicm      413:        size_t  size;
1.5       nicm      414:
1.71      nicm      415:        size = grid_utf8_size(gu);
                    416:        bufferevent_write(tty->event, gu->data, size);
                    417:        if (tty->log_fd != -1)
                    418:                write(tty->log_fd, gu->data, size);
1.54      nicm      419:        tty->cx += gu->width;
1.5       nicm      420: }
                    421:
                    422: void
1.1       nicm      423: tty_set_title(struct tty *tty, const char *title)
                    424: {
1.105     nicm      425:        if (!tty_term_has(tty->term, TTYC_TSL) ||
                    426:            !tty_term_has(tty->term, TTYC_FSL))
1.1       nicm      427:                return;
                    428:
1.105     nicm      429:        tty_putcode(tty, TTYC_TSL);
1.1       nicm      430:        tty_puts(tty, title);
1.105     nicm      431:        tty_putcode(tty, TTYC_FSL);
1.1       nicm      432: }
                    433:
                    434: void
1.107     nicm      435: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
                    436: {
                    437:        if (*ccolour == '\0')
                    438:                tty_putcode(tty, TTYC_CR);
                    439:        else
                    440:                tty_putcode_ptr1(tty, TTYC_CC, ccolour);
1.138     nicm      441:        free(tty->ccolour);
1.107     nicm      442:        tty->ccolour = xstrdup(ccolour);
                    443: }
                    444:
                    445: void
                    446: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1       nicm      447: {
                    448:        int     changed;
                    449:
1.107     nicm      450:        if (strcmp(s->ccolour, tty->ccolour))
                    451:                tty_force_cursor_colour(tty, s->ccolour);
                    452:
1.1       nicm      453:        if (tty->flags & TTY_NOCURSOR)
                    454:                mode &= ~MODE_CURSOR;
                    455:
                    456:        changed = mode ^ tty->mode;
                    457:        if (changed & MODE_CURSOR) {
                    458:                if (mode & MODE_CURSOR)
                    459:                        tty_putcode(tty, TTYC_CNORM);
                    460:                else
                    461:                        tty_putcode(tty, TTYC_CIVIS);
1.108     nicm      462:        }
                    463:        if (tty->cstyle != s->cstyle) {
                    464:                if (tty_term_has(tty->term, TTYC_CS1)) {
                    465:                        if (s->cstyle == 0 &&
                    466:                            tty_term_has(tty->term, TTYC_CSR1))
                    467:                                tty_putcode(tty, TTYC_CSR1);
                    468:                        else
                    469:                                tty_putcode1(tty, TTYC_CS1, s->cstyle);
                    470:                }
                    471:                tty->cstyle = s->cstyle;
1.1       nicm      472:        }
1.94      nicm      473:        if (changed & ALL_MOUSE_MODES) {
                    474:                if (mode & ALL_MOUSE_MODES) {
1.95      nicm      475:                        if (mode & MODE_MOUSE_UTF8)
                    476:                                tty_puts(tty, "\033[?1005h");
1.98      nicm      477:                        if (mode & MODE_MOUSE_ANY)
                    478:                                tty_puts(tty, "\033[?1003h");
1.94      nicm      479:                        else if (mode & MODE_MOUSE_BUTTON)
                    480:                                tty_puts(tty, "\033[?1002h");
1.98      nicm      481:                        else if (mode & MODE_MOUSE_STANDARD)
                    482:                                tty_puts(tty, "\033[?1000h");
1.86      nicm      483:                } else {
1.98      nicm      484:                        if (tty->mode & MODE_MOUSE_ANY)
                    485:                                tty_puts(tty, "\033[?1003l");
1.94      nicm      486:                        else if (tty->mode & MODE_MOUSE_BUTTON)
                    487:                                tty_puts(tty, "\033[?1002l");
1.98      nicm      488:                        else if (tty->mode & MODE_MOUSE_STANDARD)
                    489:                                tty_puts(tty, "\033[?1000l");
1.95      nicm      490:                        if (tty->mode & MODE_MOUSE_UTF8)
                    491:                                tty_puts(tty, "\033[?1005l");
1.86      nicm      492:                }
1.76      nicm      493:        }
                    494:        if (changed & MODE_KKEYPAD) {
                    495:                if (mode & MODE_KKEYPAD)
                    496:                        tty_putcode(tty, TTYC_SMKX);
                    497:                else
                    498:                        tty_putcode(tty, TTYC_RMKX);
1.115     nicm      499:        }
                    500:        if (changed & MODE_BRACKETPASTE) {
                    501:                if (mode & MODE_BRACKETPASTE)
                    502:                        tty_puts(tty, "\033[?2004h");
                    503:                else
                    504:                        tty_puts(tty, "\033[?2004l");
1.1       nicm      505:        }
                    506:        tty->mode = mode;
                    507: }
                    508:
                    509: void
                    510: tty_emulate_repeat(
                    511:     struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
                    512: {
                    513:        if (tty_term_has(tty->term, code))
                    514:                tty_putcode1(tty, code, n);
                    515:        else {
                    516:                while (n-- > 0)
                    517:                        tty_putcode(tty, code1);
                    518:        }
                    519: }
                    520:
1.133     nicm      521: void
                    522: tty_repeat_space(struct tty *tty, u_int n)
                    523: {
                    524:        while (n-- > 0)
                    525:                tty_putc(tty, ' ');
                    526: }
                    527:
1.1       nicm      528: /*
1.119     nicm      529:  * Is the region large enough to be worth redrawing once later rather than
                    530:  * probably several times now? Currently yes if it is more than 50% of the
                    531:  * pane.
                    532:  */
                    533: int
                    534: tty_large_region(unused struct tty *tty, const struct tty_ctx *ctx)
                    535: {
                    536:        struct window_pane      *wp = ctx->wp;
                    537:
                    538:        return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2);
                    539: }
                    540:
                    541: /*
1.1       nicm      542:  * Redraw scroll region using data from screen (already updated). Used when
                    543:  * CSR not supported, or window is a pane that doesn't take up the full
                    544:  * width of the terminal.
                    545:  */
                    546: void
1.24      nicm      547: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      548: {
1.14      nicm      549:        struct window_pane      *wp = ctx->wp;
                    550:        struct screen           *s = wp->screen;
                    551:        u_int                    i;
1.1       nicm      552:
                    553:        /*
1.119     nicm      554:         * If region is large, schedule a window redraw. In most cases this is
                    555:         * likely to be followed by some more scrolling.
1.1       nicm      556:         */
1.119     nicm      557:        if (tty_large_region(tty, ctx)) {
1.1       nicm      558:                wp->flags |= PANE_REDRAW;
                    559:                return;
                    560:        }
                    561:
1.14      nicm      562:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    563:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.113     nicm      564:                        tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1       nicm      565:        } else {
1.14      nicm      566:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.113     nicm      567:                        tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1       nicm      568:        }
                    569: }
                    570:
                    571: void
                    572: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
                    573: {
                    574:        const struct grid_cell  *gc;
1.46      nicm      575:        struct grid_line        *gl;
1.16      nicm      576:        struct grid_cell         tmpgc;
1.1       nicm      577:        const struct grid_utf8  *gu;
                    578:        u_int                    i, sx;
                    579:
1.107     nicm      580:        tty_update_mode(tty, tty->mode & ~MODE_CURSOR, s);
1.35      nicm      581:
1.1       nicm      582:        sx = screen_size_x(s);
1.19      nicm      583:        if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
                    584:                sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1       nicm      585:        if (sx > tty->sx)
                    586:                sx = tty->sx;
                    587:
1.46      nicm      588:        /*
                    589:         * Don't move the cursor to the start permission if it will wrap there
1.50      nicm      590:         * itself.
1.46      nicm      591:         */
                    592:        gl = NULL;
                    593:        if (py != 0)
                    594:                gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83      nicm      595:        if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47      nicm      596:            tty->cx < tty->sx || ox != 0 ||
                    597:            (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46      nicm      598:                tty_cursor(tty, ox, oy + py);
                    599:
1.1       nicm      600:        for (i = 0; i < sx; i++) {
                    601:                gc = grid_view_peek_cell(s->grid, i, py);
                    602:
                    603:                gu = NULL;
                    604:                if (gc->flags & GRID_FLAG_UTF8)
                    605:                        gu = grid_view_peek_utf8(s->grid, i, py);
                    606:
                    607:                if (screen_check_selection(s, i, py)) {
1.16      nicm      608:                        memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
                    609:                        tmpgc.data = gc->data;
1.77      nicm      610:                        tmpgc.flags = gc->flags &
1.38      nicm      611:                            ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
                    612:                        tmpgc.flags |= s->sel.cell.flags &
                    613:                            (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16      nicm      614:                        tty_cell(tty, &tmpgc, gu);
1.1       nicm      615:                } else
                    616:                        tty_cell(tty, gc, gu);
                    617:        }
                    618:
1.35      nicm      619:        if (sx >= tty->sx) {
1.107     nicm      620:                tty_update_mode(tty, tty->mode, s);
1.1       nicm      621:                return;
1.35      nicm      622:        }
1.1       nicm      623:        tty_reset(tty);
                    624:
1.41      nicm      625:        tty_cursor(tty, ox + sx, oy + py);
1.126     nicm      626:        if (sx != screen_size_x(s) && ox + screen_size_x(s) >= tty->sx &&
1.117     nicm      627:            tty_term_has(tty->term, TTYC_EL))
1.1       nicm      628:                tty_putcode(tty, TTYC_EL);
1.133     nicm      629:        else
                    630:                tty_repeat_space(tty, screen_size_x(s) - sx);
1.107     nicm      631:        tty_update_mode(tty, tty->mode, s);
1.15      nicm      632: }
                    633:
                    634: void
1.113     nicm      635: tty_write(
                    636:     void (*cmdfn)(struct tty *, const struct tty_ctx *), struct tty_ctx *ctx)
1.15      nicm      637: {
                    638:        struct window_pane      *wp = ctx->wp;
                    639:        struct client           *c;
                    640:        u_int                    i;
                    641:
1.75      nicm      642:        /* wp can be NULL if updating the screen but not the terminal. */
1.15      nicm      643:        if (wp == NULL)
                    644:                return;
                    645:
                    646:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    647:                return;
1.129     nicm      648:        if (!window_pane_visible(wp) || wp->flags & PANE_DROP)
1.15      nicm      649:                return;
                    650:
                    651:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    652:                c = ARRAY_ITEM(&clients, i);
1.139     nicm      653:                if (c == NULL || c->session == NULL || c->tty.term == NULL)
1.15      nicm      654:                        continue;
1.142     nicm      655:                if (c->flags & CLIENT_SUSPENDED)
                    656:                        continue;
                    657:                if (c->tty.flags & TTY_FREEZE)
1.139     nicm      658:                        continue;
                    659:                if (c->session->curw->window != wp->window)
1.15      nicm      660:                        continue;
                    661:
1.139     nicm      662:                ctx->xoff = wp->xoff;
                    663:                ctx->yoff = wp->yoff;
                    664:                if (status_at_line(c) == 0)
                    665:                        ctx->yoff++;
1.113     nicm      666:
1.139     nicm      667:                cmdfn(&c->tty, ctx);
1.1       nicm      668:        }
                    669: }
                    670:
                    671: void
1.24      nicm      672: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      673: {
1.77      nicm      674:        struct window_pane      *wp = ctx->wp;
1.1       nicm      675:
1.135     nicm      676:        if (!tty_pane_full_width(tty, ctx)) {
1.113     nicm      677:                tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      678:                return;
                    679:        }
                    680:
                    681:        tty_reset(tty);
                    682:
1.77      nicm      683:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      684:
1.1       nicm      685:        if (tty_term_has(tty->term, TTYC_ICH) ||
                    686:            tty_term_has(tty->term, TTYC_ICH1))
1.12      nicm      687:                tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.134     nicm      688:        else
1.113     nicm      689:                tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      690: }
                    691:
                    692: void
1.24      nicm      693: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      694: {
1.77      nicm      695:        struct window_pane      *wp = ctx->wp;
1.1       nicm      696:
1.135     nicm      697:        if (!tty_pane_full_width(tty, ctx) ||
1.26      nicm      698:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    699:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.113     nicm      700:                tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1       nicm      701:                return;
                    702:        }
                    703:
                    704:        tty_reset(tty);
                    705:
1.77      nicm      706:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41      nicm      707:
1.26      nicm      708:        if (tty_term_has(tty->term, TTYC_DCH) ||
                    709:            tty_term_has(tty->term, TTYC_DCH1))
                    710:                tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1       nicm      711: }
                    712:
                    713: void
1.24      nicm      714: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      715: {
1.132     nicm      716:        if (!tty_pane_full_width(tty, ctx) ||
1.77      nicm      717:            !tty_term_has(tty->term, TTYC_CSR) ||
1.72      nicm      718:            !tty_term_has(tty->term, TTYC_IL1)) {
1.14      nicm      719:                tty_redraw_region(tty, ctx);
1.1       nicm      720:                return;
                    721:        }
                    722:
                    723:        tty_reset(tty);
                    724:
1.77      nicm      725:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    726:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      727:
1.12      nicm      728:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      729: }
                    730:
                    731: void
1.24      nicm      732: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      733: {
1.132     nicm      734:        if (!tty_pane_full_width(tty, ctx) ||
1.72      nicm      735:            !tty_term_has(tty->term, TTYC_CSR) ||
                    736:            !tty_term_has(tty->term, TTYC_DL1)) {
1.14      nicm      737:                tty_redraw_region(tty, ctx);
1.1       nicm      738:                return;
                    739:        }
                    740:
                    741:        tty_reset(tty);
                    742:
1.77      nicm      743:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    744:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      745:
1.12      nicm      746:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      747: }
                    748:
                    749: void
1.24      nicm      750: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      751: {
1.77      nicm      752:        struct window_pane      *wp = ctx->wp;
1.12      nicm      753:        struct screen           *s = wp->screen;
1.1       nicm      754:
                    755:        tty_reset(tty);
                    756:
1.77      nicm      757:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41      nicm      758:
1.133     nicm      759:        if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
1.1       nicm      760:                tty_putcode(tty, TTYC_EL);
1.133     nicm      761:        else
                    762:                tty_repeat_space(tty, screen_size_x(s));
1.1       nicm      763: }
                    764:
                    765: void
1.24      nicm      766: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      767: {
1.77      nicm      768:        struct window_pane      *wp = ctx->wp;
1.12      nicm      769:        struct screen           *s = wp->screen;
1.1       nicm      770:
                    771:        tty_reset(tty);
                    772:
1.41      nicm      773:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    774:
1.132     nicm      775:        if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL))
1.1       nicm      776:                tty_putcode(tty, TTYC_EL);
1.133     nicm      777:        else
                    778:                tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.1       nicm      779: }
                    780:
                    781: void
1.24      nicm      782: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      783: {
                    784:        tty_reset(tty);
                    785:
1.113     nicm      786:        if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41      nicm      787:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      788:                tty_putcode(tty, TTYC_EL1);
                    789:        } else {
1.41      nicm      790:                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.133     nicm      791:                tty_repeat_space(tty, ctx->ocx + 1);
1.1       nicm      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.56      nicm      798:        if (ctx->ocy != ctx->orupper)
                    799:                return;
                    800:
1.132     nicm      801:        if (!tty_pane_full_width(tty, ctx) ||
1.70      nicm      802:            !tty_term_has(tty->term, TTYC_CSR) ||
                    803:            !tty_term_has(tty->term, TTYC_RI)) {
1.14      nicm      804:                tty_redraw_region(tty, ctx);
1.1       nicm      805:                return;
                    806:        }
                    807:
1.56      nicm      808:        tty_reset(tty);
1.77      nicm      809:
1.56      nicm      810:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    811:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77      nicm      812:
1.56      nicm      813:        tty_putcode(tty, TTYC_RI);
1.1       nicm      814: }
                    815:
                    816: void
1.24      nicm      817: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      818: {
1.77      nicm      819:        struct window_pane      *wp = ctx->wp;
1.1       nicm      820:
1.56      nicm      821:        if (ctx->ocy != ctx->orlower)
                    822:                return;
                    823:
1.132     nicm      824:        if (!tty_pane_full_width(tty, ctx) ||
1.1       nicm      825:            !tty_term_has(tty->term, TTYC_CSR)) {
1.128     nicm      826:                if (tty_large_region(tty, ctx))
                    827:                        wp->flags |= PANE_REDRAW;
1.140     nicm      828:                else
1.128     nicm      829:                        tty_redraw_region(tty, ctx);
1.1       nicm      830:                return;
                    831:        }
1.52      nicm      832:
                    833:        /*
                    834:         * If this line wrapped naturally (ctx->num is nonzero), don't do
                    835:         * anything - the cursor can just be moved to the last cell and wrap
                    836:         * naturally.
                    837:         */
                    838:        if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
                    839:                return;
1.1       nicm      840:
1.56      nicm      841:        tty_reset(tty);
1.77      nicm      842:
1.56      nicm      843:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    844:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77      nicm      845:
1.56      nicm      846:        tty_putc(tty, '\n');
1.1       nicm      847: }
                    848:
                    849: void
1.24      nicm      850: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      851: {
1.77      nicm      852:        struct window_pane      *wp = ctx->wp;
1.12      nicm      853:        struct screen           *s = wp->screen;
                    854:        u_int                    i, j;
1.1       nicm      855:
                    856:        tty_reset(tty);
                    857:
1.39      nicm      858:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      859:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39      nicm      860:
1.132     nicm      861:        if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1       nicm      862:                tty_putcode(tty, TTYC_EL);
1.14      nicm      863:                if (ctx->ocy != screen_size_y(s) - 1) {
1.41      nicm      864:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14      nicm      865:                        for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1       nicm      866:                                tty_putcode(tty, TTYC_EL);
                    867:                                if (i == screen_size_y(s) - 1)
                    868:                                        continue;
                    869:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    870:                                tty->cy++;
                    871:                        }
                    872:                }
                    873:        } else {
1.133     nicm      874:                tty_repeat_space(tty, screen_size_x(s) - ctx->ocx);
1.68      nicm      875:                for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41      nicm      876:                        tty_cursor_pane(tty, ctx, 0, j);
1.133     nicm      877:                        tty_repeat_space(tty, screen_size_x(s));
1.1       nicm      878:                }
                    879:        }
                    880: }
                    881:
                    882: void
1.24      nicm      883: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      884: {
1.77      nicm      885:        struct window_pane      *wp = ctx->wp;
1.12      nicm      886:        struct screen           *s = wp->screen;
                    887:        u_int                    i, j;
1.1       nicm      888:
                    889:        tty_reset(tty);
                    890:
1.39      nicm      891:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      892:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm      893:
1.132     nicm      894:        if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.14      nicm      895:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm      896:                        tty_putcode(tty, TTYC_EL);
                    897:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    898:                        tty->cy++;
                    899:                }
                    900:        } else {
1.14      nicm      901:                for (j = 0; j < ctx->ocy; j++) {
1.41      nicm      902:                        tty_cursor_pane(tty, ctx, 0, j);
1.133     nicm      903:                        tty_repeat_space(tty, screen_size_x(s));
1.1       nicm      904:                }
                    905:        }
1.133     nicm      906:        tty_repeat_space(tty, ctx->ocx + 1);
1.1       nicm      907: }
                    908:
                    909: void
1.24      nicm      910: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      911: {
1.77      nicm      912:        struct window_pane      *wp = ctx->wp;
1.12      nicm      913:        struct screen           *s = wp->screen;
                    914:        u_int                    i, j;
1.1       nicm      915:
                    916:        tty_reset(tty);
                    917:
1.39      nicm      918:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      919:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm      920:
1.132     nicm      921:        if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) {
1.1       nicm      922:                for (i = 0; i < screen_size_y(s); i++) {
                    923:                        tty_putcode(tty, TTYC_EL);
                    924:                        if (i != screen_size_y(s) - 1) {
                    925:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    926:                                tty->cy++;
                    927:                        }
                    928:                }
                    929:        } else {
                    930:                for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      931:                        tty_cursor_pane(tty, ctx, 0, j);
1.133     nicm      932:                        tty_repeat_space(tty, screen_size_x(s));
1.1       nicm      933:                }
1.4       nicm      934:        }
                    935: }
                    936:
                    937: void
1.24      nicm      938: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm      939: {
1.12      nicm      940:        struct window_pane      *wp = ctx->wp;
                    941:        struct screen           *s = wp->screen;
                    942:        u_int                    i, j;
1.4       nicm      943:
                    944:        tty_reset(tty);
                    945:
1.39      nicm      946:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4       nicm      947:
                    948:        for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      949:                tty_cursor_pane(tty, ctx, 0, j);
1.4       nicm      950:                for (i = 0; i < screen_size_x(s); i++)
                    951:                        tty_putc(tty, 'E');
1.1       nicm      952:        }
                    953: }
                    954:
                    955: void
1.24      nicm      956: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      957: {
1.46      nicm      958:        struct window_pane      *wp = ctx->wp;
                    959:        struct screen           *s = wp->screen;
1.58      nicm      960:        u_int                    cx;
1.102     nicm      961:        u_int                    width;
                    962:        const struct grid_cell  *gc = ctx->cell;
                    963:        const struct grid_utf8  *gu = ctx->utf8;
                    964:
                    965:        if (gc->flags & GRID_FLAG_UTF8)
                    966:                width = gu->width;
                    967:        else
                    968:                width = 1;
1.46      nicm      969:
                    970:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    971:
1.57      nicm      972:        /* Is the cursor in the very last position? */
1.102     nicm      973:        if (ctx->ocx > wp->sx - width) {
1.113     nicm      974:                if (ctx->xoff != 0 || wp->sx != tty->sx) {
1.57      nicm      975:                        /*
                    976:                         * The pane doesn't fill the entire line, the linefeed
                    977:                         * will already have happened, so just move the cursor.
                    978:                         */
1.125     nicm      979:                        if (ctx->ocy != wp->yoff + wp->screen->rlower)
1.122     nicm      980:                                tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
                    981:                        else
                    982:                                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.57      nicm      983:                } else if (tty->cx < tty->sx) {
                    984:                        /*
                    985:                         * The cursor isn't in the last position already, so
1.102     nicm      986:                         * move as far left as possible and redraw the last
1.57      nicm      987:                         * cell to move into the last position.
                    988:                         */
1.111     nicm      989:                        if (ctx->last_cell.flags & GRID_FLAG_UTF8)
                    990:                                cx = screen_size_x(s) - ctx->last_utf8.width;
                    991:                        else
                    992:                                cx = screen_size_x(s) - 1;
1.50      nicm      993:                        tty_cursor_pane(tty, ctx, cx, ctx->ocy);
                    994:                        tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
                    995:                }
                    996:        } else
1.46      nicm      997:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      998:
1.12      nicm      999:        tty_cell(tty, ctx->cell, ctx->utf8);
1.1       nicm     1000: }
                   1001:
                   1002: void
1.24      nicm     1003: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm     1004: {
1.53      nicm     1005:        struct window_pane      *wp = ctx->wp;
1.1       nicm     1006:
1.53      nicm     1007:        /*
                   1008:         * Cannot rely on not being a partial character, so just redraw the
                   1009:         * whole line.
                   1010:         */
1.113     nicm     1011:        tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.106     nicm     1012: }
                   1013:
                   1014: void
                   1015: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
                   1016: {
                   1017:        char    *buf;
                   1018:        size_t   off;
                   1019:
                   1020:        if (!tty_term_has(tty->term, TTYC_MS))
                   1021:                return;
                   1022:
                   1023:        off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
                   1024:        buf = xmalloc(off);
                   1025:
                   1026:        b64_ntop(ctx->ptr, ctx->num, buf, off);
                   1027:        tty_putcode_ptr2(tty, TTYC_MS, "", buf);
                   1028:
1.138     nicm     1029:        free(buf);
1.100     nicm     1030: }
                   1031:
                   1032: void
                   1033: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
                   1034: {
                   1035:        u_int    i;
                   1036:        u_char  *str = ctx->ptr;
                   1037:
                   1038:        for (i = 0; i < ctx->num; i++)
                   1039:                tty_putc(tty, str[i]);
1.141     nicm     1040:
                   1041:        tty->cx = tty->cy = UINT_MAX;
                   1042:        tty->rupper = tty->rlower = UINT_MAX;
                   1043:
                   1044:        tty_reset(tty);
                   1045:        tty_cursor(tty, 0, 0);
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: }