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

1.150   ! nicm        1: /* $OpenBSD: screen-write.c,v 1.149 2019/03/18 20:53:33 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 *);
1.109     nicm       28: static void    screen_write_collect_clear(struct screen_write_ctx *, u_int,
                     29:                    u_int);
                     30: static void    screen_write_collect_scroll(struct screen_write_ctx *);
1.111     nicm       31: static void    screen_write_collect_flush(struct screen_write_ctx *, int);
1.87      nicm       32:
1.89      nicm       33: static int     screen_write_overwrite(struct screen_write_ctx *,
                     34:                    struct grid_cell *, u_int);
1.104     nicm       35: static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
                     36:                    const struct utf8_data *, u_int *);
1.1       nicm       37:
1.88      nicm       38: static const struct grid_cell screen_write_pad_cell = {
1.91      nicm       39:        GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
1.88      nicm       40: };
                     41:
1.109     nicm       42: struct screen_write_collect_item {
                     43:        u_int                    x;
1.118     nicm       44:        int                      wrapped;
1.109     nicm       45:
                     46:        u_int                    used;
                     47:        char                     data[256];
                     48:
                     49:        struct grid_cell         gc;
                     50:
1.126     nicm       51:        TAILQ_ENTRY(screen_write_collect_item) entry;
1.109     nicm       52: };
                     53: struct screen_write_collect_line {
                     54:        TAILQ_HEAD(, screen_write_collect_item) items;
                     55: };
1.92      nicm       56:
1.139     nicm       57: static void
                     58: screen_write_offset_timer(__unused int fd, __unused short events, void *data)
                     59: {
                     60:        struct window   *w = data;
                     61:
                     62:        tty_update_window_offset(w);
                     63: }
                     64:
                     65: /* Set cursor position. */
                     66: static void
                     67: screen_write_set_cursor(struct screen_write_ctx *ctx, int cx, int cy)
                     68: {
                     69:        struct window_pane      *wp = ctx->wp;
                     70:        struct window           *w;
                     71:        struct screen           *s = ctx->s;
                     72:        struct timeval           tv = { .tv_usec = 10000 };
                     73:
                     74:        if (cx != -1 && (u_int)cx == s->cx && cy != -1 && (u_int)cy == s->cy)
                     75:                return;
                     76:
1.144     nicm       77:        if (cx != -1) {
1.145     nicm       78:                if ((u_int)cx > screen_size_x(s)) /* allow last column */
1.144     nicm       79:                        cx = screen_size_x(s) - 1;
1.139     nicm       80:                s->cx = cx;
1.144     nicm       81:        }
                     82:        if (cy != -1) {
                     83:                if ((u_int)cy > screen_size_y(s) - 1)
                     84:                        cy = screen_size_y(s) - 1;
1.139     nicm       85:                s->cy = cy;
1.144     nicm       86:        }
1.139     nicm       87:
                     88:        if (wp == NULL)
                     89:                return;
                     90:        w = wp->window;
                     91:
                     92:        if (!event_initialized(&w->offset_timer))
                     93:                evtimer_set(&w->offset_timer, screen_write_offset_timer, w);
                     94:        if (!evtimer_pending(&w->offset_timer, NULL))
                     95:                evtimer_add(&w->offset_timer, &tv);
                     96: }
                     97:
1.92      nicm       98: /* Initialize writing with a window. */
1.1       nicm       99: void
1.71      nicm      100: screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
                    101:     struct screen *s)
1.1       nicm      102: {
1.139     nicm      103:        char    tmp[32];
1.109     nicm      104:        u_int   y;
                    105:
                    106:        memset(ctx, 0, sizeof *ctx);
1.92      nicm      107:
1.1       nicm      108:        ctx->wp = wp;
                    109:        if (wp != NULL && s == NULL)
                    110:                ctx->s = wp->screen;
                    111:        else
                    112:                ctx->s = s;
1.92      nicm      113:
1.109     nicm      114:        ctx->list = xcalloc(screen_size_y(ctx->s), sizeof *ctx->list);
                    115:        for (y = 0; y < screen_size_y(ctx->s); y++)
                    116:                TAILQ_INIT(&ctx->list[y].items);
                    117:        ctx->item = xcalloc(1, sizeof *ctx->item);
1.92      nicm      118:
1.122     nicm      119:        ctx->scrolled = 0;
                    120:        ctx->bg = 8;
                    121:
1.139     nicm      122:        if (wp != NULL) {
                    123:                snprintf(tmp, sizeof tmp, "pane %%%u (at %u,%u)", wp->id,
                    124:                    wp->xoff, wp->yoff);
                    125:        }
1.92      nicm      126:        log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
1.108     nicm      127:            screen_size_y(ctx->s), wp == NULL ? "no pane" : tmp);
1.1       nicm      128: }
                    129:
                    130: /* Finish writing. */
                    131: void
1.92      nicm      132: screen_write_stop(struct screen_write_ctx *ctx)
                    133: {
1.109     nicm      134:        screen_write_collect_end(ctx);
1.111     nicm      135:        screen_write_collect_flush(ctx, 0);
1.92      nicm      136:
1.109     nicm      137:        log_debug("%s: %u cells (%u written, %u skipped)", __func__,
                    138:            ctx->cells, ctx->written, ctx->skipped);
1.92      nicm      139:
1.109     nicm      140:        free(ctx->item);
                    141:        free(ctx->list); /* flush will have emptied */
1.52      nicm      142: }
                    143:
                    144: /* Reset screen state. */
                    145: void
                    146: screen_write_reset(struct screen_write_ctx *ctx)
                    147: {
1.61      nicm      148:        struct screen   *s = ctx->s;
1.52      nicm      149:
1.61      nicm      150:        screen_reset_tabs(s);
                    151:        screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63      nicm      152:
1.141     nicm      153:        s->mode = MODE_CURSOR | MODE_WRAP;
1.52      nicm      154:
1.99      nicm      155:        screen_write_clearscreen(ctx, 8);
1.144     nicm      156:        screen_write_set_cursor(ctx, 0, 0);
1.1       nicm      157: }
                    158:
                    159: /* Write character. */
                    160: void
1.86      nicm      161: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69      nicm      162:     u_char ch)
1.1       nicm      163: {
1.86      nicm      164:        struct grid_cell        gc;
                    165:
                    166:        memcpy(&gc, gcp, sizeof gc);
                    167:
                    168:        utf8_set(&gc.data, ch);
                    169:        screen_write_cell(ctx, &gc);
1.1       nicm      170: }
                    171:
1.2       nicm      172: /* Calculate string length. */
1.71      nicm      173: size_t
1.75      nicm      174: screen_write_strlen(const char *fmt, ...)
1.2       nicm      175: {
1.36      nicm      176:        va_list                 ap;
                    177:        char                   *msg;
1.76      nicm      178:        struct utf8_data        ud;
1.36      nicm      179:        u_char                 *ptr;
                    180:        size_t                  left, size = 0;
1.79      nicm      181:        enum utf8_state         more;
1.2       nicm      182:
                    183:        va_start(ap, fmt);
                    184:        xvasprintf(&msg, fmt, ap);
                    185:        va_end(ap);
                    186:
                    187:        ptr = msg;
                    188:        while (*ptr != '\0') {
1.79      nicm      189:                if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36      nicm      190:                        ptr++;
1.2       nicm      191:
                    192:                        left = strlen(ptr);
1.77      nicm      193:                        if (left < (size_t)ud.size - 1)
1.36      nicm      194:                                break;
1.79      nicm      195:                        while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2       nicm      196:                                ptr++;
1.36      nicm      197:                        ptr++;
                    198:
1.79      nicm      199:                        if (more == UTF8_DONE)
1.78      nicm      200:                                size += ud.width;
1.2       nicm      201:                } else {
1.75      nicm      202:                        if (*ptr > 0x1f && *ptr < 0x7f)
                    203:                                size++;
1.2       nicm      204:                        ptr++;
                    205:                }
1.7       ray       206:        }
1.2       nicm      207:
1.56      nicm      208:        free(msg);
1.2       nicm      209:        return (size);
                    210: }
                    211:
1.3       nicm      212: /* Write simple string (no UTF-8 or maximum length). */
1.71      nicm      213: void
1.86      nicm      214: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71      nicm      215:     const char *fmt, ...)
1.1       nicm      216: {
                    217:        va_list ap;
                    218:
                    219:        va_start(ap, fmt);
1.86      nicm      220:        screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2       nicm      221:        va_end(ap);
                    222: }
                    223:
                    224: /* Write string with length limit (-1 for unlimited). */
1.71      nicm      225: void
                    226: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      227:     const struct grid_cell *gcp, const char *fmt, ...)
1.2       nicm      228: {
                    229:        va_list ap;
                    230:
                    231:        va_start(ap, fmt);
1.86      nicm      232:        screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2       nicm      233:        va_end(ap);
                    234: }
                    235:
                    236: void
1.3       nicm      237: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      238:     const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2       nicm      239: {
1.86      nicm      240:        struct grid_cell        gc;
                    241:        struct utf8_data       *ud = &gc.data;
1.36      nicm      242:        char                   *msg;
                    243:        u_char                 *ptr;
                    244:        size_t                  left, size = 0;
1.79      nicm      245:        enum utf8_state         more;
1.2       nicm      246:
1.86      nicm      247:        memcpy(&gc, gcp, sizeof gc);
1.1       nicm      248:        xvasprintf(&msg, fmt, ap);
                    249:
1.2       nicm      250:        ptr = msg;
                    251:        while (*ptr != '\0') {
1.86      nicm      252:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      253:                        ptr++;
1.2       nicm      254:
                    255:                        left = strlen(ptr);
1.86      nicm      256:                        if (left < (size_t)ud->size - 1)
1.36      nicm      257:                                break;
1.86      nicm      258:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2       nicm      259:                                ptr++;
1.36      nicm      260:                        ptr++;
1.7       ray       261:
1.86      nicm      262:                        if (more != UTF8_DONE)
                    263:                                continue;
                    264:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    265:                                while (size < (size_t)maxlen) {
                    266:                                        screen_write_putc(ctx, &gc, ' ');
                    267:                                        size++;
1.2       nicm      268:                                }
1.86      nicm      269:                                break;
1.2       nicm      270:                        }
1.86      nicm      271:                        size += ud->width;
                    272:                        screen_write_cell(ctx, &gc);
1.2       nicm      273:                } else {
1.86      nicm      274:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2       nicm      275:                                break;
                    276:
1.57      nicm      277:                        if (*ptr == '\001')
1.86      nicm      278:                                gc.attr ^= GRID_ATTR_CHARSET;
1.75      nicm      279:                        else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57      nicm      280:                                size++;
1.86      nicm      281:                                screen_write_putc(ctx, &gc, *ptr);
1.75      nicm      282:                        }
1.24      nicm      283:                        ptr++;
                    284:                }
                    285:        }
                    286:
1.56      nicm      287:        free(msg);
1.1       nicm      288: }
                    289:
1.132     nicm      290: /* Copy from another screen. Assumes target region is big enough. */
1.1       nicm      291: void
1.95      nicm      292: screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
1.132     nicm      293:     u_int py, u_int nx, u_int ny, bitstr_t *mbs, const struct grid_cell *mgc)
1.1       nicm      294: {
                    295:        struct screen           *s = ctx->s;
                    296:        struct grid             *gd = src->grid;
1.77      nicm      297:        struct grid_cell         gc;
1.102     nicm      298:        u_int                    xx, yy, cx, cy, b;
1.1       nicm      299:
1.125     nicm      300:        if (nx == 0 || ny == 0)
                    301:                return;
                    302:
1.1       nicm      303:        cx = s->cx;
                    304:        cy = s->cy;
1.96      nicm      305:
1.1       nicm      306:        for (yy = py; yy < py + ny; yy++) {
1.96      nicm      307:                for (xx = px; xx < px + nx; xx++) {
                    308:                        grid_get_cell(gd, xx, yy, &gc);
1.132     nicm      309:                        if (mbs != NULL) {
1.102     nicm      310:                                b = (yy * screen_size_x(src)) + xx;
1.132     nicm      311:                                if (bit_test(mbs, b)) {
                    312:                                        gc.attr = mgc->attr;
                    313:                                        gc.fg = mgc->fg;
                    314:                                        gc.bg = mgc->bg;
1.102     nicm      315:                                }
                    316:                        }
1.135     nicm      317:                        if (xx + gc.data.width <= px + nx)
                    318:                                screen_write_cell(ctx, &gc);
1.96      nicm      319:                }
1.132     nicm      320:                cy++;
1.144     nicm      321:                screen_write_set_cursor(ctx, cx, cy);
1.132     nicm      322:        }
                    323: }
                    324:
                    325: /*
                    326:  * Copy from another screen but without the selection stuff. Also assumes the
1.150   ! nicm      327:  * target region is already big enough.
1.132     nicm      328:  */
                    329: void
                    330: screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
                    331:     u_int px, u_int py, u_int nx, u_int ny)
                    332: {
                    333:        struct screen           *s = ctx->s;
                    334:        struct grid             *gd = src->grid;
                    335:        struct grid_cell         gc;
                    336:        u_int                    xx, yy, cx, cy;
1.96      nicm      337:
1.132     nicm      338:        if (nx == 0 || ny == 0)
                    339:                return;
                    340:
                    341:        cy = s->cy;
                    342:        for (yy = py; yy < py + ny; yy++) {
1.134     nicm      343:                if (yy >= gd->hsize + gd->sy)
                    344:                        break;
1.132     nicm      345:                cx = s->cx;
                    346:                for (xx = px; xx < px + nx; xx++) {
1.137     nicm      347:                        if (xx >= grid_get_line(gd, yy)->cellsize)
1.132     nicm      348:                                break;
                    349:                        grid_get_cell(gd, xx, yy, &gc);
1.135     nicm      350:                        if (xx + gc.data.width > px + nx)
                    351:                                break;
1.150   ! nicm      352:                        grid_view_set_cell(ctx->s->grid, cx, cy, &gc);
1.132     nicm      353:                        cx++;
                    354:                }
1.1       nicm      355:                cy++;
1.125     nicm      356:        }
                    357: }
                    358:
1.129     nicm      359: /* Draw a horizontal line on screen. */
1.125     nicm      360: void
1.129     nicm      361: screen_write_hline(struct screen_write_ctx *ctx, u_int nx, int left, int right)
1.125     nicm      362: {
                    363:        struct screen           *s = ctx->s;
                    364:        struct grid_cell         gc;
                    365:        u_int                    cx, cy, i;
                    366:
                    367:        cx = s->cx;
                    368:        cy = s->cy;
                    369:
                    370:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    371:        gc.attr |= GRID_ATTR_CHARSET;
                    372:
                    373:        screen_write_putc(ctx, &gc, left ? 't' : 'q');
                    374:        for (i = 1; i < nx - 1; i++)
                    375:                screen_write_putc(ctx, &gc, 'q');
                    376:        screen_write_putc(ctx, &gc, right ? 'u' : 'q');
1.129     nicm      377:
1.144     nicm      378:        screen_write_set_cursor(ctx, cx, cy);
1.129     nicm      379: }
                    380:
                    381: /* Draw a horizontal line on screen. */
                    382: void
1.130     nicm      383: screen_write_vline(struct screen_write_ctx *ctx, u_int ny, int top, int bottom)
1.129     nicm      384: {
                    385:        struct screen           *s = ctx->s;
                    386:        struct grid_cell         gc;
                    387:        u_int                    cx, cy, i;
                    388:
                    389:        cx = s->cx;
                    390:        cy = s->cy;
                    391:
                    392:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    393:        gc.attr |= GRID_ATTR_CHARSET;
                    394:
                    395:        screen_write_putc(ctx, &gc, top ? 'w' : 'x');
                    396:        for (i = 1; i < ny - 1; i++) {
1.144     nicm      397:                screen_write_set_cursor(ctx, cx, cy + i);
1.129     nicm      398:                screen_write_putc(ctx, &gc, 'x');
                    399:        }
1.144     nicm      400:        screen_write_set_cursor(ctx, cx, cy + ny - 1);
1.129     nicm      401:        screen_write_putc(ctx, &gc, bottom ? 'v' : 'x');
1.125     nicm      402:
1.144     nicm      403:        screen_write_set_cursor(ctx, cx, cy);
1.125     nicm      404: }
                    405:
                    406: /* Draw a box on screen. */
                    407: void
                    408: screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
                    409: {
                    410:        struct screen           *s = ctx->s;
                    411:        struct grid_cell         gc;
                    412:        u_int                    cx, cy, i;
                    413:
                    414:        cx = s->cx;
                    415:        cy = s->cy;
                    416:
                    417:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    418:        gc.attr |= GRID_ATTR_CHARSET;
                    419:
                    420:        screen_write_putc(ctx, &gc, 'l');
                    421:        for (i = 1; i < nx - 1; i++)
                    422:                screen_write_putc(ctx, &gc, 'q');
                    423:        screen_write_putc(ctx, &gc, 'k');
                    424:
1.144     nicm      425:        screen_write_set_cursor(ctx, cx, cy + ny - 1);
1.125     nicm      426:        screen_write_putc(ctx, &gc, 'm');
                    427:        for (i = 1; i < nx - 1; i++)
                    428:                screen_write_putc(ctx, &gc, 'q');
                    429:        screen_write_putc(ctx, &gc, 'j');
                    430:
                    431:        for (i = 1; i < ny - 1; i++) {
1.144     nicm      432:                screen_write_set_cursor(ctx, cx, cy + i);
1.125     nicm      433:                screen_write_putc(ctx, &gc, 'x');
                    434:        }
                    435:        for (i = 1; i < ny - 1; i++) {
1.144     nicm      436:                screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
1.125     nicm      437:                screen_write_putc(ctx, &gc, 'x');
                    438:        }
                    439:
1.144     nicm      440:        screen_write_set_cursor(ctx, cx, cy);
1.125     nicm      441: }
                    442:
1.132     nicm      443: /*
                    444:  * Write a preview version of a window. Assumes target area is big enough and
                    445:  * already cleared.
                    446:  */
1.125     nicm      447: void
                    448: screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
                    449:     u_int ny)
                    450: {
                    451:        struct screen           *s = ctx->s;
                    452:        struct grid_cell         gc;
                    453:        u_int                    cx, cy, px, py;
                    454:
                    455:        cx = s->cx;
                    456:        cy = s->cy;
                    457:
                    458:        /*
                    459:         * If the cursor is on, pick the area around the cursor, otherwise use
                    460:         * the top left.
                    461:         */
                    462:        if (src->mode & MODE_CURSOR) {
                    463:                px = src->cx;
                    464:                if (px < nx / 3)
                    465:                        px = 0;
                    466:                else
                    467:                        px = px - nx / 3;
                    468:                if (px + nx > screen_size_x(src)) {
                    469:                        if (nx > screen_size_x(src))
                    470:                                px = 0;
                    471:                        else
                    472:                                px = screen_size_x(src) - nx;
                    473:                }
                    474:                py = src->cy;
                    475:                if (py < ny / 3)
                    476:                        py = 0;
                    477:                else
                    478:                        py = py - ny / 3;
                    479:                if (py + ny > screen_size_y(src)) {
                    480:                        if (ny > screen_size_y(src))
                    481:                                py = 0;
                    482:                        else
                    483:                                py = screen_size_y(src) - ny;
                    484:                }
                    485:        } else {
                    486:                px = 0;
                    487:                py = 0;
                    488:        }
                    489:
1.132     nicm      490:        screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
1.125     nicm      491:
                    492:        if (src->mode & MODE_CURSOR) {
                    493:                grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
                    494:                gc.attr |= GRID_ATTR_REVERSE;
1.144     nicm      495:                screen_write_set_cursor(ctx, cx + (src->cx - px),
1.125     nicm      496:                    cy + (src->cy - py));
                    497:                screen_write_cell(ctx, &gc);
1.1       nicm      498:        }
                    499: }
                    500:
1.17      nicm      501: /* Set up context for TTY command. */
1.87      nicm      502: static void
                    503: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1       nicm      504: {
1.87      nicm      505:        struct screen   *s = ctx->s;
1.123     nicm      506:
                    507:        memset(ttyctx, 0, sizeof *ttyctx);
1.1       nicm      508:
1.17      nicm      509:        ttyctx->wp = ctx->wp;
1.1       nicm      510:
1.17      nicm      511:        ttyctx->ocx = s->cx;
                    512:        ttyctx->ocy = s->cy;
                    513:
                    514:        ttyctx->orlower = s->rlower;
                    515:        ttyctx->orupper = s->rupper;
1.87      nicm      516: }
1.31      nicm      517:
1.61      nicm      518: /* Set a mode. */
                    519: void
                    520: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
                    521: {
                    522:        struct screen   *s = ctx->s;
                    523:
                    524:        s->mode |= mode;
                    525: }
                    526:
                    527: /* Clear a mode. */
                    528: void
                    529: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
                    530: {
                    531:        struct screen   *s = ctx->s;
                    532:
                    533:        s->mode &= ~mode;
                    534: }
                    535:
1.1       nicm      536: /* Cursor up by ny. */
                    537: void
                    538: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
                    539: {
                    540:        struct screen   *s = ctx->s;
1.139     nicm      541:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      542:
                    543:        if (ny == 0)
                    544:                ny = 1;
                    545:
1.139     nicm      546:        if (cy < s->rupper) {
1.12      nicm      547:                /* Above region. */
1.139     nicm      548:                if (ny > cy)
                    549:                        ny = cy;
1.12      nicm      550:        } else {
                    551:                /* Below region. */
1.139     nicm      552:                if (ny > cy - s->rupper)
                    553:                        ny = cy - s->rupper;
1.12      nicm      554:        }
1.139     nicm      555:        if (cx == screen_size_x(s))
                    556:                cx--;
                    557:
                    558:        cy -= ny;
1.1       nicm      559:
1.139     nicm      560:        screen_write_set_cursor(ctx, cx, cy);
1.1       nicm      561: }
                    562:
                    563: /* Cursor down by ny. */
                    564: void
                    565: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
                    566: {
                    567:        struct screen   *s = ctx->s;
1.139     nicm      568:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      569:
                    570:        if (ny == 0)
                    571:                ny = 1;
                    572:
1.139     nicm      573:        if (cy > s->rlower) {
1.12      nicm      574:                /* Below region. */
1.139     nicm      575:                if (ny > screen_size_y(s) - 1 - cy)
                    576:                        ny = screen_size_y(s) - 1 - cy;
1.12      nicm      577:        } else {
                    578:                /* Above region. */
1.139     nicm      579:                if (ny > s->rlower - cy)
                    580:                        ny = s->rlower - cy;
1.12      nicm      581:        }
1.139     nicm      582:        if (cx == screen_size_x(s))
                    583:            cx--;
                    584:        else if (ny == 0)
1.1       nicm      585:                return;
                    586:
1.139     nicm      587:        cy += ny;
                    588:
                    589:        screen_write_set_cursor(ctx, cx, cy);
1.1       nicm      590: }
                    591:
1.101     nicm      592: /* Cursor right by nx. */
1.1       nicm      593: void
                    594: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
                    595: {
                    596:        struct screen   *s = ctx->s;
1.139     nicm      597:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      598:
                    599:        if (nx == 0)
                    600:                nx = 1;
                    601:
1.139     nicm      602:        if (nx > screen_size_x(s) - 1 - cx)
                    603:                nx = screen_size_x(s) - 1 - cx;
1.1       nicm      604:        if (nx == 0)
                    605:                return;
                    606:
1.139     nicm      607:        cx += nx;
                    608:
                    609:        screen_write_set_cursor(ctx, cx, cy);
1.1       nicm      610: }
                    611:
                    612: /* Cursor left by nx. */
                    613: void
                    614: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
                    615: {
                    616:        struct screen   *s = ctx->s;
1.139     nicm      617:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      618:
                    619:        if (nx == 0)
                    620:                nx = 1;
                    621:
1.139     nicm      622:        if (nx > cx)
                    623:                nx = cx;
1.1       nicm      624:        if (nx == 0)
                    625:                return;
                    626:
1.139     nicm      627:        cx -= nx;
                    628:
                    629:        screen_write_set_cursor(ctx, cx, cy);
1.5       nicm      630: }
                    631:
1.29      nicm      632: /* Backspace; cursor left unless at start of wrapped line when can move up. */
                    633: void
                    634: screen_write_backspace(struct screen_write_ctx *ctx)
                    635: {
                    636:        struct screen           *s = ctx->s;
                    637:        struct grid_line        *gl;
1.139     nicm      638:        u_int                    cx = s->cx, cy = s->cy;
1.29      nicm      639:
1.139     nicm      640:        if (cx == 0) {
                    641:                if (cy == 0)
1.29      nicm      642:                        return;
1.139     nicm      643:                gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
1.29      nicm      644:                if (gl->flags & GRID_LINE_WRAPPED) {
1.139     nicm      645:                        cy--;
                    646:                        cx = screen_size_x(s) - 1;
1.29      nicm      647:                }
                    648:        } else
1.139     nicm      649:                cx--;
                    650:
                    651:        screen_write_set_cursor(ctx, cx, cy);
1.29      nicm      652: }
                    653:
1.5       nicm      654: /* VT100 alignment test. */
                    655: void
                    656: screen_write_alignmenttest(struct screen_write_ctx *ctx)
                    657: {
                    658:        struct screen           *s = ctx->s;
1.17      nicm      659:        struct tty_ctx           ttyctx;
1.5       nicm      660:        struct grid_cell         gc;
                    661:        u_int                    xx, yy;
                    662:
                    663:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.77      nicm      664:        utf8_set(&gc.data, 'E');
1.7       ray       665:
1.5       nicm      666:        for (yy = 0; yy < screen_size_y(s); yy++) {
                    667:                for (xx = 0; xx < screen_size_x(s); xx++)
                    668:                        grid_view_set_cell(s->grid, xx, yy, &gc);
                    669:        }
1.7       ray       670:
1.139     nicm      671:        screen_write_set_cursor(ctx, 0, 0);
1.5       nicm      672:
                    673:        s->rupper = 0;
                    674:        s->rlower = screen_size_y(s) - 1;
1.143     nicm      675:
                    676:        screen_write_initctx(ctx, &ttyctx);
1.5       nicm      677:
1.109     nicm      678:        screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1.17      nicm      679:        tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1       nicm      680: }
                    681:
                    682: /* Insert nx characters. */
                    683: void
1.99      nicm      684: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, 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 (nx == 0)
                    690:                nx = 1;
                    691:
1.9       nicm      692:        if (nx > screen_size_x(s) - s->cx)
                    693:                nx = screen_size_x(s) - s->cx;
1.1       nicm      694:        if (nx == 0)
                    695:                return;
                    696:
1.107     nicm      697:        if (s->cx > screen_size_x(s) - 1)
                    698:                return;
                    699:
1.87      nicm      700:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      701:        ttyctx.bg = bg;
1.1       nicm      702:
1.107     nicm      703:        grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      704:
1.111     nicm      705:        screen_write_collect_flush(ctx, 0);
1.17      nicm      706:        ttyctx.num = nx;
                    707:        tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1       nicm      708: }
                    709:
                    710: /* Delete nx characters. */
                    711: void
1.99      nicm      712: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1       nicm      713: {
                    714:        struct screen   *s = ctx->s;
1.17      nicm      715:        struct tty_ctx   ttyctx;
1.1       nicm      716:
                    717:        if (nx == 0)
                    718:                nx = 1;
                    719:
1.9       nicm      720:        if (nx > screen_size_x(s) - s->cx)
                    721:                nx = screen_size_x(s) - s->cx;
1.1       nicm      722:        if (nx == 0)
                    723:                return;
                    724:
1.107     nicm      725:        if (s->cx > screen_size_x(s) - 1)
                    726:                return;
                    727:
1.87      nicm      728:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      729:        ttyctx.bg = bg;
1.1       nicm      730:
1.107     nicm      731:        grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      732:
1.111     nicm      733:        screen_write_collect_flush(ctx, 0);
1.17      nicm      734:        ttyctx.num = nx;
                    735:        tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59      nicm      736: }
                    737:
                    738: /* Clear nx characters. */
                    739: void
1.121     nicm      740: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.59      nicm      741: {
                    742:        struct screen   *s = ctx->s;
                    743:        struct tty_ctx   ttyctx;
                    744:
                    745:        if (nx == 0)
                    746:                nx = 1;
                    747:
                    748:        if (nx > screen_size_x(s) - s->cx)
                    749:                nx = screen_size_x(s) - s->cx;
                    750:        if (nx == 0)
                    751:                return;
                    752:
1.107     nicm      753:        if (s->cx > screen_size_x(s) - 1)
                    754:                return;
                    755:
1.87      nicm      756:        screen_write_initctx(ctx, &ttyctx);
1.121     nicm      757:        ttyctx.bg = bg;
1.59      nicm      758:
1.124     nicm      759:        grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1.59      nicm      760:
1.111     nicm      761:        screen_write_collect_flush(ctx, 0);
1.59      nicm      762:        ttyctx.num = nx;
                    763:        tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1       nicm      764: }
                    765:
                    766: /* Insert ny lines. */
                    767: void
1.99      nicm      768: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      769: {
                    770:        struct screen   *s = ctx->s;
1.107     nicm      771:        struct grid     *gd = s->grid;
1.17      nicm      772:        struct tty_ctx   ttyctx;
1.1       nicm      773:
                    774:        if (ny == 0)
                    775:                ny = 1;
                    776:
1.11      nicm      777:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    778:                if (ny > screen_size_y(s) - s->cy)
                    779:                        ny = screen_size_y(s) - s->cy;
                    780:                if (ny == 0)
                    781:                        return;
                    782:
1.87      nicm      783:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      784:                ttyctx.bg = bg;
1.11      nicm      785:
1.107     nicm      786:                grid_view_insert_lines(gd, s->cy, ny, bg);
1.11      nicm      787:
1.111     nicm      788:                screen_write_collect_flush(ctx, 0);
1.17      nicm      789:                ttyctx.num = ny;
                    790:                tty_write(tty_cmd_insertline, &ttyctx);
1.11      nicm      791:                return;
                    792:        }
                    793:
                    794:        if (ny > s->rlower + 1 - s->cy)
                    795:                ny = s->rlower + 1 - s->cy;
1.1       nicm      796:        if (ny == 0)
                    797:                return;
1.41      nicm      798:
1.87      nicm      799:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      800:        ttyctx.bg = bg;
1.1       nicm      801:
                    802:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      803:                grid_view_insert_lines(gd, s->cy, ny, bg);
                    804:        else
                    805:                grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      806:
1.111     nicm      807:        screen_write_collect_flush(ctx, 0);
1.17      nicm      808:        ttyctx.num = ny;
                    809:        tty_write(tty_cmd_insertline, &ttyctx);
1.1       nicm      810: }
                    811:
                    812: /* Delete ny lines. */
                    813: void
1.99      nicm      814: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      815: {
                    816:        struct screen   *s = ctx->s;
1.107     nicm      817:        struct grid     *gd = s->grid;
1.17      nicm      818:        struct tty_ctx   ttyctx;
1.1       nicm      819:
                    820:        if (ny == 0)
                    821:                ny = 1;
                    822:
1.11      nicm      823:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    824:                if (ny > screen_size_y(s) - s->cy)
                    825:                        ny = screen_size_y(s) - s->cy;
                    826:                if (ny == 0)
                    827:                        return;
                    828:
1.87      nicm      829:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      830:                ttyctx.bg = bg;
1.11      nicm      831:
1.107     nicm      832:                grid_view_delete_lines(gd, s->cy, ny, bg);
1.11      nicm      833:
1.111     nicm      834:                screen_write_collect_flush(ctx, 0);
1.17      nicm      835:                ttyctx.num = ny;
                    836:                tty_write(tty_cmd_deleteline, &ttyctx);
1.11      nicm      837:                return;
                    838:        }
1.41      nicm      839:
1.11      nicm      840:        if (ny > s->rlower + 1 - s->cy)
                    841:                ny = s->rlower + 1 - s->cy;
1.1       nicm      842:        if (ny == 0)
                    843:                return;
                    844:
1.87      nicm      845:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      846:        ttyctx.bg = bg;
1.1       nicm      847:
                    848:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      849:                grid_view_delete_lines(gd, s->cy, ny, bg);
                    850:        else
                    851:                grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      852:
1.111     nicm      853:        screen_write_collect_flush(ctx, 0);
1.17      nicm      854:        ttyctx.num = ny;
                    855:        tty_write(tty_cmd_deleteline, &ttyctx);
1.1       nicm      856: }
                    857:
                    858: /* Clear line at cursor. */
                    859: void
1.99      nicm      860: screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      861: {
1.92      nicm      862:        struct screen           *s = ctx->s;
                    863:        struct grid_line        *gl;
                    864:        struct tty_ctx           ttyctx;
                    865:        u_int                    sx = screen_size_x(s);
1.1       nicm      866:
1.137     nicm      867:        gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140     nicm      868:        if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1.92      nicm      869:                return;
1.1       nicm      870:
1.108     nicm      871:        screen_write_initctx(ctx, &ttyctx);
                    872:        ttyctx.bg = bg;
                    873:
1.99      nicm      874:        grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
                    875:
1.109     nicm      876:        screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      877:        screen_write_collect_flush(ctx, 0);
1.17      nicm      878:        tty_write(tty_cmd_clearline, &ttyctx);
1.1       nicm      879: }
                    880:
                    881: /* Clear to end of line from cursor. */
                    882: void
1.99      nicm      883: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      884: {
1.92      nicm      885:        struct screen           *s = ctx->s;
                    886:        struct grid_line        *gl;
                    887:        struct tty_ctx           ttyctx;
                    888:        u_int                    sx = screen_size_x(s);
1.1       nicm      889:
1.137     nicm      890:        gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140     nicm      891:        if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1.92      nicm      892:                return;
1.108     nicm      893:
                    894:        screen_write_initctx(ctx, &ttyctx);
                    895:        ttyctx.bg = bg;
1.1       nicm      896:
1.99      nicm      897:        grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
                    898:
1.109     nicm      899:        if (s->cx == 0)
                    900:                screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      901:        screen_write_collect_flush(ctx, 0);
1.41      nicm      902:        tty_write(tty_cmd_clearendofline, &ttyctx);
1.1       nicm      903: }
                    904:
                    905: /* Clear to start of line from cursor. */
                    906: void
1.99      nicm      907: screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      908: {
                    909:        struct screen   *s = ctx->s;
1.17      nicm      910:        struct tty_ctx   ttyctx;
1.92      nicm      911:        u_int            sx = screen_size_x(s);
1.1       nicm      912:
1.87      nicm      913:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm      914:        ttyctx.bg = bg;
1.1       nicm      915:
1.109     nicm      916:        if (s->cx > sx - 1)
1.99      nicm      917:                grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109     nicm      918:        else
1.99      nicm      919:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1       nicm      920:
1.109     nicm      921:        if (s->cx > sx - 1)
                    922:                screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      923:        screen_write_collect_flush(ctx, 0);
1.17      nicm      924:        tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1       nicm      925: }
                    926:
1.101     nicm      927: /* Move cursor to px,py. */
1.1       nicm      928: void
1.147     nicm      929: screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
                    930:     int origin)
1.1       nicm      931: {
                    932:        struct screen   *s = ctx->s;
                    933:
1.147     nicm      934:        if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1.146     nicm      935:                if ((u_int)py > s->rlower - s->rupper)
1.144     nicm      936:                        py = s->rlower;
                    937:                else
                    938:                        py += s->rupper;
                    939:        }
1.145     nicm      940:
1.146     nicm      941:        if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1.145     nicm      942:                px = screen_size_x(s) - 1;
1.146     nicm      943:        if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1.145     nicm      944:                py = screen_size_y(s) - 1;
1.1       nicm      945:
1.139     nicm      946:        screen_write_set_cursor(ctx, px, py);
1.1       nicm      947: }
                    948:
1.101     nicm      949: /* Reverse index (up with scroll). */
1.1       nicm      950: void
1.122     nicm      951: screen_write_reverseindex(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      952: {
                    953:        struct screen   *s = ctx->s;
1.17      nicm      954:        struct tty_ctx   ttyctx;
1.1       nicm      955:
1.87      nicm      956:        screen_write_initctx(ctx, &ttyctx);
1.122     nicm      957:        ttyctx.bg = bg;
1.1       nicm      958:
1.109     nicm      959:        if (s->cy == s->rupper)
1.122     nicm      960:                grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1.109     nicm      961:        else if (s->cy > 0)
1.139     nicm      962:                screen_write_set_cursor(ctx, -1, s->cy - 1);
1.1       nicm      963:
1.111     nicm      964:        screen_write_collect_flush(ctx, 0);
1.17      nicm      965:        tty_write(tty_cmd_reverseindex, &ttyctx);
1.1       nicm      966: }
                    967:
                    968: /* Set scroll region. */
                    969: void
1.83      nicm      970: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
                    971:     u_int rlower)
1.1       nicm      972: {
                    973:        struct screen   *s = ctx->s;
                    974:
                    975:        if (rupper > screen_size_y(s) - 1)
                    976:                rupper = screen_size_y(s) - 1;
                    977:        if (rlower > screen_size_y(s) - 1)
                    978:                rlower = screen_size_y(s) - 1;
1.13      nicm      979:        if (rupper >= rlower)   /* cannot be one line */
1.1       nicm      980:                return;
                    981:
1.111     nicm      982:        screen_write_collect_flush(ctx, 0);
1.110     nicm      983:
1.1       nicm      984:        /* Cursor moves to top-left. */
1.139     nicm      985:        screen_write_set_cursor(ctx, 0, 0);
1.1       nicm      986:
                    987:        s->rupper = rupper;
                    988:        s->rlower = rlower;
                    989: }
                    990:
1.34      nicm      991: /* Line feed. */
1.1       nicm      992: void
1.122     nicm      993: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1.1       nicm      994: {
1.20      nicm      995:        struct screen           *s = ctx->s;
1.109     nicm      996:        struct grid             *gd = s->grid;
1.20      nicm      997:        struct grid_line        *gl;
1.1       nicm      998:
1.137     nicm      999:        gl = grid_get_line(gd, gd->hsize + s->cy);
1.20      nicm     1000:        if (wrapped)
                   1001:                gl->flags |= GRID_LINE_WRAPPED;
1.73      nicm     1002:        else
                   1003:                gl->flags &= ~GRID_LINE_WRAPPED;
1.20      nicm     1004:
1.114     nicm     1005:        log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
                   1006:            s->rupper, s->rlower);
                   1007:
1.122     nicm     1008:        if (bg != ctx->bg) {
                   1009:                screen_write_collect_flush(ctx, 1);
                   1010:                ctx->bg = bg;
                   1011:        }
                   1012:
1.92      nicm     1013:        if (s->cy == s->rlower) {
1.122     nicm     1014:                grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.109     nicm     1015:                screen_write_collect_scroll(ctx);
1.110     nicm     1016:                ctx->scrolled++;
1.109     nicm     1017:        } else if (s->cy < screen_size_y(s) - 1)
1.139     nicm     1018:                screen_write_set_cursor(ctx, -1, s->cy + 1);
1.1       nicm     1019: }
                   1020:
1.110     nicm     1021: /* Scroll up. */
                   1022: void
1.122     nicm     1023: screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1.110     nicm     1024: {
                   1025:        struct screen   *s = ctx->s;
                   1026:        struct grid     *gd = s->grid;
                   1027:        u_int            i;
                   1028:
                   1029:        if (lines == 0)
                   1030:                lines = 1;
                   1031:        else if (lines > s->rlower - s->rupper + 1)
                   1032:                lines = s->rlower - s->rupper + 1;
                   1033:
1.122     nicm     1034:        if (bg != ctx->bg) {
                   1035:                screen_write_collect_flush(ctx, 1);
                   1036:                ctx->bg = bg;
                   1037:        }
                   1038:
1.110     nicm     1039:        for (i = 0; i < lines; i++) {
1.122     nicm     1040:                grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.110     nicm     1041:                screen_write_collect_scroll(ctx);
                   1042:        }
                   1043:        ctx->scrolled += lines;
                   1044: }
                   1045:
1.1       nicm     1046: /* Carriage return (cursor to start of line). */
                   1047: void
                   1048: screen_write_carriagereturn(struct screen_write_ctx *ctx)
                   1049: {
1.139     nicm     1050:        screen_write_set_cursor(ctx, 0, -1);
1.1       nicm     1051: }
                   1052:
                   1053: /* Clear to end of screen from cursor. */
                   1054: void
1.99      nicm     1055: screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1056: {
                   1057:        struct screen   *s = ctx->s;
1.107     nicm     1058:        struct grid     *gd = s->grid;
1.17      nicm     1059:        struct tty_ctx   ttyctx;
1.92      nicm     1060:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1061:
1.87      nicm     1062:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm     1063:        ttyctx.bg = bg;
1.1       nicm     1064:
1.46      nicm     1065:        /* Scroll into history if it is enabled and clearing entire screen. */
1.109     nicm     1066:        if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1.107     nicm     1067:                grid_view_clear_history(gd, bg);
1.109     nicm     1068:        else {
                   1069:                if (s->cx <= sx - 1)
1.107     nicm     1070:                        grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
                   1071:                grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1.46      nicm     1072:        }
1.1       nicm     1073:
1.109     nicm     1074:        screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1.111     nicm     1075:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1076:        tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1       nicm     1077: }
                   1078:
                   1079: /* Clear to start of screen. */
                   1080: void
1.105     nicm     1081: screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1082: {
                   1083:        struct screen   *s = ctx->s;
1.17      nicm     1084:        struct tty_ctx   ttyctx;
1.92      nicm     1085:        u_int            sx = screen_size_x(s);
1.1       nicm     1086:
1.87      nicm     1087:        screen_write_initctx(ctx, &ttyctx);
1.105     nicm     1088:        ttyctx.bg = bg;
1.1       nicm     1089:
1.109     nicm     1090:        if (s->cy > 0)
1.105     nicm     1091:                grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1.109     nicm     1092:        if (s->cx > sx - 1)
1.120     nicm     1093:                grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109     nicm     1094:        else
1.120     nicm     1095:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1       nicm     1096:
1.109     nicm     1097:        screen_write_collect_clear(ctx, 0, s->cy);
1.111     nicm     1098:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1099:        tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1       nicm     1100: }
                   1101:
                   1102: /* Clear entire screen. */
                   1103: void
1.99      nicm     1104: screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1105: {
                   1106:        struct screen   *s = ctx->s;
1.17      nicm     1107:        struct tty_ctx   ttyctx;
1.92      nicm     1108:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1109:
1.87      nicm     1110:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm     1111:        ttyctx.bg = bg;
1.1       nicm     1112:
1.46      nicm     1113:        /* Scroll into history if it is enabled. */
                   1114:        if (s->grid->flags & GRID_HISTORY)
1.99      nicm     1115:                grid_view_clear_history(s->grid, bg);
1.83      nicm     1116:        else
1.99      nicm     1117:                grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1.1       nicm     1118:
1.109     nicm     1119:        screen_write_collect_clear(ctx, 0, sy);
1.17      nicm     1120:        tty_write(tty_cmd_clearscreen, &ttyctx);
1.51      nicm     1121: }
                   1122:
                   1123: /* Clear entire history. */
                   1124: void
                   1125: screen_write_clearhistory(struct screen_write_ctx *ctx)
                   1126: {
                   1127:        struct screen   *s = ctx->s;
                   1128:        struct grid     *gd = s->grid;
                   1129:
1.99      nicm     1130:        grid_move_lines(gd, 0, gd->hsize, gd->sy, 8);
1.93      nicm     1131:        gd->hscrolled = gd->hsize = 0;
1.1       nicm     1132: }
                   1133:
1.109     nicm     1134: /* Clear a collected line. */
                   1135: static void
                   1136: screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
                   1137: {
                   1138:        struct screen_write_collect_item        *ci, *tmp;
                   1139:        u_int                                    i;
                   1140:        size_t                                   size;
                   1141:
                   1142:        for (i = y ; i < y + n; i++) {
                   1143:                if (TAILQ_EMPTY(&ctx->list[i].items))
                   1144:                        continue;
                   1145:                size = 0;
                   1146:                TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
                   1147:                        size += ci->used;
                   1148:                        TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
                   1149:                        free(ci);
                   1150:                }
                   1151:                ctx->skipped += size;
1.114     nicm     1152:                log_debug("%s: dropped %zu bytes (line %u)", __func__, size, i);
1.109     nicm     1153:        }
                   1154: }
                   1155:
                   1156: /* Scroll collected lines up. */
                   1157: static void
                   1158: screen_write_collect_scroll(struct screen_write_ctx *ctx)
                   1159: {
                   1160:        struct screen                           *s = ctx->s;
                   1161:        struct screen_write_collect_line        *cl;
                   1162:        u_int                                    y;
                   1163:
1.114     nicm     1164:        log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
                   1165:            s->rupper, s->rlower);
                   1166:
1.109     nicm     1167:        screen_write_collect_clear(ctx, s->rupper, 1);
                   1168:        for (y = s->rupper; y < s->rlower; y++) {
                   1169:                cl = &ctx->list[y + 1];
                   1170:                TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
                   1171:                TAILQ_INIT(&cl->items);
                   1172:        }
                   1173: }
                   1174:
                   1175: /* Flush collected lines. */
                   1176: static void
1.111     nicm     1177: screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only)
1.109     nicm     1178: {
                   1179:        struct screen                           *s = ctx->s;
                   1180:        struct screen_write_collect_item        *ci, *tmp;
1.116     nicm     1181:        u_int                                    y, cx, cy, items = 0;
1.109     nicm     1182:        struct tty_ctx                           ttyctx;
1.116     nicm     1183:        size_t                                   written = 0;
1.110     nicm     1184:
                   1185:        if (ctx->scrolled != 0) {
                   1186:                log_debug("%s: scrolled %u (region %u-%u)", __func__,
                   1187:                    ctx->scrolled, s->rupper, s->rlower);
                   1188:                if (ctx->scrolled > s->rlower - s->rupper + 1)
                   1189:                        ctx->scrolled = s->rlower - s->rupper + 1;
                   1190:
                   1191:                screen_write_initctx(ctx, &ttyctx);
                   1192:                ttyctx.num = ctx->scrolled;
1.122     nicm     1193:                ttyctx.bg = ctx->bg;
1.110     nicm     1194:                tty_write(tty_cmd_scrollup, &ttyctx);
                   1195:        }
                   1196:        ctx->scrolled = 0;
1.122     nicm     1197:        ctx->bg = 8;
                   1198:
1.111     nicm     1199:        if (scroll_only)
                   1200:                return;
1.109     nicm     1201:
                   1202:        cx = s->cx; cy = s->cy;
                   1203:        for (y = 0; y < screen_size_y(s); y++) {
                   1204:                TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
1.144     nicm     1205:                        screen_write_set_cursor(ctx, ci->x, y);
1.109     nicm     1206:                        screen_write_initctx(ctx, &ttyctx);
                   1207:                        ttyctx.cell = &ci->gc;
1.118     nicm     1208:                        ttyctx.wrapped = ci->wrapped;
1.109     nicm     1209:                        ttyctx.ptr = ci->data;
                   1210:                        ttyctx.num = ci->used;
                   1211:                        tty_write(tty_cmd_cells, &ttyctx);
1.116     nicm     1212:
                   1213:                        items++;
                   1214:                        written += ci->used;
1.109     nicm     1215:
                   1216:                        TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
                   1217:                        free(ci);
                   1218:                }
                   1219:        }
                   1220:        s->cx = cx; s->cy = cy;
1.116     nicm     1221:
                   1222:        log_debug("%s: flushed %u items (%zu bytes)", __func__, items, written);
                   1223:        ctx->written += written;
1.109     nicm     1224: }
                   1225:
                   1226: /* Finish and store collected cells. */
                   1227: void
                   1228: screen_write_collect_end(struct screen_write_ctx *ctx)
                   1229: {
                   1230:        struct screen                           *s = ctx->s;
                   1231:        struct screen_write_collect_item        *ci = ctx->item;
                   1232:        struct grid_cell                         gc;
1.131     nicm     1233:        u_int                                    xx;
1.109     nicm     1234:
                   1235:        if (ci->used == 0)
                   1236:                return;
                   1237:        ci->data[ci->used] = '\0';
                   1238:
                   1239:        ci->x = s->cx;
                   1240:        TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
                   1241:        ctx->item = xcalloc(1, sizeof *ctx->item);
                   1242:
                   1243:        log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
                   1244:            s->cy);
                   1245:
1.131     nicm     1246:        if (s->cx != 0) {
                   1247:                for (xx = s->cx; xx > 0; xx--) {
                   1248:                        grid_view_get_cell(s->grid, xx, s->cy, &gc);
                   1249:                        if (~gc.flags & GRID_FLAG_PADDING)
                   1250:                                break;
1.136     nicm     1251:                        grid_view_set_cell(s->grid, xx, s->cy,
                   1252:                            &grid_default_cell);
1.131     nicm     1253:                }
1.139     nicm     1254:                if (gc.data.width > 1) {
1.136     nicm     1255:                        grid_view_set_cell(s->grid, xx, s->cy,
                   1256:                            &grid_default_cell);
1.139     nicm     1257:                }
1.131     nicm     1258:        }
                   1259:
1.109     nicm     1260:        memcpy(&gc, &ci->gc, sizeof gc);
                   1261:        grid_view_set_cells(s->grid, s->cx, s->cy, &gc, ci->data, ci->used);
1.139     nicm     1262:        screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1.131     nicm     1263:
                   1264:        for (xx = s->cx; xx < screen_size_x(s); xx++) {
                   1265:                grid_view_get_cell(s->grid, xx, s->cy, &gc);
                   1266:                if (~gc.flags & GRID_FLAG_PADDING)
                   1267:                        break;
                   1268:                grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
                   1269:        }
1.109     nicm     1270: }
                   1271:
                   1272: /* Write cell data, collecting if necessary. */
                   1273: void
                   1274: screen_write_collect_add(struct screen_write_ctx *ctx,
                   1275:     const struct grid_cell *gc)
                   1276: {
                   1277:        struct screen                           *s = ctx->s;
                   1278:        struct screen_write_collect_item        *ci;
                   1279:        u_int                                    sx = screen_size_x(s);
                   1280:        int                                      collect;
                   1281:
                   1282:        /*
                   1283:         * Don't need to check that the attributes and whatnot are still the
1.116     nicm     1284:         * same - input_parse will end the collection when anything that isn't
                   1285:         * a plain character is encountered. Also nothing should make it here
                   1286:         * that isn't a single ASCII character.
1.109     nicm     1287:         */
                   1288:
                   1289:        collect = 1;
1.136     nicm     1290:        if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1.109     nicm     1291:                collect = 0;
                   1292:        else if (gc->attr & GRID_ATTR_CHARSET)
                   1293:                collect = 0;
                   1294:        else if (~s->mode & MODE_WRAP)
                   1295:                collect = 0;
                   1296:        else if (s->mode & MODE_INSERT)
                   1297:                collect = 0;
1.138     nicm     1298:        else if (s->sel != NULL)
1.109     nicm     1299:                collect = 0;
                   1300:        if (!collect) {
                   1301:                screen_write_collect_end(ctx);
1.116     nicm     1302:                screen_write_collect_flush(ctx, 0);
1.109     nicm     1303:                screen_write_cell(ctx, gc);
                   1304:                return;
                   1305:        }
                   1306:        ctx->cells++;
                   1307:
                   1308:        if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
                   1309:                screen_write_collect_end(ctx);
1.118     nicm     1310:        ci = ctx->item; /* may have changed */
                   1311:
1.109     nicm     1312:        if (s->cx > sx - 1) {
1.114     nicm     1313:                log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.118     nicm     1314:                ci->wrapped = 1;
1.122     nicm     1315:                screen_write_linefeed(ctx, 1, 8);
1.139     nicm     1316:                screen_write_set_cursor(ctx, 0, -1);
1.109     nicm     1317:        }
                   1318:
                   1319:        if (ci->used == 0)
                   1320:                memcpy(&ci->gc, gc, sizeof ci->gc);
                   1321:        ci->data[ci->used++] = gc->data.data[0];
                   1322:        if (ci->used == (sizeof ci->data) - 1)
                   1323:                screen_write_collect_end(ctx);
                   1324: }
                   1325:
1.1       nicm     1326: /* Write cell data. */
                   1327: void
1.60      nicm     1328: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1       nicm     1329: {
                   1330:        struct screen           *s = ctx->s;
                   1331:        struct grid             *gd = s->grid;
1.109     nicm     1332:        struct grid_line        *gl;
                   1333:        struct grid_cell_entry  *gce;
                   1334:        struct grid_cell         tmp_gc, now_gc;
1.15      nicm     1335:        struct tty_ctx           ttyctx;
1.92      nicm     1336:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.115     nicm     1337:        u_int                    width = gc->data.width, xx, last, cx, cy;
1.109     nicm     1338:        int                      selected, skip = 1;
1.1       nicm     1339:
1.109     nicm     1340:        /* Ignore padding cells. */
1.1       nicm     1341:        if (gc->flags & GRID_FLAG_PADDING)
                   1342:                return;
1.109     nicm     1343:        ctx->cells++;
1.32      nicm     1344:
1.109     nicm     1345:        /* If the width is zero, combine onto the previous character. */
1.1       nicm     1346:        if (width == 0) {
1.112     nicm     1347:                screen_write_collect_flush(ctx, 0);
1.104     nicm     1348:                if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1.115     nicm     1349:                        cx = s->cx; cy = s->cy;
1.144     nicm     1350:                        screen_write_set_cursor(ctx, xx, s->cy);
1.87      nicm     1351:                        screen_write_initctx(ctx, &ttyctx);
1.104     nicm     1352:                        ttyctx.cell = gc;
                   1353:                        tty_write(tty_cmd_cell, &ttyctx);
1.115     nicm     1354:                        s->cx = cx; s->cy = cy;
1.1       nicm     1355:                }
                   1356:                return;
                   1357:        }
1.112     nicm     1358:
                   1359:        /* Flush any existing scrolling. */
                   1360:        screen_write_collect_flush(ctx, 1);
1.1       nicm     1361:
1.109     nicm     1362:        /* If this character doesn't fit, ignore it. */
                   1363:        if ((~s->mode & MODE_WRAP) &&
                   1364:            width > 1 &&
                   1365:            (width > sx || (s->cx != sx && s->cx > sx - width)))
                   1366:                return;
                   1367:
1.6       nicm     1368:        /* If in insert mode, make space for the cells. */
1.98      nicm     1369:        if (s->mode & MODE_INSERT) {
1.109     nicm     1370:                grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
                   1371:                skip = 0;
                   1372:        }
1.6       nicm     1373:
1.20      nicm     1374:        /* Check this will fit on the current line and wrap if not. */
1.92      nicm     1375:        if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.128     nicm     1376:                log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.122     nicm     1377:                screen_write_linefeed(ctx, 1, 8);
1.139     nicm     1378:                screen_write_set_cursor(ctx, 0, -1);
1.128     nicm     1379:                screen_write_collect_flush(ctx, 1);
1.1       nicm     1380:        }
                   1381:
1.64      nicm     1382:        /* Sanity check cursor position. */
1.92      nicm     1383:        if (s->cx > sx - width || s->cy > sy - 1)
1.1       nicm     1384:                return;
1.106     nicm     1385:        screen_write_initctx(ctx, &ttyctx);
                   1386:
1.1       nicm     1387:        /* Handle overwriting of UTF-8 characters. */
1.137     nicm     1388:        gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.92      nicm     1389:        if (gl->flags & GRID_LINE_EXTENDED) {
                   1390:                grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
                   1391:                if (screen_write_overwrite(ctx, &now_gc, width))
                   1392:                        skip = 0;
                   1393:        }
1.1       nicm     1394:
                   1395:        /*
                   1396:         * If the new character is UTF-8 wide, fill in padding cells. Have
                   1397:         * already ensured there is enough room.
                   1398:         */
1.89      nicm     1399:        for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.131     nicm     1400:                log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1.88      nicm     1401:                grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89      nicm     1402:                skip = 0;
                   1403:        }
                   1404:
                   1405:        /* If no change, do not draw. */
1.92      nicm     1406:        if (skip) {
                   1407:                if (s->cx >= gl->cellsize)
                   1408:                        skip = grid_cells_equal(gc, &grid_default_cell);
                   1409:                else {
                   1410:                        gce = &gl->celldata[s->cx];
                   1411:                        if (gce->flags & GRID_FLAG_EXTENDED)
                   1412:                                skip = 0;
1.95      nicm     1413:                        else if (gc->flags != gce->flags)
1.92      nicm     1414:                                skip = 0;
                   1415:                        else if (gc->attr != gce->data.attr)
                   1416:                                skip = 0;
                   1417:                        else if (gc->fg != gce->data.fg)
                   1418:                                skip = 0;
                   1419:                        else if (gc->bg != gce->data.bg)
                   1420:                                skip = 0;
1.95      nicm     1421:                        else if (gc->data.width != 1)
                   1422:                                skip = 0;
1.109     nicm     1423:                        else if (gc->data.size != 1)
                   1424:                                skip = 0;
1.95      nicm     1425:                        else if (gce->data.data != gc->data.data[0])
1.92      nicm     1426:                                skip = 0;
                   1427:                }
                   1428:        }
1.1       nicm     1429:
1.127     nicm     1430:        /* Update the selected flag and set the cell. */
1.90      nicm     1431:        selected = screen_check_selection(s, s->cx, s->cy);
1.109     nicm     1432:        if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1433:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1434:                tmp_gc.flags |= GRID_FLAG_SELECTED;
                   1435:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109     nicm     1436:        } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1437:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1438:                tmp_gc.flags &= ~GRID_FLAG_SELECTED;
                   1439:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1440:        } else if (!skip)
1.89      nicm     1441:                grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109     nicm     1442:        if (selected)
                   1443:                skip = 0;
1.1       nicm     1444:
1.64      nicm     1445:        /*
                   1446:         * Move the cursor. If not wrapping, stick at the last character and
                   1447:         * replace it.
                   1448:         */
1.65      nicm     1449:        last = !(s->mode & MODE_WRAP);
1.92      nicm     1450:        if (s->cx <= sx - last - width)
1.139     nicm     1451:                screen_write_set_cursor(ctx, s->cx + width, -1);
1.64      nicm     1452:        else
1.139     nicm     1453:                screen_write_set_cursor(ctx,  sx - last, -1);
1.1       nicm     1454:
1.89      nicm     1455:        /* Create space for character in insert mode. */
1.109     nicm     1456:        if (s->mode & MODE_INSERT) {
1.113     nicm     1457:                screen_write_collect_flush(ctx, 0);
1.17      nicm     1458:                ttyctx.num = width;
                   1459:                tty_write(tty_cmd_insertcharacter, &ttyctx);
                   1460:        }
1.89      nicm     1461:
                   1462:        /* Write to the screen. */
1.109     nicm     1463:        if (!skip) {
                   1464:                if (selected) {
                   1465:                        screen_select_cell(s, &tmp_gc, gc);
                   1466:                        ttyctx.cell = &tmp_gc;
                   1467:                } else
                   1468:                        ttyctx.cell = gc;
1.16      nicm     1469:                tty_write(tty_cmd_cell, &ttyctx);
1.92      nicm     1470:                ctx->written++;
                   1471:        } else
                   1472:                ctx->skipped++;
1.35      nicm     1473: }
                   1474:
                   1475: /* Combine a UTF-8 zero-width character onto the previous. */
1.104     nicm     1476: static const struct grid_cell *
                   1477: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
                   1478:     u_int *xx)
1.35      nicm     1479: {
                   1480:        struct screen           *s = ctx->s;
                   1481:        struct grid             *gd = s->grid;
1.104     nicm     1482:        static struct grid_cell  gc;
                   1483:        u_int                    n;
1.35      nicm     1484:
                   1485:        /* Can't combine if at 0. */
                   1486:        if (s->cx == 0)
1.104     nicm     1487:                return (NULL);
1.35      nicm     1488:
1.60      nicm     1489:        /* Empty data is out. */
                   1490:        if (ud->size == 0)
1.37      nicm     1491:                fatalx("UTF-8 data empty");
                   1492:
1.60      nicm     1493:        /* Retrieve the previous cell. */
1.119     nicm     1494:        for (n = 1; n <= s->cx; n++) {
1.104     nicm     1495:                grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
                   1496:                if (~gc.flags & GRID_FLAG_PADDING)
                   1497:                        break;
                   1498:        }
1.119     nicm     1499:        if (n > s->cx)
1.104     nicm     1500:                return (NULL);
                   1501:        *xx = s->cx - n;
1.35      nicm     1502:
1.60      nicm     1503:        /* Check there is enough space. */
1.77      nicm     1504:        if (gc.data.size + ud->size > sizeof gc.data.data)
1.104     nicm     1505:                return (NULL);
                   1506:
                   1507:        log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
                   1508:            ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35      nicm     1509:
1.77      nicm     1510:        /* Append the data. */
                   1511:        memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
                   1512:        gc.data.size += ud->size;
                   1513:
                   1514:        /* Set the new cell. */
1.104     nicm     1515:        grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35      nicm     1516:
1.104     nicm     1517:        return (&gc);
1.1       nicm     1518: }
                   1519:
                   1520: /*
                   1521:  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
                   1522:  * cell on the screen, so following cells must not be drawn by marking them as
                   1523:  * padding.
                   1524:  *
                   1525:  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
                   1526:  * character, it is necessary to also overwrite any other cells which covered
                   1527:  * by the same character.
                   1528:  */
1.89      nicm     1529: static int
                   1530: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
                   1531:     u_int width)
1.1       nicm     1532: {
                   1533:        struct screen           *s = ctx->s;
                   1534:        struct grid             *gd = s->grid;
1.89      nicm     1535:        struct grid_cell         tmp_gc;
1.1       nicm     1536:        u_int                    xx;
1.89      nicm     1537:        int                      done = 0;
1.1       nicm     1538:
1.89      nicm     1539:        if (gc->flags & GRID_FLAG_PADDING) {
1.1       nicm     1540:                /*
                   1541:                 * A padding cell, so clear any following and leading padding
                   1542:                 * cells back to the character. Don't overwrite the current
                   1543:                 * cell as that happens later anyway.
                   1544:                 */
                   1545:                xx = s->cx + 1;
                   1546:                while (--xx > 0) {
1.89      nicm     1547:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1548:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1       nicm     1549:                                break;
1.131     nicm     1550:                        log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
1.1       nicm     1551:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1552:                }
                   1553:
                   1554:                /* Overwrite the character at the start of this padding. */
1.131     nicm     1555:                log_debug("%s: character at %u,%u", __func__, xx, s->cy);
1.1       nicm     1556:                grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89      nicm     1557:                done = 1;
1.43      nicm     1558:        }
1.1       nicm     1559:
1.43      nicm     1560:        /*
1.95      nicm     1561:         * Overwrite any padding cells that belong to any UTF-8 characters
                   1562:         * we'll be overwriting with the current character.
1.43      nicm     1563:         */
1.95      nicm     1564:        if (width != 1 ||
                   1565:            gc->data.width != 1 ||
                   1566:            gc->flags & GRID_FLAG_PADDING) {
1.89      nicm     1567:                xx = s->cx + width - 1;
                   1568:                while (++xx < screen_size_x(s)) {
                   1569:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1570:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
                   1571:                                break;
1.131     nicm     1572:                        log_debug("%s: overwrite at %u,%u", __func__, xx, s->cy);
1.89      nicm     1573:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1574:                        done = 1;
                   1575:                }
1.1       nicm     1576:        }
1.89      nicm     1577:
                   1578:        return (done);
1.50      nicm     1579: }
                   1580:
1.107     nicm     1581: /* Set external clipboard. */
1.50      nicm     1582: void
                   1583: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1584: {
                   1585:        struct tty_ctx  ttyctx;
                   1586:
1.87      nicm     1587:        screen_write_initctx(ctx, &ttyctx);
1.50      nicm     1588:        ttyctx.ptr = str;
                   1589:        ttyctx.num = len;
                   1590:
                   1591:        tty_write(tty_cmd_setselection, &ttyctx);
1.47      nicm     1592: }
                   1593:
1.107     nicm     1594: /* Write unmodified string. */
1.47      nicm     1595: void
                   1596: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1597: {
1.87      nicm     1598:        struct tty_ctx  ttyctx;
1.47      nicm     1599:
1.87      nicm     1600:        screen_write_initctx(ctx, &ttyctx);
1.47      nicm     1601:        ttyctx.ptr = str;
                   1602:        ttyctx.num = len;
                   1603:
                   1604:        tty_write(tty_cmd_rawstring, &ttyctx);
1.1       nicm     1605: }