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

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