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

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