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

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