[BACK]Return to screen-write.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/screen-write.c, Revision 1.96

1.96    ! nicm        1: /* $OpenBSD: screen-write.c,v 1.95 2016/10/05 12:36:36 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.84      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.56      nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
1.87      nicm       26: static void    screen_write_initctx(struct screen_write_ctx *,
                     27:                    struct tty_ctx *);
                     28: static void    screen_write_save_last(struct screen_write_ctx *,
                     29:                    struct tty_ctx *);
1.92      nicm       30: static void    screen_write_flush(struct screen_write_ctx *);
1.87      nicm       31:
1.89      nicm       32: static int     screen_write_overwrite(struct screen_write_ctx *,
                     33:                    struct grid_cell *, u_int);
1.87      nicm       34: static int     screen_write_combine(struct screen_write_ctx *,
                     35:                    const struct utf8_data *);
1.1       nicm       36:
1.88      nicm       37: static const struct grid_cell screen_write_pad_cell = {
1.91      nicm       38:        GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
1.88      nicm       39: };
                     40:
1.92      nicm       41: #define screen_dirty_bit(s, x, y) (((y) * screen_size_x(s)) + (x))
                     42: #define screen_dirty_clear(s, sx, sy, ex, ey)                  \
                     43:        do {                                                    \
                     44:                if (s->dirty != NULL) {                         \
                     45:                        bit_nclear(s->dirty,                    \
                     46:                            screen_dirty_bit(s, sx, sy),        \
                     47:                            screen_dirty_bit(s, ex, ey));       \
                     48:                }                                               \
                     49:        } while (0)
                     50:
                     51: /* Initialize writing with a window. */
1.1       nicm       52: void
1.71      nicm       53: screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
                     54:     struct screen *s)
1.1       nicm       55: {
1.92      nicm       56:        u_int            size;
                     57:        char             tmp[16];
                     58:        const char      *cp = tmp;
                     59:
1.1       nicm       60:        ctx->wp = wp;
                     61:        if (wp != NULL && s == NULL)
                     62:                ctx->s = wp->screen;
                     63:        else
                     64:                ctx->s = s;
1.92      nicm       65:
                     66:        size = screen_size_x(ctx->s) * screen_size_y(ctx->s);
                     67:        if (ctx->s->dirtysize != size) {
                     68:                free(ctx->s->dirty);
                     69:                ctx->s->dirty = NULL;
                     70:                ctx->s->dirtysize = size;
                     71:        }
                     72:        ctx->dirty = 0;
                     73:
                     74:        ctx->cells = ctx->written = ctx->skipped = 0;
                     75:
                     76:        if (wp == NULL)
                     77:                cp = "no pane";
                     78:        else
                     79:                snprintf(tmp, sizeof tmp, "pane %%%u", wp->id);
                     80:        log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
                     81:            screen_size_y(ctx->s), cp);
1.1       nicm       82: }
                     83:
                     84: /* Finish writing. */
                     85: void
1.92      nicm       86: screen_write_stop(struct screen_write_ctx *ctx)
                     87: {
                     88:        screen_write_flush(ctx);
                     89:
                     90:        log_debug("%s: %u of %u written (dirty %u, skipped %u)", __func__,
                     91:            ctx->written, ctx->cells, ctx->cells - ctx->written, ctx->skipped);
                     92: }
                     93:
                     94: /* Flush outstanding cell writes. */
                     95: static void
                     96: screen_write_flush(struct screen_write_ctx *ctx)
1.1       nicm       97: {
1.92      nicm       98:        struct screen   *s = ctx->s;
                     99:        struct tty_ctx   ttyctx;
                    100:        u_int            x, y, offset, cx, cy, dirty;
                    101:        struct grid_cell gc;
                    102:
                    103:        if (ctx->dirty == 0)
                    104:                return;
                    105:        dirty = 0;
                    106:
                    107:        cx = s->cx;
                    108:        cy = s->cy;
                    109:
                    110:        offset = 0;
                    111:        for (y = 0; y < screen_size_y(s); y++) {
                    112:                for (x = 0; x < screen_size_x(s); x++) {
                    113:                        offset++;
                    114:                        if (!bit_test(s->dirty, offset - 1))
                    115:                                continue;
                    116:                        bit_clear(s->dirty, offset - 1);
                    117:
                    118:                        screen_write_cursormove(ctx, x, y);
                    119:                        grid_view_get_cell(s->grid, x, y, &gc);
                    120:
                    121:                        screen_write_initctx(ctx, &ttyctx);
                    122:                        ttyctx.cell = &gc;
                    123:                        tty_write(tty_cmd_cell, &ttyctx);
                    124:                        ctx->written++;
                    125:
                    126:                        if (++dirty == ctx->dirty)
                    127:                                break;
                    128:                }
                    129:                if (dirty == ctx->dirty)
                    130:                        break;
                    131:        }
                    132:
                    133:        s->cx = cx;
                    134:        s->cy = cy;
1.52      nicm      135: }
                    136:
                    137: /* Reset screen state. */
                    138: void
                    139: screen_write_reset(struct screen_write_ctx *ctx)
                    140: {
1.61      nicm      141:        struct screen   *s = ctx->s;
1.52      nicm      142:
1.61      nicm      143:        screen_reset_tabs(s);
                    144:        screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63      nicm      145:
1.67      nicm      146:        s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
1.82      nicm      147:        s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
1.52      nicm      148:
                    149:        screen_write_clearscreen(ctx);
                    150:        screen_write_cursormove(ctx, 0, 0);
1.1       nicm      151: }
                    152:
                    153: /* Write character. */
                    154: void
1.86      nicm      155: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69      nicm      156:     u_char ch)
1.1       nicm      157: {
1.86      nicm      158:        struct grid_cell        gc;
                    159:
                    160:        memcpy(&gc, gcp, sizeof gc);
                    161:
                    162:        utf8_set(&gc.data, ch);
                    163:        screen_write_cell(ctx, &gc);
1.1       nicm      164: }
                    165:
1.24      nicm      166: /* Calculate string length, with embedded formatting. */
1.71      nicm      167: size_t
1.75      nicm      168: screen_write_cstrlen(const char *fmt, ...)
1.24      nicm      169: {
                    170:        va_list ap;
                    171:        char   *msg, *msg2, *ptr, *ptr2;
                    172:        size_t  size;
                    173:
                    174:        va_start(ap, fmt);
                    175:        xvasprintf(&msg, fmt, ap);
                    176:        va_end(ap);
                    177:        msg2 = xmalloc(strlen(msg) + 1);
                    178:
                    179:        ptr = msg;
                    180:        ptr2 = msg2;
                    181:        while (*ptr != '\0') {
                    182:                if (ptr[0] == '#' && ptr[1] == '[') {
                    183:                        while (*ptr != ']' && *ptr != '\0')
                    184:                                ptr++;
                    185:                        if (*ptr == ']')
                    186:                                ptr++;
                    187:                        continue;
                    188:                }
                    189:                *ptr2++ = *ptr++;
                    190:        }
                    191:        *ptr2 = '\0';
                    192:
1.75      nicm      193:        size = screen_write_strlen("%s", msg2);
1.24      nicm      194:
1.56      nicm      195:        free(msg);
                    196:        free(msg2);
1.24      nicm      197:
                    198:        return (size);
                    199: }
                    200:
1.2       nicm      201: /* Calculate string length. */
1.71      nicm      202: size_t
1.75      nicm      203: screen_write_strlen(const char *fmt, ...)
1.2       nicm      204: {
1.36      nicm      205:        va_list                 ap;
                    206:        char                   *msg;
1.76      nicm      207:        struct utf8_data        ud;
1.36      nicm      208:        u_char                 *ptr;
                    209:        size_t                  left, size = 0;
1.79      nicm      210:        enum utf8_state         more;
1.2       nicm      211:
                    212:        va_start(ap, fmt);
                    213:        xvasprintf(&msg, fmt, ap);
                    214:        va_end(ap);
                    215:
                    216:        ptr = msg;
                    217:        while (*ptr != '\0') {
1.79      nicm      218:                if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36      nicm      219:                        ptr++;
1.2       nicm      220:
                    221:                        left = strlen(ptr);
1.77      nicm      222:                        if (left < (size_t)ud.size - 1)
1.36      nicm      223:                                break;
1.79      nicm      224:                        while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2       nicm      225:                                ptr++;
1.36      nicm      226:                        ptr++;
                    227:
1.79      nicm      228:                        if (more == UTF8_DONE)
1.78      nicm      229:                                size += ud.width;
1.2       nicm      230:                } else {
1.75      nicm      231:                        if (*ptr > 0x1f && *ptr < 0x7f)
                    232:                                size++;
1.2       nicm      233:                        ptr++;
                    234:                }
1.7       ray       235:        }
1.2       nicm      236:
1.56      nicm      237:        free(msg);
1.2       nicm      238:        return (size);
                    239: }
                    240:
1.3       nicm      241: /* Write simple string (no UTF-8 or maximum length). */
1.71      nicm      242: void
1.86      nicm      243: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71      nicm      244:     const char *fmt, ...)
1.1       nicm      245: {
                    246:        va_list ap;
                    247:
                    248:        va_start(ap, fmt);
1.86      nicm      249:        screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2       nicm      250:        va_end(ap);
                    251: }
                    252:
                    253: /* Write string with length limit (-1 for unlimited). */
1.71      nicm      254: void
                    255: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      256:     const struct grid_cell *gcp, const char *fmt, ...)
1.2       nicm      257: {
                    258:        va_list ap;
                    259:
                    260:        va_start(ap, fmt);
1.86      nicm      261:        screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2       nicm      262:        va_end(ap);
                    263: }
                    264:
                    265: void
1.3       nicm      266: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      267:     const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2       nicm      268: {
1.86      nicm      269:        struct grid_cell        gc;
                    270:        struct utf8_data       *ud = &gc.data;
1.36      nicm      271:        char                   *msg;
                    272:        u_char                 *ptr;
                    273:        size_t                  left, size = 0;
1.79      nicm      274:        enum utf8_state         more;
1.2       nicm      275:
1.86      nicm      276:        memcpy(&gc, gcp, sizeof gc);
1.1       nicm      277:        xvasprintf(&msg, fmt, ap);
                    278:
1.2       nicm      279:        ptr = msg;
                    280:        while (*ptr != '\0') {
1.86      nicm      281:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      282:                        ptr++;
1.2       nicm      283:
                    284:                        left = strlen(ptr);
1.86      nicm      285:                        if (left < (size_t)ud->size - 1)
1.36      nicm      286:                                break;
1.86      nicm      287:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2       nicm      288:                                ptr++;
1.36      nicm      289:                        ptr++;
1.7       ray       290:
1.86      nicm      291:                        if (more != UTF8_DONE)
                    292:                                continue;
                    293:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    294:                                while (size < (size_t)maxlen) {
                    295:                                        screen_write_putc(ctx, &gc, ' ');
                    296:                                        size++;
1.2       nicm      297:                                }
1.86      nicm      298:                                break;
1.2       nicm      299:                        }
1.86      nicm      300:                        size += ud->width;
                    301:                        screen_write_cell(ctx, &gc);
1.2       nicm      302:                } else {
1.86      nicm      303:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2       nicm      304:                                break;
                    305:
1.57      nicm      306:                        if (*ptr == '\001')
1.86      nicm      307:                                gc.attr ^= GRID_ATTR_CHARSET;
1.75      nicm      308:                        else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57      nicm      309:                                size++;
1.86      nicm      310:                                screen_write_putc(ctx, &gc, *ptr);
1.57      nicm      311:                        }
1.2       nicm      312:                        ptr++;
                    313:                }
                    314:        }
1.1       nicm      315:
1.56      nicm      316:        free(msg);
1.24      nicm      317: }
                    318:
                    319: /* Write string, similar to nputs, but with embedded formatting (#[]). */
1.71      nicm      320: void
1.75      nicm      321: screen_write_cnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      322:     const struct grid_cell *gcp, const char *fmt, ...)
1.24      nicm      323: {
1.86      nicm      324:        struct grid_cell         gc;
                    325:        struct utf8_data        *ud = &gc.data;
1.24      nicm      326:        va_list                  ap;
                    327:        char                    *msg;
1.36      nicm      328:        u_char                  *ptr, *last;
1.24      nicm      329:        size_t                   left, size = 0;
1.79      nicm      330:        enum utf8_state          more;
1.24      nicm      331:
1.86      nicm      332:        memcpy(&gc, gcp, sizeof gc);
                    333:
1.24      nicm      334:        va_start(ap, fmt);
                    335:        xvasprintf(&msg, fmt, ap);
                    336:        va_end(ap);
                    337:
                    338:        ptr = msg;
                    339:        while (*ptr != '\0') {
                    340:                if (ptr[0] == '#' && ptr[1] == '[') {
                    341:                        ptr += 2;
                    342:                        last = ptr + strcspn(ptr, "]");
                    343:                        if (*last == '\0') {
                    344:                                /* No ]. Not much point in doing anything. */
                    345:                                break;
                    346:                        }
                    347:                        *last = '\0';
                    348:
1.86      nicm      349:                        style_parse(gcp, &gc, ptr);
1.24      nicm      350:                        ptr = last + 1;
                    351:                        continue;
                    352:                }
                    353:
1.86      nicm      354:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      355:                        ptr++;
1.24      nicm      356:
                    357:                        left = strlen(ptr);
1.86      nicm      358:                        if (left < (size_t)ud->size - 1)
1.36      nicm      359:                                break;
1.86      nicm      360:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.24      nicm      361:                                ptr++;
1.36      nicm      362:                        ptr++;
1.24      nicm      363:
1.86      nicm      364:                        if (more != UTF8_DONE)
                    365:                                continue;
                    366:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    367:                                while (size < (size_t)maxlen) {
                    368:                                        screen_write_putc(ctx, &gc, ' ');
                    369:                                        size++;
1.24      nicm      370:                                }
1.86      nicm      371:                                break;
1.24      nicm      372:                        }
1.86      nicm      373:                        size += ud->width;
                    374:                        screen_write_cell(ctx, &gc);
1.24      nicm      375:                } else {
1.86      nicm      376:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.24      nicm      377:                                break;
                    378:
1.75      nicm      379:                        if (*ptr > 0x1f && *ptr < 0x7f) {
                    380:                                size++;
1.86      nicm      381:                                screen_write_putc(ctx, &gc, *ptr);
1.75      nicm      382:                        }
1.24      nicm      383:                        ptr++;
                    384:                }
                    385:        }
                    386:
1.56      nicm      387:        free(msg);
1.1       nicm      388: }
                    389:
                    390: /* Copy from another screen. */
                    391: void
1.95      nicm      392: screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
                    393:     u_int py, u_int nx, u_int ny)
1.1       nicm      394: {
                    395:        struct screen           *s = ctx->s;
                    396:        struct grid             *gd = src->grid;
1.77      nicm      397:        struct grid_cell         gc;
1.96    ! nicm      398:        u_int                    xx, yy, cx, cy;
1.1       nicm      399:
                    400:        cx = s->cx;
                    401:        cy = s->cy;
1.96    ! nicm      402:
1.1       nicm      403:        for (yy = py; yy < py + ny; yy++) {
1.96    ! nicm      404:                for (xx = px; xx < px + nx; xx++) {
        !           405:                        grid_get_cell(gd, xx, yy, &gc);
        !           406:                        screen_write_cell(ctx, &gc);
        !           407:                }
        !           408:
1.1       nicm      409:                cy++;
                    410:                screen_write_cursormove(ctx, cx, cy);
                    411:        }
                    412: }
                    413:
1.17      nicm      414: /* Set up context for TTY command. */
1.87      nicm      415: static void
                    416: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1       nicm      417: {
1.87      nicm      418:        struct screen   *s = ctx->s;
1.1       nicm      419:
1.17      nicm      420:        ttyctx->wp = ctx->wp;
1.1       nicm      421:
1.17      nicm      422:        ttyctx->ocx = s->cx;
                    423:        ttyctx->ocy = s->cy;
                    424:
                    425:        ttyctx->orlower = s->rlower;
                    426:        ttyctx->orupper = s->rupper;
1.87      nicm      427: }
1.31      nicm      428:
1.87      nicm      429: /* Save last cell on screen. */
                    430: static void
                    431: screen_write_save_last(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
                    432: {
                    433:        struct screen           *s = ctx->s;
                    434:        struct grid             *gd = s->grid;
                    435:        struct grid_cell         gc;
                    436:        u_int                    xx;
1.31      nicm      437:
1.77      nicm      438:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.38      nicm      439:        for (xx = 1; xx <= screen_size_x(s); xx++) {
1.77      nicm      440:                grid_view_get_cell(gd, screen_size_x(s) - xx, s->cy, &gc);
                    441:                if (~gc.flags & GRID_FLAG_PADDING)
1.31      nicm      442:                        break;
                    443:        }
1.77      nicm      444:        memcpy(&ttyctx->last_cell, &gc, sizeof ttyctx->last_cell);
1.1       nicm      445: }
                    446:
1.61      nicm      447: /* Set a mode. */
                    448: void
                    449: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
                    450: {
                    451:        struct screen   *s = ctx->s;
                    452:
                    453:        s->mode |= mode;
                    454: }
                    455:
                    456: /* Clear a mode. */
                    457: void
                    458: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
                    459: {
                    460:        struct screen   *s = ctx->s;
                    461:
                    462:        s->mode &= ~mode;
                    463: }
                    464:
1.1       nicm      465: /* Cursor up by ny. */
                    466: void
                    467: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
                    468: {
                    469:        struct screen   *s = ctx->s;
                    470:
                    471:        if (ny == 0)
                    472:                ny = 1;
                    473:
1.12      nicm      474:        if (s->cy < s->rupper) {
                    475:                /* Above region. */
                    476:                if (ny > s->cy)
                    477:                        ny = s->cy;
                    478:        } else {
                    479:                /* Below region. */
                    480:                if (ny > s->cy - s->rupper)
                    481:                        ny = s->cy - s->rupper;
                    482:        }
1.66      nicm      483:        if (s->cx == screen_size_x(s))
                    484:            s->cx--;
1.1       nicm      485:        if (ny == 0)
                    486:                return;
                    487:
                    488:        s->cy -= ny;
                    489: }
                    490:
                    491: /* Cursor down by ny. */
                    492: void
                    493: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
                    494: {
                    495:        struct screen   *s = ctx->s;
                    496:
                    497:        if (ny == 0)
                    498:                ny = 1;
                    499:
1.12      nicm      500:        if (s->cy > s->rlower) {
                    501:                /* Below region. */
                    502:                if (ny > screen_size_y(s) - 1 - s->cy)
                    503:                        ny = screen_size_y(s) - 1 - s->cy;
                    504:        } else {
                    505:                /* Above region. */
                    506:                if (ny > s->rlower - s->cy)
                    507:                        ny = s->rlower - s->cy;
                    508:        }
1.66      nicm      509:        if (s->cx == screen_size_x(s))
                    510:            s->cx--;
1.1       nicm      511:        if (ny == 0)
                    512:                return;
                    513:
                    514:        s->cy += ny;
                    515: }
                    516:
                    517: /* Cursor right by nx.  */
                    518: void
                    519: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
                    520: {
                    521:        struct screen   *s = ctx->s;
                    522:
                    523:        if (nx == 0)
                    524:                nx = 1;
                    525:
                    526:        if (nx > screen_size_x(s) - 1 - s->cx)
                    527:                nx = screen_size_x(s) - 1 - s->cx;
                    528:        if (nx == 0)
                    529:                return;
                    530:
                    531:        s->cx += nx;
                    532: }
                    533:
                    534: /* Cursor left by nx. */
                    535: void
                    536: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
                    537: {
                    538:        struct screen   *s = ctx->s;
                    539:
                    540:        if (nx == 0)
                    541:                nx = 1;
                    542:
                    543:        if (nx > s->cx)
                    544:                nx = s->cx;
                    545:        if (nx == 0)
                    546:                return;
                    547:
                    548:        s->cx -= nx;
1.5       nicm      549: }
                    550:
1.29      nicm      551: /* Backspace; cursor left unless at start of wrapped line when can move up. */
                    552: void
                    553: screen_write_backspace(struct screen_write_ctx *ctx)
                    554: {
                    555:        struct screen           *s = ctx->s;
                    556:        struct grid_line        *gl;
                    557:
                    558:        if (s->cx == 0) {
                    559:                if (s->cy == 0)
                    560:                        return;
                    561:                gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
                    562:                if (gl->flags & GRID_LINE_WRAPPED) {
                    563:                        s->cy--;
                    564:                        s->cx = screen_size_x(s) - 1;
                    565:                }
                    566:        } else
                    567:                s->cx--;
                    568: }
                    569:
1.5       nicm      570: /* VT100 alignment test. */
                    571: void
                    572: screen_write_alignmenttest(struct screen_write_ctx *ctx)
                    573: {
                    574:        struct screen           *s = ctx->s;
1.17      nicm      575:        struct tty_ctx           ttyctx;
1.5       nicm      576:        struct grid_cell         gc;
                    577:        u_int                    xx, yy;
1.92      nicm      578:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.5       nicm      579:
1.87      nicm      580:        screen_write_initctx(ctx, &ttyctx);
1.17      nicm      581:
1.92      nicm      582:        screen_dirty_clear(s, 0, 0, sx - 1, sy  - 1);
                    583:
1.5       nicm      584:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.77      nicm      585:        utf8_set(&gc.data, 'E');
1.7       ray       586:
1.5       nicm      587:        for (yy = 0; yy < screen_size_y(s); yy++) {
                    588:                for (xx = 0; xx < screen_size_x(s); xx++)
                    589:                        grid_view_set_cell(s->grid, xx, yy, &gc);
                    590:        }
1.7       ray       591:
1.5       nicm      592:        s->cx = 0;
                    593:        s->cy = 0;
                    594:
                    595:        s->rupper = 0;
                    596:        s->rlower = screen_size_y(s) - 1;
                    597:
1.17      nicm      598:        tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1       nicm      599: }
                    600:
                    601: /* Insert nx characters. */
                    602: void
                    603: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx)
                    604: {
                    605:        struct screen   *s = ctx->s;
1.17      nicm      606:        struct tty_ctx   ttyctx;
1.1       nicm      607:
                    608:        if (nx == 0)
                    609:                nx = 1;
                    610:
1.9       nicm      611:        if (nx > screen_size_x(s) - s->cx)
                    612:                nx = screen_size_x(s) - s->cx;
1.1       nicm      613:        if (nx == 0)
                    614:                return;
                    615:
1.92      nicm      616:        screen_write_flush(ctx);
1.87      nicm      617:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      618:
                    619:        if (s->cx <= screen_size_x(s) - 1)
                    620:                grid_view_insert_cells(s->grid, s->cx, s->cy, nx);
                    621:
1.17      nicm      622:        ttyctx.num = nx;
                    623:        tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1       nicm      624: }
                    625:
                    626: /* Delete nx characters. */
                    627: void
                    628: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx)
                    629: {
                    630:        struct screen   *s = ctx->s;
1.17      nicm      631:        struct tty_ctx   ttyctx;
1.1       nicm      632:
                    633:        if (nx == 0)
                    634:                nx = 1;
                    635:
1.9       nicm      636:        if (nx > screen_size_x(s) - s->cx)
                    637:                nx = screen_size_x(s) - s->cx;
1.1       nicm      638:        if (nx == 0)
                    639:                return;
                    640:
1.92      nicm      641:        screen_write_flush(ctx);
1.87      nicm      642:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      643:
                    644:        if (s->cx <= screen_size_x(s) - 1)
                    645:                grid_view_delete_cells(s->grid, s->cx, s->cy, nx);
                    646:
1.17      nicm      647:        ttyctx.num = nx;
                    648:        tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59      nicm      649: }
                    650:
                    651: /* Clear nx characters. */
                    652: void
                    653: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx)
                    654: {
                    655:        struct screen   *s = ctx->s;
                    656:        struct tty_ctx   ttyctx;
                    657:
                    658:        if (nx == 0)
                    659:                nx = 1;
                    660:
                    661:        if (nx > screen_size_x(s) - s->cx)
                    662:                nx = screen_size_x(s) - s->cx;
                    663:        if (nx == 0)
                    664:                return;
                    665:
1.87      nicm      666:        screen_write_initctx(ctx, &ttyctx);
1.59      nicm      667:
1.92      nicm      668:        if (s->cx <= screen_size_x(s) - 1) {
                    669:                screen_dirty_clear(s, s->cx, s->cy, s->cx + nx - 1, s->cy);
1.59      nicm      670:                grid_view_clear(s->grid, s->cx, s->cy, nx, 1);
1.92      nicm      671:        } else
                    672:                return;
1.59      nicm      673:
                    674:        ttyctx.num = nx;
                    675:        tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1       nicm      676: }
                    677:
                    678: /* Insert ny lines. */
                    679: void
                    680: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny)
                    681: {
                    682:        struct screen   *s = ctx->s;
1.17      nicm      683:        struct tty_ctx   ttyctx;
1.1       nicm      684:
                    685:        if (ny == 0)
                    686:                ny = 1;
                    687:
1.11      nicm      688:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    689:                if (ny > screen_size_y(s) - s->cy)
                    690:                        ny = screen_size_y(s) - s->cy;
                    691:                if (ny == 0)
                    692:                        return;
                    693:
1.92      nicm      694:                screen_write_flush(ctx);
1.87      nicm      695:                screen_write_initctx(ctx, &ttyctx);
1.11      nicm      696:
                    697:                grid_view_insert_lines(s->grid, s->cy, ny);
                    698:
1.17      nicm      699:                ttyctx.num = ny;
                    700:                tty_write(tty_cmd_insertline, &ttyctx);
1.11      nicm      701:                return;
                    702:        }
                    703:
                    704:        if (ny > s->rlower + 1 - s->cy)
                    705:                ny = s->rlower + 1 - s->cy;
1.1       nicm      706:        if (ny == 0)
                    707:                return;
1.41      nicm      708:
1.92      nicm      709:        screen_write_flush(ctx);
1.87      nicm      710:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      711:
                    712:        if (s->cy < s->rupper || s->cy > s->rlower)
                    713:                grid_view_insert_lines(s->grid, s->cy, ny);
1.10      nicm      714:        else
                    715:                grid_view_insert_lines_region(s->grid, s->rlower, s->cy, ny);
1.1       nicm      716:
1.17      nicm      717:        ttyctx.num = ny;
                    718:        tty_write(tty_cmd_insertline, &ttyctx);
1.1       nicm      719: }
                    720:
                    721: /* Delete ny lines. */
                    722: void
                    723: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny)
                    724: {
                    725:        struct screen   *s = ctx->s;
1.17      nicm      726:        struct tty_ctx   ttyctx;
1.1       nicm      727:
                    728:        if (ny == 0)
                    729:                ny = 1;
                    730:
1.11      nicm      731:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    732:                if (ny > screen_size_y(s) - s->cy)
                    733:                        ny = screen_size_y(s) - s->cy;
                    734:                if (ny == 0)
                    735:                        return;
                    736:
1.92      nicm      737:                screen_write_flush(ctx);
1.87      nicm      738:                screen_write_initctx(ctx, &ttyctx);
1.11      nicm      739:
                    740:                grid_view_delete_lines(s->grid, s->cy, ny);
                    741:
1.17      nicm      742:                ttyctx.num = ny;
                    743:                tty_write(tty_cmd_deleteline, &ttyctx);
1.11      nicm      744:                return;
                    745:        }
1.41      nicm      746:
1.11      nicm      747:        if (ny > s->rlower + 1 - s->cy)
                    748:                ny = s->rlower + 1 - s->cy;
1.1       nicm      749:        if (ny == 0)
                    750:                return;
                    751:
1.92      nicm      752:        screen_write_flush(ctx);
1.87      nicm      753:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      754:
                    755:        if (s->cy < s->rupper || s->cy > s->rlower)
                    756:                grid_view_delete_lines(s->grid, s->cy, ny);
1.10      nicm      757:        else
                    758:                grid_view_delete_lines_region(s->grid, s->rlower, s->cy, ny);
1.1       nicm      759:
1.17      nicm      760:        ttyctx.num = ny;
                    761:        tty_write(tty_cmd_deleteline, &ttyctx);
1.1       nicm      762: }
                    763:
                    764: /* Clear line at cursor. */
                    765: void
                    766: screen_write_clearline(struct screen_write_ctx *ctx)
                    767: {
1.92      nicm      768:        struct screen           *s = ctx->s;
                    769:        struct grid_line        *gl;
                    770:        struct tty_ctx           ttyctx;
                    771:        u_int                    sx = screen_size_x(s);
1.1       nicm      772:
1.87      nicm      773:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      774:
1.92      nicm      775:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                    776:        if (gl->cellsize != 0) {
                    777:                screen_dirty_clear(s, 0, s->cy, sx - 1, s->cy);
                    778:                grid_view_clear(s->grid, 0, s->cy, sx, 1);
                    779:        } else
                    780:                return;
1.1       nicm      781:
1.17      nicm      782:        tty_write(tty_cmd_clearline, &ttyctx);
1.1       nicm      783: }
                    784:
                    785: /* Clear to end of line from cursor. */
                    786: void
                    787: screen_write_clearendofline(struct screen_write_ctx *ctx)
                    788: {
1.92      nicm      789:        struct screen           *s = ctx->s;
                    790:        struct grid_line        *gl;
                    791:        struct tty_ctx           ttyctx;
                    792:        u_int                    sx = screen_size_x(s);
1.1       nicm      793:
1.87      nicm      794:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      795:
1.92      nicm      796:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                    797:        if (s->cx <= sx - 1 && s->cx < gl->cellsize) {
                    798:                screen_dirty_clear(s, s->cx, s->cy, sx - 1, s->cy);
1.1       nicm      799:                grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
1.92      nicm      800:        } else
                    801:                return;
1.1       nicm      802:
1.41      nicm      803:        tty_write(tty_cmd_clearendofline, &ttyctx);
1.1       nicm      804: }
                    805:
                    806: /* Clear to start of line from cursor. */
                    807: void
                    808: screen_write_clearstartofline(struct screen_write_ctx *ctx)
                    809: {
                    810:        struct screen   *s = ctx->s;
1.17      nicm      811:        struct tty_ctx   ttyctx;
1.92      nicm      812:        u_int            sx = screen_size_x(s);
1.1       nicm      813:
1.87      nicm      814:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      815:
1.92      nicm      816:        if (s->cx > sx - 1) {
                    817:                screen_dirty_clear(s, 0, s->cy, sx - 1, s->cy);
1.1       nicm      818:                grid_view_clear(s->grid, 0, s->cy, sx, 1);
1.92      nicm      819:        } else {
                    820:                screen_dirty_clear(s, 0, s->cy, s->cx, s->cy);
1.1       nicm      821:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
1.92      nicm      822:        }
1.1       nicm      823:
1.17      nicm      824:        tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1       nicm      825: }
                    826:
                    827: /* Move cursor to px,py.  */
                    828: void
                    829: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
                    830: {
                    831:        struct screen   *s = ctx->s;
                    832:
                    833:        if (px > screen_size_x(s) - 1)
                    834:                px = screen_size_x(s) - 1;
                    835:        if (py > screen_size_y(s) - 1)
                    836:                py = screen_size_y(s) - 1;
                    837:
                    838:        s->cx = px;
                    839:        s->cy = py;
                    840: }
                    841:
                    842: /* Reverse index (up with scroll).  */
                    843: void
                    844: screen_write_reverseindex(struct screen_write_ctx *ctx)
                    845: {
                    846:        struct screen   *s = ctx->s;
1.17      nicm      847:        struct tty_ctx   ttyctx;
1.1       nicm      848:
1.87      nicm      849:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      850:
1.92      nicm      851:        if (s->cy == s->rupper) {
                    852:                screen_write_flush(ctx);
1.1       nicm      853:                grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
1.92      nicm      854:        } else if (s->cy > 0)
1.1       nicm      855:                s->cy--;
                    856:
1.17      nicm      857:        tty_write(tty_cmd_reverseindex, &ttyctx);
1.1       nicm      858: }
                    859:
                    860: /* Set scroll region. */
                    861: void
1.83      nicm      862: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
                    863:     u_int rlower)
1.1       nicm      864: {
                    865:        struct screen   *s = ctx->s;
                    866:
                    867:        if (rupper > screen_size_y(s) - 1)
                    868:                rupper = screen_size_y(s) - 1;
                    869:        if (rlower > screen_size_y(s) - 1)
                    870:                rlower = screen_size_y(s) - 1;
1.13      nicm      871:        if (rupper >= rlower)   /* cannot be one line */
1.1       nicm      872:                return;
                    873:
                    874:        /* Cursor moves to top-left. */
                    875:        s->cx = 0;
                    876:        s->cy = 0;
                    877:
                    878:        s->rupper = rupper;
                    879:        s->rlower = rlower;
                    880: }
                    881:
1.34      nicm      882: /* Line feed. */
1.1       nicm      883: void
1.34      nicm      884: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
1.1       nicm      885: {
1.20      nicm      886:        struct screen           *s = ctx->s;
                    887:        struct grid_line        *gl;
1.34      nicm      888:        struct tty_ctx           ttyctx;
1.92      nicm      889:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.34      nicm      890:
1.87      nicm      891:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      892:
1.20      nicm      893:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                    894:        if (wrapped)
                    895:                gl->flags |= GRID_LINE_WRAPPED;
1.73      nicm      896:        else
                    897:                gl->flags &= ~GRID_LINE_WRAPPED;
1.20      nicm      898:
1.92      nicm      899:        if (s->cy == s->rlower) {
                    900:                screen_dirty_clear(s, 0, s->rupper, sx - 1, s->rupper);
                    901:                screen_write_flush(ctx);
1.1       nicm      902:                grid_view_scroll_region_up(s->grid, s->rupper, s->rlower);
1.92      nicm      903:        } else if (s->cy < sy - 1)
1.1       nicm      904:                s->cy++;
                    905:
1.34      nicm      906:        ttyctx.num = wrapped;
1.41      nicm      907:        tty_write(tty_cmd_linefeed, &ttyctx);
1.1       nicm      908: }
                    909:
                    910: /* Carriage return (cursor to start of line). */
                    911: void
                    912: screen_write_carriagereturn(struct screen_write_ctx *ctx)
                    913: {
                    914:        struct screen   *s = ctx->s;
                    915:
                    916:        s->cx = 0;
                    917: }
                    918:
                    919: /* Clear to end of screen from cursor. */
                    920: void
                    921: screen_write_clearendofscreen(struct screen_write_ctx *ctx)
                    922: {
                    923:        struct screen   *s = ctx->s;
1.17      nicm      924:        struct tty_ctx   ttyctx;
1.92      nicm      925:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm      926:
1.87      nicm      927:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      928:
1.46      nicm      929:        /* Scroll into history if it is enabled and clearing entire screen. */
1.92      nicm      930:        if (s->cy == 0 && s->grid->flags & GRID_HISTORY) {
                    931:                screen_dirty_clear(s, 0, 0, sx - 1, sy  - 1);
1.46      nicm      932:                grid_view_clear_history(s->grid);
1.92      nicm      933:        } else {
                    934:                if (s->cx <= sx - 1) {
                    935:                        screen_dirty_clear(s, s->cx, s->cy, sx - 1, s->cy);
1.46      nicm      936:                        grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1);
1.92      nicm      937:                }
                    938:                screen_dirty_clear(s, 0, s->cy + 1, sx - 1, sy - 1);
1.46      nicm      939:                grid_view_clear(s->grid, 0, s->cy + 1, sx, sy - (s->cy + 1));
                    940:        }
1.1       nicm      941:
1.17      nicm      942:        tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1       nicm      943: }
                    944:
                    945: /* Clear to start of screen. */
                    946: void
                    947: screen_write_clearstartofscreen(struct screen_write_ctx *ctx)
                    948: {
                    949:        struct screen   *s = ctx->s;
1.17      nicm      950:        struct tty_ctx   ttyctx;
1.92      nicm      951:        u_int            sx = screen_size_x(s);
1.1       nicm      952:
1.87      nicm      953:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      954:
1.92      nicm      955:        if (s->cy > 0) {
                    956:                screen_dirty_clear(s, 0, 0, sx - 1, s->cy);
1.4       nicm      957:                grid_view_clear(s->grid, 0, 0, sx, s->cy);
1.92      nicm      958:        }
                    959:        if (s->cx > sx - 1) {
                    960:                screen_dirty_clear(s, 0, s->cy, sx - 1, s->cy);
1.1       nicm      961:                grid_view_clear(s->grid, 0, s->cy, sx, 1);
1.92      nicm      962:        } else {
                    963:                screen_dirty_clear(s, 0, s->cy, s->cx, s->cy);
1.4       nicm      964:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1);
1.92      nicm      965:        }
1.1       nicm      966:
1.17      nicm      967:        tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1       nicm      968: }
                    969:
                    970: /* Clear entire screen. */
                    971: void
                    972: screen_write_clearscreen(struct screen_write_ctx *ctx)
                    973: {
                    974:        struct screen   *s = ctx->s;
1.17      nicm      975:        struct tty_ctx   ttyctx;
1.92      nicm      976:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm      977:
1.87      nicm      978:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      979:
1.92      nicm      980:        screen_dirty_clear(s, 0, 0, sx - 1, sy  - 1);
                    981:
1.46      nicm      982:        /* Scroll into history if it is enabled. */
                    983:        if (s->grid->flags & GRID_HISTORY)
                    984:                grid_view_clear_history(s->grid);
1.83      nicm      985:        else
                    986:                grid_view_clear(s->grid, 0, 0, sx, sy);
1.1       nicm      987:
1.17      nicm      988:        tty_write(tty_cmd_clearscreen, &ttyctx);
1.51      nicm      989: }
                    990:
                    991: /* Clear entire history. */
                    992: void
                    993: screen_write_clearhistory(struct screen_write_ctx *ctx)
                    994: {
                    995:        struct screen   *s = ctx->s;
                    996:        struct grid     *gd = s->grid;
                    997:
                    998:        grid_move_lines(gd, 0, gd->hsize, gd->sy);
1.93      nicm      999:        gd->hscrolled = gd->hsize = 0;
1.1       nicm     1000: }
                   1001:
                   1002: /* Write cell data. */
                   1003: void
1.60      nicm     1004: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1       nicm     1005: {
                   1006:        struct screen           *s = ctx->s;
                   1007:        struct grid             *gd = s->grid;
1.15      nicm     1008:        struct tty_ctx           ttyctx;
1.64      nicm     1009:        u_int                    width, xx, last;
1.92      nicm     1010:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
                   1011:        struct grid_line        *gl;
1.89      nicm     1012:        struct grid_cell         tmp_gc, now_gc;
1.92      nicm     1013:        struct grid_cell_entry  *gce;
                   1014:        int                      insert, skip, selected, wrapped = 0;
                   1015:
                   1016:        ctx->cells++;
1.1       nicm     1017:
                   1018:        /* Ignore padding. */
                   1019:        if (gc->flags & GRID_FLAG_PADDING)
                   1020:                return;
1.77      nicm     1021:        width = gc->data.width;
1.1       nicm     1022:
1.32      nicm     1023:        /*
                   1024:         * If this is a wide character and there is no room on the screen, for
                   1025:         * the entire character, don't print it.
                   1026:         */
1.92      nicm     1027:        if (!(s->mode & MODE_WRAP) && (width > 1 &&
                   1028:            (width > sx || (s->cx != sx && s->cx > sx - width))))
1.32      nicm     1029:                return;
                   1030:
1.35      nicm     1031:        /*
                   1032:         * If the width is zero, combine onto the previous character, if
1.41      nicm     1033:         * there is space.
1.35      nicm     1034:         */
1.1       nicm     1035:        if (width == 0) {
1.77      nicm     1036:                if (screen_write_combine(ctx, &gc->data) == 0) {
1.87      nicm     1037:                        screen_write_initctx(ctx, &ttyctx);
1.35      nicm     1038:                        tty_write(tty_cmd_utf8character, &ttyctx);
1.1       nicm     1039:                }
                   1040:                return;
                   1041:        }
                   1042:
1.89      nicm     1043:        /* Initialise the redraw context. */
1.87      nicm     1044:        screen_write_initctx(ctx, &ttyctx);
1.55      nicm     1045:
1.6       nicm     1046:        /* If in insert mode, make space for the cells. */
1.92      nicm     1047:        if ((s->mode & MODE_INSERT) && s->cx <= sx - width) {
                   1048:                xx = sx - s->cx - width;
1.6       nicm     1049:                grid_move_cells(s->grid, s->cx + width, s->cx, s->cy, xx);
                   1050:                insert = 1;
1.64      nicm     1051:        } else
                   1052:                insert = 0;
1.89      nicm     1053:        skip = !insert;
1.6       nicm     1054:
1.20      nicm     1055:        /* Check this will fit on the current line and wrap if not. */
1.92      nicm     1056:        if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
                   1057:                screen_write_flush(ctx);
                   1058:                screen_write_save_last(ctx, &ttyctx);
1.34      nicm     1059:                screen_write_linefeed(ctx, 1);
1.30      nicm     1060:                s->cx = 0;      /* carriage return */
1.89      nicm     1061:                skip = 0;
1.92      nicm     1062:                wrapped = 1;
1.1       nicm     1063:        }
                   1064:
1.64      nicm     1065:        /* Sanity check cursor position. */
1.92      nicm     1066:        if (s->cx > sx - width || s->cy > sy - 1)
1.1       nicm     1067:                return;
                   1068:
                   1069:        /* Handle overwriting of UTF-8 characters. */
1.92      nicm     1070:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                   1071:        if (gl->flags & GRID_LINE_EXTENDED) {
                   1072:                grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
                   1073:                if (screen_write_overwrite(ctx, &now_gc, width))
                   1074:                        skip = 0;
                   1075:        }
1.1       nicm     1076:
                   1077:        /*
                   1078:         * If the new character is UTF-8 wide, fill in padding cells. Have
                   1079:         * already ensured there is enough room.
                   1080:         */
1.89      nicm     1081:        for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.88      nicm     1082:                grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89      nicm     1083:                skip = 0;
                   1084:        }
                   1085:
                   1086:        /* If no change, do not draw. */
1.92      nicm     1087:        if (skip) {
                   1088:                if (s->cx >= gl->cellsize)
                   1089:                        skip = grid_cells_equal(gc, &grid_default_cell);
                   1090:                else {
                   1091:                        gce = &gl->celldata[s->cx];
                   1092:                        if (gce->flags & GRID_FLAG_EXTENDED)
                   1093:                                skip = 0;
1.95      nicm     1094:                        else if (gc->flags != gce->flags)
1.92      nicm     1095:                                skip = 0;
                   1096:                        else if (gc->attr != gce->data.attr)
                   1097:                                skip = 0;
                   1098:                        else if (gc->fg != gce->data.fg)
                   1099:                                skip = 0;
                   1100:                        else if (gc->bg != gce->data.bg)
                   1101:                                skip = 0;
1.95      nicm     1102:                        else if (gc->data.width != 1)
                   1103:                                skip = 0;
                   1104:                        else if (gce->data.data != gc->data.data[0])
1.92      nicm     1105:                                skip = 0;
                   1106:                }
                   1107:        }
1.1       nicm     1108:
1.90      nicm     1109:        /* Update the selection the flag and set the cell. */
                   1110:        selected = screen_check_selection(s, s->cx, s->cy);
                   1111:        if (selected && ~gc->flags & GRID_FLAG_SELECTED) {
                   1112:                skip = 0;
                   1113:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1114:                tmp_gc.flags |= GRID_FLAG_SELECTED;
                   1115:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1116:        } else if (!selected && gc->flags & GRID_FLAG_SELECTED) {
                   1117:                skip = 0;
                   1118:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1119:                tmp_gc.flags &= ~GRID_FLAG_SELECTED;
                   1120:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1121:        } else if (!skip)
1.89      nicm     1122:                grid_view_set_cell(gd, s->cx, s->cy, gc);
1.1       nicm     1123:
1.64      nicm     1124:        /*
                   1125:         * Move the cursor. If not wrapping, stick at the last character and
                   1126:         * replace it.
                   1127:         */
1.65      nicm     1128:        last = !(s->mode & MODE_WRAP);
1.92      nicm     1129:        if (s->cx <= sx - last - width)
1.64      nicm     1130:                s->cx += width;
                   1131:        else
1.92      nicm     1132:                s->cx = sx - last;
1.1       nicm     1133:
1.89      nicm     1134:        /* Create space for character in insert mode. */
1.17      nicm     1135:        if (insert) {
1.92      nicm     1136:                if (!wrapped)
                   1137:                        screen_write_flush(ctx);
1.17      nicm     1138:                ttyctx.num = width;
                   1139:                tty_write(tty_cmd_insertcharacter, &ttyctx);
                   1140:        }
1.89      nicm     1141:
                   1142:        /* Write to the screen. */
                   1143:        if (selected) {
1.33      nicm     1144:                memcpy(&tmp_gc, &s->sel.cell, sizeof tmp_gc);
1.77      nicm     1145:                utf8_copy(&tmp_gc.data, &gc->data);
1.70      nicm     1146:                tmp_gc.attr = tmp_gc.attr & ~GRID_ATTR_CHARSET;
                   1147:                tmp_gc.attr |= gc->attr & GRID_ATTR_CHARSET;
1.85      nicm     1148:                tmp_gc.flags = gc->flags;
1.92      nicm     1149:                screen_write_flush(ctx);
1.33      nicm     1150:                ttyctx.cell = &tmp_gc;
1.16      nicm     1151:                tty_write(tty_cmd_cell, &ttyctx);
1.92      nicm     1152:                ctx->written++;
1.89      nicm     1153:        } else if (!skip) {
1.92      nicm     1154:                if (wrapped) {
                   1155:                        ttyctx.cell = gc;
                   1156:                        tty_write(tty_cmd_cell, &ttyctx);
                   1157:                        ctx->written++;
                   1158:                } else {
                   1159:                        /*
                   1160:                         * If wp is NULL, we are not updating the terminal and
                   1161:                         * don't care about actually writing the cells
                   1162:                         * (tty_write will just return). So don't even bother
                   1163:                         * allocating the dirty array.
                   1164:                         */
                   1165:                        if (ctx->wp != NULL && s->dirty == NULL) {
                   1166:                                log_debug("%s: allocating %u bits", __func__,
                   1167:                                    s->dirtysize);
                   1168:                                s->dirty = bit_alloc(s->dirtysize);
                   1169:                        }
                   1170:                        if (s->dirty != NULL) {
                   1171:                                bit_set(s->dirty, screen_dirty_bit(s,
                   1172:                                    ttyctx.ocx, ttyctx.ocy));
                   1173:                                ctx->dirty++;
                   1174:                        }
                   1175:                }
                   1176:        } else
                   1177:                ctx->skipped++;
1.35      nicm     1178: }
                   1179:
                   1180: /* Combine a UTF-8 zero-width character onto the previous. */
1.87      nicm     1181: static int
1.60      nicm     1182: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud)
1.35      nicm     1183: {
                   1184:        struct screen           *s = ctx->s;
                   1185:        struct grid             *gd = s->grid;
1.77      nicm     1186:        struct grid_cell         gc;
1.35      nicm     1187:
                   1188:        /* Can't combine if at 0. */
                   1189:        if (s->cx == 0)
                   1190:                return (-1);
                   1191:
1.60      nicm     1192:        /* Empty data is out. */
                   1193:        if (ud->size == 0)
1.37      nicm     1194:                fatalx("UTF-8 data empty");
                   1195:
1.60      nicm     1196:        /* Retrieve the previous cell. */
1.77      nicm     1197:        grid_view_get_cell(gd, s->cx - 1, s->cy, &gc);
1.35      nicm     1198:
1.60      nicm     1199:        /* Check there is enough space. */
1.77      nicm     1200:        if (gc.data.size + ud->size > sizeof gc.data.data)
1.60      nicm     1201:                return (-1);
1.35      nicm     1202:
1.77      nicm     1203:        /* Append the data. */
                   1204:        memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
                   1205:        gc.data.size += ud->size;
                   1206:
                   1207:        /* Set the new cell. */
                   1208:        grid_view_set_cell(gd, s->cx - 1, s->cy, &gc);
1.35      nicm     1209:
                   1210:        return (0);
1.1       nicm     1211: }
                   1212:
                   1213: /*
                   1214:  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
                   1215:  * cell on the screen, so following cells must not be drawn by marking them as
                   1216:  * padding.
                   1217:  *
                   1218:  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
                   1219:  * character, it is necessary to also overwrite any other cells which covered
                   1220:  * by the same character.
                   1221:  */
1.89      nicm     1222: static int
                   1223: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
                   1224:     u_int width)
1.1       nicm     1225: {
                   1226:        struct screen           *s = ctx->s;
                   1227:        struct grid             *gd = s->grid;
1.89      nicm     1228:        struct grid_cell         tmp_gc;
1.1       nicm     1229:        u_int                    xx;
1.89      nicm     1230:        int                      done = 0;
1.1       nicm     1231:
1.89      nicm     1232:        if (gc->flags & GRID_FLAG_PADDING) {
1.1       nicm     1233:                /*
                   1234:                 * A padding cell, so clear any following and leading padding
                   1235:                 * cells back to the character. Don't overwrite the current
                   1236:                 * cell as that happens later anyway.
                   1237:                 */
                   1238:                xx = s->cx + 1;
                   1239:                while (--xx > 0) {
1.89      nicm     1240:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1241:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1       nicm     1242:                                break;
                   1243:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1244:                }
                   1245:
                   1246:                /* Overwrite the character at the start of this padding. */
                   1247:                grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89      nicm     1248:                done = 1;
1.43      nicm     1249:        }
1.1       nicm     1250:
1.43      nicm     1251:        /*
1.95      nicm     1252:         * Overwrite any padding cells that belong to any UTF-8 characters
                   1253:         * we'll be overwriting with the current character.
1.43      nicm     1254:         */
1.95      nicm     1255:        if (width != 1 ||
                   1256:            gc->data.width != 1 ||
                   1257:            gc->flags & GRID_FLAG_PADDING) {
1.89      nicm     1258:                xx = s->cx + width - 1;
                   1259:                while (++xx < screen_size_x(s)) {
                   1260:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1261:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
                   1262:                                break;
                   1263:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1264:                        done = 1;
                   1265:                }
1.1       nicm     1266:        }
1.89      nicm     1267:
                   1268:        return (done);
1.50      nicm     1269: }
                   1270:
                   1271: void
                   1272: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1273: {
                   1274:        struct tty_ctx  ttyctx;
                   1275:
1.87      nicm     1276:        screen_write_initctx(ctx, &ttyctx);
1.50      nicm     1277:        ttyctx.ptr = str;
                   1278:        ttyctx.num = len;
                   1279:
                   1280:        tty_write(tty_cmd_setselection, &ttyctx);
1.47      nicm     1281: }
                   1282:
                   1283: void
                   1284: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1285: {
1.87      nicm     1286:        struct tty_ctx  ttyctx;
1.47      nicm     1287:
1.87      nicm     1288:        screen_write_initctx(ctx, &ttyctx);
1.47      nicm     1289:        ttyctx.ptr = str;
                   1290:        ttyctx.num = len;
                   1291:
                   1292:        tty_write(tty_cmd_rawstring, &ttyctx);
1.1       nicm     1293: }