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

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