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

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