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

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