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

1.51    ! nicm        1: /* $OpenBSD: tty.c,v 1.50 2009/10/17 08:24:46 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:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
1.42      nicm       24: #include <stdlib.h>
1.1       nicm       25: #include <string.h>
                     26: #include <termios.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
                     30:
                     31: void   tty_fill_acs(struct tty *);
                     32:
                     33: int    tty_try_256(struct tty *, u_char, const char *);
                     34: int    tty_try_88(struct tty *, u_char, const char *);
                     35:
                     36: void   tty_attributes_fg(struct tty *, const struct grid_cell *);
                     37: void   tty_attributes_bg(struct tty *, const struct grid_cell *);
                     38:
1.24      nicm       39: void   tty_redraw_region(struct tty *, const struct tty_ctx *);
1.13      nicm       40: void   tty_emulate_repeat(
                     41:            struct tty *, enum tty_code_code, enum tty_code_code, u_int);
1.14      nicm       42: void   tty_cell(struct tty *,
                     43:            const struct grid_cell *, const struct grid_utf8 *);
1.1       nicm       44:
                     45: void
1.30      nicm       46: tty_init(struct tty *tty, int fd, char *term)
1.1       nicm       47: {
1.30      nicm       48:        char    *path;
                     49:
                     50:        memset(tty, 0, sizeof *tty);
1.23      nicm       51:        tty->log_fd = -1;
1.22      nicm       52:
1.9       nicm       53:        if (term == NULL || *term == '\0')
1.1       nicm       54:                tty->termname = xstrdup("unknown");
                     55:        else
                     56:                tty->termname = xstrdup(term);
1.30      nicm       57:
                     58:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                     59:                fatal("fcntl failed");
                     60:        tty->fd = fd;
                     61:
                     62:        if ((path = ttyname(fd)) == NULL)
                     63:                fatalx("ttyname failed");
                     64:        tty->path = xstrdup(path);
                     65:
1.1       nicm       66:        tty->flags = 0;
                     67:        tty->term_flags = 0;
1.31      nicm       68: }
                     69:
                     70: void
                     71: tty_resize(struct tty *tty)
                     72: {
                     73:        struct winsize  ws;
                     74:
                     75:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
                     76:                tty->sx = ws.ws_col;
                     77:                tty->sy = ws.ws_row;
                     78:        }
                     79:        if (tty->sx == 0)
                     80:                tty->sx = 80;
                     81:        if (tty->sy == 0)
                     82:                tty->sy = 24;
                     83:
                     84:        tty->cx = UINT_MAX;
                     85:        tty->cy = UINT_MAX;
                     86:
                     87:        tty->rupper = UINT_MAX;
                     88:        tty->rlower = UINT_MAX;
1.1       nicm       89: }
                     90:
                     91: int
1.17      nicm       92: tty_open(struct tty *tty, const char *overrides, char **cause)
1.1       nicm       93: {
1.30      nicm       94:        int     fd;
1.1       nicm       95:
1.30      nicm       96:        if (debug_level > 3) {
                     97:                fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
                     98:                if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                     99:                        fatal("fcntl failed");
                    100:                tty->log_fd = fd;
1.1       nicm      101:        }
                    102:
1.17      nicm      103:        tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
1.21      nicm      104:        if (tty->term == NULL) {
                    105:                tty_close(tty);
                    106:                return (-1);
                    107:        }
                    108:        tty->flags |= TTY_OPENED;
1.1       nicm      109:
                    110:        tty->in = buffer_create(BUFSIZ);
                    111:        tty->out = buffer_create(BUFSIZ);
                    112:
1.29      nicm      113:        tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
1.1       nicm      114:
                    115:        tty_start_tty(tty);
                    116:
                    117:        tty_keys_init(tty);
                    118:
                    119:        tty_fill_acs(tty);
                    120:
                    121:        return (0);
                    122: }
                    123:
                    124: void
                    125: tty_start_tty(struct tty *tty)
                    126: {
                    127:        struct termios   tio;
1.34      nicm      128:        int              what, mode;
1.33      nicm      129:
                    130:        if (tty->fd == -1)
                    131:                return;
1.1       nicm      132:
1.34      nicm      133:        if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
                    134:                fatal("fcntl failed");
                    135:        if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    136:                fatal("fcntl failed");
                    137:
1.1       nicm      138:        if (tcgetattr(tty->fd, &tty->tio) != 0)
                    139:                fatal("tcgetattr failed");
                    140:        memcpy(&tio, &tty->tio, sizeof tio);
                    141:        tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
                    142:        tio.c_iflag |= IGNBRK;
                    143:        tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
                    144:        tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
                    145:            ECHOPRT|ECHOKE|ECHOCTL|ISIG);
                    146:        tio.c_cc[VMIN] = 1;
                    147:         tio.c_cc[VTIME] = 0;
                    148:        if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
                    149:                fatal("tcsetattr failed");
                    150:
                    151:        what = 0;
                    152:        if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
                    153:                fatal("ioctl(TIOCFLUSH)");
                    154:
1.10      nicm      155:        tty_putcode(tty, TTYC_SMCUP);
1.1       nicm      156:
1.25      nicm      157:        tty_putcode(tty, TTYC_SGR0);
                    158:        memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
                    159:
1.1       nicm      160:        tty_putcode(tty, TTYC_SMKX);
                    161:        tty_putcode(tty, TTYC_ENACS);
                    162:        tty_putcode(tty, TTYC_CLEAR);
                    163:
                    164:        tty_putcode(tty, TTYC_CNORM);
                    165:        if (tty_term_has(tty->term, TTYC_KMOUS))
                    166:                tty_puts(tty, "\033[?1000l");
                    167:
                    168:        tty->cx = UINT_MAX;
                    169:        tty->cy = UINT_MAX;
                    170:
                    171:        tty->rlower = UINT_MAX;
                    172:        tty->rupper = UINT_MAX;
                    173:
                    174:        tty->mode = MODE_CURSOR;
1.20      nicm      175:
                    176:        tty->flags |= TTY_STARTED;
1.1       nicm      177: }
                    178:
                    179: void
                    180: tty_stop_tty(struct tty *tty)
                    181: {
                    182:        struct winsize  ws;
1.34      nicm      183:        int             mode;
1.1       nicm      184:
1.20      nicm      185:        if (!(tty->flags & TTY_STARTED))
                    186:                return;
                    187:        tty->flags &= ~TTY_STARTED;
                    188:
1.1       nicm      189:        /*
                    190:         * Be flexible about error handling and try not kill the server just
                    191:         * because the fd is invalid. Things like ssh -t can easily leave us
                    192:         * with a dead tty.
                    193:         */
1.34      nicm      194:        if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
                    195:                return;
                    196:        if (fcntl(tty->fd, F_SETFL, mode & ~O_NONBLOCK) == -1)
                    197:                return;
1.1       nicm      198:        if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
                    199:                return;
                    200:        if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
                    201:                return;
                    202:
                    203:        tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
                    204:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
                    205:        tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
                    206:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
                    207:        tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
                    208:
                    209:        tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
                    210:        if (tty_term_has(tty->term, TTYC_KMOUS))
                    211:                tty_raw(tty, "\033[?1000l");
1.10      nicm      212:
                    213:        tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.1       nicm      214: }
                    215:
                    216: void
                    217: tty_fill_acs(struct tty *tty)
                    218: {
                    219:        const char *ptr;
                    220:
                    221:        memset(tty->acs, 0, sizeof tty->acs);
                    222:        if (!tty_term_has(tty->term, TTYC_ACSC))
                    223:                return;
                    224:
                    225:        ptr = tty_term_string(tty->term, TTYC_ACSC);
                    226:        if (strlen(ptr) % 2 != 0)
                    227:                return;
                    228:        for (; *ptr != '\0'; ptr += 2)
                    229:                tty->acs[(u_char) ptr[0]] = ptr[1];
                    230: }
                    231:
                    232: u_char
                    233: tty_get_acs(struct tty *tty, u_char ch)
                    234: {
                    235:        if (tty->acs[ch] != '\0')
                    236:                return (tty->acs[ch]);
                    237:        return (ch);
                    238: }
                    239:
                    240: void
1.20      nicm      241: tty_close(struct tty *tty)
1.1       nicm      242: {
                    243:        if (tty->log_fd != -1) {
                    244:                close(tty->log_fd);
                    245:                tty->log_fd = -1;
                    246:        }
                    247:
1.20      nicm      248:        tty_stop_tty(tty);
1.1       nicm      249:
1.21      nicm      250:        if (tty->flags & TTY_OPENED) {
                    251:                tty_term_free(tty->term);
                    252:                tty_keys_free(tty);
                    253:
                    254:                buffer_destroy(tty->in);
                    255:                buffer_destroy(tty->out);
1.1       nicm      256:
1.21      nicm      257:                tty->flags &= ~TTY_OPENED;
                    258:        }
1.1       nicm      259:
1.21      nicm      260:        if (tty->fd != -1) {
                    261:                close(tty->fd);
                    262:                tty->fd = -1;
                    263:        }
1.1       nicm      264: }
                    265:
                    266: void
1.20      nicm      267: tty_free(struct tty *tty)
1.1       nicm      268: {
1.20      nicm      269:        tty_close(tty);
1.1       nicm      270:
                    271:        if (tty->path != NULL)
                    272:                xfree(tty->path);
                    273:        if (tty->termname != NULL)
                    274:                xfree(tty->termname);
                    275: }
                    276:
                    277: void
                    278: tty_raw(struct tty *tty, const char *s)
                    279: {
                    280:        write(tty->fd, s, strlen(s));
                    281: }
                    282:
                    283: void
                    284: tty_putcode(struct tty *tty, enum tty_code_code code)
                    285: {
                    286:        tty_puts(tty, tty_term_string(tty->term, code));
                    287: }
                    288:
                    289: void
                    290: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
                    291: {
                    292:        if (a < 0)
                    293:                return;
                    294:        tty_puts(tty, tty_term_string1(tty->term, code, a));
                    295: }
                    296:
                    297: void
                    298: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
                    299: {
                    300:        if (a < 0 || b < 0)
                    301:                return;
                    302:        tty_puts(tty, tty_term_string2(tty->term, code, a, b));
                    303: }
                    304:
                    305: void
                    306: tty_puts(struct tty *tty, const char *s)
                    307: {
                    308:        if (*s == '\0')
                    309:                return;
                    310:        buffer_write(tty->out, s, strlen(s));
                    311:
                    312:        if (tty->log_fd != -1)
                    313:                write(tty->log_fd, s, strlen(s));
                    314: }
                    315:
                    316: void
                    317: tty_putc(struct tty *tty, u_char ch)
                    318: {
                    319:        u_int   sx;
                    320:
                    321:        if (tty->cell.attr & GRID_ATTR_CHARSET)
                    322:                ch = tty_get_acs(tty, ch);
                    323:        buffer_write8(tty->out, ch);
                    324:
                    325:        if (ch >= 0x20 && ch != 0x7f) {
                    326:                sx = tty->sx;
                    327:                if (tty->term->flags & TERM_EARLYWRAP)
                    328:                        sx--;
                    329:
1.46      nicm      330:                if (tty->cx >= sx) {
                    331:                        tty->cx = 1;
                    332:                        if (tty->cy != tty->rlower)
                    333:                                tty->cy++;
1.1       nicm      334:                } else
                    335:                        tty->cx++;
                    336:        }
                    337:
                    338:        if (tty->log_fd != -1)
                    339:                write(tty->log_fd, &ch, 1);
                    340: }
                    341:
                    342: void
1.5       nicm      343: tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
                    344: {
                    345:        u_int   i, width;
                    346:
                    347:        for (i = 0; i < UTF8_SIZE; i++) {
                    348:                if (gu->data[i] == 0xff)
                    349:                        break;
                    350:                buffer_write8(tty->out, gu->data[i]);
                    351:                if (tty->log_fd != -1)
                    352:                        write(tty->log_fd, &gu->data[i], 1);
                    353:        }
                    354:
                    355:        width = utf8_width(gu->data);
                    356:        tty->cx += width;
                    357: }
                    358:
                    359: void
1.1       nicm      360: tty_set_title(struct tty *tty, const char *title)
                    361: {
                    362:        if (strstr(tty->termname, "xterm") == NULL &&
                    363:            strstr(tty->termname, "rxvt") == NULL &&
                    364:            strcmp(tty->termname, "screen") != 0)
                    365:                return;
                    366:
                    367:        tty_puts(tty, "\033]0;");
                    368:        tty_puts(tty, title);
                    369:        tty_putc(tty, '\007');
                    370: }
                    371:
                    372: void
                    373: tty_update_mode(struct tty *tty, int mode)
                    374: {
                    375:        int     changed;
                    376:
                    377:        if (tty->flags & TTY_NOCURSOR)
                    378:                mode &= ~MODE_CURSOR;
                    379:
                    380:        changed = mode ^ tty->mode;
                    381:        if (changed & MODE_CURSOR) {
                    382:                if (mode & MODE_CURSOR)
                    383:                        tty_putcode(tty, TTYC_CNORM);
                    384:                else
                    385:                        tty_putcode(tty, TTYC_CIVIS);
                    386:        }
                    387:        if (changed & MODE_MOUSE) {
                    388:                if (mode & MODE_MOUSE)
                    389:                        tty_puts(tty, "\033[?1000h");
                    390:                else
                    391:                        tty_puts(tty, "\033[?1000l");
                    392:        }
                    393:        tty->mode = mode;
                    394: }
                    395:
                    396: void
                    397: tty_emulate_repeat(
                    398:     struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
                    399: {
                    400:        if (tty_term_has(tty->term, code))
                    401:                tty_putcode1(tty, code, n);
                    402:        else {
                    403:                while (n-- > 0)
                    404:                        tty_putcode(tty, code1);
                    405:        }
                    406: }
                    407:
                    408: /*
                    409:  * Redraw scroll region using data from screen (already updated). Used when
                    410:  * CSR not supported, or window is a pane that doesn't take up the full
                    411:  * width of the terminal.
                    412:  */
                    413: void
1.24      nicm      414: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      415: {
1.14      nicm      416:        struct window_pane      *wp = ctx->wp;
                    417:        struct screen           *s = wp->screen;
                    418:        u_int                    i;
1.1       nicm      419:
                    420:        /*
                    421:         * If region is >= 50% of the screen, just schedule a window redraw. In
                    422:         * most cases, this is likely to be followed by some more scrolling -
                    423:         * without this, the entire pane ends up being redrawn many times which
                    424:         * can be much more data.
                    425:         */
1.14      nicm      426:        if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
1.1       nicm      427:                wp->flags |= PANE_REDRAW;
                    428:                return;
                    429:        }
                    430:
1.14      nicm      431:        if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
                    432:                for (i = ctx->ocy; i < screen_size_y(s); i++)
1.1       nicm      433:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    434:        } else {
1.14      nicm      435:                for (i = ctx->orupper; i <= ctx->orlower; i++)
1.1       nicm      436:                        tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
                    437:        }
                    438: }
                    439:
                    440: void
                    441: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
                    442: {
                    443:        const struct grid_cell  *gc;
1.46      nicm      444:        struct grid_line        *gl;
1.16      nicm      445:        struct grid_cell         tmpgc;
1.1       nicm      446:        const struct grid_utf8  *gu;
                    447:        u_int                    i, sx;
                    448:
1.35      nicm      449:        tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
                    450:
1.1       nicm      451:        sx = screen_size_x(s);
1.19      nicm      452:        if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
                    453:                sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1       nicm      454:        if (sx > tty->sx)
                    455:                sx = tty->sx;
                    456:
1.46      nicm      457:        /*
                    458:         * Don't move the cursor to the start permission if it will wrap there
1.50      nicm      459:         * itself.
1.46      nicm      460:         */
                    461:        gl = NULL;
                    462:        if (py != 0)
                    463:                gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.47      nicm      464:        if (oy + py == 0 || (gl != NULL && !(gl->flags & GRID_LINE_WRAPPED)) ||
                    465:            tty->cx < tty->sx || ox != 0 ||
                    466:            (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46      nicm      467:                tty_cursor(tty, ox, oy + py);
                    468:
1.1       nicm      469:        for (i = 0; i < sx; i++) {
                    470:                gc = grid_view_peek_cell(s->grid, i, py);
                    471:
                    472:                gu = NULL;
                    473:                if (gc->flags & GRID_FLAG_UTF8)
                    474:                        gu = grid_view_peek_utf8(s->grid, i, py);
                    475:
                    476:                if (screen_check_selection(s, i, py)) {
1.16      nicm      477:                        memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
                    478:                        tmpgc.data = gc->data;
1.38      nicm      479:                        tmpgc.flags = gc->flags &
                    480:                            ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
                    481:                        tmpgc.flags |= s->sel.cell.flags &
                    482:                            (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16      nicm      483:                        tty_cell(tty, &tmpgc, gu);
1.1       nicm      484:                } else
                    485:                        tty_cell(tty, gc, gu);
                    486:        }
                    487:
1.35      nicm      488:        if (sx >= tty->sx) {
                    489:                tty_update_mode(tty, tty->mode);
1.1       nicm      490:                return;
1.35      nicm      491:        }
1.1       nicm      492:        tty_reset(tty);
                    493:
1.41      nicm      494:        tty_cursor(tty, ox + sx, oy + py);
1.1       nicm      495:        if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
                    496:                tty_putcode(tty, TTYC_EL);
                    497:        else {
                    498:                for (i = sx; i < screen_size_x(s); i++)
                    499:                        tty_putc(tty, ' ');
1.15      nicm      500:        }
1.35      nicm      501:        tty_update_mode(tty, tty->mode);
1.15      nicm      502: }
                    503:
                    504: void
1.24      nicm      505: tty_write(void (*cmdfn)(
                    506:     struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
1.15      nicm      507: {
                    508:        struct window_pane      *wp = ctx->wp;
                    509:        struct client           *c;
                    510:        u_int                    i;
                    511:
                    512:        if (wp == NULL)
                    513:                return;
                    514:
                    515:        if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
                    516:                return;
                    517:        if (wp->window->flags & WINDOW_HIDDEN || !window_pane_visible(wp))
                    518:                return;
                    519:
                    520:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    521:                c = ARRAY_ITEM(&clients, i);
                    522:                if (c == NULL || c->session == NULL)
                    523:                        continue;
                    524:                if (c->flags & CLIENT_SUSPENDED)
                    525:                        continue;
                    526:
                    527:                if (c->session->curw->window == wp->window) {
                    528:                        if (c->tty.flags & TTY_FREEZE || c->tty.term == NULL)
                    529:                                continue;
                    530:                        cmdfn(&c->tty, ctx);
                    531:                }
1.1       nicm      532:        }
                    533: }
                    534:
                    535: void
1.24      nicm      536: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      537: {
1.12      nicm      538:        struct window_pane      *wp = ctx->wp;
                    539:        struct screen           *s = wp->screen;
1.24      nicm      540:        u_int                    i;
1.1       nicm      541:
                    542:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
1.14      nicm      543:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      544:                return;
                    545:        }
                    546:
                    547:        tty_reset(tty);
                    548:
1.41      nicm      549:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    550:
1.1       nicm      551:        if (tty_term_has(tty->term, TTYC_ICH) ||
                    552:            tty_term_has(tty->term, TTYC_ICH1))
1.12      nicm      553:                tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1       nicm      554:        else {
                    555:                tty_putcode(tty, TTYC_SMIR);
1.24      nicm      556:                for (i = 0; i < ctx->num; i++)
1.1       nicm      557:                        tty_putc(tty, ' ');
                    558:                tty_putcode(tty, TTYC_RMIR);
                    559:        }
                    560: }
                    561:
                    562: void
1.24      nicm      563: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      564: {
1.12      nicm      565:        struct window_pane      *wp = ctx->wp;
                    566:        struct screen           *s = wp->screen;
1.1       nicm      567:
1.26      nicm      568:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    569:            (!tty_term_has(tty->term, TTYC_DCH) &&
                    570:            !tty_term_has(tty->term, TTYC_DCH1))) {
1.14      nicm      571:                tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1       nicm      572:                return;
                    573:        }
                    574:
                    575:        tty_reset(tty);
                    576:
1.41      nicm      577:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    578:
1.26      nicm      579:        if (tty_term_has(tty->term, TTYC_DCH) ||
                    580:            tty_term_has(tty->term, TTYC_DCH1))
                    581:                tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1       nicm      582: }
                    583:
                    584: void
1.24      nicm      585: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      586: {
1.12      nicm      587:        struct window_pane      *wp = ctx->wp;
                    588:        struct screen           *s = wp->screen;
1.1       nicm      589:
                    590:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    591:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      592:                tty_redraw_region(tty, ctx);
1.1       nicm      593:                return;
                    594:        }
                    595:
                    596:        tty_reset(tty);
                    597:
1.39      nicm      598:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41      nicm      599:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      600:
1.12      nicm      601:        tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1       nicm      602: }
                    603:
                    604: void
1.24      nicm      605: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      606: {
1.12      nicm      607:        struct window_pane      *wp = ctx->wp;
                    608:        struct screen           *s = wp->screen;
1.1       nicm      609:
                    610:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    611:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      612:                tty_redraw_region(tty, ctx);
1.1       nicm      613:                return;
                    614:        }
                    615:
                    616:        tty_reset(tty);
                    617:
1.39      nicm      618:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41      nicm      619:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      620:
1.12      nicm      621:        tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1       nicm      622: }
                    623:
                    624: void
1.24      nicm      625: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      626: {
1.12      nicm      627:        struct window_pane      *wp = ctx->wp;
                    628:        struct screen           *s = wp->screen;
                    629:        u_int                    i;
1.1       nicm      630:
                    631:        tty_reset(tty);
                    632:
1.41      nicm      633:        tty_cursor_pane(tty, ctx, 0, ctx->ocy);
                    634:
1.1       nicm      635:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    636:            tty_term_has(tty->term, TTYC_EL)) {
                    637:                tty_putcode(tty, TTYC_EL);
                    638:        } else {
                    639:                for (i = 0; i < screen_size_x(s); i++)
                    640:                        tty_putc(tty, ' ');
                    641:        }
                    642: }
                    643:
                    644: void
1.24      nicm      645: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      646: {
1.12      nicm      647:        struct window_pane      *wp = ctx->wp;
                    648:        struct screen           *s = wp->screen;
                    649:        u_int                    i;
1.1       nicm      650:
                    651:        tty_reset(tty);
                    652:
1.41      nicm      653:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
                    654:
1.1       nicm      655:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    656:            tty_term_has(tty->term, TTYC_EL))
                    657:                tty_putcode(tty, TTYC_EL);
                    658:        else {
1.14      nicm      659:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      660:                        tty_putc(tty, ' ');
                    661:        }
                    662: }
                    663:
                    664: void
1.24      nicm      665: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      666: {
1.12      nicm      667:        struct window_pane      *wp = ctx->wp;
                    668:        u_int                    i;
1.1       nicm      669:
                    670:        tty_reset(tty);
                    671:
                    672:        if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41      nicm      673:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      674:                tty_putcode(tty, TTYC_EL1);
                    675:        } else {
1.41      nicm      676:                tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.14      nicm      677:                for (i = 0; i < ctx->ocx + 1; i++)
1.1       nicm      678:                        tty_putc(tty, ' ');
                    679:        }
                    680: }
                    681:
                    682: void
1.24      nicm      683: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      684: {
1.12      nicm      685:        struct window_pane      *wp = ctx->wp;
                    686:        struct screen           *s = wp->screen;
1.1       nicm      687:
                    688:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    689:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      690:                tty_redraw_region(tty, ctx);
1.1       nicm      691:                return;
                    692:        }
                    693:
1.14      nicm      694:        if (ctx->ocy == ctx->orupper) {
1.38      nicm      695:                tty_reset(tty);
1.39      nicm      696:
                    697:                tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41      nicm      698:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.39      nicm      699:
1.1       nicm      700:                tty_putcode(tty, TTYC_RI);
                    701:        }
                    702: }
                    703:
                    704: void
1.24      nicm      705: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      706: {
1.12      nicm      707:        struct window_pane      *wp = ctx->wp;
                    708:        struct screen           *s = wp->screen;
1.1       nicm      709:
                    710:        if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
                    711:            !tty_term_has(tty->term, TTYC_CSR)) {
1.14      nicm      712:                tty_redraw_region(tty, ctx);
1.1       nicm      713:                return;
                    714:        }
                    715:
1.14      nicm      716:        if (ctx->ocy == ctx->orlower) {
1.37      nicm      717:                tty_reset(tty);
1.39      nicm      718:
                    719:                tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41      nicm      720:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39      nicm      721:
1.1       nicm      722:                tty_putc(tty, '\n');
                    723:        }
                    724: }
                    725:
                    726: void
1.24      nicm      727: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      728: {
1.12      nicm      729:        struct window_pane      *wp = ctx->wp;
                    730:        struct screen           *s = wp->screen;
                    731:        u_int                    i, j;
1.1       nicm      732:
                    733:        tty_reset(tty);
                    734:
1.39      nicm      735:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      736:        tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39      nicm      737:
1.1       nicm      738:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    739:            tty_term_has(tty->term, TTYC_EL)) {
                    740:                tty_putcode(tty, TTYC_EL);
1.14      nicm      741:                if (ctx->ocy != screen_size_y(s) - 1) {
1.41      nicm      742:                        tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14      nicm      743:                        for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1       nicm      744:                                tty_putcode(tty, TTYC_EL);
                    745:                                if (i == screen_size_y(s) - 1)
                    746:                                        continue;
                    747:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    748:                                tty->cy++;
                    749:                        }
                    750:                }
                    751:        } else {
1.14      nicm      752:                for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1       nicm      753:                        tty_putc(tty, ' ');
1.14      nicm      754:                for (j = ctx->ocy; j < screen_size_y(s); j++) {
1.41      nicm      755:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      756:                        for (i = 0; i < screen_size_x(s); i++)
                    757:                                tty_putc(tty, ' ');
                    758:                }
                    759:        }
                    760: }
                    761:
                    762: void
1.24      nicm      763: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      764: {
1.12      nicm      765:        struct window_pane      *wp = ctx->wp;
                    766:        struct screen           *s = wp->screen;
                    767:        u_int                    i, j;
1.1       nicm      768:
                    769:        tty_reset(tty);
                    770:
1.39      nicm      771:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      772:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm      773:
1.1       nicm      774:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    775:            tty_term_has(tty->term, TTYC_EL)) {
1.14      nicm      776:                for (i = 0; i < ctx->ocy; i++) {
1.1       nicm      777:                        tty_putcode(tty, TTYC_EL);
                    778:                        tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    779:                        tty->cy++;
                    780:                }
                    781:        } else {
1.14      nicm      782:                for (j = 0; j < ctx->ocy; j++) {
1.41      nicm      783:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      784:                        for (i = 0; i < screen_size_x(s); i++)
                    785:                                tty_putc(tty, ' ');
                    786:                }
                    787:        }
1.14      nicm      788:        for (i = 0; i <= ctx->ocx; i++)
1.1       nicm      789:                tty_putc(tty, ' ');
                    790: }
                    791:
                    792: void
1.24      nicm      793: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      794: {
1.12      nicm      795:        struct window_pane      *wp = ctx->wp;
                    796:        struct screen           *s = wp->screen;
                    797:        u_int                    i, j;
1.1       nicm      798:
                    799:        tty_reset(tty);
                    800:
1.39      nicm      801:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41      nicm      802:        tty_cursor_pane(tty, ctx, 0, 0);
1.39      nicm      803:
1.1       nicm      804:        if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
                    805:            tty_term_has(tty->term, TTYC_EL)) {
                    806:                for (i = 0; i < screen_size_y(s); i++) {
                    807:                        tty_putcode(tty, TTYC_EL);
                    808:                        if (i != screen_size_y(s) - 1) {
                    809:                                tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
                    810:                                tty->cy++;
                    811:                        }
                    812:                }
                    813:        } else {
                    814:                for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      815:                        tty_cursor_pane(tty, ctx, 0, j);
1.1       nicm      816:                        for (i = 0; i < screen_size_x(s); i++)
                    817:                                tty_putc(tty, ' ');
                    818:                }
1.4       nicm      819:        }
                    820: }
                    821:
                    822: void
1.24      nicm      823: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4       nicm      824: {
1.12      nicm      825:        struct window_pane      *wp = ctx->wp;
                    826:        struct screen           *s = wp->screen;
                    827:        u_int                    i, j;
1.4       nicm      828:
                    829:        tty_reset(tty);
                    830:
1.39      nicm      831:        tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4       nicm      832:
                    833:        for (j = 0; j < screen_size_y(s); j++) {
1.41      nicm      834:                tty_cursor_pane(tty, ctx, 0, j);
1.4       nicm      835:                for (i = 0; i < screen_size_x(s); i++)
                    836:                        tty_putc(tty, 'E');
1.1       nicm      837:        }
                    838: }
                    839:
                    840: void
1.24      nicm      841: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      842: {
1.46      nicm      843:        struct window_pane      *wp = ctx->wp;
                    844:        struct screen           *s = wp->screen;
1.50      nicm      845:        u_int                    cx;
1.46      nicm      846:
                    847:        tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
                    848:
                    849:        /*
1.50      nicm      850:         * Should the cursor be in the last cursor position ready for a natural
                    851:         * wrap? If so - and it isn't - move to and rewrite the last cell.
1.46      nicm      852:         */
1.51    ! nicm      853:        if (!(tty->term->flags & TERM_EARLYWRAP) &&
        !           854:            ctx->ocx + wp->xoff > tty->sx - ctx->last_width) {
1.50      nicm      855:                if (tty->cx < tty->sx) {
                    856:                        cx = screen_size_x(s) - ctx->last_width;
                    857:                        tty_cursor_pane(tty, ctx, cx, ctx->ocy);
                    858:                        tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
                    859:                }
                    860:        } else
1.46      nicm      861:                tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1       nicm      862:
1.12      nicm      863:        tty_cell(tty, ctx->cell, ctx->utf8);
1.1       nicm      864: }
                    865:
                    866: void
1.24      nicm      867: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1       nicm      868: {
1.12      nicm      869:        u_char  *ptr = ctx->ptr;
1.11      nicm      870:        size_t   i;
1.1       nicm      871:
1.11      nicm      872:        for (i = 0; i < UTF8_SIZE; i++) {
1.12      nicm      873:                if (ptr[i] == 0xff)
1.11      nicm      874:                        break;
1.12      nicm      875:                tty_putc(tty, ptr[i]);
1.11      nicm      876:        }
1.1       nicm      877: }
                    878:
                    879: void
                    880: tty_cell(
                    881:     struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
                    882: {
                    883:        u_int   i;
                    884:
                    885:        /* Skip last character if terminal is stupid. */
                    886:        if (tty->term->flags & TERM_EARLYWRAP &&
                    887:            tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
                    888:                return;
                    889:
                    890:        /* If this is a padding character, do nothing. */
                    891:        if (gc->flags & GRID_FLAG_PADDING)
                    892:                return;
                    893:
                    894:        /* Set the attributes. */
                    895:        tty_attributes(tty, gc);
                    896:
                    897:        /* If not UTF-8, write directly. */
                    898:        if (!(gc->flags & GRID_FLAG_UTF8)) {
                    899:                if (gc->data < 0x20 || gc->data == 0x7f)
                    900:                        return;
                    901:                tty_putc(tty, gc->data);
                    902:                return;
                    903:        }
                    904:
                    905:        /* If the terminal doesn't support UTF-8, write underscores. */
                    906:        if (!(tty->flags & TTY_UTF8)) {
                    907:                for (i = 0; i < gu->width; i++)
                    908:                        tty_putc(tty, '_');
                    909:                return;
                    910:        }
                    911:
                    912:        /* Otherwise, write UTF-8. */
1.5       nicm      913:        tty_pututf8(tty, gu);
1.1       nicm      914: }
                    915:
                    916: void
                    917: tty_reset(struct tty *tty)
                    918: {
                    919:        struct grid_cell        *gc = &tty->cell;
                    920:
                    921:        if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
                    922:                return;
                    923:
                    924:        if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
                    925:                tty_putcode(tty, TTYC_RMACS);
                    926:        tty_putcode(tty, TTYC_SGR0);
                    927:        memcpy(gc, &grid_default_cell, sizeof *gc);
                    928: }
                    929:
1.40      nicm      930: /* Set region inside pane. */
1.1       nicm      931: void
1.39      nicm      932: tty_region_pane(
                    933:     struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1       nicm      934: {
1.39      nicm      935:        struct window_pane      *wp = ctx->wp;
                    936:
1.40      nicm      937:        tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
1.39      nicm      938: }
                    939:
1.40      nicm      940: /* Set region at absolute position. */
1.39      nicm      941: void
1.40      nicm      942: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39      nicm      943: {
                    944:        if (tty->rlower == rlower && tty->rupper == rupper)
                    945:                return;
1.1       nicm      946:        if (!tty_term_has(tty->term, TTYC_CSR))
                    947:                return;
1.39      nicm      948:
                    949:        tty->rupper = rupper;
                    950:        tty->rlower = rlower;
1.42      nicm      951:
1.39      nicm      952:        tty->cx = 0;
                    953:        tty->cy = 0;
                    954:
                    955:        tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.1       nicm      956: }
                    957:
1.42      nicm      958: /* Move cursor inside pane. */
1.1       nicm      959: void
1.41      nicm      960: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
                    961: {
                    962:        struct window_pane      *wp = ctx->wp;
                    963:
                    964:        tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
                    965: }
                    966:
1.42      nicm      967: /* Move cursor to absolute position. */
1.41      nicm      968: void
                    969: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1       nicm      970: {
1.42      nicm      971:        struct tty_term *term = tty->term;
                    972:        u_int            thisx, thisy;
                    973:        int              change;
                    974:
                    975:        if (cx > tty->sx - 1)
                    976:                cx = tty->sx - 1;
                    977:
                    978:        thisx = tty->cx;
                    979:        thisy = tty->cy;
                    980:
                    981:        /* No change. */
                    982:        if (cx == thisx && cy == thisy)
                    983:                return;
                    984:
1.43      nicm      985:        /* Very end of the line, just use absolute movement. */
                    986:        if (thisx > tty->sx - 1)
                    987:                goto absolute;
                    988:
1.42      nicm      989:        /* Move to home position (0, 0). */
                    990:        if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
                    991:                tty_putcode(tty, TTYC_HOME);
                    992:                goto out;
                    993:        }
                    994:
                    995:        /* Zero on the next line. */
1.48      nicm      996:        if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1       nicm      997:                tty_putc(tty, '\r');
1.42      nicm      998:                tty_putc(tty, '\n');
                    999:                goto out;
                   1000:        }
                   1001:
1.45      nicm     1002:        /* Moving column or row. */
1.42      nicm     1003:        if (cy == thisy) {
1.45      nicm     1004:                /*
                   1005:                 * Moving column only, row staying the same.
                   1006:                 */
                   1007:
1.42      nicm     1008:                /* To left edge. */
                   1009:                if (cx == 0)    {
                   1010:                        tty_putc(tty, '\r');
                   1011:                        goto out;
                   1012:                }
                   1013:
                   1014:                /* One to the left. */
                   1015:                if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
                   1016:                        tty_putcode(tty, TTYC_CUB1);
                   1017:                        goto out;
                   1018:                }
                   1019:
                   1020:                /* One to the right. */
                   1021:                if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
                   1022:                        tty_putcode(tty, TTYC_CUF1);
                   1023:                        goto out;
                   1024:                }
                   1025:
                   1026:                /* Calculate difference. */
                   1027:                change = thisx - cx;    /* +ve left, -ve right */
                   1028:
                   1029:                /*
                   1030:                 * Use HPA if change is larger than absolute, otherwise move
                   1031:                 * the cursor with CUB/CUF.
                   1032:                 */
                   1033:                if (abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
                   1034:                        tty_putcode1(tty, TTYC_HPA, cx);
                   1035:                        goto out;
                   1036:                } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
                   1037:                        tty_putcode1(tty, TTYC_CUB, change);
                   1038:                        goto out;
                   1039:                } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
                   1040:                        tty_putcode1(tty, TTYC_CUF, -change);
                   1041:                        goto out;
                   1042:                }
1.45      nicm     1043:        } else if (cx == thisx) {
                   1044:                /*
                   1045:                 * Moving row only, column staying the same.
                   1046:                 */
1.42      nicm     1047:
                   1048:                /* One above. */
1.49      nicm     1049:                if (thisy != tty->rupper &&
1.42      nicm     1050:                    cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
                   1051:                        tty_putcode(tty, TTYC_CUU1);
                   1052:                        goto out;
                   1053:                }
                   1054:
                   1055:                /* One below. */
1.49      nicm     1056:                if (thisy != tty->rlower &&
1.42      nicm     1057:                    cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
                   1058:                        tty_putcode(tty, TTYC_CUD1);
                   1059:                        goto out;
                   1060:                }
                   1061:
                   1062:                /* Calculate difference. */
                   1063:                change = thisy - cy;    /* +ve up, -ve down */
                   1064:
                   1065:                /*
1.44      nicm     1066:                 * Try to use VPA if change is larger than absolute or if this change
1.42      nicm     1067:                 * would cross the scroll region, otherwise use CUU/CUD.
                   1068:                 */
1.44      nicm     1069:                if (abs(change) > cy ||
1.42      nicm     1070:                    (change < 0 && cy - change > tty->rlower) ||
1.44      nicm     1071:                    (change > 0 && cy - change < tty->rupper)) {
                   1072:                            if (tty_term_has(term, TTYC_VPA)) {
                   1073:                                    tty_putcode1(tty, TTYC_VPA, cy);
                   1074:                                    goto out;
                   1075:                            }
1.42      nicm     1076:                } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
                   1077:                        tty_putcode1(tty, TTYC_CUU, change);
                   1078:                        goto out;
                   1079:                } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
                   1080:                        tty_putcode1(tty, TTYC_CUD, -change);
                   1081:                        goto out;
                   1082:                }
                   1083:        }
                   1084:
1.43      nicm     1085: absolute:
1.42      nicm     1086:        /* Absolute movement. */
                   1087:        tty_putcode2(tty, TTYC_CUP, cy, cx);
                   1088:
                   1089: out:
                   1090:        tty->cx = cx;
                   1091:        tty->cy = cy;
1.1       nicm     1092: }
                   1093:
                   1094: void
                   1095: tty_attributes(struct tty *tty, const struct grid_cell *gc)
                   1096: {
                   1097:        struct grid_cell        *tc = &tty->cell;
                   1098:        u_char                   changed;
1.18      nicm     1099:        u_int                    fg, bg, attr;
                   1100:
                   1101:        /*
                   1102:         * If no setab, try to use the reverse attribute as a best-effort for a
                   1103:         * non-default background. This is a bit of a hack but it doesn't do
                   1104:         * any serious harm and makes a couple of applications happier.
                   1105:         */
                   1106:        fg = gc->fg; bg = gc->bg; attr = gc->attr;
                   1107:        if (!tty_term_has(tty->term, TTYC_SETAB)) {
                   1108:                if (attr & GRID_ATTR_REVERSE) {
                   1109:                        if (fg != 7 && fg != 8)
                   1110:                                attr &= ~GRID_ATTR_REVERSE;
                   1111:                } else {
                   1112:                        if (bg != 0 && bg != 8)
                   1113:                                attr |= GRID_ATTR_REVERSE;
                   1114:                }
                   1115:        }
1.1       nicm     1116:
                   1117:        /* If any bits are being cleared, reset everything. */
1.18      nicm     1118:        if (tc->attr & ~attr)
1.1       nicm     1119:                tty_reset(tty);
                   1120:
                   1121:        /* Filter out attribute bits already set. */
1.18      nicm     1122:        changed = attr & ~tc->attr;
                   1123:        tc->attr = attr;
1.1       nicm     1124:
                   1125:        /* Set the attributes. */
                   1126:        if (changed & GRID_ATTR_BRIGHT)
                   1127:                tty_putcode(tty, TTYC_BOLD);
                   1128:        if (changed & GRID_ATTR_DIM)
                   1129:                tty_putcode(tty, TTYC_DIM);
                   1130:        if (changed & GRID_ATTR_ITALICS)
                   1131:                tty_putcode(tty, TTYC_SMSO);
                   1132:        if (changed & GRID_ATTR_UNDERSCORE)
                   1133:                tty_putcode(tty, TTYC_SMUL);
                   1134:        if (changed & GRID_ATTR_BLINK)
                   1135:                tty_putcode(tty, TTYC_BLINK);
                   1136:        if (changed & GRID_ATTR_REVERSE) {
                   1137:                if (tty_term_has(tty->term, TTYC_REV))
                   1138:                        tty_putcode(tty, TTYC_REV);
                   1139:                else if (tty_term_has(tty->term, TTYC_SMSO))
                   1140:                        tty_putcode(tty, TTYC_SMSO);
                   1141:        }
                   1142:        if (changed & GRID_ATTR_HIDDEN)
                   1143:                tty_putcode(tty, TTYC_INVIS);
                   1144:        if (changed & GRID_ATTR_CHARSET)
                   1145:                tty_putcode(tty, TTYC_SMACS);
                   1146:
                   1147:        /* Set foreground colour. */
                   1148:        if (fg != tc->fg ||
                   1149:            (gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
                   1150:                tty_attributes_fg(tty, gc);
                   1151:                tc->fg = fg;
1.8       nicm     1152:                tc->flags &= ~GRID_FLAG_FG256;
                   1153:                tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1       nicm     1154:        }
                   1155:
                   1156:        /* Set background colour. */
                   1157:        if (bg != tc->bg ||
                   1158:            (gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
                   1159:                tty_attributes_bg(tty, gc);
                   1160:                tc->bg = bg;
1.8       nicm     1161:                tc->flags &= ~GRID_FLAG_BG256;
                   1162:                tc->flags |= gc->flags & GRID_FLAG_BG256;
1.1       nicm     1163:        }
                   1164: }
                   1165:
                   1166: int
                   1167: tty_try_256(struct tty *tty, u_char colour, const char *type)
                   1168: {
                   1169:        char    s[32];
                   1170:
                   1171:        if (!(tty->term->flags & TERM_256COLOURS) &&
                   1172:            !(tty->term_flags & TERM_256COLOURS))
                   1173:                return (-1);
                   1174:
                   1175:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1176:        tty_puts(tty, s);
                   1177:        return (0);
                   1178: }
                   1179:
                   1180: int
                   1181: tty_try_88(struct tty *tty, u_char colour, const char *type)
                   1182: {
                   1183:        char    s[32];
                   1184:
                   1185:        if (!(tty->term->flags & TERM_88COLOURS) &&
                   1186:            !(tty->term_flags & TERM_88COLOURS))
                   1187:                return (-1);
                   1188:        colour = colour_256to88(colour);
                   1189:
                   1190:        xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
                   1191:        tty_puts(tty, s);
                   1192:        return (0);
                   1193: }
                   1194:
                   1195: void
                   1196: tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
                   1197: {
                   1198:        u_char  fg;
                   1199:
                   1200:        fg = gc->fg;
                   1201:        if (gc->flags & GRID_FLAG_FG256) {
                   1202:                if (tty_try_256(tty, fg, "38") == 0)
                   1203:                        return;
                   1204:                if (tty_try_88(tty, fg, "38") == 0)
                   1205:                        return;
                   1206:                fg = colour_256to16(fg);
                   1207:                if (fg & 8) {
                   1208:                        fg &= 7;
                   1209:                        tty_putcode(tty, TTYC_BOLD);
                   1210:                        tty->cell.attr |= GRID_ATTR_BRIGHT;
                   1211:                } else if (tty->cell.attr & GRID_ATTR_BRIGHT)
                   1212:                        tty_reset(tty);
                   1213:        }
                   1214:
                   1215:        if (fg == 8 &&
                   1216:            !(tty->term->flags & TERM_HASDEFAULTS) &&
                   1217:            !(tty->term_flags & TERM_HASDEFAULTS))
                   1218:                fg = 7;
                   1219:        if (fg == 8)
                   1220:                tty_puts(tty, "\033[39m");
                   1221:        else
                   1222:                tty_putcode1(tty, TTYC_SETAF, fg);
                   1223: }
                   1224:
                   1225: void
                   1226: tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
                   1227: {
                   1228:        u_char  bg;
                   1229:
                   1230:        bg = gc->bg;
                   1231:        if (gc->flags & GRID_FLAG_BG256) {
                   1232:                if (tty_try_256(tty, bg, "48") == 0)
                   1233:                        return;
                   1234:                if (tty_try_88(tty, bg, "48") == 0)
                   1235:                        return;
                   1236:                bg = colour_256to16(bg);
                   1237:                if (bg & 8)
                   1238:                        bg &= 7;
                   1239:        }
                   1240:
                   1241:        if (bg == 8 &&
                   1242:            !(tty->term->flags & TERM_HASDEFAULTS) &&
                   1243:            !(tty->term_flags & TERM_HASDEFAULTS))
                   1244:                bg = 0;
                   1245:        if (bg == 8)
                   1246:                tty_puts(tty, "\033[49m");
                   1247:        else
                   1248:                tty_putcode1(tty, TTYC_SETAB, bg);
                   1249: }